A-A+

Python 调用 ZoomEye API 脚本批量工具

2019年12月20日 22:49 汪洋大海 暂无评论 阅读 166 views 次
0x 00 前言

前几天一位老哥由于工作需要搜集网站是本省且服务器在国外的网站信息。问本宝宝有没有程序是否可以实现。恰好本宝宝只知道有一个网站可以进行搜集(zoomeye)。嘻…嘻…于是本宝宝对zoomeyeAPI进行学习,过程记录如下。zoomeyeAPI的官方文档如下:https://www.zoomeye.org/doc?channel=api
使用方法如下:先提交账户,密码获得一个唯一的访问令牌(access_token)然后每次调用 API 的时候在 HTTP 的 Headers 里加上格式化后的 access_token 就可以使用了。

 

0x 01 Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/9/3 23:45
# @Author  : leyan
# @File    : zoomeye_v2.0.py
# @Software: PyCharm

import os
import json
import time
import math
import openpyxl
import requests

#获取zoomeye认证token
def zoomeye_login(username,password):
    url = 'https://api.zoomeye.org/user/login'
    jsondata = {'username':username,'password':password}
    json_enconde = json.dumps(jsondata)
    respond = requests.post(url,data=json_enconde)
    try:
        json_access = json.loads(respond.text)
        access_token = json_access['access_token']
    except :
        return False
    return access_token


def zoomeye_write_xls(ip,country_code,city_name,site,title):
    if  not os.path.exists("信息表.xlsx"):
        title_list = ['ip','title','url']
        wb = openpyxl.Workbook()
        ws = wb.active
        ws.title = '信息汇总'
        for column_num in range(1,len(title_list)+1):
            ws.cell(row=1,column=column_num,value=title_list[column_num-1])
        wb.save('信息表.xlsx')
        print('[-]信息表创建成功。')
    if country_code !=  'CN' or (country_code == 'CN' and city_name =='香港'):
        data_list =['  '.join(ip),title,site]
        wb = openpyxl.load_workbook('信息表.xlsx')
        ws = wb.active
        max_row = ws.max_row       
        for c in range(1,len(data_list)+1):
            ws.cell(row=max_row+1,column=c,value=data_list[c-1])
        wb.save('信息表.xlsx')

def zoomeye_write(matches):
    for m in matches:
        ip = m['ip']
        try:
            country_code = m['geoinfo']['country']['code']
        except:
            country_code = 'null'
        try:
            city_name = m['geoinfo']['city']['names']['zh-CN']
        except:
            city_name = 'null'
        site = m['site']
        title = m['title']
        zoomeye_write_xls(ip,country_code,city_name,site,title)

#返回搜索的总页数
def zoomeye_page_num(username,password,title):
    url = 'https://api.zoomeye.org/web/search?query=title:'+title+'&page=1'
    if not zoomeye_login(username, password):
        print("[-]请输入正确的账号或密码")
        return -1
    headers = {'Authorization': 'JWT ' + zoomeye_login(username, password)}
    time.sleep(2)
    respond_obj = requests.get(url,headers=headers)
    matchs = json.loads(respond_obj.text)['matches']
    if not matchs:
        with open('./log.txt', 'a', encoding='utf8') as f:
            f.write("[-]" + url +"资源用完" + "\n")
        return -2
    total = json.loads(respond_obj.text)['total']
    zoomeye_write(matchs)
    if total == 0:
        return 0
    return math.ceil(total/len(matchs))

#关键词搜索
def zoomeye_search(username,password,title,page_start_num = 1):
    headers = {'Authorization': 'JWT ' + zoomeye_login(username,password)}
    page_num = zoomeye_page_num(username, password, title)
    if page_num == -1:
        return
    elif page_num == 0:
        print('[-]搜索结果为0')
    elif page_num == -2:
        print('[-]资源用完')
    for num in range(page_start_num,page_num+1):
        time.sleep(2)
        url = 'https://api.zoomeye.org/web/search?query=title:'+title+'&page='+ str(num)
        respond_html = requests.get(url,headers=headers)
        try:
            matchs = json.loads(respond_html.text)['matches']
        except:
            print('[-]provided credentials do not provide access to specified resource')
            return
        zoomeye_write(matchs)
        print("[-]",url,"完成下载")
        with open('./log.txt', 'a', encoding='utf8') as f:
            f.write("[-]"+url+"完成下载" + "\n")

if __name__ == '__main__':
    print("###############################")
    print("     @Author : leyan")
    print("    @日期 : 2019年9月1")
    print("  @服务支持:1075180052(QQ)")
    print(" @仅供学习交流使用,请勿传播!")
    print("###############################")
    user = input('[-] PLEASE INPUT YOUR USERNAME:')
    passwd = input('[-] PLEASE INPUT YOUR PASSWORD:')
    tittle = input('[-] PLEASE INPUT SEARCH KEYWORDS:')
    page_num = input('[-] PLEASE INPUT PAGE START NUM:(DEFAULT=1)')
    if not page_num:
        page_num = 1
    zoomeye_search(user,passwd,tittle,int(page_num))
    os.system('pause')
0x 03 运行测试

运行过程

运行过程

运行结果

运行结果

最后注意:使用zoomeyeAPI进行搜集只能搜集到结果总数得百分之20。如果想要搜集全部结果需要进行开通会员。

文章来源:https://leyan.pro/2019/10/14/Python%E8%B0%83%E7%94%A8ZoomEyeAPI%E6%89%B9%E9%87%8F%E8%8E%B7%E5%8F%96%E7%9B%AE%E6%A0%87%E7%BD%91%E7%AB%99%E4%BF%A1%E6%81%AF/

布施恩德可便相知重

微信扫一扫打赏

支付宝扫一扫打赏

×
标签:

给我留言