Android Plus Shell native implementation

Source: Internet
Author: User

This example only runs through the Android2.3 virtual machine, if you want to adapt to other models, please self-study, here is just a point.

0x00

In the Android apk reinforcement (shell) principle analysis and implementation, a article in the shelling code is written in the Java layer is easy to identify, a lot of needs to the shelling process to transfer to the native layer, in fact, the idea of transfer is very simple, is to call Java layer code through JNI at the native layer.


0x01

    public class Proxyapplication extends application {@Overrideprotected void Attachbasecontext (Context base) { Super.attachbasecontext (Base);D exloader.load ("Com.jltxgcy.dynamicdex");} @Overridepublic void OnCreate () {Dexloader.run ();}}
Remember that these two methods have a lot of code, there is only one line of code to be done.

We look at the Dexloader class again.

public class Dexloader {static {system.loadlibrary ("Dexloader");} public static native void load (String path);p ublic static native void run ();}

The original core shelling code is placed in the native layer.

0x02

The implementation of JNI is as follows, and you can see that the Java layer code is essentially transferred to the native layer via JNI.

static void loadapk (JNIENV * env, Jclass clazz, jstring package) {Jclass Activitythreadclazz;jmethodid currentactivitythr Eadmethodid;jobject activitythreadobject;const Char *packagename;const char *classname;const char *methodName;int Codeoff;jfieldid mpackagesfieldid;jobject mpackagesjobject;jclass Mpackagesclazz;jmethodid getMethodID;jobject Weakreferencejobject;jclass Weakreferencejclazz;jmethodid Getweakmethodid;jobject LoadedApkJObject;jclass Loadedapkjclazz;jfieldid mclassloaderfieldid;jobject mclassloaderjobject;jstring dexPath;jstring dexOptPath;jclass Dexclassloaderclazz;jmethodid Initdexloadermethod;jobject Dexclassloaderjobject;activitythreadclazz = env-> Findclass ("Android/app/activitythread"); Currentactivitythreadmethodid = Env->getstaticmethodid ( Activitythreadclazz, "Currentactivitythread", "() landroid/app/activitythread;"); Activitythreadobject = Env->callstaticobjectmethod (Activitythreadclazz, Currentactivitythreadmethodid); PackageName = Env->getstrIngutfchars (package, jni_false); mpackagesfieldid = Env->getfieldid (Activitythreadclazz, "Mpackages", "Ljava/util /hashmap; "); Mpackagesjobject = Env->getobjectfield (Activitythreadobject, mpackagesfieldid); mpackagesclazz = env-> Getobjectclass (mpackagesjobject); getmethodid = Env->getmethodid (Mpackagesclazz, "Get", "(Ljava/lang/object;) Ljava/lang/object; "); Weakreferencejobject = Env->callobjectmethod (Mpackagesjobject, Getmethodid, package); Weakreferencejclazz = env- >getobjectclass (weakreferencejobject); getweakmethodid = Env->getmethodid (Weakreferencejclazz, "Get", "() L Java/lang/object; "); Loadedapkjobject = Env->callobjectmethod (Weakreferencejobject, getweakmethodid); loadedApkJClazz = env-> Getobjectclass (loadedapkjobject); mclassloaderfieldid = Env->getfieldid (Loadedapkjclazz, "MClassLoader", "Ljava/ Lang/classloader; "); Mclassloaderjobject = Env->getobjectfield (Loadedapkjobject, mclassloaderfieldid);d Expath = Env->NewStringUTF ( "/sdcard/payload_odex/forceapkobj.apk ");d Exoptpath = Env->newstringutf ("/sdcard/payload_odex/");d Exclassloaderclazz = Env->findclass ("Dalvik/system/dexclassloader"); Initdexloadermethod = Env->getmethodid (DexClassLoaderClazz, "<init>", "(ljava/lang/string; ljava/lang/string; ljava/lang/string; Ljava/lang/classloader;) V ");d Exclassloaderjobject = Env->newobject (Dexclassloaderclazz,initdexloadermethod, Dexpath, Dexoptpath, NULL, Mclassloaderjobject); Env->setobjectfield (Loadedapkjobject, Mclassloaderfieldid, Dexclassloaderjobject); ALOGD ("packagename:%s", PackageName);} static void Run (JNIENV * env, Jclass clazz) {jclass Activitythreadclazz;jmethodid currentactivitythreadmethodid;jobject Activitythreadobject;jfieldid Mboundapplicationfieldid;jobject Mboundapplicationjobject;jclass Mboundapplicationclazz;jfieldid minfofieldid;jobject minfojobject;jclass Minfoclazz;jfieldid MApplicationFieldID; Jobject Mapplicationjobject;jfieldid Minitialapplicationfieldid;jobject MinitialapplicationjobJect;jfieldid Mallapplicationsfieldid;jobject Mallapplicationsjobject;jclass Mallapplicationsclazz;jmethodid Removemethodid;jfieldid mapplicationinfofieldid;jobject Mapplicationinfojobject;jclass MApplicationInfoClazz; Jfieldid mbindapplicationinfofieldid;jobject Mbindapplicationinfojobject;jclass MBindApplicationInfoClazz; Jfieldid classnamefieldid;jfieldid mbindclassnamefieldid;jstring Applicationname;jmethodid MakeApplicationMethodID ; Jobject applicationjobject;jclass Applicationclazz;jmethodid oncreatemethodid;activitythreadclazz = env-> Findclass ("Android/app/activitythread"); Currentactivitythreadmethodid = Env->getstaticmethodid ( Activitythreadclazz, "Currentactivitythread", "() landroid/app/activitythread;"); Activitythreadobject = Env->callstaticobjectmethod (Activitythreadclazz, Currentactivitythreadmethodid); Mboundapplicationfieldid = Env->getfieldid (Activitythreadclazz, "mboundapplication", "Landroid/app/ Activitythread$appbinddata; "); Mboundapplicationjobject = Env->getobjectfield (Activitythreadobject, mboundapplicationfieldid); mboundapplicationclazz = env-> Getobjectclass (mboundapplicationjobject); minfofieldid = Env->getfieldid (mboundapplicationclazz, "info", " landroid/app/loadedapk; "); Minfojobject = Env->getobjectfield (Mboundapplicationjobject, minfofieldid); minfoclazz = Env->GetObjectClass ( Minfojobject); mapplicationfieldid = Env->getfieldid (Minfoclazz, "mapplication", "landroid/app/application;"); Mapplicationjobject = Env->getobjectfield (Minfojobject, Mapplicationfieldid); Env->setobjectfield ( Minfojobject, Mapplicationfieldid, NULL); minitialapplicationfieldid = Env->getfieldid (ActivityThreadClazz, " Minitialapplication "," landroid/app/application; "); Minitialapplicationjobject = Env->getobjectfield (Activitythreadobject, Minitialapplicationfieldid); Mallapplicationsfieldid = Env->getfieldid (Activitythreadclazz, "mallapplications", "Ljava/util/ArrayList;"); Mallapplicationsjobject = Env->getobjectfield (activityThreadobject, mallapplicationsfieldid); mallapplicationsclazz = Env->getobjectclass (MAllApplicationsJObject); Removemethodid = Env->getmethodid (Mallapplicationsclazz, "Remove", "(Ljava/lang/object;) Z"); Jboolean isTrue = Env->callbooleanmethod (Mallapplicationsjobject, Removemethodid, Minitialapplicationjobject); Mapplicationinfofieldid = Env->getfieldid (Minfoclazz, "Mapplicationinfo", "landroid/content/pm/applicationinfo;"); Mapplicationinfojobject = Env->getobjectfield (Minfojobject, mapplicationinfofieldid); MApplicationInfoClazz = Env->getobjectclass (mapplicationinfojobject); mbindapplicationinfofieldid = Env->getfieldid ( Mboundapplicationclazz, "AppInfo", "landroid/content/pm/applicationinfo;"); Mbindapplicationinfojobject = Env->getobjectfield (Mboundapplicationjobject, Mbindapplicationinfofieldid); Mbindapplicationinfoclazz = Env->getobjectclass (mbindapplicationinfojobject); classnamefieldid = env-> Getfieldid (Mapplicationinfoclazz, "ClassName", "ljava/lang/string; "); Mbindclassnamefieldid = Env->getfieldid (Mbindapplicationinfoclazz, "ClassName", "ljava/lang/string;"); ApplicationName = Env->newstringutf ("com.example.forceapkobj.MyApplication"); Env->setobjectfield ( Mapplicationinfojobject, Classnamefieldid, ApplicationName); Env->setobjectfield (MBindApplicationInfoJObject, Mbindclassnamefieldid, applicationname); makeapplicationmethodid = Env->getmethodid (MInfoClazz, "makeApplication "," (zlandroid/app/instrumentation;) landroid/app/application; "); Applicationjobject = Env->callobjectmethod (Minfojobject, Makeapplicationmethodid, JNI_FALSE, NULL);env-> Setobjectfield (Activitythreadobject, Minitialapplicationfieldid, Applicationjobject); Applicationclazz = Env->getobjectclass (applicationjobject); oncreatemethodid = Env->getmethodid ( Applicationclazz, "OnCreate", "() V"); Env->callvoidmethod (Applicationjobject, oncreatemethodid);} Jniexport void Jnicall java_com_jltxgcy_dynamicdex_dexloader_load (jnienv * env, Jclass Clazz, jstring PackageName) {loadapk (env, Clazz, PackageName); ALOGD ("Java_com_jltxgcy_dynamicdex_dexloader_load");} Jniexport void Jnicall Java_com_jltxgcy_dynamicdex_dexloader_run (jnienv * env, Jclass clazz) {run (env, clazz); ALOGD ("Java_com_jltxgcy_dynamicdex_dexloader_run");}

Android Plus Shell native implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.