-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.c
executable file
·496 lines (370 loc) · 13.3 KB
/
utils.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
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
488
489
490
491
492
493
494
495
496
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/time.h>
#include <stdint.h>
#include <netinet/in.h>
#include "network.h"
#include "antisurveillance.h"
#include "utils.h"
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <net/if.h>
#include "research.h"
#define BUFFER_SIZE 4096
// count the amount of entries in a linked list
int L_count(LINK *ele) {
int count = 0;
while (ele != NULL) {
count++;
ele = ele->next;
}
return count;
}
// finds the last element in a linked list
LINK *L_last(LINK *list) {
if (list == NULL) return NULL;
while (list->next != NULL) {
list = list->next;
}
return list;
}
void L_link_unordered(LINK **list, LINK *ele) {
ele->next = *list;
*list = ele;
}
// Orderd linking (first in first out) which is required for packets
void L_link_ordered(LINK **list, LINK *ele) {
LINK *_last = NULL;
// if the list has no entries.. then this becomes its first element
if (*list == NULL) {
*list = ele;
return;
}
// find the last element
_last = L_last(*list);
if (_last == NULL) {
return;
}
// and append this to that one..
_last->next = ele;
}
// unlinks something from a list
void L_unlink(LINK **list, LINK *ptr) {
LINK *lptr = *list, *llast = NULL;
while (lptr != NULL) {
// if we found it
if (lptr == ptr) {
if (llast) {
llast->next = ptr->next;
} else {
*list = ptr->next;
}
return;
}
lptr = lptr->next;
}
// now we remove link to next in this structure
ptr->next = NULL;
}
// free a pointer after verifying it even exists
void PtrFree(char **ptr) {
if (ptr == NULL) return;
if (*ptr == NULL) return;
free(*ptr);
*ptr = NULL;
}
// allocates & copies data into a new pointer
int DataPrepare(char **data, char *ptr, int size) {
char *buf = (char *)malloc(size );
if (buf == NULL) return -1;
memcpy(buf, ptr, size);
*data = buf;
return 1;
}
int PtrDuplicate(char *ptr, int size, char **dest, int *dest_size) {
char *buf = NULL;
if ((ptr == NULL) || (size <= 0)) {
return 0;
}
if ((buf = (char *)malloc(size + 1)) == NULL) {
return -1;
}
memcpy(buf, ptr, size);
*dest = buf;
*dest_size = size;
return 1;
}
//https://www.linuxquestions.org/questions/programming-9/how-to-calculate-time-difference-in-milliseconds-in-c-c-711096/
int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y) {
if (x->tv_usec < y->tv_usec) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_usec - y->tv_usec > 1000000) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec;
return x->tv_sec < y->tv_sec;
}
int FileWrite(char *filename, char *ptr, int size) {
int r = 0;
FILE *fd = fopen(filename, "wb");
if (fd == NULL) return -1;
r = fwrite(ptr, 1, size, fd);
fclose(fd);
return (r == size);
}
// put a files contents into a memory buffer
char *FileContents(char *filename, int *size) {
FILE *fd = fopen(filename,"rb");
char *buf = NULL;
struct stat stv;
if (fd == NULL) return NULL;
fstat(fileno(fd), &stv);
buf = (char *)calloc(1,stv.st_size );
if (buf != NULL) {
fread(buf,stv.st_size,1,fd);
*size = stv.st_size;
}
fclose(fd);
return buf;
}
// copy an IPv6 address over.. simple although I wanted to SHOW IPv6 everywhere it was taking place instead of 'memcpy'
void CopyIPv6Address(void *dst, void *src) {
// verify both parameters arent NULL..
if (dst && src)
memcpy(dst, src, sizeof(struct in6_addr));
}
// compare two IPv6 addresses.. allows NULL parameters on purpose
int CompareIPv6Addresses(struct in6_addr *first, struct in6_addr *second) {
if (!first && !second)
return 0;
return (memcmp(first,second,sizeof(struct in6_addr)) == 0);
}
// Orderd linking (first in first out) which is required for packets
void L_link_ordered_offset(LINK **list, LINK *ele, int offset) {
void **ptr = NULL;
void **ptr2 = NULL;
// if the original pointer is empty... lets set it
// we have to be careful and set here because the offset is for the 'next' only..
// the initial pointer is at 0.. not +offset
if (*list == NULL) {
*list = ele;
return;
}
// use the offset that was passsed.. so we add it to THAT linked list.
// its the 'next'
ptr = (void *)((unsigned char *)(*list) + offset);
// if 'next' is empty.. set and return
if (*ptr == NULL) {
*ptr = ele;
return;
}
// if not NULL... then..
while (*ptr != NULL) {
// go into that next.. and look at ITS 'next'
ptr2 = (void **)((unsigned char *)(*ptr) + offset);
// if that 'next' is NULL.. we wanna use it
if (*ptr2 == NULL) break;
// ptr = ptr->'next'
ptr = (void **)((unsigned char *)(*ptr2) + offset);
// if its free...
if (*ptr == NULL) {
*ptr = (void *)ele;
return;
}
}
// set the last one's 'next' (using the offset of it) to this new element
*ptr2 = (void *)ele;
}
// L_count() which takes offset (instead of the LINK structure ->next)
// means itll work for multidimensional lists we use in research.c
int L_count_offset(LINK *lptr, int offset) {
int count = 0;
void **ptr = NULL;
// if we dont have any at all...
if (lptr == NULL) return 0;
do {
count++;
ptr = (void **)((unsigned char *)lptr + offset);
// if the next element (using the offset instead of ->next, which is the real 'next') is NULL, then we're done
// *** this is redundant.. the do {} while will get the same information
if (*ptr == NULL) break;
// otherwise lets move forward with it
lptr = *ptr;
} while (lptr != NULL);
return count;
}
// this prepares fabricated connections using either IPv4, or IPv6 addresses.. it detects IPv6 by the :
int IP_prepare(char *ascii_ip, uint32_t *ipv4_dest, struct in6_addr *ipv6_dest, int *_is_ipv6) {
int is_ipv6 = 0;
if (ascii_ip == NULL) return 0;
is_ipv6 = strchr(ascii_ip, ':') ? 1 : 0;
if (!is_ipv6) {
if (ipv4_dest)
*ipv4_dest = inet_addr(ascii_ip);
if (_is_ipv6 != NULL)
*_is_ipv6 = 0;
} else {
if (_is_ipv6 != NULL)
*_is_ipv6 = 1;
inet_pton(AF_INET6, ascii_ip, ipv6_dest);
}
return 1;
}
// this prepares fabricated connections using either IPv4, or IPv6 addresses.. it detects IPv6 by the :
char *IP_prepare_ascii(uint32_t ipv4_dest, struct in6_addr *ipv6_src) {
char final[50]; // its 45-46.. or 16.. whatever
struct in_addr dst;
char *buf = NULL;
memset(final, 0, sizeof(final));
if (ipv4_dest) {
dst.s_addr = ipv4_dest;
strncpy(final, inet_ntoa(dst), sizeof(final));
} else if (ipv6_src != NULL) {
buf = inet_ntop(AF_INET6, (const void *)ipv6_src, (char *)&final, (socklen_t) sizeof(final));
strncpy(final, buf, 50);
free(buf);
}
return strdup(final);
}
int file_exist(char *filename) {
struct stat stv;
return (stat(filename, &stv) == 0);
}
// https://gist.githubusercontent.com/javiermon/6272065/raw/f6456b6db893a8f020a2436f1043f0eb12ac57e1/gateway_netlink.c
char *getgatewayandiface(void) {
int received_bytes = 0, msg_len = 0, route_attribute_len = 0;
int sock = 0, msgseq = 0;
struct nlmsghdr *nlh, *nlmsg;
struct rtmsg *route_entry;
// This struct contain route attributes (route type)
struct rtattr *route_attribute;
char gateway_address[INET_ADDRSTRLEN], interface[IF_NAMESIZE];
char msgbuf[BUFFER_SIZE], buffer[BUFFER_SIZE];
char *ptr = buffer;
struct timeval tv;
char *ret = NULL;
if ((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) goto end;
memset(msgbuf, 0, sizeof(msgbuf));
memset(gateway_address, 0, sizeof(gateway_address));
memset(interface, 0, sizeof(interface));
memset(buffer, 0, sizeof(buffer));
/* point the header and the msg structure pointers into the buffer */
nlmsg = (struct nlmsghdr *)msgbuf;
/* Fill in the nlmsg header*/
nlmsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
nlmsg->nlmsg_type = RTM_GETROUTE; // Get the routes from kernel routing table .
nlmsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST; // The message is a request for dump.
nlmsg->nlmsg_seq = msgseq++; // Sequence of the message packet.
nlmsg->nlmsg_pid = getpid(); // PID of process sending the request.
memset(&tv, 0, sizeof(struct timeval));
/* 1 Sec Timeout to avoid stall */
tv.tv_sec = 1;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval));
/* send msg */
if (send(sock, nlmsg, nlmsg->nlmsg_len, 0) < 0) goto end;
/* receive response */
do {
received_bytes = recv(sock, ptr, sizeof(buffer) - msg_len, 0);
if (received_bytes < 0) goto end;
nlh = (struct nlmsghdr *) ptr;
/* Check if the header is valid */
if((NLMSG_OK(nlmsg, received_bytes) == 0) ||
(nlmsg->nlmsg_type == NLMSG_ERROR)) goto end;
/* If we received all data break */
if (nlh->nlmsg_type == NLMSG_DONE)
break;
else {
ptr += received_bytes;
msg_len += received_bytes;
}
/* Break if its not a multi part message */
if ((nlmsg->nlmsg_flags & NLM_F_MULTI) == 0)
break;
} while ((nlmsg->nlmsg_seq != msgseq) || (nlmsg->nlmsg_pid != getpid()));
/* parse response */
for ( ; NLMSG_OK(nlh, received_bytes); nlh = NLMSG_NEXT(nlh, received_bytes)) {
/* Get the route data */
route_entry = (struct rtmsg *) NLMSG_DATA(nlh);
/* We are just interested in main routing table */
if (route_entry->rtm_table != RT_TABLE_MAIN)
continue;
route_attribute = (struct rtattr *) RTM_RTA(route_entry);
route_attribute_len = RTM_PAYLOAD(nlh);
/* Loop through all attributes */
for ( ; RTA_OK(route_attribute, route_attribute_len);
route_attribute = RTA_NEXT(route_attribute, route_attribute_len)) {
switch(route_attribute->rta_type) {
case RTA_OIF:
if_indextoname(*(int *)RTA_DATA(route_attribute), interface);
break;
case RTA_GATEWAY:
inet_ntop(AF_INET, RTA_DATA(route_attribute),
gateway_address, sizeof(gateway_address));
break;
default:
break;
}
}
if (*interface) ret = strdup(interface);
if ((*gateway_address) && (*interface))
break;
}
end:;
if (sock) close(sock);
return ret;
}
// on routers if the free memory is extremely low then we dont want to hold all outgoing packets in our memory (IoT routers wouldnt handle it properly)
// especially with large attack structures, and attempting to filter out our own packets
int FreeMemoryMB(void) {
FILE *fd;
char buf[1024];
unsigned long long ret = 0;
unsigned long long value = 0;
char type[32];
char *sptr = NULL;
char wanted[] = "MemFree";
if ((fd = fopen("/proc/meminfo", "r")) == NULL) return 0;
while (fgets(buf,1024,fd)) {
//if ((sptr = strchr(buf, '\r')) != NULL) *sptr = 0;
//if ((sptr = strchr(buf, '\n')) != NULL) *sptr = 0;
sscanf(buf, "%32s %llu", type, &value);
if ((sptr = strchr(type, ':')) != NULL) *sptr = 0;
if (strcmp(type, wanted)==0) {
ret = value;
break;
}
}
fclose(fd);
if (ret) ret /= (1024);
return ret;
}
// allows us to use various IPv4 addresses which we receieve data for
// this needs to obviously be redone..
uint32_t get_source_ipv4(void) {
char ip[16];
int r = 1+rand()%250;
sprintf(ip, "192.168.72.%d",r);
return inet_addr(ip);
}
// allows us to use random/various IPv6 addresses which we receive packets for
// this needs to obviously be redone..
void get_source_ipv6(struct in6_addr *addr6) {
struct in6_addr our_ipv6;
get_local_ipv6(&our_ipv6);
// now for here to modify IPv6 if we are performing massive attacks
// mangle the IP, etc... but ensure its within our ranges
}