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

Source: Internet
Author: User

Note: In the following 4 blog posts, some of the images are quoted from Dexhunter author Zyqqyz in Slide.pptx, and the version is owned by the original author;

0x01 Background Introduction

The protection of Android apps is generally divided into the following areas:

    1. JAVA/C Code Obfuscation

    2. Dex File Packers

    3. . so file Packers

    4. Anti-dynamic debugging technology

Where obfuscation and packers are used to prevent static analysis of the application, code obfuscation increases the time cost of an attacker, but does not fundamentally address the inverse of the application, and once the shell technology is cracked, its advantages are completely wiped out; counter-debug is used to counter the dynamic analysis of the app;

Yesterday to see snow Zyqqyz classmate sent an Android application general automatic shelling method: Dexhunter, see GitHub; Through custom Dalvik virtual machine implementation, and the current domestic 6 mainstream reinforcement products tested, the effect is good Below we will explain the implementation principle of Dalvik interpreter and analyze the implementation of Dexhunter code;

At present, to combat this shelling method, the best way should be the app when the hook was modified several functions, and restore it, for the developer, should be involved in assets, innovation of the key code into the native layer implementation, and through the. So shell and anti-debugging to protect;

The last time I spoke with the Bang Bang, they mentioned that Bang Bang 3.0 is developing a PC-like VMP protection shell technology to achieve apk protection; But personally think that there is too much work on compatibility, it is difficult to achieve in a short period of time;

The following is a 3-part introduction to the general shelling scheme through custom Dalvik: 1.Dalvik interpreter principle Analysis, 2.DexHunter code Analysis, 3. Testing

Principle analysis of 0x02 Dalvik interpreter

The interpreter is the execution engine for the Dalvik virtual machine, which interprets the execution of the Dalvik bytecode. After the bytecode has been loaded, the Dalvik virtual machine invoke interpreter begins to fetch the interpreted bytecode, and the interpreter jumps to the interpreter to execute the code. Currently there are two types of Android interpreter, portable and fast interpreter, respectively, using C and assembly implementation, the advantages are compatibility and performance, specific use which can be specified by themselves, so in simple principle, we analyze and use the portable interpreter;

Getting the bytecode and parsing and interpreting the execution is the main work of the Dalvik virtual machine interpreter. The entry function of the Dalvik virtual machine is the Dvminterpret function under Vm/interp, and the outside is executed by invoking the Dvminterpret function into the interpreter, whose flow is dvmcallmethod->dvmcallmethodv-> Dvminterpret.

After the external function calls the interpreter, the main process that the interpreter performs is the following steps.

    1. Initializing the Interpreter execution environment

    2. Choose to use the portable or fast interpreter, depending on the system parameters

    3. Jump to the appropriate interpreter execution

    4. Reference and Instruction inspection

    5. Execute byte code corresponding to the program segment

The Dvminterpret function acts as the interpreter's entry function, which mainly completes the first three parts of the process, and executes the following process:

Since the first three parts are not related to our custom Dalvik, here is not a detailed introduction;

According to the function of the interpreter, it can be imagined that the simplest model is a large switch statement, each instruction is judged, and then the case to the corresponding code to explain, the explanation is completed and then back to the top of the switch, as follows:

 while(INSN) {Switch(INSN) { CaseNOP: Break;  CaseMOV: Dosomething;  Break; ...         CaseOP: Dosomething;  Break; default:             Break; } to take a reference;}

However, it is an expensive expense to re-determine the instruction type once the explanation has been completed with an instruction. Because for each instruction, it will start from the top of the switch, that is, start judging from the NOP instruction, until the corresponding instruction is found, which makes the interpreter's execution very inefficient.

The solution to this kind of problem is the space change time, Dalvik adopted this idea; it assigns a corresponding label (label) for each instruction, the label is the beginning of the instruction interpreter, the end of each instruction's interpreter, there is a finger action, you can remove a command to execute Dalvik specifically uses GCC's threaded code technique, which uses a static label array to store the corresponding label addresses for each bytecode interpreter, defined by a macro:

Dalvik/libdex/dexopcodes.h

/** Macro used to generate a computed goto table for use with implementing * an interpreter in C.*/#defineDefine_goto_table (_name)Static Const void* _name[knumpackedopcodes] = {                              /*BEGIN (libdex-goto-table); GENERATED automatically by Opcode-gen*/h (OP_NOP), H (op_move),                                                            H (OP_MOVE_FROM16),                                                              H (op_move_16), H (op_move_wide), ...    }

Here's a look at the H macro implementation:

#define    H (_op)    &&op_# #_op

How do you get the corresponding label address according to the instructions? The index number is used in the Dalvik:

enumOpcode {//BEGIN (Libdex-opcode-enum); GENERATED automatically by Opcode-genOp_nop =0x00, Op_move=0x01, Op_move_from16=0x02, Op_move_16=0x03, Op_move_wide=0x04, Op_move_wide_from16=0x05, Op_move_wide_16=0x06, Op_move_object=0x07,    ....}

Therefore, the entire execution process is: Take the index number, and take the label to get the interpreter address---Execute the instruction, and remove an instruction.

The basic model of the interpreter is analyzed above, and the execution flow of Dalvik portable is shown below. Its analytical process

First, the declaration of the relevant variables, save the method currently being interpreted Curmethod, program counter PC, stack frame pointer fp, the current instruction inst, instruction decoding related parts including saving register value VSRC1,VSRC2,VDST, Sets the method call pointer Methodtocall, and so on.

The define_goto_table (handlertable) macro is used to bind the GOTO label to obtain and copy the current state stored in the Self->interpsave, including method, program counter PC, The stack frame curframe, the return value retval, the class object information for the Dex file to parse Curmethod->clazz->pdvmdex, and so on declared variables. The code is as follows:

Dalvik/vm/mterp/out/interpc-portable.cpp

    /**    = self->interpsave.method    ; = self->interpsave.pc;     = self->interpsave.curframe;     = self->interpsave.retval;   /*  *    /= curmethod->clazz->pdvmdex;

Finally, through finish (0) to obtain the first instruction to begin execution of bytecode parsing.

In Dalvik portable, the interpreter is controlled by a series of macros, represented by the corresponding label, and as an example, the NOP operation is defined as follows:

Dalvik/vm/mterp/out/interpc-portable.cpp

/*---Start of opcodes---*//*File:c/op_nop.cpp*/Handle_opcode (OP_NOP) FINISH (1); Op_end/*File:c/op_move.cpp*/Handle_opcode (Op_move/*VA, VB*/) VDST=inst_a (INST); Vsrc1=Inst_b (INST); ILOGV ("|move%s v%d,v%d%s (v%d=0x%08x)", (Inst_inst (INST)= = Op_move)?"":"-object", VDST, Vsrc1, kspacing, VDST, Get_register (VSRC1));    Set_register (VDST, Get_register (VSRC1)); FINISH (1); Op_end/*File:c/op_move_from16.cpp*/Handle_opcode (Op_move_from16/*VAA, vbbbb*/) VDST=Inst_aa (INST); Vsrc1= FETCH (1); ILOGV ("|move%s/from16 v%d,v%d%s (v%d=0x%08x)", (Inst_inst (INST)= = OP_MOVE_FROM16)?"":"-object", VDST, Vsrc1, kspacing, VDST, Get_register (VSRC1));    Set_register (VDST, Get_register (VSRC1)); FINISH (2); Op_end

Handle_opcode (OP_NOP) indicates that the corresponding OP_NOP operation is followed by a specific implementation of the interpreter. To the end of Op_end. In portable, therefore, there are interpretive procedures written in C language. The Handle_opcode, finish, and Op_end in a NOP operation are all macro definitions. Where Handle_opcode and Op_end are paired up, op_end do nothing:

#define    Op_end

So

Handle_opcode (OP_NOP)    FINISH (1); Op_end

Can be translated as:

Op_op_nop:    FINISH (1);

For NOP instructions, the work done is nothing. Therefore, the corresponding interpreter is to take a direct removal of the instructions to be executed, that is, Finish (1) Completed work. In the finish () macro, the virtual machine takes the next instruction and extracts the operation code number from the instruction, obtains the corresponding label in accordance with the operation code number to the instruction interpreter lookup table, and jumps to the handler execution. It is defined as follows:

# define FINISH (_offset) {                                                          adjust_pc (_offset);                                                          = FETCH (0);                                                             if (self->interpBreak.ctl.subMode) {                                            Dvmcheckbefore (PC, FP, self);                                           }                                                                            Goto *handlertable[inst_inst (INST)];                                    }

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

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.