-
Notifications
You must be signed in to change notification settings - Fork 7
/
ddos-inbound.sh
executable file
·80 lines (69 loc) · 1.91 KB
/
ddos-inbound.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
#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-or-later
# set -x
# count inbound connections per remote ip address
function show() {
local relay=$1
local v=""
if [[ $relay =~ "[" ]]; then
v="6"
fi
local conns=0
local ips=0
local sum=0
while read -r conns ip; do
if [[ $conns -gt $limit ]]; then
printf "%-10s %-41s %5i\n" "ip$v" "$ip" "$conns"
((++ips))
((sum += conns))
fi
done < <(
ss --no-header --tcp -${v:-4} --numeric |
grep "^ESTAB" |
grep -F " $relay " |
awk '{ print $5 }' | sort -n | sed -E -e 's,:[[:digit:]]+$,,g' | uniq -c
)
if [[ $ips -gt 0 ]]; then
printf "relay:%-42s ips:%-5i conns:%-5i\n\n" "$relay" "$ips" "$sum"
fi
}
function getConfiguredRelays() {
# shellcheck disable=SC2045
for f in $(ls /etc/tor/torrc* /etc/tor/instances/*/torrc 2>/dev/null); do
if orport=$(grep "^ORPort *" $f | grep -v -F -e ' NoListen' -e '[' -e ':auto' | grep -P "^ORPort\s+.+\s*"); then
if grep -q -Po "^ORPort\s+\d+\.\d+\.\d+\.\d+\:\d+\s*" <<<$orport; then
awk '{ print $2 }' <<<$orport
elif address=$(grep -P "^Address\s+\d+\.\d+\.\d+\.\d+\s*" $f); then
echo $(awk '{ print $2 }' <<<$address):$(awk '{ print $2 }' <<<$orport)
fi
fi
done
}
function getConfiguredRelays6() {
grep -h -e "^ORPort *" /etc/tor/torrc* /etc/tor/instances/*/torrc 2>/dev/null |
grep -v -F -e ' NoListen' -e ':auto' |
grep -P "^ORPort\s+\[[0-9a-f]*:[0-9a-f:]*:[0-9a-f]*\]:\d+\s*" |
awk '{ print $2 }'
}
#######################################################################
set -eu
export LANG=C.utf8
export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
limit=9
relays=$(
getConfiguredRelays
getConfiguredRelays6
)
while getopts l:r: opt; do
case $opt in
l) limit=$((OPTARG + 0)) ;;
r) relays="$OPTARG" ;;
*)
echo "unknown parameter '$opt'"
exit 1
;;
esac
done
for relay in $relays; do
show $relay
done