finding library subroutines in stripped statically linked
play

Finding library subroutines in stripped statically-linked binaries - PowerPoint PPT Presentation

Finding library subroutines in stripped statically-linked binaries findmagic Katharina Bogad Technische Universitt Mnchen Computer Science Department SS 2015 January 18, 2017 K. Bogad findmagic SS 2015 January 18, 2017 1 / 39


  1. Finding library subroutines in stripped statically-linked binaries findmagic Katharina Bogad Technische Universität München Computer Science Department SS 2015 January 18, 2017 K. Bogad findmagic SS 2015 January 18, 2017 1 / 39

  2. obligatory tl;dr me slide y ▸ Computer Science student ▸ Member of the H4x0rPsch0rr CTF-Team and CTF-Player for fun (and sometimes profit) ▸ Interested in reverse engineering for long time ▸ Hates QR-Codes K. Bogad findmagic SS 2015 January 18, 2017 2 / 39

  3. Preliminary audience questions y Who of you has... ▸ basic knowledge of graph theory? K. Bogad findmagic SS 2015 January 18, 2017 3 / 39

  4. Preliminary audience questions y Who of you has... ▸ basic knowledge of graph theory? ▸ reverse engineered a statically linked binary at least once? K. Bogad findmagic SS 2015 January 18, 2017 3 / 39

  5. Problem description Why? y ▸ Traditional pattern-matching: exact library needed for decent results ▸ Works reasonably well in homogenous environments like MSVCRT ▸ Open source libraries? ▸ Embedded devices? K. Bogad findmagic SS 2015 January 18, 2017 4 / 39

  6. Problem description Why? y So, what are we doing if we cannot have symbols? ▸ Looking at the arguments? ▸ Looking at suspicious constants? Think of 0x8080808080 for strlen(3) Let’s automate this! K. Bogad findmagic SS 2015 January 18, 2017 5 / 39

  7. Problem description Why? y However, there are caveats: ▸ Finding arguments is not a trivial task. ▸ What makes a constant suspicious? K. Bogad findmagic SS 2015 January 18, 2017 6 / 39

  8. Problem description Why? y However, there are caveats: ▸ Finding arguments is not a trivial task. ▸ What makes a constant suspicious? But automating gives new perspectives: Comparing callgraphs! K. Bogad findmagic SS 2015 January 18, 2017 6 / 39

  9. Algorithm Design Graph definition y ▸ Program is a set of attributed graphs G = ( N , B ) ▸ Nodes N are functions ▸ Branches B are calls between functions K. Bogad findmagic SS 2015 January 18, 2017 7 / 39

  10. Algorithm Design Definitions for later use y We need: ▸ A string definition (∀( i , c ) ∈ str ∶ c ≥ 0x20 ∧ c ≤ 0xDF ∨ c = 0x0A ∨ c = 0x0D ∨ c = 0x09 ∨ c = 0x00 ) ∧ ∣ str ∣ > 1 (1) ∧ (∀( i , c ) ∈ str ∣ i = max ( i , str ) ∶ c = 0x00 ) ∧ (∀( i , c ) ∈ str ∣ i ≠ max ( i , str ) ∶ c ≠ 0x00 ) K. Bogad findmagic SS 2015 January 18, 2017 8 / 39

  11. Algorithm Design Definitions for later use y We need: ▸ A string definition (∀( i , c ) ∈ str ∶ c ≥ 0x20 ∧ c ≤ 0xDF ∨ c = 0x0A ∨ c = 0x0D ∨ c = 0x09 ∨ c = 0x00 ) ∧ ∣ str ∣ > 1 (1) ∧ (∀( i , c ) ∈ str ∣ i = max ( i , str ) ∶ c = 0x00 ) ∧ (∀( i , c ) ∈ str ∣ i ≠ max ( i , str ) ∶ c ≠ 0x00 ) Printable characters from extended ASCII ... K. Bogad findmagic SS 2015 January 18, 2017 8 / 39

  12. Algorithm Design Definitions for later use y We need: ▸ A string definition (∀( i , c ) ∈ str ∶ c ≥ 0x20 ∧ c ≤ 0xDF ∨ c = 0x0A ∨ c = 0x0D ∨ c = 0x09 ∨ c = 0x00 ) ∧ ∣ str ∣ > 1 (1) ∧ (∀( i , c ) ∈ str ∣ i = max ( i , str ) ∶ c = 0x00 ) ∧ (∀( i , c ) ∈ str ∣ i ≠ max ( i , str ) ∶ c ≠ 0x00 ) ... and \n , \r , \t and 0x00 ... K. Bogad findmagic SS 2015 January 18, 2017 8 / 39

  13. Algorithm Design Definitions for later use y We need: ▸ A string definition (∀( i , c ) ∈ str ∶ c ≥ 0x20 ∧ c ≤ 0xDF ∨ c = 0x0A ∨ c = 0x0D ∨ c = 0x09 ∨ c = 0x00 ) ∧ ∣ str ∣ > 1 (1) ∧ (∀( i , c ) ∈ str ∣ i = max ( i , str ) ∶ c = 0x00 ) ∧ (∀( i , c ) ∈ str ∣ i ≠ max ( i , str ) ∶ c ≠ 0x00 ) ... with a minimum length of 2 ... K. Bogad findmagic SS 2015 January 18, 2017 8 / 39

  14. Algorithm Design Definitions for later use y We need: ▸ A string definition (∀( i , c ) ∈ str ∶ c ≥ 0x20 ∧ c ≤ 0xDF ∨ c = 0x0A ∨ c = 0x0D ∨ c = 0x09 ∨ c = 0x00 ) ∧ ∣ str ∣ > 1 (1) ∧ (∀( i , c ) ∈ str ∣ i = max ( i , str ) ∶ c = 0x00 ) ∧ (∀( i , c ) ∈ str ∣ i ≠ max ( i , str ) ∶ c ≠ 0x00 ) ... where the last character is 0x00 and no other character is 0x00 . K. Bogad findmagic SS 2015 January 18, 2017 8 / 39

  15. Algorithm Design Definitions for later use y We need: ▸ A node definition N = ( n , s , C , S , I ) ▸ n : Function name ▸ s : Function address ▸ C : Multiset of constant values ▸ S : Multiset of cross-referenced strings ▸ I : Ordered multiset of the machine instructions K. Bogad findmagic SS 2015 January 18, 2017 9 / 39

  16. Algorithm Design Get crackin’ y Objective: Generate a bijective mapping M = N 1 → N 2 ▸ N 1 : known library function ▸ N 2 : function inside the target library K. Bogad findmagic SS 2015 January 18, 2017 10 / 39

  17. Algorithm Design Get crackin’ y 1 Acquire target library with debug symbols K. Bogad findmagic SS 2015 January 18, 2017 11 / 39

  18. Algorithm Design Get crackin’ y 1 Acquire target library with debug symbols 2 Build the graphs for it K. Bogad findmagic SS 2015 January 18, 2017 11 / 39

  19. Algorithm Design Get crackin’ y 1 Acquire target library with debug symbols 2 Build the graphs for it 3 Build graphs for the binary we analyse K. Bogad findmagic SS 2015 January 18, 2017 11 / 39

  20. Algorithm Design Get crackin’ y 1 Acquire target library with debug symbols 2 Build the graphs for it 3 Build graphs for the binary we analyse 4 Match them K. Bogad findmagic SS 2015 January 18, 2017 11 / 39

  21. Algorithm Design Get crackin’ y Do we need exactly the same binary used for linking? ▸ Short answer: no. K. Bogad findmagic SS 2015 January 18, 2017 12 / 39

  22. Algorithm Design Get crackin’ y Do we need exactly the same binary used for linking? ▸ Short answer: no. ▸ Long answer: it depends. K. Bogad findmagic SS 2015 January 18, 2017 12 / 39

  23. Algorithm Design Get crackin’ y ▸ A reasonably close version is enough ▸ Watch out for compiler flags ▸ Also problematic: assert() K. Bogad findmagic SS 2015 January 18, 2017 13 / 39

  24. Algorithm Design Why assert() is evil y Caution: real world example 2391 assert(( unsigned long ) (old_size) < ( unsigned long ) (nb + MINSIZE)); with relocation: without relocation: 1 ( unsigned long ) (old_size) < ( unsigned 1 ( unsigned long ) (old_size) < ( unsigned long ) ( long ) ( nb + ( unsigned long )( nb + ( unsigned long )( 2 2 (((__builtin_offsetof ( struct (((__builtin_offsetof( struct 3 3 malloc_chunk, fd_nextsize)) + malloc_chunk, fd_nextsize)) + ((2 * ( sizeof (size_t)) < 4 ( 4 (2 * ( sizeof (size_t))) - 1 __alignof__ ( long double ) ? 5 __alignof__ ( long double ) : )) 6 5 2 * ( sizeof (size_t)) 7 & ~( 6 (2 * ( sizeof (size_t))) - 1 ) - 1)) 8 7 9 )))) 8 & ~( (2 * ( sizeof (size_t)) < 9 __alignof__ ( long double ) ? No code, but debug strings vary! __alignof__ ( long double ) : 10 2 * ( sizeof (size_t)) 11 ) - 1 12 13 )))) K. Bogad findmagic SS 2015 January 18, 2017 14 / 39

  25. Automatic binary analysis Overview y 1 Iterate over subroutines 2 Iterate over the instructions of these subroutines 3 If something interesting is found, add it to the corresponding list 1 1 See the paper for a marvellous formal definitions for this K. Bogad findmagic SS 2015 January 18, 2017 15 / 39

  26. Automatic binary analysis call analysis y ▸ call instructions add a new branch to the functions callgraph ▸ Additionally for Intel x86_64 architecture: ▸ Only if it’s a near call - opcode 0xE8 ▸ This ensures we’re in the same section ▸ Other architectures may need different conditions! K. Bogad findmagic SS 2015 January 18, 2017 16 / 39

  27. Automatic binary analysis Strings y ▸ Look for something that loads a pointer (x86_64: lea , mov ) ▸ Check if it’s a string by our definition ▸ If so, add it to the Strings of the current function K. Bogad findmagic SS 2015 January 18, 2017 17 / 39

  28. Automatic binary analysis Constants y ▸ We don’t want to add pointer arithmetic as constants ▸ Interesting constants are often bitmasks ▸ Thus, we limit ourselves to the immediates of and , or , xor and mov ▸ Optionally, we may exclude further by doing value checking on the constant K. Bogad findmagic SS 2015 January 18, 2017 18 / 39

  29. Automatic binary analysis Matching y Isomorphism: ▸ Ancient greek: isos = equal and morphe = shape ▸ Mathematical way to compare the structure of objects K. Bogad findmagic SS 2015 January 18, 2017 19 / 39

  30. Automatic binary analysis Matching y Choosing the right algorithm: ▸ Ullmann’s algorithm ▸ Nauty ( n o aut omporphism, y es?) ▸ VF2 K. Bogad findmagic SS 2015 January 18, 2017 20 / 39

  31. Automatic binary analysis Matching y Choosing the right algorithm: ▸ Ullmann’s algorithm ▸ Nauty ( n o aut omporphism, y es?) ▸ VF2 K. Bogad findmagic SS 2015 January 18, 2017 20 / 39

  32. Automatic binary analysis Matching y Choosing the right algorithm: ▸ Ullmann’s algorithm ▸ Nauty ( n o aut omporphism, y es?) ▸ VF2 K. Bogad findmagic SS 2015 January 18, 2017 20 / 39

  33. Automatic binary analysis Matching y Choosing the right algorithm: ▸ Ullmann’s algorithm ▸ Nauty ( n o aut omporphism, y es?) ▸ VF2 K. Bogad findmagic SS 2015 January 18, 2017 20 / 39

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend