1. 程式人生 > >Python中shodan模組的使用

Python中shodan模組的使用

關於shodan的安裝和使用,傳送門——> 滲透測試之Shodan的安裝和使用

常用 Shodan 庫函式

  • shodan.Shodan(key) :初始化連線API
  • Shodan.count(query, facets=None):返回查詢結果數量
  • Shodan.host(ip, history=False):返回一個IP的詳細資訊
  • Shodan.ports():返回Shodan可查詢的埠號
  • Shodan.protocols():返回Shodan可查詢的協議
  • Shodan.services():返回Shodan可查詢的服務
  • Shodan.queries(page=1, sort='timestamp', order='desc')
    :查詢其他使用者分享的查詢規則
  • Shodan.scan(ips, force=False):使用Shodan進行掃描,ips可以為字元或字典型別
  • Shodan.search(query, page=1, limit=None, offset=None, facets=None, minify=True): 查詢Shodan資料

先寫一個簡單的指令碼,掃描 apache 的主機

import shodan    #匯入shodan庫
api=shodan.Shodan("cB9sXwb7l95ZhSJaNgcaO7NQpkzfhQVM")  #指定API_KEY,返回控制代碼
try:
    results=api.search('apache')    #搜尋apache,返回 JSON格式的資料
    print(results)
    print("Results found:%s"%results['total'])
    for result in results['matches']:
        print(result['ip_str'])     #打印出ip地址
except shoadn.APIError,e:
    print("Error:%s"%e)

返回的JSON格式的資料 

{
        'total': 8669969,
        'matches': [
                {
                        'data': 'HTTP/1.0 200 OK\r\nDate: Mon, 08 Nov 2010 05:09:59 GMT\r\nSer...',
                        'hostnames': ['pl4t1n.de'],
                        'ip': 3579573318,
                        'ip_str': '89.110.147.239',
                        'os': 'FreeBSD 4.4',
                        'port': 80,
                        'timestamp': '2014-01-15T05:49:56.283713'
                },
                ...
        ]
}

 

我們也可以加上埠號,並且寫入檔案中,作為訪問連結

import shodan

api=shodan.Shodan("cB9sXwb7l95ZhSJaNgcaO7NQpkzfhQVM")
def FindTarget():
    try:
        f=open("target.txt","w")
        results=api.search('JAWS/1.0') 
        print("Results found:%s"%results['total'])
        for result in results['matches']:
            url=result['ip_str']+":"+str(result['port'])
            print(url) 
            f.write(url)
            f.write("\n")
        f.close()
    except shodan.APIError,e:
        print("Error:%s"%e)
FindTarget()