Post

pwntools

https://github.com/Gallopsled/pwntools

https://docs.pwntools.com/en/stable/intro.html

https://docs.pwntools.com/en/stable/globals.html

http://docs.pwntools.com/en/stable/tubes.html

  • 32bit에서도 돌아가기는 하나 정식으로 지원하지는 않음.
  • canary break같은 brute force는 threading이나 asyncio를 사용해야 하는데 threading의 경우 될 때도 있고 안될 때도 있음. 직접 짠건 잘 되는걸로 보아 시스템 상태에 더 민감하게 반응하는 듯? 그래서 이런 경우 직접 코딩하는게 낫다.
  • debug 기능이 아주 이상하기 때문에 그냥 안쓰는게 낫다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from pwn import \*

  

def exploit(r):
r.clean()        # buffer clean 잘 해줄 것.
'''interactive() 직전에 호출하게 되는 recv 계열은 반드시 timeout 지정해주어야 함.
지정 안하면 계속 대기하기 때문에 interactive()가 실행되지 않는다.'''
r.recvuntil(': ', timeout=1)
r.sendline("abcd") # 마지막에 개행 붙는다.
if r.can\_recv():
pass
r.interactive()

  


  

if len(sys.argv) > 1:
r = remote(sys.argv[1], int(sys.argv[2]))
else:
r = process(['./binary'], env={"LD\_PRELOAD":"./libc.so.6"})

  

#pause()
expolit(r)

shellcode
1
2
3
4
5
6
7
8
9
10
>>> print shellcraft.sh()
/\* execve(path='/bin///sh', argv=['sh'], envp=0) \*/...push 0x68...
>>> print enhex(asm(shellcraft.sh()))
6a68682f2f2f73682f62696e89e368010101018134247269010131c9516a045901e15189e131d26a0b58cd80
>>> shellcraft.arm.nop()
u'    orr r1, r1, r1\n'
>>> asm(shellcraft.arm.nop())
직접 assembly를 컴파일해서 기계어를 얻는 방식인  하다.
해서 현재 arch와 일치하지 않는 기계어는 얻을  없는 .

직접 만들 수 없는 경우 그냥http://shell-storm.org/shellcode/에서 찾는다.

libc symbol 계산
1
2
3
4
>>> libc = ELF("/lib/x86\_64-linux-gnu/libc-2.19.so")
>>> hex(libc.symbols["\_\_free\_hook"])
'0x3c0a10'

telnetlib의 interact

pwntools 안쓰고 interact하려면, telnetlib을 사용한다.

1
2
3
4
5
6
7
8
import telnetlib

  

t = telnetlib.Telnet()
t.sock = s    # socket object
t.interact()

This post is licensed under CC BY 4.0 by the author.