android下截獲動態庫的函數調用

來源:互聯網
上載者:User

 

最近工作需要開始研究hook,得益於http://blog.csdn.net/dinuliang/archive/2010/04/20/5509009.aspx 這篇文章,做出了些成果。

 

首先,這套hook的思路來源於linker的動態綁定,就是修改.rel表項所指向的該符號在映像(位於.got段)中的地址,可以看下面的代碼:

 

 

#include "linker.h"<br />void modifySymbol(void* handle, const char *name, void* new_addr){<br />if(!handle) return;<br />soinfo *si = (soinfo*)handle;<br />Elf32_Sym *symtab = si->symtab;<br /> const char *strtab = si->strtab;<br />Elf32_Rel *rel = si->rel;<br />unsigned count = si->rel_count;<br />unsigned idx;<br />for(idx=0; idx<count; idx++){<br />unsigned type = ELF32_R_TYPE(rel->r_info);<br />unsigned sym = ELF32_R_SYM(rel->r_info);<br />unsigned reloc = (unsigned)(rel->r_offset + si->base);<br />char *sym_name = (char *)(strtab + symtab[sym].st_name);<br />if(type==R_ARM_GLOB_DAT && strcmp(sym_name, name)==0){<br />*((unsigned*)reloc) = (Elf32_Addr)new_addr;<br />break;<br />}<br />rel++;<br />}<br />}

 

 

如果要hook xxx.so對malloc()的調用,只需要:

 

void* handle=dlopen("xxx.so", RTLD_NOW);<br />modifySymbol(handle, "malloc", (void*)fake_malloc);

 

這樣,這個庫對malloc的調用都會轉到fake_malloc()函數中

 

 

當然,也許符號是在.rel.plt表裡面,這種情況結合linker.h中的定義對modifySymbol()進行修改就可以了。

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.