-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellcodeDecryptor.cpp
More file actions
71 lines (62 loc) · 2.57 KB
/
Copy pathShellcodeDecryptor.cpp
File metadata and controls
71 lines (62 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <string.h>
#include <stdlib.h>
typedef struct {
unsigned char S[256];
int i, j;
} RC4State;
void InitRC4(RC4State* state, const unsigned char* key, size_t key_len) {
for (int i = 0; i < 256; ++i) state->S[i] = (unsigned char)i;
int j = 0;
for (int i = 0; i < 256; ++i) {
j = (j + state->S[i] + key[i % key_len]) & 0xFF;
unsigned char temp = state->S[i];
state->S[i] = state->S[j];
state->S[j] = temp;
}
state->i = state->j = 0;
}
void DecryptRC4(RC4State* state, unsigned char* data, size_t len) {
for (size_t k = 0; k < len; ++k) {
state->i = (state->i + 1) & 0xFF;
state->j = (state->j + state->S[state->i]) & 0xFF;
unsigned char temp = state->S[state->i];
state->S[state->i] = state->S[state->j];
state->S[j] = temp;
data[k] ^= state->S[(state->S[state->i] + state->S[state->j]) & 0xFF];
}
}
void DecryptXOR(unsigned char* data, size_t len, unsigned char key) {
for (size_t i = 0; i < len; ++i) data[i] ^= key;
}
unsigned char* MacToData(const char** mac_array, size_t* data_len) {
size_t count = 0;
while (mac_array[count]) ++count;
*data_len = count * 6;
unsigned char* data = (unsigned char*)malloc(*data_len);
if (!data) return nullptr;
for (size_t i = 0; i < count; ++i) {
sscanf(mac_array[i], "%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx",
&data[i * 6], &data[i * 6 + 1], &data[i * 6 + 2],
&data[i * 6 + 3], &data[i * 6 + 4], &data[i * 6 + 5]);
}
return data;
}
unsigned char* DecodeBase64(const char* encoded, size_t* decoded_len) {
const char b64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t len = strlen(encoded);
*decoded_len = (len / 4) * 3;
if (encoded[len - 1] == '=') (*decoded_len)--;
if (encoded[len - 2] == '=') (*decoded_len)--;
unsigned char* decoded = (unsigned char*)malloc(*decoded_len);
if (!decoded) return nullptr;
for (size_t i = 0, j = 0; i < len; ) {
int a = strchr(b64_chars, encoded[i++]) - b64_chars;
int b = strchr(b64_chars, encoded[i++]) - b64_chars;
int c = (encoded[i] == '=') ? 0 : strchr(b64_chars, encoded[i++]) - b64_chars;
int d = (encoded[i] == '=') ? 0 : strchr(b64_chars, encoded[i++]) - b64_chars;
decoded[j++] = (a << 2) | (b >> 4);
if (j < *decoded_len) decoded[j++] = ((b & 0xF) << 4) | (c >> 2);
if (j < *decoded_len) decoded[j++] = ((c & 0x3) << 6) | d;
}
return decoded;
}