-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
executable file
·434 lines (378 loc) · 18.1 KB
/
functions.php
File metadata and controls
executable file
·434 lines (378 loc) · 18.1 KB
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
<?php
function imported($conn, $router, $id_soubor, $date)
{
ini_set('max_execution_time', 0);
$date = substr($date, 1, 10);
//check for imported files in db and return if current processing file is there
$sql = "SELECT * FROM imported";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
if ($row["router"] == $router and $row["date"] == $date and $row["id"] == $id_soubor) {
return true;
}
}
return false;
} else {
return false;
}
}
function inset_imported($conn, $router, $id_soubor, $date)
{
$configs = include('config.php');
ini_set('max_execution_time', 0);
if ($configs['debug_echo'] == true) {
echo $insert_imported . "<br><br>";
}
//create insert for table imported and proces it
$insert_imported = "INSERT INTO `imported`(`router`, `date`, `id`) VALUES ($router, $date, $id_soubor)";
if ($conn->query($insert_imported) === true) {
if ($configs["log_print"] == true) {
$GLOBALS['log'] .= "New record created successfully: insert_imported\n\n";
}
if ($configs["dialog_echo"] == true) {
echo "<br>New record created successfully: insert_imported<br><br><br>";
}
} else {
if ($configs["log_print"] == true) {
$GLOBALS['log'] .= "Something go wrong: insert_imported\n\n";
$GLOBALS['log'] .= "$insert_imported\n\n";
$GLOBALS['save'] = true;
}
if ($configs["dialog_echo"] == true) {
echo "<br>Something go wrong: insert_imported<br><br><br>";
}
}
}
function insert_time($conn, $date)
{
$configs = include('config.php');
ini_set('max_execution_time', 0);
if ($configs['debug_echo'] == true) {
echo $insert_imported . "<br><br>";
}
//create insert for table time and proces it
$insert_time = "INSERT INTO `time`(`date`) VALUES ($date)";
if ($conn->query($insert_time) === true) {
if ($configs["log_print"] == true) {
$GLOBALS['log'] .= "New record created successfully: insert_time\n\n";
}
if ($configs["dialog_echo"] == true) {
echo "<br>New record created successfully: insert_time<br><br><br>";
}
} else {
if ($configs["log_print"] == true) {
$GLOBALS['log'] .= "Something go wrong: insert_time\n\n";
$GLOBALS['log'] .= "$insert_time\n\n";
$GLOBALS['save'] = true;
}
if ($configs["dialog_echo"] == true) {
echo "<br>Something go wrong: insert_time<br><br><br>";
}
}
}
function tabulka($where, $order, $limit, $checkbox)
{
$configs = include('config.php');
ini_set('max_execution_time', 0);
$servername = $configs["servername"];
$username = $configs["username"];
$password = $configs["password"];
$dbname = $configs["dbname"];
$order2 = "";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//will save data from array order to one string in order2
$i = 0;
while ($i != $checkbox and isset($_GET['filter' . $i]) and $_GET['filter' . $i] != "") {
$order2 = $order2 . ' ' . $order[$i];
$i++;
}
//create select for table
$select_table = "SELECT info.router, info.datetime, info.FW, info.id, info.rule, info.ipproto, info.recvif, info.iface, info.srcip, info.destip, info.srcport, info.destport, info.event, info.action FROM info $where $order2 $limit";
if ($configs['debug_echo'] == true) {
echo "<br>" . $select_table;
}
//proces slect in db
$select_table = $conn->query($select_table);
if ($select_table->num_rows > 0) {
// output data of each row
echo "<div>";
echo "<table>";
echo "\n";
echo "<tr><th>Router</th><th>Date</th><th>Category/ID</th><th>Rule</th><th>Proto</th><th>Src/Dst IF</th><th>Src/Dst IP</th><th>Src/Dst Port</th><th>Event/Action</th></tr>";
echo "\n";
// output data of each row
$i = 0;
while ($row = $select_table->fetch_assoc()) {
if ($configs["router"] == true and isset($configs[$row["router"]])) {
$row["router"] = $configs[$row["router"]];
}
if ($i % 2 == 0) {
echo "<tr><th>" . $row["router"] . "</th><th>" . $row["datetime"] . "</th><th>" . $row["FW"] . "<br><br>" . $row["id"] . "</th><th>" . $row["rule"] . "</th><th>" . $row["ipproto"] . "</th><th>" . $row["recvif"] . "<br><br>" . $row["iface"] . "</th><th>" . $row["srcip"] . "<br><br>" . $row["destip"] . "</th><th>" . $row["srcport"] . "<br><br>" . $row["destport"] . "</th><th>" . $row["event"] . "<br><br>" . $row["action"] . "</th></tr>";
} else {
echo "<tr><th>" . $row["router"] . "</th><th>" . $row["datetime"] . "</th><th>" . $row["FW"] . "<br><br>" . $row["id"] . "</th><th>" . $row["rule"] . "</th><th>" . $row["ipproto"] . "</th><th>" . $row["recvif"] . "<br><br>" . $row["iface"] . "</th><th>" . $row["srcip"] . "<br><br>" . $row["destip"] . "</th><th>" . $row["srcport"] . "<br><br>" . $row["destport"] . "</th><th>" . $row["event"] . "<br><br>" . $row["action"] . "</th></tr>";
}
echo "\n\n";
$i += 1;
}
echo "</table>";
echo "</div>";
echo "\n";
} else {
echo "<br><br>0 results";
}
$conn->close();
}
function create_db()
{
$configs = include('config.php');
ini_set('max_execution_time', 0);
$servername = $configs["servername"];
$username = $configs["username"];
$password = $configs["password"];
$dbname = $configs["dbname"];
$i = 1;
$sql[1] = "CREATE SCHEMA IF NOT EXISTS " . $configs["dbname"] . " DEFAULT CHARACTER SET utf8";
$sql[2] = "USE " . $configs["dbname"] . "";
$sql[3] = "CREATE TABLE IF NOT EXISTS " . $configs["dbname"] . ".`imported` (
`router` SMALLINT NOT NULL,
`date` DATE NOT NULL,
`id` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`router`, `date`, `id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8";
$sql[4] = "CREATE TABLE IF NOT EXISTS " . $configs["dbname"] . ".`time` (
`date` DATE NOT NULL,
PRIMARY KEY (`date`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8";
$sql[5] = "CREATE TABLE IF NOT EXISTS " . $configs["dbname"] . ".`info` (
`idPrimaryKey` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`router` VARCHAR(4) NOT NULL,
`datetime` DATETIME NOT NULL,
`FW` VARCHAR(45) NOT NULL,
`prio` TINYINT UNSIGNED NOT NULL,
`id` INT UNSIGNED NOT NULL,
`rev` TINYINT UNSIGNED NOT NULL,
`event` VARCHAR(45) NOT NULL,
`rule` VARCHAR(45) NOT NULL,
`ipproto` VARCHAR(10) NULL,
`ipdatalen` TINYINT UNSIGNED NULL,
`srcport` SMALLINT UNSIGNED NULL,
`destport` SMALLINT UNSIGNED NULL,
`tcphdrlen` TINYINT UNSIGNED NULL,
`syn` TINYINT UNSIGNED NULL,
`ece` TINYINT UNSIGNED NULL,
`cwr` TINYINT UNSIGNED NULL,
`ttl` TINYINT UNSIGNED NULL,
`ttlmin` TINYINT UNSIGNED NULL,
`udptotlen` TINYINT UNSIGNED NULL,
`ipaddr` VARCHAR(45) NULL,
`iface` VARCHAR(45) NULL,
`origsent` SMALLINT UNSIGNED NULL,
`termsent` SMALLINT UNSIGNED NULL,
`conntime` SMALLINT UNSIGNED NULL,
`conn` VARCHAR(45) NULL,
`action` VARCHAR(45) NULL,
`badflag` VARCHAR(45) NULL,
`recvif` VARCHAR(45) NULL,
`srcip` VARCHAR(45) NULL,
`destip` VARCHAR(45) NULL,
`ipdf` TINYINT UNSIGNED NULL,
`time_date` DATE NOT NULL,
PRIMARY KEY (`idPrimaryKey`),
INDEX `fk_info_time_idx` (`time_date` ASC),
CONSTRAINT `fk_info_time`
FOREIGN KEY (`time_date`)
REFERENCES " . $configs["dbname"] . ".`time` (`date`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB";
$conn = new mysqli($servername, $username, $password, $dbname);
//will create db
if ($conn->connect_error) {
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
while ($i != 6) {
if ($conn->query($sql[$i]) === true) { } else {
if ($configs["log_print"] == true) {
$GLOBALS['log'] .= "Something goes wrong: create db\n\n";
$GLOBALS['log'] .= "$sql[$i]\n\n";
$GLOBALS['save'] = true;
}
if ($configs["dialog_echo"] == true) {
echo "Something goes wrong/<br>";
}
}
$i++;
}
}
} else {
if ($configs["log_print"] == true) {
$GLOBALS['log'] .= "DB alredy exit\n\n";
}
if ($configs["dialog_echo"] == true) {
echo "DB alredy exit<br>";
}
}
$conn->close();
}
function processFileChunk($conn, $chunk, $router, $date)
{
ini_set('max_execution_time', 0);
//will create lines by date
$chunk = preg_replace(
'/\[\d{4}(?:-\d{2}){2} (?:\d{2}:){2}\d{2}]/',
"\n$0",
$chunk
);
//will delete random characters
$chunk = preg_replace(
'/\x00[^\x5B]*/',
"\n",
$chunk
);
//will separate lines to array
$lines = explode("\n", $chunk);
//will proces every line
foreach ($lines as $line) {
processLine($conn, $line, $router, $date);
}
}
function processLine($conn, string $line, $router, $date)
{
$configs = include('config.php');
ini_set('max_execution_time', 0);
$parsedLine = [];
//will save router id to array
$parsedLine["router"] = $router;
//will get datetime from line
$datetime = explode('[', $line, 1);
$datetime = preg_replace("/ /", "-", $datetime[0]);
$datetime = substr($datetime, 1, 19);
//will get FW and test if it is there if no it will skip to next line
$FW = explode('FW: ', $line, 2);
if (isset($FW[1])) {
$FW = explode(':', $FW[1], 2);
$FW = $FW[0];
//will separate items to item in format key=value
$items = explode(' ', $line);
//for evry item
foreach ($items as $item) {
//will separate item as key and value
$keyAndValue = explode('=', $item);
//if keyandvalue have 2 items it will add value to array with name as key
if (count($keyAndValue) == 2) {
if ($keyAndValue[0] == "connipproto" or $keyAndValue[0] == "connrecvif" or $keyAndValue[0] == "conndestif" or $keyAndValue[0] == "connsrcport" or $keyAndValue[0] == "conndestport" or $keyAndValue[0] == "connsrcip" or $keyAndValue[0] == "conndestip" or $keyAndValue[0] == "ipaddr") {
if ($keyAndValue[0] == "ipaddr") {
$parsedLine["srcip"] = $keyAndValue[1];
} else {
$parsedLine[substr($keyAndValue[0], 4)] = $keyAndValue[1];
}
} else {
$parsedLine[$keyAndValue[0]] = $keyAndValue[1];
}
}
}
//will take line and call creater_insert_info
if (!empty($parsedLine)) {
if ($GLOBALS["insert_count"] == 1) {
$GLOBALS["insert_info"] = "INSERT INTO `info`(`router`, `datetime`, `FW`, `prio`, `id`, `rev`, `event`, `rule`, `ipproto`, `ipdatalen`, `srcport`, `destport`, `tcphdrlen`, `syn`, `ece`, `cwr`, `ttl`, `ttlmin`, `udptotlen`, `ipaddr`, `iface`, `origsent`, `termsent`, `conntime`, `conn`, `action`, `badflag`, `recvif`, `srcip`, `destip`, `ipdf`, `time_date`) VALUES";
creater_insert_info($parsedLine, $datetime, $FW, $date);
} else {
if (($GLOBALS["insert_count"] % ($configs["insert"] + 1)) == 0) {
creater_insert_info($parsedLine, $datetime, $FW, $date);
$last5 = "INSERT INTO `info`(`router`, `datetime`, `FW`, `prio`, `id`, `rev`, `event`, `rule`, `ipproto`, `ipdatalen`, `srcport`, `destport`, `tcphdrlen`, `syn`, `ece`, `cwr`, `ttl`, `ttlmin`, `udptotlen`, `ipaddr`, `iface`, `origsent`, `termsent`, `conntime`, `conn`, `action`, `badflag`, `recvif`, `srcip`, `destip`, `ipdf`, `time_date`) VALUES";
$last5 = substr($last5, -5);
if (substr($GLOBALS["insert_info"], -5) != $last5) {
to_DB($conn);
} else {
$GLOBALS["insert_info"] = "INSERT INTO `info`(`router`, `datetime`, `FW`, `prio`, `id`, `rev`, `event`, `rule`, `ipproto`, `ipdatalen`, `srcport`, `destport`, `tcphdrlen`, `syn`, `ece`, `cwr`, `ttl`, `ttlmin`, `udptotlen`, `ipaddr`, `iface`, `origsent`, `termsent`, `conntime`, `conn`, `action`, `badflag`, `recvif`, `srcip`, `destip`, `ipdf`, `time_date`) VALUES";
creater_insert_info($parsedLine, $datetime, $FW, $date);
}
$GLOBALS["insert_count"] = 1;
} else {
creater_insert_info($parsedLine, $datetime, $FW, $date);
}
}
}
}
}
function creater_insert_info($parsedLine, $datetime, $FW, $date)
{
$configs = include('config.php');
ini_set('max_execution_time', 0);
//will values and save it in array info with name as key
$info = array("router" => "NULL", "datetime" => "NULL", "FW" => "NULL", "prio" => "NULL", "id" => "NULL", "rev" => "NULL", "event" => "NULL", "rule" => "NULL", "time_year" => "NULL", "time_month" => "NULL");
$key = array("router", "datetime", "FW", "prio", "id", "rev", "event", "rule", "time_year", "time_month");
$k = 0;
while ($k != 10) {
if (array_key_exists($key[$k], $parsedLine)) {
$info[$key[$k]] = '"' . $parsedLine[$key[$k]] . '"';
} else {
$info[$key[$k]] = '""';
}
$k++;
}
//will values and save it in array more_info with name as key
$more_info = array("ipproto" => "NULL", "ipdatalen" => "NULL", "srcport" => "NULL", "destport" => "NULL", "tcphdrlen" => "NULL", "syn" => "NULL", "ece" => "NULL", "cwr" => "NULL", "ttl" => "NULL", "ttlmin" => "NULL", "udptotlen" => "NULL", "ipaddr" => "NULL", "iface" => "NULL", "origsent" => "NULL", "srcport" => "NULL", "termsent" => "NULL", "conntime" => "NULL", "conn" => "NULL", "action" => "NULL", "badflag" => "NULL", "rule" => "NULL", "recvif" => "NULL", "srcip" => "NULL", "destip" => "NULL", "ipdf" => "NULL", "info_idPrimaryKey" => "NULL", );
$key = array("ipproto", "ipdatalen", "srcport", "destport", "tcphdrlen", "syn", "ece", "cwr", "ttl", "ttlmin", "udptotlen", "ipaddr", "iface", "origsent", "termsent", "conntime", "conn", "action", "badflag", "rule", "recvif", "srcip", "destip", "ipdf", "info_idPrimaryKey");
$k = 0;
while ($k != 25) {
if (array_key_exists($key[$k], $parsedLine)) {
$more_info[$key[$k]] = '"' . $parsedLine[$key[$k]] . '"';
}
$k++;
}
$last5 = "INSERT INTO `info`(`router`, `datetime`, `FW`, `prio`, `id`, `rev`, `event`, `rule`, `ipproto`, `ipdatalen`, `srcport`, `destport`, `tcphdrlen`, `syn`, `ece`, `cwr`, `ttl`, `ttlmin`, `udptotlen`, `ipaddr`, `iface`, `origsent`, `termsent`, `conntime`, `conn`, `action`, `badflag`, `recvif`, `srcip`, `destip`, `ipdf`, `time_date`) VALUES";
$last5 = substr($last5, -5);
//check if last 5 charakter isnt "VALUE" if no then inserrt it to db and create insert
if ($GLOBALS["insert_count"] != 1 and substr($GLOBALS["insert_info"], -5) != $last5) {
$GLOBALS["insert_info"] .= ", ($info[router], '$datetime', '$FW', $info[prio], $info[id], $info[rev], $info[event], $info[rule], $more_info[ipproto], $more_info[ipdatalen], $more_info[srcport], $more_info[destport], $more_info[tcphdrlen], $more_info[syn], $more_info[ece], $more_info[cwr], $more_info[ttl], $more_info[ttlmin], $more_info[udptotlen], $more_info[ipaddr], $more_info[iface], $more_info[origsent], $more_info[termsent], $more_info[conntime], $more_info[conn], $more_info[action], $more_info[badflag], $more_info[recvif], $more_info[srcip], $more_info[destip], $more_info[ipdf], $date)";
} else {
$GLOBALS["insert_info"] .= " ($info[router], '$datetime', '$FW', $info[prio], $info[id], $info[rev], $info[event], $info[rule], $more_info[ipproto], $more_info[ipdatalen], $more_info[srcport], $more_info[destport], $more_info[tcphdrlen], $more_info[syn], $more_info[ece], $more_info[cwr], $more_info[ttl], $more_info[ttlmin], $more_info[udptotlen], $more_info[ipaddr], $more_info[iface], $more_info[origsent], $more_info[termsent], $more_info[conntime], $more_info[conn], $more_info[action], $more_info[badflag], $more_info[recvif], $more_info[srcip], $more_info[destip], $more_info[ipdf], $date)";
}
$GLOBALS["insert_count"] += 1;
}
function to_DB($conn)
{
ini_set('max_execution_time', 0);
$configs = include('config.php');
$insert_info = $GLOBALS["insert_info"];
if ($configs['debug_echo'] == true) {
echo $insert_info . "<br><br>";
}
//insert to db
if ($conn->query($insert_info) === true) {
if ($configs["log_print"] == true) {
$GLOBALS['log'] .= "New record created successfully: insert_info\n\n";
}
if ($configs["dialog_echo"] == true) {
echo "<br>New record created successfully: insert_info<br>";
}
} else {
if ($configs["log_print"] == true) {
$GLOBALS['log'] .= "Something go wrong: insert_info\n\n";
$GLOBALS['log'] .= "$insert_info\n\n";
$GLOBALS['save'] = true;
}
if ($configs["dialog_echo"] == true) {
echo "<br>Something go wrong: insert_info<br><br><br>";
}
}
}
function save_log($filename, $text, $save)
{
//if something fale it will save
if ($save == TRUE) {
$filename = substr($filename, 6, 15);
$GLOBALS['log'] = fopen("logs/$filename.log", "at");
fwrite($GLOBALS['log'], $text);
fclose($GLOBALS['log']);
}
}