Research on the general shelling technology of Android Dex (iv.)

Source: Internet
Author: User
<span id="Label3"> </p><pre class="brush:cpp;toolbar: true; auto-links: false;">/*     when the first class executes to this function, before Dvmdefineclass executes, that is, before the first class loads      injects our dump code ; that is, the dumpclass () function */static void dalvik_dalvik_system_dexfile_defineclassnative (const u4* args ,     jvalue* presult) {    stringobject* nameobj =  (stringobject*)  args[0];    Object* loader =  (object*)  args[1];     int cookie = args[2];    classobject* clazz  = NULL;    DexOrJar* pDexOrJar =  (dexorjar*)  cookie;     dvmdex* pdvmdex;    char* name;    char*  descriptor;    name = dvmcreatecstrfromstring (nameObj);     descriptor = dvmdottodescriptor (name); &NBSP;&NBSP;&NBSP;&NBSP;ALOGV ("--- Explicit  class load  '%s'  l=%p c=0x%08x ',        descriptor, loader,  cookie),     free (name);    if  (!validatecookie (cookie))          return_void ();    if  (pDexOrJar->isDex)         pdvmdex = dvmgetrawdexfiledex (pDexOrJar-> prawdexfile);    else        pdvmdex =  Dvmgetjarfiledex (pdexorjar->pjarfile);     /* once we load something ,  we can ' T unmap the storage */    pdexorjar->okaytofree  = false;//------------------------added begin----------------------//     Int uid=getuid ();    if  (uid)  {         if  (readable) &NBSP;{&NBsp;           pthread_mutex_lock (&read_mutex);             if  (readable)  {                 readable=false;                 pthread_mutex_unlock (&read_mutex);                 pthread_t read_thread;                 pthread_create (&read_thread, null, readthread, null);             }else{                 pthread_mutex_unlock (&read_mutex);             }        }    }     //each app corresponds to a thread    if (uid && strcmp (dexname, ""))  {  //dexname Non-empty         char * res=strstr (pDexOrJar-> filename, dexname);        if  (res && flag )  {            pthread_mutex_lock (&mutex) ;            if  (flag)  {                 flag = false;                 pthread_mutex_ Unlock (&mutex);                  dexfile*&nbSp;pdexfile=pdvmdex->pdexfile;    //take dex file                 memmapping * mem=&pdvmdex->memmap ;   //take memmory map //part1 area, classdef pre-content                  char * temp=new char[100];                 strcpy (temp,dumppath);                 strcat (temp, "part1") ; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FILE&NBSP;*FP  = fopen (temp,  "wb+");                 const u1 *addr =  (const u1*) mem->addr;        &Nbsp;        int length=int (pdexfile->baseaddr+pdexfile-> pheader->classdefsoff-addr);                 fwrite (addr,1,length,fp);                 fflush (fp);                 fclose (fp);//data area, classdef content                  strcpy (temp,dumppath);                 strcat (temp, "data");                 fp = fopen (temp,  "wb+");                 addr =  pdexfile->baseaddr+pdeXfile->pheader->classdefsoff+sizeof (dexclassdef) *pdexfile->pheader->classdefssize;                 length=int ((const u1* ) mem->addr+mem->length-addr);                 fwrite (addr,1,length,fp);                 fflush (fp);                 fclose (fp);                 delete temp;                 param.loader=loader;                 param.pDvmDex=pDvmDex;                 pthread_t dumpthread;                 dvmcreateinternalthread (&dumpthread, "ClassDumper", DumpClass, ( Void*) &param)       //dumpclass used to generate classdef  and  extra content                               }else{                 pthread_mutex_unlock (&mutex);             }        }     }//------------------------added end----------------------//    clazz =  dvmdefineclass (pdvmdex, descriptor, loader);     //load class. Before the app first class loads, call our shelling code     tHread* self = dvmthreadself ();    if  (dvmCheckException (self))  {         /*         *  If we threw a  "class not found"  exception, stifle it,  since the         * contract in the  higher method says we simply return null if          * the class is not found.          */        Object* excep =  Dvmgetexception (self);        if  (strcmp (excep->clazz-> descriptor,                     "LJAVA/LANG/CLASSNOTFOundexception; ")  == 0 | | &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;STRCMP (excep->clazz->descriptor,                      "ljava/lang/noclassdeffounderror;")  == 0)         {             dvmclearexception (self);        }         clazz = NULL;    }     free (descriptor);     return_ptr (clazz);}</pre> <p><p><br></p></p><p><p>Research on the general shelling technology of Android Dex (iv.)</p></p></span>

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.