-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipcheck.sh
1542 lines (1350 loc) · 44.4 KB
/
ipcheck.sh
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# IPcheck
xver='r2023-08-12 fr2020-09-12';
# by Valerio Capello - http://labs.geody.com/ - License: GPL v3.0
# Config
iptfw=0; # IP Tables framework: 0: Auto detect, 1: Standard, 2: nft (nftables, default and recommended since Debian 10 "Buster")
shwuserip=true; # Show User's IP
shwmachip=true; # Show Machine's IP and Default Gateway
nreshd=10; # Limit Results for Log Head
nrestl=$nreshd; # Limit Results for Log Tail
nrestop=20; # Limit Results for top results
sortfileswoquery=true; # Sort top accessed files excluding query strings in the URL
showerrreq=true; # Show top IPs and Files causing HTTP errors (for example: 404 Not Found)
shwlogstats=false; # Show Log Stats anyway, whether an IP is given or not
ipundott2dott=true; # If an undotted IP is passed, convert it to a dotted IP before to look for it into logs
whoisip=1; # Whois: 0: Disabled, 1: Selected Keywords, 2: Verbose (if enabled, it will connect to a whois service)
ipping=false; # Ping IP (setting it to true would expose your machine's IP and slow down the output)
iptrroute=false; # Trace Route to IP (setting it to true would expose your machine's IP and slow down the output)
rephstmn=400; # Min HTTP Status to report in detail (recommended: 400 )
rephstmx=599; # Max HTTP Status to report in detail (recommended: 599 )
enfileprintlist=false; # Show the content of the files in printlist
fileprintlist=('/etc/hosts' '/etc/hosts.allow' '/etc/hosts.deny'); # File Printlist. Example: fileprintlist=('/etc/hosts' '/etc/hosts.allow' '/etc/hosts.deny');
enfilewatchlist=true; # Show occurrences of the IP within the files in watchlist
filewatchlist=('/etc/hosts' '/etc/hosts.allow' '/etc/hosts.deny'); # File Watchlist. Example: filewatchlist=('/etc/hosts' '/etc/hosts.allow' '/etc/hosts.deny');
ipinloggedusersnow=true; # Show occurrences of the IP in currently logged users
ipinloggeduserslast=true; # Show occurrences of the IP in last logged users
shwloggedusersnow=false; # Show currently logged users
shwloggeduserslast=false; # Show last logged users
lastuserssince='yesterday'; # Consider last logged users since the specified time
ipinlogscurrent=true; # Show occurrences of the IP in all current logs
ipinlogsold=true; # Show occurrences of the IP in all old logs
logdir='/var/log/apache2/'; # Apache Logs Directory (only required to search the IP into all current and old logs)
logscurrentext='.log'; # Extension for current logs
logsoldext='.log.1'; # Extension for old logs
enipwatchlist=false; # Show occurrences of the IP in the watchlist
minoccenipwatchlist=1; # Minimum occurrences for an IP in the watchlist to report
ipwatchlist=(); # IP watchlist. Example: ipwatchlist=('192.0.2.1' '192.0.2.100' '192.0.2.101');
subnetcheck=true; # Count and List occurrences of IPs in the subnets of the IP into the given log (if this is set to False, no subnets will be checked even if enabled)
subnetchecken=(); subnetchecken[8]=false; subnetchecken[16]=true; subnetchecken[24]=true; # Count and List occurrences of IPs in the /8 /16 /24 subnets of the IP into the given log
subnetchecklist=(); subnetchecklist[8]=true; subnetchecklist[16]=true; subnetchecklist[24]=true; # Count and List occurrences of IPs in the /8 /16 /24 subnets of the IP into the given log
subnetchecklistlim=(); subnetchecklistlim[8]=256; subnetchecklistlim[16]=256; subnetchecklistlim[24]=256; # Set output limit for the list of IPs in the /8 /16 /24 subnets of the IP into the given log
f2benable=true; # Enable Fail2Ban checks (Seek IP in Fail2Ban banned IP lists)
f2bjaillist=true; # If Fail2Ban is enabled, list Fail2Ban Jails when checking webserver logs
ufwenable=true; # Enable UFW (Uncomplicated Firewall) checks (Seek IP in UFW status)
ufwlist=true; # If UFW is enabled, list ufw status (verbose)
# Functions
iptframeworkdetect() {
if [ $iptfw -eq 1 ]; then
iptfwx=''; iptfwtxt='standard';
elif [ $iptfw -eq 2 ]; then
iptfwx='-nft'; iptfwtxt='nftables';
else
local isthereiptnft=$( type -t iptables-nft );
if [ -n "$isthereiptnft" ]; then
iptfwx='-nft'; iptfwtxt='nftables (autodetected)';
else
iptfwx=''; iptfwtxt='standard (autodetected)';
fi
fi
}
apphdr() {
echo "IPcheck";
echo "by Valerio Capello - labs.geody.com - License: GPL v3.0";
}
# Requires apphdr, iptframeworkdetect
apphelp() {
apphdr; echo;
echo -n 'IPTables Framework: ';
iptframeworkdetect;
echo $iptfwtxt;
echo;
echo "Usage:";
echo "ipcheck LOG # Check Apache LOG";
echo "ipcheck IP # Check IP";
echo "ipcheck LOG IP # Check IP activity within Apache LOG";
echo "ipcheck LOG WEBPAGE # Check Apache LOG and activities about WEBPAGE";
echo "ipcheck LOG WEBPAGE IP # Check IP activity within Apache LOG and WEBPAGE";
echo "ipcheck --help # Display this help";
echo "ipcheck --version # Display version information";
echo "ipcheck --httpstatus CODE # Return HTTP Status for given CODE";
echo "ipcheck --salute # Display the IPs of the user and the machine";
echo "ipcheck --myip # Display the IP of the user";
echo "ipcheck --yourip # Display the IPs of the machine";
echo "ipcheck --listallapachelogs # List all Apache Logs";
if ( $f2benable ); then
echo "ipcheck --f2blist # List all Fail2Ban Jails";
echo "ipcheck --f2bstatus # Display status for all Fail2Ban Jails";
fi
if ( $ufwenable ); then
echo "ipcheck --ufwstatus # Display UFW status";
fi
echo;
echo "Examples:";
echo "ipcheck /var/log/apache2/access.log";
echo "ipcheck 192.0.2.100";
echo "ipcheck /var/log/apache2/access.log 192.0.2.100";
echo "ipcheck /var/log/apache2/access.log example.html";
echo "ipcheck /var/log/apache2/access.log example.html 192.0.2.100";
echo "ipcheck --httpstatus 404";
}
userip() {
local tshilon=''; local tshilof='';
echo -n "Hello "; echo -ne "$tshilon"; echo -n "$(whoami)"; echo -ne "$tshilof";
if [ "$SSH_CONNECTION" ]; then
echo -n " ("; echo -ne "$tshilon"; echo -n "$( echo $SSH_CLIENT | awk '{print $1}' )"; echo -ne "$tshilof)";
fi
echo "."
}
machip() {
local tshilon=''; local tshilof='';
echo -n "This is "; echo -ne "$tshilon"; echo -n "$( hostname )"; echo -ne "$tshilof";
echo -n " ("; echo -ne "$tshilon";
# echo -n "$( hostname -i )";
echo -n "$( ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p' )";
echo -ne "$tshilof";
echo -n "; Gateway: ";
echo -ne "$tshilon";
echo -n "$( route -n | grep 'UG[ \t]' | awk '{print $2}' )";
echo -ne "$tshilof";
echo -n ")";
echo ".";
}
# Requires userip, machip
salute() {
userip;
machip;
}
printhttplogip() {
local ipx="$1";
local logfx="$2";
local httpstcod="$3";
local nrestop="$4";
local stverb="";
if [ -n "$httpstcod" ]; then local stverb="($(httpstatcod2msg $httpstcod)) "; fi
echo; echo "Top requests by the IP causing a $httpstcod ${stverb}error:";
grep -i $ipx $logfx | grep " $httpstcod " | awk -F\" '$9=$httpstcod{print $2}' | sort | uniq -c | sort -rg | head --lines=$nrestop # | nl -n rn -s '. ' # Requests
# grep -i $ipx $logfx | grep " $httpstcod " | awk -F\" '$9=$httpstcod{print $9" "$2}' | awk {'print $7'} | sort | uniq -c | sort -rg | head --lines=$nrestop # | nl -n rn -s '. ' # Files
}
printhttplog() {
local logfx="$1";
local httpstcod="$2";
local nrestop="$3";
local stverb="";
if [ -n "$httpstcod" ]; then local stverb="($(httpstatcod2msg $httpstcod)) "; fi
echo; echo "Top requests causing a $httpstcod ${stverb}error:";
grep " $httpstcod " $logfx | awk -F\" '$9=$httpstcod{print $2}' | sort | uniq -c | sort -rg | head --lines=$nrestop # | nl -n rn -s '. ' # Requests
# grep " $httpstcod " $logfx | awk -F\" '$9=$httpstcod{print $9" "$2}' | awk {'print $7'} | sort | uniq -c | sort -rg | head --lines=$nrestop # | nl -n rn -s '. ' # Files
echo; echo "Top IPs causing a $httpstcod ${stverb}error:";
grep " $httpstcod " $logfx | awk '$9=$httpstcod{print $1}' | sort | uniq -c | sort -rg | head --lines=$nrestop # | nl -n rn -s '. '
}
isipv4dot() {
local ip=${1:-1.2.3.4}
if expr "$ip" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
IFS=.
set $ip
for quad in 1 2 3 4; do
if eval [ \$$quad -gt 255 ]; then
# echo "fail ($ip)"
return 1
fi
done
# echo "success ($ip)"
return 0
else
# echo "fail ($ip)"
return 1
fi
}
isipv4und() {
local ip="$1";
if [[ $ip =~ ^[0-9]+$ ]] && [ $ip -ge 0 ] && [ $ip -le 4294967295 ]; then
# echo "success ($ip)"
return 0
else
# echo "fail ($ip)"
return 1
fi
}
isipv6() {
local ip="$1";
local pattipv6='^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$';
if [[ "$ip" =~ $pattipv6 ]]; then
# if (echo "$ip" | grep -Eq $regex); then # POSIX
# echo "success ($ip)"
return 0
else
# echo "fail ($ip)"
return 1
fi
}
# Requires isipv4dot, isipv4und, isipv6
isip() {
if isipv4dot "$1"; then
echo -n '4';
return 0
elif isipv4und "$1"; then
echo -n '5';
return 0
elif isipv6 "$1"; then
echo -n '6';
return 0
else
echo -n '0';
return 1
fi
}
ipdot2undot() {
if [ "$#" -le 0 ]; then return 1; fi
local a b c d ip=$@
IFS=. read -r a b c d <<< "$ip"
printf '%d' "$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d))"
}
ipundot2dot() {
if [ "$#" -le 0 ]; then return 1; fi
local ip dec=$@
for e in {3..0}
do
((octet = dec / (256 ** e) ))
((dec -= octet * 256 ** e))
ip+=$delim$octet
delim=.
done
printf '%s' "$ip"
}
dec2bin() {
local n="$1"
local zf="$2"
if [ -z "$zf" ] || [ $zf -le 0 ]; then
local zf=0
fi
local bit=""
while [ "$n" -gt 0 ]; do
local bit="$(( n&1 ))$bit";
: $(( n >>= 1 ))
done
if [ $zf -le 0 ]; then
printf "%s" "$bit"
else
printf "%0${zf}d" "$bit"
fi
}
# Requires dec2bin
ipdot2bin() {
local zf=8;
if [ "$#" -le 0 ]; then return 1; fi
local a b c d ip=$@
IFS=. read -r a b c d <<< "$ip"
echo -n "$(dec2bin $a $zf).$(dec2bin $b $zf).$(dec2bin $c $zf).$(dec2bin $d $zf)";
}
isfile() {
if [[ -d $1 ]]; then
echo -n '2';
return 0
elif [[ -f $1 ]]; then
echo -n '1';
return 0
else
echo -n '0';
return 1
fi
}
function hrtime {
local T=$1
local D=$((T/60/60/24))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
(( $D > 0 )) && printf '%d days ' $D
(( $H > 0 )) && printf '%d hours ' $H
(( $M > 0 )) && printf '%d minutes ' $M
(( $D > 0 || $H > 0 || $M > 0 )) && printf ''
printf '%d seconds' $S
}
# Requires isip (isipv4dot, isipv4und, isipv6), isipknown
ipinfo() {
local ipx="$1";
echo "Checking IP: $ipx";
# echo
local ipv=$(isip "$ipx")
echo -n "Format: ";
case $ipv in
4)
echo 'Valid IPv4 dotted';
;;
5)
echo 'Valid IPv4 undotted';
;;
6)
echo 'Valid IPv6';
;;
*)
echo 'NOT a valid IP';
;;
esac
if [ $ipv -eq 4 ] || [ $ipv -eq 5 ]; then
if [ $ipv -eq 4 ]; then
ipdott="$ipx";
ipundott="$(ipdot2undot $ipx)";
echo "Undotted IP: $ipundott";
fi
if [ $ipv -eq 5 ]; then
ipundott="$ipx";
ipdott="$(ipundot2dot $ipx)";
echo "Dotted IP: $ipdott";
fi
echo -n "Hex: "; printf '%02X' ${ipdott//./ } | sed 's/.\{2\}/&\./g' | awk '{sub(/\.$/, "")};1'
echo -n "Bin: "; ipdot2bin $ipdott ; echo
if [ $ipundott -eq 0 ]; then
echo 'Scope: Local (Private Network)'; echo 'Class: A'; echo 'Function: Routing ("All Other Network Addresses" in routing tables)';
elif [ $ipundott -eq 2130706433 ]; then
echo 'Scope: Local (Private Network)'; echo 'Class: A'; echo 'Function: Localhost';
elif [ $ipundott -ge 2130706432 ] && [ $ipundott -le 2147483647 ]; then
echo 'Scope: Local (Private Network)'; echo 'Class: A'; echo 'Function: Loopback';
elif [ $ipundott -ge 2851995648 ] && [ $ipundott -le 2852061183 ]; then
echo 'Scope: Local (Private Network)'; echo 'Class: B'; echo 'Function: Autoconfiguration (APIPA, typically because of DHCP failure)';
elif [ $ipundott -ge 3221225984 ] && [ $ipundott -le 3221226239 ]; then
echo 'Class: C'; echo 'Function: Example';
elif [ $ipundott -ge 3758096384 ] && [ $ipundott -le 4026531839 ]; then
echo 'Class: D'; echo -n 'Function: Multicast';
if [ $ipundott -ge 3758096384 ] && [ $ipundott -le 3758096639 ]; then
echo ' / Local subnetwork / Not Routable';
elif [ $ipundott -ge 3758096640 ] && [ $ipundott -le 3758096895 ]; then
echo ' / Internetwork control / Routable';
elif [ $ipundott -ge 3758096896 ] && [ $ipundott -le 3758161919 ]; then
echo ' / AD-HOC block 1 / Routable';
elif [ $ipundott -ge 3758161920 ] && [ $ipundott -le 3758424063 ]; then
echo ' / AD-HOC block 2 / Routable';
elif [ $ipundott -ge 3758424064 ] && [ $ipundott -le 3909091327 ]; then
echo ' / Source-specific multicast / Routable';
elif [ $ipundott -ge 3909091328 ] && [ $ipundott -le 3925606399 ]; then
echo ' / GLOP addressing / Routable';
elif [ $ipundott -ge 3925606400 ] && [ $ipundott -le 3925868543 ]; then
echo ' / AD-HOC block 3 / Routable';
elif [ $ipundott -ge 3925868544 ] && [ $ipundott -le 3942645759 ]; then
echo ' / Unicast-prefix-based / Routable';
elif [ $ipundott -ge 3942645760 ] && [ $ipundott -le 4026531839 ]; then
echo ' / Administratively scoped / Routable';
else
echo; # Can't happen
fi
elif [ $ipundott -eq 4294967295 ]; then
echo 'Class: E'; echo 'Function: Broadcast';
elif [ $ipundott -ge 167772160 ] && [ $ipundott -le 184549375 ]; then
echo 'Scope: Local (Private Network)'; echo 'Class: A';
elif [ $ipundott -ge 2886729728 ] && [ $ipundott -le 2887778303 ]; then
echo 'Scope: Local (Private Network)'; echo 'Class: B';
elif [ $ipundott -ge 3232235520 ] && [ $ipundott -le 3232301055 ]; then
echo 'Scope: Local (Private Network)'; echo 'Class: C';
elif [ $ipundott -ge 0 ] && [ $ipundott -le 2130706431 ]; then
echo 'Scope: Global (Public Network)'; echo 'Class: A';
elif [ $ipundott -ge 2147483648 ] && [ $ipundott -le 3221225471 ]; then
echo 'Scope: Global (Public Network)'; echo 'Class: B';
elif [ $ipundott -ge 3221225472 ] && [ $ipundott -le 3758096383 ]; then
echo 'Scope: Global (Public Network)'; echo 'Class: C';
elif [ $ipundott -ge 3758096384 ] && [ $ipundott -le 4026531839 ]; then
echo 'Scope: Global (Public Network)'; echo 'Class: D';
elif [ $ipundott -ge 4026531840 ] && [ $ipundott -le 4294967295 ]; then
echo 'Scope: Global (Public Network)'; echo 'Class: E';
fi
if [ $ipv -eq 5 ] && ( $ipundott2dott ); then
ipx="$(ipundot2dot $ipx)"; ipv=4;
fi
fi
echo;
isipknown $ipx
}
isipknown() {
local ipx="$1";
if [[ "$ipx" == "$( echo -n $SSH_CLIENT | awk '{print $1}' | tr -d '\n' )" ]]; then
echo 'This IP is YOUR IP.'; echo;
elif [[ "$ipx" == "$( hostname -i | tr -d '\n' )" ]]; then
echo "This IP is this machine's local IP."; echo;
elif [[ "$ipx" == "$( ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p' | tr -d '\n' )" ]]; then
echo "This IP is this machine's global IP."; echo;
elif [[ "$ipx" == "$( route -n | grep 'UG[ \t]' | awk '{print $2}' | tr -d '\n' )" ]]; then
echo "This IP is this machine's default gateway."; echo;
fi
}
# Requires f2blist, ufwstatus
sysstats() {
iptcmd="iptables${iptfwx}"; ipttxt='IPTables';
echo "$ipttxt:"
ipttotrulinp=$( $iptcmd -L INPUT -n | wc -l ); ipttotrulinp=$((ipttotrulinp-2));
if [ $ipttotrulinp -gt 0 ]; then
if [ $ipttotrulinp -eq 1 ]; then
echo "$ipttotrulinp INPUT rule found in $ipttxt.";
else
echo "$ipttotrulinp INPUT rules found in $ipttxt.";
fi
else
echo "No INPUT rules found in $ipttxt.";
fi
iptcmd="ip6tables${iptfwx}"; ipttxt='IP6Tables';
echo "$ipttxt:"
ipttotrulinp=$( $iptcmd -L INPUT -n | wc -l ); ipttotrulinp=$((ipttotrulinp-2));
if [ $ipttotrulinp -gt 0 ]; then
if [ $ipttotrulinp -eq 1 ]; then
echo "$ipttotrulinp INPUT rule found in $ipttxt.";
else
echo "$ipttotrulinp INPUT rules found in $ipttxt.";
fi
else
echo "No INPUT rules found in $ipttxt.";
fi
if ( $f2benable ); then
echo; f2blist;
fi
if ( $ufwenable ); then
echo; ufwstatus;
fi
if ( $shwloggedusersnow ) || ( $shwloggeduserslast ); then
echo; echo "Logged Users:"
if ( $shwloggedusersnow ); then
echo "Currently logged users:"
who -s --ips
fi
if ( $shwloggeduserslast ) && [ -n "$lastuserssince" ]; then
echo "Last logged users (since $lastuserssince):"
last -i --since yesterday | head --lines=-2
fi
fi
if ( $enfileprintlist ); then
echo; echo "Showing content of Files in Printlist:"
if [ ${#fileprintlist[@]} -gt 0 ]; then
for filei in "${fileprintlist[@]}"
do
if [ -n "$filei" ]; then
# fileish="$( basename $filei )";
echo "Showing $filei:"
filex=$(isfile "$filei")
case $filex in
1)
cat "$filei"; # | more ;
echo;
;;
2) echo "$filei is a directory, not a file." ;;
*) echo "$filei not found." ;;
esac
fi
done
else
echo "The File Printlist is empty."
fi
fi
if ( $enipwatchlist ); then
echo; echo "IPs in Watchlist:"
if [ ${#ipwatchlist[@]} -gt 0 ]; then
for ipel in "${ipwatchlist[@]}"; do
ipocc=$( grep $ipel $logfx | wc -l );
if [ $ipocc -ge $minoccenipwatchlist ]; then
if [ $ipocc -eq 1 ]; then
echo "IP $ipel : $ipocc occurrence found in the Apache Log File.";
else
echo "IP $ipel : $ipocc occurrences found in the Apache Log File.";
fi
fi
done
else
echo "The Watchlist is empty."
fi
fi
if [ -n "$istherenetstat" ]; then
echo; echo "Top IPs accessing the system now:"
conntot=$( netstat -an | wc -l )
if [ $conntot -gt 0 ]; then
connest=$( netstat -an | grep 'ESTABLISHED' | wc -l )
netstat -an | grep 'ESTABLISHED' | awk '{print $5}' | grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" | grep -E -v "(`for i in \`ip addr | grep inet | grep eth0 | cut -d/ -f1 | awk '{print $2}'\`;do echo -n "$i|"| sed 's/\./\\\./g;';done`127\.|0\.0\.0)" | sort -n | uniq -c | sort -rn | head --lines=$nrestop # | nl -n rn -s '. '
if [ $connest -gt 1 ]; then echo "$connest established connections."; else echo "$connest established connection."; fi
if [ $conntot -gt 1 ]; then echo "$conntot total connections."; else echo "$conntot total connection."; fi
else
echo "No active connections.";
fi
fi
}
# $1=true|false; # Show Memory Usage in Human Readable Format
shwmem() {
if ( $1 ); then
free -h | xargs | awk '{print "Memory: Size: "$8" Used: "$9" Free: "$10" Avail: "$13}';
swapon --show --noheadings --raw | xargs | awk '{print "Swap File: Dev: "$1" Total: "$3" Used: "$4}';
else
free;
fi
}
# Requires shwmem
sysinfo() {
shwmem true;
df / -Th | xargs | awk '{print "Disk FS: "$9" Type: "$10" Size: "$11" Used: "$12" ("$14") Avail: "$13" ("(100-$14)"%)"}';
echo -n "CPU average load (Cores: "; grep -c 'processor' /proc/cpuinfo | tr -d '\n'; echo -n "): "; uptime | awk -F'[a-z]:' '{print $2}' | xargs | awk '{print "1 m: "$1" 5 m: "$2" 15 m: "$3}';
}
f2blist() {
echo "Fail2Ban:"
local iptcmd="iptables${iptfwx}"; local ipttxt='IPTables';
ipttotrulf2b=$( $iptcmd -S | grep "\-A f2b\-" | wc -l ); # ipttotrulf2b=$((ipttotrulf2b-2));
if [ $ipttotrulf2b -gt 0 ]; then
echo "$ipttotrulf2b Fail2Ban rules found in $ipttxt.";
else
echo "No Fail2Ban rules found in $ipttxt.";
fi
local iptcmd="ip6tables${iptfwx}"; local ipttxt='IP6Tables';
ipttotrulf2b=$( $iptcmd -S | grep "\-A f2b\-" | wc -l ); # ipttotrulf2b=$((ipttotrulf2b-2));
if [ $ipttotrulf2b -gt 0 ]; then
echo "$ipttotrulf2b Fail2Ban rules found in $ipttxt.";
else
echo "No Fail2Ban rules found in $ipttxt.";
fi
if [ -n "$istheref2b" ]; then
if ( $f2bjaillist ); then
echo; echo "Fail2Ban Jails:"
jails=$(fail2ban-client status | grep -m 1 'Jail list' | sed -E 's/^[^:]+:[ \t]+//' | sed 's/,//g')
jailsnum=0; jailsthislognum=0;
for jail in $jails
do
jailsnum=$((jailsnum+1));
echo -n $jail
jailstatus=$(fail2ban-client status $jail)
filelist=$( echo -n "$jailstatus" | grep -m 1 'File list:' | awk {'print $5'} );
if [[ "$filelist" == "$logfx" ]]; then
jailsthislognum=$((jailsthislognum+1));
echo " for this Log";
else
echo " for $filelist";
fi
done
else
jailsnum=$( fail2ban-client status | grep -m 1 'Number of jail:' | awk {'print $5'} );
fi
if [ $jailsnum -eq 1 ]; then
echo "$jailsnum Fail2Ban Jail found.";
elif [ $jailsnum -gt 1 ]; then
echo "$jailsnum Fail2Ban Jails found.";
else
echo "No Fail2Ban Jails found.";
fi
if [ -n "$logfx" ]; then
if [ $jailsthislognum -eq 1 ]; then
echo "$jailsthislognum Fail2Ban Jail for this Log found.";
elif [ $jailsthislognum -gt 1 ]; then
echo "$jailsthislognum Fail2Ban Jails for this Log found.";
else
echo "No Fail2Ban Jails for this Log found.";
fi
fi
else
echo "Fail2Ban is not present.";
fi
}
ufwstatus() {
echo "UFW (Uncomplicated Firewall):"
ipttotrulufwusrinp=$( iptables -L ufw-user-input -n | wc -l ); ipttotrulufwusrinp=$((ipttotrulufwusrinp-2));
if [ $ipttotrulufwusrinp -gt 0 ]; then
if [ $ipttotrulufwusrinp -eq 1 ]; then
echo "$ipttotrulufwusrinp UFW user input rule found in IPTables.";
else
echo "$ipttotrulufwusrinp UFW user input rules found in IPTables.";
fi
else
echo "No UFW user input rules in IPTables.";
fi
if [ -n "$isthereufw" ]; then
if ( $isufwon ); then
if ( $ufwlist ); then
ufw version ; ufw status verbose | head --lines=4 ; ufw status numbered | grep -v -m 1 'Status' ;
else
echo "UFW is present and enabled.";
ufw version ;
fi
else
echo "UFW is present but disabled.";
ufw version ; ufw status ;
fi
else
echo "UFW is not present.";
fi
}
httpstatcod2msg() {
# local statuscod="$1"
local statuscat=""
local statusmsg=""
case ${1:0:1} in
1) local statuscat="Informational"
case $1 in
100) local statusmsg="Continue" ;;
101) local statusmsg="Switching Protocols" ;;
102) local statusmsg="Processing" ;;
103) local statusmsg="Early Hints" ;;
*) local statusmsg="Unknown" ;;
esac
;;
2) local statuscat="Success"
case $1 in
200) local statusmsg="OK" ;;
201) local statusmsg="Created" ;;
202) local statusmsg="Accepted" ;;
203) local statusmsg="Non-Authoritative Information" ;;
204) local statusmsg="No Content" ;;
205) local statusmsg="Reset Content" ;;
206) local statusmsg="Partial Content" ;;
207) local statusmsg="Multi-Status" ;;
208) local statusmsg="Already Reported" ;;
226) local statusmsg="IM Used" ;;
*) local statusmsg="Unknown" ;;
esac
;;
3) local statuscat="Redirection"
case $1 in
300) local statusmsg="Multiple Choices" ;;
301) local statusmsg="Moved Permanently" ;;
302) local statusmsg="Found" ;;
303) local statusmsg="See Other" ;;
304) local statusmsg="Not Modified" ;;
305) local statusmsg="Use Proxy" ;;
306) local statusmsg="Switch Proxy" ;;
307) local statusmsg="Temporary Redirect" ;;
308) local statusmsg="Permanent Redirect" ;;
*) local statusmsg="Unknown" ;;
esac
;;
4) local statuscat="Client Error"
case $1 in
400) local statusmsg="Bad Request" ;;
401) local statusmsg="Unauthorized" ;;
402) local statusmsg="Payment Required" ;;
403) local statusmsg="Forbidden" ;;
404) local statusmsg="Not Found" ;;
405) local statusmsg="Method Not Allowed" ;;
406) local statusmsg="Not Acceptable" ;;
407) local statusmsg="Proxy Authentication Required" ;;
408) local statusmsg="Request Timeout" ;;
409) local statusmsg="Conflict" ;;
410) local statusmsg="Gone" ;;
411) local statusmsg="Length Required" ;;
412) local statusmsg="Precondition Failed" ;;
413) local statusmsg="Request Entity Too Large" ;;
414) local statusmsg="Request-URI Too Long" ;;
415) local statusmsg="Unsupported Media Type" ;;
416) local statusmsg="Requested Range Not Satisfiable" ;;
417) local statusmsg="Expectation Failed" ;;
418) local statusmsg="I'm a teapot" ;;
421) local statusmsg="Misdirected Request" ;;
422) local statusmsg="Unprocessable Entity" ;;
423) local statusmsg="Locked" ;;
424) local statusmsg="Failed Dependency" ;;
425) local statusmsg="Too Early" ;;
426) local statusmsg="Upgrade Required" ;;
428) local statusmsg="Precondition Required" ;;
429) local statusmsg="Too Many Requests" ;;
431) local statusmsg="Request Header Fields Too Large" ;;
451) local statusmsg="Unavailable For Legal Reasons" ;;
*) local statusmsg="Unknown" ;;
esac
;;
5) local statuscat="Server Error"
case $1 in
500) local statusmsg="Internal Server Error" ;;
501) local statusmsg="Not Implemented" ;;
502) local statusmsg="Bad Gateway" ;;
503) local statusmsg="Service Unavailable" ;;
504) local statusmsg="Gateway Timeout" ;;
505) local statusmsg="HTTP Version Not Supported" ;;
506) local statusmsg="Variant Also Negotiates" ;;
507) local statusmsg="Insufficient Storage" ;;
508) local statusmsg="Loop Detected" ;;
510) local statusmsg="Not Extended" ;;
511) local statusmsg="Network Authentication Required" ;;
*) local statusmsg="Unknown" ;;
esac
;;
*) local statuscat="Other"
local statusmsg="Unknown"
;;
esac
echo -n "$statuscat: $statusmsg"
}
# Get Parameters
# Add a trailing slash to log's path if missing
if [ ${#logdir} -gt 0 ]; then
if [[ "${logdir: -1}" != '/' ]]; then logdir="${logdir}/"; fi
fi
istheref2b=$( type -t fail2ban-client );
isthereufw=$( type -t ufw );
if [ -n "$isthereufw" ]; then
ufwstatus=$( ufw status | head --lines=1 | tr -d '\n' );
if [[ "$ufwstatus" == 'Status: active' ]]; then isufwon=true; else isufwon=false; fi
else
isufwon=false;
fi
istherenetstat=$( type -t netstat );
action=$( echo "$1" | tr '[:upper:]' '[:lower:]' );
case $action in
'--help')
apphelp
exit 0;
;;
'--ver'|'--version')
apphdr; echo
echo "Version: $xver";
exit 0;
;;
'--httpstatus')
apphdr; echo
httpstcod=$2;
if [ $# -ge 2 ]; then
if [[ $httpstcod =~ ^[0-9]+$ ]] && [ $httpstcod -ge 100 ] && [ $httpstcod -lt 600 ]; then
stverb="($(httpstatcod2msg $httpstcod))";
if [ -n "$stverb" ]; then
echo "HTTP Status: $httpstcod $stverb"
else
echo "$httpstcod is not a valid HTTP status code";
fi
else
echo "$httpstcod is not a valid HTTP status code";
fi
else
echo 'Missing HTTP status code';
exit 1;
fi
exit 0;
;;
'--salute'|'--ourip')
apphdr; echo
salute;
exit 0;
;;
'--myip'|'--userip')
apphdr; echo
userip;
exit 0;
;;
'--yourip'|'--machip'|'--machineip')
apphdr; echo
machip;
exit 0;
;;
'--listallapachelogs'|'--listalllogs')
if [ -n "$logdir" ]; then
apphdr; echo
echo "All Apache Logs:"
filex=$(isfile "$logdir")
case $filex in
1)
echo "$logdir is a file, not a directory." ;;
2)
echo "Current Apache Logs:"
ls -aF $logdir*$logscurrentext
totlogapacur=$( ls -aF $logdir*$logscurrentext | wc -l );
echo "Total Current Apache Logs: $totlogapacur";
echo "Old Apache Logs:"
ls -aF $logdir*$logsoldext
totlogapaold=$( ls -aF $logdir*$logscurrentext | wc -l );
echo "Total Old Apache Logs: $totlogapaold";
echo "Total Apache Logs: $((totlogapacur+totlogapaold))";
;;
*) echo "$logdir not found." ;;
esac
else
echo "Log Path not provided (empty).";
fi
exit 0;
;;
'--f2blist')
apphdr; echo
if ( ! $f2benable ); then echo "Fail2Ban is disabled in IPcheck configuration."; exit 1; fi
f2blist;
exit 0;
;;
'--f2bstatus'|'--f2bstatusall')
apphdr; echo
if ( ! $f2benable ); then echo "Fail2Ban is disabled in IPcheck configuration."; exit 1; fi
if [ -n "$istheref2b" ]; then
echo "fail2ban-client status --all"; echo;
fail2ban-client --version
echo
jails=$(fail2ban-client status | grep "Jail list" | sed -E 's/^[^:]+:[ \t]+//' | sed 's/,//g')
jailsnum=0;
for jail in $jails
do
jailsnum=$((jailsnum+1));
fail2ban-client status $jail
echo
done
if [ $jailsnum -eq 1 ]; then
echo "$jailsnum Fail2Ban Jail found.";
elif [ $jailsnum -gt 1 ]; then
echo "$jailsnum Fail2Ban Jails found.";
else
echo "No Fail2Ban Jails found.";
fi
echo
# f2bdbsize=$( du -bs '/var/lib/fail2ban/' | awk '{print $1}' | tr -d '\n' );
f2bdbsize=$( du -bsc /var/lib/fail2ban/* | tail --lines=1 | awk '{print $1}' | tr -d '\n' );
f2blgsize=$( du -bsc /var/log/fail2ban.* | tail --lines=1 | awk '{print $1}' | tr -d '\n' );
f2btotsize=( $f2bdbsize + $f2blgsize );
echo "Fail2Ban Database size is $f2bdbsize bytes.";
echo "Fail2Ban Logs size is $f2blgsize bytes.";
echo "Fail2Ban total data size is $f2btotsize bytes.";
exit 1;
else
echo "Fail2Ban is not present.";
exit 1;
fi
;;
'--ufwstatus'|'--ufwlist')
apphdr; echo
if ( ! $ufwenable ); then echo "UFW is disabled in IPcheck configuration."; exit 1; fi
ufw version ; ufw status verbose | head --lines=4 ; ufw status numbered | grep -v 'Status' ;
exit 0;
;;
esac
if [ $# -eq 0 ]; then
apphdr; echo
sysinfo;
echo;
sysstats;
exit 1;
elif [ $# -eq 1 ]; then
ipv=$(isip "$1")
if [ $ipv -eq 4 ] || [ $ipv -eq 5 ] || [ $ipv -eq 6 ]; then
logf=""; # Apache Log File
pagx=""; # Target Page
ipx=$1; # Target IP
else
logf=$1; # Apache Log File
pagx=""; # Target Page
ipx=""; # Target IP
fi
elif [ $# -eq 2 ]; then
ipv=$(isip "$2")
if [ $ipv -eq 4 ] || [ $ipv -eq 5 ] || [ $ipv -eq 6 ]; then
logf=$1; # Apache Log File
pagx=""; # Target Page
ipx=$2; # Target IP
else
logf=$1; # Apache Log File
pagx=$2; # Target Page
ipx=""; # Target IP
fi
elif [ $# -eq 3 ]; then
logf=$1; # Apache Log File
pagx=$2; # Target Page
ipx=$3; # Target IP
else
apphelp
exit 1;
fi
# Log Presets
if [ -n "$logf" ]; then
case $logf in
apache | apache2 | "apache 2" | standard)
logfx="/var/log/apache2/access.log";
;;
*)
logfx=$logf;
;;
esac
fi
# Main
apphdr; echo;
iptframeworkdetect;
if ( $shwuserip ) || ( $shwmachip ); then
if ( $shwuserip ); then
userip;
fi
if ( $shwmachip ); then
machip;
fi
echo;
fi
if [ $( fail2ban-client status | wc -l ) -ge 3 ]; then
isf2bon=true;
else
isf2bon=false;
fi
if [ -n "$ipx" ]; then
ipv=$(isip "$ipx")
ipinfo $ipx
if [ $ipv -eq 6 ]; then
iptcmd="ip6tables${iptfwx}"; ipttxt='IP6Tables';
else
iptcmd="iptables${iptfwx}"; ipttxt='IPTables';
fi
if [ $ipv -eq 5 ]; then
ipx="$(ipundot2dot $ipx)"; ipv=4;
fi
echo "Host: "
host $ipx
if [ $whoisip -ge 1 ]; then
echo; echo "Who is:"
case $whoisip in
1) whois $ipx | grep -iE "^\s*netrange:|^\s*netblock:|^\s*cidr:|^\s*inetnum:|^\s*route:|^\s*\s*netname:|^\s*orgname:|^\s*organisation:|^\s*organization:|^\s*org-name:|^\s*custname:|^\s*owner:|^\s*country:|^\s*orgabuseemail:|^\s*e-mail:|^\s*descr:|^\s*remarks:" ;;
2) whois $ipx ;;
esac
fi
if ( $ipping ); then
echo; ping -c 3 $ipx
fi
if ( $iptrroute ); then
if ( ! $ipping ); then echo; fi
traceroute $ipx
fi
echo; echo "IP in $ipttxt:"
ipxintablesn=$($iptcmd -L INPUT -n --line-numbers | grep $ipx | wc -l );
if [ $ipxintablesn -eq 0 ]; then
echo "No matches found for the IP in $ipttxt INPUT rules.";
else
$iptcmd -L INPUT -n --line-numbers | grep $ipx