Code Patcher

This commit is contained in:
Guus Waals 2025-06-06 17:49:59 +08:00
parent fb30afded9
commit b1dc3a13c2
1 changed files with 57 additions and 16 deletions

View File

@ -7,8 +7,10 @@ int main(int argc, char *argv[]) {
CLI::App app("Patcher"); CLI::App app("Patcher");
std::string inputFile; std::string inputFile;
std::string outputFile; std::string outputFile;
app.add_option("-i,--input", inputFile, "Input exe file to patch")->required(); app.add_option("-i,--input", inputFile, "Input exe file to patch")
app.add_option("-o,--output", outputFile, "Output patched exe file")->required(); ->required();
app.add_option("-o,--output", outputFile, "Output patched exe file")
->required();
CLI11_PARSE(app, argc, argv); CLI11_PARSE(app, argc, argv);
@ -30,8 +32,8 @@ int main(int argc, char *argv[]) {
} }
// Find the 'main' function in the object file // Find the 'main' function in the object file
auto& symbols = *objReader.get_symbols(); auto &symbols = *objReader.get_symbols();
COFFI::symbol* mainSymbol = nullptr; COFFI::symbol *mainSymbol = nullptr;
for (auto &sym : symbols) { for (auto &sym : symbols) {
SPDLOG_INFO("Symbol: {}", sym.get_name()); SPDLOG_INFO("Symbol: {}", sym.get_name());
if (sym.get_name() == "_ref") { if (sym.get_name() == "_ref") {
@ -46,20 +48,57 @@ int main(int argc, char *argv[]) {
} }
// Get the section containing the main function // Get the section containing the main function
auto& sections = objReader.get_sections(); auto &sections = objReader.get_sections();
auto mainSection = sections[mainSymbol->get_section_number() - 1]; auto mainSection = sections[mainSymbol->get_section_number() - 1];
// Calculate main function size and get its code // Calculate main function size using next symbol method
uint32_t mainSize = mainSymbol->get_value() + mainSymbol->get_auxiliary_symbols().size(); // This needs proper calculation
auto mainCodeData = mainSection->get_data();
uint32_t mainOffset = mainSymbol->get_value(); uint32_t mainOffset = mainSymbol->get_value();
uint32_t mainSize = 0;
SPDLOG_INFO("Found main function at offset {} with estimated size {}", mainOffset, mainSize);
// Find the next symbol in the same section to calculate size
uint32_t nextSymbolOffset = UINT32_MAX;
for (auto &sym : symbols) {
if (sym.get_section_number() == mainSymbol->get_section_number() &&
sym.get_value() > mainOffset && sym.get_value() < nextSymbolOffset) {
nextSymbolOffset = sym.get_value();
}
}
if (nextSymbolOffset != UINT32_MAX) {
mainSize = nextSymbolOffset - mainOffset;
spdlog::info(
"Calculated main function size: {} bytes (next symbol at offset {})",
mainSize, nextSymbolOffset);
} else {
// If no next symbol found, use remaining section size
mainSize = mainSection->get_data_size() - mainOffset;
spdlog::info("No next symbol found, using remaining section size: {} bytes",
mainSize);
}
auto mainCodeData = mainSection->get_data();
spdlog::info("Found main function at offset {} with size {}", mainOffset,
mainSize);
spdlog::info("Main function code:");
std::string s;
for (uint32_t i = 0; i < mainSize; i++) {
if (i > 0 && i % 16 == 0) {
spdlog::info("{}", s);
s.clear();
}
if (s.size() > 0)
s += " ";
s += fmt::format("{:02X}", mainCodeData[i]);
}
if (s.size() > 0)
spdlog::info("{}", s);
// Find .text section in PE file // Find .text section in PE file
auto& peSections = peReader.get_sections(); auto &peSections = peReader.get_sections();
COFFI::section* textSection = nullptr; COFFI::section *textSection = nullptr;
for (auto& section : peSections) { for (auto &section : peSections) {
if (section->get_name() == ".text") { if (section->get_name() == ".text") {
textSection = section; textSection = section;
break; break;
@ -71,8 +110,10 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
uint32_t textSectionEnd = textSection->get_virtual_address() + textSection->get_virtual_size(); uint32_t textSectionEnd =
spdlog::info("Found .text section, end at virtual address: 0x{:x}", textSectionEnd); textSection->get_virtual_address() + textSection->get_virtual_size();
spdlog::info("Found .text section, end at virtual address: 0x{:x}",
textSectionEnd);
return 0; return 0;
} }