anonymize.c 33.6 KB
Newer Older
1
2
3
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
's avatar
committed
4
5
6
7
8
9
#include <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
's avatar
committed
10
#include <pcap.h>
's avatar
committed
11
12
13
14
15
16
17
#include "mapidflib.h"
#include "mapidlib.h"
#include "mapidevices.h"
#include "mapid.h"
#include "fhelp.h"
#include "mapiipc.h"
#include "anonymization.h"
18
#include "../extraflib/cooking.h"
19
#include "mapi_errors.h"
20

's avatar
nits    
committed
21
#include "names.h"
22
extern char *anonnames[];
's avatar
committed
23
24
25
26
27

struct anonymize_data {
	int protocol;
	int field;
	int function;
's avatar
nits    
committed
28
	char *marshalledBuffer;
's avatar
committed
29
30
31
32
33
34
35
36
37
38
39
	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;
40
	struct cooking_data *flow;
41
42
	void *decoded_packet;
	mapidflib_function_instance_t *first_anon_instance;
43
	int fd;
's avatar
committed
44
45
46
47
48
49
50
51
};

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); 
52
53
int anonymize_stream(mapidflib_function_instance_t *instance,struct cooking_data *flow,struct anonymize_data *params,mapid_pkthdr_t* pkt_head, unsigned char *mod_pkt); 
void anonymize_reordering(struct cooking_data *flow); 
's avatar
committed
54

's avatar
committed
55
56
57
58

extern void hide_addr(unsigned char *raw_addr);
extern int reg_exp_substitute(unsigned char *field, int len, char *regular_expression, char **replacement_vector, int num_of_matches,mapipacket *p,int total_len,unsigned char *packet_end);

's avatar
committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* 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: 
Stig Venaas's avatar
Stig Venaas committed
78
			if((field<PAYLOAD || field>FRAGMENT_OFFSET) && field != FLOW)
's avatar
committed
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
				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;
134
	int num_of_matches = 0,i = 0;
's avatar
committed
135
136
	char **replaceVector=NULL;
	
's avatar
nits    
committed
137
138
	if(buffer==NULL) 
		return 0;
's avatar
committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

	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;
155
			if((pos=strchr(buffer,','))==NULL || *(pos+1)=='\0')
's avatar
committed
156
				return 0;
157
			sscanf(buffer,"%d,%d",&hash_algorithm,&padding_behavior);
's avatar
committed
158
			if(hash_algorithm<ANON_SHA || hash_algorithm>ANON_DES)
's avatar
committed
159
160
161
162
163
164
165
				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;
166
			if((pos=strchr(buffer,','))==NULL || *(pos+1)=='\0')
's avatar
committed
167
				return 0;
168
			sscanf(buffer,"%d,%d,%d",&distribution_type,&median,&standard_deviation);
's avatar
committed
169
170
171
172
173
174
			if(distribution_type!=UNIFORM && distribution_type!=GAUSSIAN)
				return 0;
			break;
		case PATTERN_FILL:
			if(buffer[0]=='\0') 
				return 0;
175
			if((pos=strchr(buffer,','))==NULL || *(pos+1)=='\0')
's avatar
committed
176
				return 0;
177
			sscanf(buffer,"%d,%s",&pattern_type,pattern);
's avatar
committed
178
179
180
181
182
183
184
185
186
187
188
189
190
			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);
191
			pos=strchr(tmpbuf,',');
's avatar
committed
192
193
194
195
196
197
198
			if(pos==NULL) {
				return 0;
			}
			*pos='\0';
			regexp=(char *)strdup(tmpbuf);
			tmpbuf=pos+1;
			
199
			pos=strchr(tmpbuf,',');
's avatar
committed
200
201
202
203
204
205
206
207
208
209
			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++) {
210
				pos=strchr(tmpbuf,',');
's avatar
committed
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
				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
256
	if((anonymization_function==PREFIX_PRESERVING || anonymization_function==PREFIX_PRESERVING_MAP) && field!=SRC_IP && field!=DST_IP) {
's avatar
committed
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
		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;

}

's avatar
nits    
committed
291

's avatar
committed
292
static int anonymize_instance(mapidflib_function_instance_t *instance,
293
294
				 MAPI_UNUSED int fd,
				 MAPI_UNUSED mapidflib_flow_mod_t *flow_mod)
's avatar
committed
295
296
{
	mapiFunctArg* args=instance->args;
297
	int protocol = 0, field_description = 0, anonymization_function = 0 ;
's avatar
nits    
committed
298
	char *marshalledBuffer = NULL,*ptr=NULL;
299
	struct anonymize_data *data = NULL;
's avatar
committed
300
301
	
	marshalledBuffer=getargstr(&args);
's avatar
nits    
committed
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
	
	/***** protocol *****/
	ptr=strchr(marshalledBuffer,',');
	if(ptr==NULL) {
		return MFUNCT_INVALID_ARGUMENT_1;
	}
	*ptr='\0';
	
	if((protocol=str2anonid(marshalledBuffer))==0) 
		return MFUNCT_INVALID_ARGUMENT_1;
	
	/************ field *********/
	marshalledBuffer=ptr+1;
	ptr=strchr(marshalledBuffer,',');
	if(ptr==NULL) {
		return MFUNCT_INVALID_ARGUMENT_2;
	}
	*ptr='\0';
	
	if((field_description=str2anonid(marshalledBuffer))==0) 
		return MFUNCT_INVALID_ARGUMENT_2;
	
	/********** function ******/
	marshalledBuffer=ptr+1;
	ptr=strchr(marshalledBuffer,',');
	if(ptr!=NULL) {
		*ptr='\0';
's avatar
committed
329
330
		if((anonymization_function=str2anonid(marshalledBuffer))==0) 
			return MFUNCT_INVALID_ARGUMENT_3;
's avatar
nits    
committed
331
332
333
		marshalledBuffer=ptr+1;
	}
	else {
's avatar
committed
334
335
		if((anonymization_function=str2anonid(marshalledBuffer))==0) 
			return MFUNCT_INVALID_ARGUMENT_3;
's avatar
nits    
committed
336
337
338
		marshalledBuffer=NULL;
	}
	
's avatar
committed
339
	//printf("PROTOCOL: %d FIELD_DESCRIPTION: %d FUNCTION: %d\n",protocol,field_description,anonymization_function);
's avatar
committed
340
341
342
343
344
345
346
347
	//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
Stig Venaas's avatar
Stig Venaas committed
348
349
	if(((field_description<=BASE_FIELD_DEFS || field_description>=END_FIELD_DEFS)
	        && field_description!=FLOW)
's avatar
committed
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
		|| 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;
	}

's avatar
nits    
committed
373
	if(marshalledBuffer!=NULL && unmarshalBuffer(anonymization_function,marshalledBuffer,NULL)==0) {
's avatar
committed
374
375
376
377
378
379
380
381
		printf("UNMARSHALLING FAILED\n");
		return MFUNCT_INVALID_ARGUMENT_4;
	}
	
	mapidlib_instance_t *i;

	i = flow_mod->mi;
	
382
383
384
385
	data=(struct anonymize_data *)malloc(sizeof(struct anonymize_data));
	data->protocol=protocol;
	data->field=field_description;
	data->function=anonymization_function;
's avatar
nits    
committed
386
387
388
389
390
	if(marshalledBuffer)
		data->marshalledBuffer=strdup(marshalledBuffer);
	else 
		data->marshalledBuffer=NULL;

391
	data->fd = fd;
392
393
	instance->internal_data=(void *)data;

's avatar
committed
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
  	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 *));
}



410
static int anonymize_init(mapidflib_function_instance_t *instance, MAPI_UNUSED int fd)
's avatar
committed
411
412
413
414
{
	int protocol, field_description, anonymization_function;
	char *marshalledBuffer;
	
's avatar
committed
415
	//mapiFunctArg* args=instance->args;
's avatar
committed
416
417
418
419
	struct anonymize_data *data;
	struct unmarshal_data *unmarshaled;
	

420
	data=(struct anonymize_data *)(instance->internal_data);
's avatar
nits    
committed
421
422
423
424
	protocol=data->protocol;
	field_description=data->field;
	anonymization_function=data->function;
	marshalledBuffer=data->marshalledBuffer;
425
426
427

	data->first_anon_instance = fhlp_get_function_instance_byname(instance->hwinfo->gflist, fd, "ANONYMIZE");

's avatar
committed
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
	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]);
	}
}

////////////////////////////////
493
494
struct httpheader default_http_header;
void apply_function_to_field(int function,int protocol,int field,unsigned char *field_pointer,int len,unsigned char *header_pointer,int header_len,mapipacket *packet,struct anonymize_data *params);
's avatar
committed
495
496
497
498
499

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;
's avatar
committed
500
	unsigned short len=0,header_len=0;
501
	int i;
's avatar
committed
502
503
504
505
506
507
508
509
510
511
512
	
	if(!packet) {
		printf("WARNING: NULL packet\n");
		return;
	}

	if(function==UNCHANGED) 
		return;
	
	switch(protocol) {
		case IP:
513
514
			if (packet->iph) {
				switch(field) {
's avatar
committed
515
				case PAYLOAD:
516
517
					field_pointer=(unsigned char *)(packet->iph)+sizeof(IPHdr)+packet->ip_options_len;
					header_pointer=field_pointer;
's avatar
committed
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
					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;
548
				case FIELD_VERSION:
's avatar
committed
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
					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;
571
572
573
				}
			} else if (packet->ip6h) {
				switch(field) {
574
575
576
577
578
				case PAYLOAD:
					field_pointer = packet->ipdata;
					header_pointer = field_pointer;
					len = header_len = packet->ipdsize;
					break;
Stig Venaas's avatar
Stig Venaas committed
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
				case TOS: {
					/* not on byte boundary, so need to copy value into
					 * our own variable, and copy result back again */
					unsigned char ttl;
					unsigned char *flow = (unsigned char *)&packet->ip6h->ip6_flow;
					ttl = (flow[0] << 4) + (flow[1] >> 4);
					apply_function_to_field(function, protocol, field,
								&ttl, 1, header_pointer,
								header_len, packet, params);	
					flow[0] = (flow[0] & 0xf0) | (ttl >> 4);
					flow[1] = (flow[1] & 0xf) | (ttl << 4);
					return;
				}
				case FLOW: {
					/* not on byte boundary, so need to copy value into
					 * our own variable, and copy result back again
					 * last 20 bits are the flow label */
					unsigned int flow = ntohl(packet->ip6h->ip6_flow) & 0xfffff;
					apply_function_to_field(function, protocol, field,
								(unsigned char *)&flow, 4,
								header_pointer, header_len,
								packet, params);
					/* hopefully ok to ignore top 12 bits of anon result */
					packet->ip6h->ip6_flow = htonl((packet->ip6h->ip6_flow & 0xfff00000) | (flow & 0xfffff));
					return;
				}
Stig Venaas's avatar
Stig Venaas committed
605
606
607
608
				case TTL:
					field_pointer = (unsigned char *)&packet->ip6h->ip6_hlim;
					len = 1;
					break;
609
610
611
612
613
614
615
616
617
618
619
620
621
622
				case SRC_IP:
					field_pointer = (unsigned char *)&packet->ip6h->ip6_src;
					len = 16;
					break;
				case DST_IP:
					field_pointer = (unsigned char *)&packet->ip6h->ip6_dst;
					len = 16;
					break;
				default:
					break;
				}

			} else
				return;
's avatar
committed
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
			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:
692
693
					field_pointer=(unsigned char *)(packet->udph)+sizeof(UDPHdr); //maybe packet->data should be better
					header_pointer=field_pointer;
's avatar
committed
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
					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:
726
727
					field_pointer=(unsigned char *)(packet->icmph)+sizeof(ICMPHdr);
					header_pointer=field_pointer;
's avatar
committed
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
					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) {
751
				packet->upper_layer_protocol_headers[0]=&default_http_header;
's avatar
committed
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
				if(http_decode(packet,(struct httpheader *)packet->upper_layer_protocol_headers[0])==-1) {
					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	
775
776
				anonymize_field(IP,field,function,packet,params);
				return;
's avatar
committed
777
778
779
780
				break;
			}
			
			if(field>=SRC_PORT && field<=TCP_OPTIONS) {
781
782
				anonymize_field(TCP,field,function,packet,params);
				return;
's avatar
committed
783
784
				break;
			}
785
786
787
788
789
790
791
792
793
794
795
			
			for(i=0;i<h->pipeline_depth;i++) {
				switch(field) {
					case PAYLOAD:
						field_pointer=h->pointers_to_value[i][HTTP_PAYLOAD-BASE_HTTP_DEFS-1];
						header_pointer=h->pointers_to_value[i][HTTP_PAYLOAD-BASE_HTTP_DEFS-1];
						len=header_len=h->value_length[i][HTTP_PAYLOAD-BASE_HTTP_DEFS-1];
						break;
					default:
						field_pointer=h->pointers_to_value[i][field-BASE_HTTP_DEFS-1];
						len=h->value_length[i][field-BASE_HTTP_DEFS-1];
's avatar
committed
796
					
797
798
799
800
801
						header_pointer=h->pointers_to_header[i][field-BASE_HTTP_DEFS-1];
						header_len=h->header_length[i][field-BASE_HTTP_DEFS-1];
						break;
				}
				apply_function_to_field(function,protocol,field,field_pointer,len,header_pointer,header_len,packet,params);
's avatar
committed
802
			}
803
			return;
's avatar
committed
804
805
806
807
808
809
810
811
812
813
814
815
816
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
			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;
	}
	

865
866
867
868
869
	apply_function_to_field(function,protocol,field,field_pointer,len,header_pointer,header_len,packet,params);	
}

void apply_function_to_field(int function,int protocol,int field,unsigned char *field_pointer,int len,
	unsigned char *header_pointer,int header_len,mapipacket *packet,struct anonymize_data *params) {
's avatar
committed
870
871
	mapNode **mapTable;
	int *counter;
872
873
874
875
876
877
878
879
	unsigned char *packet_end;
	unsigned int total_len;
	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};
	int helper;
	
	if(!field_pointer) //a HTTP reply for example doesn't have URI
		return;
's avatar
committed
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
	
	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:
914
			prefix_preserving_anonymize_field(field_pointer, len);
's avatar
committed
915
			break;
's avatar
committed
916
917
918
		case PREFIX_PRESERVING_MAP:
			hide_addr(field_pointer);
			break;
's avatar
committed
919
920
		case STRIP:
			//printf("++I will call STRIP and I will keep %d bytes\n",params->seed);
921
922
923
924
925
926
927
			if (packet->iph) {
				total_len = ntohs(packet->iph->ip_len);
				packet_end = (unsigned char *)packet->iph + total_len;
			} else { /* assume IPv6 when not IPv4 */
				total_len = ntohs(packet->ip6h->ip6_plen) + IP6_HEADER_LEN;
				packet_end = (unsigned char *)packet->ip6h + total_len;
			}
's avatar
committed
928
929
930
931
			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);
Stig Venaas's avatar
Stig Venaas committed
932
933
934
935
936
937
938
			if (packet->iph) {
				total_len = ntohs(packet->iph->ip_len);
				packet_end = (unsigned char *)packet->iph + total_len;
			} else { /* assume IPv6 when not IPv4 */
				total_len = ntohs(packet->ip6h->ip6_plen) + IP6_HEADER_LEN;
				packet_end = (unsigned char *)packet->ip6h + total_len;
			}
's avatar
committed
939
940
941
942
			int donotreplace=0;
			if(field>=CHECKSUM &&  field<=CODE)
				donotreplace=1;
			switch(params->hash_algorithm) {
's avatar
committed
943
				case ANON_SHA:
's avatar
committed
944
945
					sha1_hash(field_pointer,len,params->padding_behavior,packet,total_len,packet_end,donotreplace);
					break;
's avatar
committed
946
				case ANON_MD5:
's avatar
committed
947
948
					md5_hash(field_pointer,len,params->padding_behavior,packet,total_len,packet_end,donotreplace);
					break;
's avatar
committed
949
				case ANON_CRC32:
's avatar
committed
950
951
					crc32_hash(field_pointer,len,params->padding_behavior,packet,total_len,packet_end,donotreplace);
					break;
's avatar
committed
952
				case ANON_SHA_2:
's avatar
committed
953
954
					sha256_hash(field_pointer,len,params->padding_behavior,packet,total_len,packet_end,donotreplace);
					break;
's avatar
committed
955
956
				case ANON_DES:
				case ANON_TRIPLEDES:
's avatar
committed
957
958
					des_hash(field_pointer,len,(unsigned char *)DES3_keys,params->padding_behavior,packet);
					break;
's avatar
committed
959
				case ANON_AES:
's avatar
committed
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
					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);
993
994
995
996
997
998
999
			if (packet->iph) {
				total_len = ntohs(packet->iph->ip_len);
				packet_end = (unsigned char *)packet->iph + total_len;
			} else { /* assume IPv6 when not IPv4 */
				total_len = ntohs(packet->ip6h->ip6_plen) + IP6_HEADER_LEN;
				packet_end = (unsigned char *)packet->ip6h + total_len;
			}
's avatar
committed
1000
			replace_field(field_pointer,len,(unsigned char *)params->pattern,strlen(params->pattern),packet,total_len,packet_end);
For faster browsing, not all history is shown. View entire blame