forked from MS3FGX/Bluelog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
livelog.c
426 lines (363 loc) · 9.76 KB
/
livelog.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
/*
* livelog - Simple CGI module for Bluelog Live mode
*
* Written by Tom Nardi (MS3FGX@gmail.com), released under the GPLv2.
* For more information, see: www.digifail.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
// Defines
#define APPNAME "livelog.cgi"
#define VERSION "1.1"
#define MAXNUM 4096
#define MAXLINE 500
#define INFO "/tmp/info.txt"
#define LOG "/tmp/live.log"
#define PID_FILE "/tmp/bluelog.pid"
// Conditionals
#if defined OPENWRT || PWNPLUG
#define CSSPREFIX "/bluelog/"
#else
#define CSSPREFIX "../"
#endif
// Global variables
int mobile;
// Found device struct
struct btdev
{
char time[20];
char addr[18];
char name[248];
char class[248];
char hwinfo[248];
};
// Global variables
// Device file
FILE *logfile;
// Status file
FILE *infofile;
// Init device cache, index
struct btdev dev_cache[MAXNUM];
int device_index = 0;
// Experimental, print all HTML from CGI module
void print_html(char *CSSFILE)
{
// File info
printf("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
// Boilerplate
printf("<!--This file created by %s (v%s) by MS3FGX-->\n", APPNAME, VERSION);
printf("<html>\n"
"<head>\n"
"<title>Bluelog Live</title>\n"
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" >\n");
// CSS file prefix
printf("<link href=\"%s%s\" type=\"text/css\" rel=\"stylesheet\" /></head><body>\n", CSSPREFIX,CSSFILE);
printf("<link rel=\"icon\" type=\"image/png\" href=\"/images/favicon.png\" />\n"
"<link rel=\"shortcut icon\" href=\"/images/favicon.png\" />\n"
"<META HTTP-EQUIV=\"refresh\" CONTENT=\"20\">\n"
"</head>\n"
"<body>\n"
"<div id=\"container\">\n"
"<div id=\"header\">\n"
"<span style=\"position:relative;left:15px;top:45px\">\n"
"<a href=\"http://www.digifail.com/\" target=\"_blank\" title=\"DigiFAIL\"><img src=\"/images/digifail_logo.png\" border=\"0\"></a>\n"
"</span>\n"
"</div>\n");
printf("<div class=\"footer\">\n"
"<h4>\n"
"<a href=\"about.html\" onClick=\"window.open('about.html','About','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=500,height=320,left=50,top=50'); return false;\">About</a>\n"
" \n"
"<a href=\"contact.html\" onClick=\"window.open('contact.html','Contact','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=500,height=510,left=50,top=50'); return false;\">Contact</a>\n"
"</h4>\n"
"</div>\n");
}
void print_header(char *CSSFILE)
{
// CGI header
printf("Content-type: text/html\n\n");
// Boilerplate
printf("<!--This file created with %s (v%s) by MS3FGX-->\n", APPNAME, VERSION);
// HTML head
printf("<html><head><link href=\"%s%s\" type=\"text/css\" rel=\"stylesheet\" /></head><body>\n", CSSPREFIX,CSSFILE);
}
void setup_table()
{
// Table setup
puts("<table border=\"1\" cellpadding=\"5\" cellspacing=\"5\" width=\"100%\">\n");
if (!mobile)
{
puts("<tr>\n"\
"<th>Time Discovered</th>\n"\
"<th>MAC Address</th>\n"\
"<th>Device Name</th>\n"\
"<th>Device Class</th>\n"\
"<th>Hardware Info</th>\n"\
"</tr>\n");
}
else
{
puts("<tr>\n"\
"<th>Time</th>\n"\
"<th>MAC</th>\n"\
"<th>Name</th>\n"\
"<th>Class</th>\n"\
"</tr>\n");
}
}
void print_info()
{
// Line length
char line [128];
while (fgets(line,sizeof line,infofile))
fputs(line,stdout);
}
void read_log()
{
char line [MAXLINE];
while (fgets(line,sizeof line,logfile))
{
// Copy line to buffer
char* line_buffer = strdup(line);
// Populate cache entry from file
strcpy(dev_cache[device_index].time,strsep(&line_buffer, ","));
strcpy(dev_cache[device_index].addr,strsep(&line_buffer, ","));
strcpy(dev_cache[device_index].name,strsep(&line_buffer, ","));
strcpy(dev_cache[device_index].class,strsep(&line_buffer, ","));
strcpy(dev_cache[device_index].hwinfo,strsep(&line_buffer, "\n"));
// Increment index
device_index++;
}
}
void print_devices()
{
while (device_index > 0)
{
// Write out valid table HTML
printf("<tr>");
printf("<td>%s</td>",dev_cache[device_index-1].time);
printf("<td>%s</td>",dev_cache[device_index-1].addr);
// Before writing out device name to HTML, do some VERY basic sanitization (not safe, just to block obvious stuff)
if ((strstr(dev_cache[device_index-1].name, ">")) || (strstr(dev_cache[device_index-1].name, "</")))
strcpy(dev_cache[device_index-1].name, "<p style='color:red;'>Blocked Possible Exploit</p>");
printf("<td>%s</td>",dev_cache[device_index-1].name);
printf("<td>%s</td>",dev_cache[device_index-1].class);
if(!mobile)
printf("<td>%s</td>",dev_cache[device_index-1].hwinfo);
printf("</tr>\n");
device_index--;
}
}
int read_pid()
{
// Any error will return 0
FILE *pid_file;
int pid;
if (!(pid_file=fopen(PID_FILE,"r")))
return 0;
if (fscanf(pid_file,"%d", &pid) < 0)
pid = 0;
fclose(pid_file);
return pid;
}
void TopBar()
{
// Discovered devices display
puts("<div id=\"content\">\n");
printf("Discovered Devices: %i</div>\n",device_index);
// Close up info pane
puts("</div>\n");
}
void SideBar()
{
// Start sidebar
puts("<div id=\"sidebar\">\n");
// Aux pane
puts("<div id=\"sideobject\">\n"\
"<div id=\"auxbox1\">\n"\
"</div>\n"\
"</div>\n");
// Start info pane
puts("<div id=\"sideobject\">\n"\
"<div id=\"boxheader\">Info</div>\n"\
"<div id=\"sidebox\">\n"\
"<div id=\"sidecontent\">");
// Populate sidebar from files
print_info();
// Close info pane
puts("</div>\n"\
"</div>\n"\
"</div>\n");
// Status pane
puts("<div id=\"sideobject\">\n"\
"<div id=\"boxheader\">Status</div>\n"\
"<div id=\"sidebox\">\n"\
"<div id=\"sidecontent\">");
// Print current PID status
puts("<div class=\"sideitem\">");
puts("Bluelog: ");
if (read_pid() != 0)
puts("<span style=\"color: #00ff00;\"><b>RUNNING</b></span></div>\n");
else
puts("<span style=\"color: #ff0000;\"><b>STOPPED</b></span></div>\n");
// Print number of discovered devices
puts("<div class=\"sideitem\">");
printf("Discovered Devices: %i</div>\n",device_index);
// Close status pane
puts("</div>\n"\
"</div>\n"\
"</div>\n");
// Aux pane
puts("<div id=\"sideobject\">\n"\
"<div id=\"auxbox2\">\n"\
"</div>\n"\
"</div>\n");
// Close sidebar
puts("</div>\n");
}
void shut_down(void)
{
// Close files
fclose(infofile);
fclose(logfile);
exit(1);
}
static void help(void)
{
printf("%s (v%s) by MS3FGX\n", APPNAME, VERSION);
printf("----------------------------------------------------------------\n");
printf("This is a CGI module used to create a live webpage of Bluelog\n"
"results. This module simply parses the log files created by\n"
"Bluelog, it has no scanning or logging capability of its own.\n");
printf("\n");
printf("For more information, see www.digifail.com\n");
printf("\n");
printf("Options:\n"
"\t-m Mobile format\n"
"\t-h Display help\n"
"\t-d Print Debug Info\n"
"\t-v Print Version Info\n"
"\n");
}
static void debug(void)
{
printf("System Variables\n");
printf("--------------------------\n");
printf("Module Version: %s\n", VERSION);
printf("Max Devices: %i\n",MAXNUM);
printf("Max Line Length: %i\n",MAXLINE);
printf("\n");
printf("File Locations\n");
printf("--------------------------\n");
printf("Info File: %s\n",INFO);
printf("Log File: %s\n", LOG);
printf("CSS Prefix: %s\n",CSSPREFIX);
}
static struct option main_options[] = {
{ "help", 0, 0, 'h' },
{ "debug", 0, 0, 'd' },
{ "version", 0, 0, 'v' },
{ "mobile", 0, 0, 'm' },
{ 0, 0, 0, 0 }
};
int main(int argc, char *argv[])
{
// Variables
// Default CSS
char CSSFILE[12]="style.css";
// Pointers to filenames
char *infofilename = INFO;
char *logfilename = LOG;
// Handle arguments
int opt;
while ((opt=getopt_long(argc, argv, "dvhm", main_options, NULL)) != EOF)
{
switch (opt)
{
case 'h':
help();
exit(0);
case 'd':
debug();
exit(0);
case 'v':
printf("%s (v%s) by MS3FGX\n", APPNAME, VERSION);
exit(0);
break;
case 'm':
strcpy(CSSFILE, "mobile.css");
mobile = 1;
break;
default:
printf("Unknown option.\n");
exit(0);
}
}
// Bail out if we are root, except on WRT
#ifndef OPENWRT
if(getuid() == 0)
{
syslog(LOG_ERR,"CGI module refusing to run as root!");
// Make sure error message is themed
print_header(CSSFILE);
puts("<div id=\"content\">");
puts("Server attempting to run CGI module as root, bailing out!");
puts("<p>");
puts("Check your web server configuration and try again.");
puts("</body></html>");
exit(1);
}
#endif
// Read in environment variable
// Comment this to fix compiler warning until ready to implement
//char* env_string;
//env_string=getenv("QUERY_STRING");
// Print HTML head
print_header(CSSFILE);
//print_html(CSSFILE);
// Start container div
if (!mobile)
puts("<div id=\"container\">\n");
// Open files
if ((infofile = fopen(infofilename, "r")) == NULL)
{
syslog(LOG_ERR,"Error while opening %s!",infofilename);
puts("<div id=\"content\">");
printf("Error while opening %s!\n",infofilename);
puts("</body></html>");
exit(1);
}
if ((logfile = fopen(logfilename, "r")) == NULL)
{
syslog(LOG_ERR,"Error while opening %s!",logfilename);
puts("<div id=\"content\">");
printf("Error while opening %s!\n",logfilename);
puts("</body></html>");
exit(1);
}
// Draw sidebar\topbar
read_log();
if (!mobile)
SideBar();
else
TopBar();
// Content window
puts("<div id=\"content\">");
if (device_index > 0)
{
// Print results
setup_table();
print_devices();
puts("</table>\n");
}
// Close content, body, and HTML
puts("</div></body></html>");
// Close files and exit
shut_down();
return 0;
}