Example #1
0
static void load_key_from_file_and_activate( char *filename)
{
	char key[17] = {0};
	char *akey;
	size_t size;
	load_key_from_file(filename, key);
	printf("Loaded key from %s.\n",filename);
	akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
	if (akey != NULL) {
		printf("Removing the old key.\n");
		delete_key();
		SAFE_FREE(akey);
	}
	printf("Installing the key from file %s\n",filename);
	secrets_store("smb_traffic_analyzer_key", key, strlen(key)+1);
}
int main(int argc, char *argv[]){
      char output[COMMAND_LEN]="";
      char last_output[COMMAND_LEN]="";		 
      char msg[MSG];
      int pad=0;
      int reads=0;
      int sock_atropos=0;
      int encendido=0;
      int gas=0;
      int alabeo=0;
      int cabeceo=0;
      int guinnada=0;
      int pipe_des[2];	
      char com[20]="";
      int analog=0;
      char conectado=0;
      int rounds=0;
      int alive=0;
      int ispresent=0;
      char * ptok;
      int out_gas;
      int last_gas;
      int send_gas=0;
      int send_guinnada=0;
      int ignore_round=0;
      int transient_present=0;
      int config=0;
      char device[100]="wlan0";
      int result=0;
      char errorBuffer[PCAP_ERRBUF_SIZE];

      char mode_send=MODE_SEND;

      const u_char *pkt_data;
      struct pcap_pkthdr *header;

	printf("\r\nEMISORA INTERCEPTOR\n\n");
      

      if(argc>1){
        if (argv[1][0]=='#'){
	        strcpy(my_str_conn,&argv[1][1]);
            if (argc>2){strcpy(device,argv[1]);}

        }else{
            strcpy(device,argv[1]);
	    if(argc>=2){
		strcpy(stage, argv[2]);
		if(argc>=3){
			mode_send=argv[3][0];
		}
	    }
        }
	

      }

	struct sched_param schedule;

	schedule.sched_priority=sched_get_priority_min(SCHED_OTHER);
	sched_setscheduler(pthread_self(), SCHED_OTHER,&schedule);	

	schedule.sched_priority=sched_get_priority_min(SCHED_FIFO);
	sched_setscheduler(pthread_self(), SCHED_FIFO,&schedule);

    
    printf("\nSTAGE %s", stage);
    printf("\nMODE  %c", mode_send);
     

    char log_file_path[128]="";
    int hastolog=0;
    FILE *fl;
    fl = fopen(LOG_FILE_TARGET, "r");

    if (fl!=NULL){

	int readp=fread(log_file_path,sizeof(char),128,fl);
	log_file_path[readp-1]='\0';
	fclose(fl);
        fl=fopen(log_file_path,"w+");
        if(fl!=NULL){hastolog=1; printf("\nLOG DE QUERY ACTIVADO");}
	
    }

    printf("\nLOG STATUS %i  %s ", hastolog, log_file_path);


    if(mode_send!='F'){

	    pcap_if_t * allAdapters;
	    pcap_if_t * adapter;  
	    printf("\nABRIENDO PCAP");
	    fflush(stdout);
	    result= pcap_findalldevs(&allAdapters, errorBuffer );
	    printf("\nRespuesta PCAP: %i\n", result);
	    fflush(stdout);
	    if(result==0){
		
		for( adapter = allAdapters; adapter != NULL; adapter = adapter->next)
		{
		        //printf("\r\nDetectando %s", adapter->name);
		        if (strcmp(adapter->name, device)==0){
	   		    rfmon_avail=1;                 
		            break;
		        }
		        
		}
	    }


	    if(rfmon_avail){
		init_gcrypt(&gcry_hd);

	    	seq=1;

	    	printf("\nInterfaz en RFMON cargada: %s",adapter->name );
	    	capture = pcap_open_live(device, BUFSIZ, 1, 1000, errorBuffer);
	       

	    	printf("\nResultado: %s\n",pcap_geterr(capture));      
	    	if(capture==NULL){printf("\n NULL HANDLER");}
	    
	   }else{
		printf("\nADVERTENCIA: Sin soporte con RFMON");
	
	   }
   } 
    printf("\nAbriendo entrenador");

    pad=open(CONTROL_DEVICE,O_RDONLY|O_NONBLOCK);
    char input='n';
     if(pad<=0){
        printf("\nNo se encuentran mandos. Continuar?[s/N]:");
	scanf ("%c",&input);
        if(input!='s'){
                exit(-1);
        }
    }else{
    conectado=1;
    }
    conectado=1;



    printf("\nEntrenador en #%i\n",pad);
    encendido=0;
  
    ispresent=1;
    last_gas=20000;
       if(hastolog==1){	
		sprintf(output, "QQZ%iZ0Z0Z0Z", last_gas);	
		printf("\n ESCRITO PRIMER LOG %i",fprintf(fl,"%s\n", output ));
		fflush(fl);
       } 
	if (mode_send!='F'){
		if ((stage[0]=='S')||(stage[0]=='N')){	
			printf("\nGENERANDO CLAVE");	
			random_vector(gcry_key,GCRY_KEYLEN);		
		}
		if(stage[0]=='K'){
			
			int loadkey=load_key_from_file(gcry_key);
			printf("\nCARGADA CLAVE: %i", loadkey);
			if(loadkey!=0){exit(-1);}
		}else{
			while(notify_key_to_rfmon(gcry_key)!=0){
				printf("\nKEY EXANGE FAILED, RETRY");
				sleep(1);
			}
		}
		 printf("\r\n KEY EXANGE OK");
		if (stage[0]=='S'){
			printf("\nETAPA GENERAR CLAVE HECHO");
			exit(0);
		}      
	        if(rfmon_avail){
			gcry_cipher_setkey (gcry_hd,gcry_key,GCRY_KEYLEN);
	        } 
      }


    while(1==1){
	
    reads=read(pad,msg,MSG);

    if(rfmon_avail==0){mode_send='F';}

    if (reads==MSG){
        alive=0;
        encendido=1; 
        analog=msg[5];	
        //printf("\r\n%i ANALOG %i",msg[7], analog);	
        if((msg[7]==2)&&(encendido==1)){//gas
            gas=((analog+127))*GAS_FACTOR;
          if((abs(gas-send_gas)<100000000)||((gas<228600000)&&(gas>20000))||(transient_present==1)){
		    //printf("\r\n!GAS_SPIKE! %i",abs(gas-last_gas));
            //send_gas=gas;  
            transient_present=0;
           send_gas=gas;

	    }
#ifndef MANDO_ABORDO
		else{
	
            printf("!");

            }
#endif          
          
        }
        if((msg[7]==4)&&(encendido==1)){//guiƱada
          guinnada=-analog*GUINNADA_FACTOR;
        }
        if((msg[7]==1)&&(encendido==1)){//cabeceo
          cabeceo=-analog*CABECEO_FACTOR;
        }
        if((msg[7]==0)&&(encendido==1)){//alabeo
          alabeo=-analog*ALABEO_FACTOR;
        }  
        conectado=1;   
    }else{
        alive++;
    }
    if(alive>=2000){
        alive=0;
        close(pad);
        if(ispresent==0){
            transient_present=1;
        }
        ispresent=1;
        pad=open(CONTROL_DEVICE,O_RDONLY|O_NONBLOCK);
        if( access( CONTROL_DEVICE, R_OK ) < 0 ) {
            ispresent=0;
        }
    }
    rounds++;


    if((encendido==1)&&(conectado==1)&&(ispresent==1)&&(rounds>=ROUND_WINDOW)){
       
	    if((send_gas<=GAS_CONFIG)&&(guinnada==0)){
		    config=1;

	    }
	    if(send_gas>GAS_CONFIG){config=0;}
	    if (config==1){
		    send_guinnada=0;
                      // printf("\r\n %i %i ", guinnada,STOP_WITH_YAW);
                        if(guinnada<=STOP_WITH_YAW){
                                query("Y0                         ",mode_send);
				
                        }
                        if(guinnada>=START_WITH_YAW){
				if((rfmon_avail)&&(1==2)){
					random_vector(gcry_key,GCRY_KEYLEN);
					gcry_cipher_setkey (gcry_hd,gcry_key,GCRY_KEYLEN);
    					seq=2;				
					notify_key_to_rfmon(gcry_key);
				}
	                        query("Y1000000                   ",mode_send);
				

			
                        }
			if(hastolog==1){fflush(fl);}
	    }else{
            send_guinnada=guinnada;
        }
	
	
	    sprintf(output, "QQZ%iZ%iZ%iZ%iZ", (int)send_gas/1000,(int)alabeo/1000, (int)cabeceo/1000, (int)send_guinnada/1000);

	    if(hastolog==1){
		fprintf(fl,"%s\n", output );
             }
		
		printf(".");fflush(stdout);
	    query(output, mode_send);
        rounds=0;

    }
    
    //#ifndef MANDO_ABORDO
	usleep(500);
    //#endif

    

    }   
}