2005-08-07 07:32:36 +00:00
|
|
|
/*
|
|
|
|
* PE Fixup Utility
|
|
|
|
* Copyright (C) 2005 Filip Navara
|
2020-04-13 10:20:47 +00:00
|
|
|
* Copyright (C) 2020 Mark Jansen
|
2005-08-07 07:32:36 +00:00
|
|
|
*
|
|
|
|
* The purpose of this utility is fix PE binaries generated by binutils and
|
|
|
|
* to manipulate flags that can't be set by binutils.
|
|
|
|
*
|
2020-04-13 10:20:47 +00:00
|
|
|
* Currently one features is implemented:
|
2005-08-07 07:32:36 +00:00
|
|
|
*
|
2020-04-13 10:20:47 +00:00
|
|
|
* - Updating the PE header to use a LOAD_CONFIG,
|
|
|
|
* when the struct is exported with the name '_load_config_used'
|
2005-08-07 07:32:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-04-13 10:20:47 +00:00
|
|
|
// host_includes
|
|
|
|
#include <typedefs.h>
|
|
|
|
#include <pecoff.h>
|
|
|
|
#include "../../dll/win32/dbghelp/compat.h"
|
2006-06-19 16:38:27 +00:00
|
|
|
|
2020-04-18 13:18:38 +00:00
|
|
|
static const char* g_ApplicationName;
|
|
|
|
static const char* g_Target;
|
|
|
|
|
2021-02-24 09:14:40 +00:00
|
|
|
enum fixup_mode
|
|
|
|
{
|
2021-04-11 00:30:15 +00:00
|
|
|
MODE_NONE = 0,
|
2021-02-24 09:14:40 +00:00
|
|
|
MODE_LOADCONFIG,
|
2021-02-24 10:46:53 +00:00
|
|
|
MODE_KERNELDRIVER,
|
|
|
|
MODE_WDMDRIVER,
|
|
|
|
MODE_KERNELDLL,
|
|
|
|
MODE_KERNEL
|
2021-02-24 09:14:40 +00:00
|
|
|
};
|
|
|
|
|
2020-04-13 10:20:47 +00:00
|
|
|
void *rva_to_ptr(unsigned char *buffer, PIMAGE_NT_HEADERS nt_header, DWORD rva)
|
2006-06-19 16:38:27 +00:00
|
|
|
{
|
2020-04-13 10:20:47 +00:00
|
|
|
unsigned int i;
|
|
|
|
PIMAGE_SECTION_HEADER section_header = IMAGE_FIRST_SECTION(nt_header);
|
|
|
|
|
|
|
|
for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++, section_header++)
|
|
|
|
{
|
|
|
|
if (rva >= section_header->VirtualAddress &&
|
|
|
|
rva < section_header->VirtualAddress + section_header->Misc.VirtualSize)
|
|
|
|
{
|
|
|
|
return buffer + rva - section_header->VirtualAddress + section_header->PointerToRawData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2006-06-19 16:38:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 13:18:38 +00:00
|
|
|
static void error(const char* message, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
fprintf(stderr, "%s ERROR: '%s': ", g_ApplicationName, g_Target);
|
|
|
|
|
|
|
|
va_start(args, message);
|
2021-04-10 18:05:18 +00:00
|
|
|
vfprintf(stderr, message, args);
|
2020-04-18 13:18:38 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
2006-06-19 16:38:27 +00:00
|
|
|
|
2021-04-10 18:21:43 +00:00
|
|
|
static void fix_checksum(unsigned char *buffer, size_t len, PIMAGE_NT_HEADERS nt_header)
|
2006-06-19 16:38:27 +00:00
|
|
|
{
|
2020-04-13 10:20:47 +00:00
|
|
|
unsigned int checksum = 0;
|
2021-04-10 18:21:43 +00:00
|
|
|
size_t n;
|
2006-06-19 16:38:27 +00:00
|
|
|
|
2020-04-13 10:20:47 +00:00
|
|
|
nt_header->OptionalHeader.CheckSum = 0;
|
2005-08-07 07:32:36 +00:00
|
|
|
|
2020-04-13 10:20:47 +00:00
|
|
|
for (n = 0; n < len; n += 2)
|
|
|
|
{
|
|
|
|
checksum += *(unsigned short *)(buffer + n);
|
|
|
|
checksum = (checksum + (checksum >> 16)) & 0xffff;
|
|
|
|
}
|
2005-08-07 07:32:36 +00:00
|
|
|
|
2021-04-10 18:21:43 +00:00
|
|
|
checksum += (unsigned int)len;
|
2020-04-13 10:20:47 +00:00
|
|
|
nt_header->OptionalHeader.CheckSum = checksum;
|
2005-08-07 07:32:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 10:20:47 +00:00
|
|
|
static int add_loadconfig(unsigned char *buffer, PIMAGE_NT_HEADERS nt_header)
|
2005-08-07 07:32:36 +00:00
|
|
|
{
|
2020-04-13 10:20:47 +00:00
|
|
|
PIMAGE_DATA_DIRECTORY export_dir;
|
|
|
|
PIMAGE_EXPORT_DIRECTORY export_directory;
|
2021-04-10 18:21:43 +00:00
|
|
|
PDWORD name_ptr, function_ptr;
|
|
|
|
PWORD ordinal_ptr;
|
|
|
|
DWORD n;
|
2020-04-13 10:20:47 +00:00
|
|
|
|
|
|
|
export_dir = &nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
|
2021-04-10 18:21:43 +00:00
|
|
|
if (export_dir->Size == 0)
|
2020-04-13 10:20:47 +00:00
|
|
|
{
|
2021-04-10 18:21:43 +00:00
|
|
|
error("No export directory\n");
|
|
|
|
return 1;
|
2020-04-13 10:20:47 +00:00
|
|
|
}
|
2021-04-10 18:21:43 +00:00
|
|
|
|
|
|
|
export_directory = rva_to_ptr(buffer, nt_header, export_dir->VirtualAddress);
|
|
|
|
if (export_directory == NULL)
|
2020-04-13 10:20:47 +00:00
|
|
|
{
|
2021-04-10 18:21:43 +00:00
|
|
|
error("Invalid rva for export directory\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
name_ptr = rva_to_ptr(buffer, nt_header, export_directory->AddressOfNames);
|
|
|
|
ordinal_ptr = rva_to_ptr(buffer, nt_header, export_directory->AddressOfNameOrdinals);
|
|
|
|
function_ptr = rva_to_ptr(buffer, nt_header, export_directory->AddressOfFunctions);
|
|
|
|
|
|
|
|
for (n = 0; n < export_directory->NumberOfNames; n++)
|
|
|
|
{
|
|
|
|
const char* name = rva_to_ptr(buffer, nt_header, name_ptr[n]);
|
|
|
|
if (!strcmp(name, "_load_config_used"))
|
|
|
|
{
|
|
|
|
PIMAGE_DATA_DIRECTORY load_config_dir;
|
|
|
|
DWORD load_config_rva = function_ptr[ordinal_ptr[n]];
|
|
|
|
PDWORD load_config_ptr = rva_to_ptr(buffer, nt_header, load_config_rva);
|
|
|
|
|
|
|
|
/* Update the DataDirectory pointer / size
|
|
|
|
The first entry of the LOAD_CONFIG struct is the size, use that as DataDirectory.Size */
|
|
|
|
load_config_dir = &nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG];
|
|
|
|
load_config_dir->VirtualAddress = load_config_rva;
|
|
|
|
load_config_dir->Size = *load_config_ptr;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-04-13 10:20:47 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 18:21:43 +00:00
|
|
|
error("Export '_load_config_used' not found\n");
|
2020-04-13 10:20:47 +00:00
|
|
|
return 1;
|
2005-08-07 07:32:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 00:30:15 +00:00
|
|
|
static int driver_fixup(enum fixup_mode mode, unsigned char *buffer, PIMAGE_NT_HEADERS nt_header)
|
2021-02-24 09:14:40 +00:00
|
|
|
{
|
|
|
|
/* GNU LD just doesn't know what a driver is, and has notably no idea of paged vs non-paged sections */
|
2021-04-10 18:21:43 +00:00
|
|
|
for (unsigned int i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
|
2021-02-24 09:14:40 +00:00
|
|
|
{
|
|
|
|
PIMAGE_SECTION_HEADER Section = IMAGE_FIRST_SECTION(nt_header) + i;
|
|
|
|
|
2021-02-24 10:46:53 +00:00
|
|
|
/* LD puts alignment crap that nobody asked for */
|
|
|
|
Section->Characteristics &= ~IMAGE_SCN_ALIGN_MASK;
|
|
|
|
|
|
|
|
/* LD overdoes it and puts the initialized flag everywhere */
|
|
|
|
if (Section->Characteristics & IMAGE_SCN_CNT_CODE)
|
|
|
|
Section->Characteristics &= ~IMAGE_SCN_CNT_INITIALIZED_DATA;
|
|
|
|
|
2021-04-11 00:24:19 +00:00
|
|
|
if (strncmp((char*)Section->Name, ".rsrc", 5) == 0)
|
2021-02-24 10:46:53 +00:00
|
|
|
{
|
2021-04-08 14:50:51 +00:00
|
|
|
/* .rsrc is discardable for driver images, WDM drivers and Kernel-Mode DLLs */
|
|
|
|
if (mode == MODE_KERNELDRIVER || mode == MODE_WDMDRIVER || mode == MODE_KERNELDLL)
|
|
|
|
{
|
|
|
|
Section->Characteristics |= IMAGE_SCN_MEM_DISCARDABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For some reason, .rsrc is made writable by windres */
|
2021-02-24 10:46:53 +00:00
|
|
|
Section->Characteristics &= ~IMAGE_SCN_MEM_WRITE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-02-24 09:14:40 +00:00
|
|
|
/* Known sections which can be discarded */
|
2021-04-11 00:24:19 +00:00
|
|
|
if (strncmp((char*)Section->Name, "INIT", 4) == 0)
|
2021-02-24 09:14:40 +00:00
|
|
|
{
|
|
|
|
Section->Characteristics |= IMAGE_SCN_MEM_DISCARDABLE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Known sections which can be paged */
|
2021-04-11 00:24:19 +00:00
|
|
|
if ((strncmp((char*)Section->Name, "PAGE", 4) == 0)
|
|
|
|
|| (strncmp((char*)Section->Name, ".rsrc", 5) == 0)
|
|
|
|
|| (strncmp((char*)Section->Name, ".edata", 6) == 0)
|
|
|
|
|| (strncmp((char*)Section->Name, ".reloc", 6) == 0))
|
2021-02-24 09:14:40 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If it's discardable, don't set the flag */
|
|
|
|
if (Section->Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Section->Characteristics |= IMAGE_SCN_MEM_NOT_PAGED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-11 00:30:15 +00:00
|
|
|
/* NOTE: This function tokenizes its parameter in place.
|
|
|
|
* Its format is: name[=newname][,[[!]{CDEIKOMPRSUW}][A{1248PTSX}]] */
|
|
|
|
static int change_section_attribs(char* section_attribs, unsigned char* buffer, PIMAGE_NT_HEADERS nt_header)
|
|
|
|
{
|
|
|
|
char *sectionName, *newSectionName;
|
|
|
|
char *attribsSpec, *alignSpec;
|
|
|
|
char *ptr;
|
|
|
|
DWORD dwAttribs[2]; /* Attributes to [0]: set; [1]: filter */
|
|
|
|
unsigned int i;
|
|
|
|
PIMAGE_SECTION_HEADER SectionTable, Section;
|
|
|
|
|
|
|
|
if (!section_attribs || !*section_attribs)
|
|
|
|
{
|
|
|
|
error("Section attributes specification is empty.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sectionName = section_attribs;
|
|
|
|
|
|
|
|
/* Find the optional new section name and attributes specifications */
|
|
|
|
newSectionName = strchr(section_attribs, '=');
|
|
|
|
attribsSpec = strchr(section_attribs, ',');
|
|
|
|
if (newSectionName && attribsSpec)
|
|
|
|
{
|
|
|
|
/* The attributes specification must be after the new name */
|
|
|
|
if (!(newSectionName < attribsSpec))
|
|
|
|
{
|
|
|
|
error("Invalid section attributes specification.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (newSectionName)
|
|
|
|
*newSectionName++ = 0;
|
|
|
|
if (attribsSpec)
|
|
|
|
*attribsSpec++ = 0;
|
|
|
|
|
|
|
|
/* An original section name must be specified */
|
|
|
|
if (!*sectionName)
|
|
|
|
{
|
|
|
|
error("Invalid section attributes specification.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* If a new section name begins, it must be not empty */
|
|
|
|
if (newSectionName && !*newSectionName)
|
|
|
|
{
|
|
|
|
error("Invalid section attributes specification.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The alignment specification is inside the attributes specification */
|
|
|
|
alignSpec = NULL;
|
|
|
|
if (attribsSpec)
|
|
|
|
{
|
|
|
|
/* Check for the first occurrence of the 'A' separator, case-insensitive */
|
|
|
|
for (ptr = attribsSpec; *ptr; ++ptr)
|
|
|
|
{
|
|
|
|
if (toupper(*ptr) == 'A')
|
|
|
|
{
|
|
|
|
alignSpec = ptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (alignSpec)
|
|
|
|
*alignSpec++ = 0;
|
|
|
|
|
|
|
|
/* But it's not supported at the moment! */
|
|
|
|
if (alignSpec && *alignSpec)
|
|
|
|
{
|
|
|
|
fprintf(stdout, "%s WARNING: '%s': %s", g_ApplicationName, g_Target,
|
|
|
|
"Section alignment specification not currently supported! Ignoring.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the attributes specification */
|
|
|
|
dwAttribs[0] = dwAttribs[1] = 0;
|
|
|
|
if (attribsSpec && *attribsSpec)
|
|
|
|
{
|
|
|
|
for (i = 0, ptr = attribsSpec; *ptr; ++ptr)
|
|
|
|
{
|
|
|
|
if (*ptr == '!')
|
|
|
|
{
|
|
|
|
/* The next attribute needs to be removed.
|
|
|
|
* Any successive '!' gets collapsed. */
|
|
|
|
i = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (toupper(*ptr))
|
|
|
|
{
|
|
|
|
case 'C':
|
|
|
|
dwAttribs[i%2] |= IMAGE_SCN_CNT_CODE;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
dwAttribs[i%2] |= IMAGE_SCN_MEM_DISCARDABLE;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
dwAttribs[i%2] |= IMAGE_SCN_MEM_EXECUTE;
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
dwAttribs[i%2] |= IMAGE_SCN_CNT_INITIALIZED_DATA;
|
|
|
|
break;
|
|
|
|
case 'K': /* Remove the not-cached attribute */
|
|
|
|
dwAttribs[(i+1)%2] |= IMAGE_SCN_MEM_NOT_CACHED;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
dwAttribs[i%2] |= IMAGE_SCN_LNK_REMOVE;
|
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
dwAttribs[i%2] |= IMAGE_SCN_LNK_INFO;
|
|
|
|
break;
|
|
|
|
case 'P': /* Remove the not-paged attribute */
|
|
|
|
dwAttribs[(i+1)%2] |= IMAGE_SCN_MEM_NOT_PAGED;
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
dwAttribs[i%2] |= IMAGE_SCN_MEM_READ;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
dwAttribs[i%2] |= IMAGE_SCN_MEM_SHARED;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
dwAttribs[i%2] |= IMAGE_SCN_CNT_UNINITIALIZED_DATA;
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
dwAttribs[i%2] |= IMAGE_SCN_MEM_WRITE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error("Invalid section attributes specification.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Got an attribute; reset the state */
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
/* If the state was not reset, the attributes specification is invalid */
|
|
|
|
if (i != 0)
|
|
|
|
{
|
|
|
|
error("Invalid section attributes specification.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find all sections with the given name, rename them and change their attributes */
|
|
|
|
Section = NULL;
|
|
|
|
SectionTable = IMAGE_FIRST_SECTION(nt_header);
|
|
|
|
for (i = 0; i < nt_header->FileHeader.NumberOfSections; ++i)
|
|
|
|
{
|
|
|
|
if (strncmp((char*)SectionTable[i].Name, sectionName, ARRAY_SIZE(SectionTable[i].Name)) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Section = &SectionTable[i];
|
|
|
|
|
|
|
|
if (newSectionName && *newSectionName)
|
|
|
|
{
|
|
|
|
memset(Section->Name, 0, sizeof(Section->Name));
|
|
|
|
strncpy((char*)Section->Name, newSectionName, ARRAY_SIZE(Section->Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First filter attributes out, then add the new ones.
|
|
|
|
* The new attributes override any removed ones. */
|
|
|
|
Section->Characteristics &= ~dwAttribs[1];
|
|
|
|
Section->Characteristics |= dwAttribs[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no section was found, return an error */
|
|
|
|
if (!Section)
|
|
|
|
{
|
|
|
|
error("Section '%s' does not exist.\n", sectionName);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-24 09:14:40 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
print_usage(void)
|
|
|
|
{
|
2021-04-11 00:30:15 +00:00
|
|
|
printf("Usage: %s <options> <filename>\n\n", g_ApplicationName);
|
|
|
|
printf("<options> can be one of the following options:\n"
|
|
|
|
" --loadconfig Fix the LOAD_CONFIG directory entry;\n"
|
2021-04-08 14:50:51 +00:00
|
|
|
" --kernelmodedriver Fix code, data and resource sections for driver images;\n"
|
|
|
|
" --wdmdriver Fix code, data and resource sections for WDM drivers;\n"
|
|
|
|
" --kerneldll Fix code, data and resource sections for Kernel-Mode DLLs;\n"
|
|
|
|
" --kernel Fix code, data and resource sections for kernels;\n"
|
2021-04-11 00:30:15 +00:00
|
|
|
"\n"
|
|
|
|
"and/or a combination of the following ones:\n"
|
|
|
|
" --section:name[=newname][,[[!]{CDEIKOMPRSUW}][A{1248PTSX}]]\n"
|
|
|
|
" Overrides the attributes of a section, optionally\n"
|
|
|
|
" changing its name and its alignment.\n");
|
2021-02-24 09:14:40 +00:00
|
|
|
}
|
|
|
|
|
2005-08-07 07:32:36 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2021-04-10 18:21:43 +00:00
|
|
|
int result = 1;
|
2021-04-11 00:30:15 +00:00
|
|
|
enum fixup_mode mode = MODE_NONE;
|
|
|
|
int i;
|
2020-04-13 10:20:47 +00:00
|
|
|
FILE* file;
|
2021-04-10 18:21:43 +00:00
|
|
|
size_t len;
|
2020-04-13 10:20:47 +00:00
|
|
|
unsigned char *buffer;
|
|
|
|
PIMAGE_DOS_HEADER dos_header;
|
2021-04-10 18:21:43 +00:00
|
|
|
PIMAGE_NT_HEADERS nt_header;
|
2020-04-13 10:20:47 +00:00
|
|
|
|
2020-04-18 13:18:38 +00:00
|
|
|
g_ApplicationName = argv[0];
|
|
|
|
|
2021-04-11 00:30:15 +00:00
|
|
|
/* Check for options */
|
|
|
|
for (i = 1; i < argc; ++i)
|
2020-04-13 10:20:47 +00:00
|
|
|
{
|
2021-04-11 00:30:15 +00:00
|
|
|
if (!(argv[i][0] == '-' && argv[i][1] == '-'))
|
|
|
|
{
|
|
|
|
/* We are out of options (they come first before
|
|
|
|
* anything else, and cannot come after). */
|
|
|
|
break;
|
|
|
|
}
|
2020-04-13 10:20:47 +00:00
|
|
|
|
2021-04-11 00:30:15 +00:00
|
|
|
if (strcmp(&argv[i][2], "loadconfig") == 0)
|
|
|
|
{
|
|
|
|
if (mode != MODE_NONE)
|
|
|
|
goto mode_error;
|
|
|
|
mode = MODE_LOADCONFIG;
|
|
|
|
}
|
|
|
|
else if (strcmp(&argv[i][2], "kernelmodedriver") == 0)
|
|
|
|
{
|
|
|
|
if (mode != MODE_NONE)
|
|
|
|
goto mode_error;
|
|
|
|
mode = MODE_KERNELDRIVER;
|
|
|
|
}
|
|
|
|
else if (strcmp(&argv[i][2], "wdmdriver") == 0)
|
|
|
|
{
|
|
|
|
if (mode != MODE_NONE)
|
|
|
|
goto mode_error;
|
|
|
|
mode = MODE_WDMDRIVER;
|
|
|
|
}
|
|
|
|
else if (strcmp(&argv[i][2], "kerneldll") == 0)
|
|
|
|
{
|
|
|
|
if (mode != MODE_NONE)
|
|
|
|
goto mode_error;
|
|
|
|
mode = MODE_KERNELDLL;
|
|
|
|
}
|
|
|
|
else if (strcmp(&argv[i][2], "kernel") == 0)
|
|
|
|
{
|
|
|
|
if (mode != MODE_NONE)
|
|
|
|
goto mode_error;
|
|
|
|
mode = MODE_KERNEL;
|
|
|
|
}
|
|
|
|
else if (strncmp(&argv[i][2], "section:", 8) == 0)
|
|
|
|
{
|
|
|
|
/* Section attributes override, will be handled later */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s ERROR: Unknown option: '%s'.\n", g_ApplicationName, argv[i]);
|
|
|
|
goto failure;
|
|
|
|
mode_error:
|
|
|
|
fprintf(stderr, "%s ERROR: Specific mode already set.\n", g_ApplicationName);
|
|
|
|
failure:
|
|
|
|
print_usage();
|
|
|
|
return 1;
|
|
|
|
}
|
2021-02-24 09:14:40 +00:00
|
|
|
}
|
2021-04-11 00:30:15 +00:00
|
|
|
/* Stop now if we don't have any option or file */
|
|
|
|
if ((i <= 1) || (i >= argc))
|
2021-02-24 09:14:40 +00:00
|
|
|
{
|
|
|
|
print_usage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-04-11 00:30:15 +00:00
|
|
|
g_Target = argv[i];
|
2020-04-18 13:18:38 +00:00
|
|
|
|
2021-04-11 00:30:15 +00:00
|
|
|
/* Read the whole file to memory */
|
2021-02-24 09:14:40 +00:00
|
|
|
file = fopen(g_Target, "r+b");
|
2020-04-13 10:20:47 +00:00
|
|
|
if (!file)
|
|
|
|
{
|
2020-04-18 13:18:38 +00:00
|
|
|
fprintf(stderr, "%s ERROR: Can't open '%s'.\n", g_ApplicationName, g_Target);
|
2020-04-13 10:20:47 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
len = ftell(file);
|
|
|
|
if (len < sizeof(IMAGE_DOS_HEADER))
|
|
|
|
{
|
|
|
|
fclose(file);
|
2020-04-18 13:18:38 +00:00
|
|
|
error("Image size too small to be a PE image\n");
|
2020-04-13 10:20:47 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add one byte extra for the case where the input file size is odd.
|
2021-04-11 00:30:15 +00:00
|
|
|
We rely on this in our checksum calculation. */
|
2020-04-13 10:20:47 +00:00
|
|
|
buffer = calloc(len + 1, 1);
|
|
|
|
if (buffer == NULL)
|
|
|
|
{
|
|
|
|
fclose(file);
|
2021-04-11 00:30:15 +00:00
|
|
|
error("Not enough memory available (Needed %lu bytes).\n", len + 1);
|
2020-04-13 10:20:47 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the whole input file into a buffer */
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
fread(buffer, 1, len, file);
|
|
|
|
|
2021-04-11 00:30:15 +00:00
|
|
|
/* Check the headers and save pointers to them */
|
2020-04-13 10:20:47 +00:00
|
|
|
dos_header = (PIMAGE_DOS_HEADER)buffer;
|
2021-04-10 18:21:43 +00:00
|
|
|
if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
|
2020-04-13 10:20:47 +00:00
|
|
|
{
|
2021-04-10 18:21:43 +00:00
|
|
|
error("Invalid DOS signature: %x\n", dos_header->e_magic);
|
|
|
|
goto Quit;
|
|
|
|
}
|
2020-04-13 10:20:47 +00:00
|
|
|
|
2021-04-10 18:21:43 +00:00
|
|
|
nt_header = (PIMAGE_NT_HEADERS)(buffer + dos_header->e_lfanew);
|
|
|
|
if (nt_header->Signature != IMAGE_NT_SIGNATURE)
|
|
|
|
{
|
|
|
|
error("Invalid PE signature: %x\n", nt_header->Signature);
|
|
|
|
goto Quit;
|
2020-04-13 10:20:47 +00:00
|
|
|
}
|
2021-04-10 18:21:43 +00:00
|
|
|
|
2021-04-11 00:30:15 +00:00
|
|
|
result = 0;
|
|
|
|
|
|
|
|
/* Apply mode fixups */
|
|
|
|
if (mode != MODE_NONE)
|
|
|
|
{
|
|
|
|
if (mode == MODE_LOADCONFIG)
|
|
|
|
result = add_loadconfig(buffer, nt_header);
|
|
|
|
else
|
|
|
|
result = driver_fixup(mode, buffer, nt_header);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Apply any section attributes override */
|
|
|
|
for (i = 1; (i < argc) && (result == 0); ++i)
|
|
|
|
{
|
|
|
|
/* Ignore anything but the section specifications */
|
|
|
|
if (!(argv[i][0] == '-' && argv[i][1] == '-'))
|
|
|
|
break;
|
|
|
|
if (strncmp(&argv[i][2], "section:", 8) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
result = change_section_attribs(&argv[i][10], buffer, nt_header);
|
|
|
|
}
|
2021-04-10 18:21:43 +00:00
|
|
|
|
|
|
|
if (!result)
|
2020-04-13 10:20:47 +00:00
|
|
|
{
|
2021-04-10 18:21:43 +00:00
|
|
|
/* Success. Recalculate the checksum only if this is not a reproducible build file */
|
|
|
|
if (nt_header->OptionalHeader.CheckSum != 0)
|
|
|
|
fix_checksum(buffer, len, nt_header);
|
|
|
|
|
|
|
|
/* We could optimize by only writing the changed parts, but keep it simple for now */
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
fwrite(buffer, 1, len, file);
|
2020-04-13 10:20:47 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 18:21:43 +00:00
|
|
|
Quit:
|
2020-04-13 10:20:47 +00:00
|
|
|
free(buffer);
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
return result;
|
2005-08-07 07:32:36 +00:00
|
|
|
}
|