]>
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 | # | |
1a65ea1b | 9 | # Version : 01.03 |
d72b3e64 SS |
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 | 20 | eval $(/usr/local/bin/readhash /var/ipfire/suricata/settings) |
e8a28edb | 21 | eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings) |
d72b3e64 | 22 | |
9cf253e1 SS |
23 | # Name of the firewall chains. |
24 | IPS_INPUT_CHAIN="IPS_INPUT" | |
25 | IPS_FORWARD_CHAIN="IPS_FORWARD" | |
26 | IPS_OUTPUT_CHAIN="IPS_OUTPUT" | |
3c2c5483 SS |
27 | |
28 | # Optional options for the Netfilter queue. | |
29 | NFQ_OPTS="--queue-bypass " | |
30 | ||
31 | # Array containing the 4 possible network zones. | |
51b63b41 | 32 | network_zones=( red green blue orange ovpn ) |
3c2c5483 | 33 | |
9cf253e1 SS |
34 | # Array to store the network zones weather the IPS is enabled for. |
35 | enabled_ips_zones=() | |
36 | ||
3c2c5483 | 37 | # Mark and Mask options. |
5d04cfe7 MT |
38 | MARK="0x70000000" |
39 | MASK="0x70000000" | |
3c2c5483 | 40 | |
00a03114 SS |
41 | # PID file of suricata. |
42 | PID_FILE="/var/run/suricata.pid" | |
43 | ||
c9b07d6a SS |
44 | # Function to get the amount of CPU cores of the system. |
45 | function get_cpu_count { | |
46 | CPUCOUNT=0 | |
47 | ||
48 | # Loop through "/proc/cpuinfo" and count the amount of CPU cores. | |
49 | while read line; do | |
50 | [ "$line" ] && [ -z "${line%processor*}" ] && ((CPUCOUNT++)) | |
51 | done </proc/cpuinfo | |
52 | ||
a8387f8d SS |
53 | # Limit to a maximum of 16 cores, because suricata does not support more than |
54 | # 16 netfilter queues at the moment. | |
55 | if [ $CPUCOUNT -gt "16" ]; then | |
56 | echo "16" | |
57 | else | |
58 | echo $CPUCOUNT | |
59 | fi | |
c9b07d6a SS |
60 | } |
61 | ||
9cf253e1 | 62 | # Function to flush the firewall chains. |
5e3067cb | 63 | function flush_fw_chain { |
9cf253e1 SS |
64 | # Call iptables and flush the chains |
65 | iptables -F "$IPS_INPUT_CHAIN" | |
66 | iptables -F "$IPS_FORWARD_CHAIN" | |
67 | iptables -F "$IPS_OUTPUT_CHAIN" | |
5e3067cb SS |
68 | } |
69 | ||
c9b07d6a SS |
70 | # Function to create the firewall rules to pass the traffic to suricata. |
71 | function generate_fw_rules { | |
72 | cpu_count=$(get_cpu_count) | |
73 | ||
c9b07d6a SS |
74 | # Loop through the array of network zones. |
75 | for zone in "${network_zones[@]}"; do | |
76 | # Convert zone into upper case. | |
77 | zone_upper=${zone^^} | |
78 | ||
79 | # Generate variable name for checking if the IDS is | |
80 | # enabled on the zone. | |
81 | enable_ids_zone="ENABLE_IDS_$zone_upper" | |
82 | ||
83 | # Check if the IDS is enabled for this network zone. | |
84 | if [ "${!enable_ids_zone}" == "on" ]; then | |
e8a28edb SS |
85 | # Check if the current processed zone is "red" and the configured type is PPPoE dialin. |
86 | if [ "$zone" == "red" ] && [ "$RED_TYPE" == "PPPOE" ]; then | |
87 | # Set device name to ppp0. | |
88 | network_device="ppp0" | |
51b63b41 SS |
89 | elif [ "$zone" == "ovpn" ]; then |
90 | # Get all virtual net devices because the RW server and each | |
91 | # N2N connection creates it's own tun device. | |
92 | for virt_dev in /sys/devices/virtual/net/*; do | |
93 | # Cut-off the directory. | |
94 | dev="${virt_dev##*/}" | |
95 | ||
96 | # Only process tun devices. | |
97 | if [[ $dev =~ "tun" ]]; then | |
98 | # Add the network device to the array of enabled zones. | |
99 | enabled_ips_zones+=( "$dev" ) | |
100 | fi | |
101 | done | |
102 | ||
103 | # Process next zone. | |
104 | continue | |
e8a28edb SS |
105 | else |
106 | # Generate variable name which contains the device name. | |
107 | zone_name="$zone_upper" | |
108 | zone_name+="_DEV" | |
109 | ||
110 | # Grab device name. | |
111 | network_device=${!zone_name} | |
112 | fi | |
c9b07d6a | 113 | |
9cf253e1 SS |
114 | # Add the network device to the array of enabled zones. |
115 | enabled_ips_zones+=( "$network_device" ) | |
c9b07d6a SS |
116 | fi |
117 | done | |
5d04cfe7 | 118 | |
9cf253e1 SS |
119 | # Assign NFQ_OPTS |
120 | NFQ_OPTIONS=$NFQ_OPTS | |
121 | ||
122 | # Check if there are multiple cpu cores available. | |
123 | if [ "$cpu_count" -gt "1" ]; then | |
124 | # Balance beetween all queues. | |
125 | NFQ_OPTIONS+="--queue-balance 0:$(($cpu_count-1))" | |
126 | NFQ_OPTIONS+=" --queue-cpu-fanout" | |
127 | else | |
128 | # Send all packets to queue 0. | |
129 | NFQ_OPTIONS+="--queue-num 0" | |
130 | fi | |
131 | ||
132 | # Flush the firewall chains. | |
133 | flush_fw_chain | |
134 | ||
135 | # Check if the array of enabled_ips_zones contains any elements. | |
136 | if [[ ${enabled_ips_zones[@]} ]]; then | |
137 | # Loop through the array and create firewall rules. | |
138 | for enabled_ips_zone in "${enabled_ips_zones[@]}"; do | |
139 | # Create rules queue input and output related traffic and pass it to the IPS. | |
140 | iptables -I "$IPS_INPUT_CHAIN" -i "$enabled_ips_zone" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS | |
141 | iptables -I "$IPS_OUTPUT_CHAIN" -o "$enabled_ips_zone" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS | |
142 | ||
143 | # Create rules which are required to handle forwarded traffic. | |
144 | for enabled_ips_zone_forward in "${enabled_ips_zones[@]}"; do | |
145 | iptables -I "$IPS_FORWARD_CHAIN" -i "$enabled_ips_zone" -o "$enabled_ips_zone_forward" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS | |
146 | done | |
147 | done | |
148 | ||
149 | # Clear repeat bit, so that it does not confuse IPsec or QoS | |
150 | iptables -A "${IPS_INPUT_CHAIN}" -j MARK --set-xmark "0x0/${MASK}" | |
151 | iptables -A "${IPS_FORWARD_CHAIN}" -j MARK --set-xmark "0x0/${MASK}" | |
152 | iptables -A "${IPS_OUTPUT_CHAIN}" -j MARK --set-xmark "0x0/${MASK}" | |
153 | fi | |
c9b07d6a SS |
154 | } |
155 | ||
d72b3e64 SS |
156 | case "$1" in |
157 | start) | |
158 | # Get amount of CPU cores. | |
c9b07d6a SS |
159 | cpu_count=$(get_cpu_count) |
160 | ||
161 | # Numer of NFQUES. | |
1a65ea1b | 162 | NFQUEUES="-q 0" |
c9b07d6a | 163 | |
1a65ea1b SS |
164 | if [ $cpu_count -gt "1" ]; then |
165 | NFQUEUES+=":$(($cpu_count-1))" | |
166 | fi | |
d72b3e64 | 167 | |
3c2c5483 SS |
168 | # Check if the IDS should be started. |
169 | if [ "$ENABLE_IDS" == "on" ]; then | |
3c2c5483 SS |
170 | # Start the IDS. |
171 | boot_mesg "Starting Intrusion Detection System..." | |
af006569 | 172 | /usr/bin/suricata -c /etc/suricata/suricata.yaml -D $NFQUEUES >/dev/null 2>/dev/null |
3c2c5483 | 173 | evaluate_retval |
00a03114 SS |
174 | |
175 | # Allow reading the pidfile. | |
176 | chmod 644 $PID_FILE | |
c9b07d6a SS |
177 | |
178 | # Flush the firewall chain | |
179 | flush_fw_chain | |
180 | ||
181 | # Generate firewall rules | |
182 | generate_fw_rules | |
3c2c5483 | 183 | fi |
d72b3e64 SS |
184 | ;; |
185 | ||
186 | stop) | |
187 | boot_mesg "Stopping Intrusion Detection System..." | |
00a03114 | 188 | killproc -p $PID_FILE /var/run |
d72b3e64 | 189 | |
3c2c5483 | 190 | # Flush firewall chain. |
c9b07d6a | 191 | flush_fw_chain |
3c2c5483 | 192 | |
57fda8c8 AF |
193 | # Sometimes suricata not correct shutdown. So killall. |
194 | killall -KILL /usr/bin/suricata 2>/dev/null | |
195 | ||
196 | # Remove suricata control socket. | |
d72b3e64 SS |
197 | rm /var/run/suricata/* >/dev/null 2>/dev/null |
198 | ||
62910a28 SS |
199 | # Trash remain pid file if still exists. |
200 | rm -f $PID_FILE >/dev/null 2>/dev/null | |
201 | ||
d72b3e64 SS |
202 | # Don't report returncode of rm if suricata was not started |
203 | exit 0 | |
204 | ;; | |
205 | ||
206 | status) | |
207 | statusproc /usr/bin/suricata | |
208 | ;; | |
209 | ||
210 | restart) | |
211 | $0 stop | |
212 | $0 start | |
213 | ;; | |
6187da50 SS |
214 | reload) |
215 | # Send SIGUSR2 to the suricata process to perform a reload | |
216 | # of the ruleset. | |
217 | kill -USR2 $(pidof suricata) | |
c9b07d6a SS |
218 | |
219 | # Flush the firewall chain. | |
220 | flush_fw_chain | |
221 | ||
222 | # Generate firewall rules. | |
223 | generate_fw_rules | |
6187da50 | 224 | ;; |
d72b3e64 SS |
225 | |
226 | *) | |
6187da50 | 227 | echo "Usage: $0 {start|stop|restart|reload|status}" |
d72b3e64 SS |
228 | exit 1 |
229 | ;; | |
230 | esac | |
231 | ||
232 | chmod 644 /var/log/suricata/* 2>/dev/null | |
233 | ||
234 | # End $rc_base/init.d/suricata |