Nytro Posted March 4, 2013 Report Share Posted March 4, 2013 Return Oriented Programming for the ARMArchitectureTim KornauDecember 22, 2009Contents1. Introduction 31.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.2. Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.3. Related work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.4. Thesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.5. Contributions of this work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.6. Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62. Definition of objective 72.1. Protection mechanisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.1.1. Stack cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.1.2. Address space layout randomisation . . . . . . . . . . . . . . . . . . . . . . 72.1.3. Code and data separation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.2. The evolution of return-oriented programming . . . . . . . . . . . . . . . . . . . . . 82.2.1. The evolution time line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.2.1.1. Buffer overflows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.2.1.2. Return-into-library technique . . . . . . . . . . . . . . . . . . . . . 92.2.1.3. Borrowed code chunks technique . . . . . . . . . . . . . . . . . . 92.2.1.4. Return-oriented Programming on x86 . . . . . . . . . . . . . . . . 92.2.1.5. Return-oriented programming on SPARC . . . . . . . . . . . . . . 102.2.1.6. DEPlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.2.1.7. Return-oriented programming on Harvard-type architectures . . . 112.3. Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.3.1. Problem approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113. Technical details 133.1. architecture and operating system details . . . . . . . . . . . . . . . . . . . . . . . 133.1.1. The ARM architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.1.1.1. History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.1.1.2. Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143.1.1.3. Instruction set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.1.1.4. ARM and THUMB . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.1.1.5. Endianness of memory . . . . . . . . . . . . . . . . . . . . . . . . 173.1.1.6. Stack modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173.1.1.7. Subroutine calling convention . . . . . . . . . . . . . . . . . . . . . 183.1.2. Operating system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183.1.2.1. Operating system overview . . . . . . . . . . . . . . . . . . . . . . 183.1.2.2. Memory architecture . . . . . . . . . . . . . . . . . . . . . . . . . 183.1.2.3. XIP DLLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203.1.2.4. DLL loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203.1.2.5. Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203.1.2.6. The stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213.1.2.7. ARM prologue and epilogue . . . . . . . . . . . . . . . . . . . . . 223.1.2.8. Function calling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243.1.2.9. System calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243.1.2.10. Cache synchronisation and buffers . . . . . . . . . . . . . . . . . . 25viii Contents3.1.2.11. Dumping ROM and XIP . . . . . . . . . . . . . . . . . . . . . . . . 263.1.2.12. Debugging Windows Mobile . . . . . . . . . . . . . . . . . . . . . 273.2. The REIL meta-language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303.2.1. A brief description of REIL cornerstones . . . . . . . . . . . . . . . . . . . . 303.2.2. REIL architecture and instruction set . . . . . . . . . . . . . . . . . . . . . . 303.2.2.1. The REIL VM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313.2.2.2. REIL instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . 323.2.3. Limitations of REIL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343.3. Return-Oriented Programming on ARM . . . . . . . . . . . . . . . . . . . . . . . . 353.3.1. A note on Turing-completeness . . . . . . . . . . . . . . . . . . . . . . . . . 353.3.2. Finding ARM Instruction Sequences in libraries . . . . . . . . . . . . . . . . 363.3.3. Construction of ARM Gadgets . . . . . . . . . . . . . . . . . . . . . . . . . 373.3.4. Crafting a Return-Oriented Program . . . . . . . . . . . . . . . . . . . . . . 373.3.5. Generating a return oriented program with a compiler . . . . . . . . . . . . 373.4. ARM gadget catalogue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383.4.1. Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383.4.2. Nomenclature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393.4.3. Memory gadgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393.4.3.1. Gadget: memory to register . . . . . . . . . . . . . . . . . . . . . 403.4.3.2. Gadget: memory to memory . . . . . . . . . . . . . . . . . . . . . 413.4.3.3. Memory arithmetic operation gadget . . . . . . . . . . . . . . . . . 423.4.3.4. Memory bitwise operation gadgets . . . . . . . . . . . . . . . . . . 433.4.4. Memory dereference gadgets . . . . . . . . . . . . . . . . . . . . . . . . . . 443.4.4.1. Gadget: register to memory dereference (pointer read) . . . . . . 443.4.4.2. Gadget: memory to memory dereference (pointer read) . . . . . . 453.4.4.3. Gadget: memory dereference to memory or register (pointer write) 463.4.5. Register gadgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473.4.5.1. Gadget: Register to register . . . . . . . . . . . . . . . . . . . . . 483.4.5.2. Gadget: Register to constant . . . . . . . . . . . . . . . . . . . . . 493.4.5.3. Gadget: register to memory . . . . . . . . . . . . . . . . . . . . . 493.4.5.4. Register arithmetic gadgets . . . . . . . . . . . . . . . . . . . . . . 503.4.5.5. Register bitwise operation gadgets . . . . . . . . . . . . . . . . . . 523.4.5.6. Shift gadgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523.4.6. Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543.4.7. Control Flow gadgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553.4.7.1. Gadget: branch Always . . . . . . . . . . . . . . . . . . . . . . . . 553.4.7.2. Gadget: branch conditionally . . . . . . . . . . . . . . . . . . . . . 563.4.8. Function call gadgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573.4.8.1. Gadget: normal function call . . . . . . . . . . . . . . . . . . . . . 573.4.8.2. Gadget: leaf function call . . . . . . . . . . . . . . . . . . . . . . . 583.4.9. System Call gadget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584. Algorithms for automatic gadget searching 614.1. Stage I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614.1.1. Reverse walker algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614.1.2. Algorithm: Expression tree extraction . . . . . . . . . . . . . . . . . . . . . 614.1.2.1. Instruction handler . . . . . . . . . . . . . . . . . . . . . . . . . . . 634.1.2.2. Algorithm to update the operand tree map . . . . . . . . . . . . . 674.1.2.3. Example for a single native instruction . . . . . . . . . . . . . . . . 674.1.3. Path extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694.2. Stage II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694.2.1. Algorithm to merge expression trees with path information . . . . . . . . . . 694.2.1.1. Merging example . . . . . . . . . . . . . . . . . . . . . . . . . . . 704.2.1.2. Jump condition determination algorithm . . . . . . . . . . . . . . . 70Contents ix4.2.1.3. Traverse and update operand tree map algorithm . . . . . . . . . 704.2.2. Algorithm to simplify expression tree . . . . . . . . . . . . . . . . . . . . . . 724.3. Stage III . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734.3.0.1. Locate gadgets core function . . . . . . . . . . . . . . . . . . . . . 734.3.0.2. Gadget locator functions . . . . . . . . . . . . . . . . . . . . . . . 734.3.0.3. Gadget complexity calculation . . . . . . . . . . . . . . . . . . . . 745. System implementation 755.1. Integration into BinNavi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755.2. Initial data extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755.3. Extracting information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765.3.1. Extracting instruction information callback . . . . . . . . . . . . . . . . . . . 765.3.2. Extracting path information callback . . . . . . . . . . . . . . . . . . . . . . 775.4. Merging of extracted information . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775.4.1. Updating the expression tree in a path . . . . . . . . . . . . . . . . . . . . . 775.4.2. Simplification of expression trees . . . . . . . . . . . . . . . . . . . . . . . . 785.5. Using the extracted information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785.5.1. Finding suitable sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . 785.5.2. Evaluating suitable sequences . . . . . . . . . . . . . . . . . . . . . . . . . 796. Experimental results 816.1. Testing environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816.2. Library comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816.3. Exploit example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836.3.1. Vulnerable service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836.3.2. Shellcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836.3.3. Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857. Conclusion and further work 877.1. Automatic gadget compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 877.2. Gadget description language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 877.3. Live system scanning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 877.4. Partial function reconstruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 877.5. Attack vector broadening . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 887.6. Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 887.7. Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88A. Bibliography iB. List of Figures vDownload:http://www.handgrep.se/repository/ebooks/Security/Reversing/kornau-tim--diplomarbeit--rop.pdf Quote Link to comment Share on other sites More sharing options...