-
Notifications
You must be signed in to change notification settings - Fork 243
/
Copy pathnews_server.c
619 lines (495 loc) · 11.1 KB
/
news_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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
NYU Polytechnic Institute
CS6573: Penetration Testing and Vulnerability Analysis
Code Auditing Homework Assignment
There are a number of security holes in this network service,
but your assignment is to only find 3.
They could be both architectural or implementation problems.
Look for bad logic and memory mismanagement.
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <signal.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#define PORT 9090
#define USERNAME 0x01
#define PASSWORD 0x02
#define BADUSER "\x33\x44 BAD USERNAME!"
#define BADPASS "\x33\x45 BAD PASSWORD!"
#define READY "\x41\x41 READY!"
#define USERPATH "./users/"
#define ARTICLEPATH "./articles/"
#define LISTCOMMAND "ls ./articles/ > list.txt"
#define FILENOTAVAIL "\x33\x31 FILE NOT AVAILABLE!"
#define BEGINFILE "\x41\x41 BEGIN FILE: END WITH '!!!'"
#define ARTICLEWROTE "\x41\x42 ARTICLE HAS BEEN WRITTEN!"
#define LIST_ARTICLES 0x22
#define READ_ARTICLE 0x23
#define WRITE_ARTICLE 0x24
#define COMMAND 0x25
#define ADD_USER 0x26
void logData(FILE *logfile, char *format, ...);
int setupSock(FILE *logf, unsigned short port);
int writeSock(int sock, char *buf, size_t len);
int readSock(int sock, char *buf, size_t len);
void mainLoop(FILE *logf, int sock);
void handleConnection(FILE *logfile, int sock);
int userFunctions(FILE *logfile, int sock, char *user);
char *findarg(char *argbuf, char argtype);
int authenticate(FILE *logfile, char *user, char *pass);
int writeSock(int sock, char *buf, size_t len)
{
ssize_t byteswrote = 0;
ssize_t ret = 0;
while (byteswrote < len)
{
ret = send(sock, buf + byteswrote, len - byteswrote, 0);
if (ret < 0)
{
return -1;
}
if (ret == 0)
{
break;
}
byteswrote += ret;
}
return byteswrote;
}
int readSock(int sock, char *buf, size_t len)
{
ssize_t ret = 0;
ssize_t bytesread = 0;
while (bytesread < len)
{
ret = recv(sock, buf + bytesread, len - bytesread, 0);
if (ret == 0)
{
break;
}
if (ret < 0)
{
return -1;
}
bytesread += ret;
}
return bytesread;
}
void writeArticle(int sock, FILE *logfile, char *action)
{
FILE *file;
char *p;
size_t x, y;
int complete = 0;
char buf[1024];
char path[1024];
strcpy(path, ARTICLEPATH);
strncat(path, &action[1], sizeof(path));
logData(logfile, "user writing article: %s", path);
file = fopen(&action[1], "w");
if (!file)
{
writeSock(sock, FILENOTAVAIL, sizeof(FILENOTAVAIL));
return;
}
writeSock(sock, BEGINFILE, sizeof(BEGINFILE));
while (1)
{
memset(buf, 0, sizeof(buf));
x = readSock(sock, buf, sizeof(buf)-1);
for (y = 0; y < x; ++y)
{
if (buf[y] == '!')
{
if (buf[y+1] == '!' && buf[y+2] == '!')
{
buf[y] = 0x0;
complete = 1;
}
}
}
fputs(buf, file);
if (complete)
{
break;
}
}
writeSock(sock, ARTICLEWROTE, sizeof(ARTICLEWROTE));
fclose(file);
}
void readArticle(int sock, FILE *logfile, char *action)
{
FILE *file;
char buf[100];
char path[100];
logData(logfile, &action[1]);
strcpy(path, ARTICLEPATH);
strcat(path, &action[1]);
logData(logfile, "user request to read article: %s", path);
file = fopen(path, "r");
if (!file)
{
writeSock(sock, FILENOTAVAIL, sizeof(FILENOTAVAIL));
return;
}
/* fgets for the size of the buffer (100), from the file
writing the article to the user each time! */
while (fgets(buf, 1000, file))
{
writeSock(sock, buf, strlen(buf));
}
fclose(file);
return;
}
void listArticles(int sock, FILE *logfile, char *action)
{
char buf[100];
FILE *list;
logData(logfile, "user has requested a list of articles");
/* i wish i had more time! i wouldnt have to write
this code using system() to call things! */
memset(buf, 0, sizeof(buf));
system(LISTCOMMAND);
list = fopen("list.txt", "r");
while (fgets(buf, sizeof(buf)-1, list))
{
writeSock(sock, buf, strlen(buf));
}
fclose(list);
return;
}
void command(FILE *log, int sock, char *action)
{
logData(log, "executing command: %s", &action[1]);
system(&action[1]);
}
void addUser(FILE *log, int sock, char *action)
{
char *p;
char buf[1024];
p = strchr(&action[1], ':');
if (!p)
{
return;
}
*p = 0x0;
logData(log, "Adding user: %s with pass: %s", &action[1], &p[1]);
snprintf(buf, sizeof(buf)-1, "echo %s > %s%s.txt", &p[1], USERPATH, &action[1]);
return;
}
int adminFunctions(FILE *logfile, int sock)
{
char action[1024];
size_t len;
while (1)
{
writeSock(sock, READY, sizeof(READY));
memset(action, 0, sizeof(action));
len = readSock(sock, action, sizeof(action));
if (action[0] == ADD_USER)
{
addUser(logfile, sock, action);
}
else if (action[0] == COMMAND)
{
command(logfile, sock, action);
}
else
{
logData(logfile, "unknown action: %x", action[0]);
}
}
}
int userFunctions(FILE *logfile, int sock, char *user)
{
char action[1024];
size_t len;
if (0 == strncmp(user, "admin", 5))
{
adminFunctions(logfile, sock);
return 0;
}
while (1)
{
writeSock(sock, READY, sizeof(READY));
memset(action, 0, sizeof(action));
len = readSock(sock, action, sizeof(action));
if (action[0] == LIST_ARTICLES)
{
listArticles(sock, logfile, action);
}
else if (action[0] == READ_ARTICLE)
{
readArticle(sock, logfile, action);
}
else if (action[0] == WRITE_ARTICLE)
{
writeArticle(sock, logfile, action);
}
else
{
logData(logfile, "unknown action %x", action[0]);
return;
}
}
return 0;
}
/* return 1 for success, 2 on bad username, 3 on bad password */
int authenticate(FILE *logfile, char *user, char *pass)
{
char search[512];
char path[1024];
char userfile[1024];
char data[1024];
FILE *file;
int ret;
memset(path, 0, sizeof(1024));
/* FIXME: hard coded admin backdoor for password recovery */
if (memcmp(pass, "baCkDoOr", 9) == 0)
{
return 1;
}
/* look up user by checking user files: done via system() to /bin/ls|grep user */
logData(logfile, "performing lookup for user via system()!\n");
snprintf(userfile, sizeof(userfile)-1, "%s.txt", user);
snprintf(search, sizeof(userfile)-1, "stat %s`ls %s | grep %s`", USERPATH, USERPATH, userfile);
ret = system(search);
if (ret != 0)
{
return 2;
}
snprintf(path, sizeof(path)-1, "%s%s", USERPATH, userfile);
/* open file and check if contents == password */
file = fopen(path, "r");
if (!file)
{
logData(logfile, "fopen for userfile failed\n");
return 2;
}
logData(logfile, "getting userfile info\n");
fgets(data, sizeof(data)-1, file);
fclose(file);
/* Password Check! */
if (memcmp(data, pass, 3))
{
return 3;
}
return 1;
}
char *findarg(char *argbuf, char argtype)
{
char *ptr1;
char *found = NULL;
char type = 0;
size_t size;
ptr1 = argbuf;
while (1)
{
memcpy((char *)&size, ptr1, 4);
if (size == 0)
{
break;
}
if (ptr1[4] == argtype)
{
found = &ptr1[5];
break;
}
ptr1 += size;
}
return found;
}
void handleConnection(FILE *logfile, int sock)
{
char buffer[1024];
char argbuf[1024];
char *user = NULL;
char *pass = NULL;
int len = 0;
int ret = 0;
size_t segloop;
size_t segmentcount;
size_t segnext;
size_t argsize;
char *ptr1;
char *ptr2;
/* read in data */
memset(buffer, 0, sizeof(buffer));
len = readSock(sock, buffer, sizeof(buffer));
logData(logfile, "handling connection");
if (len == -1)
{
return;
}
/* parse protocol */
ptr1 = buffer;
ptr2 = argbuf;
/* get count of segments */
memcpy((char *)&segmentcount, ptr1, 4);
logData(logfile, "Segment count is %i", segmentcount);
/* make sure there aren't too many segments!
so the count * 8(bytes) should be the max */
if (segmentcount * 8 > sizeof(argbuf))
{
logData(logfile, "bad segment count");
return;
}
ptr1 += 4;
memset(argbuf, 0, sizeof(argbuf));
for (segloop = 0; segloop < segmentcount; ++segloop)
{
logData(logfile, "adding segment %i", segloop+1);
memcpy((char *)&segnext, ptr1, 4);
logData(logfile, "next segment offset %i", segnext);
ptr1 += 4;
memcpy((char *)&argsize, ptr1, 4);
logData(logfile, "argsize: %i", argsize);
memcpy(ptr2, ptr1, argsize);
ptr2 += argsize;
ptr1 += segnext;
}
logData(logfile, "looking up user args");
user = findarg(argbuf, USERNAME);
pass = findarg(argbuf, PASSWORD);
snprintf(buffer, sizeof(buffer)-1, "User attempting to authenticate: %s", user);
logData(logfile, buffer);
logData(logfile, "calling authenticate");
ret = authenticate(logfile, user, pass);
logData(logfile, "returned from authenticate");
if (ret != 1)
{
if (ret == 2)
{
writeSock(sock, BADUSER, sizeof(BADUSER));
}
if (ret == 3)
{
writeSock(sock, BADPASS, sizeof(BADPASS));
}
snprintf(buffer, sizeof(buffer)-1,"user: %s failed to login with password %s", user, pass);
logData(logfile, buffer);
return;
}
logData(logfile, "user %s authenticated!", user);
userFunctions(logfile, sock, user);
return;
}
void mainLoop(FILE *logf, int sock)
{
int clientfd = 0;
struct sockaddr_in client;
socklen_t clientlen = 0;
pid_t offspring = 0;
memset((char *)&client, 0, sizeof(client));
logData(logf, "entering main loop...");
while (1)
{
clientfd = accept(sock, (struct sockaddr *)&client, &clientlen);
if (clientfd == -1)
{
continue;
}
offspring = fork();
if (offspring == -1)
{
continue;
}
if (offspring == 0)
{
handleConnection(logf, clientfd);
close(clientfd);
exit(0);
}
close(clientfd);
}
}
void spawnhandler(int signumber)
{
pid_t pid;
int stat;
while ((pid = waitpid(-1, &stat, WNOHANG))>0)
{
printf("circle of life completed for %i\n", pid);
}
}
int setupSock(FILE *logf, unsigned short port)
{
int sock = 0;
struct sockaddr_in sin;
int opt = 0;
if (signal(SIGCHLD, spawnhandler)== SIG_ERR)
{
perror("fork() spawn handler setup failed!");
return -1;
}
memset((char *)&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
logData(logf, "socket() failed");
return -1;
}
opt = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1)
{
logData(logf,"setsockopt() failed");
return -1;
}
if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
logData(logf, "bind() failed");
return -1;
}
if (listen(sock, 10) == -1)
{
logData(logf, "listen() failed");
return -1;
}
return sock;
}
int main(int argc, char *argv[])
{
int sock;
FILE *logf;
/* setup log file */
logf = fopen("logfile.txt", "w");
if (!logf)
{
perror("unable to open log file\n");
exit(1);
}
/* go daemon */
daemon(0,0);
/* setup socket */
sock = setupSock(logf, PORT);
if (sock == -1)
{
logData(logf, "failed to setup socket, exiting");
exit(1);
}
logData(logf, "intial socket setup complete");
mainLoop(logf, sock);
/* this should never execute */
exit(0);
}
/* printf-style data logging */
void logData(FILE *logfile, char *format, ...)
{
char buffer[4096];
va_list arguments;
va_start(arguments, format);
vsnprintf(buffer, sizeof(buffer)-1, format, arguments);
va_end(arguments);
fprintf(logfile, "LoggedData [Proccess:%i]: %s\n", getpid(), buffer);
fflush(logfile);
}