文章转载自:
http://blog.csdn.net/yyttiao/article/details/7346287
主要是拷贝到自己博客,做个备份
最近我查阅很多参考资料.发现对于讲述Linux HOOK API的资料是很少,让我们这些新人难以去走进Linux HOOK大门.在这里我将全面的讲述Linux HOOK API的全部实现过程,这个过程中我也遇到很多坎坷,所以在这么写下这份教程.让大家都来进入HOOK的神秘世界.
不要认为HOOK API是windows的专利(PS.其实我以前就是这么认为的.哈哈....),其实在Linux中也有HOOK API这样的技术,只是实现起来相对比较麻烦,首先今天主要带大家认识的是ELF文件,在Linux中,ELF文件主要是应用在可执行文件,重定位文件,可执行文件动态连接库。首先来看一下ELF Head的定义:
PS.我们这里主要针对的是32位平台.有关64位平台相关定义请参阅/usr/include/elf.h
- #define EI_NIDENT (16)
- typedef struct
- {
- unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
- Elf32_Half e_type; /* Object file type */
- Elf32_Half e_machine; /* Architecture */
- Elf32_Word e_version; /* Object file version */
- Elf32_Addr e_entry; /* Entry point virtual address */
- Elf32_Off e_phoff; /* Program header table file offset */
- Elf32_Off e_shoff; /* Section header table file offset */
- Elf32_Word e_flags; /* Processor-specific flags */
- Elf32_Half e_ehsize; /* ELF header size in bytes */
- Elf32_Half e_phentsize; /* Program header table entry size */
- Elf32_Half e_phnum; /* Program header table entry count */
- Elf32_Half e_shentsize; /* Section header table entry size */
- Elf32_Half e_shnum; /* Section header table entry count */
- Elf32_Half e_shstrndx; /* Section header string table index */
- } Elf32_Ehdr;
e_ident: 这个成员,是ELF文件的第一个成员,该成员是个数字,根据上面的宏可以看出,这个程序是个16字节的数据.该成员的前4个字节依次是 0x7F,0x45,0x4c,0x46,也
就是"\177ELF"。这是ELF文件的标志,任何一个ELF文件这四个字节都完全相同。
为了让我们更方便的使用ELF数据在elf.h中对上述数据进行了宏定义.如下:
- #define EI_MAG0 0 /* File identification byte 0 index */
- #define ELFMAG0 0x7f /* Magic number byte 0 *
- #define EI_MAG1 1 /* File identification byte 1 index */
- #define ELFMAG1 'E' /* Magic number byte 1 */
- #define EI_MAG2 2 /* File identification byte 2 index */
- #define ELFMAG2 'L' /* Magic number byte 2 */
- #define EI_MAG3 3 /* File identification byte 3 index */
- #define ELFMAG3 'F' /* Magic number byte 3 */
- /* Conglomeration of the identification bytes, for easy testing as a word. */
- #define ELFMAG "\177ELF"
- #define SELFMAG 4
第四个字节表示ELF格式,1:32位2:64位
- #define EI_CLASS 4 /* File class byte index */
- #define ELFCLASSNONE 0 /* Invalid class */
- #define ELFCLASS32 1 /* 32-bit objects */
- #define ELFCLASS64 2 /* 64-bit objects */
- #define ELFCLASSNUM 3
第五个字节表示数据编码格式,1:小端模式 2:大端模式
- #define EI_DATA 5 /* Data encoding byte index */
- #define ELFDATANONE 0 /* Invalid data encoding */
- #define ELFDATA2LSB 1 /* 2's complement, little endian */
- #define ELFDATA2MSB 2 /* 2's complement, big endian */
- #define ELFDATANUM 3
第六个字节表示文件版本,该值目前必须为1
- #define EV_CURRENT 1 /* Current version */
第七个字节表示操作系统标识:
- #define EI_OSABI 7 /* OS ABI identification */
- #define ELFOSABI_NONE 0 /* UNIX System V ABI */
- #define ELFOSABI_SYSV 0 /* Alias. */
- #define ELFOSABI_HPUX 1 /* HP-UX */
- #define ELFOSABI_NETBSD 2 /* NetBSD. */
- #define ELFOSABI_LINUX 3 /* Linux. */
- #define ELFOSABI_SOLARIS 6 /* Sun Solaris. */
- #define ELFOSABI_AIX 7 /* IBM AIX. */
- #define ELFOSABI_IRIX 8 /* SGI Irix. */