-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_bot.sh
executable file
·218 lines (199 loc) · 9 KB
/
check_bot.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
#!/bin/bash
source $(pwd)/config.sh
second_size_nginx=0
set port_scan_ip
set ip
function send_tg() {
curl -s "https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendmessage" --data "text=${*}&chat_id=${CHAT_ID}&disable_web_page_preview=true&parse_mode=Markdown" > /dev/null
}
function read_file_dmesg() {
dmesg -TS >> $LOG_DMESG
mapfile -t port_scan_ip< <(cat $IPS_FILTERED)
}
function read_file_nginx(){
cat $NGINX_LOG | cut -d " " -f1 > $IP_RAW_NGINX
awk 'FNR==NR{a[$0]=1;next}!($0 in a)' $IP_LOG $IP_RAW_NGINX | sort| uniq > $IPS_FILTERED_NGINX # https://stackoverflow.com/questions/4717250/extracting-unique-values-between-2-sets-files
mapfile -t ip < <(cat $IPS_FILTERED_NGINX)
}
function sort_ips(){
cat $LOG_DMESG | egrep -o "SRC=+[0-9]*.[0-9]*.[0-9]*.[0-9]*" | sed 's/SRC=//' > $IP_RAW
awk 'FNR==NR{a[$0]=1;next}!($0 in a)' $IP_LOG $IP_RAW | sort| uniq > $IPS_FILTERED # https://stackoverflow.com/questions/4717250/extracting-unique-values-between-2-sets-files
}
function send_tg_2(){
curl -s "https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendmessage" --data "text=${*}&chat_id=${CHAT_ID_2}&disable_web_page_preview=true&parse_mode=Markdown" > /dev/null
}
function alienvault(){
alien_output=$(curl https://otx.alienvault.com/api/v1/indicators/IPv4/"${*}"/reputation \
-H "X-OTX-API-KEY: $ALIENVAULT_API_KEY" )
alien_score=$( echo "$alien_output" | jq '.reputation.threat_score' | (sed 's/null/0/g'; sed 's/\r//g'))
malicious_host=$( echo "$alien_output" | jq '.reputation.counts."Malicious Host"' | (sed 's/null/0/g' ; sed 's/1/MALICIOUS HOST/g' ; sed 's/\r//g'))
}
function reportabuseipdb(){
report_ip=$(curl https://api.abuseipdb.com/api/v2/report \
--data-urlencode "ip=${1}" \
-d categories=${2} \
--data-urlencode "comment=${3}" \
-H "Key: $ABUSEIPDB_API_KEY" \
-H "Accept: application/json")
report_score=$(echo "$report_ip" | jq '.data.abuseConfidenceScore' | (sed 's/null/0/g' ; sed 's/\r//g'))
}
function send_abuseipbdb_request(){
ip_check="$1"
alienvault "$1"
output=$(curl -G https://api.abuseipdb.com/api/v2/check \
--data-urlencode "ipAddress=$1"\
-d maxAgeInDays=90 -d verbose \
-H "Key: $ABUSEIPDB_API_KEY" \
-H "Accept: application/json")
whitelist=$( echo "$output" | jq '.data.isWhitleisted' | (sed 's/null/0/g' ; sed 's/\r//g'))
score=$( echo "$output" | jq '.data.abuseConfidenceScore' | ( sed 's/null/0/g' ; sed 's/\r//g'))
isp=$( echo "$output" | jq '.data.isp'| (sed 's/&/ /g' ; sed 's/null/ /g' ; sed 's/\r//g')) #http api cant handle &
countryname=$( echo "$output" | jq '.data.countryName' | (sed 's/null/ /g' ; sed 's/\r//g'))
totalreport=$( echo "$output" | jq '.data.totalReports' | (sed 's/null/0/g' ; sed 's/\r//g'))
lastreport=$( echo "$output" | jq '.data.lastReportedAt'| (sed 's/null/0/g' ; sed 's/\r//g'))
if [[ $3 != "portscan" ]]; then # the following part is only for nginx logs
log_message=$(cat $NGINX_LOG | grep "$1" | awk '{print $0}' | tail -n 3 | (sed "s/https:\/\/${URL}//g" ; sed "s/http:\/\/${URL}//g" ; sed 's/\r//g')) # cut the whole line from nginx log and remove your url abuseipdb reports
nginx_time=$(echo $log_message | cut -d " " -f4) # cut the time from nginx log
if [ "$score" -gt 1 ] || [ "$alien_score" -gt 0 ] || [ "$totalreport" -gt 0 ] ; then # bad ip check with vars from api response. $score = Abusescore (AbuseIPDB) $totalreport=Amount of Reports (AbuseIPDB) $alien_score=Abusescore from Alienvault TODO: Add Countryfilter
# API Response vars from AbuseIPDB and Alienvault, ip will be blocked if it has a bad reputation
block_message="%0ASuccessfully Blocked! %0AIP: $1 \
%0ATime: $nginx_time \
%0ACountry: $countryname \
%0AAbusescore: $report_score \
%0AISP: $isp \
%0ALast Report: $lastreport%0A \
%0A############# \
%0AAlienvault Results:%0A \
%0AThreat Score: $alien_score \
%0AMalicious: $malicious_host \
%0A%0AMessage: $log_message "
iptables -A INPUT -s "$1" -j DROP #block it in firewall
reportabuseipdb "$1" "$2" "$log_message"
sleep 1 #Tg needs 0.5sec gap between 2 messages
send_tg "$block_message"
sleep 1
send_tg_2 "$block_message"
else # this condition is true, if its an ip without a bad reputation, but it could be a false positive!
message="%0AIP: $1 \
%0ATime: $nginx_time \
%0AAbusescore: $score \
%0AISP: $isp \
%0ACountry: $countryname \
%0ATotal Reports: $totalreport \
%0ALast Report: $lastreport%0A \
%0A#############%0AAlienvault Results:%0A \
%0AThreat Score: $alien_score \
%0AMalicious: $malicious_host \
%0A%0AMessage: $log_message"
sleep 1
send_tg "$message"
sleep 1
send_tg_2 "$message"
fi
else # PORTSCAN PART !
scanned_ports=$5
log_message=$(grep "$1" $LOG_DMESG | tail -1 | (sed "s/$SERVER_IP/MYSERVERIP/g" | sed "s/$SERVER_MAC/SERVERMAC/g" ; sed "s/$HOST_NAME//g")) # cut the whole line from dmesg and remove hostname and server mac
port_scan_time=$4
report_message=$(echo "$log_message Ports: $scanned_ports")
if [ "$score" -gt 1 ] || [ "$alien_score" -gt 0 ] || [ "$totalreport" -gt 0 ] ; then # bad ip check with vars from api response. $score = Abusescore (AbuseIPDB) $totalreport=Amount of Reports (AbuseIPDB) $alien_score=Abusescore from Alienvault TODO: Add Countryfilter
block_message="PORTSCAN: %0ASuccessfully Blocked!\
%0AIP: $1 \
%0APorts: $scanned_ports \
%0ATime: $port_scan_time \
%0ACountry: $countryname \
%0AAbusescore: $report_score \
%0AISP: $isp \
%0ALast Report: $lastreport%0A \
%0A#############%0AAlienvault Results:%0A \
%0AThreat Score: $alien_score \
%0AMalicious: $malicious_host \
%0A%0AMessage: $log_message "
iptables -A INPUT -s "$1" -j DROP
reportabuseipdb "$1" "$2" "$report_message"
sleep 1 #Tg needs 0.5sec gap between 2 messages
send_tg "$block_message"
sleep 1
send_tg_2 "$block_message"
else # this condition is true, if its an ip without a bad reputation, but it could be a false positive!
message="PORTSCAN: \
%0AIP: $1 \
%0APorts: $scanned_ports \
%0ATime: $port_scan_time \
%0AAbusescore: $score \
%0AISP: $isp \
%0ACountry: $countryname \
%0ATotal Reports: $totalreport \
%0ALast Report: $lastreport%0A \
%0A#############%0AAlienvault Results:%0A \
%0AThreat Score: $alien_score \
%0AMalicious: $malicious_host \
%0A%0AMessage: $log_message"
sleep 1
send_tg "$message"
sleep 1
send_tg_2 "$message"
fi
fi
echo "$1" >> $IP_LOG
}
function check_nginx_log() {
for ((i=0;i<=${#ip[@]};i++));do
if [[ -z ${ip[i]} ]]; then
break
fi
current_ip=${ip[i]}
nginx_ip=$(grep "$current_ip" $IP_LOG | tail -n 1)
if [ -z $nginx_ip ]; then
send_abuseipbdb_request "$current_ip" "$REPORT_WEB_ATTACK" "nginx"
fi
done
}
function check_port_scan(){
for ((x=0;x<=${#port_scan_ip[@]};x++));do
current_ip="${port_scan_ip[x]}"
if [[ -z $current_ip ]]; then
break
fi
current_time=$(cat $LOG_DMESG | grep "${port_scan_ip[x]}" | tail -1 | egrep -o "[A-Z]+[a-z]*[[:space:]]+[A-Z]..[[:space:]][0-9].[[:space:]][0-9]*:[0-9]*:[0-9]*[[:space:]][0-9]*" )
current_port=$(cat $LOG_DMESG | grep "${port_scan_ip[x]}" | tail -1 | egrep -o "DPT=+[0-9]*" | sed 's/DPT=//')
if [[ -z $current_port ]]; then
current_port=$(cat $LOG_DMESG | grep "${port_scan_ip[x]}" | tail -1 | egrep -o "PROTO=[A-Z]*" | sed 's/PROTO=//')
fi
if [[ -n $current_ip ]] && [[ -n $current_time ]]; then
send_abuseipbdb_request "$current_ip" "$REPORT_PORT_SCAN" "portscan" "$current_time" "$current_port"
fi
done
rm $LOG_DMESG
}
function has_log_grown(){ #Author @KEN
current_size_nginx=$(du -b $NGINX_LOG | cut -f 1)
if [[ ( $first_size_nginx -lt $current_size_nginx ) ]]
then
first_size_nginx=$current_size_nginx
return 0
fi
return 1
}
########################################################
#
# MAIN
#
########################################################
if ! [[ -w $IP_LOG ]]; then
touch $IP_LOG
for i in "127.0.0.1" "127.0.0.53" "::1" "8.8.8.8" "1.1.1.1" "8.8.4.4" "213.133.98.98" "213.133.99.99" "213.133.100.100" "149.154.167.220" "$SERVER_IP" # 149.154.167.220 api.telegram.org
do
echo "$i" >> $IP_LOG
done
fi
while true; do
dmesg -TS >> $LOG_DMESG
sleep 3
if has_log_grown; then
read_file_nginx
check_nginx_log
fi
sort_ips
read_file_dmesg
check_port_scan
done