SEED实验系列:ShellShock 攻击实验
實(shí)驗(yàn)樓課程原文鏈接:https://www.shiyanlou.com/courses/230,內(nèi)容能夠得到你的喜歡,我們感到非常高興的,也十分歡迎您分享轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)保留實(shí)驗(yàn)樓課程原文鏈接。
一、 實(shí)驗(yàn)描述
2014年9月24日,Bash中發(fā)現(xiàn)了一個(gè)嚴(yán)重漏洞shellshock,該漏洞可用于許多系統(tǒng),并且既可以遠(yuǎn)程也可以在本地觸發(fā)。在本實(shí)驗(yàn)中,學(xué)生需要親手重現(xiàn)攻擊來(lái)理解該漏洞,并回答一些問題。
二、 預(yù)備知識(shí)
1. 什么是ShellShock?
Shellshock,又稱Bashdoor,是在Unix中廣泛使用的Bash shell中的一個(gè)安全漏洞,首次于2014年9月24日公開。許多互聯(lián)網(wǎng)守護(hù)進(jìn)程,如網(wǎng)頁(yè)服務(wù)器,使用bash來(lái)處理某些命令,從而允許攻擊者在易受攻擊的Bash版本上執(zhí)行任意代碼。這可使攻擊者在未授權(quán)的情況下訪問計(jì)算機(jī)系統(tǒng)。——摘自維基百科
2. 進(jìn)行實(shí)驗(yàn)所需的準(zhǔn)備
1. 環(huán)境搭建
以root權(quán)限安裝4.1版bash(4.2版本以上的漏洞已經(jīng)被堵上了) bash4.1 下載地址:http://ftp.gnu.org/gnu/bash/bash-4.1.tar.gz?
 
 
下載
# wget http://ftp.gnu.org/gnu/bash/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
到這里就安裝完了,接下來(lái)檢測(cè)是否存在shellshock漏洞。
$ env x='() { :;}; echo vulnerable' bash -c "echo this is a test "輸出vulnerable的話,說(shuō)明bash有漏洞。
最后,讓/bin/sh 指向/bin/bash.
$ sudo ln -sf /bin/bash /bin/sh
現(xiàn)在一切就緒,進(jìn)入下一步吧。
2.預(yù)備知識(shí)
了解bash自定義函數(shù),只需要函數(shù)名就能夠調(diào)用該函數(shù)。
$ foo() { echo bar; } $ foo > bar
這個(gè)時(shí)候的Bash的環(huán)境變量:
KEY = foo VALUE = () { echo bar; }
來(lái)看看ShellShock漏洞的真身:
export foo=’() { :; }; echo Hello World’ bash >Hello World
怎么樣?看明白了沒?為什么調(diào)用bash的時(shí)候輸出Hello World了呢? 瞧瞧他內(nèi)部的情況:
KEY = foo VALUE = () { :; }; echo Hello World
bash讀取了環(huán)境變量,在定義foo之后直接調(diào)用了后面的函數(shù)。 一旦調(diào)用bash,自定義的語(yǔ)句就直接觸發(fā)。
到了這,你有想到什么么,聯(lián)系之前的Set-UID課程。 對(duì)!干壞事的孩子會(huì)被警察叔叔抓走的:)
不多說(shuō)了,來(lái)get root權(quán)限吧!
三、 實(shí)驗(yàn)內(nèi)容
1.攻擊Set-UID程序
本實(shí)驗(yàn)中,我們通過攻擊Set-UID程序來(lái)獲得root權(quán)限。 首先,確保安裝了帶有漏洞的bash版本,并讓/bin/sh 指向/bin/bash.
$ sudo ln -sf /bin/bash /bin/sh
請(qǐng)編譯下面這段代碼,并設(shè)置其為Set-UID程序,保證它的所有者是root。我們知道system()函數(shù)將調(diào)用"/bin/sh -c" 來(lái)運(yùn)行指定的命令, 這也意味著/bin/bash 會(huì)被調(diào)用,你能夠利用shellshock漏洞來(lái)獲取權(quán)限么?
#include <stdio.h> void main() {setuid(geteuid()); // make real uid = effective uid.system("/bin/ls -l"); }
我們注意到這里使用了setuid(geteuid()) 來(lái)使real uid = effective uid,這在Set-UID程序中不是普遍實(shí)踐,但它確實(shí)有時(shí)會(huì)發(fā)生。 先自己試著hack一下:) …… …… …… …… …… …… 以下是hack過程。?
如果 setuid(geteuid()) 語(yǔ)句被去掉了,再試試看攻擊,我們還能夠拿到權(quán)限么?
#include <stdio.h> void main() {system("/bin/ls -l"); }
 
 
(hack過程與step1完全一樣,sh0ck是編譯后的程序)
失敗啦!這就說(shuō)明如果 real uid 和 effective uid 相同的話,定義在環(huán)境變量中的內(nèi)容在該程序內(nèi)有效,那樣shellshock漏洞就能夠被利用了。但是如果兩個(gè)uid不同的話,環(huán)境變量失效,就無(wú)法發(fā)動(dòng)攻擊了,這可以從bash的源代碼中得到印證(variables.c,在308到369行之間)請(qǐng)指出是哪一行導(dǎo)致了這樣的不同,并說(shuō)明bash這樣設(shè)計(jì)的原因。
這里給出這部分代碼
/* 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 astring 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, andchar_index == strlen (name) */temp_var = (SHELL_VAR *)NULL;/* If exported function, define it now. Don't import functions fromthe 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 exportedfunctions 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;}elsereport_error (_("error importing function definition for `%s'"), name);/* ( */if (name[char_index - 1] == ')' && name[char_index - 2] == '\0')name[char_index - 2] = '('; /* ) */}
 摘出其中關(guān)鍵部分并簡(jiǎn)化
 
就是上述那一行判斷邏輯導(dǎo)致了兩者的不同,primode即私有模式,要求real uid 與 effective uid保持一致。至于如此設(shè)計(jì)的原因,小編覺得。。別人家的環(huán)境變量自己都不知道內(nèi)容是些什么,import了也沒用吧。。。。小編想的比較天真,你一定有更好的答案:)
至于ShellShock漏洞的防御方法么,快去升級(jí)你家Bash啦。
四、 練習(xí)
在實(shí)驗(yàn)樓環(huán)境安步驟進(jìn)行實(shí)驗(yàn),并截圖
您已經(jīng)完成本課程的所有實(shí)驗(yàn),干的漂亮!
License
本課程所涉及的實(shí)驗(yàn)來(lái)自Syracuse SEED labs,并在此基礎(chǔ)上為適配實(shí)驗(yàn)樓網(wǎng)站環(huán)境進(jìn)行修改,修改后的實(shí)驗(yàn)文檔仍然遵循GNU Free Documentation License。
本課程文檔github鏈接:https://github.com/shiyanlou/seedlab
附Syracuse SEED labs版權(quán)聲明:
Copyright ? 2014 Wenliang Du, Syracuse University. The development of this document is/was funded by the following grants from the US National Science Foundation: No. 1303306 and 1318814. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation. A copy of the license can be found athttp://www.gnu.org/licenses/fdl.html.
總結(jié)
以上是生活随笔為你收集整理的SEED实验系列:ShellShock 攻击实验的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: SEED实验系列:缓冲区溢出漏洞试验
- 下一篇: SEED实验系列:Collabtive系
