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