diff --git a/src/base64.h b/src/base64.h new file mode 100644 index 00000000..27ad2f92 --- /dev/null +++ b/src/base64.h @@ -0,0 +1,271 @@ +#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" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + +class Base64 { + public: + static bool Encode(const std::string &in, std::string *out) { + int i = 0, j = 0; + size_t enc_len = 0; + unsigned char a3[3]; + unsigned char a4[4]; + + out->resize(EncodedLength(in)); + + int input_len = in.size(); + std::string::const_iterator input = in.begin(); + + while (input_len--) { + a3[i++] = *(input++); + if (i == 3) { + a3_to_a4(a4, a3); + + for (i = 0; i < 4; i++) { + (*out)[enc_len++] = kBase64Alphabet[a4[i]]; + } + + i = 0; + } + } + + if (i) { + for (j = i; j < 3; j++) { + a3[j] = '\0'; + } + + a3_to_a4(a4, a3); + + for (j = 0; j < i + 1; j++) { + (*out)[enc_len++] = kBase64Alphabet[a4[j]]; + } + + while ((i++ < 3)) { + (*out)[enc_len++] = '='; + } + } + + return (enc_len == out->size()); + } + + static bool Encode(const char *input, size_t input_length, char *out, size_t out_length) { + int i = 0, j = 0; + char *out_begin = out; + unsigned char a3[3]; + unsigned char a4[4]; + + size_t encoded_length = EncodedLength(input_length); + + if (out_length < encoded_length) return false; + + while (input_length--) { + a3[i++] = *input++; + if (i == 3) { + a3_to_a4(a4, a3); + + for (i = 0; i < 4; i++) { + *out++ = kBase64Alphabet[a4[i]]; + } + + i = 0; + } + } + + if (i) { + for (j = i; j < 3; j++) { + a3[j] = '\0'; + } + + a3_to_a4(a4, a3); + + for (j = 0; j < i + 1; j++) { + *out++ = kBase64Alphabet[a4[j]]; + } + + while ((i++ < 3)) { + *out++ = '='; + } + } + + return (out == (out_begin + encoded_length)); + } + + static bool Decode(const std::string &in, std::string *out) { + int i = 0, j = 0; + size_t dec_len = 0; + unsigned char a3[3]; + unsigned char a4[4]; + + int input_len = in.size(); + std::string::const_iterator input = in.begin(); + + out->resize(DecodedLength(in)); + + while (input_len--) { + if (*input == '=') { + break; + } + + a4[i++] = *(input++); + if (i == 4) { + for (i = 0; i <4; i++) { + a4[i] = b64_lookup(a4[i]); + } + + a4_to_a3(a3,a4); + + for (i = 0; i < 3; i++) { + (*out)[dec_len++] = a3[i]; + } + + i = 0; + } + } + + if (i) { + for (j = i; j < 4; j++) { + a4[j] = '\0'; + } + + for (j = 0; j < 4; j++) { + a4[j] = b64_lookup(a4[j]); + } + + a4_to_a3(a3,a4); + + for (j = 0; j < i - 1; j++) { + (*out)[dec_len++] = a3[j]; + } + } + + return (dec_len == out->size()); + } + + static bool Decode(const char *input, size_t input_length, char *out, size_t out_length) { + int i = 0, j = 0; + char *out_begin = out; + unsigned char a3[3]; + unsigned char a4[4]; + + size_t decoded_length = DecodedLength(input, input_length); + + if (out_length < decoded_length) return false; + + while (input_length--) { + if (*input == '=') { + break; + } + + a4[i++] = *(input++); + if (i == 4) { + for (i = 0; i <4; i++) { + a4[i] = b64_lookup(a4[i]); + } + + a4_to_a3(a3,a4); + + for (i = 0; i < 3; i++) { + *out++ = a3[i]; + } + + i = 0; + } + } + + if (i) { + for (j = i; j < 4; j++) { + a4[j] = '\0'; + } + + for (j = 0; j < 4; j++) { + a4[j] = b64_lookup(a4[j]); + } + + a4_to_a3(a3,a4); + + for (j = 0; j < i - 1; j++) { + *out++ = a3[j]; + } + } + + return (out == (out_begin + decoded_length)); + } + + static int DecodedLength(const char *in, size_t in_length) { + int numEq = 0; + + const char *in_end = in + in_length; + while (*--in_end == '=') ++numEq; + + return ((6 * in_length) / 8) - numEq; + } + + static int DecodedLength(const std::string &in) { + int numEq = 0; + int n = in.size(); + + for (std::string::const_reverse_iterator it = in.rbegin(); *it == '='; ++it) { + ++numEq; + } + + return ((6 * n) / 8) - numEq; + } + + inline static int EncodedLength(size_t length) { + return (length + 2 - ((length + 2) % 3)) / 3 * 4; + } + + inline static int EncodedLength(const std::string &in) { + return EncodedLength(in.length()); + } + + inline static void StripPadding(std::string *in) { + while (!in->empty() && *(in->rbegin()) == '=') in->resize(in->size() - 1); + } + + private: + static inline void a3_to_a4(unsigned char * a4, unsigned char * a3) { + a4[0] = (a3[0] & 0xfc) >> 2; + a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4); + a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6); + a4[3] = (a3[2] & 0x3f); + } + + static inline void a4_to_a3(unsigned char * a3, unsigned char * a4) { + a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4); + a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2); + a3[2] = ((a4[2] & 0x3) << 6) + a4[3]; + } + + static inline unsigned char b64_lookup(unsigned char c) { + if(c >='A' && c <='Z') return c - 'A'; + if(c >='a' && c <='z') return c - 71; + if(c >='0' && c <='9') return c + 4; + if(c == '+') return 62; + if(c == '/') return 63; + return 255; + } +}; + + + +#endif // BASE64_H 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 bf4d7fc1..96005781 100644 --- a/src/hooks/others.cpp +++ b/src/hooks/others.cpp @@ -8,6 +8,7 @@ #include "../common.h" #include "../netmessage.h" #include "../hack.h" +#include "../utfccp.hpp" #include "hookedmethods.h" static CatVar no_invisibility(CV_SWITCH, "no_invis", "0", "Remove Invisibility", "Useful with chams!"); @@ -127,6 +128,7 @@ static CatVar newlines_msg(CV_INT, "chat_newlines", "0", "Prefix newlines", "Add //static CatVar queue_messages(CV_SWITCH, "chat_queue", "0", "Queue messages", "Use this if you want to use spam/killsay and still be able to chat normally (without having your msgs eaten by valve cooldown)"); static CatVar airstuck(CV_KEY, "airstuck", "0", "Airstuck"); +static CatVar crypt_chat(CV_SWITCH, "chat_crypto", "0", "Crypto chat", "Start message with !! and it will be only visible to cathook users"); bool SendNetMsg_hook(void* _this, INetMessage& msg, bool bForceReliable = false, bool bVoice = false) { static size_t say_idx, say_team_idx; @@ -138,13 +140,23 @@ bool SendNetMsg_hook(void* _this, INetMessage& msg, bool bForceReliable = false, const SendNetMsg_t original = (SendNetMsg_t)hooks::netchannel.GetMethod(offsets::SendNetMsg()); SEGV_BEGIN; // net_StringCmd - if (msg.GetType() == 4 && (newlines_msg)) { + if (msg.GetType() == 4 && (newlines_msg || crypt_chat)) { std::string str(msg.ToString()); say_idx = str.find("net_StringCmd: \"say \""); say_team_idx = str.find("net_StringCmd: \"say_team \""); if (!say_idx || !say_team_idx) { offset = say_idx ? 26 : 21; - if (newlines_msg) { + bool crpt = false; + if (crypt_chat) { + std::string msg(str.substr(offset)); + msg = msg.substr(0, msg.length() - 2); + if (msg.find("!!") == 0) { + msg = utfccp::encrypt(msg.substr(2)); + str = str.substr(0, offset) + msg + "\"\""; + crpt = true; + } + } + if (!crpt && newlines_msg) { // TODO move out? update in a value change callback? newlines = std::string((int)newlines_msg, '\n'); str.insert(offset, newlines); @@ -315,7 +327,7 @@ bool DispatchUserMessage_hook(void* _this, int type, bf_read& buf) { static const DispatchUserMessage_t original = (DispatchUserMessage_t)hooks::client.GetMethod(offsets::DispatchUserMessage()); SEGV_BEGIN; - if (clean_chat) { + if (clean_chat || crypt_chat) { if (type == 4) { loop_index = 0; s = buf.GetNumBytesLeft(); @@ -324,10 +336,22 @@ bool DispatchUserMessage_hook(void* _this, int type, bf_read& buf) { for (i = 0; i < s; i++) data[i] = buf.ReadByte(); j = 0; + std::string name; + std::string message; for (i = 0; i < 3; i++) { while ((c = data[j++]) && (loop_index < 128)) { loop_index++; - if ((c == '\n' || c == '\r') && (i == 1 || i == 2)) data[j - 1] = '*'; + if (clean_chat) + if ((c == '\n' || c == '\r') && (i == 1 || i == 2)) data[j - 1] = '*'; + if (i == 1) name.push_back(c); + if (i == 2) message.push_back(c); + } + } + if (crypt_chat) { + if (message.find("!!") == 0) { + if (utfccp::validate(message)) { + PrintChat("\x07%06X%s\x01: %s", 0xe05938, name.c_str(), utfccp::decrypt(message).c_str()); + } } } buf = bf_read(data, s); 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;