-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapper.c
277 lines (214 loc) · 6.47 KB
/
scrapper.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <signal.h>
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
#define MUTEX_NUMBER 800
#define MUTEX_TIMEOUT_MICROSECONDS 40000
#define THEAD_TIMEOUT_CREATION_MICROSECONDS 100
#define THREAD_CHECK_TIMEOUT_MICROSECONDS 2000
#define MAX_THREADS_NUMBER 1500
char *strjoin(char const *s1, char const *s2);
static pthread_mutex_t mutexs[MUTEX_NUMBER];
static float entries;
static int threads_dones = 0;
static int threads_fill = 0;
static char *validSubDomains = "\0";
typedef struct threadArg
{
int index;
char *domain;
} threadArg;
size_t arrofArrayLength(char **arr)
{
size_t i = 0;
while (arr[i] != NULL) {
i++;
}
return i;
}
char **appendStr(char **strs, char *str)
{
size_t len = arrofArrayLength(strs);
size_t i = 0;
char **copy = malloc(sizeof(char*) * (len + 2));
while (strs[i]) {
copy[i] = strs[i];
i++;
}
copy[i] = str;
copy[i + 1] = NULL;
free(strs);
i = 0;
while (copy[i]) {
i++;
}
return copy;
}
int hasStr(char **strs, char *str)
{
if (str == NULL)
return 0;
for (size_t i = 0; strs[i]; i++)
{
if (strcmp(strs[i], str) == 0)
return 1;
}
return 0;
}
char *randstring(size_t length)
{
static char charset[] = "abcdefghijklmnopqrstuvwxyz-";
char *randomString = NULL;
if (length)
{
randomString = malloc(sizeof(char) * (length + 1));
if (randomString)
{
for (size_t n = 0; n < length; n++)
{
int key = rand() % (int)(sizeof(charset) - 1);
randomString[n] = charset[key];
}
randomString[length] = '\0';
}
}
return randomString;
}
int testDomain(char *domain)
{
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM;
int result = getaddrinfo(domain, NULL, &hints, &res);
if (result == 0) {
freeaddrinfo(res);
return 1;
}
return 0;
}
void *threadProcess(void *args)
{
threadArg *datas = (threadArg*)args;
char *tmp;
size_t i = 0;
while (1 == 1) {
if (i >= MUTEX_NUMBER - 1) {
i = 0;
} else {
i++;
}
if (pthread_mutex_trylock(&mutexs[i]) == 0)
break;
usleep(MUTEX_TIMEOUT_MICROSECONDS);
}
if (testDomain(datas->domain))
{
tmp = strjoin(validSubDomains, strjoin(" ", datas->domain));
validSubDomains = tmp;
printf(GREEN "subdomain %s exists!\n" RESET, datas->domain);
}
pthread_mutex_unlock(&mutexs[i]);
// printf("[%i] thread is %sended%s, liberate mutex %s[%li]%s\n", datas->index, GREEN, RESET, MAGENTA, i, RESET);
threads_dones += 1;
free(datas);
return NULL;
}
void sigIntlCatch() {
printf(GREEN "List of valid subdomains finded : %s\n" RESET, validSubDomains);
exit(0);
}
void *monitoring(void *args) {
float process_percentage;
while (threads_dones < (int)entries - 1) {
process_percentage = threads_dones * 100 / entries;
printf("Process is done at %s%.2f%%%s. Still running %i threads, actual results [%s]\n", GREEN, process_percentage, RESET, (int)entries - threads_dones, validSubDomains);
sleep(1);
}
return args;
}
int main(int ac, char **av)
{
char **triesDomains = malloc(sizeof(char *) * 2);
triesDomains[0] = "Test\0";
triesDomains[1] = NULL;
if (ac != 3)
{
printf(RED "You must specify an domain and a number of tries\n" RESET);
return 1;
}
pthread_t threads[MAX_THREADS_NUMBER];
pthread_t thead_monitoring;
for (size_t i = 0; i < MUTEX_NUMBER; i++)
pthread_mutex_init(&mutexs[i], NULL);
if (strchr(av[1], '.') == NULL)
{
printf(RED "Your domain %s, is invalid !\n" RESET, av[1]);
return 1;
}
entries = atof(av[2]);
pthread_create(&thead_monitoring, NULL, &monitoring, NULL);
printf(MAGENTA "Start sub domain parsing for domain : %s\n" RESET, av[1]);
// Catch SIGINT for not lost results
signal(SIGINT, sigIntlCatch);
for (int i = 0; i < atoi(av[2]); i++)
{
int maxLength = rand() % 30;
if (!maxLength)
continue;
char *subDomain = strjoin(strjoin(randstring(maxLength), "."), av[1]);
printf("[%i] add subdomain %s into the subdomains list\n", i, subDomain);
// If subDomain has already been tested, pass it!
if (hasStr(triesDomains, subDomain))
{
i -= 1;
printf(RED "Domain %s already in the list!\n" RESET, subDomain);
free(subDomain);
continue;
}
triesDomains = appendStr(triesDomains, subDomain);
}
for (int i = 0; i < atoi(av[2]); i++) {
char *subDomain = triesDomains[i];
threadArg *args = malloc(sizeof(threadArg));
args->index = i;
args->domain = subDomain;
pthread_create(&threads[threads_fill], NULL, &threadProcess, args);
threads_fill += 1;
usleep(THEAD_TIMEOUT_CREATION_MICROSECONDS);
if (threads_fill >= MAX_THREADS_NUMBER) {
for (int j = 0; j < threads_fill ; j++) {
pthread_join(threads[j], NULL);
}
threads_fill = 0;
}
}
printf(GREEN "List of valid subdomains finded : %s\n" RESET, validSubDomains);
}