-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.c
454 lines (364 loc) · 12.5 KB
/
decode.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/ioctl.h>
#define LINUX 1
#define MAX_LINE_LENGTH 1024
//bringing it alllllll in, for me to decide later.
#include "pel.h"
#include "aes.h"
#include "sha1.h"
#include "decode.h"
unsigned char buffer_d[BUFSIZE + 16 + 20];
// Convert hex char to integer
int hex_char_to_int(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
return 0;
}
// Convert a hex string to raw byte array
void hex_string_to_byte_array(const char *hex_string, unsigned char *byte_array, int byte_array_size) {
for (size_t i = 0; i < sizeof byte_array_size; i++){
byte_array[i] = '\0';
}
int length = strlen(hex_string);
for (int i = 0; i < length && i / 2 < byte_array_size; i += 2) {
byte_array[i / 2] = (hex_char_to_int(hex_string[i]) << 4) + hex_char_to_int(hex_string[i + 1]);
}
}
// Function that would handle the byte array (e.g., decryption)
void process_data(unsigned char *data, int data_len, bool cut) {
// Example function that just prints the data
if(!cut){
printf("Data received (hex): ");
}
for (int i = 0; i < data_len; i++) {
if(cut && i<2){
continue;
} else if (cut){
printf("%c", data[i]);
} else
{
printf("%02x", data[i]);
}
}
printf("\n");
// Add decryption or other data handling logic here
}
//completely validated
void setIVs(unsigned char *byte_array){
memcpy( IV1, &byte_array[20], 20 );
printf("%s","IV2:");
process_data(IV1, 20, false);
memcpy( IV2, &byte_array[ 0], 20 );
printf("%s","IV2:");
process_data(IV2, 20, false);
pel_setup_context( &send_d_ctx, secret, IV1 );
pel_setup_context( &recv_d_ctx, secret, IV2 );
}
//this program is INCREDIBLY PICKY with its function sigs
int pel_recv_msg_d( unsigned char buffer[BUFSIZE + 16 + 20], unsigned char *data, int *len ){
unsigned char temp[16];
unsigned char hmac[20];
unsigned char digest[20];
struct sha1_context sha1_ctx;
int i, j, ret, blk_len;
//pulls in 16 bytes
memcpy( temp, data, 16 );
aes_decrypt( &recv_d_ctx.SK, data );
for( j = 0; j < 16; j++ )
{
data[j] ^= recv_d_ctx.LCT[j];
}
*len = ( ((int) data[0]) << 8 ) + (int) data[1];
memcpy( data, temp, 16 );
if( *len <= 0 || *len > BUFSIZE )
{
pel_errno = PEL_BAD_MSG_LENGTH;
puts("out of range msg according to buffsize(client)");
exit( 666 );
}
blk_len = 2 + *len;
if( ( blk_len & 0x0F ) != 0 )
{
blk_len += 16 - ( blk_len & 0x0F );
}
//pulls in next block of data, from pos 16 onwards
memcpy( hmac, &data[blk_len], 20 );
/* verify the ciphertext integrity */
data[blk_len ] = ( recv_d_ctx.p_cntr << 24 ) & 0xFF;
data[blk_len + 1] = ( recv_d_ctx.p_cntr << 16 ) & 0xFF;
data[blk_len + 2] = ( recv_d_ctx.p_cntr << 8 ) & 0xFF;
data[blk_len + 3] = ( recv_d_ctx.p_cntr ) & 0xFF;
sha1_starts( &sha1_ctx );
sha1_update( &sha1_ctx, recv_d_ctx.k_ipad, 64 );
sha1_update( &sha1_ctx, data, blk_len + 4 );
sha1_finish( &sha1_ctx, digest );
sha1_starts( &sha1_ctx );
sha1_update( &sha1_ctx, recv_d_ctx.k_opad, 64 );
sha1_update( &sha1_ctx, digest, 20 );
sha1_finish( &sha1_ctx, digest );
if( memcmp( hmac, digest, 19 ) != 0 )
{
pel_errno = PEL_CORRUPTED_DATA;
puts("corruped block according to hmac(client)");
exit(666);
}
/* increment the packet counter */
recv_d_ctx.p_cntr++;
/* finally, decrypt and copy the message */
for( i = 0; i < blk_len; i += 16 )
{
memcpy( temp, &data[i], 16 );
aes_decrypt( &recv_d_ctx.SK, &data[i] );
for( j = 0; j < 16; j++ )
{
data[i + j] ^= recv_d_ctx.LCT[j];
}
memcpy( recv_d_ctx.LCT, temp, 16 );
}
//memcpy( data, &buffer[2], *len );
pel_errno = PEL_UNDEFINED_ERROR;
return( PEL_SUCCESS );
}
//this program is INCREDIBLY PICKY with its function sigs
int pel_recv_msg_d_send( unsigned char buffer[BUFSIZE + 16 + 20], unsigned char *data, int *len ){
unsigned char temp[16];
unsigned char hmac[20];
unsigned char digest[20];
struct sha1_context sha1_ctx;
int i, j, ret, blk_len;
//pulls in 16 bytes
memcpy( temp, data, 16 );
aes_decrypt( &send_d_ctx.SK, data );
for( j = 0; j < 16; j++ )
{
data[j] ^= send_d_ctx.LCT[j];
}
*len = ( ((int) data[0]) << 8 ) + (int) data[1];
memcpy( data, temp, 16 );
if( *len <= 0 || *len > BUFSIZE )
{
pel_errno = PEL_BAD_MSG_LENGTH;
puts("out of range msg according to buffsize(server)");
exit( 666 );
}
blk_len = 2 + *len;
if( ( blk_len & 0x0F ) != 0 )
{
blk_len += 16 - ( blk_len & 0x0F );
}
//pulls in next block of data, from pos 16 onwards
memcpy( hmac, &data[blk_len], 20 );
/* verify the ciphertext integrity */
data[blk_len ] = ( send_d_ctx.p_cntr << 24 ) & 0xFF;
data[blk_len + 1] = ( send_d_ctx.p_cntr << 16 ) & 0xFF;
data[blk_len + 2] = ( send_d_ctx.p_cntr << 8 ) & 0xFF;
data[blk_len + 3] = ( send_d_ctx.p_cntr ) & 0xFF;
sha1_starts( &sha1_ctx );
sha1_update( &sha1_ctx, send_d_ctx.k_ipad, 64 );
sha1_update( &sha1_ctx, data, blk_len + 4 );
sha1_finish( &sha1_ctx, digest );
sha1_starts( &sha1_ctx );
sha1_update( &sha1_ctx, send_d_ctx.k_opad, 64 );
sha1_update( &sha1_ctx, digest, 20 );
sha1_finish( &sha1_ctx, digest );
if( memcmp( hmac, digest, 19 ) != 0 )
{
pel_errno = PEL_CORRUPTED_DATA;
puts("corruped block according to hmac(server)");
exit(666);
}
/* increment the packet counter */
send_d_ctx.p_cntr++;
/* finally, decrypt and copy the message */
for( i = 0; i < blk_len; i += 16 )
{
memcpy( temp, &data[i], 16 );
aes_decrypt( &send_d_ctx.SK, &data[i] );
for( j = 0; j < 16; j++ )
{
data[i + j] ^= send_d_ctx.LCT[j];
}
memcpy( send_d_ctx.LCT, temp, 16 );
}
//memcpy( data, &buffer[2], *len );
pel_errno = PEL_UNDEFINED_ERROR;
return( PEL_SUCCESS );
}
void process_file(const char* filename, const char* filename2) {
FILE *client_traffic = fopen(filename, "r");
FILE *server_traffic = fopen(filename2, "r");
char line[MAX_LINE_LENGTH];
unsigned char data[MAX_LINE_LENGTH / 2];
char line2[MAX_LINE_LENGTH];
unsigned char data2[MAX_LINE_LENGTH / 2]; // Each byte is represented by two hex characters
bool ivset = false;
bool chal = false;
bool tsh_cmd = false;
bool runshell = false;
bool shell_term = false;
bool shell_winsize = false;
bool get_file = false;
bool put_file = false;
bool skipsend = false;
bool chal_server = true;
struct winsize ws;
bool client_EOF = false;
bool server_EOF = false;
//ripped from pel_server_init
int len, len2, frame1;
char * ret;
int c = 0;
size_t c_i = 0;
//main processing
//each line is processed here.
while (!client_EOF) {
c_i = 0;
do{
c = fgetc(client_traffic);
line[c_i] = c;
c_i++;
}while(c != '\n' && c != EOF && c_i < MAX_LINE_LENGTH);
if (c == EOF){
client_EOF = true;
}
int line_length = strlen(line);
if (line[line_length - 1] == '\n') {
line[line_length - 1] = '\0'; // Remove newline character
line_length--;
}
hex_string_to_byte_array(line, data, sizeof(data));
//split first line into IVs, then process IVs appropriately
//then, next line
if(!ivset){
ivset = true;
setIVs(data);
continue;
}
if(!skipsend){
c_i = 0;
do{
c = fgetc(server_traffic);
line2[c_i] = c;
c_i++;
}while(c != '\n' && c != EOF && c_i < MAX_LINE_LENGTH);
if (c == EOF){
server_EOF = true;
}
int line2_length = strlen(line);
if (line2[line2_length - 1] == '\n') {
line2[line2_length - 1] = '\0'; // Remove newline character
line2_length--;
}
hex_string_to_byte_array(line2, data2, sizeof(data2));
pel_recv_msg_d_send( 0, (unsigned char *)&data2, &len2 );
if(!chal){
chal_server = memcmp(challenge_d, &data2[1], 16);
}
} else {
skipsend = false;
}
//printf("input(server_traffic):");
//process_data(data, line_length/2,false);
pel_recv_msg_d( 0, &data, &len );
//process_data(data, line_length / 2);
//the idea is to next recreate pel_recv_msg
if(!chal){
if(memcmp(challenge_d, &data[1], 16)){
puts("CHALLENGE PASSED");
chal = true;
continue;
} else {
puts("Challenge failed");
exit(666);
}
}
if(!tsh_cmd){
switch(data[2]){
case RUNSHELL:
printf("runshell command recv'd\n");
tsh_cmd = true;
runshell = true;
shell_term = true;
skipsend = true;
continue;
break;
case PUT_FILE:
printf("PUT_FILE command recv'd\n");
tsh_cmd = true;
continue;
break;
case GET_FILE:
printf("GET_FILE command recv'd\n");
tsh_cmd = true;
continue;
break;
default:
//puts("unknown command");
}
}
if(tsh_cmd){
if(runshell){
if(shell_term){
printf("%s","TERM Environ Var: ");
fwrite(&data[1],sizeof(char),len+1,stdout);
puts("");
shell_term = false;
frame1 = len;
//hypothesis, I can advance 1 block at a time.
//each block is 36 characters, and they are all on the same line.
unsigned char * nextBlock;
nextBlock = &data[36];
pel_recv_msg_d( 0, nextBlock, &len );
ws.ws_row = ( (int) nextBlock[0] << 8 ) + (int) nextBlock[1];
ws.ws_col = ( (int) nextBlock[2] << 8 ) + (int) nextBlock[3];
printf("Window size: %hu, %hu\n",ws.ws_row,ws.ws_col);
//here we go. "exec bash --login"
nextBlock += 36;
pel_recv_msg_d( 0, nextBlock, &len );
//process_data(nextBlock, line_length/2-36,false);
if(memcmp("exec bash --login", &nextBlock[1], 17)){
puts("starting a shell");
process_data(data2, len2+2,true);
} else {
printf("%s","executing cmd: ");
process_data(nextBlock, len+2,true);
}
tsh_cmd = false;
continue;
} else if (shell_winsize) {
//TODO:
printf("%s","window size Environ Var: ");
fwrite(&data[1],sizeof(char),len+1,stdout);
puts("");
shell_winsize = false;
}
} else if (get_file){
} else if (put_file){
} else {
puts("You arent supposed to be here");
}
}
if(memcmp(data,data2,len+2)){
process_data(data, len+2,true);
process_data(data2, len2+2,true);
} else {
printf("Final output(client_traffic):");
process_data(data, len+2,true);
printf("Final output(server_traffic):");
process_data(data2, len2+2,true);
}
}
fclose(client_traffic);
return;
}
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <filename> <filename2>\n", argv[0]);
return 1;
}
process_file(argv[1],argv[2]);
}