20 7 int func1(int a, int b, int c) { char buffer[8]; // declare a character array of 8 bytes gets(buffer); // read user input string return 0; // return zero } buffer[8] 0x00000000 0xffffffff EBP ret-addr a Stack grows in this way b c Last Stack Frame Current Stack Frame ... ... 0x41414141 0x41414141 0x00000000 0xffffffff 0x41414141 0x41414141 0x41414141 Stack grows in this way b c Last Stack Frame Current Stack Frame ... ...
20 8 buffer[8] 0x00000000 0xffffffff EBP ret-addr a Stack grows in this way b c Last Stack Frame Current Stack Frame ... ... Canary (?) 0x41414141 0x41414141 0x00000000 0xffffffff 0x41414141 0x41414141 a Stack grows in this way b c Last Stack Frame Current Stack Frame ... ... 0x41414141
2: iv = ''.join(random.choice(string.hexdigits) for _ in range(16)) 3: flag = "ais3{NEVERPHDNEVERPHDNEVERPHD..}" # Not real flag ... 4: 5: def encrypt(p): 6: return AES.new(key, AES.MODE_OFB, iv).encrypt(p) ... 7: print encrypt(flag).encode("hex") 8: while True: ... 9: p = ''.join(random.choice(string.lowercase) for _ in range(32)) 10: print encrypt(p).encode("hex")
The list containing n ciphertexts (except the first one) c = [ c1 , c2 , c3 , …, cn ] Suppose cu,v represents the vth byte in ciphertext cu , 1 ≤ u ≤ n pad = ""; for v = 1 to 32: for x = 0 to 255: if x XOR cu,v is a lowercase alphabet for all u in [1, n] pad = pad + x break use pad to decrypt the real ciphertext and obtain the flag
the number first!! • Strategy #1: Play with the game • Pwntools: recv, send … try all possible combinations • Strategy #2: Use the random number trick • Remember we have: srand(time(0)) + rand()? • In python, we can do: 32 1: from ctypes import * 2: cdll.LoadLibrary("libc.so.6") 3: libc = CDLL("libc.so.6") 4: libc.srand(libc.time(0)) 5: print libc.rand();
# this is from pwntools … 2: num = "" 3: while len(num) < 4: 4: while True: 5: d = chr(libc.rand() % 10 + 48) 6: if len(set(num + d)) == len(num + d): 7: num = num + d 8: break 9: print r.recv() 10: print num 11: r.send(num + '\n') 12: print r.recv() • Use ntpdateto synchronize your system clock • You may need to uncheck "Hardware Clock in UTC Time" if you are playing with VirtualBox or other virtual machines …
0x00000000 0xffffffff 0x41414141 Jump to stack Stack grows in this way ... ... 0x90909090 ... ... 0x90909090 shellcode s[24] 0x00000000 0xffffffff EBP ret-addr Stack grows in this way Last Stack Frame Current Stack Frame ... ...
would not like to guess any more L • Ask 'gets()' to do something for us • Remember that 'gets()' requires one arguments – the address to store the user input string 36
want the stack to looks like … 37 s[24] 0x00000000 0xffffffff EBP addr of gets() Stack grows in this way ... ... ret-addr argument #1 return addr after gets() address for gets() to fill garbage
use malloc() • dump • add • Allocate header first (32 bytes) • Allocate thesis-text according to the given length • edit • Modify header content • Reallocate thesis-text if necessary • remove 44 thesis text ... thesis text ... ... ... 0x00000000 0xffffffff name[20] age length *thesis name[20] age length *thesis Record #0 Record #1
Edit records #0 3. Add one more record thesis text ... thesis text ... ... ... 0x00000000 0xffffffff name (aaa) age length (32) *thesis name (bbb) age length (32) *thesis Record #0 Record #1 thesis text ... freed ... ... 0x00000000 0xffffffff name (aaa) age length (32) *thesis name (bbb) age length (32) *thesis Record #0 Record #1 thesis text ... thesis text ... ... 0x00000000 0xffffffff name (aaa) age length (32) *thesis name (bbb) age length (32) *thesis Record #0 Record #1 name (ccc) age length (32) *thesis Record #2 Record #2
real address of atoi in memory • We can then know the C library base • Real address of atoi minus atoi’s offset in C library • Use objdump -d libc.so.6 to get atoi’s offset • From objdump -d phddb, we got the GOT entry address for atoi is 0x804b03c 47 08048560 <atoi@plt>: 8048560: ff 25 3c b0 04 08 jmp *0x804b03c 8048566: 68 60 00 00 00 push $0x60 804856b: e9 20 ff ff ff jmp 8048490 <_init+0x30>
thesis text using: • "A" * 24 • 0x20 (length) • 0x804b03c • GOT entry is a function pointer to the real address of a function (in .so) • Dump record #2 • Reveal atoi(?) 48 atoi (?) c99_scanf ... ... GOT 0x804b03c 0x804b038 0x804b040 thesis text ... thesis text ... ... 0x00000000 0xffffffff name (aaa) age length (32) *thesis name (bbb) age length (32) *thesis Record #0 Record #1 name (ccc) age length (32) *thesis Record #2 Record #2
does read() + atoi() • We can replace atoi’s GOT entry value to any functionwe want to call • Replace atoi’s GOT entry value with the real address of system(), and then send 'sh\n' • Simple arithmetic • atoi‘s real address = 0xf7e59560 • atoi offset in C library = 0x31560 • system offset in C library = 0x3fcd0 • system’s real address= 0xf7e59560 – 0x31560 + 0x3fcd0 = 0xf7e67cd0 50
thesis text using: • "A" * 24 • 0x20 (length) • 0xf7e67cd0 • read() + atoi() now becomes read() + system() • Send 'sh\n' 51 atoi (0xf7e59560) c99_scanf ... ... GOT 0x804b03c 0x804b038 0x804b040 thesis text ... thesis text ... ... 0x00000000 0xffffffff name (aaa) age length (32) *thesis name (bbb) age length (32) *thesis Record #0 Record #1 name (ccc) age length (32) *thesis Record #2 Record #2 system (0xf7e67cd0)
Army • Capture The Flag (CTF) • Information security competition • Cyber Grand Challenge (CGC) • All-computer CTF tournament • Held by DARPA of US DoD with the DEFCON Conference in Las Vegas in 2016 55
rules • Automatic attack and defense • Automatic Attack • Analyze the program binary to find the failure • Generate exploit • Payload to bypass mitigation • Automatic Defense • Analyze the program to find the fault • Find the faulty point • Patch the fault in binary level 56
search and chain gadgets tool Tool Compare Exploit Strengthening ROPgadget Gadget Type Long/Short Gadgets Short Gadgets Payload Type Turing complete ROP Payload API One type payload Integrate CRAX + Metasploit
must patch the binary program without source code • There are different tricks to patch different faults • We must analyze the type of fault before patching it • Our CRS is targeted at stack-based buffer overflow 72
CGC • Fault localization • Binary Patch • Our method can succeed in patching five challenge binaries • Only fail in one availability test • All security tests pass 76