Attack on Android Injection "II"

Source: Internet
Author: User

Continue in the "one", I put the basic ideas described in the narrative, and then for us to start with the injection start.
Injection
Classify what we usually call code injection, the main static and dynamic two ways
    • Static injection, for is to be able to run files, for example, we change elf,dex files, etc., related auxiliary tools are also very many, such as Ida, JEB, Apktool and so on;
    • Dynamic injection, for the process, such as changing the process register, memory value and so on;
Dynamic and static The biggest difference is that the dynamic does not need to modify the source files, but the need for high permissions (usually root), and the required technical content is higher.
Intrinsic dynamic injection technology is essentially a scheduling technique. Think about what functions we can do when we debug a process? In general, the following items are available:
    • View variable values
    • Changing the value of a variable
    • Tracking process Jumps
    • To view the process call stack
    • Wait a minute
Dynamic injection is compared to normal debugging, the biggest difference is that dynamic injection is a"Self-initiated debugging and to load their own definition of dynamic link library"The process. The so-called self-initiative, in fact, through the code implementation, on Linux through the Ptrace can complete all the above functions, of course, the Ptrace function is relatively original, the function of debugging usually need a lot of high-level logic packaging talent enough to achieve. Before reading the following chapters, it is strongly recommended that you read the man documentation here.
Objective in general, we want to inject a process, mainly for the following purposes:
    • Enhance the function of the target process;
    • Fix target process defect;
    • Hijack the target process function;
    • Stealing target process data;
    • Tampering with target process data;


As seen, process A is injected into process B, by altering the register and memory, let process b load its own definition of dynamic library A, when a is loaded, a will attempt to load other modules, such as the Dex file, and so on, detailed injection steps such as the following:
    • Attatch, specify the target process, start debugging;
    • Getregs, get the register of the target process, save the scene;
    • Setregs, change the PC and other related registers, make it point to mmap;
    • Popetext, write the so path to the address space of the mmap application;
    • SETRESG, change the PC and other related registers, make it point to Dlopen;
    • Setregs, restore the scene;
    • DETACH, remove the debugging, make it recover;
The above is a simplified process, the entire injected code, I have uploaded to GitHub, address Https://github.com/boyliang/Poison when so is loaded into the target process by dlopen, we need to let the logic in so be run, The more complicated approach is to use Ptrace to alter the register, allowing the target process to call Dlsym to find the address of our function. There are two simple ways to do this, such as the following
    • Using the GCC Precompiled Directive __attribute__ ((__constructor__)), the function is to let so be loaded, functions are self-running;
__attribute__ ((__constructor__)) void Main () {logi (">>>>>>>>>>>>>i am in, I am a bad boy 1!!!! <<<<<<<<<<<<<< "); void* handle = Dlopen ("libinso.so", Rtld_now); void (*SETA_FUNC) (int) = (void (*) (int)) Dlsym (handle, "SetA"); if (seta_func) {   seta_func (999);}}
    • Using C + + Global object initialization, its constructor will be run by itself;
void Main (); static void* _main (void*) {main (); return NULL;} Class Entryclass {Public:entryclass () {pthread_t tid;pthread_create (&tid, NULL, _main, NULL);p Thread_detach (TID);}} Boy

Demo sample The following demo sample a demo sample injected by Ptrace involves two parts of code, part of the target process code as host, and part of the so code that we injected into libmyso.so
The host code consists of three source files, each of which is demo1.c,inso.h, inso.c
/* * inso.h * *  Created on:2014 June 24 *      Author:boyliang */__attribute__ ((Visibility ("default")) void SetA (int i); __attribute__ ((Visibility ("default"))) int geta ();
/* * INSO.C * *  Created on:2014 June 24 *      Author:boyliang * * #include <stdio.h> #include "inso.h" static int GA = 1;void SetA (int i) {GA = i;} int Geta () {return gA;}
/* * demo1.c * *  Created on:2014 June 24 *      Author:boyliang * * #include <stdio.h> #include <unistd.h> #in Clude "Inso.h" #include "log.h" int main () {Logi ("DEMO1 start."); while (1) {Logi ("%d", Geta ()), SetA (Geta () + 1); sleep (2);} return 0;}
Libmyso.so Code
/* * MYSO.C * *  Created on:2014 June 24 *      Author:boyliang * * #include <stdio.h> #include <stddef.h> #inc Lude <dlfcn.h> #include <pthread.h> #include <stddef.h> #include  "Log.h" __attribute__ ((__ constructor__)) void Main () {logi (">>>>>>>>>>>>>i am in, I am a bad boy 1!!!! <<<<<<<<<<<<<< "); void* handle = Dlopen (" libinso.so ", Rtld_now); Void (* SETA_FUNC) (int) = (void (*) (int)) Dlsym (handle, "SetA"), if (Seta_func) {seta_func (999);}}
Call the inject program, I'll name it poison, and use poison <so_path> <target_pit>. The following is the output display of the Demo sample:
I/TTT     (  594): DEMO1 start. I/TTT     (  594): 1i/ttt     (  594): 2i/ttt     (  594): 3i/ttt     (  594): 4i/ttt     (  594) : 5i/ttt     (  594): 6i/ttt     (  594): 7i/ttt     (  594): >>>>>>>>> >>>>i am in, I am a bad boy 1!!!! <<<<<<<<<<<<<<I/TTT     (  594): 999i/ttt     (  594): 1000I /TTT     (  594): 1001
When the./poison/data/local/tmp/libmyso.so 594 is run, a specific string appears immediately in the output, and the printed data becomes 999 at a time, proving that we have successfully injected. Demo sample Code The example above demonstrates the code, I put it on GitHub, and we assume that the study code can go to Https://github.com/boyliang/injection_by_ptrace in the "three", I'm going to introduce a specific injection technology on Android, so please look forward to it.

Attack on Android Injection "II"

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.