From a8976e487bcca2fbac138421791ecf3ae2acf74a Mon Sep 17 00:00:00 2001 From: nullifiedcat Date: Sat, 1 Jul 2017 19:32:47 +0300 Subject: [PATCH] updated UTFCCP, added commands. --- src/base64.h | 15 ++++++ src/common.h | 1 + src/hooks/others.cpp | 8 ++-- src/utfccp.hpp | 100 ++++++++++++++++++++++++++++++++++++++++ src/utfccp_commands.cpp | 22 +++++++++ src/utfccp_commands.hpp | 13 ++++++ src/xb64.hpp | 51 -------------------- 7 files changed, 155 insertions(+), 55 deletions(-) create mode 100644 src/utfccp.hpp create mode 100644 src/utfccp_commands.cpp create mode 100644 src/utfccp_commands.hpp delete mode 100644 src/xb64.hpp diff --git a/src/base64.h b/src/base64.h index d0e2705e..27ad2f92 100644 --- a/src/base64.h +++ b/src/base64.h @@ -1,6 +1,21 @@ #ifndef BASE64_H #define BASE64_H +/* + * + Copyright (C) 2013 Tomas Kislan + Copyright (C) 2013 Adam Rudd + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * + * https://github.com/tkislan/base64 + */ + #include const char kBase64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" diff --git a/src/common.h b/src/common.h index fce172e8..d3e464c8 100644 --- a/src/common.h +++ b/src/common.h @@ -50,6 +50,7 @@ extern "C" { #include "hoovy.hpp" #include "enums.h" #include "projlogging.hpp" +#include "utfccp_commands.hpp" #include "velocity.hpp" #include "angles.hpp" #include "entityhitboxcache.hpp" diff --git a/src/hooks/others.cpp b/src/hooks/others.cpp index e8bca0cc..96005781 100644 --- a/src/hooks/others.cpp +++ b/src/hooks/others.cpp @@ -8,7 +8,7 @@ #include "../common.h" #include "../netmessage.h" #include "../hack.h" -#include "../xb64.hpp" +#include "../utfccp.hpp" #include "hookedmethods.h" static CatVar no_invisibility(CV_SWITCH, "no_invis", "0", "Remove Invisibility", "Useful with chams!"); @@ -151,7 +151,7 @@ bool SendNetMsg_hook(void* _this, INetMessage& msg, bool bForceReliable = false, std::string msg(str.substr(offset)); msg = msg.substr(0, msg.length() - 2); if (msg.find("!!") == 0) { - msg = "!!" + xb64::encrypt(msg.substr(2)); + msg = utfccp::encrypt(msg.substr(2)); str = str.substr(0, offset) + msg + "\"\""; crpt = true; } @@ -349,8 +349,8 @@ bool DispatchUserMessage_hook(void* _this, int type, bf_read& buf) { } if (crypt_chat) { if (message.find("!!") == 0) { - if (xb64::validate(message.substr(2))) { - PrintChat("\x07%06X%s\x01: %s", 0xe05938, name.c_str(), xb64::decrypt(message.substr(2)).c_str()); + if (utfccp::validate(message)) { + PrintChat("\x07%06X%s\x01: %s", 0xe05938, name.c_str(), utfccp::decrypt(message).c_str()); } } } diff --git a/src/utfccp.hpp b/src/utfccp.hpp new file mode 100644 index 00000000..57a77f8c --- /dev/null +++ b/src/utfccp.hpp @@ -0,0 +1,100 @@ +/* + * utfccp.hpp + * + * Created on: Jul 1, 2017 + * Author: nullifiedcat + */ + +#pragma once + +#include +#include "base64.h" + +/* + * Usage: + * To send message, call utfccp::encrypt on text of the message and send the result to chat + * When receiving a message, call utfccp::validate, and if it succeeds, call utfccp::decrypt and show the result + * + * To include this to your project, add this file and base64.h to your source and include this file. + * + * General structure of message: + * !![version][data] + * version: char from base64 alphabet + * data: depends on version + * + * Versions: + * A: + * !!A[data][checksum] + * data = base64(crappy_xorstring(input)) + * checksum = crappy_checksum(input) + */ + +namespace utfccp { + +inline std::string crappy_checksum(const std::string& in) { + int s = 0, ss = 0; + for (int i = 0; i < in.length(); i += 2) { + s += (int)in[i]; + if (i < in.length() - 1) + ss += (int)in[i + 1] * (int)in[i + 1]; + } + return { kBase64Alphabet[s % 64], kBase64Alphabet[ss % 64] }; +} + +inline std::string crappy_xorstring(const std::string& in) { + std::string out; + for (int i = 0; i < in.length(); i++) { + // 696969th prime, 13371337th prime + out.push_back(in[i] ^ ((10522103 * in.length()) + i * 244053389 % 256)); + } + return out; +} + +/* + * validate + * A + */ +inline bool validate(const std::string& in) { + if (in.length() < 4) return false; + if (in[0] != '!' || in[1] != '!') return false; + switch (in[2]) { + case 'A': + return crappy_checksum(in.substr(3, in.length() - 5)) == in.substr(in.length() - 2); + default: + return false; + } +} + +/* + * decrypt + * A + */ +inline std::string decrypt(const std::string& in) { + switch (in[2]) { + case 'A': { + std::string b64; + Base64::Decode(in.substr(3, in.length() - 5), &b64); + return crappy_xorstring(b64); + } + default: + return "Unsupported version"; + } +} + +/* + * encrypt + * falls back to A if version is invalid + * A + */ +inline std::string encrypt(const std::string& in, char version = 'A') { + switch (version) { + default: + case 'A': { + std::string b64; + Base64::Encode(crappy_xorstring(in), &b64); + return "!!A" + b64 + crappy_checksum(b64); + } + } +} + +} diff --git a/src/utfccp_commands.cpp b/src/utfccp_commands.cpp new file mode 100644 index 00000000..ce994947 --- /dev/null +++ b/src/utfccp_commands.cpp @@ -0,0 +1,22 @@ +/* + * utfccp_commands.cpp + * + * Created on: Jul 1, 2017 + * Author: nullifiedcat + */ + +#include "utfccp_commands.hpp" +#include "utfccp.hpp" +#include "common.h" + +CatCommand utfccp_encrypt("utfccp_encrypt", "Encrypt a message", [](const CCommand& args) { + logging::Info("%s", utfccp::encrypt(std::string(args.ArgS())).c_str()); +}); + +CatCommand utfccp_decrypt("utfccp_decrypt", "Decrypt a message", [](const CCommand& args) { + if (utfccp::validate(std::string(args.ArgS()))) { + logging::Info("%s", utfccp::decrypt(std::string(args.ArgS())).c_str()); + } else { + logging::Info("Invalid input data!"); + } +}); diff --git a/src/utfccp_commands.hpp b/src/utfccp_commands.hpp new file mode 100644 index 00000000..969caa3d --- /dev/null +++ b/src/utfccp_commands.hpp @@ -0,0 +1,13 @@ +/* + * utfccp_commands.hpp + * + * Created on: Jul 1, 2017 + * Author: nullifiedcat + */ + +#pragma once + +class CatCommand; + +extern CatCommand utfccp_encrypt; +extern CatCommand utfccp_decrypt; diff --git a/src/xb64.hpp b/src/xb64.hpp deleted file mode 100644 index 464101e9..00000000 --- a/src/xb64.hpp +++ /dev/null @@ -1,51 +0,0 @@ -/* - * xb64.hpp - * - * Created on: Jul 1, 2017 - * Author: nullifiedcat - */ - -#pragma once - -#include -#include "base64.h" - -namespace xb64 { - -std::string crappy_checksum(const std::string& in) { - int s = 0, ss = 0; - for (int i = 0; i < in.length(); i += 2) { - s += (int)in[i]; - if (i < in.length() - 1) - ss += (int)in[i + 1] * (int)in[i + 1]; - } - return { kBase64Alphabet[s % 64], kBase64Alphabet[ss % 64] }; -} - -std::string crappy_xorstring(const std::string& in) { - std::string out; - for (int i = 0; i < in.length(); i++) { - // 696969th prime, 13371337th prime - out.push_back(in[i] ^ ((10522103 * in.length()) + i * 244053389 % 256)); - } - return out; -} - -bool validate(const std::string& in) { - if (in.length() < 2) return false; - return crappy_checksum(in.substr(0, in.length() - 2)) == in.substr(in.length() - 2); -} - -std::string decrypt(const std::string& in) { - std::string b64; - Base64::Decode(in.substr(0, in.length() - 2), &b64); - return crappy_xorstring(b64); -} - -std::string encrypt(const std::string& in) { - std::string b64; - Base64::Encode(crappy_xorstring(in), &b64); - return b64 + crappy_checksum(b64); -} - -}