-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxiwall
More file actions
1458 lines (1243 loc) · 42.4 KB
/
maxiwall
File metadata and controls
1458 lines (1243 loc) · 42.4 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
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
# ==================================================
_APP_SPECIFIC_NAME="Maxiwall"
_APP_VERSION="0.6"
_APP_STATUS="beta"
_APP_INFO="${_APP_SPECIFIC_NAME} (maxiwall) is bash script to manage CSF, AbuseIPDB and Suricata
with extra features. This script is also a wrapper to manage suricata .lua report with IPS capability"
_APP_VERSION_STATUS="${_APP_VERSION}-${_APP_STATUS}"
_AUTHOR="Author: Arafat Ali | Email: arafat@sofibox.com | (C) 2019-2023"
# ====================================================
# This function is used to handle exit trap that can accept multiple trap arguments
# syntax: traps <traps_cleanup_function> SIG1 SIG2 SIG3 ... SIGN[N]
# eg: traps exit_function QUIT INT TERM EXIT
traps() {
local clean_function
clean_function="$1"
shift
for sig; do
trap "${clean_function} ${sig}" "${sig}"
done
}
maxiwall_debug_exit() {
local retval caller
caller="$(basename -- "$0")->${FUNCNAME[0]}"
((CTRL_C_COUNT++))
if [[ $CTRL_C_COUNT == 1 ]]; then
local signal
signal="$1"
echo ""
if [ "${signal}" == "INT" ]; then
echo "[${caller}]: *** Suricata debug mode has been terminated by ${USER}! ***"
echo ""
fi
fi
# Restore suricata.yaml
echo "[${caller}]: Restoring suricata.yaml ..."
yq eval --inplace '.rule-files = ["suricata.rules", "/usr/local/maxicode/maxiwall/maxiwall.rules"]' "${SURICATA_YAML}"
retval="$?"
if [[ "${retval}" -eq 0 ]]; then
echo "[${caller}]: suricata.yaml restored successfully."
else
echo "[${caller}]: Error, suricata.yaml restore failed."
fi
echo ""
# Starting suricata
echo "[${caller}]: Starting suricata.service ..."
systemctl restart suricata
retval="$?"
if [[ "${retval}" -eq 0 ]]; then
echo "[${caller}]: suricata.service started successfully."
else
echo "[${caller}]: Error, suricata.service start failed."
fi
echo ""
echo " **END DEBUG** "
echo "=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~"
exit
}
# This function is used by traps() function to clean exit
exit_script() {
((CTRL_C_COUNT++))
if [[ ${CTRL_C_COUNT} == 1 ]]; then
local signal
signal="$1"
if [ "${signal}" == "INT" ]; then
echo "*** Warning, this script has been terminated by user: ${USER}!***"
fi
fi
# clean exit
exit
}
# This function is used to display the main help message from readme file.
# Usage: readme [file]
# Example: readme /docs/README.md
readme() {
local readme_file="${1}"
if [ -f "${readme_file}" ]; then
cat "${readme_file}"
echo ""
else
echo "Error, the readme file ${readme_file} does not exist."
exit 1
fi
}
# This function is used to convert an integer to ordinal number
# Usage: ordinal [number]
# Example: ordinal 1
# Output: 1st
to_ordinal() {
local integer
integer="$1"
case "${integer}" in
*1[0-9] | *[04-9]) echo "${integer}"th ;;
*1) echo "${integer}"st ;;
*2) echo "${integer}"nd ;;
*3) echo "${integer}"rd ;;
esac
}
# This function gets the operating system ID, version number and codename
# syntax: get_distro <id|version|codename>
# eg: get_distro id | sample output: centos
get_distro() {
local arg distro_id distro_version distro_codename
arg="$1"
# any new Linux distribution must have this (it is a standard file os checking for linux)
if [ -e /etc/os-release ]; then
# get the distro ID from /etc/os-release and make sure it is in lowercase format without any double quotations
distro_id=$(awk -F= '$1 == "ID" {print $2}' /etc/os-release | LC_ALL=C tr '[:upper:]' '[:lower:]' | tr -d "\"")
distro_version=$(awk -F= '$1 == "VERSION_ID" {print $2}' /etc/os-release | tr -d "\"")
distro_codename=$(awk -F= '$1 == "VERSION_CODENAME" {print $2}' /etc/os-release | tr -d "\"")
# but just in case (a small case) if it doesn't have this then we can use lsb_release command
elif type lsb_release >/dev/null 2>&1; then
# get the distro id using lsb_release function.
# if the ID has uppercase letter, cover it to lowercase
distro_id=$(lsb_release -si | LC_ALL=C tr '[:upper:]' '[:lower:]')
distro_version=$(lsb_release -sr)
distro_codename=$(lsb_release -sc)
# else if that doesn't exist at all, we can use the standard checking version format: "Linux <version>"
else
# this will not printout a single distribution ID that we want (it prints out the Generic Linux kernel version)
# but using this we should terminate this script because we only need single distribution ID (eg: debian)
distro_id=$(uname -s)
distro_version=$(uname -r)
distro_codename=""
fi
# return values here with echo
if [ "${arg}" == "id" ]; then
echo "${distro_id}"
elif [ "${arg}" == "version" ]; then
echo "${distro_version}"
elif [ "${arg}" == "codename" ]; then
echo "${distro_codename}"
fi
}
# This function is used to navigate to a given path and save the previous path to a variable
# You can use PREVIOUS to go back to the previous path
# Usage: navigate_to [path]
# Example: navigate_to /opt
# Example 2: navigate_to PREVIOUS
navigate_to() {
local to_path caller
to_path="$1"
caller="$(basename -- "$0")->${FUNCNAME[0]}"
if [[ "${to_path^^}" == +(PRE|PREVIOUS|OLDPATH|BACK|OLDPWD) ]]; then
# OLD_PATH holds a previous path (see global variable of PREPATH in this script)
echo "[${caller}]: Navigating out into ${OLD_PATH} ..."
to_path="${OLD_PATH}"
else
echo "[${caller}]: Navigating into ${to_path} ..."
OLD_PATH="${PWD}"
fi
cd "${to_path}" || {
echo "[${caller}]: Error, failed to enter into the directory ${to_path}"
exit 1
}
}
# This function will display a confirmation to continue (force continue; no exit function)
_confirm() {
# call with a prompt string or use a default
read -r -p "[${SCRIPT_NAME}]: ${1:-Continue? [y] Exit? [CTRL+C]} " response
case "$response" in
[yY][eE][sS] | [yY])
return 0
;;
*)
echo "[${SCRIPT_NAME}]: Invalid response to the question!"
_confirm "$1"
;;
esac
}
# This function is used to ask the next action.
# Usage: _ask [message] [default-response]
# Example: _ask "Do you want to continue?" "y"
_ask() {
local text default_response
text=$1
default_response=$2
if [ -z "${default_response}" ]; then
default_response="Y"
fi
read -r -p "${text} [default:${default_response}] [Y/n]: " response
# Simulate a default response so that we can press Enter key
if [[ -z "${response}" ]]; then
response="${default_response}"
fi
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
return 0
else
echo "[${SCRIPT_NAME}->${FUNCNAME[0]}->cancel]: Operation aborted!"
exit 0
fi
}
# Print if verbose is enabled and not in scripting mode
info() {
local caller
caller="${SCRIPT_NAME}->${FUNCNAME[0]}"
if [[ ${VERBOSE} -ge "$1" ]]; then
echo "[${caller}]: $2"
fi
}
# Error handling that must exit the script manually
error() {
local caller
caller="${SCRIPT_NAME}->${FUNCNAME[0]}"
[[ "${SCRIPTING}" == false ]] && echo "[${caller}]: $1" >&2
[[ "${SCRIPTING}" == true ]] && echo "error"
exit 254
}
# Handling script simple status
get_status_message() {
local retval
retval="$1"
if [[ "${retval}" -eq 0 ]]; then
info 3 "[ OK ]"
else
error "[ FAILED ]"
fi
}
check_path() {
local paths
paths="$*"
for path in ${paths}; do
if [ -f "${path}" ]; then
:
#echo "path is file"
elif [ -d "${path}" ]; then
:
# echo "path is dir"
elif [ -L "${path}" ]; then
:
#echo "path is link"
elif [ -S "${path}" ]; then
:
#echo "path is socket"
else
error "Error, path is not valid: ${path}"
fi
done
}
# Prompt user to reconfigure script
setup_wizard() {
local caller
caller="${SCRIPT_NAME}->${FUNCNAME[0]}"
echo -ne "[${caller}->input]: Do you want to run setup wizard? [y/n]: "
read -r answer
if [[ "${answer}" == "y" || "${answer}" == "Y" ]]; then
# Copy the sample config file from template
info 1 "Converting config file into unix format"
sed -i 's/\r$//' "${SCRIPT_PATH}/${SCRIPT_NAME}.conf.sample"
info 1 "Copying sample config file from template ..."
cp -f "${SCRIPT_PATH}/${SCRIPT_NAME}.conf.sample" "${CONFIG_FILE}"
retval=$?
info 1 "Using config file: ${CONFIG_FILE}"
if [ "${retval}" -eq 0 ]; then
info 1 "Ok, sample config file copied successfully"
read_config
else
error "Sample config file copied failed"
exit 1
fi
else
info 1 "Ok, setup wizard skipped"
fi
exit 0
}
# Read config file
read_config() {
local caller
caller="${SCRIPT_NAME}->${FUNCNAME[0]}"
if [[ -z "${CONFIG_FILE}" ]]; then
info 2 "Using default config file: ${SCRIPT_PATH}/${SCRIPT_NAME}.conf"
CONFIG_FILE="${SCRIPT_PATH}/${SCRIPT_NAME}.conf"
else
info 2 "Using config file: ${CONFIG_FILE}"
fi
info 2 "Checking config file ${CONFIG_FILE} ..."
if [[ -f ${CONFIG_FILE} ]]; then
sed -i 's/\r$//' "${CONFIG_FILE}"
TEST_SOURCE="$(source "${CONFIG_FILE}" 2>&1 >/dev/null)"
RETVAL=$?
if [[ ${RETVAL} -eq 0 ]]; then
source "${CONFIG_FILE}" 2>/dev/null
else
info 1 "Warning, config file ${CONFIG_FILE} contains invalid syntax"
info 2 "Invalid syntax details:"
info 2 "${TEST_SOURCE}"
mv "${CONFIG_FILE}" "${CONFIG_FILE}".old
info 2 "The old configuration file has been backed up as ${CONFIG_FILE}.old"
setup_wizard
fi
else
echo "[${caller}]: Warning, config file ${CONFIG_FILE} not found"
setup_wizard
fi
}
# Resolve a given target to an IP
resolve() {
local target ip_lookup
target="$1"
# Convert target to IP with slash notation
if ipcalc -c -4 "${target}" >/dev/null 2>&1; then
ip_lookup="${target}"
elif ipcalc -c -6 "${target}" >/dev/null 2>&1; then
ip_lookup=$(ipcalc -6 "${target}" | awk '/^Full Address:/ { print $NF }')
else
local get_ip_lookup
get_ip_lookup=$(getent hosts "${target}" | awk '{ print $1 ; exit }')
if ipcalc -c -4 "${get_ip_lookup}" >/dev/null 2>&1; then
ip_lookup="${get_ip_lookup}"
elif ipcalc -c -6 "${get_ip_lookup}" >/dev/null 2>&1; then
ip_lookup=$(ipcalc -6 "${get_ip_lookup}" | awk '/^Full Address:/ { print $NF }')
else
ip_lookup=""
fi
fi
echo "${ip_lookup}"
}
scan() {
local caller target
caller="${SCRIPT_NAME}->${FUNCNAME[0]}"
target="${TARGET}"
if [[ -z "${target}" ]]; then
error "No target specified for scanning (eg ${SCRIPT_NAME} -t example.com)"
fi
echo "[${caller}]: Scanning target ${target} ..."
if [[ "${ABUSEIPDB_ENABLE_SCAN}" == "true" ]]; then
if command -v aipdb >/dev/null 2>&1; then
# echo "[${caller}]: Scanning target ${target} using AbuseIPDB ..."
aipdb check -ko "${LOG_PATH}/aipdb_output.log" -t "${target}"
else
echo "[Skipped]: aipdb is not installed"
fi
echo ""
else
echo "[Skipped]: ABUSEIPDB_ENABLE_SCAN is disabled"
fi
if [[ "${BLCHECK_ENABLE_SCAN}" == "true" ]]; then
if command -v blcheck >/dev/null 2>&1; then
# echo "[${caller}]: Scanning ${target} using blcheck ..."
blcheck -ko "${LOG_PATH}/blcheck_output.log" -l "${CONFIG_PATH}/blcheck_service_list" "${target}"
else
echo "[Skipped]: blcheck is not installed"
fi
echo ""
else
echo "[Skipped]: BLCHECK_ENABLE_SCAN is disabled"
fi
if [[ "${GREYNOISE_ENABLE_SCAN}" == "true" ]]; then
if command -v greynoise >/dev/null 2>&1; then
# echo "[${caller}]: Scanning target ${target} using greynoise ..."
greynoise check -ko "${LOG_PATH}/greynoise_output.log" -t "${target}"
else
echo "[Skipped]: greynoise is not installed"
fi
echo ""
else
echo "[Skipped]: GREYNOISE_ENABLE_SCAN is disabled"
fi
if [[ "${VIRUSTOTAL_ENABLE_SCAN}" == "true" ]]; then
if command -v virustotal >/dev/null 2>&1; then
# echo "[${caller}]: Scanning target ${target} using virustotal ..."
virustotal check -ko "${LOG_PATH}/virustotal_output.log" -t "${target}"
else
echo "[Skipped]: virustotal is not installed"
fi
echo ""
else
echo "[Skipped]: VIRUSTOTAL_ENABLE_SCAN is disabled"
fi
if [[ "${MAXILOG_ENABLE_SCAN}" == "true" ]]; then
if command -v maxilog >/dev/null 2>&1; then
# echo "[${caller}]: Scanning target ${target} using maxilog ..."
maxilog scan --target "${target}"
else
echo "[Skipped]: maxilog is not installed"
fi
echo ""
else
echo "[Skipped]: MAXILOG_ENABLE_SCAN is disabled"
fi
if [[ "${MODSECURITY_ENABLE_SCAN}" == "true" ]]; then
local ipv4 ipv6_uncompress ipv6_semi_compress ipv6_compress mod_security_log_path result
mod_security_log_path="/var/log/nginx/modsec_audit.log"
# Search for modsecurity logs
if [[ ! -f "${mod_security_log_path}" ]]; then
mod_security_log_path="/var/log/apache2/modsec_audit.log"
fi
if [[ ! -f "${mod_security_log_path}" ]]; then
mod_security_log_path="/var/log/httpd/modsec_audit.log"
fi
if [[ ! -f "${mod_security_log_path}" ]]; then
mod_security_log_path="/var/log/apache/modsec_audit.log"
fi
if [[ ! -f "${mod_security_log_path}" ]]; then
error "ModSecurity log file not found"
fi
if ipcalc -4 -n "${target}" >/dev/null 2>&1; then
ipv4="${target}"
result=$(jq --arg ipv4 "${ipv4}" '.transaction | select(.client_ip == $ipv4)' "${mod_security_log_path}")
else
ipv6_uncompress=$(ipcalc -6 "${target}" | awk '/^Full Address:/ { print $NF }')
ipv6_semi_compress=$(echo "${ipv6_uncompress}" | awk -F: 'BEGIN {OFS=":"} {for (i=1; i<=NF; i++) if ($i == "0000") {$i="0"}; gsub(/0{1,3}:/,"::"); print}')
ipv6_compress=$(ipcalc -6 "${ipv6_uncompress}" | awk '/^Address:/ { print $NF }')
result=$(jq --arg ipv6_uncompress "${ipv6_uncompress}" --arg ipv6_semi_compress "${ipv6_semi_compress}" --arg ipv6_compress "${ipv6_compress}" '.transaction | select(.client_ip == $ipv6_uncompress or .client_ip == $ipv6_semi_compress or .client_ip == $ipv6_compress)' "${mod_security_log_path}")
fi
if [ -n "${result}" ]; then
local found_count
found_count=$(echo "${result}" | jq -r '.client_ip' | wc -l)
info 0 "IP found in modsecurity log file (count): ${found_count}"
else
info 0 "IP not found in modsecurity log file"
fi
echo ""
else
error "MODSECURITY_ENABLE_SCAN is disabled"
fi
if [[ "${CSF_ENABLE_SCAN}" == "true" ]]; then
local csf_deny_path
csf_deny_path="/etc/csf/csf.deny"
if [[ ! -f "${csf_deny_path}" ]]; then
error "CSF deny file not found"
fi
# Check the result of the csf command for the given IP
result=$(csf -g "${target}")
# Check if the IP is blocked
if [[ "${result}" == *"IPSET: Set:chain_DENY Match:"* || "${result}" == *"IPSET: Set:chain_6_DENY Match:"* ]]; then
info 0 "IP found in csf deny file"
else
info 0 "IP not found in csf deny file"
fi
else
error "CSF_ENABLE_SCAN is disabled"
fi
if [[ "${CSF_ENABLE_CIDR_SCAN}" == "true" ]]; then
local csf_deny_path target_ip ip_to_cidr bad_ips cidr_ips_file cidr_ips_count
csf_deny_path="/etc/csf/csf.deny"
if [[ ! -f "${csf_deny_path}" ]]; then
error "CSF deny file not found"
fi
target_ip=$(resolve "${TARGET}")
info 1 "Target ${TARGET} is resolved to ${target_ip}"
if [ -z "${target_ip}" ]; then
error "No IP address found for target ${TARGET} (eg: ${SCRIPT_NAME} --ip-address=1.2.3.4)"
fi
if ! ipcalc -4 -sc "${target_ip}"; then
error "IP ${target_ip} is not a valid IPv4 address (eg: ${SCRIPT_NAME} --ip-address=1.2.3.4)"
fi
if [ -z "${NETMASK}" ]; then
info 1 "No netmask specified, using default netmask of 24"
NETMASK=24
else
info 1 "Using netmask ${NETMASK}"
fi
# Make sure target_ip exist in INPUT_FILE
if ! grep -q "^${target_ip}" "${INPUT_FILE}"; then
error "IP ${target_ip} does not exist in ${INPUT_FILE}"
fi
ip_to_cidr=$(ipcalc --silent "${target_ip}/${NETMASK}" | awk '/^Network:/{ print $NF }' | tail -n 1)
info 0 "Searching for IP ${target_ip} with network ${ip_to_cidr} in ${INPUT_FILE} ..."
# Check inside the file if the IP (in netmask/cidr form) is already there
if grep -q "${ip_to_cidr}" "${INPUT_FILE}"; then
error "IP ${ip_to_cidr} already exists in ${INPUT_FILE}"
fi
# This will only get input that is not blankspace, comment
bad_ips=$(grep -Eo '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' "${INPUT_FILE}" | grep -v '^\s*$\|^\s*\#')
if [[ -z "${bad_ips}" ]]; then
error "No bad IPs found in ${INPUT_FILE}"
fi
cidr_ips_file=$(mktemp)
while read -r ip; do
cidr_ip=$(ipcalc --silent "${ip}/${NETMASK}" | awk '/^Network:/{ print $NF }')
echo "${cidr_ip}" >>"${cidr_ips_file}"
done <<<"${bad_ips}"
cidr_ips_count=$(grep -c "${ip_to_cidr}" "${cidr_ips_file}")
((cidr_ips_count--))
if [[ "${VERBOSE}" -ge 0 ]]; then
info 0 "The IP address ${target_ip} shares its network with ${cidr_ips_count} other IP(s)"
else
echo "${cidr_ips_count}"
fi
rm -f "${cidr_ips_file}"
else
error "CSF_ENABLE_CIDR_SCAN is disabled"
fi
}
# This will return the abuseipdb score of a given IP
# Range is from 0 to 100 (integer), or error (exit 254)
# Use with --scripting option
get_aipdb_score() {
local caller target
caller="${SCRIPT_NAME}->${FUNCNAME[0]}"
target="${TARGET}"
if [[ "${ABUSEIPDB_ENABLE_SCAN}" == "true" ]]; then
if command -v aipdb >/dev/null 2>&1; then
aipdb check -sko "${LOG_PATH}/aipdb_output.log" -t "${target}"
else
error "aipdb is not installed"
fi
else
error "ABUSEIPDB_ENABLE_SCAN is disabled"
fi
}
# This will return the blcheck score of a given IP
# Range is from 0 to 100.00 (float), or error (exit 254)
# Use with --scripting option
get_blcheck_score() {
local caller target
caller="${SCRIPT_NAME}->${FUNCNAME[0]}"
target="${TARGET}"
if [[ "${BLCHECK_ENABLE_SCAN}" == "true" ]]; then
if command -v blcheck >/dev/null 2>&1; then
blcheck -sko "${LOG_PATH}/blcheck_output.log" -l "${CONFIG_PATH}/blcheck_service_list" "${target}"
else
error "blcheck is not installed"
fi
else
error "BLCHECK_ENABLE_SCAN is disabled"
fi
}
# This will return the greynoise score of a given IP
# Return status are clean - (return 0), malicious (return 1), unknown (return 2), null (return 3), error (return 4).
# Use with --scripting option
get_greynoise_score() {
local caller target
caller="${SCRIPT_NAME}->${FUNCNAME[0]}"
target="${TARGET}"
if [[ "${GREYNOISE_ENABLE_SCAN}" == "true" ]]; then
if command -v greynoise >/dev/null 2>&1; then
greynoise check -sko "${LOG_PATH}/greynoise_output.log" -t "${target}"
else
error "greynoise is not installed"
fi
else
error "GREYNOISE_ENABLE_SCAN is disabled"
fi
}
# This will return the virustotal score of a given IP
# Return status are clean - (return 0), suspicious (return 1), malicious (return 2), unknown (return 3), or error (exit 254).
# Use with --scripting option
get_virustotal_score() {
local caller target
caller="${SCRIPT_NAME}->${FUNCNAME[0]}"
target="${TARGET}"
if [[ "${VIRUSTOTAL_ENABLE_SCAN}" == "true" ]]; then
if command -v virustotal >/dev/null 2>&1; then
# echo "[${caller}]: Scanning target ${target} using virustotal ..."
virustotal check -sko "${LOG_PATH}/virustotal_output.log" -t "${target}"
else
error "virustotal is not installed"
fi
else
error "VIRUSTOTAL_ENABLE_SCAN is disabled"
fi
}
# Use with --scripting
get_modsecurity_score() {
# Check if target IP is in the modsecurity file
local caller target
local ipv4 ipv6_uncompress ipv6_semi_compress ipv6_compress mod_security_log_path result
caller="${SCRIPT_NAME}->${FUNCNAME[0]}"
target=$(resolve "${TARGET}")
mod_security_log_path="/var/log/nginx/modsec_audit.log"
# Search for modsecurity logs
if [[ ! -f "${mod_security_log_path}" ]]; then
mod_security_log_path="/var/log/apache2/modsec_audit.log"
fi
if [[ ! -f "${mod_security_log_path}" ]]; then
mod_security_log_path="/var/log/httpd/modsec_audit.log"
fi
if [[ ! -f "${mod_security_log_path}" ]]; then
mod_security_log_path="/var/log/apache/modsec_audit.log"
fi
if [[ ! -f "${mod_security_log_path}" ]]; then
error "ModSecurity log file not found"
fi
if ipcalc -4 -n "${target}" >/dev/null 2>&1; then
ipv4="${target}"
result=$(jq --arg ipv4 "${ipv4}" '.transaction | select(.client_ip == $ipv4)' "${mod_security_log_path}")
else
ipv6_uncompress=$(ipcalc -6 "${target}" | awk '/^Full Address:/ { print $NF }')
ipv6_semi_compress=$(echo "${ipv6_uncompress}" | awk -F: 'BEGIN {OFS=":"} {for (i=1; i<=NF; i++) if ($i == "0000") {$i="0"}; gsub(/0{1,3}:/,"::"); print}')
ipv6_compress=$(ipcalc -6 "${ipv6_uncompress}" | awk '/^Address:/ { print $NF }')
result=$(jq --arg ipv6_uncompress "${ipv6_uncompress}" --arg ipv6_semi_compress "${ipv6_semi_compress}" --arg ipv6_compress "${ipv6_compress}" '.transaction | select(.client_ip == $ipv6_uncompress or .client_ip == $ipv6_semi_compress or .client_ip == $ipv6_compress)' "${mod_security_log_path}")
fi
if [ -n "${result}" ]; then
[[ "${VERBOSE}" -ge 0 ]] && echo "IP address ${target_ip} found in modsecurity log"
[[ "${VERBOSE}" -lt 0 ]] && echo "ip-found"
else
[[ "${VERBOSE}" -ge 0 ]] && echo "IP address ${target_ip} not found in modsecurity log"
[[ "${VERBOSE}" -lt 0 ]] && echo "ip-not-found"
fi
}
get_csf_score() {
local caller target_ip csf_deny_path
caller="${SCRIPT_NAME}->${FUNCNAME[0]}"
target_ip=$(resolve "${TARGET}")
csf_deny_path="/etc/csf/csf.deny"
if [[ ! -f "${csf_deny_path}" ]]; then
error "CSF deny file not found"
fi
# Check the result of the csf command for the given IP
result=$(csf -g "${target_ip}")
# Check if the IP is blocked
if [[ "${result}" == *"IPSET: Set:chain_DENY Match:"* || "${result}" == *"IPSET: Set:chain_6_DENY Match:"* ]]; then
[[ "${VERBOSE}" -ge 0 ]] && echo "IP addres ${target_ip} found in csf.deny"
[[ "${VERBOSE}" -lt 0 ]] && echo "ip-not-found"
else
[[ "${VERBOSE}" -ge 0 ]] && echo "Error no IP found in csf.deny"
[[ "${VERBOSE}" -lt 0 ]] && echo "ip-not-found"
fi
}
get_dabfm_score() {
local caller target
caller="${SCRIPT_NAME}->${FUNCNAME[0]}"
target=$(resolve "${TARGET}")
# TODO use Directadmin API to get the list of blocked IP
}
# At this moment use CSF to block target
# Scripting value:
# error-no-dns-record, ip-already-blocked, ok-ip-block-success, error-ip-block-failed
block_target() {
local caller target
# Note CSF will always use compressed IPv6 address in csf.deny even if we pass full uncompressed IP
local caller target_ip csf_ip_previous_block_status csf_ip_block_status csf_success_block_status block_msg
caller="${SCRIPT_NAME}->${FUNCNAME[0]}"
target_ip=$(resolve "${TARGET}")
block_msg="${COMMENTS}"
# Sometimes target is not reachable or contains another domain (unresolvable), in this case we quit
if [ -z "${target_ip}" ]; then
[[ "${VERBOSE}" -ge 0 ]] && echo "[${SCRIPT_NAME}]: Error, No DNS record found for target ${target}"
[[ "${VERBOSE}" -lt 0 ]] && echo "error-no-dns-record"
exit 1
fi
[[ "${VERBOSE}" -ge 0 ]] && echo "[${SCRIPT_NAME}]: Using target ${TARGET}, Blocking IP ${target_ip}, with comment \"${block_msg}\" ..."
# This will check if the existing IP has been blocked before (works with IPv6 and IPv4)
csf_ip_previous_block_status=$(csf -g "${target_ip}" | grep -E 'IPSET: Set:chain_DENY Match:|IPSET: Set:chain_6_DENY Match:')
# If string found, means already blocked
if [ -n "${csf_ip_previous_block_status}" ]; then
[[ "${VERBOSE}" -ge 0 ]] && echo "Notice, IP ${target_ip} already blocked"
[[ "${VERBOSE}" -lt 0 ]] && echo "ip-already-blocked"
exit 1
else
csf_ip_block_status=$(csf -d "${target_ip}" "${block_msg}")
csf_success_block_status=$(echo "${csf_ip_block_status}" | grep 'Adding')
retval=$?
if [ "${retval}" -eq 0 ]; then
[[ "${VERBOSE}" -ge 0 ]] && echo "${csf_success_block_status}"
[[ "${VERBOSE}" -lt 0 ]] && echo "ok-ip-block-success"
exit 0
else
[[ "${VERBOSE}" -ge 0 ]] && echo "${csf_ip_block_status}"
[[ "${VERBOSE}" -lt 0 ]] && echo "error-ip-block-failed"
exit 1
fi
fi
}
# At this moment use aipdb website to send report
report_target() {
local caller target
caller="${SCRIPT_NAME}->${FUNCNAME[0]}"
target_ip=$(resolve "${TARGET}")
report_category="${CATEGORIES}"
report_comment="${COMMENTS}"
# Check if ABUSEIPDB_ENABLE_WEB_REPORT is set to true
if [ "${ABUSEIPDB_ENABLE_WEB_REPORT}" = true ]; then
if command -v aipdb >/dev/null 2>&1; then
local get_aipdb_report
get_aipdb_report=$(aipdb report --ip-address="${target_ip}" --categories="${report_category}" --comment="${report_comment}" --scripting)
if [ "${get_aipdb_report}" = "success" ]; then
[[ "${VERBOSE}" -ge 0 ]] && echo "Report sent to AbuseIPDB"
[[ "${VERBOSE}" -lt 0 ]] && echo "ok-report-sent"
else
[[ "${VERBOSE}" -ge 0 ]] && echo "Error sending report to AbuseIPDB"
[[ "${VERBOSE}" -lt 0 ]] && echo "error-report-not-sent"
fi
else
error "aipdb-command-not-found"
fi
else
error "abuseipdb-web-report-disabled"
fi
}
# This only work with IPv4. IPv6 is not supported yet
# Return the IP address count that belongs to a network
# Use with --scripting option
# Usage: maxiwall getIP2CIDR --ip-address=1.2.3.4 --netmask=24 --input-file=/etc/csf/csf.deny --scripting
get_ip2cidr() {
local target_ip ip_to_cidr
target_ip=$(resolve "${TARGET}")
info 1 "Target ${TARGET} is resolved to ${target_ip}"
if [ -z "${target_ip}" ]; then
error "ip-is-not-specified"
fi
if ! ipcalc -4 -sc "${target_ip}"; then
error "ip-is-not-valid"
fi
if [ -z "${NETMASK}" ]; then
info 1 "No netmask specified, using default netmask of 24"
NETMASK=24
else
info 1 "Using netmask ${NETMASK}"
fi
# Make sure target_ip exist in INPUT_FILE
if ! grep -q "^${target_ip}" "${INPUT_FILE}"; then
echo "ip-is-not-found"
exit 254
fi
ip_to_cidr=$(ipcalc --silent "${target_ip}/${NETMASK}" | awk '/^Network:/{ print $NF }' | tail -n 1)
info 0 "Searching for IP ${target_ip} with network ${ip_to_cidr} in ${INPUT_FILE} ..."
# Check inside the file if the IP (in netmask/cidr form) is already there
if grep -q "${ip_to_cidr}" "${INPUT_FILE}"; then
echo "ip-already-exist"
exit 254
fi
# This will only get input that is not blankspace, comment
bad_ips=$(grep -Eo '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' "${INPUT_FILE}" | grep -v '^\s*$\|^\s*\#')
if [[ -z "${bad_ips}" ]]; then
echo "no-bad-ip-found"
exit 254
fi
cidr_ips_file=$(mktemp)
while read -r ip; do
cidr_ip=$(ipcalc --silent "${ip}/${NETMASK}" | awk '/^Network:/{ print $NF }')
echo "${cidr_ip}" >>"${cidr_ips_file}"
done <<<"${bad_ips}"
cidr_ips_count=$(grep -c "${ip_to_cidr}" "${cidr_ips_file}")
((cidr_ips_count--))
if [[ "${VERBOSE}" -ge 0 ]]; then
info 0 "The IP address ${target_ip} shares its network with ${cidr_ips_count} other IP(s)"
else
echo "${cidr_ips_count}"
fi
rm -f "${cidr_ips_file}"
}
get_env() {
local varname="$1"
if [[ -z "${!varname}" ]]; then
error "Variable ${varname} is not set"
fi
echo "${!varname}"
}
edit_file() {
local file="$1"
if [ -f "${file}" ]; then
info 1 "Editing file ${file}"
if command -v "nano" &>/dev/null; then
nano -c "${file}"
elif command -v "vi" &>/dev/null; then
vi "${file}"
else
error "No editor found"
fi
else
error "File ${file} does not exist"
fi
}
# Show file using tail with 100 lines
show_file() {
local file="$1"
if [ -f "${file}" ]; then
info 1 "Showing file ${file}"
tail -f -n 100 "${file}"
else
error "File ${file} does not exist"
fi
}
get_public_ipv4() {
local ipv4
ipv4=$(curl -s https://api.ipify.org)
retval=$?
if [ "${retval}" -eq 0 ]; then
echo "${ipv4}"
else
error "Unable to get public IPv4 address"
fi
}
get_public_ipv6() {
local ipv6
ipv6=$(curl -s https://api6.ipify.org)
retval=$?
if [ "${retval}" -eq 0 ]; then
# Make sure the IPv6 is in an expanded form
ipv6=$(ipcalc -s "${ipv6}" | awk '/Full Address:/{ print $NF }')
echo "${ipv6}"
else
error "Unable to get public IPv6 address"
fi
}
stop() {
local service_name="$1"
echo "service_name: ${service_name}"
if [ -z "${service_name}" ]; then
error "No service name specified"
fi
if ! systemctl is-active --quiet "${service_name}"; then
info 0 "Service ${service_name} is not running"
return 0
fi
info 0 "Stopping service ${service_name} ..."
systemctl stop "${service_name}"
retval=$?
if [ "${retval}" -ne 0 ]; then
error "Unable to stop service ${service_name}"
fi
info 0 "Service ${service_name} stopped"
}
start() {
local service_name="$1"
if [ -z "${service_name}" ]; then
error "No service name specified"
fi
if systemctl is-active --quiet "${service_name}"; then
info 0 "Service ${service_name} is already running"
return 0
fi
info 0 "Starting service ${service_name} ..."
systemctl start "${service_name}"
retval=$?
if [ "${retval}" -ne 0 ]; then
error "Unable to start service ${service_name}"
fi
info 0 "Service ${service_name} started"
}
restart() {
local service_name="$1"
if [ -z "${service_name}" ]; then
error "No service name specified"
fi