ACCU Conference 2003, © Mark Bartosik, www.bugbrowser.com 1 An Introduction to Native Postmortem Debugging This talk contains x86 and OS and compiler specific.

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
10/6: Lecture Topics Procedure call Calling conventions The stack
Procedure Calls Prof. Sirer CS 316 Cornell University.
The University of Adelaide, School of Computer Science
The art of exploitation
Utilizing the GDB debugger to analyze programs Background and application.
CS 4284 Systems Capstone Godmar Back Linking and Loading.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Advanced topics in X86 assembly by Istvan Haller.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
CS 536 Spring Run-time organization Lecture 19.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Run time vs. Compile time
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Microprocessors Frame Pointers and the use of the –fomit-frame-pointer switch Feb 25th, 2002.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Run-time Environment and Program Organization
OllyDbg Debuger.
CS2422 Assembly Language & System Programming November 7, 2006.
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Chapter 2 Software Tools and Assembly Language Syntax.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Practical Malware Analysis Ch 8: Debugging Rev
Introduction to InfoSec – Recitation 2 Nir Krakowski (nirkrako at post.tau.ac.il) Itamar Gilad (itamargi at post.tau.ac.il)
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
6.828: PC hardware and x86 Frans Kaashoek
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2011.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
Part 3: Advanced Dynamic Analysis Chapter 8: Debugging.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Lecture-1 Compilation process
CIS250 OPERATING SYSTEMS Memory Management Since we share memory, we need to manage it Memory manager only sees the address A program counter value indicates.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Topic 2d High-Level languages and Systems Software
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Module 6: Debugging a Windows CE Image.  Overview Debug Zones IDE Debug Setup IDE Debug Commands Platform Builder Integrated Kernel Debugger Other Debugging.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Renesas Technology America Inc. 1 M16C Seminars Lab 3 Creating Projects Using HEW4 14 March 2005 M16C Seminars Lab 3 Creating Projects Using HEW4 Last.
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Compiler Construction Code Generation Activation Records
CNIT 127: Exploit Development Ch 8: Windows Overflows Part 1.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2014.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Instructions for test_function
Static and dynamic analysis of binaries
Computer Architecture and Assembly Language
143A: Principles of Operating Systems Lecture 4: Calling conventions
CS 5204 Operating Systems Linking and Loading Godmar Back.
Introduction to Compilers Tim Teitelbaum
Topic 2e High-Level languages and Systems Software
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
CS 4284 Systems Capstone Linking and Loading Godmar Back.
Assembly Language Programming II: C Compiler Calling Sequences
Understanding Program Address Space
10/4: Lecture Topics Overflow and underflow Logical operations
Multi-modules programming
CSC 497/583 Advanced Topics in Computer Security
Topic 2b ISA Support for High-Level Languages
By Hugues Leger / Intro to GDB debugger By Hugues Leger / 11/16/2019.
Presentation transcript:

ACCU Conference 2003, © Mark Bartosik, 1 An Introduction to Native Postmortem Debugging This talk contains x86 and OS and compiler specific details. Helps if you know a little assembler (understand “ push EAX ”) “I think it’s dead, Jim” Dr. L McCoy, Star date 3196 “Hex codes never lie” Mark Bartosik, 2003 AD

ACCU Conference 2003, © Mark Bartosik, 2 Motivation ? Software phases –Requirement –Design –Code –Test –Debug –Support & maintenance

ACCU Conference 2003, © Mark Bartosik, 3 Debug This! Call stack: Somedll! Dllcanunloadnow() Program! e() Program! () Program! () Program! a()

ACCU Conference 2003, © Mark Bartosik, 4 Importance of a Stack Trace Information on the stack: History of which functions executed what the program / thread was doing before it crashed Contents of automatic variables

ACCU Conference 2003, © Mark Bartosik, 5 Debugging without symbols

ACCU Conference 2003, © Mark Bartosik, 6 Debugging with symbols

ACCU Conference 2003, © Mark Bartosik, 7 Symbol file contents Addresses of functions, source code locations Stack decoding information Locations of variables Type information

ACCU Conference 2003, © Mark Bartosik, 8 Sources of symbols Symbol files -- the ideal source! (Generated by the compiler and linker -- keep them safe) Public exports __declspec(dllexport) a poor fallback C:\>dumpbin /exports your.dll Type libraries (only one debugger that I know uses these) See Improve Your Debugging by Generating Symbols from COM Type Libraries, Matt Pietrek MSJ March 1999.map files (limited human readable symbol files)

ACCU Conference 2003, © Mark Bartosik, 9 Symbol servers How many versions of kernel32.dll are there out there? How many versions of your.dll have you released? How should you organize your symbol files? Which symbol files should be on your debugger’s symbol path? A symbol server automatically provides the right symbols for the executable image. Microsoft provides one accessible via HTTP. Tip: Take a look at symchk.exe, symstore.exe, ships with windbg Tip: Add this to your debugger’s symbol path: SRV*c:\websymbols*

ACCU Conference 2003, © Mark Bartosik, 10 Symbol server support Visual Studio 5.0, 6.0 loads symbols only on startup Visual Studio 7.0 /.NET can load on demand, easy to configure Windbg 6.x has symbol server support Visual Studio 7.1 symchk /ie notepad.exe /s SRV*c:\temp*\\server\symbols symbol path SRV*\\hpgs088fs1\symbols\microsoft\fixed_cache; SRV*\\hpgs088fs1\symbols\microsoft\web_cache*

ACCU Conference 2003, © Mark Bartosik, 11 User / mini dump files Stacks Stacks + data sections (global variables) All accessible memory Operating system handles A photograph of a process or part of a process Microsoft: see MSDN MiniDumpWriteDump, and/or userdump, mscordmp cygwin: see dumper, and error_start environment variable aka “core files” Can be loaded into a debugger, but you need the module files Can be save by a debugger for later use

ACCU Conference 2003, © Mark Bartosik, 12 Anatomy of a function call int foo(char * arg1, int arg2); void bar() { int my_val = 27;. int retval = foo(“abcd”, my_val);. } int __stdcall foo(char * arg1, int arg2); void bar() { int my_val = 27;. int retval = foo(“abcd”, my_val);. }

ACCU Conference 2003, © Mark Bartosik, 13 __stdcall ? A binary level contract between caller and callee All arguments are passed on the stack (none are passed in registers) Order of passing is right to left Callee unwinds the stack Return value (if any) in EAX Core registers are preserved by callee Other conventions are __cdecl, __fastcall, __thiscall

ACCU Conference 2003, © Mark Bartosik, 14 retval = foo(“abcd”, my_val); compiler’s view mov eax, DWORD PTR _my_val$[ebp] push eax push OFFSET FLAT:$SG171 call ; foo mov DWORD PTR _retval$[ebp], eax c:\tmp>Cl /Fas /Od foo.cpp output in foo.asm

ACCU Conference 2003, © Mark Bartosik, 15 retval = foo(“abcd”, my_val); debugger’s view mov eax, [ebp-4] // store value of my_val in eax push eax // push arg2 onto stack push offset string “abcd” // push arg1 onto stack call foo // transfer control to foo mov DWORD PTR [ebp-8], eax // save return value in an automatic Stop on a break point Show disassembler

ACCU Conference 2003, © Mark Bartosik, 16 Generated call stack

ACCU Conference 2003, © Mark Bartosik, 17 The big picture What we have is a singly linked list of return addresses. With EBP pointing to the head Above each node are function parameters Below each node are automatics Between is temporary space

ACCU Conference 2003, © Mark Bartosik, 18 Optimized code i386 cpu is CISC not RISC, so registers are valuable and sparse. EBP is often reused for other purposes Result is a “fragmented” singly linked list critical() context: EIP = CODE_ADDR EBP = GOOD_FRAME

ACCU Conference 2003, © Mark Bartosik, 19 Modules list moduleaddress program.exe0x x0042BFFF your.dll0x x2002BFFF kernel32.dll0x77F x77F5EFFF ntdll.dll0x77F x77FBDFFF Tip: Always fix the “base address” of your dlls. Tip: Examine module layout with dumpbin /headers Code plus global data Code usually precedes data Any code or data for program.exe will be at an address in the range 0x to 0x0042BFFF

ACCU Conference 2003, © Mark Bartosik, 20 Recognizing values If you can identify the code addresses, you can find the nodes in a fragmented singly linked list Code addresses always point to the instruction after a call instruction

ACCU Conference 2003, © Mark Bartosik, 21 Laying false trails int bar() { int array[100]; //uninitialized // sub esp, 400 int bar() { int array[100] = {0};

ACCU Conference 2003, © Mark Bartosik, 22 Application: stack overflow Find the beginning of the stack, take ESP add the default stack length Search memory backwards from this address, until the address of func1 or func2 appear (repeatedly) Find the previous code address on the stack Use the debugger to look up the function name (disassemble, evaluate or watch it) Check if it could call func1 or func2 Instruct debugger to decode stack frame or decode it by hand

ACCU Conference 2003, © Mark Bartosik, 23 The most common cause of a short stack trace is missing symbol files. Make sure all symbol files are loaded. Especially for the last module listed on the stack ( kernel32 in this case) Find the last decoded code address Examine memory for the next potential code address Verify the code address. Disassemble the code at that location it should follow a “ call ” instruction Lookup the symbol Read the source code Application: short stack trace

ACCU Conference 2003, © Mark Bartosik, 24 Application: no stack trace EIP is corrupt, thus the crash, and EBP is probably useless Take ESP find the first potential code address on the stack Set the function context manually: set EIP to this value set EBP to the position of this code on the stack -4 Instruct the debugger to redisplay the call stack

ACCU Conference 2003, © Mark Bartosik, 25 Summary Symbolic debuggers need symbols! Use a symbol server and symbol cache Get familiar with more than one debugger Use mini dumps Code locations are readily identifiable on the stack Links and further reading John Robbins, Debugging Applications, Microsoft Press. Also his Bug Slayer columns MSJ/MSDN magazine. Various MSJ/MSDN magazine articles by Matt Pietrek How debuggers work, Rosenberg, Wiley microsoft.public.windbg microsoft.public.vc.debugger