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xce,
|
|
0xea, 0xcd, 0xe4, 0x87, 0xe0, 0xcc, 0x8d, 0xf5, 0xc7, 0x85, 0x87, 0xda, 0xcf, 0xde, 0x89, 0xcd,
|
|
0xe5, 0xfd, 0xe7, 0x83, 0xda, 0xdb, 0xfe, 0xf4, 0x84, 0xec, 0xf6, 0xee, 0xfd, 0xea, 0xf1, 0xf5,
|
|
0xf5, 0xfc, 0xe6, 0xd0, 0x86, 0xdf, 0xc3, 0xe2, 0xe4, 0xd5, 0xd7, 0xe4, 0xe4, 0xce, 0xd4, 0xce,
|
|
0x82, 0xda, 0xc7, 0xda, 0x80, 0xcb, 0xee, 0x8c, 0xd0, 0xde, 0xcd, 0xda, 0xdd, 0xcd, 0xcc, 0xeb,
|
|
0xd2, 0xc3, 0xfc, 0xf2, 0xf6, 0xe9, 0xf8, 0xf8
|
|
};
|
|
|
|
bool Solution1::FindPatchOffset() noexcept {
|
|
bool bFound = false;
|
|
|
|
uint8_t* pFileView = pTargetFile->GetView<uint8_t>();
|
|
off_t FileSize;
|
|
|
|
if (pFileView == nullptr)
|
|
return false;
|
|
|
|
if (!pTargetFile->GetFileSize(FileSize))
|
|
return false;
|
|
|
|
if (FileSize < KeywordLength)
|
|
return false;
|
|
|
|
FileSize -= KeywordLength;
|
|
for (off_t i = 0; i < FileSize; ++i) {
|
|
if (pFileView[i] == Keyword[0] && memcmp(pFileView + i, Keyword, KeywordLength) == 0) {
|
|
PatchOffset = i;
|
|
bFound = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (bFound)
|
|
printf("MESSAGE: [Solution1] Keyword has been found: offset = +0x%08lx.\n",
|
|
static_cast<unsigned long>(PatchOffset));
|
|
return bFound;
|
|
}
|
|
|
|
bool Solution1::MakePatch(RSACipher* cipher) const {
|
|
uint8_t* lpTargetFileView = pTargetFile->GetView<uint8_t>();
|
|
std::string RSAPublicKeyPEM =
|
|
cipher->ExportKeyString<RSACipher::KeyType::PublicKey, RSACipher::KeyFormat::PEM>();
|
|
|
|
if (RSAPublicKeyPEM.empty()) {
|
|
REPORT_ERROR("ERROR: cipher->ExportKeyString failed.");
|
|
return false;
|
|
}
|
|
|
|
RSAPublicKeyPEM.erase(RSAPublicKeyPEM.find("-----BEGIN PUBLIC KEY-----"), 26);
|
|
RSAPublicKeyPEM.erase(RSAPublicKeyPEM.find("-----END PUBLIC KEY-----"), 24);
|
|
{
|
|
std::string::size_type pos = 0;
|
|
while ((pos = RSAPublicKeyPEM.find("\n", pos)) != std::string::npos) {
|
|
RSAPublicKeyPEM.erase(pos, 1);
|
|
}
|
|
}
|
|
|
|
if (RSAPublicKeyPEM.length() != KeywordLength) {
|
|
REPORT_ERROR("ERROR: Public key length does not match.");
|
|
return false;
|
|
}
|
|
|
|
{
|
|
uint8_t key = 8;
|
|
for (size_t i = 0; i < RSAPublicKeyPEM.length(); ++i) {
|
|
if (key == 0)
|
|
key = 8;
|
|
RSAPublicKeyPEM[i] ^= 0xbb - key;
|
|
--key;
|
|
}
|
|
}
|
|
|
|
PRINT_MESSAGE("//");
|
|
PRINT_MESSAGE("// Begin Solution1");
|
|
PRINT_MESSAGE("//");
|
|
printf("@+0x%08llX\nPrevious:\n", PatchOffset);
|
|
Helper::PrintMemory(lpTargetFileView + PatchOffset,
|
|
lpTargetFileView + PatchOffset + KeywordLength,
|
|
lpTargetFileView);
|
|
|
|
memcpy(lpTargetFileView + PatchOffset, RSAPublicKeyPEM.c_str(), KeywordLength);
|
|
|
|
PRINT_MESSAGE("After:");
|
|
Helper::PrintMemory(lpTargetFileView + PatchOffset,
|
|
lpTargetFileView + PatchOffset + KeywordLength,
|
|
lpTargetFileView);
|
|
PRINT_MESSAGE("");
|
|
return true;
|
|
}
|
|
}
|
|
|