forked from 0xbug/Hawkeye
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hawkeye.py
305 lines (239 loc) · 10.1 KB
/
Hawkeye.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from flask import Flask, render_template, jsonify
from pymongo import MongoClient
from crontab import CronTab
# from flask_cors import CORS, cross_origin
from flask_restful import Resource, Api, reqparse
import configparser
import json
import os
import hashlib
import base64
base_path = os.path.split(os.path.realpath(__file__))[0]
conf_path = base_path + '/config.ini'
def hawkeye_conf():
config = configparser.ConfigParser()
config.read(conf_path)
return config
def get_conf(section, option):
config = hawkeye_conf()
return config.get(section=section, option=option)
cli = MongoClient(host=get_conf('MongoDB', 'HOST'),
port=int(get_conf('MongoDB', 'PORT')))
try:
db = cli.Hawkeye
db.authenticate(get_conf('MongoDB', 'ACCOUNT'),
get_conf('MongoDB', 'PASSWORD'))
except BaseException:
db = cli.Hawkeye
leakage_col = db.leakage
query_col = db.query
blacklist_col = db.blacklist
notice_col = db.notice
app = Flask(__name__)
api = Api(app)
# CORS(app)
@app.route('/')
def index():
return render_template('index.html')
class Leakage(Resource):
def get(self):
parser = reqparse.RequestParser()
parser.add_argument('status', type=str, help='')
parser.add_argument('tag', type=str, help='')
parser.add_argument('limit', type=int, default=10, help='')
parser.add_argument('from', type=int, default=1, help='')
args = parser.parse_args()
status = json.loads(args.get('status'))
filters = status
if args.get('tag'):
filters = dict({'tag': args.get('tag')}, **filters)
results = list(
leakage_col.find(filters, {'code': 0, 'detail': 0}).sort('datetime', 1).limit(args.get('limit')).skip(
args.get('limit') * (args.get('from') - 1)))
total = leakage_col.count(filters)
if total:
msg = '共 {} 条记录'.format(total)
else:
msg = '暂无数据'
data = {
'msg': msg,
'status': 200,
'result': results,
'total': total
}
return jsonify(data)
def patch(self):
parser = reqparse.RequestParser()
parser.add_argument('id', type=str, help='')
parser.add_argument('project', type=str, help='')
parser.add_argument('ignore', type=int, choices=(0, 1), help='')
parser.add_argument('security', type=int, choices=(0, 1), help='')
parser.add_argument('desc', type=str, default='', help='')
args = parser.parse_args()
desc = base64.b64encode(
args.get('desc').encode(encoding='utf-8')).decode()
leakage_col.update({'_id': args.get('id')}, {'$set': {'security': int(
args.get('security')), 'ignore': int(args.get('ignore')), 'desc': desc}})
if not args.get('security'):
if not args.get('ignore'):
for leakage in leakage_col.find({'project': args.get('project')}):
leakage_col.update({'_id': leakage['_id']}, {
'$set': {'security': 0, 'ignore': 0, 'desc': desc}})
if args.get('security') and args.get('ignore'):
for leakage in leakage_col.find({'project': args.get('project')}):
leakage_col.update({'_id': leakage['_id']}, {
'$set': {'security': 1, 'ignore': 1, 'desc': desc}})
return jsonify({'status': 201, 'msg': '处理成功', 'result': []})
api.add_resource(Leakage, '/api/leakage')
class Statistics(Resource):
def get(self):
pipeline = [
{'$match': {'security': 0}},
{'$group': {'_id': '$tag', 'value': {'$sum': 1}}},
]
results = list(leakage_col.aggregate(pipeline))
if not len(results):
pipeline = [
{'$group': {'_id': '$tag', 'value': {'$sum': 0}}},
]
results = list(leakage_col.aggregate(pipeline))
return jsonify({'status': 200, 'msg': '获取信息成功', 'result': results})
api.add_resource(Statistics, '/api/statistics')
class LeakageCode(Resource):
def get(self, leakage_id):
results = list(leakage_col.find(
{'_id': leakage_id}, {'_id': 0, 'code': 1}))
return jsonify({'status': 200, 'msg': '获取信息成功', 'result': results})
class LeakageInfo(Resource):
def get(self, leakage_id):
results = list(leakage_col.find(
{'_id': leakage_id}, {'_id': 0, 'code': 0}))
return jsonify({'status': 200, 'msg': '获取信息成功', 'result': results})
api.add_resource(LeakageCode, '/api/leakage/<leakage_id>/code')
api.add_resource(LeakageInfo, '/api/leakage/<leakage_id>/info')
class Blacklist(Resource):
def get(self):
blacklists = list(blacklist_col.find({}, {'_id': 0}))
return jsonify({'status': 200, 'msg': '获取信息成功', 'result': blacklists})
def post(self):
parser = reqparse.RequestParser()
parser.add_argument('keyword', type=str, help='')
args = parser.parse_args()
keyword = args.get('keyword')
keyword = keyword.strip().replace(' ', '')
blacklist_col.save({'_id': md5(keyword), 'keyword': keyword})
blacklists = list(blacklist_col.find({}, {'_id': 0}))
return jsonify({'status': 201, 'msg': '添加成功', 'result': blacklists})
def delete(self):
parser = reqparse.RequestParser()
parser.add_argument('keyword', type=str, help='')
args = parser.parse_args()
blacklist_col.delete_many({'keyword': args.get('keyword')})
blacklists = list(blacklist_col.find({}, {'_id': 0}))
return jsonify({'status': 404, 'msg': '删除成功', 'result': blacklists})
api.add_resource(Blacklist, '/api/setting/blacklist')
class Notice(Resource):
def get(self):
notices = list(notice_col.find({}, {'_id': 0}))
return jsonify({'status': 200, 'msg': '获取信息成功', 'result': notices})
def post(self):
parser = reqparse.RequestParser()
parser.add_argument('keyword', type=str, help='')
args = parser.parse_args()
keyword = args.get('keyword')
keyword = keyword.strip().replace(' ', '')
notice_col.save({'_id': md5(keyword), 'keyword': keyword})
notices = list(notice_col.find({}, {'_id': 0}))
return jsonify({'status': 201, 'msg': '添加成功', 'result': notices})
def delete(self):
parser = reqparse.RequestParser()
parser.add_argument('keyword', type=str, help='')
args = parser.parse_args()
notice_col.delete_many({'keyword': args.get('keyword')})
notices = list(notice_col.find({}, {'_id': 0}))
return jsonify({'status': 404, 'msg': '删除成功', 'result': notices})
api.add_resource(Notice, '/api/setting/notice')
class Query(Resource):
def get(self):
querys = list(query_col.find({}))
return jsonify({'status': 200, 'msg': '获取信息成功', 'result': querys})
def post(self):
parser = reqparse.RequestParser()
parser.add_argument('keyword', type=str, help='')
parser.add_argument('tag', type=str, help='')
args = parser.parse_args()
query = args
status_code = insert(query)
querys = list(query_col.find({}))
return jsonify({'status': status_code, 'msg': '添加成功', 'result': querys})
def delete(self):
parser = reqparse.RequestParser()
parser.add_argument('_id', type=str, help='')
parser.add_argument('tag', type=str, help='')
args = parser.parse_args()
query_col.delete_many({'_id': args.get('_id')})
leakage_col.delete_many({'tag': args.get('tag')})
querys = list(query_col.find({}))
return jsonify({'status': 404, 'msg': '删除成功', 'result': querys})
api.add_resource(Query, '/api/setting/query')
class Cron(Resource):
def get(self):
return jsonify({'status': 200, 'msg': '获取信息成功', 'result': read_cron()})
def put(self):
parser = reqparse.RequestParser()
parser.add_argument('every', type=int, help='')
parser.add_argument('page', type=int, help='')
args = parser.parse_args()
write_cron(args.get('every'), int(args.get('page') + 1))
return jsonify({'status': 200, 'msg': '修改成功', 'result': read_cron()})
api.add_resource(Cron, '/api/setting/cron')
def insert(query):
if set(query.keys()) != {'tag', 'keyword'}:
return 422
elif not query_col.find_one({'tag': query.get('tag')}):
query['_id'] = md5(''.join(query.values()))
query_col.save(query)
return 201
elif query_col.find_one({'tag': query.get('tag')}):
query_col.delete_many({'tag': query.get('tag')})
query['_id'] = md5(''.join(query.values()))
query_col.save(query)
return 201
else:
return 200
def write_cron(time, page):
if isinstance(time, int):
base_path = os.path.split(os.path.realpath(__file__))[0]
cron_command = '{0}/venv/bin/python {0}/spider.py 1 {1}'.format(
base_path, page)
my_user_cron = CronTab(user=True)
if list(my_user_cron.find_comment('Hawkeye')):
for cron in my_user_cron.find_comment('Hawkeye'):
cron.delete()
job = my_user_cron.new(command=cron_command)
job.setall('*/{} * * * *'.format(time))
job.set_comment("Hawkeye")
job.enable()
my_user_cron.write()
return True
else:
return False
def read_cron():
user_cron = CronTab(user=True)
if list(user_cron.find_comment('Hawkeye')):
page = int(list(user_cron.find_comment('Hawkeye'))
[0].command.split(' ')[-1]) - 1
every = int(str(list(user_cron.find_comment('Hawkeye'))
[0].minutes).replace('*/', ''))
return {'every': every, 'page': page}
else:
return {'every': 15, 'page': 1}
def md5(data):
m = hashlib.md5()
m.update(data.encode('utf-8'))
result = m.hexdigest()
return result
if __name__ == '__main__':
app.run(host='0.0.0.0', threaded=True)