Imported Upstream version 2.3
[ossec-hids.git] / active-response / firewall-drop.sh
1 #!/bin/sh
2 # Adds an IP to the iptables drop list (if linux)
3 # Adds an IP to the ipfilter drop list (if solaris, freebsd or netbsd)
4 # Adds an IP to the ipsec drop list (if aix)
5 # Requirements: Linux with iptables, Solaris/FreeBSD/NetBSD with ipfilter or AIX with IPSec
6 # Expect: srcip
7 # Author: Ahmet Ozturk (ipfilter and IPSec)
8 # Author: Daniel B. Cid (iptables)
9 # Last modified: Feb 14, 2006
10
11 UNAME=`uname`
12 ECHO="/bin/echo"
13 GREP="/bin/grep"
14 IPTABLES="/sbin/iptables"
15 IPFILTER="/sbin/ipf"
16 GENFILT="/usr/sbin/genfilt"
17 LSFILT="/usr/sbin/lsfilt"
18 MKFILT="/usr/sbin/mkfilt"
19 RMFILT="/usr/sbin/rmfilt"
20 ARG1=""
21 ARG2=""
22 RULEID=""
23 ACTION=$1
24 USER=$2
25 IP=$3
26
27 LOCAL=`dirname $0`;
28 cd $LOCAL
29 cd ../
30 PWD=`pwd`
31 echo "`date` $0 $1 $2 $3 $4 $5" >> ${PWD}/../logs/active-responses.log
32
33
34 # Checking for an IP
35 if [ "x${IP}" = "x" ]; then
36    echo "$0: <action> <username> <ip>" 
37    exit 1;
38 fi
39
40
41
42 # Blocking IP
43 if [ "x${ACTION}" != "xadd" -a "x${ACTION}" != "xdelete" ]; then
44    echo "$0: invalid action: ${ACTION}"
45    exit 1;
46 fi
47
48
49
50 # We should run on linux
51 if [ "X${UNAME}" = "XLinux" ]; then
52    if [ "x${ACTION}" = "xadd" ]; then
53       ARG1="-I INPUT -s ${IP} -j DROP"
54       ARG2="-I FORWARD -s ${IP} -j DROP"
55    else
56       ARG1="-D INPUT -s ${IP} -j DROP"
57       ARG2="-D FORWARD -s ${IP} -j DROP"
58    fi
59    
60    # Checking if iptables is present
61    ls ${IPTABLES} >> /dev/null 2>&1
62    if [ $? != 0 ]; then
63       IPTABLES="/usr"${IPTABLES}
64       ls ${IPTABLES} >> /dev/null 2>&1
65       if [ $? != 0 ]; then
66          exit 0;
67       fi
68    fi
69
70    # Executing and exiting
71    COUNT=0;
72    while [ 1 ]; do
73        echo ".."
74         ${IPTABLES} ${ARG1}
75         RES=$?
76         if [ $RES = 0 ]; then
77             break;
78         else
79             COUNT=`expr $COUNT + 1`;
80             echo "`date` Unable to run (iptables returning != $RES): $COUNT - $0 $1 $2 $3 $4 $5" >> ${PWD}/../logs/active-responses.log     
81             sleep $COUNT;
82
83             if [ $COUNT -gt 4 ]; then
84                 break;
85             fi    
86         fi
87    done
88    
89    while [ 1 ]; do
90         ${IPTABLES} ${ARG2}
91         RES=$?
92         if [ $RES = 0 ]; then
93             break;
94         else
95             COUNT=`expr $COUNT + 1`;
96             echo "`date` Unable to run (iptables returning != $RES): $COUNT - $0 $1 $2 $3 $4 $5" >> ${PWD}/../logs/active-responses.log     
97             sleep $COUNT;
98
99             if [ $COUNT -gt 4 ]; then
100                 break;
101             fi       
102         fi
103    done
104             
105    exit 0;
106    
107 # FreeBSD, SunOS or NetBSD with ipfilter
108 elif [ "X${UNAME}" = "XFreeBSD" -o "X${UNAME}" = "XSunOS" -o "X${UNAME}" = "XNetBSD" ]; then
109    
110    # Checking if ipfilter is present
111    ls ${IPFILTER} >> /dev/null 2>&1
112    if [ $? != 0 ]; then
113       exit 0;
114    fi    
115
116    # Checking if echo is present
117    ls ${ECHO} >> /dev/null 2>&1
118    if [ $? != 0 ]; then
119        exit 0;
120    fi    
121    
122    if [ "x${ACTION}" = "xadd" ]; then
123       ARG1="\"@1 block out quick from any to ${IP}\""
124       ARG2="\"@1 block in quick from ${IP} to any\""
125       IPFARG="${IPFILTER} -f -"
126    else
127       ARG1="\"@1 block out quick from any to ${IP}\""
128       ARG2="\"@1 block in quick from ${IP} to any\""
129       IPFARG="${IPFILTER} -rf -"
130    fi
131   
132    # Executing it 
133    eval ${ECHO} ${ARG1}| ${IPFARG}       
134    eval ${ECHO} ${ARG2}| ${IPFARG}
135    
136    exit 0;
137
138 # AIX with ipsec
139 elif [ "X${UNAME}" = "XAIX" ]; then
140
141   # Checking if genfilt is present
142   ls ${GENFILT} >> /dev/null 2>&1
143   if [ $? != 0 ]; then
144      exit 0;
145   fi
146          
147   # Checking if lsfilt is present
148   ls ${LSFILT} >> /dev/null 2>&1
149   if [ $? != 0 ]; then
150      exit 0;
151   fi
152   # Checking if mkfilt is present
153   ls ${MKFILT} >> /dev/null 2>&1
154   if [ $? != 0 ]; then
155      exit 0;
156   fi
157          
158   # Checking if rmfilt is present
159   ls ${RMFILT} >> /dev/null 2>&1
160   if [ $? != 0 ]; then
161      exit 0;
162   fi
163
164   if [ "x${ACTION}" = "xadd" ]; then
165     ARG1=" -v 4 -a D -s ${IP} -m 255.255.255.255 -d 0.0.0.0 -M 0.0.0.0 -w B -D \"Access Denied by OSSEC-HIDS\"" 
166     #Add filter to rule table
167     eval ${GENFILT} ${ARG1}
168     
169     #Deactivate  and activate the filter rules.
170     eval ${MKFILT} -v 4 -d
171     eval ${MKFILT} -v 4 -u
172   else
173     # removing a specific rule is not so easy :(
174      eval ${LSFILT} -v 4 -O  | ${GREP} ${IP} | 
175      while read -r LINE
176      do
177          RULEID=`${ECHO} ${LINE} | cut -f 1 -d "|"`
178          let RULEID=${RULEID}+1
179          ARG1=" -v 4 -n ${RULEID}"
180          eval ${RMFILT} ${ARG1}
181      done
182     #Deactivate  and activate the filter rules.
183     eval ${MKFILT} -v 4 -d
184     eval ${MKFILT} -v 4 -u
185   fi
186
187 else
188     exit 0;
189 fi