如何挂钩我的Android应用程序的系统调用

时间:2014-11-24 09:55:28

标签: android hook system-calls function-interposition library-interposition

我想拦截connect()系统调用并使用我自己的自定义实现。自定义实现将执行一些操作,例如为简单起见打印日志,然后进一步调用系统实现。

我看了Audrey's blog,其中的方法是修补PLT。但遗憾的是,当尝试更改重定位表中的地址时,此代码会崩溃。

经过一段时间的沉迷,我遇到了This already answered question。但是这里描述的方法给了我以下错误。

*****跳转到案例标签[-fpermissive] jni / test.cpp:107:20:错误:越过初始化' uint32_t entry_page_start' jni / test.cpp:106:15:错误:越过初始化'uint32_t page_size' *****

在建议的更改Andrey's blog之后,来自Here的钩子调用方法看起来像这样。

int hook_call(char *soname, char *symbol, unsigned newval) {  
soinfo *si = NULL;  
Elf32_Rel *rel = NULL;  
Elf32_Sym *s = NULL;   
unsigned int sym_offset = 0;  
if (!soname || !symbol || !newval)  
 return 0;  
si = (soinfo*) dlopen(soname, 0);  
if (!si)  
 return 0;  
s = soinfo_elf_lookup(si, elfhash(symbol), symbol);  
if (!s)  
 return 0;  
sym_offset = s - si->symtab;  
rel = si->plt_rel;  
/* walk through reloc table, find symbol index matching one we've got */  
for (int i = 0; i < si->plt_rel_count; i++, rel++) {  
  unsigned type = ELF32_R_TYPE(rel->r_info);  
  unsigned sym = ELF32_R_SYM(rel->r_info);  
  unsigned reloc = (unsigned)(rel->r_offset + si->base);  
  unsigned oldval = 0;  
  if (sym_offset == sym) {  
   switch(type) {  
     case R_ARM_JUMP_SLOT:
      // YOUR LINES
      uint32_t page_size = getpagesize();
      uint32_t entry_page_start = reloc& (~(page_size - 1));
      mprotect((uint32_t *)entry_page_start, page_size, PROT_READ | PROT_WRITE);

      /* we do not have to read original value, but it would be good   
       idea to make sure it contains what we are looking for */  
     oldval = *(unsigned*) reloc;  
     *((unsigned*)reloc) = newval;  
     return 1;  
   default:  
     return 0;  
}  

我做错了什么,我把mProtect()方法放在了错误的地方吗?我们有没有人在安德烈博客的帮助下做过这件事?还有其他方法吗?我被封锁了。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

该错误与mProtect()无关。这实际上是我放置代码片段的完全相同的地方。这是我的代码,它工作正常:

void* hook_call(char *soname, char *symbol, void* newval) {
 soinfo *si = NULL;
   Elf32_Rel *rel = NULL;
   Elf32_Sym *s = NULL;
   unsigned int sym_offset = 0;
   if (!soname || !symbol || !newval)
      return 0;

   si = (soinfo*) dlopen(soname, RTLD_LAZY);
   if (!si)
    return 0;


   s = soinfo_elf_lookup(si, elfhash(symbol), symbol);
   if (!s)
     return 0;

   sym_offset = s - si->symtab;
   rel = si->plt_rel;


   const char *strtab = si->strtab;
   Elf32_Sym *symtab = si->symtab;

   /* walk through reloc table, find symbol index matching one we've got */
   int i;

   for (i = 0; i < si->plt_rel_count; i++, rel++) {
    unsigned type = ELF32_R_TYPE(rel->r_info);
    unsigned sym = ELF32_R_SYM(rel->r_info);
    unsigned reloc = (unsigned)(rel->r_offset + si->base);
    //unsigned oldval = 0;
    void* pOldFun;

    if (sym_offset == sym) {

     switch(type) {
       case R_ARM_JUMP_SLOT:
          //Set appropriate memory access rights
          uint32_t page_size = getpagesize();
          uint32_t entry_page_start = reloc& (~(page_size - 1));
          mprotect((uint32_t *)entry_page_start, page_size, PROT_READ | PROT_WRITE);

         pOldFun = (void *)*((unsigned *)reloc);

          *((unsigned int*)reloc)= (unsigned)newval;  

          return pOldFun;
       default:
          return 0;
     }
    }
   }
   return 0;

}

*跳转到案例标签...错误:当使用开关案例时,如果未正确启动变量,即在一种情况下初始化并在另一种情况下使用,则通常会发生十字架初始化。看看这个question。发生了类似的错误并得到了解决。