『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 字符串结果,如下 

  

 

posted @ 2016-03-30 16:19  Anka9080  阅读(11453)  评论(7编辑  收藏  举报