2022-2023-1 20222804《Linux内核原理与分析》第十三周作业

Linux系统内核第十三周作业

录制视频地址:实验过程录制视屏:https://www.bilibili.com/video/BV1FG411K7sE/?spm_id_from=444.41.list.card_archive.click

一、实验简介

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

二、实验过程

(一)环境搭建

以root权限安装4.1版bash(4.2版本以上的漏洞已经被堵上了) bash4.1 原来的下载地址是 http://ftp/gnu.org/gnu/bash/bash-4.1.tar.gz ,为了加快速度,我们这里使用下面的下载地址 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
$ exit
$ env x='() { :; }; echo vulnerable' bash -c "echo this is a test"

在这里插入图片描述

(四)检测shellshock漏洞

在这里插入图片描述
因为输出了vulnerable的话,说明bash存在漏洞。

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

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

三、实验内容

(一)攻击Set-UID程序

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

在 /home/shiyanlou 目录下新建一个 shock.c 文件;按 I 键切换到插入模式,再输入如下内容。

$ vi shock.c

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
void main()
{
    setuid(geteuid()); // make real uid = effective uid.
    system("/bin/ls -l");
}
```![在这里插入图片描述](https://img-blog.csdnimg.cn/86b048aa8e7a4cb0bd17b343362927b2.png)

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

```cpp
$ sudo su
$ gcc -o shock shock.c
$ chmod u+s shock
$ ls -il shock
$ exit
$ ./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 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] = '(';   /* ) */
  }

就是其中的这一段代码逻辑导致了两者的不同,primode即私有模式,要求real uid 与 effective uid保持一致

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);
[...]
} }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值