python pwntools 사용법

·2022년 11월 9일
0

pwntools 설치 - python2, 3 둘 다 지원한다. (2022.11.10 기준)

pip{3} install pwntools

pwntools를 사용하기 위해 pwn을 임포트한다.

from pwn import *

추가)
python3에서 발생하는 ByteWarning(보통 send{line} 에서 bytes가 아닌 str을 인자로 넣었을 때)을 supress할 때 쓴다.

import warnings
warnings.filterwarnings('ignore')

pwntools의 context를 환경에 맞게 재설정 해줄 때 사용한다.

context.clear()
context.update(arch='amd64', os='linux', log_level='debug')

로컬 파일과 연결할 때

p = process("./whysoserious", aslr=False, env={'LD_PRELOAD': './libc.so.6'})

추가)
ELF(): ELF정보를 가져오고 바이너리에 적용되어 있는 보호 기법을 출력해준다.
e.libc: ELF 바이너리가 사용하고 있는 glibc의 정보를 가져오고 보호 기법을 출력해준다.

# e.bss(), e.plt, e.got, e.symbols 등 다양한 정보를 가져올 수 있다.
e = ELF("./whysoserious")

# 보통 l.symbols를 통해 심볼의 오프셋을 구한다.
l = e.libc 

소켓으로 연결할 때

r = remote("localhost", 516)

SSH로 연결할 때

s = ssh("pi", "192.168.35.250", port=22, password="mainenginecutoff!")

를 사용함으로써 elf 파일이나 remote 서버의 소켓이나 ssh에 연결 할 수 있다.

연결된 곳으로부터 데이터를 받아오는 방법:

  1. recv()를 사용하여 한 바이트 받기.
r = remote("localhost", 516)
data = r.recv() # 인자값에 n넣어주면 n만큼 받아요!
data = r.recvn(6) # 정확하게 얼마큼 받아야 하는지 알고 있다면 recvn() 사용!
  1. recvline()를 사용하여 한 줄 받기.
r = remote("localhost", 516)
line = s.recvline() # \n까지 계속 받을 수 있다!
  1. recvuntil(param)을 사용하여 받아오는 데이터 값에서 인자 값이 나올 때 까지 데이터 받기.
r = remote("localhost", 516)
until_number = s.recvuntil("1024")

연결된 곳으로부터 데이터를 보내는 방법:

  1. send(data)을 사용하여 data 보내기.
r.send(data)
  1. sendline(data)을 사용하여 data 한 줄 보내기.
r.sendline(data)
  1. send{line}after(delim, data)를 이용하여 정확한 때 데이터 보내기.
r.sendafter("What's your name? ", "bogdam")
r.sendlineafter("Enter your code name below:\n", "bogdam")

송,수신 제어를 유저에게 넘기기 - interactive()

r = remote("localhost", 516)
r.interactive()

template?

from pwn import *
import warnings
warnings.filterwarnings('ignore')

context.clear()
context.update(arch='amd64', os='linux', log_level='debug')

binary = ""
# r = process("./" + binary)
r = remote("", 0)
e = ELF("./" + binary)
l = e.libc

# declare gadgets and symbols that you are using.
pr = p64()
ret = p64()

main_sym = p64(e.symbols['main'])
read_got = p64(e.got['read'])

# enter your exploit code stuff.

r.interactive()

0개의 댓글