]>
Commit | Line | Data |
---|---|---|
d72b3e64 SS |
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 | ||
d72b3e64 SS |
20 | eval $(/usr/local/bin/readhash /var/ipfire/suricata/settings) |
21 | ||
3c2c5483 SS |
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. | |
5d04cfe7 MT |
32 | MARK="0x70000000" |
33 | MASK="0x70000000" | |
3c2c5483 | 34 | |
00a03114 SS |
35 | # PID file of suricata. |
36 | PID_FILE="/var/run/suricata.pid" | |
37 | ||
c9b07d6a SS |
38 | # Function to get the amount of CPU cores of the system. |
39 | function get_cpu_count { | |
40 | CPUCOUNT=0 | |
41 | ||
42 | # Loop through "/proc/cpuinfo" and count the amount of CPU cores. | |
43 | while read line; do | |
44 | [ "$line" ] && [ -z "${line%processor*}" ] && ((CPUCOUNT++)) | |
45 | done </proc/cpuinfo | |
46 | ||
47 | echo $CPUCOUNT | |
48 | } | |
49 | ||
50 | # Function to create the firewall rules to pass the traffic to suricata. | |
51 | function generate_fw_rules { | |
52 | cpu_count=$(get_cpu_count) | |
53 | ||
54 | # Flush the firewall chain. | |
55 | iptables -F "$FW_CHAIN" | |
56 | ||
57 | # Loop through the array of network zones. | |
58 | for zone in "${network_zones[@]}"; do | |
59 | # Convert zone into upper case. | |
60 | zone_upper=${zone^^} | |
61 | ||
62 | # Generate variable name for checking if the IDS is | |
63 | # enabled on the zone. | |
64 | enable_ids_zone="ENABLE_IDS_$zone_upper" | |
65 | ||
66 | # Check if the IDS is enabled for this network zone. | |
67 | if [ "${!enable_ids_zone}" == "on" ]; then | |
68 | # Generate name of the network interface. | |
69 | network_device=$zone | |
70 | network_device+="0" | |
71 | ||
72 | # Assign NFQ_OPTS | |
73 | NFQ_OPTIONS=$NFQ_OPTS | |
74 | ||
75 | # Check if there are multiple cpu cores available. | |
76 | if [ "$cpu_count" -gt "1" ]; then | |
77 | # Balance beetween all queues. | |
78 | NFQ_OPTIONS+="--queue-balance 0:" | |
79 | NFQ_OPTIONS+=$(($cpu_count-1)) | |
80 | else | |
81 | # Send all packets to queue 0. | |
82 | NFQ_OPTIONS+="--queue-num 0" | |
83 | fi | |
84 | ||
85 | # Create firewall rules to queue the traffic and pass to | |
86 | # the IDS. | |
87 | iptables -I "$FW_CHAIN" -i "$network_device" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS | |
88 | iptables -I "$FW_CHAIN" -o "$network_device" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS | |
89 | fi | |
90 | done | |
5d04cfe7 MT |
91 | |
92 | # Clear repeat bit, so that it does not confuse IPsec or QoS | |
93 | iptables -A "${FW_CHAIN}" -j MARK --set-xmark "0x0/${MASK}" | |
c9b07d6a SS |
94 | } |
95 | ||
96 | # Function to flush the firewall chain. | |
97 | function flush_fw_chain { | |
98 | # Call iptables and flush the chain | |
99 | iptables -F "$FW_CHAIN" | |
100 | } | |
101 | ||
d72b3e64 SS |
102 | case "$1" in |
103 | start) | |
104 | # Get amount of CPU cores. | |
c9b07d6a SS |
105 | cpu_count=$(get_cpu_count) |
106 | ||
107 | # Numer of NFQUES. | |
d72b3e64 | 108 | NFQUEUES= |
c9b07d6a SS |
109 | |
110 | for i in $(seq 0 $cpu_count); do | |
111 | NFQUEUES+="-q $i " | |
112 | done | |
d72b3e64 | 113 | |
3c2c5483 SS |
114 | # Check if the IDS should be started. |
115 | if [ "$ENABLE_IDS" == "on" ]; then | |
3c2c5483 SS |
116 | # Start the IDS. |
117 | boot_mesg "Starting Intrusion Detection System..." | |
af006569 | 118 | /usr/bin/suricata -c /etc/suricata/suricata.yaml -D $NFQUEUES >/dev/null 2>/dev/null |
3c2c5483 | 119 | evaluate_retval |
00a03114 SS |
120 | |
121 | # Allow reading the pidfile. | |
122 | chmod 644 $PID_FILE | |
c9b07d6a SS |
123 | |
124 | # Flush the firewall chain | |
125 | flush_fw_chain | |
126 | ||
127 | # Generate firewall rules | |
128 | generate_fw_rules | |
3c2c5483 | 129 | fi |
d72b3e64 SS |
130 | ;; |
131 | ||
132 | stop) | |
133 | boot_mesg "Stopping Intrusion Detection System..." | |
00a03114 | 134 | killproc -p $PID_FILE /var/run |
d72b3e64 | 135 | |
3c2c5483 | 136 | # Flush firewall chain. |
c9b07d6a | 137 | flush_fw_chain |
3c2c5483 | 138 | |
d72b3e64 SS |
139 | # Remove suricata control socket. |
140 | rm /var/run/suricata/* >/dev/null 2>/dev/null | |
141 | ||
142 | # Don't report returncode of rm if suricata was not started | |
143 | exit 0 | |
144 | ;; | |
145 | ||
146 | status) | |
147 | statusproc /usr/bin/suricata | |
148 | ;; | |
149 | ||
150 | restart) | |
151 | $0 stop | |
152 | $0 start | |
153 | ;; | |
6187da50 SS |
154 | reload) |
155 | # Send SIGUSR2 to the suricata process to perform a reload | |
156 | # of the ruleset. | |
157 | kill -USR2 $(pidof suricata) | |
c9b07d6a SS |
158 | |
159 | # Flush the firewall chain. | |
160 | flush_fw_chain | |
161 | ||
162 | # Generate firewall rules. | |
163 | generate_fw_rules | |
6187da50 | 164 | ;; |
d72b3e64 SS |
165 | |
166 | *) | |
6187da50 | 167 | echo "Usage: $0 {start|stop|restart|reload|status}" |
d72b3e64 SS |
168 | exit 1 |
169 | ;; | |
170 | esac | |
171 | ||
172 | chmod 644 /var/log/suricata/* 2>/dev/null | |
173 | ||
174 | # End $rc_base/init.d/suricata |