-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipinfo
More file actions
executable file
·648 lines (564 loc) · 15.2 KB
/
ipinfo
File metadata and controls
executable file
·648 lines (564 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#!/bin/bash -
# Print extra information about IPv4 address to standard output
# Version 3.0
# Written by Bernard Lim <lim.yiok.kia@gmail.com>
AUTHOR="Bernard Lim <lim.yiok.kia@gmail.com>"
PROG=$(basename $0)
VERSION="3.0"
HTTP="http://"
SOCKS="socks4://"
USER_AGENT="Mozilla/5.0"
DNS_OPTION=
PROXY_OPTION=
MAX_FIELDS=11
# public DNS servers
# non-responsive DNS servers are commented out
DNS_HOST="pcsupport.about.com"
DNS_PATH="/od/tipstricks/a/free-public-dns-servers.htm"
DNS_SOURCE="${HTTP}${DNS_HOST}${DNS_PATH}"
DNS_SERVERS=( \
209.244.0.3 \
209.244.0.4 \
8.8.8.8 \
8.8.4.4 \
84.200.69.80 \
84.200.70.40
8.26.56.26 \
8.20.247.20 \
208.67.222.222 \
208.67.220.220 \
156.154.70.1 \
156.154.71.1 \
199.85.126.10 \
199.85.127.10 \
#81.218.119.11 \
#209.88.198.133 \
195.46.39.39 \
195.46.39.40 \
107.150.40.234 \
50.116.23.211 \
208.76.50.50 \
#208.76.51.51 \
216.146.35.35 \
216.146.36.36 \
37.235.1.174 \
37.235.1.177 \
89.233.43.71 \
#91.239.100.100 \
74.82.42.42 \
109.69.8.51 )
# dependency message
depends() {
echo "$PROG: depends on \`$*'" >&2
echo "Install \`$*' and try again." >&2
exit 1
}
# die a graceful death!
die() {
echo "$PROG: $*" >&2
echo "Try \`$PROG -h' for more information." >&2
exit 1
}
# fanciful output
b="\033[1m" # bold
u="\033[4m" # underline
o="\033[0m" # off
# shorter usage
short() {
usage | sed '2q'
echo "Try \`$PROG -h' for more information."
}
# usage message
usage() {
cat <<EOF
Usage: $PROG [OPTION]... [ADDRESS]...
Print extra information about ADDRESS to standard output.
$(echo -e ${b}Output${o})
ipaddr|ptr;...|pdns;...|cc|state|city|zip|lat|lon|asn|isp
where "|" is the field separator.
$(echo -e ${b}Fields${o})
1) ipaddr IPv4 address in quad-dotted notation
2) ptr PTR record from reverse DNS lookup
3) pdns Passive DNS
4) cc Two-letter country code
5) state Two-letter state code
6) city City name
7) zip Postal or ZIP code
8) lat Latitude coordinate
9) lon Longitude coordinate
10) asn Autonomous System (AS) number
11) isp AS Operator or previously ISP
$(echo -e ${b}Options${o})
-d Use random public domain name server
to perform reverse DNS lookup
-f LIST Select only these fields. Like \`cut'
-h Display this help and exit
-l(d|p) List available public (d)omain name
servers or open (p)roxies
-p Use random open proxy to perform
passive DNS lookup and GeoIP lookup
-v Print version information and exit
Use one, and only one -f. Each LIST is made up of one range,
or many ranges separated by commas. Selected input is written
in the same order that it is read, and is written exactly once.
Each range is one of:
N N'th field, counted from 1
N- from N'th field, to end of line
N-M from N'th to M'th (included) field
-M from first to M'th (included) field
$(echo -e ${b}Examples${o})
$PROG 8.8.8.8 # Use default settings and print all fields
$PROG -l(d|p) # List available domain name servers or open proxies
$PROG -d 8.8.8.8 # Use random domain name server and print all fields
$PROG -f2 8.8.8.8 # Print second field, PTR record
$PROG -f-2,4- 8.8.8.8 # Print fields 1 to 2 and fields 4 to 11
Report $PROG bugs to $AUTHOR
This software is distributed in the hope that it will be
useful but without any warranty. It is provided “as is”.
EOF
}
# proxify - return random proxy from xroxy.com
# timeout in 5 seconds
# type = Anonymous, 1atency < 1ms, reliability > 90%
proxify() {
PROXY_HOST="www.xroxy.com"
PROXY_TYPE="Anonymous"
PROXY_LIST="type=${PROXY_TYPE}&latency=1000&reliability=9000&desc=true&pnum="
PROXY_PATH="/proxylist.php?"
PROXY_SOURCE="${PROXY_HOST}${PROXY_PATH}${PROXY_LIST}"
get_proxies() {
for ((page=0; page<3; page++)); do
(curl -m5 -s -A $USER_AGENT "${PROXY_SOURCE}$page" &)
done |
grep -Eo 'host=[0-9.]+&port=[0-9]+' |
sed -e 's/\&port=/:/' -e 's/host=//'
}
if which curl &>/dev/null; then
PROXY_SERVERS=($(get_proxies))
if [ ${#PROXY_SERVERS[@]} -gt 0 ]; then
PROXY_SERVER=${PROXY_SERVERS[$RANDOM % ${#PROXY_SERVERS[@]}]}
echo "$PROXY_SERVER"
else
PROXY_SERVER=
return 1
fi
else
depends "curl"
fi
}
# parse ranges separated by commas
# and print sorted/unique integers
# for up to 50, if limit is absent
parse() {
local limit=$2
local ranges=($(tr ',' ' ' <<<"$1"))
local nr=$(awk -F"," '{ print NF }' <<<"$1")
if [ ${#ranges[@]} -lt $nr ]; then
die "fields are numbered from 1"
fi
for range in ${ranges[@]}; do
expansion=$(expand $range ${limit:=50})
if [ -z "$expansion" ]; then return 1; fi
echo "$expansion"
done |
tr ' ' '\n' |
sort -un |
tr '\n' ' '
}
# expand ranges of the form N, N-, N-M, -N
# bash - largest positive integer is 9223372036854775807
expand() {
local input=$1
local max=$2
if [ -z "$input" ]; then
die "fields are numbered from 1"
fi
if grep -Eo '^-$' <<<"$input" &>/dev/null; then
die "invalid range with no endpoint: $input"
fi
# N
if grep -Eo '^[0-9]+$' <<<"$input" &>/dev/null; then
let value="$input"
let nd="$(echo -n $value | wc -c)"
if [ $nd -ge 5 ]; then
die "field number \`$input' is too large"
fi
if [ $value -eq 0 ]; then
die "fields are numbered from 1"
fi
echo $value
# N-
elif grep -Eo '^[0-9]+-$' <<<"$input" &>/dev/null; then
lo="${input%-}"
let value="$lo"
let nd="$(echo -n $value | wc -c)"
if [ $nd -ge 5 ]; then
die "field number \`$lo' is too large"
fi
if [ $value -eq 0 ]; then
die "fields are numbered from 1"
fi
seq $lo $max
# N-M
elif grep -Eo '^[0-9]+-[0-9]+' <<<"$input" &>/dev/null; then
lo="${input%-*}"
let value="$lo"
let nd="$(echo -n $value | wc -c)"
if [ $nd -ge 5 ]; then
die "field number \`$lo' is too large"
fi
if [ $value -eq 0 ]; then
die "fields are numbered from 1"
fi
hi="${input#*-}"
let value="$hi"
let nd="$(echo -n $value | wc -c)"
if [ $nd -ge 5 ]; then
die "field number \`$hi' is too large"
fi
if [ $value -eq 0 ]; then
die "fields are numbered from 1"
fi
if (( $lo > $hi )); then
die "invalid decreasing range"
fi
if (( $hi > $max )); then
hi=$max
fi
seq $lo $hi
# -N
elif grep -Eo '^-[0-9]+$' <<<"$input" &>/dev/null; then
hi="${input#-}"
let value="$hi"
let nd="$(echo -n $value | wc -c)"
if [ $nd -ge 5 ]; then
die "field number \`$hi' is too large"
fi
if [ $value -eq 0 ]; then
die "fields are numbered from 1"
fi
if (( $hi > $max )); then
hi=$max
fi
seq $hi
# No man's land
else
die "invalid field list"
fi
}
# wrapper for host - DNS lookup tool
# timeout in 1 second
dnslookup() {
if which host &>/dev/null; then
local dnp="domain name pointer"
local A=$(host -W1 -s $1 $DNS_OPTION | grep "$dnp" || echo "(NXDOMAIN)")
local B=$(awk '{ print $NF }' <<<"$A")
if [ $(tr -cd '.' <<<"$B" | wc -c) -ne 1 ]; then
sed -r 's/\.?$//' <<<"$B" |
tr '\n' ';' |
sed 's/;$//'
else
echo "(NULL)"
fi
else
depends "host"
fi
}
# over-simplistic JSON parser
retrieve() {
local json=$1
local key=$2
local value=$(grep -Eo "\"$key\":.*,?" <<<"$json" | \
cut -d"," -f1 | \
cut -d":" -f2 | \
tr -d '"' | \
sed -r 's/^\s?\{?//' | \
sed -r 's/\s?\}?$//')
[ -z "$value" ] && echo "N/A" || echo "$value"
}
# web scraper for GeoIP
# timeout in 2 seconds and retry
geoip() {
local urls=( \
freegeoip.net/json \
www.telize.com/geoip \
ip.pycox.com/json \
ip-api.com/json \
)
if which curl &>/dev/null; then
while true; do
local url=${urls[$RANDOM % ${#urls[@]}]}
local json=$(curl -m2 -s -A $USER_AGENT $PROXY_OPTION "$url/$1")
if [ -n "$json" ]; then break; fi
done
local ccy=$(retrieve "$json" "(country_code|countryCode)")
if grep 'telize' <<<"$url" &>/dev/null; then
local stt=$(retrieve "$json" "region_code")
else
local stt=$(retrieve "$json" "region(_code)?")
fi
local cty=$(retrieve "$json" "city")
local zip=$(retrieve "$json" "(postal|zip)(_code)?")
local lat=$(retrieve "$json" "lat(itude)?")
local lon=$(retrieve "$json" "lon(gitude)?")
if [[ "$lat" != "N/A" ]]; then lat=$(printf "%.3f" "$lat"); fi
if [[ "$lon" != "N/A" ]]; then lon=$(printf "%.3f" "$lon"); fi
echo "$ccy|$stt|$cty|$zip|$lat|$lon"
else
depends "curl"
fi
}
# whois wrapper for IP to ASN - v4.whois.cymru.com
ip2asn() {
if which whois &>/dev/null; then
( IFS=' |' read asn ip name < <(whois -h v4.whois.cymru.com " -f $1")
asn=$(grep -E '(Error|NA)' <<<"$asn" &>/dev/null && \
echo "N/A" || echo "AS$asn")
name=$(grep -E '(line|NA)' <<<"$name" &>/dev/null && \
echo "N/A" || echo "$name")
name=${name#* }
name=${name/- /}
name=${name%,*}
echo "$asn|$name" )
else
depends "whois"
fi
}
# wrapper for geoiplookup - geolocation lookup tool
geolookup() {
if which geoiplookup &>/dev/null; then
geoiplookup $1 | awk ' \
BEGIN { RS=""; FS="\n"; OFS="|"; } \
{ split($3, a, ": "); \
$3=a[2]; \
gsub(", ", "|", $3); \
print $3 \
}'
else
geoip $1
fi
}
# wrapper for geoiplookup - geolocation lookup tool (ASN)
asnlookup() {
if which geoiplookup &>/dev/null; then
geoiplookup $1 | awk ' \
BEGIN { RS=""; FS="\n"; OFS="|"; } \
{ split($4, b, ": "); \
$4=b[2]; \
sub(" ", "|", $4); \
print $4 \
}'
else
ip2asn $1
fi
}
# bypass bgp.he.net nounce mechanism
nounce() {
if which curl &>/dev/null; then
local ua=Mozilla/5.0
local s=bgp.he.net
local path=$(curl -s -A $ua -H "Host: $s" -D - $s/ip/$1 | \
grep -Eo '.*--.*;' | \
sed -r 's/.*=(.*);/\1/' | \
sed 's/%/\/x/g')
local p=$(echo -ne $path | md5sum | cut -d' ' -f1)
local i=$(curl -s -A $ua -H "Host: $s" $s/i | md5sum | cut -d' ' -f1)
# oh yeah!
curl \
-s \
-XPOST \
-A $ua \
-H "Host: $s" \
-d "p=$p" -d "i=$i" \
-D - \
-b "path=$path" \
$s/jc | grep -Eo 'c=.*--.*;'
else
depends "curl"
fi
}
# web scraper for passive DNS - bgp.he.net
# timeout in 10 seconds
psvlookup() {
if which curl &>/dev/null; then
local url="bgp.he.net/ip/$1#_dns"
n=$(nounce $1)
PSV=$(curl -m10 -s -A "$USER_AGENT" "$PROXY_OPTION" -b $n $url | \
sed 's/>, </>\n</g' | \
sed -nr 's/^\s*<a href="\/dns.*>(.*)<\/a>/\1/p' | \
tr '\n' ';' | \
sed 's/;$//')
if [ -z "$PSV" ]; then echo "N/A"; else echo "$PSV"; fi
else
depends "curl"
fi
}
# stitch 'em up
stitch() {
RESULT=$1
if [ -n "$DNS" ]; then RESULT="$RESULT|$DNS"; fi
if [ -n "$BGP" ]; then RESULT="$RESULT|$BGP"; fi
if [ -n "$GEO" ]; then RESULT="$RESULT|$GEO"; fi
if [ -n "$ASN" ]; then RESULT="$RESULT|$ASN"; fi
echo "$RESULT"
}
# input validation
validate() {
# sanity check
if [ -z "$1" ]; then
die "missing argument: [ADDRESS]"
else
# quad dotted format check
if grep -E -v '^([0-9]+\.){3}[0-9]+$' <<<"$1" &>/dev/null; then
die "invalid IPv4 address: $1"
else
# ip value check
OLD_IFS=$IFS
IFS=. read a b c d <<<"$1"
if (( $a > 255 || $b > 255 || $c > 255 || $d > 255 )); then
die "invalid IPv4 address: $1"
fi
IFS=$OLD_IFS
fi
fi
}
# parse short options
OPTIND=1
while getopts ":df:hl:pv" opt; do
case $opt in
d)
if [[ "$LASTOPT" == "$opt" ]]; then
die "only one \`-$opt' may be specified"
fi
DNS_SERVER=${DNS_SERVERS[$RANDOM % ${#DNS_SERVERS[@]}]}
DNS_OPTION="$DNS_SERVER"
LASTOPT=$opt
;;
f)
if [[ "$LASTOPT" == "$opt" ]]; then
die "only one \`-$opt' may be specified"
fi
# validate ranges
if grep -E -v '^[0-9,-]+$' <<<"$OPTARG" &>/dev/null; then
die "invalid field list -- '$OPTARG'"
fi
FIELD=$OPTARG
LASTOPT=$opt
;;
h)
if [[ $# -ne 1 || $OPTIND -eq 1 ]]; then short; exit 1; fi
usage
exit 0
;;
l)
if [ $# -ne 1 ]; then short; exit 1; fi
# validate option argument
case $OPTARG in
d)
echo "# Public DNS servers"
echo "# $DNS_SOURCE"
tr ' ' '\n' <<<"${DNS_SERVERS[@]}"
exit 0
;;
p)
if proxify &>/dev/null; then
echo "# Open proxies"
echo "# ${HTTP}${PROXY_HOST}"
tr ' ' '\n' <<<"${PROXY_SERVERS[@]}"
exit 0
else
die "$PROXY_HOST (connection timeout)"
fi
;;
*)
die "invalid option argument -- '$OPTARG'"
;;
esac
;;
p)
if [[ "$LASTOPT" == "$opt" ]]; then
die "only one \`-$opt' may be specified"
fi
PROXY="on"
LASTOPT=$opt
;;
v)
if [[ $# -ne 1 || $OPTIND -eq 1 ]]; then short; exit 1; fi
echo "$PROG $VERSION"
echo "Written by $AUTHOR"
exit 0
;;
:)
die "option requires an argument -- '$OPTARG'"
;;
\?)
die "invalid option -- '$OPTARG'"
;;
esac
done
shift $((OPTIND - 1))
# check for empty arguments
if [[ -n "$LASTOPT" && $# -eq 0 ]]; then
validate; exit 1
elif [ $# -eq 0 ]; then
short; exit 1
fi
# main
for i in $*; do
input=$i; validate "$input"
if [ -n "$PROXY" ]; then
PROXY=$(proxify)
if [ -z "$PROXY" ]; then
PROXY_OPTION=
elif [ "$PROXY" = ":" ]; then
PROXY_OPTION=
elif [ "${PROXY#*:}" -eq 1080 ]; then
PROXY_OPTION="-x ${SOCKS}${PROXY}"
else
PROXY_OPTION="-x ${HTTP}${PROXY}"
fi
fi
if [ -z "$FIELD" ]; then
DNS=$(dnslookup "$input"); if [ $? -eq 1 ]; then exit 1; fi
BGP=$(psvlookup "$input"); if [ $? -eq 1 ]; then exit 1; fi
GEO=$(geolookup "$input"); if [ $? -eq 1 ]; then exit 1; fi
ASN=$(asnlookup "$input"); if [ $? -eq 1 ]; then exit 1; fi
stitch "$input"
else
SET=($(parse "$FIELD" "$MAX_FIELDS"))
# if field is in set; turn on
if grep -E '\b2\b' <<<"${SET[*]}" &>/dev/null; then DNS="on"; fi
if grep -E '\b3\b' <<<"${SET[*]}" &>/dev/null; then BGP="on"; fi
if grep -E '\b[4-9]\b' <<<"${SET[*]}" &>/dev/null; then GEO="on"; fi
if grep -E '\b(10|11)\b' <<<"${SET[*]}" &>/dev/null; then ASN="on"; fi
TWO=$(printf "N/A|%0.s" {1..2})
SIX=$(printf "N/A|%0.s" {1..6})
if [[ "$DNS" == "on" ]]; then
DNS=$(dnslookup "$input")
if [ $? -eq 1 ]; then exit 1; fi
else
DNS="N/A"
fi
if [[ "$BGP" == "on" ]]; then
BGP=$(psvlookup "$input")
if [ $? -eq 1 ]; then exit 1; fi
else
BGP="N/A"
fi
if [[ "$GEO" == "on" ]]; then
GEO=$(geolookup "$input")
if [ $? -eq 1 ]; then exit 1; fi
else
GEO="${SIX%|}"
fi
if [[ "$ASN" == "on" ]]; then
ASN=$(asnlookup "$input")
if [ $? -eq 1 ]; then exit 1; fi
else
ASN="${TWO%|}"
fi
stitch "$input" |
cut -d"|" -f"$FIELD" 2>/dev/null |
sed '/^$/d'
fi
done