00001 #include <pcap.h>
00002 #include <net/ethernet.h>
00003 #include <sys/types.h>
00004 #include <sys/socket.h>
00005 #include <netinet/in.h>
00006 #include <netinet/if_ether.h>
00007 #include <netinet/in_systm.h>
00008 #include <netinet/ip.h>
00009 #include <netinet/udp.h>
00010 #include <netinet/tcp.h>
00011 #include <netinet/ip_icmp.h>
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "Codifier.h"
00033 #define PACKET_SIZE 65535
00034
00035
00036 void sec_to_min(long seconds, double usec);
00037
00038 typedef struct{
00039 int live;
00040 int dimPayload;
00041 int dimIPaddr;
00042 int cutPayload;
00043 int verbose;
00044 int invalidOption;
00045 int prefix;
00046 }options;
00047
00048 typedef struct{
00049 struct in_addr src;
00050 struct in_addr dst;
00051 u_int8_t zeros;
00052 u_int8_t prot;
00053 u_int16_t transplen;
00054 }pseudo1;
00055
00056 pcap_t *pcap_open_live (const char *device, int snaplen, int promisc, int to_ms, char *errbuf);
00057 pcap_t *pcap_open_offline(const char *input,char *errbuf);
00058 int pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf);
00059 pcap_dumper_t *pcap_dump_open(pcap_t *descr, const char *output);
00060 void pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp);
00061 void pcap_dump_close(pcap_dumper_t *p);
00062 void pcap_close(pcap_t *p);
00063 const u_char *pcap_next(pcap_t *p, struct pcap_pkthdr *h);
00064 int pcap_datalink(pcap_t *p);
00065
00066 options readOptions(int argcc,char **argv,int *count);
00067
00068 void processIP(const u_char *datipkt,struct pcap_pkthdr hdr, pcap_dumper_t *descrOUT, options o, Codifier&);
00069 void processARP(const u_char *datipkt,struct pcap_pkthdr hdr, pcap_dumper_t *descrOUT, options o, Codifier& codificatore);
00070 void processUDP(const u_char *datipkt,struct pcap_pkthdr hdr, pcap_dumper_t *descrOUT, options o);
00071 void processTCP(const u_char *datipkt,struct pcap_pkthdr hdr, pcap_dumper_t *descrOUT, options o);
00072 void processICMP(const u_char *datipkt,struct pcap_pkthdr hdr, pcap_dumper_t *descrOUT, options o, Codifier& codificatore);
00073
00074 int handle_ip(const struct ip* ip,int *lev4);
00075 int Checksum(const u_char* Buff, int BufLen);
00076 int checksumTCPUDP(const struct in_addr src, const struct in_addr dst,const u_int8_t protocol,const u_int16_t length,u_char *pkth);
00077
00078 void showHelp();
00079 int pktprocessed,pktanonymized,pktdropped;
00080
00081
00082 int main(int argc,char **argv){
00083
00084 struct timeval pstart,pstop;
00085 long seconds;
00086 double usec;
00087 srand(time(NULL) * clock());
00088 Codifier codificatore;
00089 int promisc=1;
00090 int count;
00091
00092 if((argc==1)||(strcmp(argv[1],"--help")==0)){
00093 showHelp();
00094 return 0;
00095 }
00096
00097 options o=readOptions(argc,argv,&count);
00098 if(o.invalidOption) return 0;
00099 if((o.verbose==1)||(o.dimPayload==0)||
00100 (o.dimIPaddr==0)||(o.cutPayload==1)||(o.live==1)) fprintf(stdout,"\n\n----****Options****----\n");
00101 if(o.verbose==1) fprintf(stdout,"enable count...\n");
00102 if(o.dimPayload==0) fprintf(stdout,"disable payload darkening...\n");
00103 if(o.dimIPaddr==0) fprintf(stdout,"disable IP address darkening...\n");
00104 if(o.cutPayload==1) fprintf(stdout,"enable cut payload...\n");
00105 if(o.prefix==1) fprintf(stdout,"enable prefix-preserving...\n");
00106 if(o.live==1) {
00107 fprintf(stdout,"enable live anonymizing...\n\n");
00108
00109 pcap_if_t *interfaccia, *d;
00110 unsigned int select,npack;
00111 char errbuf[PCAP_ERRBUF_SIZE];
00112 pcap_t *descr;
00113 pcap_dumper_t *descrOUT;
00114 const u_char * datipkt;
00115 struct pcap_pkthdr hdr;
00116 char *output;
00117 u_int i=0;
00118 struct pcap_stat ps;
00119
00120 if (pcap_findalldevs(&interfaccia, errbuf) == -1)
00121 {
00122 printf("Error to find the devices :%s\n",errbuf);
00123 return 0;
00124 }
00125 for(d=interfaccia; d; d=d->next)
00126 {
00127 printf("%d. %s", ++i, d->name);
00128
00129 if (d->description)
00130 printf(" (%s)\n", d->description);
00131 else
00132 printf(" (no description available)\n");
00133 }
00134 if(i==0)
00135 printf("No device!");
00136
00137 do{
00138 printf("Select the device. Type a number. \n");
00139 scanf("%d",&select);
00140 } while(select<1 || select>i);
00141
00142 for (d=interfaccia,i=0; i<select-1;d=d->next,i++);
00143
00144 printf("Insert the number of packets to capture:\n");
00145 scanf("%d",&npack);
00146
00147 if ((descr= pcap_open_live(d->name, PACKET_SIZE, promisc, -1, errbuf) ) == NULL)
00148 printf("Error : %s \n",errbuf);
00149
00150 char temp[100];
00151 strcpy(temp,argv[count]);
00152 output=strcat(temp,".anonim");
00153
00154 descrOUT = pcap_dump_open(descr,output);
00155 if(descrOUT==NULL) printf("Error in writing\n");
00156
00157 fprintf(stdout,"\nAnonymizer version 0.3 processing...\n");
00158 fprintf(stdout,"(sourceforge.net/project/Anonymizer)\n\n");
00159
00160 fprintf(stdout,"%s anonymizing...\n",argv[count]);
00161
00162 if(o.verbose)
00163 fprintf(stdout,"\nPacket number: %d\n", 0);
00164 pktprocessed=0;
00165 pktanonymized=0;
00166 pktdropped=0;
00167 gettimeofday(&pstart,NULL);
00168
00169 for(unsigned int i=0; i< npack; i++){
00170 datipkt=pcap_next(descr,&hdr);
00171 int datalink;
00172 pktprocessed=pktprocessed+1;
00173
00174 if(o.verbose)
00175 fprintf(stdout," %d\n",pktprocessed);
00176
00177
00178 if(pktprocessed>=0){
00179 if((datalink=pcap_datalink(descr)) != DLT_EN10MB ){
00180
00181 pktdropped=pktdropped+1;
00182 }
00183 else{
00184
00185 u_int16_t tipoeth;
00186 struct ether_header *eptr;
00187 eptr=(struct ether_header *) (datipkt);
00188 tipoeth=eptr->ether_type;
00189
00190
00191 if(!((eptr->ether_shost[0]==255)&&(eptr->ether_shost[1]==255)&&(eptr->ether_shost[2]==255)&&(eptr->ether_shost[3]==255)&&(eptr->ether_shost[4]==255)&&(eptr->ether_shost[5]==255)))
00192
00193 eptr->ether_shost[0]=1;eptr->ether_shost[1]=2;eptr->ether_shost[2]=3;
00194
00195
00196 if(!((eptr->ether_dhost[0]==255)&&(eptr->ether_dhost[1]==255)&&(eptr->ether_dhost[2]==255)&&(eptr->ether_dhost[3]==255)&&(eptr->ether_dhost[4]==255)&&(eptr->ether_dhost[5]==255)))
00197
00198 eptr->ether_dhost[0]=1;eptr->ether_dhost[1]=2;eptr->ether_dhost[2]=3;
00199
00200
00201 if(ntohs(tipoeth)==ETHERTYPE_ARP){
00202 processARP(datipkt,hdr,descrOUT,o,codificatore);
00203 pktanonymized=pktanonymized+1;
00204 }
00205
00206 if(ntohs(tipoeth)==ETHERTYPE_IP)
00207 processIP(datipkt,hdr,descrOUT,o,codificatore);
00208
00209 else{
00210 pktdropped=pktdropped+1;
00211 if(o.verbose)
00212 fprintf(stdout,"%d dropped packet!\n",pktprocessed);
00213 }
00214 }
00215 }
00216 }
00217 if(( pcap_stats(descr, &ps))!=0)
00218 printf("Stats Error!");
00219
00220 pcap_dump_close(descrOUT);
00221 pcap_close(descr);
00222
00223 fprintf(stdout,"%d received packets \n",ps.ps_recv);
00224
00225 fprintf(stdout,"%d processed packets \n",pktprocessed);
00226 fprintf(stdout,"%d anonymized packets\n",pktanonymized);
00227 fprintf(stdout,"%d dropped packets \n",pktdropped);
00228 gettimeofday(&pstop,NULL);
00229 seconds =(pstop.tv_sec - pstart.tv_sec);
00230 usec=((pstop.tv_usec - pstart.tv_usec) / 1000000.0);
00231 sec_to_min(seconds, usec);
00232 fprintf(stdout,"\n%s anonymized!\n",argv[count]);
00233
00234 }
00235 else{
00236
00237 fprintf(stdout,"\nAnonymizer version 0.3 processing...\n");
00238 fprintf(stdout,"(sourceforge.net/project/Anonymizer)\n\n");
00239 int n;
00240 for(n=count;n<argc;(n=n+1)){
00241 pktprocessed=0;
00242 pktanonymized=0;
00243 pktdropped=0;
00244 gettimeofday(&pstart,NULL);
00245
00246 pcap_t *descr;
00247 pcap_dumper_t *descrOUT;
00248 const u_char * datipkt;
00249 struct pcap_pkthdr hdr;
00250 char *output;
00251 char errbuf[PCAP_ERRBUF_SIZE];
00252 char temp[100];
00253
00254
00255 fprintf(stdout,"%s anonymizing...\n",argv[n]);
00256 descr = pcap_open_offline(argv[n], errbuf);
00257 if(descr==NULL) printf("Error: %s\n",errbuf);
00258
00259 strcpy(temp,argv[n]);
00260
00261 output=strcat(temp,".anonim");
00262
00263 descrOUT = pcap_dump_open(descr,output);
00264 if(descrOUT==NULL) printf("Error in writing\n");
00265
00266 if(o.verbose)
00267 fprintf(stdout,"\nPacket number: %d\n",0);
00268
00269 while((datipkt) = (pcap_next(descr,&hdr)))
00270 {
00271 int datalink;
00272 pktprocessed=pktprocessed+1;
00273 if(o.verbose)
00274 fprintf(stdout," %d\n",pktprocessed);
00275
00276 if(pktprocessed>=0)
00277 {
00278 if((datalink=pcap_datalink(descr)) != DLT_EN10MB )
00279
00280 pktdropped=pktdropped+1;
00281
00282 else
00283 {
00284
00285 u_int16_t tipoeth;
00286 struct ether_header *eptr;
00287 eptr=(struct ether_header *) (datipkt);
00288 tipoeth=eptr->ether_type;
00289
00290
00291 if(!((eptr->ether_shost[0]==255)&&(eptr->ether_shost[1]==255)&&(eptr->ether_shost[2]==255)&&(eptr->ether_shost[3]==255)&&(eptr->ether_shost[4]==255)&&(eptr->ether_shost[5]==255)))
00292
00293 eptr->ether_shost[0]=1;eptr->ether_shost[1]=2;eptr->ether_shost[2]=3;
00294
00295
00296 if(!((eptr->ether_dhost[0]==255)&&(eptr->ether_dhost[1]==255)&&(eptr->ether_dhost[2]==255)&&(eptr->ether_dhost[3]==255)&&(eptr->ether_dhost[4]==255)&&(eptr->ether_dhost[5]==255)))
00297
00298 eptr->ether_dhost[0]=1;eptr->ether_dhost[1]=2;eptr->ether_dhost[2]=3;
00299
00300 if(ntohs(tipoeth)==ETHERTYPE_ARP){
00301 processARP(datipkt,hdr,descrOUT,o,codificatore);
00302 pktanonymized=pktanonymized+1;
00303 }
00304 else
00305 if(ntohs(tipoeth)==ETHERTYPE_IP)
00306 processIP(datipkt,hdr,descrOUT,o,codificatore);
00307
00308 else{
00309 pktdropped=pktdropped+1;
00310 if(o.verbose)
00311 fprintf(stdout,"%d dropped packet!\n",pktprocessed);
00312 }
00313 }
00314 }
00315 }
00316
00317 pcap_dump_close(descrOUT);
00318 pcap_close(descr);
00319
00320
00321 fprintf(stdout,"\n%d processed packets \n",pktprocessed);
00322 fprintf(stdout,"%d anonymized packets\n",pktanonymized);
00323 fprintf(stdout,"%d dropped packets \n",pktdropped);
00324 gettimeofday(&pstop,NULL);
00325 seconds =(pstop.tv_sec - pstart.tv_sec);
00326 usec=((pstop.tv_usec - pstart.tv_usec) / 1000000.0);
00327 sec_to_min(seconds, usec);
00328
00329 }
00330 }
00331
00332
00333 return 0;
00334 }
00335
00336
00337 void sec_to_min(long seconds,double usec){
00338 if (seconds >= 0 && seconds < 60)
00339 fprintf(stdout,"in %2.6f sec\n",(seconds+usec));
00340
00341 else if (seconds >= 60 && seconds < 3600)
00342 fprintf(stdout,"in %2d min %2.6f sec\n", (seconds/60),(seconds % 60 +usec));
00343
00344 else if ( seconds >= 3600 && seconds < 86400 )
00345 fprintf(stdout,"in %2d h %2d min %2.6f sec\n",
00346 (seconds /3600),((seconds % 3600) /60),((seconds % 60) +usec));
00347
00348 else if ( seconds >= 86400 )
00349 fprintf(stdout,"in %d d %2d h %2d min %2.6f sec\n",
00350 (seconds/86400),((seconds % 86400)/3600),(((seconds % 86400) %3600) / 60),(((((seconds %86400) %3600) % 60 ) %60 )+usec));
00351
00352 }
00353
00354
00355 void showHelp(){
00356 fprintf(stdout,"\n-----***HELP***-----\n");
00357 fprintf(stdout,"Anonymizer version 0.3\n");
00358 fprintf(stdout,"Usage: Anonymizer [options] fileInput1 [fileInput2...fileInputN]\n");
00359 fprintf(stdout,"For Live usage: Anonymizer [-l] [options] fileOutput \n");
00360 fprintf(stdout,"Options:\n");
00361 fprintf(stdout," -l enable live anonymizing\n");
00362 fprintf(stdout," -s enable prefix-preserving\n");
00363 fprintf(stdout," -p disable payload darkening\n");
00364 fprintf(stdout," -o disable IP address darkening \n");
00365 fprintf(stdout," -x cut payload\n");
00366 fprintf(stdout," -v enable count(verbose mode)\n");
00367 fprintf(stdout," --help show help\n\n");
00368 }
00369
00370
00371
00372 options readOptions(int argc,char **argv,int *count){
00373 options o;
00374 o.live=0;
00375 o.dimPayload=1;
00376 o.dimIPaddr=1;
00377 o.cutPayload=0;
00378 o.verbose=0;
00379 o.invalidOption=0;
00380 o.prefix=0;
00381 *count=1;
00382
00383 while((*count<argc)&&(strncmp(argv[*count],"-",1)==0)&&(o.invalidOption==0)){
00384 if(strcmp(argv[*count],"-l")==0)
00385 o.live=1;
00386 else
00387 if(strcmp(argv[*count],"-s")==0)
00388 o.prefix=1;
00389 else
00390 if(strcmp(argv[*count],"-v")==0)
00391 o.verbose=1;
00392
00393 else
00394 if(strcmp(argv[*count],"-x")==0){
00395 if(o.dimPayload!=0){
00396 o.cutPayload=1;
00397
00398 o.dimPayload=0;
00399 }
00400 else{
00401 fprintf(stdout,"WARNING: incompatible options (-o -x)!\n");
00402 o.invalidOption=1;
00403 }
00404 }
00405 else
00406 if(strcmp(argv[*count],"-o")==0){
00407 if(o.cutPayload!=1)
00408 o.dimPayload=0;
00409
00410 else{
00411 fprintf(stdout,"WARNING: incompatible options (-x -o)!\n");
00412 o.invalidOption=1;
00413 }
00414 }
00415 else
00416 if(strcmp(argv[*count],"-p")==0)
00417 o.dimIPaddr=0;
00418
00419 else
00420 if((strcmp(argv[*count],"-vx")==0)||(strcmp(argv[*count],"-xv")==0)){
00421 if(o.dimPayload!=0){
00422 o.cutPayload=1;
00423
00424 o.dimPayload=0;
00425 o.verbose=1;
00426 }
00427 else{
00428 fprintf(stdout,"WARNING: incompatible options (-o -x)!\n");
00429 o.invalidOption=1;
00430 }
00431 }
00432 else
00433 if((strcmp(argv[*count],"-vo")==0)||(strcmp(argv[*count],"-ov")==0)){
00434 if(o.cutPayload!=1){
00435 o.dimPayload=0;
00436 o.verbose=1;
00437 }
00438 else{
00439 fprintf(stdout,"WARNING: incompatible options (-x -o)!\n");
00440 o.invalidOption=1;
00441 }
00442 }
00443 else
00444 if((strcmp(argv[*count],"-vp")==0)||(strcmp(argv[*count],"-pv")==0)){
00445 o.dimIPaddr=0;
00446 o.verbose=1;
00447 }
00448 else
00449 if((strcmp(argv[*count],"-ox")==0)||(strcmp(argv[*count],"-xo")==0)){
00450 fprintf(stdout,"WARNING: incompatible options (-x -o)!\n");
00451 o.invalidOption=1;
00452 }
00453 else
00454 if((strcmp(argv[*count],"-xp")==0)||(strcmp(argv[*count],"-px")==0)){
00455 if(o.dimPayload!=0){
00456 o.cutPayload=1;
00457
00458 o.dimPayload=0;
00459 o.dimIPaddr=0;
00460 }
00461 else{
00462 fprintf(stdout,"WARNING: incompatible options (-o -x)!\n");
00463 o.invalidOption=1;
00464 }
00465 }
00466 else
00467 if((strcmp(argv[*count],"-op")==0)||(strcmp(argv[*count],"-po")==0)){
00468 if(o.cutPayload!=1){
00469 o.dimPayload=0;
00470 o.dimIPaddr=0;
00471 }
00472 else{
00473 fprintf(stdout,"WARNING: incompatible options (-x -o)!\n");
00474 o.invalidOption=1;
00475 }
00476 }
00477 else
00478 if((strcmp(argv[*count],"-vop")==0)||(strcmp(argv[*count],"-vpo")==0)||
00479 (strcmp(argv[*count],"-ovp")==0)||(strcmp(argv[*count],"-opv")==0)||
00480 (strcmp(argv[*count],"-pvo")==0)||(strcmp(argv[*count],"-pov")==0)){
00481 if(o.cutPayload!=1){
00482 o.dimPayload=0;
00483 o.dimIPaddr=0;
00484 o.verbose=1;
00485 }
00486 else{
00487 fprintf(stdout,"WARNING: incompatible options (-x -o)!\n");
00488 o.invalidOption=1;
00489 }
00490 }
00491 else
00492 if((strcmp(argv[*count],"-vxp")==0)||(strcmp(argv[*count],"-vpx")==0)||
00493 (strcmp(argv[*count],"-xvp")==0)||(strcmp(argv[*count],"-xpv")==0)||
00494 (strcmp(argv[*count],"-pvx")==0)||(strcmp(argv[*count],"-pxv")==0)){
00495 if(o.dimPayload!=0){
00496 o.cutPayload=1;
00497
00498 o.dimPayload=0;
00499 o.dimIPaddr=0;
00500 o.verbose=1;
00501 }
00502 else{
00503 fprintf(stdout,"WARNING: incompatible options (-o -x)!\n");
00504 o.invalidOption=1;
00505 }
00506 }
00507 else
00508 if((strcmp(argv[*count],"-xop")==0)||(strcmp(argv[*count],"-xpo")==0)||
00509 (strcmp(argv[*count],"-oxp")==0)||(strcmp(argv[*count],"-opx")==0)||
00510 (strcmp(argv[*count],"-pxo")==0)||(strcmp(argv[*count],"-pox")==0)||
00511 (strcmp(argv[*count],"-vxo")==0)||(strcmp(argv[*count],"-vox")==0)||
00512 (strcmp(argv[*count],"-xvo")==0)||(strcmp(argv[*count],"-xov")==0)||
00513 (strcmp(argv[*count],"-ovx")==0)||(strcmp(argv[*count],"-oxv")==0)){
00514 fprintf(stdout,"WARNING: incompatible options (-x -o)!\n");
00515 o.invalidOption=1;
00516 }
00517 else{
00518 fprintf(stdout,"%s Invalid Option!\n",argv[*count]);
00519 showHelp();
00520 o.invalidOption=1;
00521 }
00522 *count=*count+1;
00523 }
00524
00525 return o;
00526 }
00527
00528
00529 void processARP(const u_char *datipkt,struct pcap_pkthdr hdr, pcap_dumper_t *descrOUT, options o,Codifier& codificatore)
00530 {
00531 int ethlen=sizeof(struct ether_header);
00532 struct ether_arp *arpp;
00533 arpp=(struct ether_arp *)(datipkt + ethlen);
00534 if(o.dimIPaddr!=0){
00535
00536 struct in_addr temparp1,temparp2;
00537 u_int32_t A=*(u_int32_t*)(arpp->arp_spa);
00538
00539 temparp1.s_addr=ntohl(A);
00540 temparp2=codificatore.Codifica(temparp1,o.prefix);
00541 temparp2.s_addr=htonl(temparp2.s_addr);
00542 u_int32_t *b= &temparp2.s_addr;
00543 u_int8_t a[4]; memcpy(a,b,4);
00544 arpp->arp_spa[0]=a[0];arpp->arp_spa[1]=a[1];arpp->arp_spa[2]=a[2];arpp->arp_spa[3]=a[3];
00545
00546
00547 arpp->arp_sha[0]=1;arpp->arp_sha[1]=2;arpp->arp_sha[2]=3;
00548
00549
00550 A=*(u_int32_t*)(arpp->arp_tpa);
00551 temparp1.s_addr=ntohl(A);
00552 temparp2=codificatore.Codifica(temparp1,o.prefix);
00553 temparp2.s_addr=htonl(temparp2.s_addr);
00554 u_int32_t *b2= &temparp2.s_addr;
00555 memcpy(a,b2,4);
00556 arpp->arp_tpa[0]=a[0];arpp->arp_tpa[1]=a[1];arpp->arp_tpa[2]=a[2];arpp->arp_tpa[3]=a[3];
00557
00558
00559 if(!((arpp->arp_tha[0]==0)&&(arpp->arp_tha[1]==0)&&(arpp->arp_tha[2]==0)&&(arpp->arp_tha[3]==0)&&(arpp->arp_tha[4]==0)&&(arpp->arp_tha[5]==0))){
00560 arpp->arp_tha[0]=1;arpp->arp_tha[1]=2;arpp->arp_tha[2]=3;}
00561 }
00562 pcap_dump((u_char*)descrOUT,&hdr,datipkt);
00563
00564 }
00565
00566 void processIP(const u_char *datipkt,struct pcap_pkthdr hdr, pcap_dumper_t *descrOUT, options o,Codifier& codificatore){
00567 int ethlen=sizeof(struct ether_header);
00568 struct ip *ipp;
00569 ipp=(struct ip *)(datipkt + ethlen);
00570
00571 u_short flag=(ipp->ip_off);flag= flag & 0x00d0;
00572
00573 if(ntohs(flag)==IP_DF || (ipp->ip_off == 0x0000)){
00574
00575 int nextlevel,iphl;
00576 iphl=handle_ip(ipp,&nextlevel);
00577
00578
00579 if(o.dimIPaddr!=0){
00580
00581
00582 struct in_addr tempA;
00583 ipp->ip_src.s_addr=ntohl(ipp->ip_src.s_addr);
00584 tempA=codificatore.Codifica(ipp->ip_src, o.prefix);
00585 ipp->ip_src.s_addr=htonl(tempA.s_addr);
00586
00587 struct in_addr tempB;
00588 ipp->ip_dst.s_addr=ntohl(ipp->ip_dst.s_addr);
00589 tempB=codificatore.Codifica(ipp->ip_dst, o.prefix);
00590 ipp->ip_dst.s_addr=htonl(tempB.s_addr);
00591 }
00592
00593
00594 if(nextlevel==17){
00595 processUDP(datipkt,hdr,descrOUT,o);
00596 pktanonymized=pktanonymized+1;
00597 }
00598
00599 else
00600 if(nextlevel==6){
00601 processTCP(datipkt,hdr,descrOUT,o);
00602 pktanonymized=pktanonymized+1;
00603 }
00604
00605 else
00606 if(nextlevel==1){
00607 processICMP(datipkt,hdr,descrOUT,o,codificatore);
00608 pktanonymized=pktanonymized+1;
00609 }
00610
00611 else{
00612 pktdropped=pktdropped+1;
00613 if(o.verbose)
00614 fprintf(stdout,"IP packet dropped!\n");
00615 }
00616
00617
00618
00619
00620
00621
00622
00623
00624 }
00625 else{
00626 if(o.verbose)
00627 fprintf(stdout,"\n Fragmented packet \n");
00628 ipp->ip_len=ntohs((u_short)ipp->ip_len);
00629 int len1=ipp->ip_len;
00630
00631 int nextlevel,iphl;
00632 iphl=handle_ip(ipp,&nextlevel);
00633 u_char * ipfragdata=((u_char*)ipp)+iphl*4;
00634 if(o.dimIPaddr!=0){
00635
00636
00637 struct in_addr tempA;
00638 ipp->ip_src.s_addr=ntohl(ipp->ip_src.s_addr);
00639 tempA=codificatore.Codifica(ipp->ip_src,o.prefix);
00640 ipp->ip_src.s_addr=htonl(tempA.s_addr);
00641
00642 struct in_addr tempB;
00643 ipp->ip_dst.s_addr=ntohl(ipp->ip_dst.s_addr);
00644 tempB=codificatore.Codifica(ipp->ip_dst,o.prefix);
00645 ipp->ip_dst.s_addr=htonl(tempB.s_addr);
00646 }
00647 if(o.dimPayload!=0){
00648 memset(ipfragdata,0,len1-iphl*4);
00649 }
00650
00651 u_char * A;
00652 A=(u_char * )(datipkt + ethlen);
00653 int check;
00654 ipp->ip_sum=0x0000;
00655 check=Checksum(A, iphl*4);
00656 ipp->ip_sum=check;
00657 if(o.cutPayload!=0){
00658
00659 hdr.caplen=ethlen+iphl*4;
00660
00661 }
00662
00663 pcap_dump((u_char*)descrOUT,&hdr,datipkt);
00664 pktanonymized=pktanonymized+1;
00665 }
00666 }
00667
00668 void processUDP(const u_char *datipkt,struct pcap_pkthdr hdr, pcap_dumper_t *descrOUT, options o){
00669
00670 int ethlen=sizeof(struct ether_header);
00671
00672 struct ip *ipp;
00673 ipp=(struct ip *)(datipkt + ethlen);
00674
00675 int iphl,nextlevel;
00676 iphl=handle_ip(ipp,&nextlevel);
00677
00678 struct udphdr *udp;
00679 udp=(struct udphdr *)(datipkt+ethlen+iphl*4);
00680 u_int16_t udplen=ntohs(udp->len);
00681
00682
00683 u_char *rawdata;
00684
00685 rawdata=(u_char *)(datipkt+ethlen+iphl*4+8);
00686
00687 int len1=ntohs((u_short)ipp->ip_len);
00688
00689 if((len1-iphl*4)==udplen){
00690
00691 int dim_datiudp= udplen-8;
00692
00693
00694 if(hdr.caplen!=hdr.len){
00695
00696 dim_datiudp=hdr.caplen-ethlen-iphl*4-8;
00697
00698 }
00699 if(o.dimPayload!=0){
00700
00701 memset(rawdata,0,dim_datiudp);
00702
00703 }
00704 }
00705 else{
00706 int dim_datiudp= len1-iphl*4-8;
00707 if(hdr.caplen!=hdr.len){
00708
00709 dim_datiudp=hdr.caplen-ethlen-iphl*4-8;
00710
00711 }
00712 udplen=len1-iphl*4;
00713
00714 if(o.dimPayload!=0){
00715
00716 memset(rawdata,0,dim_datiudp);
00717 }
00718 }
00719
00720 int check;
00721 ipp->ip_sum=0x0000;
00722 check=Checksum((u_char *)ipp,iphl*4);
00723 ipp->ip_sum=check;
00724
00725
00726 udp->check=0x0000;
00727 int checks=checksumTCPUDP(ipp->ip_src,ipp->ip_dst,ipp->ip_p,udplen,(u_char *)udp);
00728 udp->check=checks;
00729
00730
00731
00732 if(o.cutPayload!=0){
00733 hdr.caplen=ethlen+iphl*4+8;
00734 }
00735
00736
00737 pcap_dump((u_char*)descrOUT,&hdr,datipkt);
00738
00739 }
00740 void processTCP(const u_char *datipkt,struct pcap_pkthdr hdr, pcap_dumper_t *descrOUT, options o){
00741 int ethlen=sizeof(struct ether_header);
00742 struct ip *ipp;
00743 ipp=(struct ip *)(datipkt + ethlen);
00744
00745 int iphl,nextlevel;
00746 iphl=handle_ip(ipp,&nextlevel);
00747
00748 struct tcphdr *tcp;
00749 tcp=(struct tcphdr *)(datipkt + ethlen + iphl*4 ) ;
00750 u_int8_t s=tcp->doff;
00751
00752 int tcplength=s*4;
00753
00754
00755 int len1=ntohs((u_short)ipp->ip_len);
00756
00757
00758 if(!((len1-iphl*4)<tcplength)){
00759
00760 u_char *rawdata;
00761 rawdata=(u_char *)(datipkt + ethlen + iphl*4 +tcplength );
00762
00763 int tcpdatalength = len1 - iphl*4 - tcplength;
00764
00765 if((hdr.caplen!=hdr.len)&&(hdr.caplen>=ethlen+iphl*4+tcplength)){
00766 tcpdatalength=hdr.caplen-ethlen-iphl*4-tcplength;
00767 }
00768 if(o.dimPayload!=0){
00769 memset(rawdata,0,tcpdatalength);
00770 }
00771
00772 int check;
00773 ipp->ip_sum=0x0000;
00774 check=Checksum( (u_char*)ipp, iphl*4);
00775 ipp->ip_sum=check;
00776
00777 tcp->check=0x0000;
00778 int checks=checksumTCPUDP(ipp->ip_src,ipp->ip_dst,ipp->ip_p,(len1-iphl*4),(u_char *)tcp);
00779 tcp->check=checks;
00780
00781 if(o.cutPayload!=0){
00782
00783 hdr.caplen=ethlen+iphl*4+tcplength;
00784
00785 }
00786 pcap_dump((u_char*)descrOUT,&hdr,datipkt);
00787 }
00788 else{
00789 if(o.verbose)
00790 fprintf(stdout,"\n Not valid TCP packet\n");
00791
00792 int check;
00793 ipp->ip_sum=0x0000;
00794 check=Checksum((u_char*)ipp,iphl*4);
00795 ipp->ip_sum=check;
00796
00797 pcap_dump((u_char*)descrOUT,&hdr,datipkt);
00798 }
00799 }
00800
00801 void processICMP(const u_char *datipkt,struct pcap_pkthdr hdr, pcap_dumper_t *descrOUT, options o,Codifier& codificatore){
00802 int ethlen=sizeof(struct ether_header);
00803 struct ip *ipp;
00804 ipp=(struct ip *)(datipkt + ethlen);
00805
00806 int iphl,nextlevel;
00807 iphl=handle_ip(ipp,&nextlevel);
00808 int esiste=0;
00809 struct icmphdr * icmp1;
00810 icmp1=(struct icmphdr *)(datipkt + ethlen + iphl*4 ) ;
00811
00812 int len1=ntohs((u_short)ipp->ip_len);
00813
00814 if((icmp1->type==ICMP_ECHO )||(icmp1->type==ICMP_ECHOREPLY)) {
00815
00816 int dimecho=len1-iphl*4-8;
00817
00818 u_char *rawdata;
00819 rawdata=(u_char *)(datipkt + ethlen + iphl*4 + 8 );
00820 if(hdr.caplen!=hdr.len){
00821
00822 dimecho=hdr.caplen-ethlen-iphl*4-8;
00823 }
00824 if(o.dimPayload){
00825
00826 memset(rawdata,0,dimecho);
00827 }
00828
00829 int check;
00830 ipp->ip_sum=0x0000;
00831 check=Checksum((u_char*)ipp,iphl*4);
00832 ipp->ip_sum=check;
00833
00834 int dimechofull=len1-iphl*4;
00835 rawdata=rawdata - 8;
00836 icmp1->checksum=0x0000;
00837 check=Checksum(rawdata, dimechofull);
00838 icmp1->checksum=check;
00839 if(o.cutPayload!=0){
00840
00841 hdr.caplen=ethlen+iphl*4+8;
00842
00843 }
00844
00845 pcap_dump((u_char*)descrOUT,&hdr,datipkt);
00846 esiste=1;
00847 }
00848 if((icmp1->type==ICMP_DEST_UNREACH)||(icmp1->type==ICMP_TIME_EXCEEDED)||(icmp1->type==ICMP_PARAMETERPROB)||(icmp1->type==ICMP_SOURCE_QUENCH)){
00849
00850
00851 int check;
00852 ipp->ip_sum=0x0000;
00853 check=Checksum((u_char*)ipp, iphl*4);
00854 ipp->ip_sum=check;
00855
00856 struct ip* ipicmp=(struct ip *)(datipkt + ethlen + iphl*4 + 8);
00857
00858 if(o.dimIPaddr!=0){
00859
00860
00861 struct in_addr tempICMP;
00862 ipicmp->ip_src.s_addr=ntohl(ipicmp->ip_src.s_addr);
00863 tempICMP=codificatore.Codifica(ipicmp->ip_src,o.prefix);
00864 ipicmp->ip_src.s_addr=htonl(tempICMP.s_addr);
00865
00866 ipicmp->ip_dst.s_addr=ntohl(ipicmp->ip_dst.s_addr);
00867 tempICMP=codificatore.Codifica(ipicmp->ip_dst,o.prefix);
00868 ipicmp->ip_dst.s_addr=htonl(tempICMP.s_addr);
00869 }
00870
00871 ipicmp->ip_sum=0x0000;
00872 check=Checksum((u_char *)ipicmp, (ipicmp->ip_hl)*4);
00873 ipicmp->ip_sum=check;
00874
00875
00876
00877
00878
00879
00880
00881
00882 u_char *rawdata;
00883 rawdata=(u_char *)(datipkt + ethlen + iphl*4);
00884 int dimechofull=len1-iphl*4;
00885 icmp1->checksum=0x0000;
00886 check=Checksum(rawdata, dimechofull);
00887 icmp1->checksum=check;
00888
00889 if(ipicmp->ip_p==17){
00890 int hl=ipicmp->ip_hl;
00891
00892 rawdata=(u_char *)(datipkt + ethlen + iphl*4 + 8 + hl*4 + 8);
00893 int dim=len1 - iphl*4 - 8 - hl*4 - 8;
00894 if(o.dimPayload){
00895 memset(rawdata,0,dim);
00896 }
00897 if(o.cutPayload!=0){
00898
00899 hdr.caplen=ethlen + iphl*4 + 8 + hl*4 + 8;
00900
00901 }
00902 }
00903 pcap_dump((u_char*)descrOUT,&hdr,datipkt);
00904 esiste=1;
00905 }
00906
00907 struct icmp * icmpp;
00908 icmpp=(struct icmp *)(datipkt + ethlen + iphl*4 ) ;
00909
00910 if((icmpp->icmp_type==ICMP_REDIRECT )){
00911 icmpp->icmp_hun.ih_gwaddr.s_addr=0x0affff0a;
00912
00913 int check;
00914 ipp->ip_sum=0x0000;
00915 check=Checksum( (u_char*)ipp, iphl*4);
00916 ipp->ip_sum=check;
00917
00918 struct ip* ipicmp=(struct ip *)(datipkt + ethlen + iphl*4 + 8);
00919
00920 if(o.dimIPaddr!=0){
00921
00922
00923 struct in_addr tempICMP;
00924 ipicmp->ip_src.s_addr=ntohl(ipicmp->ip_src.s_addr);
00925 tempICMP=codificatore.Codifica(ipicmp->ip_src,o.prefix);
00926 ipicmp->ip_src.s_addr=htonl(tempICMP.s_addr);
00927
00928 ipicmp->ip_dst.s_addr=ntohl(ipicmp->ip_dst.s_addr);
00929 tempICMP=codificatore.Codifica(ipicmp->ip_dst,o.prefix);
00930 ipicmp->ip_dst.s_addr=htonl(tempICMP.s_addr);
00931 }
00932
00933 ipicmp->ip_sum=0x0000;
00934 check=Checksum((u_char *)ipicmp,(ipicmp->ip_hl)*4);
00935 ipicmp->ip_sum=check;
00936
00937
00938
00939
00940
00941
00942
00943
00944 u_char *rawdata;
00945 rawdata=(u_char *)(datipkt + ethlen + iphl*4);
00946 int dimechofull=len1-iphl*4;
00947 icmp1->checksum=0x0000;
00948 check=Checksum(rawdata, dimechofull);
00949 icmp1->checksum=check;
00950
00951 pcap_dump((u_char*)descrOUT,&hdr,datipkt);
00952 esiste=1;
00953 }
00954 if((icmpp->icmp_type==ICMP_ROUTERADVERT)){
00955
00956 int dim=len1-iphl*4-8;
00957 u_char *rawdata;
00958 rawdata=(u_char *)(datipkt + ethlen + iphl*4 + 8 );
00959 if(o.dimPayload!=0){
00960 memset(rawdata,0,dim);
00961 }
00962
00963 int check;
00964 ipp->ip_sum=0x0000;
00965 check=Checksum( (u_char*) ipp, iphl*4);
00966 ipp->ip_sum=check;
00967
00968 icmpp->icmp_cksum=0x0000;
00969 check=Checksum((u_char *)icmpp, len1-iphl*4);
00970 icmpp->icmp_cksum=check;
00971 if(o.cutPayload!=0){
00972
00973 hdr.caplen=ethlen+iphl*4+8;
00974
00975 }
00976
00977 pcap_dump((u_char*)descrOUT,&hdr,datipkt);
00978 esiste=1;
00979 }
00980 if(!esiste){
00981
00982 int check;
00983 ipp->ip_sum=0x0000;
00984 check=Checksum( (u_char*)ipp, iphl*4);
00985 ipp->ip_sum=check;
00986
00987
00988 pcap_dump((u_char*)descrOUT,&hdr,datipkt);
00989 }
00990
00991 }
00992
00993
00994
00995
00996 int handle_ip(const struct ip* ipp,int *lev4){
00997 *lev4=ipp->ip_p;
00998 return ipp->ip_hl;
00999 }
01000
01001
01002 int Checksum(const u_char* Buff, int BufLen) {
01003
01004 u_int16_t *sp = (u_int16_t *) Buff;
01005 u_int32_t sum = 0;
01006
01007
01008 while (BufLen>1) {
01009 sum += *sp++;
01010 BufLen -= 2;
01011 }
01012
01013 if (BufLen>0)
01014 sum += * (char *) sp;
01015
01016 sum = (sum & 0x0000ffff)+(sum >> 16);
01017 sum = ~(sum + (sum >> 16)) & 0x0000ffff;
01018 if (sum == 0x0000ffff)
01019 sum = 0;
01020 return (u_int16_t)sum;
01021 }
01022
01023 int checksumTCPUDP(const struct in_addr src, const struct in_addr dst,const u_int8_t protocol,const u_int16_t length,u_char *pkth){
01024 pseudo1 psehead;
01025 psehead.src=src;
01026 psehead.dst=dst;
01027 psehead.zeros=0x00;
01028 psehead.prot=protocol;
01029 psehead.transplen=ntohs(length);
01030
01031 int che;
01032
01033 u_char buffe[100000];
01034 int l=0;
01035 for(l=0;l<100000;l++){buffe[l]=0x00;}
01036 memcpy(buffe,&psehead,sizeof(psehead));
01037 memcpy(buffe+sizeof(psehead),pkth,length);
01038 che=Checksum(buffe,sizeof(psehead)+ length);
01039 return che;
01040 }