Files
SimpleRemoter/common/encrypt.h
T
yuanyuanxianganddeepseek-flash ab990d8d31 Refactor: extract the shared AES key into common/aes_key.h
The 16-byte AES key was copy-pasted in the shellcode loader, the DLL
loader and XOREncoder16, so rotating it meant editing each copy.

Move it into a single inline get_aes_key() in common/aes_key.h and use
it from all three call sites; no literal key bytes remain elsewhere in
the tree. The header carries #pragma once because test.cpp includes it
directly and again through Common.h -> IOCPClient.h -> encrypt.h, which
without the guard gives C2084 and, on the v142 toolset, a follow-on
C2065 for every later use in the TU.

Verified: test.cpp and main.c compile under the project's v142 toolset;
the emitted key bytes are unchanged. Linux/macOS/Android keep pulling the
key through common/encrypt.h.

Co-Authored-By: deepseek-flash
2026-11-28 16:18:33 +01:00

162 lines
4.5 KiB
C++

#pragma once
// This file implements a serial of data encoding methods.
#include <vector>
extern "C" {
#include "aes.h"
}
#include "aes_key.h"
#define ALIGN16(n) ( (( (n) + 15) / 16) * 16 )
// Encoder interface. The default encoder will do nothing.
class Encoder
{
public:
virtual ~Encoder() {}
// Encode data before compress.
virtual void Encode(unsigned char* data, int len, unsigned char* param = 0) {}
// Decode data after uncompress.
virtual void Decode(unsigned char* data, int len, unsigned char* param = 0) {}
};
// XOR Encoder implementation.
class XOREncoder : public Encoder
{
private:
std::vector<char> Keys;
public:
XOREncoder(const std::vector<char>& keys = { 0 }) : Keys(keys) {}
virtual void Encode(unsigned char* data, int len, unsigned char* param = 0)
{
XOR(data, len, Keys);
}
virtual void Decode(unsigned char* data, int len, unsigned char* param = 0)
{
static std::vector<char> reversed(Keys.rbegin(), Keys.rend());
XOR(data, len, reversed);
}
protected:
void XOR(unsigned char* data, int len, const std::vector<char>& keys) const
{
for (char key : keys) {
for (int i = 0; i < len; ++i) {
data[i] ^= key;
}
}
}
};
// XOREncoder16 A simple Encoder for the TCP body. It's using for `HELL` protocol.
// This method is provided by ChatGPT. Encode data according to the 6th and 7th elem.
class XOREncoder16 : public Encoder
{
private:
static uint16_t pseudo_random(uint16_t seed, int index)
{
return ((seed ^ (index * 251 + 97)) * 733) ^ (seed >> 3);
}
void encrypt_internal(unsigned char* data, int len, unsigned char k1, unsigned char k2) const
{
uint16_t key = ((k1 << 8) | k2);
for (int i = 0; i < len; ++i) {
data[i] ^= (k1 + i * 13) ^ (k2 ^ (i << 1));
}
// Two rounds of pseudo-random swaps
for (int round = 0; round < 2; ++round) {
for (int i = 0; i < len; ++i) {
int j = pseudo_random(key, i + round * 100) % len;
std::swap(data[i], data[j]);
}
}
}
void decrypt_internal(unsigned char* data, int len, unsigned char k1, unsigned char k2) const
{
uint16_t key = ((k1 << 8) | k2);
for (int round = 1; round >= 0; --round) {
for (int i = len - 1; i >= 0; --i) {
int j = pseudo_random(key, i + round * 100) % len;
std::swap(data[i], data[j]);
}
}
for (int i = 0; i < len; ++i) {
data[i] ^= (k1 + i * 13) ^ (k2 ^ (i << 1));
}
}
#ifndef NO_AES
void aes_encrypt(unsigned char* data, int len, const unsigned char* key, const unsigned char* iv)
{
if (!data || !key || !iv || len <= 0 || len % 16 != 0) {
return; // AES CBC requires data length to be multiple of 16
}
struct AES_ctx ctx;
AES_init_ctx_iv(&ctx, key, iv);
AES_CBC_encrypt_buffer(&ctx, data, len);
}
void aes_decrypt(unsigned char* data, int len, const unsigned char* key, const unsigned char* iv)
{
if (!data || !key || !iv || len <= 0 || len % 16 != 0)
return;
struct AES_ctx ctx;
AES_init_ctx_iv(&ctx, key, iv);
AES_CBC_decrypt_buffer(&ctx, data, len);
}
#endif
public:
XOREncoder16() {}
void Encode(unsigned char* data, int len, unsigned char* param) override
{
if (param[6] == 0 && param[7] == 0) return;
if (param[7] == 1) {
#ifndef NO_AES
return aes_encrypt(data, len, get_aes_key(), param + 8);
#endif
}
encrypt_internal(data, len, param[6], param[7]);
}
void Decode(unsigned char* data, int len, unsigned char* param) override
{
if (param[6] == 0 && param[7] == 0) return;
decrypt_internal(data, len, param[6], param[7]);
}
};
class WinOsEncoder : public Encoder
{
public:
virtual ~WinOsEncoder() {}
// Encode data before compress.
virtual void Encode(unsigned char* data, int len, unsigned char* param = 0)
{
return XOR(data, len, param);
}
// Decode data after uncompress.
virtual void Decode(unsigned char* data, int len, unsigned char* param = 0)
{
return XOR(data, len, param);
}
private:
void XOR(unsigned char* data, int len, unsigned char* password)
{
for (int i = 0, j = 0; i < len; i++) {
((char*)data)[i] ^= (password[j++]) % 456 + 54;
if (i % (10) == 0)
j = 0;
}
}
};