Skip to content

Instantly share code, notes, and snippets.

@amir9480
Created January 9, 2020 13:03
Show Gist options
  • Save amir9480/6b1250ee68146bb342010bb7b0fab82d to your computer and use it in GitHub Desktop.
Save amir9480/6b1250ee68146bb342010bb7b0fab82d to your computer and use it in GitHub Desktop.
Change executable file constants in C++.
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <direct.h>
const char* test[] = { "UNIQUE_START_TOKEN", "WELCOME\0------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", "UNIQUE_END_TOKEN" };
int main(int argc, const char** argv)
{
// Executable directory
std::string executablePath(300, '\0');
// Executable full path
std::string exceutableFullPath(300, '\0');
_getcwd((char*)executablePath.c_str(), 300);
GetModuleFileNameA(NULL, (char*)exceutableFullPath.c_str(), 300);
executablePath = executablePath.c_str();
exceutableFullPath = exceutableFullPath.c_str();
std::cout << "1. Show constant\n2. Change constant\n";
int choise = 0;
std::cin >> choise;
if (choise == 2)
{
std::string newConstant;
std::cout << "New Constant value:";
std::cin.get();
std::getline(std::cin, newConstant);
if (newConstant.size() > 255)
{
std::cout << "ERROR: Maximum allowed size for string is :" << 255 << std::endl;
}
std::ifstream fileRead(argv[0], std::ios::binary);
fileRead.seekg(0, std::ios::end);
unsigned int size = fileRead.tellg();
std::string fileContent(size + newConstant.size(), ' ');
fileRead.seekg(0);
fileRead.read(&fileContent[0], size);
fileRead.close();
int startPos = -1;
int endPos = -1;
for (int i = 0; i < size; i++)
{
// Check for start token exists
if (fileContent[i] == test[0][0])
{
for (int j = 1; j < strlen(test[0]) && i + j < size; j++)
{
if (fileContent[i + j] != test[0][j])
{
break;
}
// start token matched
if (j == strlen(test[0]) - 1)
{
j++;
while (fileContent[i + j] == 0)
{
j++;
}
startPos = i + j;
break;
}
}
}
// Check for end token exists
if (fileContent[i] == test[2][0])
{
for (int j = 1; j < strlen(test[2]) && i + j < size; j++)
{
if (fileContent[i + j] != test[2][j])
{
break;
}
// end token matched
if (j == strlen(test[2]) - 1)
{
endPos = i - 1;
break;
}
}
}
}
// Did not find tokens so stop modify executable
if (startPos == -1 || endPos == -1)
{
std::cout << "ERROR: TOKEN NOT FOUND!\n";
return -1;
}
// Put new constant value in file content
for (int i = 0; i < newConstant.length(); i++)
{
fileContent[startPos + i] = newConstant[i];
}
fileContent[startPos + newConstant.size()] = '\0';
// write replaced content in temp file
std::ofstream fileWrite("temp.exe", std::ios::binary);
fileWrite.write(&fileContent[0], size);
fileWrite.close();
// rename current running file and rename temp file to original running file name
std::string copyCommand = "del \"" + executablePath + "\\old.exe\" >nul 2>&1 & rename \"" + exceutableFullPath + "\" old.exe & rename \"" + executablePath + "\\temp.exe\" \"" + exceutableFullPath.substr(exceutableFullPath.find_last_of("/\\") + 1) + "\"";
if (system(copyCommand.c_str()) == 0)
{
std::cout << "\nREPLACED SUCCESSFULLY\n";
}
}
else
{
std::cout << test[1] << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment