-
Notifications
You must be signed in to change notification settings - Fork 5
/
openheimer.cpp
487 lines (417 loc) · 10.5 KB
/
openheimer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#include <sys/types.h>
#ifdef _WIN32
#include <Winsock2.h>
#include <Ws2tcpip.h>
#else
#include <fcntl.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#endif
#include <algorithm>
#include <condition_variable>
#include <errno.h>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <thread>
#include <vector>
std::vector<std::thread> ThreadVector;
#define HANDSHAKE_SIZE 1024
#define STRING_BUF_SIZE 16
#define PROTOCOL_VERSION 210
#define TIMEOUT_SEC 1 // 1000ms
#ifdef _WIN32
#pragma comment(lib, "Ws2_32.lib")
typedef SSIZE_T ssize_t;
#endif
using namespace std;
std::condition_variable cv;
std::mutex cv_m;
int threads = 800;
std::ifstream myfile("masscan.txt");
int connect_w_to(struct addrinfo *addr, time_t sec)
{
int res;
long arg;
fd_set myset;
struct timeval tv;
int valopt;
socklen_t lon;
int soc;
// Create socket
soc = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (soc < 0)
{
fprintf(stderr, "Error creating socket (%d %s)\n", errno, strerror(errno));
return -1;
}
// Set non-blocking
if ((arg = fcntl(soc, F_GETFL, NULL)) < 0)
{
fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno));
return -1;
}
arg |= O_NONBLOCK;
if (fcntl(soc, F_SETFL, arg) < 0)
{
fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno));
return -1;
}
// Trying to connect with timeout
res = connect(soc, addr->ai_addr, addr->ai_addrlen);
if (res < 0)
{
if (errno == EINPROGRESS)
{
do
{
tv.tv_sec = sec;
tv.tv_usec = 0;
FD_ZERO(&myset);
FD_SET(soc, &myset);
res = select(soc + 1, NULL, &myset, NULL, &tv);
if (res < 0 && errno != EINTR)
{
fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
return -1;
}
else if (res > 0)
{
// Socket selected for write
lon = sizeof(int);
if (getsockopt(soc, SOL_SOCKET, SO_ERROR, (void *)(&valopt), &lon) < 0)
{
fprintf(stderr, "Error in getsockopt() %d - %s\n", errno, strerror(errno));
close(soc);
return -1;
}
// Check the value returned...
if (valopt)
{
close(soc);
return -1;
}
break;
}
else
{
close(soc);
return -1;
}
} while (1);
}
else
{
fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
return -1;
}
}
// Set to blocking mode again...
if ((arg = fcntl(soc, F_GETFL, NULL)) < 0)
{
fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno));
return -1;
}
arg &= (~O_NONBLOCK);
if (fcntl(soc, F_SETFL, arg) < 0)
{
fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno));
return -1;
}
return soc;
}
int set_timeout(int sfd, time_t sec)
{
struct timeval timeout;
timeout.tv_sec = sec;
timeout.tv_usec = 0;
int yes = 1;
setsockopt(
sfd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(int));
// Receive
if (setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
{
fprintf(stderr, "setsockopt failed\n");
return -1;
}
// Send
if (setsockopt(sfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
{
fprintf(stderr, "setsockopt failed\n");
return -1;
}
return 0;
}
size_t build_handshake(unsigned char *buffer, char *host, unsigned short port)
{
size_t host_len = strlen(host);
size_t len = 1 /* packet id */ + 2 /* Protocol version */;
len += 1 /* str len */ + host_len;
len += 2; // port
len += 1; // state
size_t i = 0;
buffer[i++] = len;
buffer[i++] = 0; /* packet id */
buffer[i++] = PROTOCOL_VERSION;
buffer[i++] = 1; /* encoded protocol version - varint */
buffer[i++] = host_len;
memcpy(buffer + i, host, host_len);
i += host_len;
buffer[i++] = (port >> 8) & 0xFF; /* port little-endian */
buffer[i++] = port & 0xFF;
buffer[i] = 1; // next state
return len + 1; /* add length byte */
}
ssize_t read_byte(const int sfd, void *buf)
{
ssize_t nread;
nread = recv(sfd, buf, 1, 0);
if (nread == -1)
{
//perror("Read byte");
return (1);
}
return nread;
}
int read_varint(const int sfd)
{
int numread = 0;
int result = 0;
int value;
char byte;
do
{
if (read_byte(sfd, &byte) == 0)
{
//fprintf(stderr, "Failed read varint: eof\n");
return (-1);
}
value = byte & 0x7F;
result |= value << (7 * numread);
numread++;
if (numread > 5)
{
//fprintf(stderr, "Error reading varint: varint too big\n");
return (-1);
}
} while ((byte & 0x80) != 0);
return result;
}
void ping_server(char *hostname, unsigned short port)
{
int sfd, s, json_len;
char string[STRING_BUF_SIZE];
char port_str[6];
struct addrinfo hints;
struct addrinfo *result, *rp;
char byte;
unsigned char handshake[HANDSHAKE_SIZE];
char request[] = {0x1, 0x0};
size_t len;
ssize_t nread;
if (strlen(hostname) > 250)
{
fprintf(stderr, "Hostname too long\n");
return;
}
if (port == 0)
{
fprintf(stderr, "Invalid port\n");
return;
}
#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
int err;
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0)
{
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
fprintf(stderr, "Could not find a usable version of Winsock.dll\n");
WSACleanup();
return;
}
#endif
/* Obtain address(es) matching host/port */
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* TCP socket */
hints.ai_flags = 0;
hints.ai_protocol = 0; /* Any protocol */
sprintf(port_str, "%d", port);
s = getaddrinfo(hostname, port_str, &hints, &result);
if (s != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
return;
}
/* getaddrinfo() returns a list of address structures.
Try each address until we successfully connect(2).
If socket(2) (or connect(2)) fails, we (close the socket
and) try the next address. */
for (rp = result; rp != NULL; rp = rp->ai_next)
{
sfd = connect_w_to(rp, TIMEOUT_SEC);
if (sfd != -1)
{
break;
}
close(sfd);
}
if (rp == NULL)
{ /* No address succeeded */
//fprintf(stderr, "Could not connect\n");
return;
}
if (set_timeout(sfd, TIMEOUT_SEC) == -1)
{
close(sfd);
return;
}
freeaddrinfo(result);
len = build_handshake(handshake, hostname, port);
if (send(sfd, handshake, len, 0) != len)
{
//fprintf(stderr, "Failed to send handshake\n");
close(sfd);
return;
}
if (send(sfd, request, 2, 0) != 2)
{
//fprintf(stderr, "Failed to send request\n");
close(sfd);
return;
}
read_varint(sfd); /* read packet length */
if (read_byte(sfd, &byte) == 0)
{ /* read packet id */
//fprintf(stderr, "Failed to read\n");
close(sfd);
return;
}
if (byte != 0)
{
//fprintf(stderr, "Unknown packet id\n");
close(sfd);
return;
}
std::string jsonStuff;
/* read json and print to stdout */
json_len = read_varint(sfd);
while (json_len > 0)
{
nread = recv(sfd, string, STRING_BUF_SIZE, 0);
if (nread == -1)
{
//perror("json read");
close(sfd);
return;
}
json_len -= nread;
std::string readBuffer(string, nread);
jsonStuff += readBuffer;
}
close(sfd);
if (jsonStuff.length() != 0)
{
cout << "> " << jsonStuff.c_str() << endl;
}
return;
}
int get_number_of_lines()
{
// new lines will be skipped unless we stop it from happening:
myfile.unsetf(std::ios_base::skipws);
// count the newlines with an algorithm specialized for counting:
int line_count = std::count(
std::istream_iterator<char>(myfile),
std::istream_iterator<char>(),
'\n');
myfile.clear();
myfile.seekg(0);
return line_count;
}
void scan_ip_chunk(vector<string> array_of_servers)
{
sleep(5);
for (int i = 0; i < array_of_servers.size(); i++)
{
std::string str(array_of_servers[i]);
std::string buf; // Have a buffer string
std::stringstream ss(str); // Insert the string into a stream
std::vector<std::string> tokens; // Create vector to hold our words
while (ss >> buf)
tokens.push_back(buf);
char *ip = (char *)tokens[3].c_str();
unsigned short port = stoi(tokens[2]);
ping_server(ip, port);
}
}
int main()
{
int line_count = get_number_of_lines();
//Read lines into array
std::string line;
std::string line_array[line_count];
int z = 0;
while (std::getline(myfile, line))
{
line_array[z] = line;
++z;
}
int linesPerChunk = line_count / threads;
cout << "Total IP's: " << line_count << endl;
cout << "Total threads: " << threads << endl;
cout << "IP's per thread: " << linesPerChunk << "\n";
int extra = 0;
int extra_times = 0;
for (int i = 0; i < line_count;)
{
if (extra_times < line_count % threads)
{
extra = 1;
++extra_times;
}
else
{
extra = 0;
}
string current_thread_array[linesPerChunk + extra];
for (int x = 0; x < linesPerChunk + extra; ++x)
{
current_thread_array[x] = line_array[i];
++i;
}
std::vector<std::string> vecOfStr(current_thread_array,
current_thread_array +
sizeof(current_thread_array) / sizeof(current_thread_array[0]));
ThreadVector.emplace_back([test = std::move(vecOfStr)]()
{ scan_ip_chunk(test); });
}
for (auto &t : ThreadVector)
{
t.join();
}
return 0;
}