/
zoomeye.py
executable file
·188 lines (163 loc) · 4.56 KB
/
zoomeye.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/python3
'''
# -*- coding: UTF-8 -*-
# coding: utf-8
# author : evilclay
# datetime: 20160330
# modified by: jm33_m0
# http://www.cnblogs.com/anka9080/p/ZoomEyeAPI.html
'''
import json
import os
import sys
import threading
import time
import traceback
import requests
import colors
import console
class ZoomEyeAPI:
'''
gain API key
'''
QRY = ""
OUTFILE = '../data/zoomeye-{}.txt'.format('-'.join(QRY.split()))
def __init__(self, conf):
try:
cred_file = open(conf)
for line in cred_file:
line = line.strip()
if line.startswith('user'):
self.user = line.split(':')[1]
elif line.startswith('password'):
self.passwd = line.split(':')[1]
except FileNotFoundError:
console.print_error('[-] Please look into zoomeye.conf first')
else:
pass
def login(self):
'''
login using given username and password
'''
data = {
'username': self.user,
'password': self.passwd
}
print(data)
data_encoded = json.dumps(data)
try:
r_post = requests.post(
url='https://api.zoomeye.org/user/login',
data=data_encoded)
r_decoded = json.loads(r_post.text)
return r_decoded['access_token']
except (EOFError, KeyboardInterrupt, SystemExit):
pass
else:
console.print_error(
'[-] invalid username or password, try again with curl')
sys.exit(1)
def debug_traceback():
'''
display traceback info
'''
answ = console.input_check("[?] Display traceback? [y/n] ",
choices=['y', 'n'])
if answ == 'y':
console.print_error(traceback.format_exc())
def save_str_to_file(file, string):
'''
save str to file
'''
if not os.path.exists(file):
os.system('touch {}'.format(file))
# check if we are writing duplicate lines to the file
f_hand = open(file)
for line in f_hand:
if line.strip() == string:
return
# write line to file
with open(file, 'a') as output:
output.write(string + '\n')
output.close()
def progress(file):
'''
display progress
'''
l_count = 0
if not os.path.exists(file):
os.system('touch {}'.format(file))
while True:
l_count = sum(1 for line in open(file))
sys.stdout.write(
colors.CYAN + '\r[+] Found ' + str(
l_count) + ' hosts' + colors.END)
sys.stdout.flush()
time.sleep(.5)
def crawler(qery, _, page, headers):
'''
fetch result from zoomeye
'''
try:
r_get = requests.get(
url='https://api.zoomeye.org/host/search?query=' +
qery +
'&facet=app,os&page=' +
str(page),
headers=headers)
r_decoded = json.loads(r_get.text)
for item in r_decoded['matches']:
save_str_to_file(ZoomEyeAPI.OUTFILE, item['ip'])
except BaseException:
pass
def api_test():
'''
get verified with zoomeye
'''
amnt = int(
console.input_check(
"[*] How many results do you want? (10 IPs on each page) ",
check_type=int).strip())
threads = []
api = ZoomEyeAPI('zoomeye.conf')
access_token = api.login()
headers = {
'Authorization': 'JWT ' + access_token,
}
status = threading.Thread(target=progress, args=(ZoomEyeAPI.OUTFILE,))
status.setDaemon(True)
status.start()
limit = 0
for page in range(1, int(amnt / 10)):
thd = threading.Thread(
target=crawler, args=(ZoomEyeAPI.QRY, amnt, page, headers,))
threads.append(thd)
for job in threads:
job.setDaemon(True)
job.start()
if limit == 0 or limit == 10:
limit = 0
job.join()
limit += 1
def main():
'''
put things together
'''
try:
api_test()
except (EOFError, KeyboardInterrupt, SystemExit):
pass
else:
console.print_error('[-] Error with api_test')
debug_traceback()
if __name__ == '__main__':
try:
ZoomEyeAPI.QRY = console.input_check(
"[*] Your query is: ", allow_blank=False)
ZoomEyeAPI.OUTFILE = '../data/zoomeye-{}.txt'.format(
'-'.join(ZoomEyeAPI.QRY.replace(':', '_').split()))
main()
except KeyboardInterrupt:
print('\n[*] Exiting...')
else:
debug_traceback()