Debian Codegraph @

whx.sh

raw symbols SCIP
1 #!/bin/bash
2 # whx.sh: "whois" spammer lookup
3 # Author: Walter Dnes
4 # Slight revisions (first section) by ABS Guide author.
5 # Used in ABS Guide with permission.
6
7 # Needs version 3.x or greater of Bash to run (because of =~ operator).
8 # Commented by script author and ABS Guide author.
9
10
11
12 E_BADARGS=85 # Missing command-line arg.
13 E_NOHOST=86 # Host not found.
14 E_TIMEOUT=87 # Host lookup timed out.
15 E_UNDEF=88 # Some other (undefined) error.
16
17 HOSTWAIT=10 # Specify up to 10 seconds for host query reply.
18 # The actual wait may be a bit longer.
19 OUTFILE=whois.txt # Output file.
20 PORT=4321
21
22
23 if [ -z "$1" ] # Check for (required) command-line arg.
24 then
25 echo "Usage: $0 domain name or IP address"
26 exit $E_BADARGS
27 fi
28
29
30 if [[ "$1" =~ [a-zA-Z][a-zA-Z]$ ]] # Ends in two alpha chars?
31 then # It's a domain name &&
32 #+ must do host lookup.
33 IPADDR=$(host -W $HOSTWAIT $1 | awk '{print $4}')
34 # Doing host lookup
35 #+ to get IP address.
36 # Extract final field.
37 else
38 IPADDR="$1" # Command-line arg was IP address.
39 fi
40
41 echo; echo "IP Address is: "$IPADDR""; echo
42
43 if [ -e "$OUTFILE" ]
44 then
45 rm -f "$OUTFILE"
46 echo "Stale output file \"$OUTFILE\" removed."; echo
47 fi
48
49
50 # Sanity checks.
51 # (This section needs more work.)
52 # ===============================
53 if [ -z "$IPADDR" ]
54 # No response.
55 then
56 echo "Host not found!"
57 exit $E_NOHOST # Bail out.
58 fi
59
60 if [[ "$IPADDR" =~ ^[;;] ]]
61 # ;; Connection timed out; no servers could be reached.
62 then
63 echo "Host lookup timed out!"
64 exit $E_TIMEOUT # Bail out.
65 fi
66
67 if [[ "$IPADDR" =~ [(NXDOMAIN)]$ ]]
68 # Host xxxxxxxxx.xxx not found: 3(NXDOMAIN)
69 then
70 echo "Host not found!"
71 exit $E_NOHOST # Bail out.
72 fi
73
74 if [[ "$IPADDR" =~ [(SERVFAIL)]$ ]]
75 # Host xxxxxxxxx.xxx not found: 2(SERVFAIL)
76 then
77 echo "Host not found!"
78 exit $E_NOHOST # Bail out.
79 fi
80
81
82
83
84 # ======================== Main body of script ========================
85
86 AFRINICquery() {
87 # Define the function that queries AFRINIC. Echo a notification to the
88 #+ screen, and then run the actual query, redirecting output to $OUTFILE.
89
90 echo "Searching for $IPADDR in whois.afrinic.net"
91 whois -h whois.afrinic.net "$IPADDR" > $OUTFILE
92
93 # Check for presence of reference to an rwhois.
94 # Warn about non-functional rwhois.infosat.net server
95 #+ and attempt rwhois query.
96 if grep -e "^remarks: .*rwhois\.[^ ]\+" "$OUTFILE"
97 then
98 echo " " >> $OUTFILE
99 echo "***" >> $OUTFILE
100 echo "***" >> $OUTFILE
101 echo "Warning: rwhois.infosat.net was not working \
102 as of 2005/02/02" >> $OUTFILE
103 echo " when this script was written." >> $OUTFILE
104 echo "***" >> $OUTFILE
105 echo "***" >> $OUTFILE
106 echo " " >> $OUTFILE
107 RWHOIS=`grep "^remarks: .*rwhois\.[^ ]\+" "$OUTFILE" | tail -n 1 |\
108 sed "s/\(^.*\)\(rwhois\..*\)\(:4.*\)/\2/"`
109 whois -h ${RWHOIS}:${PORT} "$IPADDR" >> $OUTFILE
110 fi
111 }
112
113 APNICquery() {
114 echo "Searching for $IPADDR in whois.apnic.net"
115 whois -h whois.apnic.net "$IPADDR" > $OUTFILE
116
117 # Just about every country has its own internet registrar.
118 # I don't normally bother consulting them, because the regional registry
119 #+ usually supplies sufficient information.
120 # There are a few exceptions, where the regional registry simply
121 #+ refers to the national registry for direct data.
122 # These are Japan and South Korea in APNIC, and Brasil in LACNIC.
123 # The following if statement checks $OUTFILE (whois.txt) for the presence
124 #+ of "KR" (South Korea) or "JP" (Japan) in the country field.
125 # If either is found, the query is re-run against the appropriate
126 #+ national registry.
127
128 if grep -E "^country:[ ]+KR$" "$OUTFILE"
129 then
130 echo "Searching for $IPADDR in whois.krnic.net"
131 whois -h whois.krnic.net "$IPADDR" >> $OUTFILE
132 elif grep -E "^country:[ ]+JP$" "$OUTFILE"
133 then
134 echo "Searching for $IPADDR in whois.nic.ad.jp"
135 whois -h whois.nic.ad.jp "$IPADDR"/e >> $OUTFILE
136 fi
137 }
138
139 ARINquery() {
140 echo "Searching for $IPADDR in whois.arin.net"
141 whois -h whois.arin.net "$IPADDR" > $OUTFILE
142
143 # Several large internet providers listed by ARIN have their own
144 #+ internal whois service, referred to as "rwhois".
145 # A large block of IP addresses is listed with the provider
146 #+ under the ARIN registry.
147 # To get the IP addresses of 2nd-level ISPs or other large customers,
148 #+ one has to refer to the rwhois server on port 4321.
149 # I originally started with a bunch of "if" statements checking for
150 #+ the larger providers.
151 # This approach is unwieldy, and there's always another rwhois server
152 #+ that I didn't know about.
153 # A more elegant approach is to check $OUTFILE for a reference
154 #+ to a whois server, parse that server name out of the comment section,
155 #+ and re-run the query against the appropriate rwhois server.
156 # The parsing looks a bit ugly, with a long continued line inside
157 #+ backticks.
158 # But it only has to be done once, and will work as new servers are added.
159 #@ ABS Guide author comment: it isn't all that ugly, and is, in fact,
160 #@+ an instructive use of Regular Expressions.
161
162 if grep -E "^Comment: .*rwhois.[^ ]+" "$OUTFILE"
163 then
164 RWHOIS=`grep -e "^Comment:.*rwhois\.[^ ]\+" "$OUTFILE" | tail -n 1 |\
165 sed "s/^\(.*\)\(rwhois\.[^ ]\+\)\(.*$\)/\2/"`
166 echo "Searching for $IPADDR in ${RWHOIS}"
167 whois -h ${RWHOIS}:${PORT} "$IPADDR" >> $OUTFILE
168 fi
169 }
170
171 LACNICquery() {
172 echo "Searching for $IPADDR in whois.lacnic.net"
173 whois -h whois.lacnic.net "$IPADDR" > $OUTFILE
174
175 # The following if statement checks $OUTFILE (whois.txt) for
176 #+ the presence of "BR" (Brasil) in the country field.
177 # If it is found, the query is re-run against whois.registro.br.
178
179 if grep -E "^country:[ ]+BR$" "$OUTFILE"
180 then
181 echo "Searching for $IPADDR in whois.registro.br"
182 whois -h whois.registro.br "$IPADDR" >> $OUTFILE
183 fi
184 }
185
186 RIPEquery() {
187 echo "Searching for $IPADDR in whois.ripe.net"
188 whois -h whois.ripe.net "$IPADDR" > $OUTFILE
189 }
190
191 # Initialize a few variables.
192 # * slash8 is the most significant octet
193 # * slash16 consists of the two most significant octets
194 # * octet2 is the second most significant octet
195
196
197
198
199 slash8=`echo $IPADDR | cut -d. -f 1`
200 if [ -z "$slash8" ] # Yet another sanity check.
201 then
202 echo "Undefined error!"
203 exit $E_UNDEF
204 fi
205 slash16=`echo $IPADDR | cut -d. -f 1-2`
206 # ^ Period specified as 'cut" delimiter.
207 if [ -z "$slash16" ]
208 then
209 echo "Undefined error!"
210 exit $E_UNDEF
211 fi
212 octet2=`echo $slash16 | cut -d. -f 2`
213 if [ -z "$octet2" ]
214 then
215 echo "Undefined error!"
216 exit $E_UNDEF
217 fi
218
219
220 # Check for various odds and ends of reserved space.
221 # There is no point in querying for those addresses.
222
223 if [ $slash8 == 0 ]; then
224 echo $IPADDR is '"This Network"' space\; Not querying
225 elif [ $slash8 == 10 ]; then
226 echo $IPADDR is RFC1918 space\; Not querying
227 elif [ $slash8 == 14 ]; then
228 echo $IPADDR is '"Public Data Network"' space\; Not querying
229 elif [ $slash8 == 127 ]; then
230 echo $IPADDR is loopback space\; Not querying
231 elif [ $slash16 == 169.254 ]; then
232 echo $IPADDR is link-local space\; Not querying
233 elif [ $slash8 == 172 ] && [ $octet2 -ge 16 ] && [ $octet2 -le 31 ];then
234 echo $IPADDR is RFC1918 space\; Not querying
235 elif [ $slash16 == 192.168 ]; then
236 echo $IPADDR is RFC1918 space\; Not querying
237 elif [ $slash8 -ge 224 ]; then
238 echo $IPADDR is either Multicast or reserved space\; Not querying
239 elif [ $slash8 -ge 200 ] && [ $slash8 -le 201 ]; then LACNICquery "$IPADDR"
240 elif [ $slash8 -ge 202 ] && [ $slash8 -le 203 ]; then APNICquery "$IPADDR"
241 elif [ $slash8 -ge 210 ] && [ $slash8 -le 211 ]; then APNICquery "$IPADDR"
242 elif [ $slash8 -ge 218 ] && [ $slash8 -le 223 ]; then APNICquery "$IPADDR"
243
244 # If we got this far without making a decision, query ARIN.
245 # If a reference is found in $OUTFILE to APNIC, AFRINIC, LACNIC, or RIPE,
246 #+ query the appropriate whois server.
247
248 else
249 ARINquery "$IPADDR"
250 if grep "whois.afrinic.net" "$OUTFILE"; then
251 AFRINICquery "$IPADDR"
252 elif grep -E "^OrgID:[ ]+RIPE$" "$OUTFILE"; then
253 RIPEquery "$IPADDR"
254 elif grep -E "^OrgID:[ ]+APNIC$" "$OUTFILE"; then
255 APNICquery "$IPADDR"
256 elif grep -E "^OrgID:[ ]+LACNIC$" "$OUTFILE"; then
257 LACNICquery "$IPADDR"
258 fi
259 fi
260
261 #@ ---------------------------------------------------------------
262 # Try also:
263 # wget http://logi.cc/nw/whois.php3?ACTION=doQuery&DOMAIN=$IPADDR
264 #@ ---------------------------------------------------------------
265
266 # We've now finished the querying.
267 # Echo a copy of the final result to the screen.
268
269 cat $OUTFILE
270 # Or "less $OUTFILE" . . .
271
272
273 exit 0
274
275 #@ ABS Guide author comments:
276 #@ Nothing fancy here, but still a very useful tool for hunting spammers.
277 #@ Sure, the script can be cleaned up some, and it's still a bit buggy,
278 #@+ (exercise for reader), but all the same, it's a nice piece of coding
279 #@+ by Walter Dnes.
280 #@ Thank you!