-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsca_utils.cpp
353 lines (288 loc) · 10.1 KB
/
nsca_utils.cpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//
// Created by macskas on 12/9/20.
//
#include "nsca_common.h"
#include "nsca_utils.h"
#include <mcrypt.h>
#include <cstring>
#include <unistd.h>
#include <csignal>
#include <cstdlib>
#include <ctime>
#include <cstdio>
static unsigned long crc32_table[256];
static volatile sig_atomic_t mcrypt_initialized=FALSE;
static unsigned long local_rnd_x = 123456789, local_rnd_y=362436069, local_rnd_z = 521288629;
void generate_crc32_table(){
unsigned long crc, poly;
int i, j;
poly=0xEDB88320L;
for(i=0;i<256;i++){
crc=i;
for(j=8;j>0;j--){
if(crc & 1)
crc=(crc>>1)^poly;
else
crc>>=1;
}
crc32_table[i]=crc;
}
}
/* calculates the CRC 32 value for a buffer */
unsigned long calculate_crc32(const char *buffer, int buffer_size){
if (buffer == nullptr)
return 0;
register unsigned long crc = 0xFFFFFFFF;
int this_char;
int current_index;
for(current_index=0; current_index<buffer_size; current_index++){
this_char=(int)buffer[current_index];
crc=((crc>>8) & 0x00FFFFFF) ^ crc32_table[(crc ^ this_char) & 0xFF];
}
return (crc ^ 0xFFFFFFFF);
}
unsigned long xorshf96() { //period 2^96-1
unsigned long t;
local_rnd_x ^= local_rnd_x << 16;
local_rnd_x ^= local_rnd_x >> 5;
local_rnd_x ^= local_rnd_x << 1;
t = local_rnd_x;
local_rnd_x = local_rnd_y;
local_rnd_y = local_rnd_z;
local_rnd_z = t ^ local_rnd_x ^ local_rnd_y;
return local_rnd_z;
}
/* initializes encryption routines */
int encrypt_init(const char *password,int encryption_method,char *received_iv,struct crypt_instance **CIptr){
int i;
int iv_size;
struct crypt_instance *CI;
CI=(crypt_instance *)(malloc(sizeof(struct crypt_instance)));
*CIptr=CI;
if(CI== nullptr){
return ERROR;
}
/* server generates IV used for encryption */
if(received_iv==nullptr)
generate_transmitted_iv(CI->transmitted_iv);
/* client receives IV from server */
else
memcpy(CI->transmitted_iv,received_iv,TRANSMITTED_IV_SIZE);
CI->iv_size = 0;
CI->blocksize=1; /* block size = 1 byte w/ CFB mode */
CI->keysize=7; /* default to 56 bit key length */
CI->mcrypt_mode = nullptr;
CI->mcrypt_algorithm = nullptr;
/* XOR or no encryption */
if(encryption_method==ENCRYPT_NONE || encryption_method==ENCRYPT_XOR)
return OK;
CI->mcrypt_mode = (char*)malloc(64); /* CFB = 8-bit cipher-feedback mode */
CI->mcrypt_algorithm = (char*)malloc(128);
memset(CI->mcrypt_mode, 0, 64);
memset(CI->mcrypt_algorithm, 0, 128);
strcpy(CI->mcrypt_mode, "cfb");
strcpy(CI->mcrypt_algorithm, "unknown");
/* get the name of the mcrypt encryption algorithm to use */
switch(encryption_method){
case ENCRYPT_DES:
strcpy(CI->mcrypt_algorithm, MCRYPT_DES);
break;
case ENCRYPT_3DES:
strcpy(CI->mcrypt_algorithm, MCRYPT_3DES);
break;
case ENCRYPT_CAST128:
strcpy(CI->mcrypt_algorithm, MCRYPT_CAST_128);
break;
case ENCRYPT_CAST256:
strcpy(CI->mcrypt_algorithm, MCRYPT_CAST_256);
break;
case ENCRYPT_XTEA:
strcpy(CI->mcrypt_algorithm, MCRYPT_XTEA);
break;
case ENCRYPT_3WAY:
strcpy(CI->mcrypt_algorithm, MCRYPT_3WAY);
break;
case ENCRYPT_BLOWFISH:
strcpy(CI->mcrypt_algorithm, MCRYPT_BLOWFISH);
break;
case ENCRYPT_TWOFISH:
strcpy(CI->mcrypt_algorithm, MCRYPT_TWOFISH);
break;
case ENCRYPT_LOKI97:
strcpy(CI->mcrypt_algorithm, MCRYPT_LOKI97);
break;
case ENCRYPT_RC2:
strcpy(CI->mcrypt_algorithm, MCRYPT_RC2);
break;
case ENCRYPT_ARCFOUR:
strcpy(CI->mcrypt_algorithm, MCRYPT_ARCFOUR);
break;
case ENCRYPT_RIJNDAEL128:
strcpy(CI->mcrypt_algorithm, MCRYPT_RIJNDAEL_128);
break;
case ENCRYPT_RIJNDAEL192:
strcpy(CI->mcrypt_algorithm, MCRYPT_RIJNDAEL_192);
break;
case ENCRYPT_RIJNDAEL256:
strcpy(CI->mcrypt_algorithm, MCRYPT_RIJNDAEL_256);
break;
case ENCRYPT_WAKE:
strcpy(CI->mcrypt_algorithm, MCRYPT_WAKE);
break;
case ENCRYPT_SERPENT:
strcpy(CI->mcrypt_algorithm, MCRYPT_SERPENT);
break;
case ENCRYPT_ENIGMA:
strcpy(CI->mcrypt_algorithm, MCRYPT_ENIGMA);
break;
case ENCRYPT_GOST:
strcpy(CI->mcrypt_algorithm, MCRYPT_GOST);
break;
case ENCRYPT_SAFER64:
strcpy(CI->mcrypt_algorithm, MCRYPT_SAFER_SK64);
break;
case ENCRYPT_SAFER128:
strcpy(CI->mcrypt_algorithm, MCRYPT_SAFER_SK128);
break;
case ENCRYPT_SAFERPLUS:
strcpy(CI->mcrypt_algorithm, MCRYPT_SAFERPLUS);
break;
default:
strcpy(CI->mcrypt_algorithm, "unknown");
break;
}
/* open encryption module */
if((CI->td=mcrypt_module_open(CI->mcrypt_algorithm, nullptr,CI->mcrypt_mode, nullptr))==MCRYPT_FAILED){
//syslog(LOG_ERR,"Could not open mcrypt algorithm '%s' with mode '%s'",CI->mcrypt_algorithm,CI->mcrypt_mode);
return ERROR;
}
/* determine size of IV buffer for this algorithm */
iv_size=mcrypt_enc_get_iv_size(CI->td);
if(iv_size>TRANSMITTED_IV_SIZE){
//syslog(LOG_ERR,"IV size for crypto algorithm exceeds limits");
return ERROR;
}
CI->iv_size = iv_size;
/* allocate memory for IV buffer */
if((CI->IV=(char *)malloc(iv_size))== nullptr){
//syslog(LOG_ERR,"Could not allocate memory for IV buffer");
return ERROR;
}
/* fill IV buffer with first bytes of IV that is going to be used to crypt (determined by server) */
for(i=0;i<iv_size;i++)
CI->IV[i]=CI->transmitted_iv[i];
/* get maximum key size for this algorithm */
CI->keysize=mcrypt_enc_get_key_size(CI->td);
/* generate an encryption/description key using the password */
if((CI->key=(char *)malloc(CI->keysize))== nullptr){
//syslog(LOG_ERR,"Could not allocate memory for encryption/decryption key");
return ERROR;
}
bzero(CI->key,CI->keysize);
if((size_t)CI->keysize < strlen(password))
strncpy(CI->key,password,CI->keysize);
else
strncpy(CI->key,password,strlen(password));
/* initialize encryption buffers */
mcrypt_generic_init(CI->td,CI->key,CI->keysize,CI->IV);
mcrypt_initialized=TRUE;
return OK;
}
/* encryption routine cleanup */
void encrypt_cleanup(int encryption_method, struct crypt_instance *CI){
/* no crypt instance */
if(CI==nullptr)
return;
/* mcrypt cleanup */
if(encryption_method!=ENCRYPT_NONE && encryption_method!=ENCRYPT_XOR){
if(mcrypt_initialized==TRUE)
mcrypt_generic_end(CI->td);
free(CI->key);
CI->key = nullptr;
free(CI->IV);
CI->IV = nullptr;
free(CI->mcrypt_algorithm);
CI->mcrypt_algorithm = nullptr;
free(CI->mcrypt_mode);
CI->mcrypt_mode = nullptr;
}
free(CI);
}
void generate_transmitted_iv(char *transmitted_iv){
for(int x=0;x<TRANSMITTED_IV_SIZE;x++)
transmitted_iv[x]= (char)(xorshf96() & 0xff);
}
void generate_transmitted_iv_secure(char *transmitted_iv){
FILE *fp;
int x;
int seed=0;
/*********************************************************/
/* fill IV buffer with data that's as random as possible */
/*********************************************************/
/* try to get seed value from /dev/urandom, as its a better source of entropy */
fp=fopen("/dev/urandom","r");
if(fp!= nullptr){
seed=fgetc(fp);
fclose(fp);
}
/* else fallback to using the current time as the seed */
else
seed=(int)time(nullptr);
/* generate pseudo-random IV */
srand(seed);
for(x=0;x<TRANSMITTED_IV_SIZE;x++)
transmitted_iv[x]=(char)((int)((256.0*rand())/(RAND_MAX+1.0)) & 0xff);
}
void encrypt_buffer(char *buffer,int buffer_size, const char *password, int encryption_method, struct crypt_instance *CI){
int x;
int y;
int password_length;
/* no crypt instance */
if(CI == nullptr)
return;
/* no encryption */
if(encryption_method==ENCRYPT_NONE)
return;
/* simple XOR "encryption" - not meant for any real security, just obfuscates data, but its fast... */
else if(encryption_method==ENCRYPT_XOR){
/* rotate over IV we received from the server... */
for(y=0,x=0;y<buffer_size;y++,x++){
/* keep rotating over IV */
if(x>=TRANSMITTED_IV_SIZE)
x=0;
buffer[y]^=CI->transmitted_iv[x];
}
/* rotate over password... */
password_length=strlen(password);
for(y=0,x=0;y<buffer_size;y++,x++){
/* keep rotating over password */
if(x>=password_length)
x=0;
buffer[y]^=password[x];
}
return;
}
else{
/* encrypt each byte of buffer, one byte at a time (CFB mode) */
for(x=0;x<buffer_size;x++)
mcrypt_generic(CI->td,&buffer[x],1);
}
}
void decrypt_buffer(char *buffer,int buffer_size, const char *password, int encryption_method, struct crypt_instance *CI){
int x=0;
/* no crypt instance */
if(CI== nullptr)
return;
/* no encryption */
if(encryption_method == ENCRYPT_NONE)
return;
/* XOR "decryption" is the same as encryption */
else if(encryption_method==ENCRYPT_XOR)
encrypt_buffer(buffer,buffer_size,password,encryption_method,CI);
else{
//mdecrypt_generic(CI->td, buffer, buffer_size);
/* encrypt each byte of buffer, one byte at a time (CFB mode) */
for(x=0;x<buffer_size;x++)
mdecrypt_generic(CI->td,&buffer[x],1);
}
}