首页
社区
课程
招聘
[原创]适配古河大佬的注入工具到 AndroidN+
发表于: 2018-11-24 10:01 10143

[原创]适配古河大佬的注入工具到 AndroidN+

2018-11-24 10:01
10143
原注入工具 https://bbs.pediy.com/thread-141355.htm

做了两个改动:

1. 获取目标进程中dlopen等函数地址时,先获取下导出该函数的模块名称,原代码是默认dlxxxx函数通过linker导出,有极少设备是libdl.so或libc.so

原代码:

int inject_remote_process(...) {
    ...
    dlopen_addr = get_remote_addr( target_pid, linker_path, (void *)dlopen );
    dlsym_addr = get_remote_addr( target_pid, linker_path, (void *)dlsym );
    dlclose_addr = get_remote_addr( target_pid, linker_path, (void *)dlclose );
    ...
}

修改后代码:

int inject_remote_process(...) {
    ...
    dlopen_addr = get_remote_addr(target_pid, (void *)dlopen);
    dlsym_addr = get_remote_addr(target_pid, (void *)dlsym );
    dlclose_addr = get_remote_addr(target_pid, (void *)dlclose);
    dlerror_addr = get_remote_addr(target_pid, (void *)dlerror);
    ...
}

void* get_remote_addr(pid_t target_pid, void* local_addr) {
    ...
    get_module_path((unsigned long)local_addr, module_path);
    local_handle = get_module_base(-1, module_path);
    remote_handle = get_module_base(target_pid, module_path);
    ...
}


2. androidN以上设备对dlopen的使用有权限检查,可以使用另外一个函数加载so,代码如下:

static void init_dlopen_ext_offset() {
    FILE *fp = NULL;
    if (!(fp = fopen(linker_path, "rb")))  {
        DEBUG_PRINT("[init_dlopen_ext_offset]Unable to open %s\n", linker_path);
        return;
    }

    fseek(fp, 0, SEEK_END);
    int size = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    char *buffer = (char*)malloc(size);
    if (fread(buffer, 1, size, fp) != size) {
        DEBUG_PRINT("fread error\n");
        return;
    }
    fclose(fp);

    unsigned long symstr_off = 0, symtab_off = 0, symtab_size = 0;
    unsigned long symtab_entsize = 0, symtab_count = 0;
    const elf_header_t* eh  = (elf_header_t*)buffer;
    const elf_sheader_t* esh = (elf_sheader_t*)(buffer + eh->shoff);
    char* section_str = esh[eh->shstrndx].sh_offset + buffer;

    for (int i = 0; i < eh->shnum; i++) {
        char* sname = esh[i].sh_name + section_str;
        if (strcmp(sname, ".symtab") == 0) {
            symtab_off = esh[i].sh_offset; 
            symtab_size = esh[i].sh_size;
            symtab_entsize = esh[i].sh_entsize;
            symtab_count = symtab_size / symtab_entsize;
            DEBUG_PRINT("[init_dlopen_ext_offset]: symtab offset = %lx, count=%lx, index= %d\n", 
                symtab_off, symtab_count, i);
        }
        if (strcmp(sname, ".strtab") == 0) {
            symstr_off = esh[i].sh_offset;
            DEBUG_PRINT("[init_dlopen_ext_offset] symstr offset = %lx, index = %d\n", symstr_off, i);
        }

    }

    if(!symtab_off) {
        DEBUG_PRINT("[init_dlopen_ext_offset] can't find symtab from sections\n");
    }

    elf_sym_t* edt = (elf_sym_t*)(buffer + symtab_off);

    const char* name_dlopen_ext_N = "__dl__ZL10dlopen_extPKciPK17android_dlextinfoPv";
    const char* name_dlopen_ext_O = "__dl__ZL10dlopen_extPKciPK17android_dlextinfoPKv";
    const char* name_dlopen_ext_P = "__dl___loader_android_dlopen_ext";

    for(int i = 0 ; i < symtab_count; i++) {
        uint8_t st_type = ELF32_ST_TYPE(edt[i].info);
        char* st_name = buffer + symstr_off + edt[i].name;
        // DEBUG_PRINT("[init_dlopen_ext_offset] walk sym name:%s, value:%x\n", st_name, edt[i].value);
        if (st_type == STT_FUNC && edt[i].size) {
            if(strcmp(st_name, name_dlopen_ext_N) == 0) {
                dlopen_ext_offset = edt[i].value;
                DEBUG_PRINT("[init_dlopen_ext_offset] find dlopen_ext_N: %x\n", dlopen_ext_offset);
                break;
            }
            else if (strcmp(st_name, name_dlopen_ext_O) == 0) {
                dlopen_ext_offset = edt[i].value;
                DEBUG_PRINT("[init_dlopen_ext_offset] find dlopen_ext_O: %x\n", dlopen_ext_offset);
                break;
            }
            else if (strcmp(st_name, name_dlopen_ext_P) == 0) {
                dlopen_ext_offset = edt[i].value;
                DEBUG_PRINT("[init_dlopen_ext_offset] find dlopen_ext_P: %x\n", dlopen_ext_offset);
                break;
            }
        }
    }

    free(buffer);
}


完整代码:https://github.com/longpoxin/inject_dex


[课程]FART 脱壳王!加量不加价!FART作者讲授!

收藏
免费 5
支持
分享
打赏 + 1.00雪花
打赏次数 1 雪花 + 1.00
 
赞赏  junkboy   +1.00 2018/11/24
最新回复 (9)
雪    币: 11716
活跃值: (133)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
支持
2018-11-24 10:06
1
雪    币: 302
活跃值: (825)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
支持
2018-11-24 12:24
1
雪    币: 21449
活跃值: (62288)
能力值: (RANK:125 )
在线值:
发帖
回帖
粉丝
4
666
2018-11-24 18:55
0
雪    币: 1392
活跃值: (4872)
能力值: ( LV13,RANK:240 )
在线值:
发帖
回帖
粉丝
5
牛逼了啊~
2018-11-26 10:56
0
雪    币: 2679
活跃值: (1576)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
6666
2018-11-26 17:50
0
雪    币: 1135
活跃值: (2018)
能力值: ( LV5,RANK:75 )
在线值:
发帖
回帖
粉丝
7
2018-11-26 18:04
0
雪    币: 6818
活跃值: (153)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
2018-11-27 06:49
0
雪    币: 230
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
9
手机小米5,安卓7,这个io错误是啥原因,能帮忙看看么
gemini:/data/local/tmp # ./inject 22652 /data/local/tmp/libbridge.so
[main] target_pid:22652, libpath:/data/local/tmp/libbridge.so
[init_sdk_ver] ro.build.version.sdk: 24
[init_dlopen_ext_offset]: symtab offset = 5faec, count=1888, index= 23
[init_dlopen_ext_offset] symstr offset = 7836c, index = 24
[init_dlopen_ext_offset] find dlopen_ext_N: 2bd1
[+] Injecting process: 22652
[get_module_path] module_path:/system/lib/libc.so
[+] get_remote_addr: local[0xe7c13000], remote[0x0]
[+] Remote mmap address: 0x20a91
[+] Calling mmap in target process, current pc=-1780947352
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[+] Target process returned from mmap, return value=0, pc=20a90
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] peektext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[+] read data: �������������������������������
[+] local dlopen=0xe7d2cc21, dlsym=0xe7d2ccd9, dlclose=0xe7d2cd11, dlerror=0xe7d2cb71
[get_module_path] module_path:/system/bin/linker
[+] get_remote_addr: local[0xe7d2a000], remote[0xffffffff]
[get_module_path] module_path:/system/bin/linker
[+] get_remote_addr: local[0xe7d2a000], remote[0xffffffff]
[get_module_path] module_path:/system/bin/linker
[+] get_remote_addr: local[0xe7d2a000], remote[0xffffffff]
[get_module_path] module_path:/system/bin/linker
[+] get_remote_addr: local[0xe7d2a000], remote[0xffffffff]
[+] Get imports: dlopen: 0x2c20, dlsym: 0x2cd8, dlclose: 0x2d10, dlerror: 0x2b70
library path = /data/local/tmp/libbridge.so, map_base = 0x0
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] peektext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[+] android 7.0+ system, use dlopen_ext
[+] target_dlopen_ext_addr addr: 0x2bd0
[+] target_caller_addr addr: 0x2000
[+] Calling dlopen_ext in target process, current pc=133776
[+] Target process returned from dlopen_ext, return value=0, pc=2bd0
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] peektext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[+] Calling dlsym in target process, current pc=11216
[+] Target process returned from dlsym, return value=0, pc=2cd8
hook_entry_addr = 0x0
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
[ptrace_writedata] peektext failed: I/O error
[ptrace_writedata] poketext failed: I/O error
Press enter to dlclose and detach
[+] Calling dlclose in target process, current pc=11480
[+] Target process returned from dlclose, return value=0, pc=e7d2cd10
2020-5-7 13:25
0
雪    币: 245
活跃值: (97)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
10
这些一般都无法直接使用的,不同手机,不同路径,很多需要自己修改的,
2022-3-14 21:00
0
游客
登录 | 注册 方可回帖
返回
//