实验三 ShellShock 攻击实验
ShellShock 攻擊實驗
沙雨濟
一、 實驗描述
2014年9月24日,Bash中發(fā)現(xiàn)了一個嚴重漏洞shellshock,該漏洞可用于許多系統(tǒng),并且既可以遠程也可以在本地觸發(fā)。在本實驗中,學生需要親手重現(xiàn)攻擊來理解該漏洞,并回答一些問題。
二、 預備知識
1. 什么是ShellShock?
Shellshock,又稱Bashdoor,是在Unix中廣泛使用的Bash shell中的一個安全漏洞,首次于2014年9月24日公開。許多互聯(lián)網(wǎng)守護進程,如網(wǎng)頁服務器,使用bash來處理某些命令,從而允許攻擊者在易受攻擊的Bash版本上執(zhí)行任意代碼。這可使攻擊者在未授權的情況下訪問計算機系統(tǒng)。——摘自維基百科
2. 進行實驗所需的準備
1. 環(huán)境搭建
以root權限安裝4.1版bash(4.2版本以上的漏洞已經(jīng)被堵上了) bash4.1 下載地址:http://labfile.oss.aliyuncs.com/bash-4.1.tar.gz
下載
# wget http://labfile.oss.aliyuncs.com/bash-4.1.tar.gz安裝
# tar xf bash-4.1.tar.gz# cd bash-4.1# ./configure# make & make install鏈接
# rm /bin/bash# ln -s /usr/local/bin/bash /bin/bash到這里就安裝完了,接下來檢測是否存在shellshock漏洞。
$ env x='() { :;}; echo vulnerable' bash -c "echo this is a test "輸出vulnerable的話,說明bash有漏洞。
最后,讓/bin/sh 指向/bin/bash.
$ sudo ln -sf /bin/bash /bin/sh現(xiàn)在一切就緒,進入下一步吧。
2.預備知識
了解bash自定義函數(shù),只需要函數(shù)名就能夠調用該函數(shù)。
$ foo() { echo bar; } $ foo > bar這個時候的Bash的環(huán)境變量:
KEY = foo VALUE = () { echo bar; }來看看ShellShock漏洞的真身:
export foo=’() { :; }; echo Hello World’ bash >Hello World怎么樣?看明白了沒?為什么調用bash的時候輸出Hello World了呢? 瞧瞧他內部的情況:
KEY = foo VALUE = () { :; }; echo Hello Worldbash讀取了環(huán)境變量,在定義foo之后直接調用了后面的函數(shù)。 一旦調用bash,自定義的語句就直接觸發(fā)。
到了這,你有想到什么么,聯(lián)系之前的Set-UID課程。 對!干壞事的孩子會被警察叔叔抓走的:)
不多說了,來get root權限吧!
三、 實驗內容
1.攻擊Set-UID程序
本實驗中,我們通過攻擊Set-UID程序來獲得root權限。 首先,確保安裝了帶有漏洞的bash版本,并讓/bin/sh 指向/bin/bash.
$ sudo ln -sf /bin/bash /bin/sh請編譯下面這段代碼,并設置其為Set-UID程序,保證它的所有者是root。我們知道system()函數(shù)將調用"/bin/sh -c" 來運行指定的命令, 這也意味著/bin/bash 會被調用,你能夠利用shellshock漏洞來獲取權限么?
#include <stdio.h> void main() { setuid(geteuid()); // make real uid = effective uid. system("/bin/ls -l"); }我們注意到這里使用了setuid(geteuid()) 來使real uid = effective uid,這在Set-UID程序中不是普遍實踐,但它確實有時會發(fā)生。 先自己試著hack一下:) …… …… …… …… …… …… 以下是hack過程。
如果 setuid(geteuid()) 語句被去掉了,再試試看攻擊,我們還能夠拿到權限么? #include <stdio.h> void main() { system("/bin/ls -l"); }
(hack過程與step1完全一樣,sh0ck是編譯后的程序)
失敗啦!這就說明如果 real uid 和 effective uid 相同的話,定義在環(huán)境變量中的內容在該程序內有效,那樣shellshock漏洞就能夠被利用了。但是如果兩個uid不同的話,環(huán)境變量失效,就無法發(fā)動攻擊了,這可以從bash的源代碼中得到印證(variables.c,在308到369行之間)請指出是哪一行導致了這樣的不同,并說明bash這樣設計的原因。
這里給出這部分代碼
/* Initialize the shell variables from the current environment.If PRIVMODE is nonzero, don't import functions from ENV orparse $SHELLOPTS. */ void initialize_shell_variables (env, privmode) char **env; int privmode; { char *name, *string, *temp_string; int c, char_index, string_index, string_length; SHELL_VAR *temp_var; create_variable_tables (); for (string_index = 0; string = env[string_index++]; ) { char_index = 0; name = string; while ((c = *string++) && c != '=') ; if (string[-1] == '=') char_index = string - name - 1; /* If there are weird things in the environment, like `=xxx' or a string without an `=', just skip them. */ if (char_index == 0) continue; /* ASSERT(name[char_index] == '=') */ name[char_index] = '\0'; /* Now, name = env variable name, string = env variable value, and char_index == strlen (name) */ temp_var = (SHELL_VAR *)NULL; /* If exported function, define it now. Don't import functions from the environment in privileged mode. */ if (privmode == 0 && read_but_dont_execute == 0 && STREQN ("() {", string, 4)) { string_length = strlen (string); temp_string = (char *)xmalloc (3 + string_length + char_index); strcpy (temp_string, name); temp_string[char_index] = ' '; strcpy (temp_string + char_index + 1, string); parse_and_execute (temp_string, name, SEVAL_NONINT|SEVAL_NOHIST); /* Ancient backwards compatibility. Old versions of bash exported functions like name()=() {...} */ if (name[char_index - 1] == ')' && name[char_index - 2] == '(') name[char_index - 2] = '\0'; if (temp_var = find_function (name)) { VSETATTR (temp_var, (att_exported|att_imported)); array_needs_making = 1; } else report_error (_("error importing function definition for `%s'"), name); /* ( */ if (name[char_index - 1] == ')' && name[char_index - 2] == '\0') name[char_index - 2] = '('; /* ) */ }摘出其中關鍵部分并簡化
void initialize_shell_variables(){ // 循環(huán)遍歷所有環(huán)境變量 for (string_index = 0; string = env[string_index++]; ) { /*...*/ /* 如果有export過的函數(shù), 在這里定義 */ /* 無法導入在特權模式下(root下)定義的函數(shù) */ if (privmode == 0 && read_but_dont_execute == 0 && STREQN (“() {“, string, 4)) { [...] // 這里是shellshock發(fā)生的地方 // 傳遞函數(shù)定義 + 運行額外的指令 parse_and_execute (temp_string, name, SEVAL_NONINT|SEVAL_NOHIST); [...] } }就是上述那一行判斷邏輯導致了兩者的不同,primode即私有模式,要求real uid 與 effective uid保持一致。至于如此設計的原因,小編覺得。。別人家的環(huán)境變量自己都不知道內容是些什么,import了也沒用吧。。。。小編想的比較天真,你一定有更好的答案:)
至于ShellShock漏洞的防御方法么,快去升級你家Bash啦。
四、 練習
在實驗樓環(huán)境安步驟進行實驗,并截圖
?
五、實驗總結
至此,Linux實驗就已經(jīng)全部結束了,從三次實驗中,我學到了很多實際應用在Linux內核中的知識,比如緩沖區(qū)溢出漏洞,SET-UID程序漏洞,還有關于Bash防御ShellShock漏洞等,但有一些實驗中的問題我還沒完全弄明白,在接下來的學習中,我會留意。
?
?
?
轉載于:https://www.cnblogs.com/Diky/p/4523287.html
總結
以上是生活随笔為你收集整理的实验三 ShellShock 攻击实验的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《构建之法》读书笔记
- 下一篇: 【Auto Layout】Xcode6及