anonymize.c 28.4 KB
Newer Older
's avatar
committed
1
2
3
4
5
6
#include <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
's avatar
committed
7
#include <pcap.h>
's avatar
committed
8
9
10
11
12
13
14
15
#include "mapidflib.h"
#include "mapidlib.h"
#include "mapidevices.h"
#include "mapid.h"
#include "fhelp.h"
#include "list.h"
#include "mapiipc.h"
#include "anonymization.h"
's avatar
committed
16
#include "../stdlib/cooking.h"
's avatar
committed
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

struct anonymize_data {
	int protocol;
	int field;
	int function;
	int hash_algorithm;
	int padding_behavior;
	int distribution_type;
	int median;
	int standard_deviation;
	int seed;
	int pattern_type;
	char *pattern;
	char *regexp;
	int num_of_matches;
	char **replaceVector;
	struct mapidlibflow *flow;
};

int can_field_be_applied_to_protocol(int protocol,int field);
int can_field_be_applied_to_function(int anonymization_function,int field); 
void anonymize_field(int protocol, int field, int function,mapipacket *packet,struct anonymize_data *params); 
void init_mapping_tables();
void swap(unsigned char *a,unsigned char *b);
void checkSwap(unsigned char *field_pointer,int field); 
's avatar
committed
42
int anonymize_stream(mapidflib_function_instance_t *instance,struct mapidlibflow *flow,struct anonymize_data *params,mapid_pkthdr_t* pkt_head); 
's avatar
committed
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
void anonymize_reordering(struct mapidlibflow *flow); 

/* mapping table for fields */

mapNode *ipMappingTable[MAPPING_ENTRIES]; int ip_count=1;
mapNode *portsMappingTable[MAPPING_ENTRIES]; int ports_count=1;
mapNode *generalMapping32Table[MAPPING_ENTRIES]; int general32_count=1;
mapNode *generalMapping16Table[MAPPING_ENTRIES]; int general16_count=1;
mapNode *generalMapping8Table[MAPPING_ENTRIES]; int general8_count=1;

/******FUNCTION STUFF*******/

///////////////////////////////////
struct mapidlibflow *tmp_flow;

int can_field_be_applied_to_protocol(int protocol,int field) {
	if(field==PAYLOAD) //common to all protocols
		return 1;
	
	switch(protocol) {
		case IP: 
			if(field<PAYLOAD || field>FRAGMENT_OFFSET) 
				return 0;
			break;
		case TCP:
			if(field<PAYLOAD || field>TCP_OPTIONS)
				return 0;
			break;
		case UDP:
			if((field<PAYLOAD || field>DST_PORT) && field!=UDP_DATAGRAM_LENGTH)
				return 0;
			break;
		case ICMP:
			if(field<PAYLOAD || (field>FRAGMENT_OFFSET && field!=TYPE && field!=CODE)) 
				return 0;
			break;
		case HTTP:
			if(field<PAYLOAD || (field>TCP_OPTIONS && (field<=BASE_HTTP_DEFS || field>=END_HTTP_DEFS)))
				return 0;
			break;
		case FTP:
			if(field<PAYLOAD || (field>TCP_OPTIONS && (field<=BASE_FTP_DEFS || field>=END_FTP_DEFS)))
				return 0;
			break;
		default:
			return 0;
	}
	
	return 1;
}

struct unmarshal_data {
	int hash_algorithm;
	int padding_behavior;
	int distribution_type;
	int median;
	int standard_deviation;
	int seed;
	int pattern_type;
	char *pattern;
	char *regexp;
	int num_of_matches;
	char **replaceVector;
};

int unmarshalBuffer(int function,char *buffer,struct unmarshal_data *data); 

int unmarshalBuffer(int function,char *buffer,struct unmarshal_data *data) {
	int hash_algorithm=0;
	int padding_behavior=0;
	int median=0;
	int standard_deviation=0;
	int distribution_type=0;
	int seed=0,pattern_type=0;
	char pattern[1024];
	char *pos;
	char *regexp=NULL,*tmpbuf;
	int num_of_matches,i;
	char **replaceVector=NULL;
	

	switch(function) {
		case UNCHANGED:
		case MAP:
		case ZERO:
		case PREFIX_PRESERVING:
		case RANDOM:
		case FILENAME_RANDOM:
		case CHECKSUM_ADJUST:
			return 1;
		case STRIP:
			sscanf(buffer,"%d",&seed);
			break;
		case HASHED:
			if(buffer[0]=='\0') 
				return 0;
			if((pos=strchr(buffer,'#'))==NULL || *(pos+1)=='\0')
				return 0;
			sscanf(buffer,"%d#%d",&hash_algorithm,&padding_behavior);
			if(hash_algorithm<SHA || hash_algorithm>DES)
				return 0;
			if(padding_behavior!=PAD_WITH_ZERO && padding_behavior!=STRIP_REST && padding_behavior!=UNCHANGED) 
				return 0;
			break;
		case MAP_DISTRIBUTION:
			if(buffer[0]=='\0') 
				return 0;
			if((pos=strchr(buffer,'#'))==NULL || *(pos+1)=='\0')
				return 0;
			sscanf(buffer,"%d#%d#%d",&distribution_type,&median,&standard_deviation);
			if(distribution_type!=UNIFORM && distribution_type!=GAUSSIAN)
				return 0;
			break;
		case PATTERN_FILL:
			if(buffer[0]=='\0') 
				return 0;
			if((pos=strchr(buffer,'#'))==NULL || *(pos+1)=='\0')
				return 0;
			sscanf(buffer,"%d#%s",&pattern_type,pattern);
			if(pattern_type!=0 && pattern_type!=1)
				return 0;
			break;
		case REPLACE:
			if(buffer[0]=='\0') 
				return 0;
			sscanf(buffer,"%s",pattern);
			break;
		case REGEXP:
			if(buffer[0]=='\0') 
				return 0;
				
			tmpbuf=(char *)strdup(buffer);
			pos=strchr(tmpbuf,'#');
			if(pos==NULL) {
				return 0;
			}
			*pos='\0';
			regexp=(char *)strdup(tmpbuf);
			tmpbuf=pos+1;
			
			pos=strchr(tmpbuf,'#');
			if(pos==NULL) {
				return 0;
			}
			*pos='\0';
			num_of_matches=atoi(tmpbuf);	
			tmpbuf=pos+1;
			printf(">>>>>>> REGEXP: %s %d\n",regexp,num_of_matches);
			replaceVector=(char **)malloc(num_of_matches*sizeof(char *));
			memset(replaceVector,0,num_of_matches*sizeof(char *));	
			for(i=0;i<(num_of_matches-1);i++) {
				pos=strchr(tmpbuf,'#');
				if(pos==NULL) {
					printf("!!!!\n");
					return 0;
				}
				*pos='\0';
				printf("REPLACE VECTOR[%d]=%s\n",i,tmpbuf);
				if(strcmp(tmpbuf,"NULL")!=0) 
					replaceVector[i]=(char *)strdup(tmpbuf);
				tmpbuf=pos+1;
			}
			
			printf("REPLACE VECTOR[%d]=%s\n",i,tmpbuf);
			if(strcmp(tmpbuf,"NULL")!=0) 
				replaceVector[i]=(char *)strdup(tmpbuf);
			
			break;
		default:
			printf("UNKNOWN FUNCTION\n");
			return 0;
			
	}
	
	if(data) {
		data->seed=seed;
		data->hash_algorithm=hash_algorithm;
		data->padding_behavior=padding_behavior;
		data->distribution_type=distribution_type;
		data->median=median;
		data->standard_deviation=standard_deviation;
		data->pattern_type=pattern_type;
		if(function==PATTERN_FILL || function==REPLACE) {
			data->pattern=strdup(pattern);
		}
		else {
			data->pattern=NULL;
		}
		data->regexp=regexp;
		data->num_of_matches=num_of_matches;
		data->replaceVector=replaceVector;
	}

	return 1;
}

int can_field_be_applied_to_function(int anonymization_function,int field) {
's avatar
committed
240
	if((anonymization_function==PREFIX_PRESERVING || anonymization_function==PREFIX_PRESERVING_MAP) && field!=SRC_IP && field!=DST_IP) {
's avatar
committed
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
		printf("PREFIX_PRESERVING can only be applied to IP addresses\n");
		return 0;
	}

	if((anonymization_function==MAP ||anonymization_function==MAP_DISTRIBUTION) && (field<CHECKSUM || field>CODE || field==OPTIONS || field==TCP_OPTIONS)) {
		printf("MAP/MAP_DISTRIBUTION can only be applied to IP,TCP,UDP and ICMP headers (except IP and TCP options\n");
		return 0;
	}
	
	if(anonymization_function==STRIP && (field!=PAYLOAD) && (field!=OPTIONS) && (field!=TCP_OPTIONS) && (field<=BASE_HTTP_DEFS)
		&& (field>=END_HTTP_DEFS) && (field<=BASE_FTP_DEFS) && (field<=END_FTP_DEFS)) {
		printf("STRIP can only be applied to IP and TCP options, PAYLOAD and all HTTP, FTP headers\n");
		return 0;
	}

	/*if(anonymization_function==HASHED && (field>=CHECKSUM &&  field<=CODE)) {
		printf("HASHING cannot be performed on headers\n");
		return 0;
	}*/
	
	if(anonymization_function==REPLACE && (field>=CHECKSUM &&  field<=CODE)) {
		printf("REPLACE cannot be performed on headers\n");
		return 0;
	}
	
	if(anonymization_function==CHECKSUM_ADJUST && field!=CHECKSUM) {
		printf("CHECKSUM_ADJUST can only be applied to CHECKSUM field\n");
		return 0;
	}
	
	return 1;

}

static int anonymize_instance(mapidflib_function_instance_t *instance,
				 flist_t *flow_flist,
's avatar
committed
277
				 mapidflib_flow_mod_t *flow_mod,function_manipulation_t* manip)
's avatar
committed
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
{
	mapiFunctArg* args=instance->args;
	int protocol, field_description, anonymization_function;
	char *marshalledBuffer;
	
	protocol=getargint(&args);
	field_description=getargint(&args);
	anonymization_function=getargint(&args);
	marshalledBuffer=getargstr(&args);

	//printf("PROTOCOL: %d FIELD_DESCRIPTION: %d FUNCTION: %d\n",protocol,field_description,anonymization_function);
	//printf("MARSHALLED PARAMETERS: %s\n",marshalledBuffer);
	
	//first trivial sanity checks
	if(protocol<IP || protocol>FTP) {
		return MFUNCT_INVALID_ARGUMENT_1;
	}

	//field shouldn't be special enumeration like BASE_FTP_DEFS
	if((field_description<=BASE_FIELD_DEFS || field_description>=END_FIELD_DEFS) 
		|| field_description==BASE_FTP_DEFS 
		|| field_description==END_FTP_DEFS 
		|| field_description==BASE_HTTP_DEFS
		|| field_description==END_HTTP_DEFS) {
		
		return MFUNCT_INVALID_ARGUMENT_2;
	}

	if(anonymization_function<UNCHANGED || anonymization_function>REGEXP) {
		printf("UNKNOWN FUNCTION\n");
		return MFUNCT_INVALID_ARGUMENT_3;
	}

	if(!can_field_be_applied_to_protocol(protocol,field_description)) {
		printf("FIELD CANNOT BE APPLIED TO SPECIFIC PROTOCOL\n");
		return MFUNCT_INVALID_ARGUMENT_2;
	}

	if(!can_field_be_applied_to_function(anonymization_function,field_description)) {
		printf("FIELD CANNOT BE APPLIED TO SPECIFIC FUNCTION\n");
		return MFUNCT_INVALID_ARGUMENT_2;
	}

	if(unmarshalBuffer(anonymization_function,marshalledBuffer,NULL)==0) {
		printf("UNMARSHALLING FAILED\n");
		return MFUNCT_INVALID_ARGUMENT_4;
	}
	
	flist_node_t *n;
	struct mapidlibflow *flow;
	mapidlib_instance_t *i;

	i = flow_mod->mi;
	n = flist_head(i->flowlist);

	while(n)
	{
		flow = flist_data(n);
		
		if(flow->fd == instance->fd)
		{
			break;
		}

		n = flist_next(n);
	}
	tmp_flow=flow;
	
  	return 0;
};

///////////////////////////////
int various_inited=0;

void init_mapping_tables() {
	memset(ipMappingTable,0,MAPPING_ENTRIES*sizeof(mapNode *));
	memset(portsMappingTable,0,MAPPING_ENTRIES*sizeof(mapNode *));
	memset(generalMapping32Table,0,MAPPING_ENTRIES*sizeof(mapNode *));
	memset(generalMapping16Table,0,MAPPING_ENTRIES*sizeof(mapNode *));
	memset(generalMapping8Table,0,MAPPING_ENTRIES*sizeof(mapNode *));
}



static int anonymize_init(mapidflib_function_instance_t *instance, flist_t *flist)
{
	int protocol, field_description, anonymization_function;
	char *marshalledBuffer;
	
	mapiFunctArg* args=instance->args;
	struct anonymize_data *data;
	struct unmarshal_data *unmarshaled;
	
	protocol=getargint(&args);
	field_description=getargint(&args);
	anonymization_function=getargint(&args);
	marshalledBuffer=getargstr(&args);

	data=(struct anonymize_data *)malloc(sizeof(struct anonymize_data));
	data->protocol=protocol;
	data->field=field_description;
	data->function=anonymization_function;
	data->flow=tmp_flow;
	
	//printf("FLOW POINTER : %p\n",tmp_flow); 
	
	
	instance->internal_data=(void *)data;

	switch(anonymization_function) {
		case UNCHANGED:
		case MAP:
		case ZERO:
		case PREFIX_PRESERVING:
		case RANDOM:
		case FILENAME_RANDOM:
		case CHECKSUM_ADJUST:
			break;
		default: 
			unmarshaled=(struct unmarshal_data *)malloc(sizeof(struct unmarshal_data));
			if(unmarshalBuffer(anonymization_function,marshalledBuffer,unmarshaled)==0) {
				printf("UNMARSHALLING FAILED\n");
				return -1;
			}

			data->seed=unmarshaled->seed;
			data->hash_algorithm=unmarshaled->hash_algorithm;
			data->padding_behavior=unmarshaled->padding_behavior;
			data->median=unmarshaled->median;
			data->standard_deviation=unmarshaled->standard_deviation;
			data->pattern_type=unmarshaled->pattern_type;
			data->distribution_type=unmarshaled->distribution_type;
			if(unmarshaled->pattern)
				data->pattern=strdup(unmarshaled->pattern);
			else
				data->pattern=NULL;
			
			data->regexp=unmarshaled->regexp;
			data->num_of_matches=unmarshaled->num_of_matches;
			data->replaceVector=unmarshaled->replaceVector;

			free(unmarshaled);
	}

	if(!various_inited) {
		init_mapping_tables();
		gen_table();
		srand48((long)time(NULL));
		various_inited=1;
	}


	
  	return 0;
}


void swap(unsigned char *a,unsigned char *b) {
	unsigned char c;
	c=*a;
	*a=*b;
	*b=c;
}

void checkSwap(unsigned char *field_pointer,int field) {
	if(field==SRC_IP || field==DST_IP || field==SEQUENCE_NUMBER || field==ACK_NUMBER) {
		swap(&field_pointer[0],&field_pointer[3]);
		swap(&field_pointer[1],&field_pointer[1]);
	}
	else if(field==SRC_PORT || field==DST_PORT || field==PACKET_LENGTH) {
		swap(&field_pointer[0],&field_pointer[1]);
	}
}

////////////////////////////////

void anonymize_field(int protocol, int field, int function,mapipacket *packet,struct anonymize_data *params) 
{
	unsigned char *field_pointer=NULL;
	unsigned char *header_pointer=NULL;
	unsigned short len,header_len;
	int helper;
	unsigned char DES3_keys[24] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23 } ;
	unsigned char AES_keys[32] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x01, 0x23, 0x45, 0x67, 0x23, 0x45, 0x67, 0x89};
	unsigned char *packet_end;
	unsigned int total_len;
	
	if(!packet) {
		printf("WARNING: NULL packet\n");
		return;
	}

	if(function==UNCHANGED) 
		return;
	
	switch(protocol) {
		case IP:
			if(!packet->iph) 
				return;
			switch(field) {
				case PAYLOAD:
					field_pointer=(unsigned char *)(packet->iph+sizeof(IPHdr)+packet->ip_options_len);
					header_pointer=(unsigned char *)(packet->iph+sizeof(IPHdr)+packet->ip_options_len);
					len=header_len=ntohs(packet->iph->ip_len)-sizeof(IPHdr)-packet->ip_options_len;
					break;
				case CHECKSUM:
					field_pointer=(unsigned char *)(&(packet->iph->ip_csum));
					len=2;
					break;
				case TTL:
					field_pointer=(unsigned char *)(&(packet->iph->ip_ttl));
					len=1;
					break;
				case SRC_IP:
					field_pointer=(unsigned char *)(&(packet->iph->ip_src));
					len=4;
					break;
				case DST_IP:
					field_pointer=(unsigned char *)(&(packet->iph->ip_dst));
					len=4;
					break;
				case TOS:
					field_pointer=(unsigned char *)(&(packet->iph->ip_tos));
					len=1;
					break;
				case ID:
					field_pointer=(unsigned char *)(&(packet->iph->ip_id));
					len=2;
					break;
				case FRAGMENT_OFFSET:
					field_pointer=(unsigned char *)(&(packet->iph->ip_off));
					len=2;
					break;
				case VERSION:
					field_pointer=(unsigned char *)(&(packet->iph->ip_verhl));
					len=1;
					break;
				case IHL:
					field_pointer=(unsigned char *)(&(packet->iph->ip_verhl));
					len=1;
					break;
				case PACKET_LENGTH:
					field_pointer=(unsigned char *)(&(packet->iph->ip_len));
					len=2;
					break;
				case IP_PROTO:
					field_pointer=(unsigned char *)(&(packet->iph->ip_proto));
					len=1;
					break;
				case OPTIONS: 
					field_pointer=(unsigned char *)(packet->ip_options_data);
					header_pointer=(unsigned char *)(packet->ip_options_data);
					len=header_len=packet->ip_options_len;
					break;
				default:
					break;
			}
			break;
		case TCP:
			if(!packet->tcph) 
				return;
			
			if(field>=SRC_IP && field<=FRAGMENT_OFFSET) { //hierarchical	
				return anonymize_field(IP,field,function,packet,params);
				break;
			}
			switch(field) {
				case PAYLOAD:
					field_pointer=(unsigned char *)(packet->data);
					header_pointer=(unsigned char *)(packet->data);
					len=header_len=packet->dsize;
					break;
				case CHECKSUM:
					field_pointer=(unsigned char *)(&(packet->tcph->th_sum));
					len=2;
					break;
				case SRC_PORT:
					field_pointer=(unsigned char *)(&(packet->tcph->th_sport));
					len=2;
					break;
				case DST_PORT:
					field_pointer=(unsigned char *)(&(packet->tcph->th_dport));
					len=2;
					break;
				case SEQUENCE_NUMBER:
					field_pointer=(unsigned char *)(&(packet->tcph->th_seq));
					len=4;
					break;
				case ACK_NUMBER:
					field_pointer=(unsigned char *)(&(packet->tcph->th_ack));
					len=4;
					break;
				case WINDOW:
					field_pointer=(unsigned char *)(&(packet->tcph->th_win));
					len=2;
					break;
				case FLAGS:	
					field_pointer=(unsigned char *)(&(packet->tcph->th_flags));
					len=1;
					break;
				case OFFSET_AND_RESERVED:
					field_pointer=(unsigned char *)(&(packet->tcph->th_offx2));
					len=1;
					break;
				case URGENT_POINTER: 
					field_pointer=(unsigned char *)(&(packet->tcph->th_urp));
					len=2;
				case TCP_OPTIONS:
					field_pointer=packet->tcp_options_data;
					header_pointer=packet->tcp_options_data;
					len=packet->tcp_options_len;
					break;
				default:
					break;
			}
			break;
		case UDP: 
			if(!packet->udph) 
				return;
			
			if(field>=SRC_IP && field<=FRAGMENT_OFFSET) { //hierarchical	
				return anonymize_field(IP,field,function,packet,params);
				break;
			}
			switch(field) {
				case PAYLOAD:
					field_pointer=(unsigned char *)(packet->udph+sizeof(UDPHdr)); //maybe packet->data should be better
					header_pointer=(unsigned char *)(packet->udph+sizeof(UDPHdr)); //maybe packet->data should be better
					len=header_len=packet->dsize;
					break;
				case CHECKSUM:
					field_pointer=(unsigned char *)(&(packet->udph->uh_chk));
					len=2;
					break;
				case SRC_PORT:
					field_pointer=(unsigned char *)(&(packet->udph->uh_sport));
					len=2;
					break;
				case DST_PORT:
					field_pointer=(unsigned char *)(&(packet->udph->uh_dport));
					len=2;
					break;
				case UDP_DATAGRAM_LENGTH:
					field_pointer=(unsigned char *)(&(packet->udph->uh_len));
					len=2;
					break;
				default:
					break;
			}
			break;
		case ICMP:
			if(!packet->icmph) 
				return;

			if(field>=SRC_IP && field<=FRAGMENT_OFFSET) { //hierarchical	
				return anonymize_field(IP,field,function,packet,params);
				break;
			}
			switch(field) {
				case PAYLOAD:
					field_pointer=(unsigned char *)(packet->icmph+sizeof(ICMPHdr));
					header_pointer=(unsigned char *)(packet->icmph+sizeof(ICMPHdr));
					len=header_len=packet->dsize;
					break;
				case CHECKSUM:
					field_pointer=(unsigned char *)(&(packet->icmph->csum));
					len=2;
					break;
				case TYPE:
					field_pointer=(unsigned char *)(&(packet->icmph->type));
					len=1;
					break;
				case CODE:
					field_pointer=(unsigned char *)(&(packet->icmph->code));
					len=1;
					break;
				default:
					break;
			}
			break;
		case HTTP:
			{
			struct httpheader *h;
			//decode the http if we haven't done so
			if(packet->num_of_upper_layer_protocols==0) {
				packet->upper_layer_protocol_headers[0]=(void *)malloc(sizeof(struct httpheader));
				if(http_decode(packet,(struct httpheader *)packet->upper_layer_protocol_headers[0])==-1) {
					//printf("Cannot parse HTTP protocol\n");
					free(packet->upper_layer_protocol_headers[0]);
					return;
				}
				else {
					packet->upper_layer_names[0]=HTTP;
					packet->num_of_upper_layer_protocols++;
					h=(struct httpheader *)packet->upper_layer_protocol_headers[0];
				}
			}
			else { //try to find the HTTP header
				int j;
				for(j=0;j<packet->num_of_upper_layer_protocols;j++) {
					if(packet->upper_layer_names[j]==HTTP)
						break;
				}

				if(j==packet->num_of_upper_layer_protocols) 
					return;
				h=(struct httpheader *)packet->upper_layer_protocol_headers[j];
				
			}

			if(field>=SRC_IP && field<=FRAGMENT_OFFSET) { //hierarchical	
				return anonymize_field(IP,field,function,packet,params);
				break;
			}
			
			if(field>=SRC_PORT && field<=TCP_OPTIONS) {
				return anonymize_field(TCP,field,function,packet,params);
				break;
			}

			switch(field) {
				case PAYLOAD:
					field_pointer=h->pointers_to_value[HTTP_PAYLOAD-BASE_HTTP_DEFS-1];
					header_pointer=h->pointers_to_value[HTTP_PAYLOAD-BASE_HTTP_DEFS-1];
					len=header_len=h->value_length[HTTP_PAYLOAD-BASE_HTTP_DEFS-1];
					break;
				default:
					field_pointer=h->pointers_to_value[field-BASE_HTTP_DEFS-1];
					len=h->value_length[field-BASE_HTTP_DEFS-1];
					
					header_pointer=h->pointers_to_header[field-BASE_HTTP_DEFS-1];
					header_len=h->header_length[field-BASE_HTTP_DEFS-1];
					break;
			}
			break;	
			}
		case FTP:
			{
			struct ftpheader *h;
			//decode the http if we haven't done so
			if(packet->num_of_upper_layer_protocols==0) {
				packet->upper_layer_protocol_headers[0]=(void *)malloc(sizeof(struct ftpheader));
				if(ftp_decode(packet,(struct ftpheader *)packet->upper_layer_protocol_headers[0])==-1) {
					//printf("Cannot parse FTP protocol\n");
					free(packet->upper_layer_protocol_headers[0]);
					return;
				}
				else {
					packet->upper_layer_names[0]=FTP;
					packet->num_of_upper_layer_protocols++;
					h=(struct ftpheader *)packet->upper_layer_protocol_headers[0];
				}
			}
			else { //try to find the HTTP header
				int j;
				for(j=0;j<packet->num_of_upper_layer_protocols;j++) {
					if(packet->upper_layer_names[j]==FTP)
						break;
				}

				if(j==packet->num_of_upper_layer_protocols) 
					return;
				h=(struct ftpheader *)packet->upper_layer_protocol_headers[j];
				
			}

			if(field>=SRC_IP && field<=FRAGMENT_OFFSET) { //hierarchical	
				return anonymize_field(IP,field,function,packet,params);
				break;
			}
			
			if(field>=SRC_PORT && field<=TCP_OPTIONS) {
				return anonymize_field(TCP,field,function,packet,params);
				break;
			}

			switch(field) {
				case PAYLOAD:
					return;
				default:
					field_pointer=h->pointers_to_value[field-BASE_FTP_DEFS-1];
					len=h->value_length[field-BASE_HTTP_DEFS-1];
					
					header_pointer=h->pointers_to_header[field-BASE_FTP_DEFS-1];
					header_len=h->header_length[field-BASE_FTP_DEFS-1];
					break;
			}
			break;	
			}
		default:
		
			break;
	}
	
	if(!field_pointer) //a HTTP reply for example doesn't have URI
		return;

	//function-specific argument selection
	mapNode **mapTable;
	int *counter;
	
	switch(function) {
		case MAP:
			if(field==SRC_IP || field==DST_IP) {
				mapTable=ipMappingTable;
				counter=&ip_count;
			}
			else if(field==SRC_PORT || field==DST_PORT) {
				mapTable=portsMappingTable;
				counter=&ports_count;
			}
			else {
				if(len==4) {
					mapTable=generalMapping32Table;
					counter=&general32_count;
				}
				else if(len==2) {
					mapTable=generalMapping16Table;
					counter=&general16_count;
				}
				else {
					mapTable=generalMapping8Table;
					counter=&general8_count;
				}
			}
			map_field(field_pointer,len,mapTable,counter);
			//checkSwap(field_pointer,field);	
						
			break;
		case MAP_DISTRIBUTION:
			map_distribution(field_pointer,len,params->distribution_type,params->median,params->standard_deviation);
			//checkSwap(field_pointer,field);	
			break;
		case PREFIX_PRESERVING:
			prefix_preserving_anonymize_field(field_pointer);
			break;
's avatar
committed
814
815
816
		case PREFIX_PRESERVING_MAP:
			hide_addr(field_pointer);
			break;
's avatar
committed
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
		case STRIP:
			//printf("++I will call STRIP and I will keep %d bytes\n",params->seed);
			total_len=ntohs(packet->iph->ip_len);
			packet_end=((unsigned char *)(packet->iph))+total_len;
			strip(packet,header_pointer,header_len,params->seed,total_len,packet_end);
			break;
		case HASHED:
			//printf("I will call HASH for algorithm %d and padding %d\n",params->hash_algorithm,params->padding_behavior);
			total_len=ntohs(packet->iph->ip_len);
			packet_end=((unsigned char *)(packet->iph))+total_len;
			int donotreplace=0;
			if(field>=CHECKSUM &&  field<=CODE)
				donotreplace=1;
			switch(params->hash_algorithm) {
				case SHA:
					sha1_hash(field_pointer,len,params->padding_behavior,packet,total_len,packet_end,donotreplace);
					break;
				case MD5:
					md5_hash(field_pointer,len,params->padding_behavior,packet,total_len,packet_end,donotreplace);
					break;
				case CRC32:
					crc32_hash(field_pointer,len,params->padding_behavior,packet,total_len,packet_end,donotreplace);
					break;
				case SHA_2:
					sha256_hash(field_pointer,len,params->padding_behavior,packet,total_len,packet_end,donotreplace);
					break;
				case DES:
				case TRIPLEDES:
					des_hash(field_pointer,len,(unsigned char *)DES3_keys,params->padding_behavior,packet);
					break;
				case AES:
					aes_hash(field_pointer,len,(unsigned char *)AES_keys,params->padding_behavior,packet);
					break;
				default:
					printf("Fatal Error!\n");
					exit(0);
			}
			break;
		case PATTERN_FILL:
			//printf("I will call PATTERN_FILL with type %d and pattern: %s\n",params->pattern_type,params->pattern);
			switch(params->pattern_type) {
				case 0: //integers
					helper=atoi(params->pattern);
					pattern_fill_field(field_pointer,len,params->pattern_type,(void *)&helper);
					break;
				case 1:
					pattern_fill_field(field_pointer,len,params->pattern_type,(void *)params->pattern);
					break;
			}
			checkSwap(field_pointer,field);	
			break;
		case FILENAME_RANDOM:
			//printf("++I will call FILENAME_RANDOM (%p,%d)\n",field_pointer,len);
			filename_random_field(field_pointer,len);
			break;
		case RANDOM:
			//printf("++I will call RANDOM %u.%u.%u.%u\n",field_pointer[0],field_pointer[1],field_pointer[2],field_pointer[3]);
			random_field(field_pointer,len);	
			break;
		case ZERO:
			memset(field_pointer,0,len);
			break;
		case REPLACE:
			//printf("++I will call REPLACE with pattern: %s\n",params->pattern);
			total_len=ntohs(packet->iph->ip_len);
			packet_end=((unsigned char *)(packet->iph))+total_len;
			replace_field(field_pointer,len,params->pattern,strlen(params->pattern),packet,total_len,packet_end);
			break;
		case CHECKSUM_ADJUST:
			switch(protocol) {
				case IP:
					packet->iph->ip_csum=calculate_ip_sum(packet);
					if(packet->tcph) { //pseudoheader uses some info from IP
						packet->tcph->th_sum=calculate_tcp_sum(packet);
					}
					else if(packet->udph) {
						packet->udph->uh_chk=calculate_udp_sum(packet);
					}
					break;
				case TCP:
					packet->tcph->th_sum=calculate_tcp_sum(packet);
					break;
				case UDP:
					packet->udph->uh_chk=calculate_udp_sum(packet);
					break;
				case ICMP:
					packet->icmph->csum=calculate_icmp_sum(packet);
					break;
				case HTTP:
				case FTP:
					if(packet->tcph) { //pseudoheader uses some info from IP
						packet->tcph->th_sum=calculate_tcp_sum(packet);
					}
					break;
			}
			break;
		case REGEXP:
			total_len=ntohs(packet->iph->ip_len);
			packet_end=((unsigned char *)(packet->iph))+total_len;
			reg_exp_substitute(field_pointer,len,params->regexp,params->replaceVector,params->num_of_matches,packet,total_len,packet_end);
			break;
		default:
			break;
	}
}


mapid_pkthdr_t *last_header_seen=NULL;
's avatar
committed
925
mapipacket decoded_packet;
's avatar
committed
926

's avatar
committed
927
928
929
930
931
932
933
934
935
936
937
938
939
int anonymize_stream(mapidflib_function_instance_t *instance,struct mapidlibflow *flow,struct anonymize_data *params,mapid_pkthdr_t* pkt_head) {
	struct pcap_pkthdr pkthdr;
	mapid_pkthdr_t* mapi_head;
	
	mapi_head=pkt_head;
		
	pkthdr.caplen=mapi_head->caplen;
	pkthdr.len=mapi_head->wlen;
	pkthdr.ts.tv_sec=mapi_head->ts; //XXX
	pkthdr.ts.tv_usec=mapi_head->ts;

	flow->decoded_packet=&decoded_packet;
	decode_packet(instance->hwinfo->link_type,instance->hwinfo->cap_length,&pkthdr,flow->mod_pkt,flow->decoded_packet);	
's avatar
committed
940
	((mapipacket *)flow->decoded_packet)->dsize=flow->client_size;
's avatar
committed
941
	if(((mapipacket *)flow->decoded_packet)->data==NULL) { //decoder does not work well above 65536
's avatar
committed
942
		((mapipacket *)flow->decoded_packet)->data = ((mapipacket *)flow->decoded_packet)->pkt+(mapi_head->caplen - flow->client_size);
's avatar
committed
943
944
945
946
	}
	
	last_header_seen = mapi_head;
	anonymize_field(params->protocol,params->field,params->function,flow->decoded_packet,params);
's avatar
committed
947
			
's avatar
committed
948

's avatar
committed
949
950
951
952
953
954
955
956
	return 1;
}


static int anonymize_process(mapidflib_function_instance_t *instance,
			const unsigned char* dev_pkt,
			const unsigned char* link_pkt,
			mapid_pkthdr_t* pkt_head)  
's avatar
committed
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
{
	struct anonymize_data *params;
	struct mapidlibflow *flow;
	struct pcap_pkthdr pkthdr;

	params=(struct anonymize_data *)instance->internal_data;
	flow=params->flow;
	
	if(flow->client_headers!=NULL) { //if this is a cooked packet try to anonymize it
		return anonymize_stream(instance,flow,params,pkt_head);
	}
	pkthdr.caplen=pkt_head->caplen;
	pkthdr.len=pkt_head->wlen;
	pkthdr.ts.tv_sec=pkt_head->ts; //XXX 
	pkthdr.ts.tv_usec=pkt_head->ts; 
	
	last_header_seen = pkt_head;

	if(flow->decoded_packet==NULL) {
		flow->decoded_packet=&decoded_packet;
		decode_packet(instance->hwinfo->link_type,instance->hwinfo->cap_length,&pkthdr,(unsigned char *)dev_pkt,flow->decoded_packet);	
	}
	
	anonymize_field(params->protocol,params->field,params->function,flow->decoded_packet,params);
	
's avatar
committed
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
	return 1;
}

///////////////////////////////////////

static int anonymize_reset(mapidflib_function_instance_t *instance) 
{
  return 0;
}

//////////////////////////////////////////

static int anonymize_cleanup(mapidflib_function_instance_t *instance) 
{
  return 0;
}

/////////////////////////////////////////

For faster browsing, not all history is shown. View entire blame