- UID
- 66114
注册时间2010-4-1
阅读权限30
最后登录1970-1-1
龙战于野
TA的每日心情 | 慵懒 2019-3-12 17:25 |
---|
签到天数: 3 天 [LV.2]偶尔看看I
|
楼主 |
发表于 2010-5-26 14:11:43
|
显示全部楼层
- /*- -[PEWRSEC.C]- - - - - - - - - - - - - - - - - - - - - - - - - - - ->8 */
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "types.h"
- #include "mz.h"
- #include "pe.h"
- #define SizeBuffMZ sizeof(IMAGE_DOS_HEADER)
- #define SizeBuffPE (4 + IMAGE_SIZEOF_FILE_HEADER + IMAGE_SIZEOF_STD_OPTIONAL_HEADER)
- #define SizeBuffSH IMAGE_SIZEOF_SECTION_HEADER
- #define SizeBuffMax max(SizeBuffMZ, max(SizeBuffPE, SizeBuffSH))
- 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 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; }
- while (--argc) {
- if (*argv[argc] != '/') {
- if ((FileName = argv[argc]) == NULL) { printf("No filename specified\n"); goto Ret; } }
- else if (!strncmpi(argv[argc] + 1, "SEC:", 4)) SecName = argv[argc] + 5;
- else { printf("Unknown option '%s'\n", argv[argc]); goto Ret; } }
- 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;
- 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);
- 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;
- }
复制代码 主程序就这么两行?我们一行一行分析! |
|