1 #!/usr/bin/env bash
2 # Script to check if specified IP is on common known dns blacklists
3 # Copyright (C) 2018 Aaron Ball <nullspoon@oper.io>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 # Default to opendns main nameserver if the NS variable isn't set
19 export NS=${NS:-208.67.220.220}
20
21 # Pertinent color escape sequences
22 export CGREEN=$'\e[32m'
23 export CRED=$'\e[31m'
24 export CRESET=$'\e[0m'
25
26 # is_ipv4:
27 # Checks if the provided string is a valid IPv4 address.
28 #
29 # @str String to check
30 # @return (stdout) 1 == valid ip, 0 == invalid ip
31 is_ipv4() {
32 local str="${1}"
33 local match
34 match=$(printf "${str}" | grep -E '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$')
35 if [ "${match}" = "${str}" ]; then
36 printf 1
37 return 0;
38 fi
39 printf 0;
40 return 1;
41 }
42
43
44 usage() {
45 printf "Usage:\n %s [options] <ip>\n\n" "$(basename ${0})"
46 printf "Options:\n"
47 printf " -h,--help Print this help text\n"
48 printf " -m,--manifest Path to file containing DNSBL manifest\n"
49 printf " -s,--summary Print only number of lists IP was found on\n"
50 }
51
52
53 parseargs() {
54 local args=("${@}")
55 for (( i = 0; i < ${#args[@]}; i++ )); do
56 if [ "${args[$i]}" = '-h' ] || [ "${args[$i]}" = '-h' ]; then
57 usage
58 exit 0
59 elif [ "${args[$i]}" = '-s' ] || [ "${args[$i]}" = '--summary' ]; then
60 _SUMMARY=1
61 elif [ "${args[$i]}" = '-m' ] || [ "${args[$i]}" = '--manifest' ]; then
62 i=$(( i + 1 ))
63 _MANIFEST="${args[$i]}"
64 else
65 _IP="${args[$i]}"
66 fi
67 done
68
69 if [ -z "${_IP}" ]; then
70 printf "IP address required\n"
71 return 1
72 fi
73
74 if [ $(is_ipv4 "${_IP}") = 0 ]; then
75 printf "Provided IP '%s' is not a valid IPv4 address\n" "${_IP}"
76 return 1
77 fi
78 }
79
80
81 # main:
82 # Ye olde' main.
83 #
84 # @ip IP address to check for blacklist
85 main() {
86 local rev # IP address, reversed for dns lookup (dig)
87 local resp # Response from dns query
88 local dnsbls # Array of dns blacklist endpoints
89 local found # Number of times the ip was found in blacklists
90
91 local _IP='' # IP to query DNSBLs for
92 local _SUMMARY=0 # Print only DNSBL count summary (number of lists
93 # blacklisting the ip)
94 local _MANIFEST='' # Manifest of DNSBLs to check
95
96 # Set defaults
97 _MANIFEST="dnsbls.txt"
98
99 # Parse cli args into arg variables
100 parseargs ${@} || return $?
101
102 # If the terminal is not a char terminal (eg: someone is using less, more,
103 # cat, etc), we don't want to print escape codes because they will get
104 # mangled by the tool most likely.
105 if [ ! -t 1 ]; then
106 unset CGREEN
107 unset CRED
108 unset CRESET
109 fi
110
111 # Reverse the ip address
112 rev=$(printf '%s.' "${_IP}" | tac -s.)
113
114 # Some basic information
115 if [ "${_SUMMARY}" -eq 0 ]; then
116 printf "Checking %s\n" "${_IP}"
117 printf "Reverse DNS: %s\n\n" "$(dig @${NS} +short -x ${_IP})"
118 fi
119
120 if [ ! -f "${_MANIFEST}" ]; then
121 printf "ERROR: DNSBL manifest '%s' not accessible.\n" "${_MANIFEST}"
122 printf "Please set LIST environment variable to file that exists\n"
123 return 1
124 else
125 dnsbls=($(grep -v '^#' "${_MANIFEST}"))
126 fi
127
128 found=0
129 for bl in ${dnsbls[@]}; do
130 # I can dig it
131 resp="$(dig @${NS} +short -t a ${rev}${bl})"
132 [ "${_SUMMARY}" -eq 0 ] && printf "%-25s: " "${bl}"
133 if [ -z "${resp}" ]; then
134 [ "${_SUMMARY}" -eq 0 ] && printf "%bNot found%b\n" "${CGREEN}" "${CRESET}"
135 else
136 [ "${_SUMMARY}" -eq 0 ] && printf "%bFound%b\n" "${CRED}" "${CRESET}"
137 found=$((found + 1))
138 fi
139 done
140
141 if [ ${_SUMMARY} -eq 0 ]; then
142 printf "\nFound %s on %d lists\n" "${_IP}" "${found}"
143 else
144 printf "%d\n" "${found}"
145 fi
146 }
147
148 main ${@}
|