LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-01-2008, 06:09 AM   #1
sarahnetworking
LQ Newbie
 
Registered: Mar 2008
Posts: 2

Rep: Reputation: 0
Question server client application in c


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:

#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(char *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("socket() failed");

/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */

/* Establish the connection to the echo server */
if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError("connect() failed");

handeltcpclient(sock, argv);

close(sock);
}



this is the handeltcpclient.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(char *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,sizeof(clntBuf));
memset (&srvBuf,0,sizeof(srvBuf));
MsgSize = RecieveMessage(clntSocket,&srvBuf, sizeof(srvBuf));

printf("Recieved: %s" &srvBuf,rcvMsgSize);

response = strncpy(&srvBuf, 3);
result = strcmp( result, "230");
if( result == 0)
{
sprintf(&clntBuf, "USER %s", USER);
send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf));
DieWithError("send() sent a different number of bytes than expected");


printf("Recieved: %s" &srvBuf,messageSize);


/* 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",USER);
}

/*send USER to server*/
sprintf(clntBuf, "USER %s", USER);

if ( send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf)))
DieWithError("send() 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(&clntBuf, "PASS %s", PASS);
send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf));
DieWithError("send() sent a different number of bytes than expected");

result = strncpy(&srvBuf, 3);
result = strcmp(result, "230");

if (result == 0)
{
printf("successfully 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",PASS);
}
}
/*send PASS to server*/
sprintf(clntBuf, "PASS %s", PASS);

if ( send(clntSocket, &clntBuf, strlen(clntBuf), 0) != (strlen(clntBuf)))
DieWithError("send() 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("revc() failed");
}
return received;
}

/*end*/

this is the diewitherror.c (error handling file)

#include <stdio.h> /* for perror() */
#include <stdlib.h> /* for exit() */

void DieWithError(char *errorMessage)
{
perror(errorMessage);
exit(1);
}
 
Old 03-01-2008, 09:44 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Well, just a few style things to start with. You should use inet_aton instead of inet_addr because inet_addr is obsolete. Also, sizeof when used with a variable name should not have (); that's only for type names.

This looks like it was supposed to have an if statement:
Code:
send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf));
DieWithError("send() sent a different number of bytes than expected");
Code:
if (send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= strlen(clntBuf))
DieWithError("send() sent a different number of bytes than expected");
Are you connecting to a real ftp to test it? I say that because I don't see a bind or listen call.
ta0kira
 
Old 03-02-2008, 03:09 AM   #3
sarahnetworking
LQ Newbie
 
Registered: Mar 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Smile

yeah i am suppose to connect it to a real server on assessment day and run the program. there seems to be a problem with my strcmp and strncpy as i get errors on them. don't know where i have gone wrong. i would appreciate it if u could help me out as this module is worth 50% and i've never done c before and this is my final year at uni.
 
Old 03-02-2008, 10:55 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I'll take another look. When you use strncpy, you need to use a number 1 greater than the number of characters you expect, then manually set the last character in the target array to \0:
Code:
char  old_array[] = "1234567890";
char *new_array   = strncpy(new_array, old_array, 4); //<-- use 4 to copy 3 chars
if (new_array) new_array[3] = 0x00; //<-- set the last to null (not "NULL" proper)
If you only use the number of readable characters in the copy then you won't have a null-terminated string. You need to allow for the additional byte to hold the null, and if the source string is longer than expected strncpy won't terminate it.
ta0kira

PS That might fix your strcmp problem.

Last edited by ta0kira; 03-02-2008 at 10:57 AM.
 
Old 03-02-2008, 06:23 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,426

Rep: Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786
Actually, you only need copy 3 chars if that's how many you want, then you append the null char:

new_array[3] ='\0';

If you copy 4 and then overwrite the 4th char (new_array[3]) with '\0' it just confuses people.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
RAD IDE for Client/Server centric application spikeymike Programming 1 03-18-2008 03:51 PM
GUI client ftp application not connected with server dhananjaya Linux - Networking 1 10-28-2007 06:35 PM
How Stun Server communicates with Stun Client - Application level code needed sougata123 Linux - Networking 0 12-21-2006 06:49 AM
LQ.org Client Application? vharishankar LQ Suggestions & Feedback 2 04-08-2005 10:49 AM
server and client configuration oof any application on IPv6 e-ahmed Linux - Networking 0 05-17-2003 05:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration