1
|
<?php
|
2
|
/*
|
3
|
* snort_check_for_rule_updates.php
|
4
|
*
|
5
|
* Copyright (C) 2006 Scott Ullrich
|
6
|
* Copyright (C) 2009 Robert Zelaya
|
7
|
* Copyright (C) 2011-2012 Ermal Luci
|
8
|
* All rights reserved.
|
9
|
*
|
10
|
* Redistribution and use in source and binary forms, with or without
|
11
|
* modification, are permitted provided that the following conditions are met:
|
12
|
*
|
13
|
* 1. Redistributions of source code must retain the above copyright notice,
|
14
|
* this list of conditions and the following disclaimer.
|
15
|
*
|
16
|
* 2. Redistributions in binary form must reproduce the above copyright
|
17
|
* notice, this list of conditions and the following disclaimer in the
|
18
|
* documentation and/or other materials provided with the distribution.
|
19
|
*
|
20
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
21
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
22
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
23
|
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
24
|
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
25
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
26
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
27
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
28
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29
|
* POSSIBILITY OF SUCH DAMAGE.
|
30
|
*/
|
31
|
|
32
|
require_once("functions.inc");
|
33
|
require_once("service-utils.inc");
|
34
|
require_once("/usr/local/pkg/snort/snort.inc");
|
35
|
|
36
|
global $snort_gui_include;
|
37
|
|
38
|
$snortdir = SNORTDIR;
|
39
|
|
40
|
if (!isset($snort_gui_include))
|
41
|
$pkg_interface = "console";
|
42
|
|
43
|
$tmpfname = "{$snortdir}/tmp/snort_rules_up";
|
44
|
$snort_filename_md5 = "{$snort_rules_file}.md5";
|
45
|
$snort_filename = "{$snort_rules_file}";
|
46
|
$emergingthreats_filename_md5 = "emerging.rules.tar.gz.md5";
|
47
|
$emergingthreats_filename = "emerging.rules.tar.gz";
|
48
|
|
49
|
/* define checks */
|
50
|
$oinkid = $config['installedpackages']['snortglobal']['oinkmastercode'];
|
51
|
$snortdownload = $config['installedpackages']['snortglobal']['snortdownload'];
|
52
|
$emergingthreats = $config['installedpackages']['snortglobal']['emergingthreats'];
|
53
|
$vrt_enabled = $config['installedpackages']['snortglobal']['snortdownload'];
|
54
|
$et_enabled = $config['installedpackages']['snortglobal']['emergingthreats'];
|
55
|
|
56
|
/* Start of code */
|
57
|
conf_mount_rw();
|
58
|
|
59
|
if (!is_dir($tmpfname))
|
60
|
exec("/bin/mkdir -p {$tmpfname}");
|
61
|
|
62
|
/* Set user agent to Mozilla */
|
63
|
ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0)');
|
64
|
ini_set("memory_limit","150M");
|
65
|
|
66
|
/* remove old $tmpfname files */
|
67
|
if (is_dir("{$tmpfname}"))
|
68
|
exec("/bin/rm -r {$tmpfname}");
|
69
|
|
70
|
/* Make sure snortdir exits */
|
71
|
exec("/bin/mkdir -p {$snortdir}/rules");
|
72
|
exec("/bin/mkdir -p {$snortdir}/signatures");
|
73
|
exec("/bin/mkdir -p {$tmpfname}");
|
74
|
exec("/bin/mkdir -p /usr/local/lib/snort/dynamicrules");
|
75
|
|
76
|
/* download md5 sig from snort.org */
|
77
|
if ($snortdownload == 'on') {
|
78
|
update_status(gettext("Downloading snort.org md5 file..."));
|
79
|
$max_tries = 4;
|
80
|
while ($max_tries > 0) {
|
81
|
$image = @file_get_contents("http://www.snort.org/pub-bin/oinkmaster.cgi/{$oinkid}/{$snort_filename_md5}");
|
82
|
if (false === $image) {
|
83
|
$max_tries--;
|
84
|
if ($max_tries > 0)
|
85
|
sleep(30);
|
86
|
continue;
|
87
|
} else
|
88
|
break;
|
89
|
}
|
90
|
log_error("Snort MD5 Attempts: " . (4 - $max_tries + 1));
|
91
|
@file_put_contents("{$tmpfname}/{$snort_filename_md5}", $image);
|
92
|
if (0 == filesize("{$tmpfname}/{$snort_filename_md5}")) {
|
93
|
update_status(gettext("Please wait... You may only check for New Rules every 15 minutes..."));
|
94
|
log_error(gettext("Please wait... You may only check for New Rules every 15 minutes..."));
|
95
|
update_output_window(gettext("Rules are released every month from snort.org. You may download the Rules at any time."));
|
96
|
$snortdownload = 'off';
|
97
|
} else
|
98
|
update_status(gettext("Done downloading snort.org md5"));
|
99
|
}
|
100
|
|
101
|
/* Check if were up to date snort.org */
|
102
|
if ($snortdownload == 'on') {
|
103
|
if (file_exists("{$snortdir}/{$snort_filename_md5}")) {
|
104
|
$md5_check_new = file_get_contents("{$tmpfname}/{$snort_filename_md5}");
|
105
|
$md5_check_old = file_get_contents("{$snortdir}/{$snort_filename_md5}");
|
106
|
if ($md5_check_new == $md5_check_old) {
|
107
|
update_status(gettext("Snort rules are up to date..."));
|
108
|
log_error("Snort rules are up to date...");
|
109
|
$snortdownload = 'off';
|
110
|
}
|
111
|
}
|
112
|
}
|
113
|
|
114
|
/* download snortrules file */
|
115
|
if ($snortdownload == 'on') {
|
116
|
update_status(gettext("There is a new set of Snort.org rules posted. Downloading..."));
|
117
|
log_error(gettext("There is a new set of Snort.org rules posted. Downloading..."));
|
118
|
$max_tries = 4;
|
119
|
while ($max_tries > 0) {
|
120
|
download_file_with_progress_bar("http://www.snort.org/pub-bin/oinkmaster.cgi/{$oinkid}/{$snort_filename}", "{$tmpfname}/{$snort_filename}");
|
121
|
if (300000 > filesize("{$tmpfname}/$snort_filename")){
|
122
|
$max_tries--;
|
123
|
if ($max_tries > 0)
|
124
|
sleep(30);
|
125
|
continue;
|
126
|
} else
|
127
|
break;
|
128
|
}
|
129
|
update_status(gettext("Done downloading rules file."));
|
130
|
log_error("Snort Rules Attempts: " . (4 - $max_tries + 1));
|
131
|
if (300000 > filesize("{$tmpfname}/$snort_filename")){
|
132
|
update_output_window(gettext("Snort rules file download failed..."));
|
133
|
log_error(gettext("Snort rules file download failed..."));
|
134
|
log_error("Failed Rules Filesize: " . filesize("{$tmpfname}/$snort_filename"));
|
135
|
$snortdownload = 'off';
|
136
|
}
|
137
|
}
|
138
|
|
139
|
/* download md5 sig from emergingthreats.net */
|
140
|
if ($emergingthreats == 'on') {
|
141
|
update_status(gettext("Downloading emergingthreats md5 file..."));
|
142
|
$image = @file_get_contents("http://rules.emergingthreats.net/open/snort-{$emerging_threats_version}/emerging.rules.tar.gz.md5");
|
143
|
/* XXX: error checking */
|
144
|
@file_put_contents("{$tmpfname}/{$emergingthreats_filename_md5}", $image);
|
145
|
update_status(gettext("Done downloading emergingthreats md5"));
|
146
|
|
147
|
if (file_exists("{$snortdir}/{$emergingthreats_filename_md5}")) {
|
148
|
/* Check if were up to date emergingthreats.net */
|
149
|
$emerg_md5_check_new = file_get_contents("{$tmpfname}/{$emergingthreats_filename_md5}");
|
150
|
$emerg_md5_check_old = file_get_contents("{$snortdir}/{$emergingthreats_filename_md5}");
|
151
|
if ($emerg_md5_check_new == $emerg_md5_check_old) {
|
152
|
update_status(gettext("Emerging threat rules are up to date..."));
|
153
|
log_error(gettext("Emerging threat rules are up to date..."));
|
154
|
$emergingthreats = 'off';
|
155
|
}
|
156
|
}
|
157
|
}
|
158
|
|
159
|
/* download emergingthreats rules file */
|
160
|
if ($emergingthreats == "on") {
|
161
|
update_status(gettext("There is a new set of Emergingthreats rules posted. Downloading..."));
|
162
|
log_error(gettext("There is a new set of Emergingthreats rules posted. Downloading..."));
|
163
|
download_file_with_progress_bar("http://rules.emergingthreats.net/open/snort-{$emerging_threats_version}/emerging.rules.tar.gz", "{$tmpfname}/{$emergingthreats_filename}");
|
164
|
update_status(gettext('Done downloading Emergingthreats rules file.'));
|
165
|
log_error("Emergingthreats rules file update downloaded succsesfully");
|
166
|
}
|
167
|
|
168
|
/* Normalize rulesets */
|
169
|
$sedcmd = "s/^#alert/# alert/g\n";
|
170
|
$sedcmd .= "s/^##alert/# alert/g\n";
|
171
|
$sedcmd .= "s/^#[ \\t#]*alert/# alert/g\n";
|
172
|
$sedcmd .= "s/^##\\talert/# alert/g\n";
|
173
|
$sedcmd .= "s/^\\talert/alert/g\n";
|
174
|
$sedcmd .= "s/^[ \\t]*alert/alert/g\n";
|
175
|
@file_put_contents("{$snortdir}/tmp/sedcmd", $sedcmd);
|
176
|
|
177
|
/* Untar emergingthreats rules to tmp */
|
178
|
if ($emergingthreats == 'on') {
|
179
|
safe_mkdir("{$snortdir}/tmp/emerging");
|
180
|
if (file_exists("{$tmpfname}/{$emergingthreats_filename}")) {
|
181
|
update_status(gettext("Extracting EmergingThreats.org rules..."));
|
182
|
exec("/usr/bin/tar xzf {$tmpfname}/{$emergingthreats_filename} -C {$snortdir}/tmp/emerging rules/");
|
183
|
|
184
|
$files = glob("{$snortdir}/tmp/emerging/rules/*.rules");
|
185
|
foreach ($files as $file) {
|
186
|
$newfile = basename($file);
|
187
|
@copy($file, "{$snortdir}/rules/{$newfile}");
|
188
|
}
|
189
|
/* IP lists for Emerging Threats rules */
|
190
|
$files = glob("{$snortdir}/tmp/emerging/rules/*.txt");
|
191
|
foreach ($files as $file) {
|
192
|
$newfile = basename($file);
|
193
|
@copy($file, "{$snortdir}/rules/{$newfile}");
|
194
|
}
|
195
|
/* base etc files for Emerging Threats rules */
|
196
|
foreach (array("classification.config", "reference.config", "gen-msg.map", "unicode.map") as $file) {
|
197
|
if (file_exists("{$snortdir}/tmp/emerging/rules/{$file}"))
|
198
|
@copy("{$snortdir}/tmp/emerging/rules/{$file}", "{$snortdir}/ET_{$file}");
|
199
|
}
|
200
|
|
201
|
/* make sure default rules are in the right format */
|
202
|
exec("/usr/bin/sed -I '' -f {$snortdir}/tmp/sedcmd {$snortdir}/rules/emerging*.rules");
|
203
|
|
204
|
/* Copy emergingthreats md5 sig to snort dir */
|
205
|
if (file_exists("{$tmpfname}/$emergingthreats_filename_md5")) {
|
206
|
update_status(gettext("Copying md5 sig to snort directory..."));
|
207
|
@copy("{$tmpfname}/$emergingthreats_filename_md5", "{$snortdir}/$emergingthreats_filename_md5");
|
208
|
}
|
209
|
update_status(gettext("Extraction of EmergingThreats.org rules completed..."));
|
210
|
}
|
211
|
}
|
212
|
|
213
|
/* Untar snort rules file individually to help people with low system specs */
|
214
|
if ($snortdownload == 'on') {
|
215
|
if (file_exists("{$tmpfname}/{$snort_filename}")) {
|
216
|
if ($pfsense_stable == 'yes')
|
217
|
$freebsd_version_so = 'FreeBSD-7-2';
|
218
|
else
|
219
|
$freebsd_version_so = 'FreeBSD-8-1';
|
220
|
|
221
|
update_status(gettext("Extracting Snort VRT rules..."));
|
222
|
/* extract snort.org rules and add prefix to all snort.org files*/
|
223
|
safe_mkdir("{$snortdir}/tmp/snortrules");
|
224
|
exec("/usr/bin/tar xzf {$tmpfname}/{$snort_filename} -C {$snortdir}/tmp/snortrules rules/");
|
225
|
$files = glob("{$snortdir}/tmp/snortrules/rules/*.rules");
|
226
|
foreach ($files as $file) {
|
227
|
$newfile = basename($file);
|
228
|
@copy($file, "{$snortdir}/rules/snort_{$newfile}");
|
229
|
}
|
230
|
/* IP lists */
|
231
|
$files = glob("{$snortdir}/tmp/snortrules/rules/*.txt");
|
232
|
foreach ($files as $file) {
|
233
|
$newfile = basename($file);
|
234
|
@copy($file, "{$snortdir}/rules/{$newfile}");
|
235
|
}
|
236
|
exec("rm -r {$snortdir}/tmp/snortrules");
|
237
|
|
238
|
/* extract so rules */
|
239
|
update_status(gettext("Extracting Snort VRT Shared Objects rules..."));
|
240
|
exec('/bin/mkdir -p /usr/local/lib/snort/dynamicrules/');
|
241
|
$snort_arch = php_uname("m");
|
242
|
$nosorules = false;
|
243
|
if ($snort_arch == 'i386'){
|
244
|
exec("/usr/bin/tar xzf {$tmpfname}/{$snort_filename} -C {$snortdir}/tmp so_rules/precompiled/$freebsd_version_so/i386/{$snort_version}/");
|
245
|
exec("/bin/cp {$snortdir}/tmp/so_rules/precompiled/$freebsd_version_so/i386/{$snort_version}/* /usr/local/lib/snort/dynamicrules/");
|
246
|
} else if ($snort_arch == 'amd64') {
|
247
|
exec("/usr/bin/tar xzf {$tmpfname}/{$snort_filename} -C {$snortdir}/tmp so_rules/precompiled/$freebsd_version_so/x86-64/{$snort_version}/");
|
248
|
exec("/bin/cp {$snortdir}/tmp/so_rules/precompiled/$freebsd_version_so/x86-64/{$snort_version}/* /usr/local/lib/snort/dynamicrules/");
|
249
|
} else
|
250
|
$nosorules = true;
|
251
|
exec("rm -r {$snortdir}/tmp/so_rules");
|
252
|
|
253
|
if ($nosorules == false) {
|
254
|
/* extract so rules none bin and rename */
|
255
|
update_status(gettext("Copying Snort VRT Shared Objects rules..."));
|
256
|
exec("/usr/bin/tar xzf {$tmpfname}/{$snort_filename} -C {$snortdir}/tmp so_rules/");
|
257
|
$files = glob("{$snortdir}/tmp/so_rules/*.rules");
|
258
|
foreach ($files as $file) {
|
259
|
$newfile = basename($file, ".rules");
|
260
|
@copy($file, "{$snortdir}/rules/snort_{$newfile}.so.rules");
|
261
|
}
|
262
|
exec("rm -r {$snortdir}/tmp/so_rules");
|
263
|
|
264
|
/* extract base etc files */
|
265
|
update_status(gettext("Extracting Snort VRT base config files..."));
|
266
|
exec("/usr/bin/tar xzf {$tmpfname}/{$snort_filename} -C {$snortdir}/tmp etc/");
|
267
|
foreach (array("classification.config", "reference.config", "gen-msg.map", "unicode.map") as $file) {
|
268
|
if (file_exists("{$snortdir}/tmp/etc/{$file}"))
|
269
|
@copy("{$snortdir}/tmp/etc/{$file}", "{$snortdir}/VRT_{$file}");
|
270
|
}
|
271
|
exec("rm -r {$snortdir}/tmp/etc");
|
272
|
|
273
|
/* Untar snort signatures */
|
274
|
$signature_info_chk = $config['installedpackages']['snortglobal']['signatureinfo'];
|
275
|
if ($premium_url_chk == 'on') {
|
276
|
update_status(gettext("Extracting Snort VRT Signatures..."));
|
277
|
exec("/usr/bin/tar xzf {$tmpfname}/{$snort_filename} -C {$snortdir} doc/signatures/");
|
278
|
update_status(gettext("Done extracting Signatures."));
|
279
|
|
280
|
if (is_dir("{$snortdir}/doc/signatures")) {
|
281
|
update_status(gettext("Copying Snort VRT signatures..."));
|
282
|
exec("/bin/cp -r {$snortdir}/doc/signatures {$snortdir}/signatures");
|
283
|
update_status(gettext("Done copying signatures."));
|
284
|
}
|
285
|
}
|
286
|
|
287
|
foreach (glob("/usr/local/lib/snort/dynamicrules/*example*") as $file)
|
288
|
@unlink($file);
|
289
|
|
290
|
exec("/usr/bin/tar xzf {$tmpfname}/{$snort_filename} -C {$snortdir} preproc_rules/");
|
291
|
|
292
|
/* make sure default rules are in the right format */
|
293
|
exec("/usr/bin/sed -I '' -f {$snortdir}/tmp/sedcmd {$snortdir}/rules/snort_*.rules");
|
294
|
|
295
|
if (file_exists("{$tmpfname}/{$snort_filename_md5}")) {
|
296
|
update_status(gettext("Copying md5 sig to snort directory..."));
|
297
|
@copy("{$tmpfname}/$snort_filename_md5", "{$snortdir}/$snort_filename_md5");
|
298
|
}
|
299
|
}
|
300
|
update_status(gettext("Extraction of Snort VRT rules completed..."));
|
301
|
}
|
302
|
}
|
303
|
|
304
|
/* remove old $tmpfname files */
|
305
|
if (is_dir("{$snortdir}/tmp")) {
|
306
|
update_status(gettext("Cleaning up after rules extraction..."));
|
307
|
exec("/bin/rm -r {$snortdir}/tmp");
|
308
|
}
|
309
|
|
310
|
function build_sid_msg_map($rules_dir, $sid_file) {
|
311
|
|
312
|
$sidMap = array();
|
313
|
|
314
|
/* Normalize the supplied rules path to have a trailing */
|
315
|
/* slash in the directory name. */
|
316
|
$my_rules_dir = ((strrpos($rules_dir, '/') + 1) == strlen($rules_dir)) ? $rules_dir : $rules_dir . "/";
|
317
|
|
318
|
/* Read the rule files into an array, then interate the list */
|
319
|
foreach (glob($my_rules_dir . "*.rules") as $file) {
|
320
|
|
321
|
/* Don't process files with "deleted" in the filename */
|
322
|
if (preg_match('/deleted/i', $file))
|
323
|
continue;
|
324
|
|
325
|
/* Read the file into an array, skipping empty lines. */
|
326
|
$rules_array = file($file, FILE_SKIP_EMPTY_LINES);
|
327
|
$record = "";
|
328
|
$b_Multiline = false;
|
329
|
|
330
|
/* Read and process each line from the rules in the */
|
331
|
/* current file. */
|
332
|
foreach ($rules_array as $rule) {
|
333
|
|
334
|
/* Skip any non-rule lines unless we're in */
|
335
|
/* multiline mode.
|
336
|
if (!preg_match('/^\s*#*\s*(alert|drop|pass)/i', $rule) && !$b_Multiline)
|
337
|
continue;
|
338
|
|
339
|
/* Skip disabled rules or comment lines. */
|
340
|
if (preg_match('/^\s*#/', $rule))
|
341
|
continue;
|
342
|
|
343
|
/* Test for a multi-line rule, and reassemble the */
|
344
|
/* pieces back into a single line. */
|
345
|
if (preg_match('/\\\\s*[\n]$/m', $rule)) {
|
346
|
$rule = substr($rule, 0, strrpos($rule, '\\'));
|
347
|
$record .= $rule;
|
348
|
$b_Multiline = true;
|
349
|
continue;
|
350
|
}
|
351
|
/* If the last segment of a multiline rule, then */
|
352
|
/* append it onto the previous parts to form a */
|
353
|
/* single-line rule for further processing below. */
|
354
|
elseif (!preg_match('/\\\\s*[\n]$/m', $rule) && $b_Multiline) {
|
355
|
$record .= $rule;
|
356
|
$rule = $record;
|
357
|
}
|
358
|
$b_Multiline = false;
|
359
|
$record = "";
|
360
|
|
361
|
/* Parse the rule to find sid and any references. */
|
362
|
$sid = '';
|
363
|
$msg = '';
|
364
|
$matches = '';
|
365
|
$sidEntry = '';
|
366
|
if (preg_match('/\bmsg\s*:\s*"(.+?)"\s*;/i', $rule, $matches))
|
367
|
$msg = trim($matches[1]);
|
368
|
if (preg_match('/\bsid\s*:\s*(\d+)\s*;/i', $rule, $matches))
|
369
|
$sid = trim($matches[1]);
|
370
|
if (!empty($sid) && !empty($msg)) {
|
371
|
$sidEntry = $sid . ' || ' . $msg;
|
372
|
preg_match_all('/\breference\s*:\s*([^\;]+)/i', $rule, $matches);
|
373
|
foreach ($matches[1] as $ref)
|
374
|
$sidEntry .= " || " . trim($ref);
|
375
|
$sidEntry .= "\n";
|
376
|
$sidMap[$sid] = $sidEntry;
|
377
|
}
|
378
|
}
|
379
|
}
|
380
|
/* Sort the generated sid-msg map by sid */
|
381
|
ksort($sidMap);
|
382
|
|
383
|
/* Now print the result to the supplied file */
|
384
|
file_put_contents($sid_file, array_values($sidMap));
|
385
|
}
|
386
|
|
387
|
function snort_merge_reference_configs($cfg_in, $cfg_out) {
|
388
|
$outMap = array();
|
389
|
foreach ($cfg_in as $file) {
|
390
|
$in = file($file, FILE_SKIP_EMPTY_LINES);
|
391
|
foreach ($in as $line) {
|
392
|
/* Skip comment lines */
|
393
|
if (preg_match('/^\s*#/', $line))
|
394
|
continue;
|
395
|
if (preg_match('/(\:)\s*(\w+)\s*(.*)/', $line, $matches)) {
|
396
|
if (!empty($matches[2]) && !empty($matches[3])) {
|
397
|
$matches[2] = trim($matches[2]);
|
398
|
if (!array_key_exists($matches[2], $outMap))
|
399
|
$outMap[$matches[2]] = trim($matches[3]);
|
400
|
}
|
401
|
}
|
402
|
}
|
403
|
}
|
404
|
/* Sort the new reference map. */
|
405
|
uksort($outMap,'strnatcasecmp');
|
406
|
|
407
|
/* Format and write it to the supplied output file. */
|
408
|
$format = "config reference: %-12s %s\n";
|
409
|
foreach ($outMap as $key=>$value)
|
410
|
$outMap[$key] = sprintf($format, $key, $value);
|
411
|
file_put_contents($cfg_out, array_values($outMap));
|
412
|
}
|
413
|
|
414
|
function snort_merge_classification_configs($cfg_in, $cfg_out) {
|
415
|
$outMap = array();
|
416
|
foreach ($cfg_in as $file) {
|
417
|
$in = file($file, FILE_SKIP_EMPTY_LINES);
|
418
|
foreach ($in as $line) {
|
419
|
if (preg_match('/(.*:)(\s*.*),(.*),(.*)/', $line, $matches)) {
|
420
|
/* Skip comment lines */
|
421
|
if (preg_match('/^\s*#/', $line))
|
422
|
continue;
|
423
|
if (!empty($matches[2]) && !empty($matches[3]) && !empty($matches[4])) {
|
424
|
$matches[2] = trim($matches[2]);
|
425
|
if (!array_key_exists($matches[2], $outMap))
|
426
|
$outMap[$matches[2]] = trim($matches[3]) . "," . trim($matches[4]);
|
427
|
}
|
428
|
}
|
429
|
}
|
430
|
}
|
431
|
/* Sort the new classification map. */
|
432
|
uksort($outMap,'strnatcasecmp');
|
433
|
|
434
|
/* Format and write it to the supplied output file. */
|
435
|
$format = "config classification: %s,%s\n";
|
436
|
foreach ($outMap as $key=>$value)
|
437
|
$outMap[$key] = sprintf($format, $key, $value);
|
438
|
file_put_contents($cfg_out, array_values($outMap));
|
439
|
}
|
440
|
|
441
|
function snort_apply_customizations($snortcfg, $if_real) {
|
442
|
global $snortdir;
|
443
|
|
444
|
if (empty($snortcfg['rulesets']))
|
445
|
return;
|
446
|
else {
|
447
|
$enabled_rulesets_array = explode("||", $snortcfg['rulesets']);
|
448
|
foreach($enabled_rulesets_array as $enabled_item) {
|
449
|
@copy("{$snortdir}/rules/{$enabled_item}", "{$snortdir}/snort_{$snortcfg['uuid']}_{$if_real}/rules/{$enabled_item}");
|
450
|
if (substr($enabled_item, 0, 5) == "snort" && substr($enabled_item, -9) == ".so.rules") {
|
451
|
$slib = substr($enabled_item, 6, -6);
|
452
|
if (file_exists("/usr/local/lib/snort/dynamicrules/{$slib}"))
|
453
|
@copy("/usr/local/lib/snort/dynamicrules/{$slib}", "{$snortdir}/snort_{$snortcfg['uuid']}_{$if_real}/dynamicrules/{$slib}");
|
454
|
}
|
455
|
}
|
456
|
|
457
|
@copy("{$snortdir}/classification.config", "{$snortdir}/snort_{$snortcfg['uuid']}_{$if_real}/classification.config");
|
458
|
@copy("{$snortdir}/gen-msg.map", "{$snortdir}/snort_{$snortcfg['uuid']}_{$if_real}/gen-msg.map");
|
459
|
if (is_dir("{$snortdir}/generators"))
|
460
|
exec("/bin/cp -r {$snortdir}/generators {$snortdir}/snort_{$snortcfg['uuid']}_{$if_real}");
|
461
|
@copy("{$snortdir}/reference.config", "{$snortdir}/snort_{$snortcfg['uuid']}_{$if_real}/reference.config");
|
462
|
@copy("{$snortdir}/sid", "{$snortdir}/snort_{$snortcfg['uuid']}_{$if_real}/sid");
|
463
|
@copy("{$snortdir}/sid-msg.map", "{$snortdir}/snort_{$snortcfg['uuid']}_{$if_real}/sid-msg.map");
|
464
|
@copy("{$snortdir}/unicode.map", "{$snortdir}/snort_{$snortcfg['uuid']}_{$if_real}/unicode.map");
|
465
|
}
|
466
|
|
467
|
if (!empty($snortcfg['rule_sid_on']) || !empty($snortcfg['rule_sid_off'])) {
|
468
|
if (!empty($snortcfg['rule_sid_on'])) {
|
469
|
$enabled_sid_on_array = explode("||", trim($snortcfg['rule_sid_on']));
|
470
|
$enabled_sids = array_flip($enabled_sid_on_array);
|
471
|
}
|
472
|
|
473
|
if (!empty($snortcfg['rule_sid_off'])) {
|
474
|
$enabled_sid_off_array = explode("||", trim($snortcfg['rule_sid_off']));
|
475
|
$disabled_sids = array_flip($enabled_sid_off_array);
|
476
|
}
|
477
|
|
478
|
$files = glob("{$snortdir}/snort_{$snortcfg}_{$if_real}/rules/*.rules");
|
479
|
foreach ($files as $file) {
|
480
|
$splitcontents = file($file);
|
481
|
$changed = false;
|
482
|
foreach ( $splitcontents as $counter => $value ) {
|
483
|
$sid = snort_get_rule_part($value, 'sid:', ';', 0);
|
484
|
if (!is_numeric($sid))
|
485
|
continue;
|
486
|
if (isset($enabled_sids["enablesid {$sid}"])) {
|
487
|
if (substr($value, 0, 5) == "alert")
|
488
|
/* Rule is already enabled */
|
489
|
continue;
|
490
|
if (substr($value, 0, 7) == "# alert") {
|
491
|
/* Rule is disabled, change */
|
492
|
$splitcontents[$counter] = substr($value, 2);
|
493
|
$changed = true;
|
494
|
} else if (substr($splitcontents[$counter - 1], 0, 5) == "alert") {
|
495
|
/* Rule is already enabled */
|
496
|
continue;
|
497
|
} else if (substr($splitcontents[$counter - 1], 0, 7) == "# alert") {
|
498
|
/* Rule is disabled, change */
|
499
|
$splitcontents[$counter - 1] = substr($value, 2);
|
500
|
$changed = true;
|
501
|
}
|
502
|
} else if (isset($disabled_sids["disablesid {$sid}"])) {
|
503
|
if (substr($value, 0, 7) == "# alert")
|
504
|
/* Rule is already disabled */
|
505
|
continue;
|
506
|
if (substr($value, 0, 5) == "alert") {
|
507
|
/* Rule is enabled, change */
|
508
|
$splitcontents[$counter] = "# {$value}";
|
509
|
$changed = true;
|
510
|
} else if (substr($splitcontents[$counter - 1], 0, 7) == "# alert") {
|
511
|
/* Rule is already disabled */
|
512
|
continue;
|
513
|
} else if (substr($splitcontents[$counter - 1], 0, 5) == "alert") {
|
514
|
/* Rule is enabled, change */
|
515
|
$splitcontents[$counter - 1] = "# {$value}";
|
516
|
$changed = true;
|
517
|
}
|
518
|
|
519
|
}
|
520
|
}
|
521
|
if ($changed == true)
|
522
|
@file_put_contents($file, implode("\n", $splitcontents));
|
523
|
}
|
524
|
}
|
525
|
}
|
526
|
|
527
|
if ($snortdownload == 'on' || $emergingthreats == 'on') {
|
528
|
/* Build a new sid-msg.map file from downloaded and enabled rules. */
|
529
|
update_status(gettext('Generating new sid-msg map file...'));
|
530
|
build_sid_msg_map("{$snortdir}/rules/", "{$snortdir}/sid-msg.map");
|
531
|
|
532
|
update_status(gettext('Copying new config and map files...'));
|
533
|
|
534
|
/* Determine which base etc file set to use for the master copy. */
|
535
|
/* If the Snort VRT rules are not enabled, then use Emerging Threats. */
|
536
|
if (($vrt_enabled == 'off') && (et_enabled == 'on')) {
|
537
|
foreach (array("classification.config", "reference.config", "gen-msg.map", "unicode.map") as $file) {
|
538
|
if (file_exists("{$snortdir}/ET_{$file}"))
|
539
|
@rename("{$snortdir}/ET_{$file}", "{$snortdir}/{$file}");
|
540
|
}
|
541
|
}
|
542
|
elseif (($vrt_enabled == 'on') && ($et_enabled == 'off')) {
|
543
|
foreach (array("classification.config", "reference.config", "gen-msg.map", "unicode.map") as $file) {
|
544
|
if (file_exists("{$snortdir}/VRT_{$file}"))
|
545
|
@rename("{$snortdir}/VRT_{$file}", "{$snortdir}/{$file}");
|
546
|
}
|
547
|
}
|
548
|
else {
|
549
|
/* Both VRT and ET rules are enabled, so build combined */
|
550
|
/* reference.config and classification.config files. */
|
551
|
$cfgs = glob("{$snortdir}/*reference.config");
|
552
|
snort_merge_reference_configs($cfgs, "{$snortdir}/reference.config");
|
553
|
$cfgs = glob("{$snortdir}/*classification.config");
|
554
|
snort_merge_classification_configs($cfgs, "{$snortdir}/classification.config");
|
555
|
}
|
556
|
|
557
|
/* Clean-up our temp versions of the config and map files. */
|
558
|
update_status(gettext('Cleaning up temp files...'));
|
559
|
$cfgs = glob("{$snortdir}/??*_*.config");
|
560
|
foreach ($cfgs as $file) {
|
561
|
if (file_exists($file)) {
|
562
|
$cmd = "/bin/rm -r " . $file;
|
563
|
exec($cmd);
|
564
|
}
|
565
|
}
|
566
|
$cfgs = glob("{$snortdir}/??*_*.map");
|
567
|
foreach ($cfgs as $file) {
|
568
|
if (file_exists($file)) {
|
569
|
$cmd = "/bin/rm -r " . $file;
|
570
|
exec($cmd);
|
571
|
}
|
572
|
}
|
573
|
|
574
|
/* Start the proccess for each configured interface */
|
575
|
if (is_array($config['installedpackages']['snortglobal']['rule'])) {
|
576
|
foreach ($config['installedpackages']['snortglobal']['rule'] as $id => $value) {
|
577
|
|
578
|
/* Create configuration for each active Snort interface */
|
579
|
$if_real = snort_get_real_interface($value['interface']);
|
580
|
$tmp = "Updating rules configuration for ";
|
581
|
$tmp += snort_get_friendly_interface($if_real);
|
582
|
$tmp += "...";
|
583
|
update_status(gettext($tmp));
|
584
|
log_error($tmp);
|
585
|
snort_apply_customizations($value, $if_real);
|
586
|
}
|
587
|
}
|
588
|
exec("/bin/sh /usr/local/etc/rc.d/snort.sh restart");
|
589
|
sleep(10);
|
590
|
if (!is_process_running("snort"))
|
591
|
exec("/bin/sh /usr/local/etc/rc.d/snort.sh start");
|
592
|
update_output_window(gettext("Snort has restarted with your new set of rules..."));
|
593
|
log_error("Snort has restarted with your new set of rules...");
|
594
|
}
|
595
|
|
596
|
update_status(gettext("The Rules update finished..."));
|
597
|
log_error("The Rules update finished...");
|
598
|
conf_mount_ro();
|
599
|
|
600
|
?>
|