现在的位置: 首页 > 综合 > 正文

一步一步走进Linux HOOK API(一)

2013年08月14日 ⁄ 综合 ⁄ 共 3208字 ⁄ 字号 评论关闭

文章转载自:

http://blog.csdn.net/yyttiao/article/details/7346287

主要是拷贝到自己博客,做个备份

最近我查阅很多参考资料.发现对于讲述Linux HOOK API的资料是很少,让我们这些新人难以去走进Linux HOOK大门.在这里我将全面的讲述Linux HOOK API的全部实现过程,这个过程中我也遇到很多坎坷,所以在这么写下这份教程.让大家都来进入HOOK的神秘世界.

不要认为HOOK APIwindows的专利(PS.其实我以前就是这么认为的.哈哈....),其实在Linux中也有HOOK API这样的技术,只是实现起来相对比较麻烦,首先今天主要带大家认识的是ELF文件,Linux,ELF文件主要是应用在可执行文件,重定位文件,可执行文件动态连接库。首先来看一下ELF Head的定义:

      PS.我们这里主要针对的是32位平台.有关64位平台相关定义请参阅/usr/include/elf.h

  1.    
  1. #define EI_NIDENT (16)  
  2.   
  3.   
  4. typedef struct  
  5.   
  6. {  
  7.   
  8.   unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */  
  9.   
  10.   Elf32_Half e_type; /* Object file type */  
  11.   
  12.   Elf32_Half e_machine; /* Architecture */  
  13.   
  14.   Elf32_Word e_version; /* Object file version */  
  15.   
  16.   Elf32_Addr e_entry; /* Entry point virtual address */  
  17.   
  18.   Elf32_Off e_phoff; /* Program header table file offset */  
  19.   
  20.   Elf32_Off e_shoff; /* Section header table file offset */  
  21.   
  22.   Elf32_Word e_flags; /* Processor-specific flags */  
  23.   
  24.   Elf32_Half e_ehsize; /* ELF header size in bytes */  
  25.   
  26.   Elf32_Half e_phentsize; /* Program header table entry size */  
  27.   
  28.   Elf32_Half e_phnum; /* Program header table entry count */  
  29.   
  30.   Elf32_Half e_shentsize; /* Section header table entry size */  
  31.   
  32.   Elf32_Half e_shnum; /* Section header table entry count */  
  33.   
  34.   Elf32_Half e_shstrndx; /* Section header string table index */  
  35.   
  36. } Elf32_Ehdr;  


 

e_ident: 这个成员,ELF文件的第一个成员,该成员是个数字,根据上面的宏可以看出,这个程序是个16字节的数据.该成员的前4个字节依次是 0x7F,0x45,0x4c,0x46,也
就是"\177ELF"。这是ELF文件的标志,任何一个ELF文件这四个字节都完全相同。

为了让我们更方便的使用ELF数据在elf.h中对上述数据进行了宏定义.如下:

  1. #define EI_MAG0 0 /* File identification byte 0 index */  
  2.   
  3. #define ELFMAG0 0x7f /* Magic number byte 0 *  
  4.   
  5. #define EI_MAG1 1 /* File identification byte 1 index */  
  6.   
  7. #define ELFMAG1 'E' /* Magic number byte 1 */  
  8.   
  9. #define EI_MAG2 2 /* File identification byte 2 index */  
  10.   
  11. #define ELFMAG2 'L' /* Magic number byte 2 */  
  12.   
  13. #define EI_MAG3 3 /* File identification byte 3 index */  
  14.   
  15. #define ELFMAG3 'F' /* Magic number byte 3 */  
  16.   
  17. /* Conglomeration of the identification bytes, for easy testing as a word.  */  
  18.   
  19. #define ELFMAG "\177ELF"  
  20.   
  21. #define SELFMAG 4  


第四个字节表示ELF格式,1:322:64

  1. #define EI_CLASS 4 /* File class byte index */  
  2.   
  3. #define ELFCLASSNONE 0 /* Invalid class */  
  4.   
  5. #define ELFCLASS32 1 /* 32-bit objects */  
  6.   
  7. #define ELFCLASS64 2 /* 64-bit objects */  
  8.   
  9. #define ELFCLASSNUM 3  


第五个字节表示数据编码格式,1:小端模式 2:大端模式

  1. #define EI_DATA 5 /* Data encoding byte index */  
  2.   
  3. #define ELFDATANONE 0 /* Invalid data encoding */  
  4.   
  5. #define ELFDATA2LSB 1 /* 2's complement, little endian */  
  6.   
  7. #define ELFDATA2MSB 2 /* 2's complement, big endian */  
  8.   
  9. #define ELFDATANUM 3  


第六个字节表示文件版本,该值目前必须为1

  1. #define EV_CURRENT 1 /* Current version */  

第七个字节表示操作系统标识:

  1. #define EI_OSABI 7 /* OS ABI identification */  
  2.   
  3. #define ELFOSABI_NONE 0 /* UNIX System V ABI */  
  4.   
  5. #define ELFOSABI_SYSV 0 /* Alias.  */  
  6.   
  7. #define ELFOSABI_HPUX 1 /* HP-UX */  
  8.   
  9. #define ELFOSABI_NETBSD 2 /* NetBSD.  */  
  10.   
  11. #define ELFOSABI_LINUX 3 /* Linux.  */  
  12.   
  13. #define ELFOSABI_SOLARIS 6 /* Sun Solaris.  */  
  14.   
  15. #define ELFOSABI_AIX 7 /* IBM AIX.  */  
  16.   
  17. #define ELFOSABI_IRIX 8 /* SGI Irix.  */  

抱歉!评论已关闭.