-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis_struts2.py
217 lines (165 loc) · 6.7 KB
/
is_struts2.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
#!/usr/bin/env python3
# _*_ coding:utf-8 _*_
# @Author : shley
# @Time : 2023/3/1 16:45
import random
import string
import time
import argparse
import re
import requests
from urllib.parse import urlparse
requests.packages.urllib3.disable_warnings()
ERROR_KEYS = ['Struts Problem Report', 'org.apache.struts2', 'struts.devMode', 'struts-tags',
'There is no Action mapped for namespace']
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0"
}
# check suffix :.do,.action
def check_by_suffix(page_res_json):
if page_res_json['code'] == 404:
return False
html = page_res_json['html']
match_action = re.findall(r"""(['"]{1})(/?((?:(?!\1|\n|http(s)?://).)+)\.action)(\?(?:(?!\1).)*)?\1""", html,
re.IGNORECASE)
match_do = re.findall(r"""(['"]{1})(/?((?:(?!\1|\n|http(s)?://).)+)\.do)(\?(?:(?!\1).)*)?\1""", html,
re.IGNORECASE)
if len(match_do) + len(match_action) > 0 and (".action" in str(match_action) or ".do" in str(match_do)):
return True
else:
return False
# check dev mode page
def check_dev_mode(url):
dev_mode_url = url + "/struts/webconsole.html"
info = get_html(dev_mode_url)
if info['code'] == 200 and "Welcome to the OGNL console" in info['html']:
return True
else:
return False
# check error msg
def check_actions_errors(url):
error_path = [url + "/?actionErrors=1111", url + "/tmp2017.action", url + "/tmp2017.do",
url + "/system/index!testme.action", url + "/system/index!testme.do"]
for test_url in error_path:
info = get_html(test_url)
for error_message in ERROR_KEYS:
if error_message in info['html'] and info['code'] == 500:
print("[+] found error_message:", error_message)
return True
return False
# check add random str
def check_add_random_path(url):
random_path = generate_random_str()
parsed_url = urlparse(url)
if len(parsed_url.query) != 0:
url = parsed_url.scheme + "://" + parsed_url.netloc + "/" + random_path + parsed_url.path + "?" + parsed_url.query
else:
url = parsed_url.scheme + "://" + parsed_url.netloc + "/" + random_path + parsed_url.path
res = requests.get(url, timeout=3, headers=headers, allow_redirects=True, verify=False)
if res.status_code == 200:
return True
else:
return False
# check CheckboxInterceptor
def check_check_box(url):
for match in re.finditer(r"((\A|[?&])(?P<parameter>[^_]\w*)=)(?P<value>[^&#]+)", url):
info = get_html(url.replace(match.group('parameter'), "__checkbox_" + match.group('parameter')))
check_key = 'name="{}"'.format(match.group('parameter'))
check_value = 'value="false"'
html = info['html']
match_input_tags = re.findall(r"""<\s*input[^>]*>""", html, re.IGNORECASE)
for input_tag in match_input_tags:
if check_key in input_tag and check_value in input_tag:
return True
return False
# check I18N -> internationalization and localization
def check_i18n(url):
info_origin = get_html(url)
time.sleep(0.5)
info_zhCN = get_html(url + "?" + 'request_locale=zh_CN')
time.sleep(0.5)
info_enUS = get_html(url + "?" + 'request_locale=en_US')
time.sleep(0.5)
if "request_locale=zh_CN" in info_origin['html'] and "request_locale=en_US" in info_origin['html']:
return True
if abs(len(info_zhCN['html']) - len(info_enUS['html'])) > 1024:
return True
return False
# check default .css
# 低版本的 struts2 可能不存在此文件,因此此项仅作为一个辅助手段
def check_default_css(target):
css_path = target + "/struts/domTT.css"
ctx = get_html(css_path)
req = requests.get(url, timeout=3, headers=headers, allow_redirects=True, verify=False)
if req.status_code == 200 and "Licensed to" in ctx['html']:
return True
return False
# 生成 16 位随机随机字符串
def generate_random_str(length=16):
"""
生成一个指定长度的随机字符串,其中
string.digits=0123456789
string.ascii_letters=abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
"""
str_list = [random.choice(string.digits + string.ascii_letters) for i in range(length)]
random_str = ''.join(str_list)
return random_str
# 获取网页源代码
def get_html(url):
res = requests.get(url, timeout=3, headers=headers, allow_redirects=True, verify=False)
content = res.text
return {"html": content, "code": res.status_code, "url": url}
# 对传入进来的 url 进行格式化
def format_url(url):
if not url.startswith('http://') and not url.startswith('https://'):
url = 'http://' + url
return url
def check_is_struts2(url):
url = format_url(url)
# 得到页面内容 html 代码
index_html = get_html(url)
# 1. 判断是否存在 dev mode 页面
if check_dev_mode(url):
return "[success] %s is struts2! [check_dev_mode]" % url
# 2. 判断网页代码中是否存在 .do 或者 .action 连接
if check_by_suffix(index_html):
return "[success] %s is struts2! [check_by_suffix]" % url
# 3. 判断页面中的错误信息
if check_actions_errors(url):
return "[success] %s is struts2! [check_actions_errors]" % url
# 4. 添加随机路径,看页面是否仍可以访问
if check_add_random_path(url):
return "[success] %s is struts2! [check_add_random_path]" % url
# 5.
if check_check_box(url):
return "[success] %s is struts2! [check_check_box]" % url
# 6. 检测 i18n
if check_i18n(url):
return "[success] %s is struts2! [check_i18n]" % url
# 7. 判断网站是否存在 /struts/domTT.css 文件
if check_default_css(url):
return "[success] %s is struts2! [check_default_css]" % url
return False
if __name__ == "__main__":
# 接受单个 url 以及 file 文件
parser = argparse.ArgumentParser(description=" struts2 框架检测脚本")
parser.add_argument("-u", "--url", help="target URL")
parser.add_argument("-f", "--file", help="file containing target URLs (one per line)")
args = parser.parse_args()
if args.url:
result = check_is_struts2(args.url)
if not result:
print("[*] %s is not struts2!" % args.url)
else:
print(result)
elif args.file:
with open(args.file) as f:
for line in f:
url = line.strip()
result = check_is_struts2(url)
if not result:
print("[*] %s is not struts2!" % url)
else:
print(result)
else:
parser.print_help()