78 lines
2.3 KiB
C++
78 lines
2.3 KiB
C++
#include <windows.h>
|
|
#include <coffi/coffi.hpp>
|
|
#include <CLI11.hpp>
|
|
#include <spdlog/spdlog.h>
|
|
|
|
int main(int argc, char *argv[]) {
|
|
CLI::App app("Patcher");
|
|
std::string inputFile;
|
|
std::string outputFile;
|
|
app.add_option("-i,--input", inputFile, "Input exe file to patch")->required();
|
|
app.add_option("-o,--output", outputFile, "Output patched exe file")->required();
|
|
|
|
CLI11_PARSE(app, argc, argv);
|
|
|
|
// Load the object file containing the main function
|
|
COFFI::coffi objReader;
|
|
std::string objPath = SRC_OBJECT;
|
|
SPDLOG_INFO("Loading object file: {}", objPath);
|
|
if (!objReader.load(objPath)) {
|
|
spdlog::error("Failed to load object file: {}", objPath);
|
|
return 1;
|
|
}
|
|
|
|
// Load the source PE file
|
|
COFFI::coffi peReader;
|
|
SPDLOG_INFO("Loading PE file: {}", inputFile);
|
|
if (!peReader.load(inputFile)) {
|
|
spdlog::error("Failed to load PE file: {}", inputFile);
|
|
return 1;
|
|
}
|
|
|
|
// Find the 'main' function in the object file
|
|
auto& symbols = *objReader.get_symbols();
|
|
COFFI::symbol* mainSymbol = nullptr;
|
|
for (auto &sym : symbols) {
|
|
SPDLOG_INFO("Symbol: {}", sym.get_name());
|
|
if (sym.get_name() == "_ref") {
|
|
mainSymbol = &sym;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!mainSymbol) {
|
|
spdlog::error("Could not find 'main' symbol in object file");
|
|
return 1;
|
|
}
|
|
|
|
// Get the section containing the main function
|
|
auto& sections = objReader.get_sections();
|
|
auto mainSection = sections[mainSymbol->get_section_number() - 1];
|
|
|
|
// Calculate main function size and get its code
|
|
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();
|
|
|
|
SPDLOG_INFO("Found main function at offset {} with estimated size {}", mainOffset, mainSize);
|
|
|
|
// Find .text section in PE file
|
|
auto& peSections = peReader.get_sections();
|
|
COFFI::section* textSection = nullptr;
|
|
for (auto& section : peSections) {
|
|
if (section->get_name() == ".text") {
|
|
textSection = section;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!textSection) {
|
|
spdlog::error("Could not find .text section in PE file");
|
|
return 1;
|
|
}
|
|
|
|
uint32_t textSectionEnd = textSection->get_virtual_address() + textSection->get_virtual_size();
|
|
spdlog::info("Found .text section, end at virtual address: 0x{:x}", textSectionEnd);
|
|
|
|
return 0;
|
|
} |