From 4428c063d72cd99c95970d40ddb575bfffd97456 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Tue, 14 Jun 2016 19:49:27 +0200 Subject: [PATCH] Small optimization --- src/Main.cpp | 2 +- src/Utils/Cryptography.cpp | 68 ++++++++++++++++++++++++++++++++++++++ src/Utils/Cryptography.hpp | 17 ++++++++++ src/Utils/Hooking.cpp | 39 ++++++++++++++++++++++ src/Utils/Hooking.hpp | 23 +++++++++++++ src/Utils/Utils.cpp | 2 +- 6 files changed, 149 insertions(+), 2 deletions(-) diff --git a/src/Main.cpp b/src/Main.cpp index dcb88fbe..0487d138 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -8,7 +8,7 @@ namespace Main { Main::EntryPointHook.Uninstall(); - Utils::Cryptography::Rand::Initialize(); + Utils::Cryptography::Initialize(); Components::Loader::Initialize(); #if defined(DEBUG) || defined(FORCE_UNIT_TESTS) diff --git a/src/Utils/Cryptography.cpp b/src/Utils/Cryptography.cpp index 11bcc10b..d256d532 100644 --- a/src/Utils/Cryptography.cpp +++ b/src/Utils/Cryptography.cpp @@ -6,6 +6,11 @@ namespace Utils { namespace Cryptography { + void Initialize() + { + TDES::Initialize(); + Rand::Initialize(); + } #pragma region Rand @@ -117,6 +122,69 @@ namespace Utils #pragma endregion +#pragma region TDES + + void TDES::Initialize() + { + register_cipher(&des3_desc); + } + + std::string TDES::Encrypt(std::string text, std::string iv, std::string key) + { + std::string encData; + encData.resize(text.size()); + + symmetric_CBC cbc; + int des3 = find_cipher("3des"); + + cbc_start(des3, reinterpret_cast(iv.data()), reinterpret_cast(key.data()), key.size(), 0, &cbc); + cbc_encrypt(reinterpret_cast(text.data()), reinterpret_cast(const_cast(encData.data())), text.size(), &cbc); + cbc_done(&cbc); + + return encData; + } + + std::string TDES::Decrpyt(std::string data, std::string iv, std::string key) + { + std::string decData; + decData.resize(data.size()); + + symmetric_CBC cbc; + int des3 = find_cipher("3des"); + + cbc_start(des3, reinterpret_cast(iv.data()), reinterpret_cast(key.data()), key.size(), 0, &cbc); + cbc_decrypt(reinterpret_cast(data.data()), reinterpret_cast(const_cast(decData.data())), data.size(), &cbc); + cbc_done(&cbc); + + return decData; + } + +#pragma endregion + +#pragma region Tiger + + std::string Tiger::Compute(std::string data, bool hex) + { + return Tiger::Compute(reinterpret_cast(data.data()), data.size(), hex); + } + + std::string Tiger::Compute(const uint8_t* data, size_t length, bool hex) + { + uint8_t buffer[24] = { 0 }; + + hash_state state; + tiger_init(&state); + tiger_process(&state, data, length); + tiger_done(&state, buffer); + + std::string hash(reinterpret_cast(buffer), sizeof(buffer)); + if (!hex) return hash; + + return Utils::DumpHex(hash, ""); + } + +#pragma endregion + #pragma region SHA256 std::string SHA256::Compute(std::string data, bool hex) diff --git a/src/Utils/Cryptography.hpp b/src/Utils/Cryptography.hpp index df076140..fee89dab 100644 --- a/src/Utils/Cryptography.hpp +++ b/src/Utils/Cryptography.hpp @@ -2,6 +2,8 @@ namespace Utils { namespace Cryptography { + void Initialize(); + class Token { public: @@ -283,6 +285,21 @@ namespace Utils static bool VerifyMessage(Key key, std::string message, std::string signature); }; + class TDES + { + public: + static void Initialize(); + static std::string Encrypt(std::string text, std::string iv, std::string key); + static std::string Decrpyt(std::string text, std::string iv, std::string key); + }; + + class Tiger + { + public: + static std::string Compute(std::string data, bool hex = false); + static std::string Compute(const uint8_t* data, size_t length, bool hex = false); + }; + class SHA256 { public: diff --git a/src/Utils/Hooking.cpp b/src/Utils/Hooking.cpp index fde46784..73ae86a1 100644 --- a/src/Utils/Hooking.cpp +++ b/src/Utils/Hooking.cpp @@ -5,6 +5,45 @@ namespace Utils std::map Hook::Interceptor::IReturn; std::map Hook::Interceptor::ICallbacks; + void Hook::Signature::Process() + { + if (Hook::Signature::Signatures.empty()) return; + + char* start = reinterpret_cast(Hook::Signature::Start); + + unsigned int sigCount = Hook::Signature::Signatures.size(); + Hook::Signature::Container* containers = Hook::Signature::Signatures.data(); + + for (size_t i = 0; i < Hook::Signature::Length; ++i) + { + char* address = start + i; + + for (unsigned int k = 0; k < sigCount; ++k) + { + Hook::Signature::Container* container = &containers[k]; + + unsigned int j = 0; + for (j = 0; j < strlen(container->Mask); ++j) + { + if (container->Mask[j] != '?' &&container->Signature[j] != address[j]) + { + break; + } + } + + if (j == strlen(container->Mask)) + { + container->Callback(address); + } + } + } + } + + void Hook::Signature::Add(Hook::Signature::Container& container) + { + Hook::Signature::Signatures.push_back(container); + } + void Hook::Interceptor::Install(void* place, void(*stub)()) { return Hook::Interceptor::Install(reinterpret_cast(place), stub); diff --git a/src/Utils/Hooking.hpp b/src/Utils/Hooking.hpp index 84442603..3e2ae323 100644 --- a/src/Utils/Hooking.hpp +++ b/src/Utils/Hooking.hpp @@ -8,6 +8,29 @@ namespace Utils class Hook { public: + class Signature + { + public: + struct Container + { + const char* Signature; + const char* Mask; + std::function Callback; + }; + + Signature(void* start, size_t length) : Start(start), Length(length) {} + Signature(DWORD start, size_t length) : Signature(reinterpret_cast(start), length) {} + Signature() : Signature(0x400000, 0x800000) {} + + void Process(); + void Add(Container& container); + + private: + void* Start; + size_t Length; + std::vector Signatures; + }; + class Interceptor { public: diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index 7740fb3e..87368fa8 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -268,4 +268,4 @@ namespace Utils this->KeyValuePairs[KeyValues[i]] = KeyValues[i + 1]; } } -} \ No newline at end of file +}