Sanitize All Messages

This commit is contained in:
TheBrokenRail 2021-02-21 22:43:57 -05:00
parent 7b6fa15893
commit c36f89e523
4 changed files with 40 additions and 24 deletions

View File

@ -18,6 +18,7 @@ extern "C" {
// Set obj To NULL On asprintf() Failure
#define asprintf(obj, ...) if (asprintf(obj, __VA_ARGS__) == -1) { *obj = NULL; }
// Hook Library Function
#define HOOK(name, return_type, args) \
typedef return_type (*name##_t)args; \
static name##_t real_##name = NULL; \
@ -34,6 +35,11 @@ extern "C" {
\
__attribute__((__used__)) return_type name args
// Sanitize String
void sanitize_string(char **str, int max_length);
// Patching Functions
void _overwrite_call(const char *file, int line, void *start, void *target);
#define overwrite_call(start, target) _overwrite_call(__FILE__, __LINE__, start, target);

View File

@ -210,3 +210,23 @@ void _patch_address(const char *file, int line, void *start, void *target) {
unsigned char patch_data[4] = {addr & 0xff, (addr >> 8) & 0xff, (addr >> 16) & 0xff, (addr >> 24) & 0xff};
_patch(file, line, start, patch_data);
}
// Sanitize String
#define MINIMUM_MESSAGE_CHARACTER 32
#define MAXIMUM_MESSAGE_CHARACTER 126
void sanitize_string(char **str, int max_length) {
// Store Message Length
int length = strlen(*str);
// Truncate Message
if (max_length != -1 && length > max_length) {
(*str)[max_length] = '\0';
length = max_length;
}
// Loop Through Message
for (int i = 0; i < length; i++) {
if ((*str)[i] < MINIMUM_MESSAGE_CHARACTER || (*str)[i] > MAXIMUM_MESSAGE_CHARACTER) {
// Replace Illegal Character
(*str)[i] = '?';
}
}
}

View File

@ -12,9 +12,7 @@
#include "chat.h"
// Message Limitations
#define MAX_MESSAGE_LENGTH 512
#define MINIMUM_MESSAGE_CHARACTER 32
#define MAXIMUM_MESSAGE_CHARACTER 126
#define MAX_CHAT_MESSAGE_LENGTH 512
// Send API Command
static void send_api_command(unsigned char *minecraft, char *str) {
@ -36,29 +34,12 @@ static void send_api_chat_command(unsigned char *minecraft, char *str) {
free(command);
}
// Sanitize Message
static void sanitize_message(char **message) {
// Store Message Length
int length = strlen(*message);
// Truncate Message
if (length > MAX_MESSAGE_LENGTH) {
(*message)[MAX_MESSAGE_LENGTH] = '\0';
length = MAX_MESSAGE_LENGTH;
}
// Loop Through Message
for (int i = 0; i < length; i++) {
if ((*message)[i] < MINIMUM_MESSAGE_CHARACTER || (*message)[i] > MAXIMUM_MESSAGE_CHARACTER) {
// Replace Illegal Character
(*message)[i] = '?';
}
}
}
// Send Message To Players
static void send_message(unsigned char *server_side_network_handler, char *username, char *message) {
char *full_message = NULL;
asprintf(&full_message, "<%s> %s", username, message);
ALLOC_CHECK(full_message);
sanitize_message(&full_message);
sanitize_string(&full_message, MAX_CHAT_MESSAGE_LENGTH);
(*ServerSideNetworkHandler_displayGameMessage)(server_side_network_handler, std::string(full_message));
free(full_message);
}

View File

@ -71,22 +71,31 @@ static int32_t Inventory_setupDefault_FillingContainer_addItem_call_injection(un
// Print Chat To Log
static bool Gui_addMessage_recursing = false;
static void Gui_addMessage_injection(unsigned char *gui, std::string const& text) {
// Sanitize Message
char *new_message = strdup(text.c_str());
ALLOC_CHECK(new_message);
sanitize_string(&new_message, -1);
// Process Message
if (!Gui_addMessage_recursing) {
// Start Recursing
Gui_addMessage_recursing = true;
// Print Log Message
fprintf(stderr, "[CHAT]: %s\n", text.c_str());
fprintf(stderr, "[CHAT]: %s\n", new_message);
// Call Original Method
(*Gui_addMessage)(gui, text);
(*Gui_addMessage)(gui, std::string(new_message));
// End Recursing
Gui_addMessage_recursing = false;
} else {
// Call Original Method
(*Gui_addMessage)(gui, text);
(*Gui_addMessage)(gui, std::string(new_message));
}
// Free
free(new_message);
}
// Death Messages