updated UTFCCP, added commands.

This commit is contained in:
nullifiedcat 2017-07-01 19:32:47 +03:00
parent ad6c1c3f05
commit a8976e487b
7 changed files with 155 additions and 55 deletions

View File

@ -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 <string>
const char kBase64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

View File

@ -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"

View File

@ -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());
}
}
}

100
src/utfccp.hpp Normal file
View File

@ -0,0 +1,100 @@
/*
* utfccp.hpp
*
* Created on: Jul 1, 2017
* Author: nullifiedcat
*/
#pragma once
#include <string>
#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);
}
}
}
}

22
src/utfccp_commands.cpp Normal file
View File

@ -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!");
}
});

13
src/utfccp_commands.hpp Normal file
View File

@ -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;

View File

@ -1,51 +0,0 @@
/*
* xb64.hpp
*
* Created on: Jul 1, 2017
* Author: nullifiedcat
*/
#pragma once
#include <string>
#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);
}
}