Compare commits

...
Sign in to create a new pull request.

3 commits

6 changed files with 99 additions and 82 deletions

View file

@ -72,7 +72,7 @@ execute_process(
if(${VERSION_STRING} MATCHES "^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9]+-g[0-9a-f]+(-dirty)?)?$")
add_compile_definitions(SYNTHXEX_VERSION="${VERSION_STRING}")
else()
add_compile_definitions(SYNTHXEX_VERSION="v0.0.4") # Only used as a fallback
add_compile_definitions(SYNTHXEX_VERSION="v0.0.5") # Only used as a fallback
endif()
# Setting install target settings...

View file

@ -164,13 +164,13 @@ int main(int argc, char **argv)
{
static struct option longOptions[] =
{
{ "help", no_argument, 0, 0 },
{ "version", no_argument, 0, 0 },
{ "libs", no_argument, 0, 0 },
{ "skip-machine-check", no_argument, 0, 0 },
{ "input", required_argument, 0, 0 },
{ "output", required_argument, 0, 0 },
{ "type", required_argument, 0, 0 },
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'v' },
{ "libs", no_argument, 0, 'l' },
{ "skip-machine-check", no_argument, 0, 's' },
{ "input", required_argument, 0, 'i' },
{ "output", required_argument, 0, 'o' },
{ "type", required_argument, 0, 't' },
{ 0, 0, 0, 0 }
};
@ -202,34 +202,26 @@ int main(int argc, char **argv)
while((option = getopt_long(argc, argv, "hvlsi:o:t:", longOptions, &optIndex)) != -1)
{
if(option == 'h' || option == '?' || (option == 0 && strcmp(longOptions[optIndex].name, "help") == 0))
{
dispHelp(argv);
freeAllMainStructs(&offsets, &xexHeader, &secInfoHeader, &peData,
&optHeaderEntries, &optHeaders);
return SUCCESS;
}
else if(option == 'v' || (option == 0 && strcmp(longOptions[optIndex].name, "version") == 0))
switch(option)
{
case 'v':
dispVer();
freeAllMainStructs(&offsets, &xexHeader, &secInfoHeader, &peData,
&optHeaderEntries, &optHeaders);
return SUCCESS;
}
else if(option == 'l' || (option == 0 && strcmp(longOptions[optIndex].name, "libs") == 0))
{
case 'l':
dispLibs();
freeAllMainStructs(&offsets, &xexHeader, &secInfoHeader, &peData,
&optHeaderEntries, &optHeaders);
return SUCCESS;
}
else if(option == 's' || (option == 0 && strcmp(longOptions[optIndex].name, "skip-machine-check") == 0))
{
case 's':
printf("%s WARNING: Skipping machine ID check.\n", SYNTHXEX_PRINT_STEM);
skipMachineCheck = true;
}
else if(option == 'i' || (option == 0 && strcmp(longOptions[optIndex].name, "input") == 0))
{
break;
case 'i':
gotInput = true;
pePath = malloc(strlen(optarg) + 1);
@ -243,9 +235,9 @@ int main(int argc, char **argv)
}
strcpy(pePath, optarg);
}
else if(option == 'o' || (option == 0 && strcmp(longOptions[optIndex].name, "output") == 0))
{
break;
case 'o':
gotOutput = true;
xexfilePath = malloc(strlen(optarg) + 1);
@ -259,9 +251,9 @@ int main(int argc, char **argv)
}
strcpy(xexfilePath, optarg);
}
else if(option == 't' || (option == 0 && strcmp(longOptions[optIndex].name, "type") == 0))
{
break;
case 't':
if(strcmp(optarg, "title") == 0)
{ xexHeader->moduleFlags = XEX_MOD_FLAG_TITLE; }
else if(strcmp(optarg, "titledll") == 0)
@ -281,6 +273,15 @@ int main(int argc, char **argv)
&optHeaderEntries, &optHeaders);
return -1;
}
break;
case 'h':
default:
dispHelp(argv);
freeAllMainStructs(&offsets, &xexHeader, &secInfoHeader, &peData,
&optHeaderEntries, &optHeaders);
return SUCCESS;
}
}

View file

@ -40,6 +40,7 @@ int xenonifyIAT(FILE *basefile, struct peData *peData)
// Loop through each import and handle it's IAT entry
for(uint32_t j = 0; j < peData->peImportInfo.tables[i].importCount; j++)
{
// Read in the current IAT entry
uint32_t iatEntry = get32BitFromPE(basefile);
if(errno != SUCCESS)
@ -60,6 +61,10 @@ int xenonifyIAT(FILE *basefile, struct peData *peData)
if(fwrite(&iatEntry, sizeof(uint32_t), 1, basefile) < 1)
{ return ERR_FILE_WRITE; }
// Call file positioning function between reads and writes to the same file.
// This is mandated by the C standard.
fseek(basefile, 0, SEEK_CUR);
}
}

View file

@ -39,12 +39,15 @@ int setPageDescriptors(FILE *pe, struct peData *peData, struct secInfoHeader *se
if(!secInfoHeader->descriptors)
{ return ERR_OUT_OF_MEM; }
struct pageDescriptor *descriptors = secInfoHeader->descriptors;
struct pageDescriptor *descriptors = secInfoHeader->descriptors; // So we don't dereference an unaligned pointer
// Setting size/info data and calculating hashes for page descriptors
for(int64_t i = secInfoHeader->pageDescCount - 1; i >= 0; i--)
{
// Get page type (rwx)
descriptors[i].sizeAndInfo = getRwx(secInfoHeader, peData, i);
// Init sha1 hash
struct sha1_ctx shaContext;
sha1_init(&shaContext);
@ -62,6 +65,7 @@ int setPageDescriptors(FILE *pe, struct peData *peData, struct secInfoHeader *se
return ERR_FILE_READ;
}
// For little endian systems, swap into big endian for hashing, then back (to keep struct endianness consistent)
#ifdef LITTLE_ENDIAN_SYSTEM
descriptors[i].sizeAndInfo = __builtin_bswap32(descriptors[i].sizeAndInfo);
#endif

View file

@ -22,6 +22,7 @@
// to determine the hash, but reading the file we just created is easier.
int setHeaderSha1(FILE *xex)
{
// Get basefile offset
if(fseek(xex, 0x8, SEEK_SET) != 0)
{ return ERR_FILE_READ; }
@ -30,6 +31,7 @@ int setHeaderSha1(FILE *xex)
if(errno != SUCCESS)
{ return errno; }
// Get secinfo offset
if(fseek(xex, 0x10, SEEK_SET) != 0)
{ return ERR_FILE_READ; }
@ -38,13 +40,15 @@ int setHeaderSha1(FILE *xex)
if(errno != SUCCESS)
{ return errno; }
uint32_t endOfImageInfo = secInfoOffset + 0x8 + 0x174;
uint32_t remainingSize = basefileOffset - endOfImageInfo;
uint32_t endOfImageInfo = secInfoOffset + 0x8 + 0x174; // 0x8 == image info offset in security info, 0x174 == length of that
uint32_t remainingSize = basefileOffset - endOfImageInfo; // How much data is between end of image info and basefile (we hash that too)
// Init sha1 hash
struct sha1_ctx shaContext;
memset(&shaContext, 0, sizeof(shaContext));
sha1_init(&shaContext);
// Hash first part (remainder of headers is done first, then the start)
uint8_t *remainderOfHeaders = malloc(remainingSize);
if(!remainderOfHeaders)
@ -67,6 +71,7 @@ int setHeaderSha1(FILE *xex)
sha1_update(&shaContext, remainingSize, remainderOfHeaders);
nullAndFree((void **)&remainderOfHeaders);
// Hash from start up to image info (0x8 into security header)
uint32_t headersLen = secInfoOffset + 0x8;
uint8_t *headersStart = malloc(headersLen);
@ -90,11 +95,13 @@ int setHeaderSha1(FILE *xex)
sha1_update(&shaContext, headersLen, headersStart);
nullAndFree((void **)&headersStart);
// Get final hash
uint8_t headerHash[20];
memset(headerHash, 0, sizeof(headerHash));
sha1_digest(&shaContext, 20, headerHash);
if(fseek(xex, secInfoOffset + 0x164, SEEK_SET) != 0)
// Finally, write it out
if(fseek(xex, secInfoOffset + 0x164, SEEK_SET) != 0) // 0x164 == offset in secinfo of header hash
{ return ERR_FILE_READ; }
if(fwrite(headerHash, 1, 20, xex) != 20)

View file

@ -31,7 +31,7 @@
;;; Hardcoded fallback version in case we can't rely on git
(define synthxex-fallback-version "v0.0.4")
(define synthxex-fallback-version "v0.0.5")
;;; Determine the version of SynthXEX we are building