飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 6061|回复: 9

PE Write Section(写入一个节)

[复制链接]
  • TA的每日心情
    慵懒
    2019-3-12 17:25
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    发表于 2010-5-26 14:01:57 | 显示全部楼层 |阅读模式
    本帖最后由 whypro 于 2010-5-26 14:04 编辑

    /*
                            . .: .:.. :.. .. .:.::. :. ..:
                          <<-==苒圹圹?苒圹圹?苒圹圹?==<
                           .:: 圹?圹?圹?圹?圹?圹?.:.
                           . .:.苘苒圻.咣圹圹?圹圹圹?..
                            ...圹圮苘?苘苘圹?圹?圹?::.
                           >===圹圹圹?圹圹圹?圹?圹?->>
                          .: .:.. ..:. .: ..:.::. ::.. :.:.

                                      [PEWRSEC]
                        PE Write Section, by Jacky Qwerty/29A


    Here's a new utility from 29A. This program simply sets the  write bit to a
    section in a PE file. This is needed when you need write access to the code
    section in a  first generation sample,  for instance.  There is one utility
    from the SDK (EDITBIN) which does exactly the same thing with PE filez, but
    it needs some huge DLLz from VC to work.  On the other hand, PEWRSEC can be
    compiled as a stupid COM file. Hope this will be handy enough for you ;)

                                                                              */



    29A杂志里的一篇文章,通过c语言文件操作来搞定添加新节的功能。
    VC++编译成功!

    23.zip (215.71 KB, 下载次数: 0)
    PYG19周年生日快乐!
  • TA的每日心情
    慵懒
    2019-3-12 17:25
  • 签到天数: 3 天

    [LV.2]偶尔看看I

     楼主| 发表于 2010-5-26 14:11:43 | 显示全部楼层
    1. /*- -[PEWRSEC.C]- - - - - - - - - - - - - - - - - - - - - - - - - - - ->8 */

    2. #include <errno.h>
    3. #include <stdio.h>
    4. #include <stdlib.h>
    5. #include <string.h>
    6. #include "types.h"
    7. #include "mz.h"
    8. #include "pe.h"

    9. #define SizeBuffMZ sizeof(IMAGE_DOS_HEADER)
    10. #define SizeBuffPE (4 + IMAGE_SIZEOF_FILE_HEADER + IMAGE_SIZEOF_STD_OPTIONAL_HEADER)
    11. #define SizeBuffSH IMAGE_SIZEOF_SECTION_HEADER
    12. #define SizeBuffMax max(SizeBuffMZ, max(SizeBuffPE, SizeBuffSH))

    13. INT Strncmpz(BYTE *S1, BYTE *S2, INT Count) {
    14.   while (Count--) {
    15.     if (*S1 < *S2) return -1;  // This fucntion doesnt seem to be implemented
    16.     if (*S1 > *S2++) return 1; // in the standard C string library, It combines
    17.     if (!*S1++) break; }       // the funtionality of "strcmp" and "strncmp".
    18.   return 0;
    19. }

    20. INT main(INT argc, CHAR *argv[]) {
    21.   FILE *File;
    22.   INT RetValue = 1;
    23.   PCHAR SecName = NULL, FileName = NULL;
    24.   WORD Sections;
    25.   PIMAGE_DOS_HEADER pMZ;
    26.   PIMAGE_NT_HEADERS pPE;
    27.   PIMAGE_SECTION_HEADER pSH;
    28.   CHAR Buffer[SizeBuffMax];
    29.   printf("PEWRSEC - Sets the WRITE bit to a PE section - (c) 1997 jqwerty/29A\n\n");
    30.   if (argc != 2 && argc != 3) {
    31.     printf("  Syntax: PEWRSEC [/SEC:<SectionName>] <FileName>  (default: code section)\n");
    32.     Ret: return RetValue; }
    33.   while (--argc) {
    34.     if (*argv[argc] != '/') {
    35.       if ((FileName = argv[argc]) == NULL) { printf("No filename specified\n"); goto Ret; } }
    36.     else if (!strncmpi(argv[argc] + 1, "SEC:", 4)) SecName = argv[argc] + 5;
    37.          else { printf("Unknown option '%s'\n", argv[argc]); goto Ret; } }
    38.   if ((File = fopen(FileName, "rb+")) == 0) {
    39.     printf("Can't open '%s'\n", FileName); goto Ret; }
    40.   if (!fread(pMZ = (PIMAGE_DOS_HEADER)Buffer, SizeBuffMZ, 1, File)) {
    41.     ReadErr:
    42.       if (!feof(File)) { printf("Error reading file\n"); CloseFile: fclose(File); goto Ret; }
    43.       else { InvalidPE: printf("Not a valid PE file\n"); goto CloseFile; } }
    44.   if (pMZ->e_magic != IMAGE_DOS_SIGNATURE) goto InvalidPE;
    45.   if (fseek(File, pMZ->e_lfanew, SEEK_SET)) {
    46.     SeekErr:
    47.       if (errno != EBADF) { printf("Error in file seek\n"); goto CloseFile; }
    48.       else goto InvalidPE; }
    49.   if (!fread(pPE = (PIMAGE_NT_HEADERS)Buffer, SizeBuffPE, 1, File)) goto ReadErr;
    50.   if (pPE->Signature != IMAGE_NT_SIGNATURE || !(Sections = pPE->FileHeader.NumberOfSections)) goto InvalidPE;
    51.   if (fseek(File, FIELD_OFFSET(IMAGE_NT_HEADERS, OptionalHeader) + pPE->FileHeader.SizeOfOptionalHeader - SizeBuffPE, SEEK_CUR)) goto SeekErr;
    52.   do {
    53.     if (!fread(pSH = (PIMAGE_SECTION_HEADER)Buffer, SizeBuffSH, 1, File)) goto ReadErr;
    54.     if (SecName) { if (!Strncmpz(SecName, pSH->Name, 8)) break; }
    55.     else if (pSH->VirtualAddress <= pPE->OptionalHeader.AddressOfEntryPoint && pPE->OptionalHeader.AddressOfEntryPoint < pSH->VirtualAddress + pSH->Misc.VirtualSize) break;
    56.   } while (--Sections);               
    57.   if (!Sections) { printf("Section not found\n"); goto CloseFile; }
    58.   if (!(pSH->Characteristics & IMAGE_SCN_MEM_WRITE)) {
    59.     pSH->Characteristics |= IMAGE_SCN_MEM_WRITE;
    60.     if (fseek(File, - SizeBuffSH, SEEK_CUR)) goto SeekErr;
    61.     if (!fwrite(pSH, SizeBuffSH, 1, File) || fflush(File)) {
    62.       printf("Error writing file\n"); goto CloseFile; } }
    63.   printf("Ok\n"); RetValue = 0; goto CloseFile;
    64. }
    复制代码
    主程序就这么两行?我们一行一行分析!
    PYG19周年生日快乐!
  • TA的每日心情
    慵懒
    2019-3-12 17:25
  • 签到天数: 3 天

    [LV.2]偶尔看看I

     楼主| 发表于 2010-5-26 14:14:50 | 显示全部楼层
    3-9行是文件头
    #include <errno.h>

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    #include "types.h"<------------自定义类型

    #include "mz.h"<------------dos文件头

    #include "pe.h" <------------pe文件头
    PYG19周年生日快乐!
  • TA的每日心情
    慵懒
    2019-3-12 17:25
  • 签到天数: 3 天

    [LV.2]偶尔看看I

     楼主| 发表于 2010-5-26 14:17:37 | 显示全部楼层
    本帖最后由 whypro 于 2010-5-26 14:26 编辑

    11-14行
    参考pe.h文件头
    #define SizeBuffMZ sizeof(IMAGE_DOS_HEADER)
    返回IMAGE_DOS_HEADER大小存入SizeBuffMZ
    #define SizeBuffPE (4 + IMAGE_SIZEOF_FILE_HEADER + IMAGE_SIZEOF_STD_OPTIONAL_HEADER)
    IMAGE_NT_HEADERS大小存入SizeBuffPE
    #define SizeBuffSH IMAGE_SIZEOF_SECTION_HEADER
    IMAGE_SECTION_HEADER大小存入SizeBuffSH
    #define SizeBuffMax max(SizeBuffMZ, max(SizeBuffPE, SizeBuffSH))
    #define max(a,b)    (((a) > (b)) ? (a) : (b))
    返回最大那个存SizeBuffMax
    PYG19周年生日快乐!
  • TA的每日心情
    慵懒
    2019-3-12 17:25
  • 签到天数: 3 天

    [LV.2]偶尔看看I

     楼主| 发表于 2010-5-26 14:28:21 | 显示全部楼层
    本帖最后由 whypro 于 2010-5-26 14:38 编辑

    16-22行
    自定义函数比较两个字符的大小!
    INT Strncmpz(BYTE *S1, BYTE *S2, INT Count) {

      while (Count--) {

        if (*S1 < *S2) return -1;  // This fucntion doesnt seem to be implemented

        if (*S1 > *S2++) return 1; // in the standard C string library, It combines

        if (!*S1++) break; }       // the funtionality of "strcmp" and "strncmp".

      return 0;

    }

    和它的功能差不多一样
    int strncmp ( const char * str1, const char * str2, size_t num );

    #include <stdio.h>
    #include <string.h>

    int main ()
    {
      char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
      int n;
      puts ("Looking for R2 astromech droids...");
      for (n=0 ; n<3 ; n++)
        if (strncmp (str[n],"R2xx",2) == 0)
        {
          printf ("found %s\n",str[n]);
        }
      return 0;
    }
    Looking for R2 astromech droids...
    found R2D2
    found R2A6
    PYG19周年生日快乐!
  • TA的每日心情
    慵懒
    2019-3-12 17:25
  • 签到天数: 3 天

    [LV.2]偶尔看看I

     楼主| 发表于 2010-5-26 14:39:49 | 显示全部楼层
    本帖最后由 whypro 于 2010-5-26 14:46 编辑

    24-36行
    INT main(INT argc, CHAR *argv[]) {

      FILE *File;

      INT RetValue = 1;

      PCHAR SecName = NULL, FileName = NULL;

      WORD Sections;

      PIMAGE_DOS_HEADER pMZ;

      PIMAGE_NT_HEADERS pPE;

      PIMAGE_SECTION_HEADER pSH;

      CHAR Buffer[SizeBuffMax];
    _________________________________分割线       结构,变量,数组都有体现 ——————————————————————
      printf("PEWRSEC - Sets the WRITE bit to a PE section - (c) 1997 jqwerty/29A\n\n");

      if (argc != 2 && argc != 3) {

        printf("  Syntax: PEWRSEC [/SEC:<SectionName>] <FileName>  (default: code section)\n");

        Ret: return RetValue; }
    命令行参数argc请看 我以前发的贴 好了 就剩printf了自己玩吧!
    PYG19周年生日快乐!
  • TA的每日心情
    慵懒
    2019-3-12 17:25
  • 签到天数: 3 天

    [LV.2]偶尔看看I

     楼主| 发表于 2010-5-26 14:49:30 | 显示全部楼层
    37-41行
    while (--argc) {
        if (*argv[argc] != '/') {
          if ((FileName = argv[argc]) == NULL) { printf("No filename specified\n"); goto Ret; } }
        else if (!Strncmpz(argv[argc] + 1, "SEC:", 4)) SecName = argv[argc] + 5;
             else { printf("Unknown option '%s'\n", argv[argc]); goto Ret; } }
    PYG19周年生日快乐!
  • TA的每日心情
    慵懒
    2019-3-12 17:25
  • 签到天数: 3 天

    [LV.2]偶尔看看I

     楼主| 发表于 2010-5-26 14:50:44 | 显示全部楼层
    本帖最后由 whypro 于 2010-5-26 14:52 编辑

    if ((File = fopen(FileName, "rb+")) == 0) {
        printf("Can't open '%s'\n", FileName); goto Ret; }
      if (!fread(pMZ = (PIMAGE_DOS_HEADER)Buffer, SizeBuffMZ, 1, File)) {
        ReadErr:
          if (!feof(File)) { printf("Error reading file\n"); CloseFile: fclose(File); goto Ret; }
          else { InvalidPE: printf("Not a valid PE file\n"); goto CloseFile; } }
      if (pMZ->e_magic != IMAGE_DOS_SIGNATURE) goto InvalidPE;
      if (fseek(File, pMZ->e_lfanew, SEEK_SET)) {
        SeekErr:
          if (errno != EBADF) { printf("Error in file seek\n"); goto CloseFile; }
          else goto InvalidPE; }
      if (!fread(pPE = (PIMAGE_NT_HEADERS)Buffer, SizeBuffPE, 1, File)) goto ReadErr;
      if (pPE->Signature != IMAGE_NT_SIGNATURE || !(Sections = pPE->FileHeader.NumberOfSections)) goto InvalidPE;
      if (fseek(File, FIELD_OFFSET(IMAGE_NT_HEADERS, OptionalHeader) + pPE->FileHeader.SizeOfOptionalHeader - SizeBuffPE, SEEK_CUR)) goto SeekErr;
    PYG19周年生日快乐!
  • TA的每日心情
    慵懒
    2019-3-12 17:25
  • 签到天数: 3 天

    [LV.2]偶尔看看I

     楼主| 发表于 2010-5-26 14:50:51 | 显示全部楼层
    do {
        if (!fread(pSH = (PIMAGE_SECTION_HEADER)Buffer, SizeBuffSH, 1, File)) goto ReadErr;
        if (SecName) { if (!Strncmpz(SecName, pSH->Name, 8)) break; }
        else if (pSH->VirtualAddress <= pPE->OptionalHeader.AddressOfEntryPoint && pPE->OptionalHeader.AddressOfEntryPoint < pSH->VirtualAddress + pSH->Misc.VirtualSize) break;
      } while (--Sections);
    PYG19周年生日快乐!
  • TA的每日心情
    慵懒
    2019-3-12 17:25
  • 签到天数: 3 天

    [LV.2]偶尔看看I

     楼主| 发表于 2010-5-26 14:50:58 | 显示全部楼层
    本帖最后由 whypro 于 2010-5-26 14:53 编辑

    if (!Sections) { printf("Section not found\n"); goto CloseFile; }
      if (!(pSH->Characteristics & IMAGE_SCN_MEM_WRITE)) {
        pSH->Characteristics |= IMAGE_SCN_MEM_WRITE;
        if (fseek(File, - SizeBuffSH, SEEK_CUR)) goto SeekErr;
        if (!fwrite(pSH, SizeBuffSH, 1, File) || fflush(File)) {
          printf("Error writing file\n"); goto CloseFile; } }
      printf("Ok\n"); RetValue = 0; goto CloseFile;
    PYG19周年生日快乐!
    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表