| 1 |
#!/bin/sh |
| 2 |
# Copyright (C) 2000-2013 Boris Wesslowski |
| 3 |
# $Id: fwlw_respond 731 2013-05-17 14:15:23Z bw $ |
| 4 |
# fwlogwatch realtime response script |
| 5 |
|
| 6 |
# Set the $MODE variable to activate realtime modification of |
| 7 |
# ipchains or netfilter packet filters. |
| 8 |
|
| 9 |
# You may want to add custom commands at the commented spots to modify |
| 10 |
# tcp wrappers or ipfilter rules or even remote control access lists |
| 11 |
# on cisco routers... |
| 12 |
|
| 13 |
# $TARGET contains the name of the chain that will be used for rules |
| 14 |
# generated by this script. |
| 15 |
|
| 16 |
# See fwlw_notify for the contents of the variables passed by fwlogwatch |
| 17 |
|
| 18 |
#MODE=iptables |
| 19 |
IPCHAINS=/sbin/ipchains |
| 20 |
IPTABLES=/sbin/iptables |
| 21 |
TARGET=fwlw |
| 22 |
RETVAL=0 |
| 23 |
|
| 24 |
|
| 25 |
case "$1" in |
| 26 |
############################################################################## |
| 27 |
|
| 28 |
start) |
| 29 |
case "$MODE" in |
| 30 |
ipchains) |
| 31 |
if $IPCHAINS -n -L $TARGET 2>/dev/null | /bin/grep "Chain $TARGET " >/dev/null |
| 32 |
then |
| 33 |
$IPCHAINS -F $TARGET |
| 34 |
else |
| 35 |
$IPCHAINS -N $TARGET |
| 36 |
$IPCHAINS -I input -j $TARGET |
| 37 |
fi |
| 38 |
;; |
| 39 |
iptables) |
| 40 |
if $IPTABLES -t filter -n -L $TARGET 2>/dev/null | /bin/grep "Chain $TARGET " >/dev/null |
| 41 |
then |
| 42 |
$IPTABLES -F $TARGET |
| 43 |
else |
| 44 |
$IPTABLES -N $TARGET |
| 45 |
$IPTABLES -I INPUT -j $TARGET |
| 46 |
$IPTABLES -I FORWARD -j $TARGET |
| 47 |
fi |
| 48 |
;; |
| 49 |
# Insert setup for custom response here |
| 50 |
*) |
| 51 |
RETVAL=1 |
| 52 |
;; |
| 53 |
esac |
| 54 |
;; |
| 55 |
|
| 56 |
############################################################################## |
| 57 |
|
| 58 |
add) |
| 59 |
if [ -z "$3" ] |
| 60 |
then |
| 61 |
exit 1 |
| 62 |
fi |
| 63 |
|
| 64 |
case "$MODE" in |
| 65 |
ipchains) |
| 66 |
if $IPCHAINS -n -L $TARGET 2>/dev/null | /bin/grep "Chain $TARGET " >/dev/null |
| 67 |
then |
| 68 |
$IPCHAINS -A $TARGET -s $3 -j DENY |
| 69 |
else |
| 70 |
$IPCHAINS -N $TARGET |
| 71 |
$IPCHAINS -I input -j $TARGET |
| 72 |
$IPCHAINS -A $TARGET -s $3 -j DENY |
| 73 |
fi |
| 74 |
;; |
| 75 |
iptables) |
| 76 |
if $IPTABLES -t filter -n -L $TARGET 2>/dev/null | /bin/grep "Chain $TARGET " >/dev/null |
| 77 |
then |
| 78 |
$IPTABLES -A $TARGET -s $3 -j DROP |
| 79 |
else |
| 80 |
$IPTABLES -N $TARGET |
| 81 |
$IPTABLES -I INPUT -j $TARGET |
| 82 |
$IPTABLES -I FORWARD -j $TARGET |
| 83 |
$IPTABLES -A $TARGET -s $3 -j DROP |
| 84 |
fi |
| 85 |
;; |
| 86 |
# Insert custom response action here |
| 87 |
*) |
| 88 |
RETVAL=1 |
| 89 |
;; |
| 90 |
esac |
| 91 |
;; |
| 92 |
|
| 93 |
############################################################################## |
| 94 |
|
| 95 |
remove) |
| 96 |
if [ -z "$3" ] |
| 97 |
then |
| 98 |
exit 1 |
| 99 |
fi |
| 100 |
|
| 101 |
case "$MODE" in |
| 102 |
ipchains) |
| 103 |
if $IPCHAINS -n -L $TARGET 2>/dev/null | /bin/grep "Chain $TARGET " >/dev/null |
| 104 |
then |
| 105 |
$IPCHAINS -D $TARGET -s $3 -j DENY |
| 106 |
else |
| 107 |
RETVAL=1 |
| 108 |
fi |
| 109 |
;; |
| 110 |
iptables) |
| 111 |
if $IPTABLES -t filter -n -L $TARGET 2>/dev/null | /bin/grep "Chain $TARGET " >/dev/null |
| 112 |
then |
| 113 |
$IPTABLES -D $TARGET -s $3 -j DROP |
| 114 |
else |
| 115 |
RETVAL=1 |
| 116 |
fi |
| 117 |
;; |
| 118 |
# Insert custom response action stop here |
| 119 |
*) |
| 120 |
RETVAL=1 |
| 121 |
;; |
| 122 |
esac |
| 123 |
;; |
| 124 |
|
| 125 |
############################################################################## |
| 126 |
|
| 127 |
stop) |
| 128 |
case "$MODE" in |
| 129 |
ipchains) |
| 130 |
if $IPCHAINS -n -L $TARGET 2>/dev/null | /bin/grep "Chain $TARGET " >/dev/null |
| 131 |
then |
| 132 |
$IPCHAINS -F $TARGET |
| 133 |
$IPCHAINS -D input -j $TARGET |
| 134 |
$IPCHAINS -X $TARGET |
| 135 |
fi |
| 136 |
;; |
| 137 |
iptables) |
| 138 |
if $IPTABLES -t filter -n -L $TARGET 2>/dev/null | /bin/grep "Chain $TARGET " >/dev/null |
| 139 |
then |
| 140 |
$IPTABLES -F $TARGET |
| 141 |
$IPTABLES -D INPUT -j $TARGET |
| 142 |
$IPTABLES -D FORWARD -j $TARGET |
| 143 |
$IPTABLES -X $TARGET |
| 144 |
fi |
| 145 |
;; |
| 146 |
# Insert cleanup for custom response here |
| 147 |
*) |
| 148 |
RETVAL=1 |
| 149 |
;; |
| 150 |
esac |
| 151 |
;; |
| 152 |
|
| 153 |
############################################################################## |
| 154 |
|
| 155 |
*) |
| 156 |
echo "Usage: $0 {start|add|remove|stop} [count src_ip dst_ip protocol src_port dst_port]" |
| 157 |
;; |
| 158 |
|
| 159 |
############################################################################## |
| 160 |
esac |
| 161 |
exit $RETVAL |
| 162 |
# EOF |