README.txt: A file describing the contents of the directory
ctarget: An executable program vulnerable to code-injection attacks
rtarget: An executable program vulnerable to return-oriented-programming attacks
cookie.txt: An 8-digit hex code that you will use as a unique identifier in your attacks.
farm.c: The source code of your target’s “gadget farm,” which you will use in generating return-oriented programming attacks.
hex2raw: A utility to generate attack strings.
Important points
You must do the assignment on a machine that is similar to the one that generated your targets.
You may only construct gadgets from file rtarget with addresses ranging between those for functions start_farm and end_farm.
Your solutions may not use attacks to circumvent the validation code in the programs.
Your exploit string must not contain byte value 0x0a at any intermediate position, since this is the ASCII code for newline (‘\n’). When Gets encounters this byte, it will assume you intended to terminate the string.
HEX2RAW expects two-digit hex values separated by one or more white spaces. So if you want to create a byte with a hex value of 0, you need to write it as 00. To create the word 0xdeadbeef you should pass “ef be ad de” to HEX2RAW (note the reversal required for little-endian byte ordering).
Target programs
Both CTARGET and RTARGET read strings from standard input. They do so with the function getbuf defined below:
This program is set up in a way that the stack positions will be consistent from one run to the next and so that data on the stack can be treated as executable code.
Level 1
your exploit string will redirect the program to execute an existing procedure.
Your task is to get CTARGET to execute the code for touch1 when getbuf executes its return statement, rather than returning to test.
Level 2
Phase 2 involves injecting a small amount of code as part of your exploit string
Your task is to get CTARGET to execute the code for touch2 rather than returning to test. In this case, however, you must make it appear to touch2 as if you have passed your cookie as its argument.
1 2 3 4 5 6 7 8 9 10 11 12
voidtouch2(unsigned val){ vlevel = 2 ; if (val == cookie){ printf("Touch2!: You called touch2(0x%.8x\n)", val); validate(2); } else{ printf("Misfire: You called touch2(0x%.8x)\n" , val); fail(2); } exit(0); }
Level 3
Phase 3 also involves a code injection attack, but passing a string as argument.
Within the file ctarget there is code for functions hexmatch and touch3 having the following C representations:
voidtouch3(char *sval) { vlevel = 3; /* Part of validation protocol */ if (hexmatch(cookie, sval)) { printf("Touch3!: You called touch3(\"%s\")\n", sval); validate(3); } else { printf("Misfire: You called touch3(\"%s\")\n", sval); fail(3); } exit(0); }
Your task is to get CTARGET to execute the code for touch3 rather than returning to test. You must make it appear to touch3 as if you have passed a string representation of your cookie as its argument.
Part II: Return-Oriented Programming
It uses randomization so that the stack positions differ from one run to another. This makes it impossible to determine where your injected code will be located.
It marks the section of memory holding the stack as nonexecutable, so even if you could set the program counter to the start of your injected code, the program would fail with a segmentation fault.
ROP can bypass ASLR is an old story now!!! It only work for DEP now.
#include<stdio.h> #include<stdlib.h> voidf(){} intmain(){ int * local = alloca(sizeof(int)); printf("stack :%p\n" , local); printf("code section :%p\n" , &main); printf("address of function:%p\n", &f); return0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# for((i=0;i<5;i++)) ./test # doesn't work in bash, it might be a zsh feature $ for i in 1 1 1 1 1 ; do ./test; done; stack :0x7fffa96fc860 code section :0x55d5afc056b1 address of function:0x55d5afc056aa stack :0x7ffd5f52a270 code section :0x55869f60d6b1 address of function:0x55869f60d6aa stack :0x7ffee61d8040 code section :0x5560accf16b1 address of function:0x5560accf16aa stack :0x7ffe122676e0 code section :0x563d9fb196b1 address of function:0x563d9fb196aa stack :0x7fffb0521330 code section :0x5606ed2d56b1 address of function:0x5606ed2d56aa