『Python』Python 調用 ZoomEye API 批量獲取目標網站IP


  

  #### 20160712 更新

  原API的訪問方式是以 HTTP 的方式訪問的,根據官網最新文檔,現在已經修改成 HTTPS 方式,測試可以正常使用API了。

 

0x 00 前言

  ZoomEye 的 API 在前幾天正式對外部開發,這對網絡滲透人員來說是一件開心的事

  可以說“媽媽再也不用擔心批量測(x)試(zhan)沒有資源了。”

  官方的 API 幫助文檔在下面:

  https://www.zoomeye.org/api/

  看了下,使用方法是先提交賬戶,密碼獲得一個唯一的訪問令牌(access_token)

  然后每次調用 API 的時候在 HTTP 的 Headers 里加上格式化后的 access_token 就可以使用了

  官方文檔為了方便使用給出的是 cURL 方式調用 API ,在這里我給出一個用 Python 調用 API 的 Demo

0x 01 Code

  該 Demo 抓取 ZoomEye 上 搜索 dedecms 的所有結果並把前 100 個IP保存到 文件中

# coding: utf-8
# author  : evilclay
# datetime: 20160330
# http://www.cnblogs.com/anka9080/p/ZoomEyeAPI.html

import os
import requests
import json

access_token = ''
ip_list = []

def login():
    """
        輸入用戶米密碼 進行登錄操作
    :return: 訪問口令 access_token
    """
    user = raw_input('[-] input : username :')
    passwd = raw_input('[-] input : password :')
    data = {
        'username' : user,
        'password' : passwd
    }
    data_encoded = json.dumps(data)  # dumps 將 python 對象轉換成 json 字符串
    try:
        r = requests.post(url = 'https://api.zoomeye.org/user/login',data = data_encoded)
        r_decoded = json.loads(r.text) # loads() 將 json 字符串轉換成 python 對象
        global access_token
        access_token = r_decoded['access_token']
    except Exception,e:
        print '[-] info : username or password is wrong, please try again '
        exit()

def saveStrToFile(file,str):
    """
        將字符串寫如文件中
    :return:
    """
    with open(file,'w') as output:
        output.write(str)

def saveListToFile(file,list):
    """
        將列表逐行寫如文件中
    :return:
    """
    s = '\n'.join(list)
    with open(file,'w') as output:
        output.write(s)

def apiTest():
    """
        進行 api 使用測試
    :return:
    """
    page = 1
    global access_token
    with open('access_token.txt','r') as input:
        access_token = input.read()
    # 將 token 格式化並添加到 HTTP Header 中
    headers = {
        'Authorization' : 'JWT ' + access_token,
    }
    # print headers
    while(True):
        try:
            
            r = requests.get(url = 'https://api.zoomeye.org/host/search?query="dedecms"&facet=app,os&page=' + str(page),
                         headers = headers)
            r_decoded = json.loads(r.text)
            # print r_decoded
            # print r_decoded['total']
            for x in r_decoded['matches']:
                print x['ip']
                ip_list.append(x['ip'])
            print '[-] info : count ' + str(page * 10)

        except Exception,e:
            # 若搜索請求超過 API 允許的最大條目限制 或者 全部搜索結束,則終止請求
            if str(e.message) == 'matches':
                print '[-] info : account was break, excceeding the max limitations'
                break
            else:
                print  '[-] info : ' + str(e.message)
        else:
            if page == 10:
                break
            page += 1

def main():
    # 訪問口令文件不存在則進行登錄操作
    if not os.path.isfile('access_token.txt'):
        print '[-] info : access_token file is not exist, please login'
        login()
        saveStrToFile('access_token.txt',access_token)

    apiTest()
    saveListToFile('ip_list.txt',ip_list)

if __name__ == '__main__':
    main()

  

 

0x 03 運行測試

   

  打開 access_token.txt 文件:

  

  打開 ip_list.txt 文件:

  

  題外話: 使用 Firefox 的 Modify Headers 添加上 Authentication 這個 頭信息后在瀏覽器里可以

  直接看到返回的 JSON 字符串結果,如下 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM