Imported Upstream version 2.3
[ossec-hids.git] / active-response / firewalls / pf.sh
1 #!/bin/sh
2 # Author: Rafael M. Capovilla
3 # Last modified: Daniel B. Cid
4
5 UNAME=`uname`
6 GREP=`which grep`
7 PFCTL="/sbin/pfctl"
8
9 # Getting pf rules file.
10 PFCTL_RULES=`${GREP} pf_rules /etc/rc.conf | awk -F"=" '{print $2}' | awk '{print $1}' | awk -F"\"" '{print $1 $2}'`
11 if [ "X${PFCTL_RULES}" = "X" ]; then
12     PFCTL_RULES="/etc/pf.conf"
13 fi    
14
15 # Checking if ossec table is configured
16 PFCTL_TABLE=`cat ${PFCTL_RULES} | egrep -v "(^#|^$)" | grep ossec_fwtable | head -1 | awk '{print $2}' | sed "s/<//;s/>//"`
17 ARG1=""
18 ACTION=$1
19 USER=$2
20 IP=$3
21
22
23 # Finding path
24 LOCAL=`dirname $0`;
25 cd $LOCAL
26 cd ../
27 PWD=`pwd`
28 echo "`date` $0 $1 $2 $3 $4 $5" >> ${PWD}/../logs/active-responses.log
29
30
31 # Checking for an IP
32 if [ "x${IP}" = "x" ]; then
33    echo "$0: <action> <username> <ip>" 
34    exit 1;
35 fi
36
37
38
39 # Blocking IP
40 if [ "x${ACTION}" != "xadd" -a "x${ACTION}" != "xdelete" ]; then
41    echo "$0: invalid action: ${ACTION}"
42    echo "$0: invalid action: ${ACTION}" >> ${PWD}/ossec-hids-responses.log
43    exit 1;
44 fi
45
46
47
48 # OpenBSD and FreeBSD pf
49 if [ "X${UNAME}" = "XOpenBSD" -o "X${UNAME}" = "XFreeBSD" ]; then
50   
51   # Checking if pfctl is present
52   ls ${PFCTL} > /dev/null 2>&1
53   if [ ! $? = 0 ]; then
54       echo "$0: PF not configured."
55       echo "$0: PF not configured." >> ${PWD}/ossec-hids-responses.log
56           exit 0;
57   fi
58
59   # Checking if we have pf config file
60   if [ -e ${PFCTL_RULES} ]; then
61       
62         #Checking if we got the table to add the bad guys
63         if [ "x${PFCTL_TABLE}" = "x" ]; then
64         echo "$0: PF not configured."
65         echo "$0: PF not configured." >> ${PWD}/ossec-hids-responses.log
66                 exit 0;
67         else
68                 if [ "x${ACTION}" = "xadd" ]; then
69                         ARG1="-t $PFCTL_TABLE -T add ${IP}"
70                 else
71                         ARG1="-t $PFCTL_TABLE -T delete ${IP}"
72                 fi
73         fi
74   else
75         exit 0;
76   fi
77
78   #Executing it
79   ${PFCTL} ${ARG1} > /dev/null 2>&1 
80
81   exit 0;
82   
83 else
84     exit 0;
85 fi