2 # Adds an IP to the /etc/hosts.deny file
3 # Requirements: sshd and other binaries with tcp wrappers support
5 # Author: Daniel B. Cid
6 # Last modified: Nov 09, 2005
16 LOCK="${PWD}/host-deny-lock"
17 LOCK_PID="${PWD}/host-deny-lock/pid"
21 # This number should be more than enough (even if a hundred
22 # instances of this script is ran together). If you have
23 # a really loaded env, you can increase it to 75 or 100.
33 mkdir ${LOCK} > /dev/null 2>&1
35 if [ "${MSL}" = "0" ]; then
36 # Lock aquired (setting the pid)
37 echo "$$" > ${LOCK_PID}
41 # Getting currently/saved PID locking the file
42 C_PID=`cat ${LOCK_PID} 2>/dev/null`
43 if [ "x" = "x${S_PID}" ]; then
47 # Breaking out of the loop after X attempts
48 if [ "x${C_PID}" = "x${S_PID}" ]; then
52 # Sleep 1 after 10/25 interactions
53 if [ "$i" = "10" -o "$i" = "25" ]; then
59 # So i increments 2 by 2 if the pid does not change.
60 # If the pid keeps changing, we will increments one
61 # by one and fail after MAX_ITERACTION
62 if [ "$i" = "${MAX_ITERATION}" ]; then
63 echo "`date` Unable to execute. Locked: $0" \
64 >> ${PWD}/ossec-hids-responses.log
66 # Unlocking and exiting
81 echo "`date` $0 $1 $2 $3 $4 $5" >> ${PWD}/../logs/active-responses.log
84 # IP Address must be provided
85 if [ "x${IP}" = "x" ]; then
86 echo "$0: Missing argument <action> <user> (ip)"
91 # Checking for invalid entries (lacking ".", etc)
92 echo "${IP}" | grep "\." > /dev/null 2>&1
94 echo "`date` Invalid ip/hostname entry: ${IP}" >> ${PWD}/../logs/active-responses.log
99 # Adding the ip to hosts.deny
100 if [ "x${ACTION}" = "xadd" ]; then
102 if [ "X$UNAME" = "XFreeBSD" ]; then
103 echo "ALL : ${IP} : deny" >> /etc/hosts.allow
105 echo "ALL:${IP}" >> /etc/hosts.deny
111 # Deleting from hosts.deny
112 elif [ "x${ACTION}" = "xdelete" ]; then
114 TMP_FILE = `mktemp /var/ossec/ossec-hosts.XXXXXXXXXX`
115 if [ "X${TMP_FILE}" = "X" ]; then
116 # Cheap fake tmpfile, but should be harder then no random data
117 TMP_FILE = "/var/ossec/ossec-hosts.`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -1 `"
119 if [ "X$UNAME" = "XFreeBSD" ]; then
120 cat /etc/hosts.allow | grep -v "ALL : ${IP} : deny$"> ${TMP_FILE}
121 mv ${TMP_FILE} /etc/hosts.allow
123 cat /etc/hosts.deny | grep -v "ALL:${IP}$"> ${TMP_FILE}
124 cat ${TMP_FILE} > /etc/hosts.deny
133 echo "$0: invalid action: ${ACTION}"