Microgadgets: Size Does Matter in Turing-Complete Return-Oriented Programming
Total Page:16
File Type:pdf, Size:1020Kb
Microgadgets: Size Does Matter In Turing-complete Return-oriented Programming Andrei Homescu Michael Stewart Per Larsen Stefan Brunthaler Michael Franz Department of Computer Science University of California, Irvine Abstract other applications or systems. One important attack technique, so-called return-into- Return-oriented programming (ROP) has gained a lot of libc [16], uses library functions to compromise the sys- popularity lately, as an attack against currently imple- tem. To get access to those library functions, the attacker mented defenses in modern operating systems. Several only has to manipulate the stack, so that the proper re- kinds of ROP-based attacks and anti-ROP defenses have turn address and parameters are in the desired positions. been proposed in recent years. The original attack tech- Later, Shacham [15] describes a technique called return- nique depends on the existence of a hand-picked set of oriented programming, or ROP for short, which uses ex- byte sequences (called gadgets) in the program, while isting snippets of code, called gadgets, to execute arbi- subsequent approaches use complex scanners, which per- trary algorithms. These gadgets, much smaller than a form semantic analysis on the code to locate gadgets. function, are used as instructions of an abstract virtual The latter ones are efficient at finding gadgets and build- machine, which are proven Turing-complete. The at- ing an attack, but incur a significant cost in time. tacker chains gadgets together into a translated version We propose a ROP attack technique, based on a hand- of the original attack. picked but flexible and Turing-complete set of gadgets. Return-oriented programming is an active research One novelty in this approach is the use of microgadgets, topic, of interest to both the academic community and which are gadgets restricted to 2 or 3 bytes in length. the security community at large. The original paper on Our approach splits gadgets into several classes of vary- ROP presents a hand-picked set of gadgets, specific to ing sizes (from 1 to more than 800). Only a single gadget a particular version of libc. While it is possible to from each class is required for Turing-completeness. The update the set of gadgets for newer versions, the paper short length of the gadgets, as well as the large size of implies that this would be done manually by visual in- the classes, increase the likelihood of finding all required spection and suggests analyzing other libraries as future gadgets. We also describe an efficient scanner which work. Follow-up research [14, 7, 17, 6] describes au- locates these gadgets in a given program. We then use tomated systems that scan a binary and build a list of this scanner on the /usr/bin directories from several gadgets programatically; in some cases they even com- Linux distributions, to show that many programs indeed pile the attack payload using those gadgets. Some of contain a Turing-complete set of microgadgets, which at- these tools use various semantic analysis techniques on tackers can use to perform arbitrary computations. the gadgets, determining the effects of each gadget be- fore using it. 1 Introduction The evolution of these tools allowed more complex gadgets to be used in an attack, therefore making more In many cases, application code can be useful not only binaries vulnerable to ROP-based attacks. At first, scan- to its legitimate users, but also to attackers which seek ning was limited to single-instruction gadgets [7], but unauthorized access to the application. The intruder can later research lifted that restriction. Gadgets used for at- then either steal sensitive application data, or hijack its tacks have become more and more complex, with one or execution. For the latter purpose, the attacker needs to more of the following traits: find a way to force the application to execute some code chosen by the attacker. Once this happens, the program • Several instructions operating on different registers, can be used for arbitrary purposes, such as attacking or in some cases the same register. This compli- cates gadget analysis and usage, as the tools must smaller gadgets have a higher probability of occurrence. take into account interactions between different in- Therefore, we attempt to build an attack from gadgets structions. that are as small as possible, while still providing sim- plicity and power. The smallest useful gadget on the x86 • Memory operands with arbitrary offsets, which the architecture is 2 bytes long: one byte for the proper in- gadget tool must compensate for using other gad- struction and another for the return instruction (the C3 gets. byte). However, we raised the limit on gadget length to • Extra instructions in a gadget destroying values in 3 bytes, so that we could include more useful operations, registers. Gadget compilers must use extra gadgets like addition and memory accesses. to preserve these values, using register spilling tech- This paper makes the following contributions: niques. • In x 2, we present a Turing-complete ROP-based As opposed to the growing complexity of gadgets gadget set focusing on short gadgets, so-called mi- used in ROP attacks, we propose a very simple set crogadgets. of hand-picked gadgets with the same computational • In x 2.5, we describe the applicability of our ap- power: Turing-completeness. We group the gadgets in proach to attacks which use ROP to allocate an ex- the set into classes. Each class contains a set of micro- ecutable area of memory, then use that area to copy gadgets with equivalent functionality, so that any single and execute a second payload. microgadget from that class is sufficient to implement the functionality of that class. Some microgadgets from • In x 3, we analyze the likelihood of success using the same class differ just by their input operands, e.g., this attack with a case study on several Linux distri- they operate on different registers, while others perform butions and several individual binaries from those the same operation in different ways. For example, one distributions, showing the success rate of our ap- way to clear the carry flag on x86 is to use the CLC in- proach and performance of our scanner. struction, another is to use SAHF. Either of these two op- tions achieves the goal of finding microgadgets to clear • In x 3.4, we present a concrete ROP-based exploit the flag. for PHP 5.3.2, using microgadgets to load and exe- We designed this set with 3 goals: cute another x86 payload. Simplicity so we can easily describe and analyze the set of gadgets, without needing to account for any 2 Description of Gadgets complications or corner cases that other gadget sets Our gadget set implements a simple two-address RISC- present. Our set does not contain gadgets with like instruction set on top of 32-bit x86 microgadgets, memory operand offsets, immediate values or more using CPU registers as operands. It supports only sim- than one instruction. ple addition, subtraction and logical operations, as well Ubiquity (or near-ubiquity) so that the set occurs so fre- as loads and stores to and from any memory location. quently in the binaries that the threat level warrants We allow all general-purpose integer registers except for attention. ESP as microgadget operands. Since each microgadget contains one x86 instruction, other than the RET, we re- Computational power so the attack can be used suc- strict the inputs to 2 different input registers and the out- cessfully for arbitrary code execution or informa- put to one of the input registers. tion disclosure. For every operation in our set, we hand-picked a list We picked Turing-completeness as the measurement of of candidate microgadgets that execute that operation. computational power, since it is the most powerful tar- We derived this hand-picked set from practical experi- get, while also being very clearly specified. While some ence with code generated by compilers such as gcc and existing attacks [19] do not rely on this (instead using Clang/LLVM. Looking at larger binaries, we observe ROP just as a prelude to x86 code injection), we expect the following facts: that analyzing a Turing-complete gadget set will provide • Many useful 2-byte gadgets are very likely to ap- valuable information about less powerful sets. pear, such as POP reg and LAHF. To achieve ubiquity, our set must be found in all bi- naries. While that is not truly possible due to the low • Useful 3-byte gadgets appear in some forms, but not frequency of RET instructions, we designed our set to all; for example, an XOR EAX, EBX might not al- achieve a good balance between all three goals. For this ways appear, but a XOR r1, r2 gadget has a high reason, the key insight in our choice of gadgets is that probability of occurence. 2 Contents Instruction EAX EBX EDX ROP program code XCHG EBX, EAX v1 v2 v3 inial ESP load prologue XCHG EAX, EDX v2 v1 v3 ESP TMP XCHG EBX, EAX v3 v1 v2 ROP program data v1 v3 v2 ROP program stack Table 1: Exchanging EBX with EDX, using EAX as a tem- Supplied as shellcode by a:acker porary. The rightmost columns show the contents of each Generated by ROP program register before each instruction. Figure 1: Layout of the ROP program on the x86 stack. XCHG A property of the x86 instruction set is that The load prologue and ROP program code are supplied EAX, reg is a 1-byte instruction. Therefore, we can by the attacker as shellcode, while the data and stack implement any exchange or move operation between two cells are written by the ROP program itself.