-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
308 lines (251 loc) · 8.76 KB
/
server.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
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>
//Daemon
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <syslog.h>
//Crypto
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/des.h>
void error(const char *msg)
{
perror(msg);
exit(0);
}
void demonize()
{
//Set our Logging Mask and open the Log
setlogmask(LOG_UPTO(LOG_NOTICE));
openlog("wifither", LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
syslog(LOG_INFO, "Entering Daemon");
pid_t pid;
//Fork the Process making a child
pid = fork();
//Failed to fork: EXIT_FAILURE
if (pid < 0) {
exit(EXIT_FAILURE);
}
//We got a good pid, Close the Parent Process
if (pid > 0) {
exit(EXIT_SUCCESS);
}
//Change File Mask
umask(0);
//Create a new Signature Id for our child
if (setsid() < 0) {
exit(EXIT_FAILURE);
}
//Fork again to remove leadership given by setsid()
pid = fork();
//Failed to fork: EXIT_FAILURE
if (pid < 0) {
exit(EXIT_FAILURE);
}
//We got a good pid, Close the Parent Process
if (pid > 0) {
exit(EXIT_SUCCESS);
}
//Change the working Directory
//If we cant find the directory we exit with failure.
if ((chdir("/")) < 0) {
exit(EXIT_FAILURE);
}
//Close Standard File Descriptors
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
//Base64 decoder
char *unbase64(unsigned char *input, int length)
{
BIO *b64, *bmem;
char *buffer = (char *)malloc(length);
memset(buffer, 0, length);
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new_mem_buf(input, length);
bmem = BIO_push(b64, bmem);
BIO_read(bmem, buffer, length);
BIO_free_all(bmem);
return buffer;
}
//Service core
void process(char *port, char *pass)
{
int sock, length, n;
socklen_t fromlen;
struct sockaddr_in server;
struct sockaddr_in from;
char buf[1024];
char command[1024];
char mac[18] = {0};
//Security vars
DES_cblock key;
DES_key_schedule schedule;
unsigned char *data;
FILE *fp;
char filter[5];
//Socket and packet vars
sock=socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) error("Opening socket");
length = sizeof(server);
bzero(&server,length);
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(atoi(port));
if (bind(sock,(struct sockaddr *)&server,length)<0)
error("binding");
fromlen = sizeof(struct sockaddr_in);
//Service loop
while (1) {
//Var reset
memset(buf, 0, sizeof(buf));
memset(command, 0, sizeof(command));
//Receiver
n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen);
if (n < 0) error("recvfrom");
data = unbase64(buf, strlen(buf));
DES_set_key_unchecked((DES_cblock*) pass, &schedule);
/*
* Decrypt loop.
* Does the same, but in a less efficient way than the long version below
*
for(int i = 0; i < 17; i += 8) {
DES_ecb_encrypt((DES_cblock*) (data+i), (DES_cblock*) (data+i), &schedule, DES_DECRYPT);
}
*/
DES_ecb_encrypt((DES_cblock*) data, (DES_cblock*) data, &schedule, DES_DECRYPT);
data += 8;
DES_ecb_encrypt((DES_cblock*) data, (DES_cblock*) data, &schedule, DES_DECRYPT);
data += 8;
DES_ecb_encrypt((DES_cblock*) data, (DES_cblock*) data, &schedule, DES_DECRYPT);
data -= 16;
switch(data[0]) {
//Wifi restart
case '0':
system("wifi reload");
break;
//Wifi off
case '1':
system("wifi off");
system("wifi down");
break;
//Wifi on
case '2':
system("wifi");
break;
//Remove MAC filter
case '3':
system("sed -i '/macfilter/d' /etc/config/wireless");
break;
case '4':
//If macfilter exists
//Sustitute old value with the new one (deny)
//Otherwise add a new line with it
system("sed -i '/option macfilter/{h;s/'\\''.*'\\''/'\\''deny'\\''/};${x;/^$/{s//\\toption macfilter '\\''deny'\\'' /;H};x}' /etc/config/wireless");
break;
case '5':
//If macfilter exists
//Sustitute old value with the new one (allow)
//Otherwise add a new line with it
system("sed -i '/option macfilter/{h;s/'\\''.*'\\''/'\\''allow'\\''/};${x;/^$/{s//\\toption macfilter '\\''allow'\\'' /;H};x}' /etc/config/wireless");
break;
//Add MAC address
case '6':
strncpy(mac, data+1, 17);
snprintf(command, sizeof(command), "sed -i '/list maclist '\\''%s'\\''/{h};${x;/^$/{s//\\tlist maclist '\\''%s'\\'' /;H};x}' /etc/config/wireless", mac, mac);
system(command);
memset(mac, 0, sizeof(memset));
break;
//Remove MAC address
case '7':
strncpy(mac, data+1, 17);
snprintf(command, sizeof(command), "sed -i '/%s/d}' /etc/config/wireless", mac, mac);
system(command);
memset(mac, 0, sizeof(memset));
break;
//Update config
case '8':
/* Open the command for reading. */
fp = popen("wifi status | sed -n -r 's/.*up.: (true).*/\\1/p'", "r");
if (fp == NULL) {
printf("ERROR: Failed to fetch data\n" );
}
/* Read the output a line at a time - output it. */
if(fgets(filter, 5, fp) != NULL) {
n = sendto(sock, "3", 1, 0,(struct sockaddr *)&from, fromlen);
if (n < 0) error("sendto");
}
/* Open the command for reading. */
fp = popen("sed -n -r 's/.*option macfilter '\\''(allow|deny)'\\''.*/\\1/p' /etc/config/wireless", "r");
if (fp == NULL) {
printf("ERROR: Failed to fetch data\n" );
}
/* Read the output a line at a time - output it. */
if(fgets(filter, 5, fp) == NULL) {
n = sendto(sock, "0", 1, 0,(struct sockaddr *)&from, fromlen);
if (n < 0) error("sendto");
}
else {
if(strcmp(filter, "allo") == 0){
n = sendto(sock, "2", 1, 0,(struct sockaddr *)&from, fromlen);
if (n < 0) error("sendto");
}
else if (strcmp(filter, "deny") == 0){
n = sendto(sock, "1", 1, 0,(struct sockaddr *)&from, fromlen);
if (n < 0) error("sendto");
}
}
n = sendto(sock, "done", 4, 0,(struct sockaddr *)&from, fromlen);
if (n < 0) error("sendto");
/* Close stream */
pclose(fp);
break;
//Update devices
case '9':
/* Open the command for reading. */
fp = popen("sed -n -r 's/.*list maclist '\\''(([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2}))'\\''.*/\\1/p' /etc/config/wireless", "r");
if (fp == NULL) {
printf("ERROR: Failed to fetch data\n" );
}
/* Read the output a line at a time - output it. */
while (fgets(mac, 18, fp) != NULL) {
n = sendto(sock, &mac, strlen(mac), 0,(struct sockaddr *)&from, fromlen);
if (n < 0) error("sendto");
memset(mac, 0, sizeof(mac));
}
n = sendto(sock, "done", 4, 0,(struct sockaddr *)&from, fromlen);
if (n < 0) error("sendto");
/* close */
pclose(fp);
break;
}
n = sendto(sock, &data[0], 1, 0,(struct sockaddr *)&from, fromlen);
if (n < 0) error("sendto");
}
}
int main(int argc, char *argv[]) {
if (argc < 3) {
fprintf(stderr,"ERROR: Missing argument\nSintax: wifither [port] [password]\n");
exit(1);
}
if (strlen(argv[2]) < 8) {
fprintf(stderr,"ERROR: Password must be at leas 8 characters\n");
exit(1);
}
//Detach
demonize();
//----------------
//Main Process
//----------------
process(argv[1], argv[2]); //Run our Process
//Close the log
closelog ();
}