SEED实验系列:ShellShock 攻击实验
實驗樓課程原文鏈接:https://www.shiyanlou.com/courses/230,內容能夠得到你的喜歡,我們感到非常高興的,也十分歡迎您分享轉載,轉載請保留實驗樓課程原文鏈接。
一、 實驗描述
2014年9月24日,Bash中發現了一個嚴重漏洞shellshock,該漏洞可用于許多系統,并且既可以遠程也可以在本地觸發。在本實驗中,學生需要親手重現攻擊來理解該漏洞,并回答一些問題。
二、 預備知識
1. 什么是ShellShock?
Shellshock,又稱Bashdoor,是在Unix中廣泛使用的Bash shell中的一個安全漏洞,首次于2014年9月24日公開。許多互聯網守護進程,如網頁服務器,使用bash來處理某些命令,從而允許攻擊者在易受攻擊的Bash版本上執行任意代碼。這可使攻擊者在未授權的情況下訪問計算機系統。——摘自維基百科
2. 進行實驗所需的準備
1. 環境搭建
以root權限安裝4.1版bash(4.2版本以上的漏洞已經被堵上了) 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
到這里就安裝完了,接下來檢測是否存在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
現在一切就緒,進入下一步吧。
2.預備知識
了解bash自定義函數,只需要函數名就能夠調用該函數。
$ foo() { echo bar; } $ foo > bar
這個時候的Bash的環境變量:
KEY = foo VALUE = () { echo bar; }
來看看ShellShock漏洞的真身:
export foo=’() { :; }; echo Hello World’ bash >Hello World
怎么樣?看明白了沒?為什么調用bash的時候輸出Hello World了呢? 瞧瞧他內部的情況:
KEY = foo VALUE = () { :; }; echo Hello World
bash讀取了環境變量,在定義foo之后直接調用了后面的函數。 一旦調用bash,自定義的語句就直接觸發。
到了這,你有想到什么么,聯系之前的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()函數將調用"/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程序中不是普遍實踐,但它確實有時會發生。 先自己試著hack一下:) …… …… …… …… …… …… 以下是hack過程。?
如果 setuid(geteuid()) 語句被去掉了,再試試看攻擊,我們還能夠拿到權限么?
#include <stdio.h> void main() {system("/bin/ls -l"); }
(hack過程與step1完全一樣,sh0ck是編譯后的程序)
失敗啦!這就說明如果 real uid 和 effective uid 相同的話,定義在環境變量中的內容在該程序內有效,那樣shellshock漏洞就能夠被利用了。但是如果兩個uid不同的話,環境變量失效,就無法發動攻擊了,這可以從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 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] = '('; /* ) */}
摘出其中關鍵部分并簡化
就是上述那一行判斷邏輯導致了兩者的不同,primode即私有模式,要求real uid 與 effective uid保持一致。至于如此設計的原因,小編覺得。。別人家的環境變量自己都不知道內容是些什么,import了也沒用吧。。。。小編想的比較天真,你一定有更好的答案:)
至于ShellShock漏洞的防御方法么,快去升級你家Bash啦。
四、 練習
在實驗樓環境安步驟進行實驗,并截圖
您已經完成本課程的所有實驗,干的漂亮!
License
本課程所涉及的實驗來自Syracuse SEED labs,并在此基礎上為適配實驗樓網站環境進行修改,修改后的實驗文檔仍然遵循GNU Free Documentation License。
本課程文檔github鏈接:https://github.com/shiyanlou/seedlab
附Syracuse SEED labs版權聲明:
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.
總結
以上是生活随笔為你收集整理的SEED实验系列:ShellShock 攻击实验的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SEED实验系列:缓冲区溢出漏洞试验
- 下一篇: pygame开发PC端微信打飞机游戏