ZoomEye API 调用示例

参考资料

http://www.cnblogs.com/anka9080/p/ZoomEyeAPI.html

脚本内容

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
# -*- 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
print "ACCESS Response"
print r_decoded
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:
# query_keywords = "gpms+country%3AChina+port%3A8000"
query_keywords = "gpms"
r = requests.get(url='https://api.zoomeye.org/host/search?query=' + query_keywords + '&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'])
ip_list.append(str(x['ip']) + " 80")
# 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()