From 9c263445b3f6ab09bfca57b9ce368924ca79c681 Mon Sep 17 00:00:00 2001 From: robiscool Date: Sat, 21 Nov 2009 00:51:11 -0800 Subject: snort-dev,add new start up snort.sh, add new dynamic_ip_reload.php --- config/snort-dev/NOTES.txt | 2 - config/snort-dev/snort.inc | 315 ++++++++++++++++----------- config/snort-dev/snort_dynamic_ip_reload.php | 59 +++++ config/snort-dev/snort_interfaces_edit.php | 35 ++- 4 files changed, 273 insertions(+), 138 deletions(-) create mode 100644 config/snort-dev/snort_dynamic_ip_reload.php (limited to 'config') diff --git a/config/snort-dev/NOTES.txt b/config/snort-dev/NOTES.txt index 584c84a0..9b4d8d0e 100644 --- a/config/snort-dev/NOTES.txt +++ b/config/snort-dev/NOTES.txt @@ -26,8 +26,6 @@ snort.inc Must be recoded so that it reads the [snortglobal] [snortglobal][rule] options in conf.xml and makes the files whitelist, snort.sh, snort.conf, and barnyard.conf. This is easy, just cut and paste from the old snort.inc. I will work on this. -Should be working for only one interface. Add code to wirite files for every snort rule in conf.xml - ================================= Any Devs that would like to help please work on snort_rules_edit.php and snort_rules.php. They work but need cleaning up. diff --git a/config/snort-dev/snort.inc b/config/snort-dev/snort.inc index 8bd4e880..6422df2c 100644 --- a/config/snort-dev/snort.inc +++ b/config/snort-dev/snort.inc @@ -40,7 +40,8 @@ $id = $_GET['id']; if (isset($_POST['id'])) $id = $_POST['id']; - +$interface_fake = $config['installedpackages']['snortglobal']['rule'][$id][interface]; +$if_real = convert_friendly_interface_to_real_interface_name($interface_fake); /* Allow additional execution time 0 = no limit. */ ini_set('max_execution_time', '9999'); @@ -66,8 +67,9 @@ function sync_package_snort_reinstall() /* make sure this func on writes to files and does not start snort */ function sync_package_snort() { - global $config, $g; + global $config, $g, $id, $if_real; + if(!file_exists("/var/log/snort/")) mwexec("mkdir -p /var/log/snort/"); if(!file_exists("/var/log/snort/alert")) @@ -79,8 +81,8 @@ function sync_package_snort() $bpfmaxinsns = $config['installedpackages']['snortglobal']['bpfmaxinsns']; /* set the snort performance model */ - if($config['installedpackages']['snortglobal']['rule'][0]['performance']) - $config['installedpackages']['snortglobal']['rule'][0]['performance']; + if($config['installedpackages']['snortglobal']['rule'][$id]['performance']) + $config['installedpackages']['snortglobal']['rule'][$id]['performance']; else $snort_performance = "lowmem"; @@ -89,6 +91,8 @@ function sync_package_snort() exec("/bin/mkdir -p /usr/local/etc/snort"); exec("/bin/mkdir -p /var/log/snort"); exec("/bin/mkdir -p /usr/local/etc/snort/rules"); + + if(file_exists("/usr/local/etc/snort/snort.conf-sample")) { exec("/bin/rm /usr/local/etc/snort/snort.conf-sample"); exec("/bin/rm /usr/local/etc/snort/threshold.conf-sample"); exec("/bin/rm /usr/local/etc/snort/sid-msg.map-sample"); @@ -99,25 +103,41 @@ function sync_package_snort() exec("/bin/rm /usr/local/etc/snort/gen-msg.map-sample"); exec("/bin/rm /usr/local/etc/snort/sid"); exec("/bin/rm -f /usr/local/etc/rc.d/snort"); + } - /* create log directory */ - $start = "/bin/mkdir -p /var/log/snort\n"; + /* create basic files */ + if ($id != "") { + if(!file_exists("/usr/local/etc/snort/snort/snort_$id$if_real")) { + exec("/bin/mkdir -p /usr/local/etc/snort/snort_$id$if_real/"); + exec("/bin/mkdir -p /usr/local/etc/snort/snort_$id$if_real/rules"); + } + if(!file_exists("/usr/local/etc/snort/snort_$id$if_real/gen-msg.map")) { + exec("/bin/cp /usr/local/etc/snort/classification.config /usr/local/etc/snort/snort_$id$if_real/classification.config"); + exec("/bin/cp /usr/local/etc/snort/gen-msg.map /usr/local/etc/snort/snort_$id$if_real/gen-msg.map"); + exec("/bin/cp /usr/local/etc/snort/reference.config /usr/local/etc/snort/snort_$id$if_real/reference.config"); + exec("/bin/cp /usr/local/etc/snort/sid-msg.map /usr/local/etc/snort/snort_$id$if_real/sid-msg.map"); + exec("/bin/cp /usr/local/etc/snort/unicode.map /usr/local/etc/snort/snort_$id$if_real/unicode.map"); + exec("/bin/cp /usr/local/etc/snort/threshold.conf /usr/local/etc/snort/snort_$id$if_real/threshold.conf"); + exec("/bin/cp /usr/local/etc/snort/snort.conf /usr/local/etc/snort/snort_$id$if_real/snort.conf"); + exec("/bin/mkdir -p /usr/local/etc/snort/snort_$id$if_real/rules"); + } + } /* snort advanced features - bpf tuning */ - if($bpfbufsize) - $start .= "sysctl net.bpf.bufsize={$bpfbufsize}\n"; - if($bpfmaxbufsize) - $start .= "sysctl net.bpf.maxbufsize={$bpfmaxbufsize}\n"; - if($bpfmaxinsns) - $start .= "sysctl net.bpf.maxinsns={$bpfmaxinsns}\n"; +// if($bpfbufsize) +// $start .= "sysctl net.bpf.bufsize={$bpfbufsize}\n"; +// if($bpfmaxbufsize) +// $start .= "sysctl net.bpf.maxbufsize={$bpfmaxbufsize}\n"; +// if($bpfmaxinsns) +// $start .= "sysctl net.bpf.maxinsns={$bpfmaxinsns}\n"; /* go ahead and issue bpf changes */ - if($bpfbufsize) - mwexec_bg("sysctl net.bpf.bufsize={$bpfbufsize}"); - if($bpfmaxbufsize) - mwexec_bg("sysctl net.bpf.maxbufsize={$bpfmaxbufsize}"); - if($bpfmaxinsns) - mwexec_bg("sysctl net.bpf.maxinsns={$bpfmaxinsns}"); +// if($bpfbufsize) +// mwexec_bg("sysctl net.bpf.bufsize={$bpfbufsize}"); +// if($bpfmaxbufsize) +// mwexec_bg("sysctl net.bpf.maxbufsize={$bpfmaxbufsize}"); +// if($bpfmaxinsns) +// mwexec_bg("sysctl net.bpf.maxinsns={$bpfmaxinsns}"); /* let there be snort.sh for each rule */ /* start snort.sh for writing */ @@ -131,6 +151,15 @@ $counter_rule += 1; $result_lan = $config['installedpackages']['snortglobal']['rule'][$counter_rule][interface]; $if_real_c = convert_friendly_interface_to_real_interface_name($result_lan); +$snortbarnyardlog_info_chk = $config['installedpackages']['snortglobal']['rule'][$counter_rule]['barnyard_enable']; +/* define snortbarnyardlog_chk */ +if ($snortbarnyardlog_info_chk == on) { + +$start_barnyard2 = "\nsleep 4\n/usr/local/bin/barnyard2 -c /usr/local/etc/snort/snort_$counter_rule$if_real_c/barnyard2.conf -d /var/log/snort -f snort.u2_$counter_rule$if_real_c -w /usr/local/etc/snort/snort_$counter_rule$if_real_c/barnyard2.waldo -D -q\n\n"; + +} + + /* open snort.sh for writing" */ conf_mount_rw(); @@ -163,68 +192,114 @@ $snort_sh_text = << /dev/null -then - echo "snort_$counter_rule$if_real_c.sh is running" - exit 0 -else - echo "snort_$counter_rule$if_real_c.sh is not running" -fi + # If no rules dir exit + + if [ "ls -A /usr/local/etc/snort/snort_$counter_rule$if_real_c/rules" ] ; then + echo "rules DO exist" + else + exit 2 + fi + + # If Snort.sh is running exit + + if ls /tmp/snort_$counter_rule$if_real_c.sh.pid > /dev/null ; then + echo "snort.sh is running" + exit 3 + else + echo "snort.sh is not running" + fi -echo "snort_$counter_rule$if_real_c.sh run" > /tmp/snort_$counter_rule$if_real_c.sh.pid + # If Snort proc is running exit -echo "snort_$counter_rule$if_real_c.sh run" >> /tmp/snort_$counter_rule$if_real_c.sh_startup.log + if [ "`ps -auwx | grep -v grep | grep "$counter_rule$if_real_c -c" | awk '{print $2;}'`" != "" ] ; then + echo "Snort is running" + exit 4 + fi -rm -f /var/run/snort_$counter_rule$if_real_c.sh -BEFORE_MEM=`top | grep Wired | awk '{print $12}'` -/bin/mkdir -p /var/log/snort -/usr/bin/killall barnyard2 + cp /var/log/system.log /var/log/system.log.bk + logger -p daemon.info -i -t SnortStartup "Snort is NOT running, hard restart" -sleep 4 -/usr/local/bin/snort -G $counter_rule$if_real_c -R $counter_rule$if_real_c -c /usr/local/etc/snort/snort_$counter_rule$if_real_c/snort.conf -l /var/log/snort -D -i $if_real_c -q + if [ "`ps -auwx | grep -v grep | grep "$counter_rule$if_real_c -c" | awk '{print $2;}'`" = "" ] ; then + /bin/rm /tmp/snort_$counter_rule$if_real_c.sh.pid + fi -# sleep 4 -# /usr/local/bin/barnyard2 -c /usr/local/etc/snort/snort_$counter_rule$if_real_c/barnyard2.conf -d /var/log/snort -f snort.u2 -w /usr/local/etc/snort/snort_$counter_rule$if_real_c/barnyard2.waldo -D -q + echo "snort_$counter_rule$if_real_c.sh run" > /tmp/snort_$counter_rule$if_real_c.sh.pid - sleep 2 - MYSNORTLOG=`/usr/sbin/clog /var/log/system.log | grep snort | tail | grep 'Snort initialization completed successfully'` + echo "snort_$counter_rule$if_real_c.sh run" >> /tmp/snort_$counter_rule$if_real_c.sh_startup.log + + # Start the interfaces + + /usr/local/bin/snort -G $counter_rule$if_real_c -R $counter_rule$if_real_c -c /usr/local/etc/snort/snort_$counter_rule$if_real_c/snort.conf -l /var/log/snort -D -i $if_real_c -q + + sleep 3 + AFTER_MEM=`top | grep Wired | awk '{print $12}'` + cp /var/log/system.log /var/log/snort/snort_sys_$counter_rule$if_real_c.log + /usr/sbin/clog -i -s 262144 /var/log/system.log + cp /var/log/system.log.bk /var/log/system.log + logger -p daemon.info -i -t SnortStartup "Snort HARD Reload For $counter_rule$if_real_c..." + logger -p daemon.info -i -t SnortStartup "MEM after $counter_rule$if_real_c START \${AFTER_MEM}" + echo "snort is running, but snort.sh finished removed pid" + /bin/rm /tmp/snort_$counter_rule$if_real_c.sh.pid } rc_stop() { - /usr/bin/killall snort; killall barnyard2 + + pid_s=`ps -auwx | grep -v grep | grep "$counter_rule$if_real_c -c" | awk '{print \$2;}'` + pid_b=`ps -auwx | grep -v grep | grep "snort.u2_$counter_rule$if_real_c" | awk '{print \$2;}'` + + if [ \${pid_s} ] ; then + cp /var/log/system.log /var/log/system.log.bk + logger -p daemon.info -i -t SnortStartup "Snort IS running, hard STOP" + /bin/kill \${pid_s}; /bin/kill \${pid_b}; + sleep 3 + AFTER_MEM=`top | grep Wired | awk '{print $12}'` + cp /var/log/system.log /var/log/snort/snort_sys_$counter_rule$if_real_c.log + /usr/sbin/clog -i -s 262144 /var/log/system.log + cp /var/log/system.log.bk /var/log/system.log + logger -p daemon.info -i -t SnortStartup "Snort HARD STOP For $counter_rule$if_real_c..." + logger -p daemon.info -i -t SnortStartup "MEM after $counter_rule$if_real_c STOP \${AFTER_MEM}" + fi } case $1 in start) rc_start ;; + start_real) + rc_start_real + ;; stop) rc_stop ;; restart) rc_stop - rc_start + rc_start_real ;; esac @@ -246,17 +321,12 @@ EOD; create_snort_conf(); /* create barnyard2 configuration file */ -$snortbarnyardlog_info_chk = $config['installedpackages']['snortglobal']['rule'][0]['barnyard_enable']; +$snortbarnyardlog_info_chk = $config['installedpackages']['snortglobal']['rule'][$id]['barnyard_enable']; if ($snortbarnyardlog_info_chk == on) create_barnyard2_conf(); - /* snort will not start on install untill setting are set */ - /* do start snort create a funtion to start snort */ -if ($config['installedpackages']['snortglobal']['autorulesupdate7'] != "") { - /* start snort service */ - conf_mount_ro(); - start_service("snort"); - } +conf_mount_ro(); + } @@ -265,9 +335,9 @@ function create_barnyard2_conf() { global $bconfig, $bg; /* write out barnyard2_conf */ $barnyard2_conf_text = generate_barnyard2_conf(); - $bconf = fopen("/usr/local/etc/snort/snort_0vr1/barnyard2.conf", "w"); + $bconf = fopen("/usr/local/etc/snort/$id$if_real/barnyard2.conf", "w"); if(!$bconf) { - log_error("Could not open /usr/local/etc/snort/snort_0vr1/barnyard2.conf for writing."); + log_error("Could not open /usr/local/etc/snort/$id$if_real/barnyard2.conf for writing."); exit; } fwrite($bconf, $barnyard2_conf_text); @@ -277,18 +347,15 @@ function create_barnyard2_conf() { /* open barnyard2.conf for writing" */ function generate_barnyard2_conf() { - global $config, $g, $id; + global $config, $g, $id, $if_real; conf_mount_rw(); /* define snortbarnyardlog */ /* TODO add support for the other 5 output plugins */ -$snortbarnyardlog_database_info_chk = $config['installedpackages']['snortglobal']['rule'][0]['barnyard_mysql']; +$snortbarnyardlog_database_info_chk = $config['installedpackages']['snortglobal']['rule'][$id]['barnyard_mysql']; $snortbarnyardlog_hostname_info_chk = exec("/bin/hostname"); -/* convert fake interfaces to real */ -$if_real = convert_friendly_interface_to_real_interface_name($config['installedpackages']['snortglobal']['rule'][0]['interface']); - $snortbarnyardlog_interface_info_chk = $if_real; $barnyard2_conf_text = <<\\2", $buffer); diff --git a/config/snort-dev/snort_dynamic_ip_reload.php b/config/snort-dev/snort_dynamic_ip_reload.php new file mode 100644 index 00000000..7c42c85f --- /dev/null +++ b/config/snort-dev/snort_dynamic_ip_reload.php @@ -0,0 +1,59 @@ + \ No newline at end of file diff --git a/config/snort-dev/snort_interfaces_edit.php b/config/snort-dev/snort_interfaces_edit.php index f6fc2143..8d9def44 100644 --- a/config/snort-dev/snort_interfaces_edit.php +++ b/config/snort-dev/snort_interfaces_edit.php @@ -75,7 +75,7 @@ if (isset($_GET['dup'])) /* convert fake interfaces to real */ $if_real = convert_friendly_interface_to_real_interface_name($pconfig['interface']); -if ($_POST) { +if ($_POST["Submit"]) { /* input validation */ // if(strtoupper($_POST['proto']) == "TCP" or strtoupper($_POST['proto']) == "UDP" or strtoupper($_POST['proto']) == "TCP/UDP") { @@ -160,21 +160,35 @@ if ($_POST) { $a_nat[] = $natent; } - touch($d_natconfdirty_path); - write_config(); // stop_service("snort"); //create_snort_conf(); //create_barnyard2_conf(); - sync_package_snort(); - // sleep(2); - // start_service("snort"); - header("Location: snort_interfaces.php"); + if ($pconfig['performance'] != "") { + sync_package_snort(); + } + + if ($pconfig['performance'] != "") { + header("Location: /snort/snort_interfaces_edit.php?id=$id"); + }else{ + touch($d_natconfdirty_path); + header("Location: /snort_interfaces.php"); + } exit; } } + if ($_POST["Submit2"]) { + if ($pconfig['performance'] != "") { + sync_package_snort(); + sleep(1); + exec("/bin/sh /usr/local/etc/rc.d/snort_{$id}{$if_real}.sh restart"); + header("Location: /snort/snort_interfaces_edit.php?id=$id"); + exit; + } + } + $pgtitle = "Snort: Interface: $id$if_real Settings Edit"; include("head.inc"); @@ -269,11 +283,8 @@ if($id != "") { /* if base directories dont exist create them */ - if(!file_exists("/usr/local/pkg/snort/snort_{$snortIf}_{$id}/")) - { - exec("/bin/mkdir -p /usr/local/pkg/snort/snort_{$snortIf}_{$id}/"); - if(!file_exists("/usr/local/www/snort/snort_{$snortIf}_{$id}/")) - exec("/bin/mkdir -p /usr/local/www/snort/snort_{$snortIf}_{$id}/"); + if(!file_exists("/usr/local/etc/snort/snort_{$id}{$if_real}/")) { + exec("/bin/mkdir -p /usr/local/etc/snort/snort_{$id}{$if_real}/"); } $tab_array = array(); -- cgit v1.2.3