2019-2020-1 20199325《Linux内核原理与分析》第十二周作业

什么是ShellShock?

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

1.环境搭建

$ 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

接下来进行使用ln进行软连接,做完这一步,安装就完成了

$ 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说明shellshock漏洞存在,让/bin/sh 指向/bin/bash.

我们通过攻击Set-UID程序来获得root权限。system()函数将调用"/bin/sh -c" 来运行指定的命令, 这意味着/bin/bash 会被调用,我们让/bin/sh 指向/bin/bash.在 /home/shiyanlou 目录下新建一个 shock.c 文件输入代码,我们注意到这里使用了setuid(geteuid()) 来使real uid = effective uid,这在Set-UID程序中不是普遍现象,但它确实有时会发生。以下是hack过程:

我们将setuid(geteuid()) 语句被去掉了,再试试看攻击,发现失败了!这就说明如果 real uid 和 effective uid 相同的话,定义在环境变量中的内容在该程序内有效,那样shellshock漏洞就能够被利用了。但是如果两个 uid 不同的话,环境变量失效,就无法发动攻击了,这可以从 bash的源代码中得到印证


以下是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] = '(';   /* ) */
  }

总结:这说明在shell存在漏洞的版本当中,真实id和有效id一定是分开的,如果不分开,就很有可能存在被利用的漏洞。我们需要注意这些。

猜你喜欢

转载自www.cnblogs.com/buhery/p/11981316.html
今日推荐