Program Analysis

Program Analysis

Notes Lecture 2 Program Analysis Computer and Network Security October 19, 2020 Computer Science and Engineering Department CSE Dep, ACS, UPB Lecture 2, Program Analysis 1/79 Notes Program Analysis CSE Dep, ACS, UPB Lecture 2, Program Analysis 2/79 Program Analysis Notes I automatic analysis of programs I property verification I optimization (performance) or correctness I static analysis or dynamic analysis CSE Dep, ACS, UPB Lecture 2, Program Analysis 3/79 Program Model Notes I automaton I control flow graph (CFG) (set of states and transitions) I coverage: how much of the CFG can the analysis cover to ensure property validation CSE Dep, ACS, UPB Lecture 2, Program Analysis 4/79 Static and Dynamic Analysis Notes I do not execute or execute the program I static analysis on source code or on binary program (executable) I dynamic analysis on resource usage and behavior (process) I symbolic execution is static analysis I fuzzing is dynamic analysis I static analysis: broad, may go into path explosion I dynamic analysis: depth, may miss certain cases CSE Dep, ACS, UPB Lecture 2, Program Analysis 5/79 Source Code vs Executable Notes I extensive analysis on source code but . I we don't know what the compiler / linker does to it, what optimizations happen, how it links to other components I it may not be available I we focus most on static binary analysis CSE Dep, ACS, UPB Lecture 2, Program Analysis 6/79 Challenges of Static Binary Analysis Notes I more difficult to understand: requires reverse engineering I may be subject to obfuscation, encryption, packing I typically doubled by dynamic analysis CSE Dep, ACS, UPB Lecture 2, Program Analysis 7/79 Process as a Goal Notes I provide functionality I dynamic / run time I allocate and use memory and other resources CSE Dep, ACS, UPB Lecture 2, Program Analysis 9/79 Steps from Source Code to Process Notes 1. compile and assemble source code into object files 2. link object files into executable 3. load executable (disk image file) into process (memory + CPU) CSE Dep, ACS, UPB Lecture 2, Program Analysis 10/79 Object File Notes I binary files I headers and binary code I may be disassembled I data and code I sections CSE Dep, ACS, UPB Lecture 2, Program Analysis 11/79 Library Files Notes I archive/collection of object files I modularity I static-linking and dynamic linking libraries I linking happens at link time I linking happens at load time CSE Dep, ACS, UPB Lecture 2, Program Analysis 12/79 Executable Files Notes I binary files I similar to object files, consist of object code I may be disassembled I created from object files I static and dynamic executables I static: all object code is part of the executable I dynamic: library stubs to library functions CSE Dep, ACS, UPB Lecture 2, Program Analysis 13/79 ELF Notes http://www.roman10.net/2012/11/28/an-intro-to-elf-file-formatpart-1-file-types-and-dual-views/ CSE Dep, ACS, UPB Lecture 2, Program Analysis 14/79 Object File Format Notes I format of a file that contains object code: object file, executable files, dynamic-linking library files I headers, sections I data and code I may be disassembled I PE (Portable Executable) on Windows I COFF (Common Object File Format) on Unix I ELF (Executable and Linking Format) on Linux CSE Dep, ACS, UPB Lecture 2, Program Analysis 16/79 Common Information in Executabile Files Notes I entry point I program addresses (section addresses) I section sizes I symbols (names and addresses) I permissions CSE Dep, ACS, UPB Lecture 2, Program Analysis 17/79 ELF Format Notes I header I program headers I sections I segments I symbols I readelf, objdump, nm CSE Dep, ACS, UPB Lecture 2, Program Analysis 18/79 Sections Notes I storing data or code I readelf -S program I .text, .data, .bss I .symtab, .strtab CSE Dep, ACS, UPB Lecture 2, Program Analysis 19/79 Sections vs. Segments Notes I segments contain 0 ore more sections I sections are used by linker, some sections may be ditched at runtime I segments are used by the operating system (loaded into memory) CSE Dep, ACS, UPB Lecture 2, Program Analysis 20/79 View of ELF File Notes http://www.roman10.net/2012/11/28/an-intro-to-elf-file-formatpart-1-file-types-and-dual-views/ CSE Dep, ACS, UPB Lecture 2, Program Analysis 21/79 Symbols Notes I readelf -s program I .dynsym and .symtab I name, value, type, bind, size CSE Dep, ACS, UPB Lecture 2, Program Analysis 22/79 Debugging Symbols Notes I Map Assembly instructions to variable, function or line in the source code I Help mapping stack values with function parameters I Optimize data flow analysis I Optimize static and dynamic analysis I On Linux, symbol table is embedded in the ELF file. PE files use an external symbols file CSE Dep, ACS, UPB Lecture 2, Program Analysis 23/79 Stripping Notes I Removing symbol table from program executable I Complicates reverse engineering I Less space used by original binary CSE Dep, ACS, UPB Lecture 2, Program Analysis 24/79 Overview of Linking Notes I All object files are linked together to produce an executable file I Input: Object files, static libraries, dynamic libraries I Output: Executable image I The linker resolved external references from each object file CSE Dep, ACS, UPB Lecture 2, Program Analysis 26/79 Using ld Notes I Command used in the last compiling phase I Libraries are specified using -l option I PIE option enables ASLR support CSE Dep, ACS, UPB Lecture 2, Program Analysis 27/79 Static Linking Notes I Linker copies library routines directly into executables image I Executable is more portable because all data needed to execute resides in the file I Faster execution because imports are not resolved at runtime I Uses more space CSE Dep, ACS, UPB Lecture 2, Program Analysis 28/79 Tools of Trade Notes I building machine code files I inspecting machine code files I disassembling machine code files CSE Dep, ACS, UPB Lecture 2, Program Analysis 30/79 Building Executables Notes I gcc, gas, nasm, ar, ld CSE Dep, ACS, UPB Lecture 2, Program Analysis 31/79 ELF Inspection Notes I strings I xxd I readelf I nm CSE Dep, ACS, UPB Lecture 2, Program Analysis 32/79 Disassembling Notes I IDA I objdump I radare2 CSE Dep, ACS, UPB Lecture 2, Program Analysis 33/79 Not for Static Analysis Notes I pmap I lsof I ltrace I strace I GDB CSE Dep, ACS, UPB Lecture 2, Program Analysis 34/79 Dynamic Analysis Notes I starts from executable files I investigate processes I requires process to run I runtime analysis I blackbox analysis CSE Dep, ACS, UPB Lecture 2, Program Analysis 36/79 Processes Notes I unit of work in the operating system I virtual memory address space, threads, resources I isolated from each other I at load time the executable gives birth to a process CSE Dep, ACS, UPB Lecture 2, Program Analysis 37/79 Process Memory Layout Notes http://www.tenouk.com/Bufferoverflowc/Bufferoverflow1_files/image022.png CSE Dep, ACS, UPB Lecture 2, Program Analysis 38/79 Interesting Process Information Notes I the process memory map (virtual memory areas) I memory addresses: code, variables I memory region access rights I machine code (to be disassembled) I process state: registers, (call) stack, code CSE Dep, ACS, UPB Lecture 2, Program Analysis 39/79 Why Dynamic Analysis Notes I get output for input (blackbox) I glimpse into the internals I monitor/inspect resource usage I debug execution and test attacks (step by step) CSE Dep, ACS, UPB Lecture 2, Program Analysis 40/79 What Do We Investigate? Notes I code: system calls, library calls, function calls, step-by-step code I state: thread information, process maps, open files, resources I data: registers, variables, raw memory data CSE Dep, ACS, UPB Lecture 2, Program Analysis 41/79 Inspecting Code Notes I function call tracing I disassembling I step by step instructions I look into code where required in the process virtual address space CSE Dep, ACS, UPB Lecture 2, Program Analysis 42/79 Inspecting Data Notes I variables: global (data) and local (stack) I runtime metadata: return addresses, function arguments, command line arguments, GOT and PLT (to be discussed later) I registers I raw memory data: heap, stack, random address CSE Dep, ACS, UPB Lecture 2, Program Analysis 43/79 Inspecting State Notes I process memory map I thread state I open file descriptors CSE Dep, ACS, UPB Lecture 2, Program Analysis 44/79 Types of Tools Notes I blackbox inspection: function call tracers (strace, ltrace, dtrace/dtruss), fuzzers I profilers: most often for performance: perf, callgrind, vTune I debugging: GDB, LLDB, valgrind CSE Dep, ACS, UPB Lecture 2, Program Analysis 46/79 Fuzzing Notes I generate \random" input and detect program flaws I program is run I smart fuzzer try to direct I AFL, libfuzzer CSE Dep, ACS, UPB Lecture 2, Program Analysis 47/79 strace/ltrace Notes I strace ./a.out I strace -e read,write ./a.out I strace -e file ./a.out I strace -e file -f ./a.out I strace -e file -s 512 -f ./a.out I similar options for ltrace CSE Dep, ACS, UPB Lecture 2, Program Analysis 48/79 lsof/pmap Notes I PID as argument I lsof -p 12345 I pmap 12345 CSE Dep, ACS, UPB Lecture 2, Program Analysis 49/79 perf Notes I default profiler on Linux I sampling profiler, doesn't instrument the code I uses events sampling I perf stat -e cache-misses -a ./mem-walk I sudo perf list I some actions and events may require privileged access CSE Dep, ACS, UPB Lecture 2, Program Analysis 50/79 GDB Notes I default debugger on GNU/Linux distributions I command line; there are some GUI front-ends I incorporated in Linux-based IDEs I debugging, dynamic analysis / process investigation I gdb ./a.out I gdb -q ./a.out

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    18 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us