/*
                        . .: .:.. :.. .. .:.::. :. ..:
                      <<-==млллллм=млллллм=млллллм===<
                       .:: ллл ллл:ллл ллл.ллл ллл .:.
                       . .:.мммллп.плллллл.ллллллл:..
                        ...лллмммм:ммммллл:ллл ллл.::.
                       >===ллллллл=ллллллп=ллл ллл=->>
                      .: .:.. ..:. .: ..:.::. ::.. :.:.

                                  [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 ;)

                                                                          */
/*- -[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;
}

/*- -[MZ.H] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ->8 */

//
// DOS EXE MZ format
//

#define IMAGE_DOS_SIGNATURE 0x5A4D        // MZ

typedef struct _IMAGE_DOS_HEADER {        // DOS EXE header
    WORD   e_magic;                       // Magic number
    WORD   e_cblp;                        // Bytes on last page of file
    WORD   e_cp;                          // Pages in file
    WORD   e_crlc;                        // Relocations
    WORD   e_cparhdr;                     // Size of header in paragraphs
    WORD   e_minalloc;                    // Minimum extra paragraphs needed
    WORD   e_maxalloc;                    // Maximum extra paragraphs needed
    WORD   e_ss;                          // Initial (relative) SS value
    WORD   e_sp;                          // Initial SP value
    WORD   e_csum;                        // Checksum
    WORD   e_ip;                          // Initial IP value
    WORD   e_cs;                          // Initial (relative) CS value
    WORD   e_lfarlc;                      // File address of relocation table
    WORD   e_ovno;                        // Overlay number
    WORD   e_res[4];                      // Reserved words
    WORD   e_oemid;                       // OEM identifier (for e_oeminfo)
    WORD   e_oeminfo;                     // OEM information; e_oemid specific
    WORD   e_res2[10];                    // Reserved words
    LONG   e_lfanew;                      // File address of new exe header
  } IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;

/*- -[PE.H] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ->8 */

//
// Portable Executable format
//

#define IMAGE_NT_SIGNATURE                  0x00004550  // PE00

// File header format.

typedef struct _IMAGE_FILE_HEADER {
    WORD    Machine;
    WORD    NumberOfSections;
    DWORD   TimeDateStamp;
    DWORD   PointerToSymbolTable;
    DWORD   NumberOfSymbols;
    WORD    SizeOfOptionalHeader;
    WORD    Characteristics;
} IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;

#define IMAGE_SIZEOF_FILE_HEADER             20

#define IMAGE_FILE_RELOCS_STRIPPED           0x0001  // Relocation info stripped from file.
#define IMAGE_FILE_EXECUTABLE_IMAGE          0x0002  // File is executable  (i.e. no unresolved externel references).
#define IMAGE_FILE_LINE_NUMS_STRIPPED        0x0004  // Line nunbers stripped from file.
#define IMAGE_FILE_LOCAL_SYMS_STRIPPED       0x0008  // Local symbols stripped from file.
#define IMAGE_FILE_MINIMAL_OBJECT            0x0010  // Reserved.
#define IMAGE_FILE_UPDATE_OBJECT             0x0020  // Reserved.
#define IMAGE_FILE_16BIT_MACHINE             0x0040  // 16 bit word machine.
#define IMAGE_FILE_BYTES_REVERSED_LO         0x0080  // Bytes of machine word are reversed.
#define IMAGE_FILE_32BIT_MACHINE             0x0100  // 32 bit word machine.
#define IMAGE_FILE_DEBUG_STRIPPED            0x0200  // Debugging info stripped from file in .DBG file
#define IMAGE_FILE_PATCH                     0x0400  // Reserved.
#define IMAGE_FILE_SYSTEM                    0x1000  // System File.
#define IMAGE_FILE_DLL                       0x2000  // File is a DLL.
#define IMAGE_FILE_BYTES_REVERSED_HI         0x8000  // Bytes of machine word are reversed.

#define IMAGE_FILE_MACHINE_UNKNOWN           0
#define IMAGE_FILE_MACHINE_I386              0x14c   // Intel 386.
#define IMAGE_FILE_MACHINE_R3000             0x162   // MIPS little-endian, 0540 big-endian
#define IMAGE_FILE_MACHINE_R4000             0x166   // MIPS little-endian
#define IMAGE_FILE_MACHINE_ALPHA             0x184   // Alpha_AXP
#define IMAGE_FILE_MACHINE_POWERPC           0x1F0   // IBM PowerPC Little-Endian

// Directory format.

typedef struct _IMAGE_DATA_DIRECTORY {
    DWORD   VirtualAddress;
    DWORD   Size;
} IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;

#define IMAGE_NUMBEROF_DIRECTORY_ENTRIES    16

// Optional header format.

typedef struct _IMAGE_OPTIONAL_HEADER {

    // Standard fields.

    WORD    Magic;
    BYTE    MajorLinkerVersion;
    BYTE    MinorLinkerVersion;
    DWORD   SizeOfCode;
    DWORD   SizeOfInitializedData;
    DWORD   SizeOfUninitializedData;
    DWORD   AddressOfEntryPoint;
    DWORD   BaseOfCode;
    DWORD   BaseOfData;

    // NT additional fields.

    DWORD   ImageBase;
    DWORD   SectionAlignment;
    DWORD   FileAlignment;
    WORD    MajorOperatingSystemVersion;
    WORD    MinorOperatingSystemVersion;
    WORD    MajorImageVersion;
    WORD    MinorImageVersion;
    WORD    MajorSubsystemVersion;
    WORD    MinorSubsystemVersion;
    DWORD   Reserved1;
    DWORD   SizeOfImage;
    DWORD   SizeOfHeaders;
    DWORD   CheckSum;
    WORD    Subsystem;
    WORD    DllCharacteristics;
    DWORD   SizeOfStackReserve;
    DWORD   SizeOfStackCommit;
    DWORD   SizeOfHeapReserve;
    DWORD   SizeOfHeapCommit;
    DWORD   LoaderFlags;
    DWORD   NumberOfRvaAndSizes;
    IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
} IMAGE_OPTIONAL_HEADER, *PIMAGE_OPTIONAL_HEADER;

#define IMAGE_SIZEOF_STD_OPTIONAL_HEADER      28
#define IMAGE_SIZEOF_NT_OPTIONAL_HEADER      224

#define IMAGE_NT_OPTIONAL_HDR_MAGIC        0x10b

typedef struct _IMAGE_NT_HEADERS {
    DWORD Signature;
    IMAGE_FILE_HEADER FileHeader;
    IMAGE_OPTIONAL_HEADER OptionalHeader;
} IMAGE_NT_HEADERS, *PIMAGE_NT_HEADERS;

// Calculate the byte offset of a field in a structure of type type.

#define FIELD_OFFSET(type, field)    ((LONG)&(((type *)0)->field))

// Calculate the first section header

#define IMAGE_FIRST_SECTION( ntheader ) ((PIMAGE_SECTION_HEADER)        \
    ((DWORD)ntheader +                                                  \
     FIELD_OFFSET( IMAGE_NT_HEADERS, OptionalHeader ) +                 \
     ((PIMAGE_NT_HEADERS)(ntheader))->FileHeader.SizeOfOptionalHeader   \
    ))

// Subsystem Values

#define IMAGE_SUBSYSTEM_UNKNOWN              0   // Unknown subsystem.
#define IMAGE_SUBSYSTEM_NATIVE               1   // Image doesn't require a subsystem.
#define IMAGE_SUBSYSTEM_WINDOWS_GUI          2   // Image runs in the Windows GUI subsystem.
#define IMAGE_SUBSYSTEM_WINDOWS_CUI          3   // Image runs in the Windows character subsystem.
#define IMAGE_SUBSYSTEM_OS2_CUI              5   // image runs in the OS/2 character subsystem.
#define IMAGE_SUBSYSTEM_POSIX_CUI            7   // image run  in the Posix character subsystem.

// Dll Characteristics

#define IMAGE_LIBRARY_PROCESS_INIT           1   // Dll has a process initialization routine.
#define IMAGE_LIBRARY_PROCESS_TERM           2   // Dll has a thread termination routine.
#define IMAGE_LIBRARY_THREAD_INIT            4   // Dll has a thread initialization routine.
#define IMAGE_LIBRARY_THREAD_TERM            8   // Dll has a thread termination routine.

// Loader Flags

#define IMAGE_LOADER_FLAGS_BREAK_ON_LOAD    0x00000001
#define IMAGE_LOADER_FLAGS_DEBUG_ON_LOAD    0x00000002


// Directory Entries

#define IMAGE_DIRECTORY_ENTRY_EXPORT         0   // Export Directory
#define IMAGE_DIRECTORY_ENTRY_IMPORT         1   // Import Directory
#define IMAGE_DIRECTORY_ENTRY_RESOURCE       2   // Resource Directory
#define IMAGE_DIRECTORY_ENTRY_EXCEPTION      3   // Exception Directory
#define IMAGE_DIRECTORY_ENTRY_SECURITY       4   // Security Directory
#define IMAGE_DIRECTORY_ENTRY_BASERELOC      5   // Base Relocation Table
#define IMAGE_DIRECTORY_ENTRY_DEBUG          6   // Debug Directory
#define IMAGE_DIRECTORY_ENTRY_COPYRIGHT      7   // Description String
#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR      8   // Machine Value (MIPS GP)
#define IMAGE_DIRECTORY_ENTRY_TLS            9   // TLS Directory
#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG   10   // Load Configuration Directory

// Section header format.

#define IMAGE_SIZEOF_SHORT_NAME              8

typedef struct _IMAGE_SECTION_HEADER {
    BYTE    Name[IMAGE_SIZEOF_SHORT_NAME];
    union {
            DWORD   PhysicalAddress;
            DWORD   VirtualSize;
    } Misc;
    DWORD   VirtualAddress;
    DWORD   SizeOfRawData;
    DWORD   PointerToRawData;
    DWORD   PointerToRelocations;
    DWORD   PointerToLinenumbers;
    WORD    NumberOfRelocations;
    WORD    NumberOfLinenumbers;
    DWORD   Characteristics;
} IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;

#define IMAGE_SIZEOF_SECTION_HEADER          40

#define IMAGE_SCN_TYPE_REGULAR               0x00000000  //
#define IMAGE_SCN_TYPE_DUMMY                 0x00000001  // Reserved.
#define IMAGE_SCN_TYPE_NO_LOAD               0x00000002  // Reserved.
#define IMAGE_SCN_TYPE_GROUPED               0x00000004  // Used for 16-bit offset code.
#define IMAGE_SCN_TYPE_NO_PAD                0x00000008  // Reserved.
#define IMAGE_SCN_TYPE_COPY                  0x00000010  // Reserved.

#define IMAGE_SCN_CNT_CODE                   0x00000020  // Section contains code.
#define IMAGE_SCN_CNT_INITIALIZED_DATA       0x00000040  // Section contains initialized data.
#define IMAGE_SCN_CNT_UNINITIALIZED_DATA     0x00000080  // Section contains uninitialized data.

#define IMAGE_SCN_LNK_OTHER                  0x00000100  // Reserved.
#define IMAGE_SCN_LNK_INFO                   0x00000200  // Section contains comments or some other type of information.
#define IMAGE_SCN_LNK_OVERLAY                0x00000400  // Section contains an overlay.
#define IMAGE_SCN_LNK_REMOVE                 0x00000800  // Section contents will not become part of image.
#define IMAGE_SCN_LNK_COMDAT                 0x00001000  // Section contents comdat.

#define IMAGE_SCN_ALIGN_1BYTES               0x00100000  //
#define IMAGE_SCN_ALIGN_2BYTES               0x00200000  //
#define IMAGE_SCN_ALIGN_4BYTES               0x00300000  //
#define IMAGE_SCN_ALIGN_8BYTES               0x00400000  //
#define IMAGE_SCN_ALIGN_16BYTES              0x00500000  // Default alignment if no others are specified.
#define IMAGE_SCN_ALIGN_32BYTES              0x00600000  //
#define IMAGE_SCN_ALIGN_64BYTES              0x00700000  //

#define IMAGE_SCN_MEM_DISCARDABLE            0x02000000  // Section can be discarded.
#define IMAGE_SCN_MEM_NOT_CACHED             0x04000000  // Section is not cachable.
#define IMAGE_SCN_MEM_NOT_PAGED              0x08000000  // Section is not pageable.
#define IMAGE_SCN_MEM_SHARED                 0x10000000  // Section is shareable.
#define IMAGE_SCN_MEM_EXECUTE                0x20000000  // Section is executable.
#define IMAGE_SCN_MEM_READ                   0x40000000  // Section is readable.
#define IMAGE_SCN_MEM_WRITE                  0x80000000  // Section is writeable.

/*- -[TYPES.H]- - - - - - - - - - - - - - - - - - - - - - - - - - - - ->8 */

#ifndef CHAR
typedef signed char CHAR;
#endif

#ifndef SHORT
typedef signed short SHORT;
#endif

#ifndef LONG
typedef signed long LONG;
#endif

#ifndef INT
typedef signed INT;
#endif

#ifndef BYTE
typedef unsigned char BYTE;
#endif

#ifndef WORD
typedef unsigned short WORD;
#endif

#ifndef DWORD
typedef unsigned long DWORD;
#endif

#ifndef UINT
typedef unsigned UINT;
#endif

#ifndef PCHAR
typedef CHAR *PCHAR;
#endif

#ifndef PSHORT
typedef SHORT *PSHORT;
#endif

#ifndef PLONG
typedef LONG *PLONG;
#endif

#ifndef PINT
typedef INT *PINT;
#endif

#ifndef PBYTE
typedef BYTE *PBYTE;
#endif

#ifndef PWORD
typedef WORD *PWORD;
#endif

#ifndef PDWORD
typedef DWORD *PDWORD;
#endif

#ifndef PUINT
typedef UINT *PUINT;
#endif
