Streamfab/libModKey2Key/dllmain.cpp
2026-02-26 08:18:02 +02:00

141 lines
4.2 KiB
C++

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include "dllmain.h"
typedef unsigned long long undefined8;
typedef unsigned long undefined4;
typedef unsigned char undefined;
typedef unsigned char byte;
typedef unsigned long ulong;
typedef unsigned int uint;
void hex_to_bytes(char* input, char* output, ulong buffer_size) {
/*
Hex to Bytes -- Returned as char* (0-255)
Parameters:
input: Input hex string data
output: Output bytes buffer
buffer_size: Output buffer size
*/
size_t len;
ulong i;
len = strlen(input);
if (len + 1 >> 1 <= buffer_size) {
for (i = 0; i < len; i = i + 2) {
sscanf(input + i, "%2hhx", output + (i >> 1));
}
}
return;
}
void bytes_to_hex(char* input, char* output, ulong buffer_size) {
ulong i;
for (i = 0; i < buffer_size; i = i + 1) {
sprintf((char*)(output + i * 2), "%02x", (ulong) * (byte*)(input + i));
}
return;
}
void _modkey2key(uint* mod, uint* tok) {
/* Secure function to undo modifications to "mod" key (In-place). */
// - when doing pointer arithmetic, make sure to use (long long) and not (long) as we
// are compiling for a 64-bit platform, not 32-bit.
long local_50;
long local_48;
byte local_31;
byte local_30;
byte local_2f;
short local_2e;
uint local_28;
uint local_24;
int local_20;
int local_1c;
short local_18;
byte local_15;
int i;
uint tok_xor = *tok ^ 0x2a2e3706;
uint mod_deref = *mod;
for (i = 1; i <= 3; i = i + 1) {
tok_xor = tok_xor ^ mod_deref;
local_2e = (short)(tok_xor >> 0x10);
local_18 = (short)tok_xor;
local_2f = (byte)(tok_xor >> 0x10);
local_30 = (byte)(local_2e >> 5) & 0x1f;
local_31 = (byte)((int)tok_xor >> 0x1a);
tok[i] = tok[i] << (-local_31 & 0x1f) | tok[i] >> (local_31 & 0x1f);
tok_xor = (int)(local_2e >> 0xf) ^
(tok_xor >> (local_2f & 0x1f) | tok_xor << (-local_2f & 0x1f)) ^
tok[i];
byte t_l = *(byte*)((long long)&tok_xor + (tok[i] - i & 3)) >> 3;
byte t_r = -(*(byte*)((long long)&tok_xor + (tok[i] - i & 3)) >> 3) & 0x1f;
mod[i] = mod[i] << t_l | mod[i] >> t_r;
for (local_20 = 0; local_20 < 4; local_20 = local_20 + 1) {
local_15 = 0;
for (local_1c = 0; local_1c < 4; local_1c = local_1c + 1) {
if ((int)((int)local_18 & 3U) < local_1c) {
local_24 = 0xff;
}
else {
local_24 = (uint) * (byte*)((long long)&tok_xor + (ulong)(local_1c + local_20 & 3));
}
local_15 = local_15 ^ (byte)local_24;
}
for (local_1c = 0; local_1c < 4; local_1c = local_1c + 1) {
if ((int)((int)(local_18 >> 2) & 3U) < local_1c) {
local_28 = 0xff;
}
else {
local_28 = (uint)local_15;
}
*(byte*)((long long)mod + (int)(i * 4 + (local_1c + local_20 & 3U))) =
*(byte*)((long long)mod + (int)(i * 4 + (local_1c + local_20 & 3U))) ^
(byte)local_28;
}
local_18 = local_18 >> 4;
}
mod_deref = mod[i];
local_50 = (long)i;
mod[i] = mod[i] << local_30 | mod[i] >> (-local_30 & 0x1f);
}
return;
}
char* modkey2key(char* mod, char* tok) {
size_t key_len;
char key[32];
char mod_bytes[16];
char tok_bytes[16];
char* key_buffer;
hex_to_bytes(mod, mod_bytes, 0x10);
hex_to_bytes(tok, tok_bytes, 0x10);
_modkey2key((uint*)&mod_bytes, (uint*)&tok_bytes);
bytes_to_hex(mod_bytes, key, 0x10);
// copy key to new buffer as `key` is allocated on the func stack
// it would get cleared on return if we don't do this
key_len = strlen(key);
key_buffer = (char*)malloc(key_len + 1); // +1 for null terminator
strcpy(key_buffer, key);
//std::cout << key << std::endl;
return key;
}