-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
159 lines (137 loc) · 4.03 KB
/
main.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
//
// main.c
// autoMACtic
//
// Created by fairy-slipper on 1/24/16.
// Copyright © 2016 fairy-slipper. All rights reserved.
//
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
pid_t pid;
int status;
int minimum;
int maximum;
void safe_printf(const char *format, ...)
{
char buf[1000];
va_list args;
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
write(1, buf, strlen(buf)); /* write is async-signal-safe */
}
int isValidNDevice(char *name) {
char data[4096];
memset(data, 0, 4096);
struct ifconf ifc;
struct ifreq *ifr;
int sk, length;
sk = socket(AF_INET, SOCK_DGRAM, 0);
if(sk < 0)
{
perror("socket");
return 0;
}
ifc.ifc_len = sizeof(data);
ifc.ifc_buf = (caddr_t)data;
if(ioctl(sk, SIOCGIFCONF, &ifc) < 0)
{
perror("ioctl(SIOCGIFCONF)");
return 0;
}
ifr = (struct ifreq*)data;
for(int i=0;i<ifc.ifc_len;)
{
length=IFNAMSIZ + ifr->ifr_addr.sa_len;
printf("%s\n", ifr->ifr_name);
if (!strcmp(ifr->ifr_name,name)) {
printf("Interface Found!\n");
return 1;
}
ifr=(struct ifreq*)((char*)ifr+length);
i+=length;
}
return 0;
}
int main(int argc, char **argv) {
maximum = 3600;
minimum = 1800;
srand(time(NULL));
uid_t euid=geteuid();
if (0!=euid) {
printf("\nPlease run as root\n");
exit(0);
}
if (argc<2) {
printf("\nPlease specify a network device\n");
printf("./autoMACtic <device>\n");
printf("\n\nYou can also choose refresh bounds.");
printf("\n./autoMACtic <device> <minumum timeout in minutes> <maximum timeout in minutes>\n\n");
exit(0);
}
if (!isValidNDevice(argv[1])) {
printf("\nPlease choose a valid network device.\nFor example: en0 or p2p0\n");
exit(0);
}
if (argv[2] != NULL) {
minimum = atoi(argv[2])*60;
}
if (argv[3] != NULL) {
maximum = atoi(argv[3])*60;
}
if (maximum<minimum) {
printf("\nmaximum must be equal to or greater than minimum\n");
exit(0);
}
if (maximum<60 || minimum<60) {
printf("\ntimeout must be equal or greater than 1 minute\n");
exit(0);
}
printf("\nDevice: %s", argv[1]);
printf("\nMin Refresh: %.01fmin", (float)minimum/60.0f);
printf("\nMax Refresh: %.01fmin\n", (float)maximum/60.0f);
sleep(3);
while (1) {
char hwaddr[1000];
memset(hwaddr, 0, strlen(hwaddr));
sprintf(hwaddr, "%x%x:%x%x:%x%x:%x%x:%x%x:%x%x", rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16);
pid = fork();
if (pid == 0) {
char *execArgs[] = {"sudo", "ifconfig", argv[1], "ether", hwaddr, NULL};
printf("\nSetting new MAC address: %s on device: %s\n", hwaddr, argv[1]);
execvp(execArgs[0], execArgs);
} else {
waitpid(-1, &status, 0);
sleep(10);
pid = fork();
if (pid == 0) {
char *execArgs[] = {"sudo", "ifconfig", argv[1], "down", NULL};
execvp(execArgs[0], execArgs);
} else {
waitpid(-1, &status, 0);
sleep(10);
pid = fork();
if (pid == 0) {
char *execArgs[] = {"sudo", "ifconfig", argv[1], "up", NULL};
execvp(execArgs[0], execArgs);
} else {
waitpid(-1, &status, 0);
printf("Completed\n");
int r = rand()%(maximum+1-minimum);
sleep(minimum+r);
}
}
}
}
return EXIT_SUCCESS;
}