-
Notifications
You must be signed in to change notification settings - Fork 101
/
airbug.py
147 lines (125 loc) · 4.06 KB
/
airbug.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2019/6/15 9:15 PM
# @Author : w8ay
# @File : airbug.py
import hashlib
import sys
import json
from concurrent import futures
from importlib import util
from importlib.abc import Loader
try:
import HackRequests
except ImportError:
print("You must run 'pip3 install HackRequests'")
exit()
WEB_REPOSITORY = "https://github.com/boy-hack/airbug"
HACK = HackRequests.hackRequests()
def get_md5(value):
if isinstance(value, str):
value = value.encode(encoding='UTF-8')
return hashlib.md5(value).hexdigest()
def load_string_to_module(code_string, fullname=None):
try:
module_name = 'pocs_{0}'.format(get_md5(code_string)) if fullname is None else fullname
file_path = 'w12scan://{0}'.format(module_name)
poc_loader = PocLoader(module_name, file_path)
poc_loader.set_data(code_string)
spec = util.spec_from_file_location(module_name, file_path, loader=poc_loader)
mod = util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
except ImportError:
error_msg = "load module '{0}' failed!".format(fullname)
print(error_msg)
raise
class PocLoader(Loader):
def __init__(self, fullname, path):
self.fullname = fullname
self.path = path
self.data = None
def set_data(self, data):
self.data = data
def get_filename(self, fullname):
return self.path
def get_data(self, filename):
if filename.startswith('w12scan://') and self.data:
data = self.data
else:
with open(filename, encoding='utf-8') as f:
data = f.read()
return data
def exec_module(self, module):
filename = self.get_filename(self.fullname)
poc_code = self.get_data(filename)
obj = compile(poc_code, filename, 'exec', dont_inherit=True, optimize=-1)
exec(obj, module.__dict__)
def load_remote_poc():
middle = "/master"
suffix = "/API.json"
prefix = WEB_REPOSITORY.replace("github.com", "raw.githubusercontent.com")
_api = prefix + middle + suffix
hh = HACK.http(_api)
data = json.loads(hh.text(), encoding='utf-8')
for _ in data:
_["webfile"] = prefix + middle + _["filepath"]
return data
def run_airbug(target: str, keywords: list):
PocQueue = []
print("load poc from airbug repository")
pocs = load_remote_poc()
for poc in pocs:
for keyword in keywords:
if keyword.lower() in poc["name"].lower():
webfile = poc["webfile"]
msg = "load {0} poc:{1} poc_time:{2}".format(poc["type"], webfile, poc["time"])
print(msg)
code = HACK.http(webfile).text()
obj = load_string_to_module(code, webfile)
PocQueue.append((target, obj))
print("Start to run poc")
collector = []
if not PocQueue:
msg = "Not found poc {}".format(repr(keywords))
print(msg)
return collector
executor = futures.ThreadPoolExecutor(len(PocQueue))
fs = []
for target, obj in PocQueue:
fs.append(executor.submit(obj.poc, target))
for f in futures.as_completed(fs):
try:
ret = f.result()
except Exception as e:
ret = None
print("load poc error:{} error:{}".format(target, str(e)))
if ret:
collector.append(ret)
print("over.")
return collector
def main():
target = ''
keywords = []
argv = sys.argv
index = 0
msg_help = "help:-u http://xxx.com -r emlog,wordpress"
for arg in argv:
try:
if arg == "-u":
target = argv[index + 1]
if arg == "-r":
keywords = argv[index + 1].split(",")
except IndexError:
print(msg_help)
return
index += 1
if target and keywords:
ret = run_airbug(target, keywords)
if not ret:
print("nothing.")
for i in ret:
print(i)
else:
print(msg_help)
main()