-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathencrypt_shellcode.c
222 lines (179 loc) · 6.01 KB
/
encrypt_shellcode.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIRST_BYTE 1
#define CHARACTER_SET_SIZE 255
int encrypt(int characterSet[][CHARACTER_SET_SIZE], unsigned char *key, unsigned char *payload, short debug)
{
int payloadLength = strlen((char *)payload);
// Loop through each char in payload, and use the values from
// the corresponding char from the key to determine which byte
// to select from characterSet and use it as the encoded byte.
int encrypted[payloadLength];
for (int i = 0; i < payloadLength; i++)
{
int currentByte = (int)payload[i];
int keyByte = (int)key[i];
encrypted[i] = characterSet[keyByte - FIRST_BYTE][currentByte - FIRST_BYTE];
if (debug)
{
printf("Position: %d\n", i);
printf("Payload byte (dec): %d\n", (int)payload[i]);
printf("Key byte (dec): %d\n", keyByte);
printf("Encrypted byte (dec): %d\n\n", encrypted[i]);
}
}
printf("\nEncrypted: ");
for (int i = 0; i < payloadLength; i++)
{
printf("\\x%02x", (int)encrypted[i]);
}
printf("\n");
return 0;
}
int decrypt(int characterSet[][CHARACTER_SET_SIZE], unsigned char *key, unsigned char *ciphertext, short execute, short debug)
{
int payloadLength = strlen((char *)ciphertext);
unsigned char originalPayload[payloadLength];
for (int i = 0; i < payloadLength; i++)
{
int encryptedByte = (int)ciphertext[i];
int keyByte = (int)key[i];
for (int i2 = 0; i2 < CHARACTER_SET_SIZE; i2++)
{
if (characterSet[keyByte - FIRST_BYTE][i2] == encryptedByte)
{
if (debug)
{
printf("Position: %d\n", i);
printf("Payload byte (dec): %d\n", (int)ciphertext[i]);
printf("Key byte (dec): %d\n", keyByte);
printf("Decrypted byte (dec): %d\n\n", characterSet[0][i2]);
}
originalPayload[i] = (unsigned char)characterSet[0][i2];
break;
}
}
}
if (execute == 1)
{
void (*s)() = (void *)originalPayload;
s();
}
else
{
printf("\nDecrypted: ");
for (int i = 0; i < payloadLength; i++)
{
printf("\\x%02x", (int)originalPayload[i]);
}
printf("\n");
}
return 0;
}
unsigned char* parseByteString(char *byteString)
{
unsigned int byteStringLength = strlen(byteString);
char byteStringCopy[byteStringLength];
strcpy(byteStringCopy, byteString);
unsigned int length = 0;
for (unsigned int i = 0; i < byteStringLength; i++)
{
if (byteStringCopy[i] == 'x')
{
length += 1;
}
}
unsigned char* parsedString = (unsigned char*)malloc(sizeof (unsigned char) * length);
const char delim[3] = "\\x";
char *b;
b = strtok(byteStringCopy, delim);
int currentByte = 0;
while( b != NULL ) {
char parsedByte = (char)(int)strtol(b, NULL, 16);
parsedString[currentByte] = parsedByte;
currentByte += 1;
b = strtok(NULL, delim);
}
return parsedString;
}
int main(int argc, char **argv)
{
short debug = argc == 5;
int characterSet[CHARACTER_SET_SIZE][CHARACTER_SET_SIZE];
// Loop for each permutation required
for (int i = 0; i < CHARACTER_SET_SIZE; i++)
{
// Add each character to the right of the
// initial offset to the start of the row.
for (int i2 = i; i2 < CHARACTER_SET_SIZE; i2++)
{
characterSet[i][i2 - i] = i2 + FIRST_BYTE;
}
// Rotate the characters to the left of the
// initial offset to the end of the row.
for (int i2 = 0; i2 < i; i2++)
{
characterSet[i][(CHARACTER_SET_SIZE - i) + i2] = i2 + FIRST_BYTE;
}
}
char *mode = argv[1];
unsigned char *baseKey = parseByteString(argv[2]);
int keyLength = strlen((char *)baseKey);
if (debug)
{
printf("Key: ");
for (unsigned int i = 0; i < strlen((char *)baseKey); i++)
{
printf("%02x ", baseKey[i]);
}
printf("\n");
}
unsigned char *payload = parseByteString(argv[3]);
int payloadLength = strlen((char *)payload);
if (debug)
{
printf("Payload: ");
for (unsigned int i = 0; i < strlen((char *)payload); i++)
{
printf("%02x ", payload[i]);
}
printf("\n");
}
int inflatedKeySize = keyLength;
int iterationsNeeded = 1;
// If the payload is larger than the key, the key needs to be
// repeated N times to make it match or exceed the length of
// the payload.
if (payloadLength > keyLength)
{
// Determine the number of times the key needs to be expanded
// to meet the length required to encrypt the payload.
iterationsNeeded = (int)((payloadLength / keyLength) + 0.5) + 1;
// Determine the new key size required and store it in
// inflatedKeySize for use when initialising the new key.
inflatedKeySize = keyLength * iterationsNeeded;
}
// Initialise the key with a null byte so that strcat can work.
unsigned char key[inflatedKeySize];
key[0] = '\x00';
// Concatenate the base key on to the new key to ensure it
// is long enough to encrypt the payload.
for (int i = 0; i < iterationsNeeded; i++)
{
strcat((char *)key, (char *)baseKey);
}
if (strcmp(mode, "e") == 0)
{
return encrypt(characterSet, key, payload, debug);
}
if (strcmp(mode, "d") == 0)
{
return decrypt(characterSet, key, payload, 0, debug);
}
if (strcmp(mode, "x") == 0)
{
return decrypt(characterSet, key, payload, 1, debug);
}
return 0;
}