]> git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/system/suricata
suricata: Add code to create iptables rules to the initscript
[ipfire-2.x.git] / src / initscripts / system / suricata
1 #!/bin/sh
2 ########################################################################
3 # Begin $rc_base/init.d/suricata
4 #
5 # Description : Suricata Initscript
6 #
7 # Author : Stefan Schantl <stefan.schantl@ipfire.org>
8 #
9 # Version : 01.00
10 #
11 # Notes :
12 #
13 ########################################################################
14
15 . /etc/sysconfig/rc
16 . ${rc_functions}
17
18 PATH=/usr/local/sbin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin; export PATH
19
20 eval $(/usr/local/bin/readhash /var/ipfire/suricata/settings)
21
22 # Name of the firewall chain.
23 FW_CHAIN="IPS"
24
25 # Optional options for the Netfilter queue.
26 NFQ_OPTS="--queue-bypass "
27
28 # Array containing the 4 possible network zones.
29 network_zones=( red green blue orange )
30
31 # Mark and Mask options.
32 MARK="0x1"
33 MASK="0x1"
34
35 case "$1" in
36 start)
37 # Get amount of CPU cores.
38 NFQUEUES=
39 CPUCOUNT=0
40 while read line; do
41 [ "$line" ] && [ -z "${line%processor*}" ] && NFQUEUES+="-q $CPUCOUNT " && ((CPUCOUNT++))
42 done </proc/cpuinfo
43
44 # Check if the IDS should be started.
45 if [ "$ENABLE_IDS" == "on" ]; then
46 # Loop through the array of network zones.
47 for zone in "${network_zones[@]}"; do
48 # Convert zone into upper case.
49 zone_upper=${zone^^}
50
51 # Check if the IDS is enabled for this network zone.
52 if [ "$ENABLE_IDS_$$zone_upper" == "on" ]; then
53 # Generate name of the network interface.
54 network_device=$zone
55 network_device+="0"
56
57 # Assign NFQ_OPTS
58 NFQ_OPTIONS=$NFQ_OPTS
59
60 # Check if there are multiple cpu cores available.
61 if [ "$CPUCOUNT" > 0 ]; then
62 # Balance beetween all queues.
63 NFQ_OPTIONS+="--queue-balance 0:"
64 NFQ_OPTIONS+=$(($CPUCOUNT-1))
65 else
66 # Send all packets to queue 0.
67 NFQ_OPTIONS+="--queue-num 0"
68 fi
69
70 # Create firewall rules to queue the traffic and pass to
71 # the IDS.
72 iptables -I "$FW_CHAIN" -i "$network_device" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE "$NFQ_OPTIONS"
73 iptables -I "$FW_CHAIN" -o "$network_device" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE "$NFQ_OPTIONS"
74 fi
75 done
76
77 # Start the IDS.
78 boot_mesg "Starting Intrusion Detection System..."
79 /usr/bin/suricata -c /etc/suricata/suricata.yaml -D $NFQUEUES
80 evaluate_retval
81 fi
82 ;;
83
84 stop)
85 boot_mesg "Stopping Intrusion Detection System..."
86 killproc -p /var/run/suricata.pid /var/run
87
88 # Flush firewall chain.
89 iptables -F $FW_CHAIN
90
91 # Remove suricata control socket.
92 rm /var/run/suricata/* >/dev/null 2>/dev/null
93
94 # Don't report returncode of rm if suricata was not started
95 exit 0
96 ;;
97
98 status)
99 statusproc /usr/bin/suricata
100 ;;
101
102 restart)
103 $0 stop
104 $0 start
105 ;;
106 reload)
107 # Send SIGUSR2 to the suricata process to perform a reload
108 # of the ruleset.
109 kill -USR2 $(pidof suricata)
110 ;;
111
112 *)
113 echo "Usage: $0 {start|stop|restart|reload|status}"
114 exit 1
115 ;;
116 esac
117
118 chmod 644 /var/log/suricata/* 2>/dev/null
119
120 # End $rc_base/init.d/suricata