|
8 | 8 | #include <miniupnpc.h> |
9 | 9 | #include <upnpcommands.h> |
10 | 10 | #include <WinSock2.h> |
| 11 | +#include <fstream> |
| 12 | +#include <wincrypt.h> |
| 13 | + |
| 14 | +class ProtectLoginData : public Hook |
| 15 | +{ |
| 16 | + // C2C saves plaintext online login details into SaveGame/Common.dat by default |
| 17 | + // It's not obvious this file contains login info though, so some might share it out freely |
| 18 | + // Now that online is restored those details could be extracted and reused |
| 19 | + // |
| 20 | + // Instead of keeping it inside SaveGame/Common.dat, we'll store it in a seperate file next to game EXE |
| 21 | + // and zero-out the data inside Common.dat before it gets saved to disk |
| 22 | + // |
| 23 | + // We'll also encrypt the data using CryptProtectData, which encrypts it against the users Windows account |
| 24 | + // So even if the file does get shared, it'd be difficult for others to be able to decrypt it |
| 25 | + const static inline std::string LoginDataFilename = "OnlineLoginData.dat"; |
| 26 | + |
| 27 | + static void EncryptDataToFile(const uint8_t* inputData, int dataLength, const std::filesystem::path& outputFilePath) |
| 28 | + { |
| 29 | + DATA_BLOB inputBlob = { static_cast<DWORD>(dataLength), const_cast<BYTE*>(inputData) }; |
| 30 | + DATA_BLOB outputBlob; |
| 31 | + |
| 32 | + if (!CryptProtectData(&inputBlob, L"OnlineLoginData", nullptr, nullptr, nullptr, 0, &outputBlob)) |
| 33 | + throw std::runtime_error("CryptProtectData = false"); |
| 34 | + else |
| 35 | + { |
| 36 | + std::ofstream outputFile(outputFilePath, std::ios::binary); |
| 37 | + if (outputFile.is_open()) |
| 38 | + { |
| 39 | + outputFile.write((char*)outputBlob.pbData, outputBlob.cbData); |
| 40 | + outputFile.close(); |
| 41 | + } |
| 42 | + LocalFree(outputBlob.pbData); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + static bool DecryptDataFromFile(uint8_t* outputData, int dataLength, const std::filesystem::path& inputFilePath) |
| 47 | + { |
| 48 | + if (!std::filesystem::exists(inputFilePath)) |
| 49 | + return false; |
| 50 | + |
| 51 | + std::ifstream inputFile(inputFilePath, std::ios::binary | std::ios::ate); |
| 52 | + if (inputFile.is_open()) |
| 53 | + { |
| 54 | + std::streamsize size = inputFile.tellg(); |
| 55 | + inputFile.seekg(0, std::ios::beg); |
| 56 | + std::vector<char> encryptedData(size); |
| 57 | + if (inputFile.read(encryptedData.data(), size)) |
| 58 | + { |
| 59 | + DATA_BLOB inputBlob = { static_cast<DWORD>(size), reinterpret_cast<BYTE*>(encryptedData.data()) }; |
| 60 | + DATA_BLOB outputBlob; |
| 61 | + |
| 62 | + if (!CryptUnprotectData(&inputBlob, nullptr, nullptr, nullptr, nullptr, 0, &outputBlob)) |
| 63 | + throw std::runtime_error("CryptUnprotectData = false"); |
| 64 | + else |
| 65 | + { |
| 66 | + std::memcpy(outputData, outputBlob.pbData, min(int(outputBlob.cbData), dataLength)); |
| 67 | + LocalFree(outputBlob.pbData); |
| 68 | + return true; |
| 69 | + } |
| 70 | + } |
| 71 | + inputFile.close(); |
| 72 | + } |
| 73 | + |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + inline static SafetyHookInline Sumo_CommonDat_Read_hook = {}; |
| 78 | + static int Sumo_CommonDat_Read_dest() |
| 79 | + { |
| 80 | + auto ret = Sumo_CommonDat_Read_hook.call<int>(); |
| 81 | + |
| 82 | + uint8_t* loginData = Module::exe_ptr<uint8_t>(0x3C205C); |
| 83 | + int loginDataLength = (0x10 * 8) + (8 * 8); |
| 84 | + |
| 85 | + try |
| 86 | + { |
| 87 | + DecryptDataFromFile(loginData, loginDataLength, Module::ExePath.parent_path() / LoginDataFilename); |
| 88 | + } |
| 89 | + catch (const std::exception& e) |
| 90 | + { |
| 91 | + memset(loginData, 0, loginDataLength); |
| 92 | + spdlog::error("ProtectLoginData: Failed to decrypt login data from {}: {}", LoginDataFilename, e.what()); |
| 93 | + } |
| 94 | + |
| 95 | + return ret; |
| 96 | + } |
| 97 | + inline static SafetyHookInline Sumo_CommonDat_Write_hook = {}; |
| 98 | + static int Sumo_CommonDat_Write_dest() |
| 99 | + { |
| 100 | + constexpr int loginDataLength = (0x10 * 8) + (8 * 8); |
| 101 | + uint8_t* loginData = Module::exe_ptr<uint8_t>(0x3C205C); |
| 102 | + |
| 103 | + uint8_t tempLoginData[loginDataLength]; |
| 104 | + memcpy(tempLoginData, loginData, loginDataLength); |
| 105 | + |
| 106 | + try |
| 107 | + { |
| 108 | + EncryptDataToFile(loginData, loginDataLength, Module::ExePath.parent_path() / LoginDataFilename); |
| 109 | + } |
| 110 | + catch (const std::exception& e) |
| 111 | + { |
| 112 | + spdlog::error("ProtectLoginData: failed to encrypt login data to {} ({}), login details will be scrubbed from common.dat", e.what(), LoginDataFilename); |
| 113 | + } |
| 114 | + |
| 115 | + SecureZeroMemory(loginData, loginDataLength); |
| 116 | + |
| 117 | + auto ret = Sumo_CommonDat_Write_hook.call<int>(); |
| 118 | + |
| 119 | + memcpy(loginData, tempLoginData, loginDataLength); |
| 120 | + |
| 121 | + return ret; |
| 122 | + } |
| 123 | + inline static SafetyHookInline Sumo_CommonDat_WriteRaw_hook = {}; |
| 124 | + static int Sumo_CommonDat_WriteRaw_dest() |
| 125 | + { |
| 126 | + constexpr int loginDataLength = (0x10 * 8) + (8 * 8); |
| 127 | + uint8_t* loginData = Module::exe_ptr<uint8_t>(0x3C205C); |
| 128 | + |
| 129 | + uint8_t tempLoginData[loginDataLength]; |
| 130 | + memcpy(tempLoginData, loginData, loginDataLength); |
| 131 | + |
| 132 | + try |
| 133 | + { |
| 134 | + EncryptDataToFile(loginData, loginDataLength, Module::ExePath.parent_path() / LoginDataFilename); |
| 135 | + } |
| 136 | + catch (const std::exception& e) |
| 137 | + { |
| 138 | + spdlog::error("ProtectLoginData: failed to encrypt login data to {} ({}), login details will be scrubbed from common.dat", e.what(), LoginDataFilename); |
| 139 | + } |
| 140 | + |
| 141 | + SecureZeroMemory(loginData, loginDataLength); |
| 142 | + |
| 143 | + auto ret = Sumo_CommonDat_WriteRaw_hook.call<int>(); |
| 144 | + |
| 145 | + memcpy(loginData, tempLoginData, loginDataLength); |
| 146 | + |
| 147 | + return ret; |
| 148 | + } |
| 149 | + |
| 150 | +public: |
| 151 | + std::string_view description() override |
| 152 | + { |
| 153 | + return "ProtectLoginData"; |
| 154 | + } |
| 155 | + |
| 156 | + bool validate() override |
| 157 | + { |
| 158 | + return Settings::ProtectLoginData; |
| 159 | + } |
| 160 | + |
| 161 | + bool apply() override |
| 162 | + { |
| 163 | + constexpr int Sumo_CommonDat_Read_Addr = 0x16380; |
| 164 | + constexpr int Sumo_CommonDat_Write_Addr = 0x16420; |
| 165 | + constexpr int Sumo_CommonDat_WriteRaw_Addr = 0x164D0; |
| 166 | + |
| 167 | + Sumo_CommonDat_Read_hook = safetyhook::create_inline(Module::exe_ptr(Sumo_CommonDat_Read_Addr), Sumo_CommonDat_Read_dest); |
| 168 | + Sumo_CommonDat_Write_hook = safetyhook::create_inline(Module::exe_ptr(Sumo_CommonDat_Write_Addr), Sumo_CommonDat_Write_dest); |
| 169 | + Sumo_CommonDat_WriteRaw_hook = safetyhook::create_inline(Module::exe_ptr(Sumo_CommonDat_WriteRaw_Addr), Sumo_CommonDat_WriteRaw_dest); |
| 170 | + |
| 171 | + return true; |
| 172 | + } |
| 173 | + |
| 174 | + static ProtectLoginData instance; |
| 175 | +}; |
| 176 | +ProtectLoginData ProtectLoginData::instance; |
11 | 177 |
|
12 | 178 | class PlaySegaJingle : public Hook |
13 | 179 | { |
|
0 commit comments