Part 3: Advanced Dynamic Analysis

Slides:



Advertisements
Similar presentations
Utilizing the GDB debugger to analyze programs Background and application.
Advertisements

Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
RIVERSIDE RESEARCH INSTITUTE Helikaon Linux Debugger: A Stealthy Custom Debugger For Linux Jason Raber, Team Lead - Reverse Engineer.
Nullcon Goa 2010http://nullcon.net Intelligent Debugging and in-memory Fuzzers By Vishwas Sharma Amandeep Bharti Rohan Thakur.
Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
Lab6 – Debug Assembly Language Lab
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Memory & Storage Architecture Seoul National University Computer Architecture “ Bomb Lab Hints” 2nd semester, 2014 Modified version : The original.
OllyDbg Debuger.
System Calls 1.
Trying to like a boss… REVERSE ENGINEERING. WHAT EVEN IS… REVERSE ENGINEERING?? Reverse engineering is the process of disassembling and analyzing a particular.
Practical Malware Analysis Ch 8: Debugging Rev
Memory & Storage Architecture Seoul National University GDB commands Hyeon-gyu School of Computer Science and Engineering.
Part 3: Advanced Dynamic Analysis Chapter 8: Debugging.
Chapter 2: Operating-System Structures. 2.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 2: Operating-System Structures Operating.
A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.
CSE 351 GDB Introduction. Lab 1 Status? How is Lab 1 going? I’ll be available at the end of class to answer questions There are office hours later today.
Data Display Debugger (DDD)
Disclaimer The Content, Demonstration, Source Code and Programs presented here is "AS IS" without any warranty or conditions.
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 4 Slide 1 Slide 1 What we'll cover here l Using the debugger: Starting the debugger Setting.
Debugging 1/6/2016. Debugging 1/6/2016 Debugging  Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a program.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Unit - V. Debugging GNU Debugger helps you in getting information about the following: 1.If a core dump happened, then what statement or expression did.
EXPLOITATION CRASH COURSE – FALL 2013 UTD Computer Security Group – Andrew Folloder csg.utdallas.edu (credit: Scott Hand)
Lecture 11 Example Rootkit. Intel internship Intel CTG (Corporate Technology Group) –Advanced research & development –System integrity services using.
HP-SEE Debugging with GDB Vladimir Slavnic Research Assistant SCL, Institute of Physics Belgrade The HP-SEE initiative.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Lecture 3 Translation.
Introduction to Information Security
Mitigation against Buffer Overflow Attacks
Lab assignments Follow each lab walkthrough in textbook
Recitation 5: Attack Lab
Introduction to Operating Systems
Static and dynamic analysis of binaries
Live Phishing Attack Authentication Activity from a Foreign Address.
Recitation: Bomb Lab _______________ 18 Sep 2017.
Dynamic Analysis ddaa.
Debugging Dwight Deugo
Chapter 2: System Structures
Assembly Language Programming Part 3
Debugging with gdb gdb is the GNU debugger on our CS machines.
Homework Reading Machine Projects Labs PAL, pp ,
Recitation: Bomb Lab _______________ 06 Feb 2017.
Threads CSSE 332 Operating Systems Rose-Hulman Institute of Technology
Malware Incident Response  Dynamic Analysis - 2
gdb gdb is the GNU debugger on our CS machines.
Important terms Black-box testing White-box testing Regression testing
Computer Architecture “Bomb Lab Hints”
Important terms Black-box testing White-box testing Regression testing
Chapter 9 :: Subroutines and Control Abstraction
Introduction to Operating Systems
Debugging with Eclipse
Lab assignments Follow each lab walkthrough in textbook
Processes in Unix, Linux, and Windows
Assembly Language Programming II: C Compiler Calling Sequences
BIC 10503: COMPUTER ARCHITECTURE
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2015.
System Calls David Ferry CSCI 3500 – Operating Systems
CSC235 - Visual Studio Tutorial
Debugging Dwight Deugo
Following Malware Execution in IDA
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2016.
Makefiles, GDB, Valgrind
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2013.
Debugging with Eclipse
Computer Architecture and System Programming Laboratory
In Today’s Class.. General Kernel Responsibilities Kernel Organization
Reverse Engineering for CTFs
By Hugues Leger / Intro to GDB debugger By Hugues Leger / 11/16/2019.
Presentation transcript:

Part 3: Advanced Dynamic Analysis Chapter 8: Debugging Chapter 9: OllyDbg

Chapter 8: Debugging 2

Debugger Hardware or software used to examine execution of another program Disassembler: static snapshot of what code looks like before execution Debugger: dynamic snapshot of what code does during execution

Types of debuggers Source-level Assembly-level Debug while coding Map machine execution to corresponding source code lines Allow setting of breakpoints at source-code lines Assembly-level Strictly operate at machine instruction level Main debugger used for malware

Types of debuggers User mode Kernel mode Debug one program via another program all in user space Examples: OllyDbg, gdb Kernel mode Debugging a kernel requires a second machine Must configure target OS to allow kernel debugging Examples: WinDbg

Debugging functions Single stepping One machine instruction or source line at a time Stepping-over: calls to functions executed all at once before control returned to debugger (next instruction) Stepping-into: calls to functions followed (enters callee) one machine instruction at a time (step instruction) Stepping-out: execute until return back to calling function (finish)

Debugging functions Breakpoints (software) Set at virtual memory address of instruction or at source line Allows one to examine the state of the machine at critical execution points File creation (Listing 8-4, Figure 8-1, p. 171-172, Loc. 4373, 4388) Encryption (Listing 8-5, Figure 8-2, p. 172-173, Loc. 4399, 4414) Implemented by overwriting INT 3 (0xcc) into opcode of instruction (Table 8-1, p. 174, Loc. 4425) Debugger restores overwritten byte upon continue

Debugging functions Hardware execution breakpoints Dedicated registers that store virtual addresses Can be set to break on access, rather than on execution Memory watchpoints on data (reads or writes) 4 hardware registers (DR0-DR3) Can be modified by running program! Malware can disable them Counter-measure is “General Detect” flag in DR7 that triggers a breakpoint on any mov involving debug registers

Debugging functions Conditional software execution breakpoints Break only if a certain condition is met Example Break on GetProcAddress function only if address parameter is RegSetValue Implemented as normal software breakpoint, but debugger checks condition and automatically continues if not met

Handling exceptions Exceptions pass control to debugger INT 3, Trap flag in FLAGS register, Division by 0, invalid memory access Problem Might interfere with exception handlers that program needs to run What if my malware is embedded in an INT 3 handler? First-chance and second-chance exceptions Debugger (if attached) gets first-chance control If debugger does not want it, program allowed to handle exception If program does not handle exception and would crash, debugger gets a second-chance to handle exception Malware may intentionally trigger first-chance exceptions to determine environment

Modifying execution Skip functions by changing EIP directly Invoke functions directly on arguments you choose Useful in debugging metamorphic malware Malware programmed to behave differently under different circumstances (e.g. when being debugged, when in a VM, or when located in a nuclear enrichment facility) Debugger can be set to trace branches of metamorphic code Locale-based metamorphism (Listing 8-6, p. 177, Loc 4535)

Chapter 9: OllyDbg

OllyDbg Developed by Oleh Yuschuk Debugger of choice for malware analysis *and* exploit developers Many still use OllyDbg 1.1. OllyDbg 2.0 also available. 32-bit only Purchased by Immunity and forked as Immunity Debugger Python API support added Also free

Loading code in OllyDbg Open executable from within OllyDbg In-class exercise Launch OllyDbg and use File=>Open to recreate (Figure 9-2, p. 181, Loc. 4601) for C:\WINDOWS\notepad.exe 4 main windows of OllyDbg Disassembler, Registers, Stack, Memory dump

Loading code in OllyDbg Launch executable and attach In-class exercise Launch notepad.exe from OllyDbg Attach OllyDbg to running notepad Use the View tools to list memory, recreating (Figure 9-4, p. 183) for notepad.exe

Rebasing Memory locations of Figure 9-4 dynamic Relocatable code allows libraries to be rebased Enables libraries to be written independent of each other Absolute address references modified at load time via .reloc information in PE header Supports ASLR to thwart malware In-class exercise (Windows 7 only) Note the location of notepad's .text section Relaunch OllyDbg on notepad again What is the location now?

Threads Most programs and malware multi-threaded In-class exercise Launch Internet Explorer Attach OllyDbg View threads via View>Threads How many threads are there?

Executing code Debug menu Run Breakpoint=>Run to selection Continue execution until specified instruction Debug=>Execute till Return Runs until next return hit (e.g. Finish) Debug=>Execute till User Code Run until user program code is reached Pulls out of library calls Step into, step over See previous lecture

Executing code Malware making a mess out of step-over p. 187, Loc. 4734 Step over a “call” instruction sets breakpoint to next instruction after call Malware using a “tail” call might never return e.g. call 01007568 at 0x010073a4 might never return to xor instruction at 0x01007568

Breakpoints Software Unconditional breakpoint (Toggle) Right-click instruction to find sub-menu to set Or, double-click opcode View=>Breakpoints to list

Breakpoints Conditional breakpoint example Poison Ivy Backdoor that reads shellcode commands from socket and executes them Uses a call to VirtualAlloc to store command Typical call to VirtualAlloc (Figure 9-7, p. 189, Loc. 4797) Want to break only on large allocations indicative of a batch of commands (> 100bytes) Size parameter at [ESP+8] Set breakpoint at VirtualAlloc entry point if condition [ESP+8] > 100 Breakpoint=>Conditional Figure 9-8, p. 190, Loc. 4812

Breakpoints Conditional breakpoints In OllyDbg with notepad.exe Right-click and add condition (ecx == 0xFFFF) View=>Breakpoints (to list) Hardware Memory breakpoint (on execution, access, write) Right-click location in memory window Debug => Hardware breakpoints (to list)

Loading DLLs Malware often delivered as DLLs to be injected into other processes OllyDbg uses loaddll.exe as dummy program Calls into DllMain function of target DLL In-class exercise Generate Figure 9-10, p. 191, Loc. 4856 Open C:\WINDOWS\system32\ws2_32.dll in OllyDbg(32-bit only) Hit play to initialize DLL Debug=>Call DLL export to call a particular exported function with custom parameters Disassembler window automatically updates with disassembled code

Tracing Recording execution Call Stack Trace Open notepad.exe and run View => Call Stack Compact view the function call path that has led to your current execution point

Tracing Recording execution Run Trace OllyDbg saves every executed instruction and all changes to registers and flags In-class: Try tracing on a lab binary Highlight code to trace Run Trace=>Add Selection Execute View=>Run Trace Go to red-highlighted traced instructions - and + to navigate trace and see changes Or use “Trace Into” and “Trace Over” options to run trace until next breakpoint Take care to limit size of trace

Tracing Poison Ivy backdoor example VirtualAlloc to store commands from C&C server Stored in heap memory EIP executes from heap locations Goal: Begin tracing when EIP points to heap Step #1: Use Debug=>Set condition to set condition to pause on EIP outside of program segment (Figure 9-11, p. 194, Loc. 4906) Step #2: Trace Into to execute until condition met Step #3: Use – key to backup execution to see where entry into shellcode occurred

Exceptions Exception handling with OllyDbg User options Step into exception Step over exception Run debugger exception handler Can also set in Debugging Options to ignore all exceptions (immediately transfer control back to program)

Patching Modifying program instructions to change behavior In class In OllyDbg, modify conditional branch within Lab 3-4 to bypass filename check that deletes file NOP out call to ShellExecute At 0x04024EE => Right-click, Binary=>Edit, Fill with NOPs Copy modifications to new executable via OllyDump (see next slide)

Dumping Create new binary upon unpacking program OllyDump plug-in Find entry point after unpacking and decryption operations of malware performed Creates a new executable that can be analyzed within IDA Pro Figure 9-16, p. 198, Loc. 4991

Chapter 9.5: Other debuggers

gdb (the ancient one) CTF levels for teaching the tool gdb –tui

Controlling program execution run Starts the program step Execute until a different source line reached (step into calls) next Execute until next source line reached, proceeding through subroutine calls. continue Resume program execution until signal or breakpoint.

Controlling program execution break, del Set and delete breakpoints at particular lines of code watch, rwatch, awatch Data breakpoints Stop when the value of an expression changes (watch), when expression is read (rwatch), or either (awatch)‏

Displaying data print x (examine) Print expression Basic print /x addr print argc print argv[0] print $rsp print /x addr ‘/x’ says to print in hex. See “help x” for more formats Same as examine memory address command (x)‏ printf “format string” arg-list (gdb) printf "%s\n", argv[0] x (examine) Examine memory x /s $rax => print the string at address contained in %rax x /32xw 0x4006b7 => print 32 words at 0x4006b7 in hexadecimal

Displaying code disassemble <fn> Disassemble specific function fn or bytes at an address

Other Useful Commands where, backtrace up, down info quit Produces a backtrace - the chain of function calls that brought the program to its current place. up, down Change scope in stack info Get information ‘info’ alone prints a list of info commands ‘info br’ : a table of all breakpoints and watchpoints ‘info reg’ : the machine registers quit Exit the debugger

gdb tui layout <cmd> focus <pane> split (creates a split screen with multiple panes) asm (loads assembly up in a pane) regs (loads registers up in a pane) focus <pane> Puts focus onto a particular pane (cmd, asm, regs)

Walkthrough example TUI Step, next, and finish unalias gdb gdb layout split quit alias gdb='gdb -tui' layout asm layout regs Control-X 1 Control-X 2 focus asm <scroll to first printf> Breakpoints, stack traces break *main+47 (at printf) info br run del 1 break printf where disass main => printf invocation del 2 Step, next, and finish break main run ni ni 5 del 3 break *0x80484da si si 5 finish Debug stack and function calls p /x $esp x/8xw $esp x/s 0x080485c0 x/s 0x08048528 x/s 0x08048530 x/s 0x08048538 ni to the scanf call git clone https://bitbucket.org/wuchangfeng/cs492.git

Walkthrough example Debug the scanf and the puts x/s the format string ni to get integer x/d the integer -> 0x8048a048 x/s the two puts calls to find Good Job del 4 Win? Find compare: *main+81 to *main+92 Break on *main+92 x/d 0x804a024 run 11 continue Registers eax and edx x/d 0x804a048 del 5 Debug via memory watchpoint break main watch *0x804a048 run continue (scanf) continue (sub) continue del 6 Debug via time travel break *main+73 break *main+92 record reverse-stepi <return all the way back through to sub> reverse-continue <goes back to beginning of trace> Win!

PEDA Python Exploit Development Assistance for GDB https://github.com/longld/peda

rr (deterministic replay via gdb) Record all CPU state at system calls Allow debugger to go back in time and play forward Can get to any process state Used for debugging browser crashes deterministically Must run on bare metal to get access to hardware debug registers

QIRA (deterministic replay via QEMU) Timeless debugging Record the entire program trace at emulator or VM level George Hotz, Enigma 2016 https://youtu.be/eGl6kpSajag?t=255 4:15-12:22 Use if you’d like (can do CTF level on it)

DDD

radare2 Similar to gdb, but with an IDA Pro-like disassembler included CTF levels for teaching the tool radare2:gdb akin to Unix:Windows r2 <binary> For using it as a disassembler r2 –d <binary> For running it in the debugger

Binary Ninja Lifts binary to a family of intermediate languages similar to LLVM or bytecode Binary Ninja Intermediate Language (BNIL) But with flexibility in what is abstracted (multi-stage vs. single-stage) Tool works on IL If you like this kind of stuff…. Sophia d’Antoine INFILTRATE 2017 https://vimeo.com/215511922

In-class exercise Lab 9-2