session:11
Differences
This shows you the differences between two versions of the page.
— | session:11 [2020/07/19 09:49] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== 0x0A: Information Leaks ====== | ||
+ | |||
+ | ===== Slides ===== | ||
+ | |||
+ | ===== Resources ===== | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | ===== Stack Protection (Canaries) ===== | ||
+ | |||
+ | The name comes from canaries (birds) that were used by mining workers when entering mines and were affected by any deadly gases such as methane before humans were. In our case, stack canaries are used to check if a buffer overflow of a stack variable resulted in overriding the return address. The mechanism is based on a (sometimes random) value that is placed on each function' | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | There are 3 main variations of this mechanism: //random//, // | ||
+ | |||
+ | **Random** canaries are generated when programs start, and are stored in a global variable. The global variable //can// be located in a memory region surrounded by unmapped pages - this protects against information leak attacks (see next section) that dump big memory chunks, since accessing the unmapped pages will trigger a segmentation fault. This first method is a little bit hard to implement because the //crt0.o// code (see note below) has to read ''/ | ||
+ | |||
+ | The **terminator** canaries contain string termination characters such as '' | ||
+ | |||
+ | The **random XOR** canaries work by applying a XOR-based algorithm having both a random number (the canary), and the correct address as inputs. The attacker has to both obtain the random number, and apply the algorithm on the new return address before building the payload. | ||
+ | |||
+ | < | ||
+ | **crt0.o** is a set of initialization routines linked into compiled C programs, and executed before calling '' | ||
+ | </ | ||
+ | |||
+ | The 3 well known implementations of stack protections are: StackGuard, ProPolice, and StackShield. | ||
+ | |||
+ | ==== StackGuard ==== | ||
+ | |||
+ | The [[https:// | ||
+ | |||
+ | <note important> | ||
+ | [[http:// | ||
+ | </ | ||
+ | |||
+ | ==== StackShield ==== | ||
+ | |||
+ | The most notable feature of StackShield, | ||
+ | |||
+ | ==== ProPolice ==== | ||
+ | |||
+ | ProPolice, proposed by IBM, started from an implementation similar to StackGuard, but evolved and introduced new features. It is currently the method used by GCC when the '' | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | < | ||
+ | GCC supports 3 levels of stack smashing protection: complete, normal, and strong. The difference lies in the types of function that are protected, with the decision being made by looking at what kinds of local variables are used. Details in [[http:// | ||
+ | </ | ||
+ | |||
+ | Let's compile a small application with GCC's stack protection. | ||
+ | |||
+ | <file c ssp.c> | ||
+ | void func() { | ||
+ | char buffer[1337]; | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | int main() { | ||
+ | func(); | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Compile the file using: | ||
+ | |||
+ | <code bash> | ||
+ | ~$ CFLAGS=' | ||
+ | </ | ||
+ | |||
+ | The disassembled code for '' | ||
+ | |||
+ | <code bash> | ||
+ | ~$ objdump -M intel -d -j .text ./ssp | ||
+ | </ | ||
+ | |||
+ | <code objdump> | ||
+ | 0804841b < | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | |||
+ | We can observe the random value being read from '' | ||
+ | |||
+ | <file text canary.gdb> | ||
+ | set disassembly-flavor intel | ||
+ | file ssp | ||
+ | break *0x804842a | ||
+ | commands | ||
+ | p/x $eax | ||
+ | c | ||
+ | end | ||
+ | run | ||
+ | quit | ||
+ | </ | ||
+ | |||
+ | Run using: | ||
+ | |||
+ | <code bash> | ||
+ | ~$ gdb -x canary.gdb ssp | ||
+ | </ | ||
+ | |||
+ | ==== Defeating Canaries ==== | ||
+ | |||
+ | This [[http:// | ||
+ | |||
+ | For example, the attacker might target: | ||
+ | * parameters function pointers (pushed onto the stack before calling functions) | ||
+ | * the return address | ||
+ | * the old base pointer | ||
+ | * a plain function pointer (local variable) | ||
+ | |||
+ | Buffers could be stored either on the stack, the heap or '' | ||
+ | |||
+ | <note important> | ||
+ | Note that attacks can also be carried out via indirect pointers. The attacker could target a stack local variable, without trying to change the return value, that is later used as a pointer in a write operation. If this write can be fully controlled, the attacker can change the return address without even writing over the canary. | ||
+ | </ | ||
+ | |||
+ | Besides indirect attacks, stack canaries can also be defeated if the attacker is able to exploit an **information leak** vulnerability. | ||
+ | |||
+ | |||
+ | ===== Format String Exploits ===== | ||
+ | |||
+ | <note warning> | ||
+ | In the following, '' | ||
+ | This formality arises from this paper on [[https:// | ||
+ | </ | ||
+ | |||
+ | The scenario that enables format string vulnerabilities is the direct use of unsanitized user provided input as a parameter to functions that can perform special operations based on that input. | ||
+ | Eg. | ||
+ | |||
+ | <code C> | ||
+ | void print_something(char* user_input) | ||
+ | { | ||
+ | printf(user_input); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | vs. | ||
+ | |||
+ | <code C> | ||
+ | void print_something(char* user_input) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | </ | ||
+ | ==== Format functions ==== | ||
+ | A number of format functions are defined in the ANSI C definition. There are some basic format string functions on which more complex functions are based on, some of which are not part of the standard but are widely available. | ||
+ | Real family members: | ||
+ | * fprintf — prints to a FILE stream | ||
+ | * printf — prints to the ‘stdout’ stream | ||
+ | * sprintf — prints into a string | ||
+ | * snprintf — prints into a string with length checking | ||
+ | * vfprintf — print to a FILE stream from a va_arg structure | ||
+ | * vprintf — prints to ‘stdout’ from a va_arg structure | ||
+ | * vsprintf — prints to a string from a va_arg structure | ||
+ | * vsnprintf — prints to a string with length checking from a va_arg structure | ||
+ | |||
+ | == Relatives: == | ||
+ | * setproctitle — set argv[] | ||
+ | * syslog — output to the syslog facility | ||
+ | * others like err*, verr*, warn*, vwarn* | ||
+ | |||
+ | === Use of format functions === | ||
+ | To understand where this vulnerability is common in C code, we have to examine the purpose of format functions. | ||
+ | |||
+ | == Functionality == | ||
+ | * used to convert simple C datatypes to a string representation | ||
+ | * allow to specify the format of the representation | ||
+ | * process the resulting string (output to stderr, stdout, syslog, ...) | ||
+ | |||
+ | == How the format function works == | ||
+ | * the format string controls the behaviour of the function | ||
+ | * it specifies the type of parameters that should be printed | ||
+ | * parameters are saved on the stack (pushed) | ||
+ | * saved either directly (by value), or indirectly (by reference) | ||
+ | |||
+ | == The calling function == | ||
+ | * has to know how many parameters it pushes to the stack, since it has to do the stack correction, when the format function returns | ||
+ | |||
+ | === What exactly is a format string === | ||
+ | A format string is an ASCIIZ string that contains text and format parameters. | ||
+ | Example: | ||
+ | <code C> | ||
+ | printf ("The magic number is: %d\n", 1911); | ||
+ | </ | ||
+ | The text to be printed is "The magic number is:", followed by a format parameter (" | ||
+ | < | ||
+ | |||
+ | Some format parameters: | ||
+ | |||
+ | ^ Parameter | ||
+ | | %d | decimal(int) | ||
+ | | %u | unsigned decimal (unsigned int) | value | | ||
+ | | %x | hexadecimal (unsigned int) | value | | ||
+ | | %s | string ( char *) | reference | ||
+ | | %n | number of bytes written so far, (* int) | reference | ||
+ | |||
+ | The ' | ||
+ | Example: | ||
+ | <code C> | ||
+ | printf ("The magic number is: \x25d\n", | ||
+ | </ | ||
+ | The code above works, because ' | ||
+ | |||
+ | ==== The stack and its role at format strings ==== | ||
+ | The behaviour of the format function is controlled by the format string. The function retrieves the parameters requested by the format string from the stack. | ||
+ | <code C> | ||
+ | printf (" | ||
+ | </ | ||
+ | |||
+ | From within the '' | ||
+ | {{ : | ||
+ | |||
+ | The format function now parses the format string ' | ||
+ | should be evaluated. The string " | ||
+ | |||
+ | ==== What do we control? ==== | ||
+ | Through supplying the format string we are able to control the behaviour of the format function. We now have to examine what exactly we are able to control, and how to use this control to extend this partial control over | ||
+ | the process to full control of the execution flow. | ||
+ | ==== Crash of the program ==== | ||
+ | By utilizing format strings we can easily trigger some invalid pointer access by just supplying a format string like: | ||
+ | <code C> | ||
+ | printf (" | ||
+ | </ | ||
+ | Because ' | ||
+ | implementations offer the ' | ||
+ | ==== Viewing the stack ==== | ||
+ | We can show some parts of the stack memory by using a format string like this: | ||
+ | <code C> | ||
+ | printf (" | ||
+ | </ | ||
+ | This works, because we instruct the printf-function to retrieve five parameters from the stack and display them as 8-digit padded hexadecimal numbers. So a possible output may look like: | ||
+ | < | ||
+ | 40012980.080628c4.bffff7a4.00000005.08059c04 | ||
+ | </ | ||
+ | This is a partial dump of the stack memory, starting from the current bottom of the stack towards the top — assuming the stack grows towards the low addresses. Depending on the size of the format string buffer and the size of the output buffer, you can reconstruct more or less large parts of the stack memory by using this technique. In some cases you can even retrieve the entire stack memory. | ||
+ | A stack dump gives important information about the program flow and local function variables and may be very helpful for finding the correct offsets for a successful exploitation. | ||
+ | ==== Viewing memory at any location | ||
+ | It is also possible to peek at memory locations different from the stack memory. To do this we have to get the format function to display memory from an address we can supply. | ||
+ | This poses two problems to us: | ||
+ | * First, we have to find a format parameter which uses an address (by reference) as stack parameter and displays memory from there | ||
+ | * Secondly, we have to supply that address. | ||
+ | We are lucky in the first case, since the ' | ||
+ | So the remaining problem is, how to get that address on the stack, into the right place. | ||
+ | |||
+ | |||
+ | Our format string is usually located on the stack itself, so we already have near to full control over the space where the format string lies. | ||
+ | The format function internally maintains a pointer to the stack location of the current format parameter. | ||
+ | If we would be able to get this pointer pointing into a memory space we can control, we can supply an address to the ' | ||
+ | <note important> | ||
+ | For re-creating the following attack you should place the string passed to '' | ||
+ | </ | ||
+ | To modify the stack pointer we can simply use dummy parameters that will ' | ||
+ | <code C> | ||
+ | printf (" | ||
+ | </ | ||
+ | The ' | ||
+ | After more or less of this increasing parameters the stack pointer points into our memory: the format string itself. | ||
+ | The format function always maintains the lowest stack frame, so if our buffer lies on the stack at all, it lies above the current stack pointer for sure. | ||
+ | If we choose the number of ‘%08x’ parameters correctly, we could just display memory from an arbitrary address, by appending ' | ||
+ | |||
+ | In our case the address is illegal and would be ' | ||
+ | Example: | ||
+ | |||
+ | < | ||
+ | address = 0x08480110 | ||
+ | address (encoded as 32 bit le string): " | ||
+ | </ | ||
+ | |||
+ | <code C> | ||
+ | printf (" | ||
+ | </ | ||
+ | |||
+ | This will dump memory from 0x08480110 until a NULL byte is reached. By increasing the memory address dynamically we can map out the entire process space. | ||
+ | It is even possible to create a coredump like image of the remote process and to reconstruct a binary from it. It is also helpful to find the cause of unsuccessful exploitation attempts. | ||
+ | |||
+ | If we cannot reach the exact format string boundary by using 4-Byte pops (' | ||
+ | This is analog to the alignment in buffer overflow exploits. | ||
+ | |||
+ | ==== Exploitation - through pure format strings ==== | ||
+ | Our goal in the case of exploitation is to be able to control the instruction pointer, i.e we want to extend our very limited control — the ability to control the behaviour of the format function — to real execution control, that is executing our raw machine code. | ||
+ | Let's take a look at the following code: | ||
+ | <code C> | ||
+ | { | ||
+ | char buffer[512]; | ||
+ | snprintf (buffer, sizeof (buffer), user); | ||
+ | buffer[sizeof (buffer) - 1] = ’\0’; | ||
+ | } | ||
+ | </ | ||
+ | In the code above it is not possible to enlarge our buffer by inserting some kind of ' | ||
+ | At first it may look as if we cannot do much useful things, except crashing the program and inspecting some memory. | ||
+ | |||
+ | Lets remember the format parameters mentioned. There is the ' | ||
+ | The address of the variable is given to the format function by placing an integer pointer as parameter onto the stack. | ||
+ | Example: | ||
+ | <code C> | ||
+ | int i; | ||
+ | printf (" | ||
+ | printf ("i = %d\n", i); | ||
+ | </ | ||
+ | Would print "i = 6". With the same method we used above to print memory from arbitrary addresses, we can write to arbitrary locations: | ||
+ | < | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | With the ' | ||
+ | We do this until this pointer points to the beginning of our format string (to ' | ||
+ | The ' | ||
+ | But if we supply a correct mapped and writeable address this works and we overwrite four bytes (sizeof (int)) at the address: | ||
+ | < | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | The format string above will overwrite four bytes at 0xbfffc8c0 with a small integer number. | ||
+ | We have reached one of our goals: we can write to arbitrary addresses. But we cannot control the number we are writing yet — but this will change. | ||
+ | |||
+ | The number we are writing — the count of characters written by the format function — is dependant on the format string. | ||
+ | Since we control the format string, we can at least take influence on this counter, by writing more or less bytes: | ||
+ | <code C> | ||
+ | int a; | ||
+ | printf (" | ||
+ | /* a == 10 */ | ||
+ | int a; | ||
+ | printf (" | ||
+ | /* a == 150 */ | ||
+ | </ | ||
+ | By using a dummy parameter ' | ||
+ | But for writing large numbers — such as addresses — this is not sufficient, so we have to find a way to write arbitrary data. | ||
+ | |||
+ | An integer number on the x86 architecture is stored in four bytes, which are little-endian ordered, the least significant byte being the first in memory. | ||
+ | So a number like 0x0000014c is stored in memory as: " | ||
+ | |||
+ | For the counter in the format function we can control the least significant byte, the first byte stored in memory by using dummy ' | ||
+ | Example: | ||
+ | <code C> | ||
+ | unsigned char foo[4]; | ||
+ | printf (" | ||
+ | </ | ||
+ | |||
+ | When the printf function returns, foo[0] contains ' | ||
+ | |||
+ | But for an address, there are four bytes that we have to control completely. If we are unable to write four bytes at once, we can try to write a byte a time for four times in a row. | ||
+ | On most CISC architectures it is possible to write to unaligned arbitrary addresses. This can be used to write to the second least significant byte of the memory, where the address is stored. | ||
+ | This would look as follows: | ||
+ | <code C> | ||
+ | unsigned char canary[5]; | ||
+ | unsigned char foo[4]; | ||
+ | memset (foo, 0, sizeof (foo)); | ||
+ | /* 0 * before */ strcpy (canary, " | ||
+ | /* 1 */ printf (" | ||
+ | /* 2 */ printf (" | ||
+ | /* 3 */ printf (" | ||
+ | /* 4 */ printf (" | ||
+ | /* 5 * after */ printf (" | ||
+ | foo[2], foo[3]); | ||
+ | printf (" | ||
+ | canary[1], canary[2], canary[3]); | ||
+ | </ | ||
+ | This returns the output " | ||
+ | By increasing the pointer each time, the least significant byte moves through the memory we want to write to, and allows us to store completely arbitrary data. | ||
+ | As you can see in the first row of the following figure, all eight bytes are not touched yet by our overwrite code. | ||
+ | From the second row on we trigger four overwrites, shifted by one byte to the right for every step. | ||
+ | The last row shows the final desired state: we overwrote all four bytes of our foo array, but while doing so, we destroyed three bytes of the canary array. | ||
+ | We included the canary array just to see that we are overwriting memory we do not want to. | ||
+ | {{ : | ||
+ | Although this method looks complex, it can be used to overwrite arbitrary data at arbitrary addresses. | ||
+ | For explanation we have only used one write per format string until now, but it is also possible to write multiple times within one format string: | ||
+ | <code C> | ||
+ | strcpy (canary, " | ||
+ | printf (" | ||
+ | 1, (int *) & | ||
+ | 1, (int *) & | ||
+ | printf (" | ||
+ | foo[2], foo[3]); | ||
+ | printf (" | ||
+ | canary[1], canary[2], canary[3]); | ||
+ | </ | ||
+ | |||
+ | We use the ' | ||
+ | So we only have to add 16 characters instead of 32 to it, to get the results we desire. | ||
+ | This was a special case, in which all the bytes increased throughout the writes. But we could also write '' | ||
+ | |||
+ | Since we write integer numbers and the order is little endian, only the least significant byte is important in the writes. | ||
+ | By using counters of 0x80, 0x140, 0x220 and 0x310 characters respectivly when “%n” is triggered, we can construct the desired string. | ||
+ | The code to calculate the desired numberof-written-chars counter is this: | ||
+ | <code C> | ||
+ | write_byte += 0x100; | ||
+ | already_written %= 0x100; | ||
+ | padding = (write_byte - already_written) % 0x100; | ||
+ | if (padding < 10) | ||
+ | padding += 0x100; | ||
+ | </ | ||
+ | |||
+ | Where ' | ||
+ | Example: | ||
+ | <code C> | ||
+ | write_byte = 0x7f; | ||
+ | already_written = 30; | ||
+ | write_byte += 0x100; /* write_byte is 0x17f now */ | ||
+ | already_written %= 0x100; /* already_written is 30 */ | ||
+ | |||
+ | /* afterwards padding is 97 (= 0x61) */ | ||
+ | padding = (write_byte - already_written) % 0x100; | ||
+ | if (padding < 10) | ||
+ | padding += 0x100; | ||
+ | </ | ||
+ | |||
+ | Now a format string of “%97u” would increase the ' | ||
+ | The final check if the padding is below ten deserves some attention. A simple integer output, such as " | ||
+ | If the required length is larger than the padding we specify, say we want to output ' | ||
+ | By ensuring our padding is always larger than 10, we can keep an always accurate number of ‘already_written’, | ||
+ | |||
+ | ==== A general method to exploit format strings vulnerabilities ==== | ||
+ | The only remaining thing to exploit such vulnerabilities in a hands-on practical way is to put the arguments into the right order on the stack and use a stackpop sequence to increase the stack pointer. | ||
+ | It should look like: | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | Where: | ||
+ | * **stackpop** The sequence of stack popping parameters that increase the stack pointer. Once the stackpop has been processed, the format function internal stack pointer points to the beginning of the dummy-addr-pair strings. | ||
+ | * **dummy-addr-pair** Four pairs of dummy integer values and addresses to write to. The addresses are increasing by one with each pair, the dummy integer value can be anything that does not contain NULL bytes. | ||
+ | * **write-code** The part of the format string that actually does the writing to the memory, by using ' | ||
+ | |||
+ | The write code has to be modified to match the number of bytes written by the stackpop, since the stackpop wrote already characters to the output when the format function parses the write-code — the format function counter does not start at zero, and this has to be considered. | ||
+ | ==== Direct Parameter Access ==== | ||
+ | There is a huge simplification which is known as ' | ||
+ | method to format string exploitation. | ||
+ | The direct parameter access is controlled by the ' | ||
+ | <code C> | ||
+ | printf (" | ||
+ | </ | ||
+ | |||
+ | Prints ' | ||
+ | |||
+ | <code C> | ||
+ | char foo[4]; | ||
+ | printf (" | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | 1, | ||
+ | (int *) & | ||
+ | (int *) & | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Generalizing format string exploits ==== | ||
+ | The '' | ||
+ | In general, any system where user input affects program execution and data access in a custom way can be susceptible to such a vulnerability. Other specialized examples can be considered: | ||
+ | * SQL injections | ||
+ | * XSS injections | ||
+ | ===== Tasks ===== | ||
+ | |||
+ | ==== Stack Canaries ==== | ||
+ | |||
+ | Download the archive with the tasks at the top of the page. The binaries should be fairly easy to reverse engineer. You can use any tool. | ||
+ | |||
+ | === Task 1 === | ||
+ | |||
+ | The '' | ||
+ | |||
+ | === Task 2 === | ||
+ | |||
+ | The '' | ||
+ | |||
+ | <note warning> | ||
+ | You need to use the 32 bit VM to solve the second part of this task. | ||
+ | </ | ||
+ | |||
+ | <note warning> | ||
+ | '' | ||
+ | </ | ||
+ | |||
+ | <note tip>In case you need some help on these, please take a look at the {{: | ||
+ | |||
+ | |||
+ | ==== Task 3 - Format Strings ==== | ||
+ | Download the archive with the tasks at the top of the page containing 5 binaries exhibiting a format string vulnerability. Analyze what each binary does using the methods already familiar to you and try to determine the exact format string that will lead to the desired result. | ||
+ | <note important> | ||
+ | The difficulty of the task associated with each binary increases with the number of the binary. | ||
+ | </ | ||
+ | <note tip> | ||
+ | shows you the invalid address associated with a SIGSEGV signal. | ||
+ | </ | ||