Compare commits
13 commits
feature/ip
...
master
Author | SHA1 | Date | |
---|---|---|---|
9b4a55dfb2 | |||
64918328c9 | |||
22bc52d665 | |||
31f0901134 | |||
70d41673f7 | |||
a1d5821cc7 | |||
b3afda4932 | |||
142db098be | |||
5078363348 | |||
f5180a5e57 | |||
11ad1b471a | |||
8e0f22da8f | |||
0871a25bf7 |
3 changed files with 194 additions and 47 deletions
30
ddos-mitigator.conf
Normal file
30
ddos-mitigator.conf
Normal file
|
@ -0,0 +1,30 @@
|
|||
# Example configuration file for ddos-mitigator.sh.
|
||||
# PLEASE TAKE CARE not to put any whitespace around the '=' signs, as this file is directly sourced by the script
|
||||
# file and this needs to conform to the BASH syntax. Also, make sure to declare the COUNTRIES variable with the
|
||||
# correct array syntax: COUNTRIES=("AA" "BB" "CC"), or to comment it out altogether.
|
||||
|
||||
# The path to the GeoIP2 database file (must be either country or city database). This parameter is mandatory. If it is
|
||||
# not specified here, it must be given on the command line (through the -d option).
|
||||
DATABASE_FILE="/path/to/geoip/country-or-city-database.mmdb"
|
||||
|
||||
# Enable the autopilot for automatically banning IP addresses of the desired countries (see also COUNTRIES option).
|
||||
# Only ban IP addresses with at least AUTOPILOT current connections. If the value is not specified or 0, don't
|
||||
# automatically ban IP addresses, but run in interactive mode.
|
||||
AUTOPILOT="0"
|
||||
|
||||
# Defines the subnet size in bytes to be analyzed. Valid values are:
|
||||
# - 8 for class A networks (X.0.0.0/8)
|
||||
# - 16 for class B networks (X.X.0.0/16)
|
||||
# - 24 for class C networks (X.X.X.0/24)
|
||||
# - 32 for class D networks (X.X.X.X/32)
|
||||
# If not specified, run in interactive mode and prompt for the netmask size.
|
||||
NETMASK="8"
|
||||
|
||||
# The country-codes to block as an array. Defaults to "CN" (China).
|
||||
#COUNTRIES=("CN" "HK" "TW")
|
||||
|
||||
# Specify the JAIL to use for banning the IP addresses. Defaults to 'apache-auth'.
|
||||
#JAIL="apache-auth"
|
||||
|
||||
# The desired port to monitor. Defaults to 443 (https).
|
||||
#PORT="443"
|
|
@ -32,10 +32,7 @@
|
|||
# #
|
||||
################################################################################
|
||||
|
||||
# Set the host's own IP address. So far, only an IPv4 address is supported.
|
||||
MY_IP="94.199.214.20"
|
||||
|
||||
# After this point, no editing is required.
|
||||
# Store the start time; this enables us to output the total runtime at the end.
|
||||
start="$(date +%s)"
|
||||
|
||||
# Dependencies of this script. Simple array with the following structure:
|
||||
|
@ -139,6 +136,13 @@ Usage: $(basename $0) -d FILE [OPTION...]
|
|||
printed to stderr and the program
|
||||
terminates with exit code 1.
|
||||
|
||||
-f, --config-file=FILENAME Specify the full path to the configuration
|
||||
file. If omitted, all options are read
|
||||
from the command line.
|
||||
Any parameter specified on the command
|
||||
line takes precedence over configuration
|
||||
option read from the file.
|
||||
|
||||
-j, --jail=JAIL Specify the JAIL to use for banning the IP
|
||||
addresses.
|
||||
Defaults to 'apache-auth'.
|
||||
|
@ -194,8 +198,48 @@ function filter() {
|
|||
mv "${filtered}" "${file}"
|
||||
}
|
||||
|
||||
function set_default_values() {
|
||||
if [[ -z "${autopilot}" ]]; then
|
||||
autopilot=0
|
||||
fi
|
||||
if [[ -z "${bancountries}" ]]; then
|
||||
bancountries=("CN")
|
||||
fi
|
||||
if [[ -z "${jail}" ]]; then
|
||||
jail="apache-auth"
|
||||
fi
|
||||
if [[ -z "${netmask}" ]]; then
|
||||
netmask=0
|
||||
fi
|
||||
if [[ -z "${port}" ]]; then
|
||||
port=443
|
||||
fi
|
||||
}
|
||||
|
||||
function parse_config_file() {
|
||||
source "${configfile}"
|
||||
if [[ -z "${autopilot}" ]]; then
|
||||
autopilot="${AUTOPILOT}"
|
||||
fi
|
||||
if [[ -z "${bancountries}" ]]; then
|
||||
bancountries=(${COUNTRIES[@]})
|
||||
fi
|
||||
if [[ -z "${database}" ]]; then
|
||||
database="${DATABASE_FILE}"
|
||||
fi
|
||||
if [[ -z "${jail}" ]]; then
|
||||
jail="${JAIL}"
|
||||
fi
|
||||
if [[ -z "${netmask}" ]]; then
|
||||
netmask="${NETMASK}"
|
||||
fi
|
||||
if [[ -z "${port}" ]]; then
|
||||
port="${PORT}"
|
||||
fi
|
||||
}
|
||||
|
||||
function parse_command_line_args() {
|
||||
TEMP=$(getopt -o 'a::,c:,d:,e,j:,n:,p:,h' -l 'auto::,country:,database:,dependencies,jail:,netmask:,port:,help' -- "$@")
|
||||
TEMP=$(getopt -o 'a::,c:,d:,e,f:,j:,n:,p:,h' -l 'auto::,country:,database:,dependencies,config-file:,jail:,netmask:,port:,help' -- "$@")
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo 'Error parsing command line options. Terminating. Invoke with --help for help.' >&2
|
||||
|
@ -212,10 +256,6 @@ function parse_command_line_args() {
|
|||
'')
|
||||
autopilot=1
|
||||
;;
|
||||
*[!0-9]*)
|
||||
echo "Invalid argument for parameter 'auto': '${2}'. Invoke with --help for help." >&2
|
||||
exit 1
|
||||
;;
|
||||
*)
|
||||
autopilot="$2"
|
||||
;;
|
||||
|
@ -224,10 +264,6 @@ function parse_command_line_args() {
|
|||
;;
|
||||
'-c' | '--country')
|
||||
IFS=',' read -ra bancountries <<<"${2}"
|
||||
if [[ -z ${bancountries[@]// /} ]]; then
|
||||
echo "Invalid argument for parameter 'country': '${2}'. Invoke with --help for help." >&2
|
||||
exit 1
|
||||
fi
|
||||
shift
|
||||
;;
|
||||
'-d' | '--database')
|
||||
|
@ -238,27 +274,30 @@ function parse_command_line_args() {
|
|||
check_dependencies
|
||||
exit $?
|
||||
;;
|
||||
'-f' | '--config-file')
|
||||
configfile="${2}"
|
||||
shift
|
||||
;;
|
||||
'-j' | '--jail')
|
||||
jail="${2}"
|
||||
shift
|
||||
;;
|
||||
'-n' | '--netmask')
|
||||
case "${2}" in
|
||||
'1' | '8')
|
||||
'1')
|
||||
netmask=8
|
||||
;;
|
||||
'2' | '16')
|
||||
'2')
|
||||
netmask=16
|
||||
;;
|
||||
'3' | '24')
|
||||
'3')
|
||||
netmask=24
|
||||
;;
|
||||
'4' | '32')
|
||||
'4')
|
||||
netmask=32
|
||||
;;
|
||||
*)
|
||||
echo "Invalid argument for parameter 'netmask': '${2}'. Invoke with --help for help." >&2
|
||||
exit 1
|
||||
netmask="${2}"
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
|
@ -283,15 +322,81 @@ function parse_command_line_args() {
|
|||
shift
|
||||
done
|
||||
|
||||
# If the config file option is set, parse the config file.
|
||||
if [[ ! -z ${configfile+x} ]]; then
|
||||
if [[ ! -f "${configfile}" || ! -r "${configfile}" ]]; then
|
||||
echo "Can not read configuration file '${2}'. Invoke with --help for help." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
parse_config_file
|
||||
fi
|
||||
|
||||
# Here, we set the default values for all options that have not been set yet.
|
||||
set_default_values
|
||||
}
|
||||
|
||||
function validate_parameter_values() {
|
||||
# GeoIP-Database
|
||||
if [[ -z "${database}" ]]; then
|
||||
echo "No GeoIP database specified. Invoke with --help for more information." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -r "${database}" ]]; then
|
||||
if [[ ! -f "${database}" || ! -r "${database}" ]]; then
|
||||
echo "Database '${database}' is not accessible." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Autopilot
|
||||
case "${autopilot}" in
|
||||
*[!0-9]*)
|
||||
echo "Invalid value for parameter 'auto' / 'AUTOPILOT': '${autopilot}'." >&2
|
||||
echo "Invoke with --help for help." >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# Countries
|
||||
if [[ -z ${bancountries[@]// /} ]]; then
|
||||
echo "Invalid value for parameter 'country' / 'COUNTRIES': '${bancountries[*]}'." >&2
|
||||
echo "Invoke with --help for help." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Jail
|
||||
if [[ -z "${jail}" ]]; then
|
||||
echo "Invalid value for parameter 'jail' / 'JAIL': '${jail}'." >&2
|
||||
echo "Invoke with --help for help." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Netmask
|
||||
case "${netmask}" in
|
||||
'0' | '8' | '16' | '24' | '32')
|
||||
# Everything OK.
|
||||
;;
|
||||
*)
|
||||
echo "Invalid value for parameter 'netmask': '${2}'." >&2
|
||||
echo "Invoke with --help for help." >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# Port
|
||||
case "${port}" in
|
||||
*[!0-9]*)
|
||||
echo "Invalid value for parameter 'port' / 'PORT': '${autopilot}'." >&2
|
||||
echo "Invoke with --help for help." >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
if [[ ${port} -lt 0 || ${port} -gt 65535 ]]; then
|
||||
echo "Invalid value for parameter 'port' / 'PORT': '${autopilot}'." >&2
|
||||
echo "Value must be between 0 ... 65535 (inclusive)." >&2
|
||||
echo "Invoke with --help for help." >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
################################################################################
|
||||
|
@ -444,14 +549,15 @@ banlist="${tmpdir}/banlist.txt"
|
|||
touch "${banlist}"
|
||||
|
||||
# Parse the command line options
|
||||
autopilot=0
|
||||
netmask=0
|
||||
jail="apache-auth"
|
||||
bancountries=("CN")
|
||||
autopilot=
|
||||
netmask=
|
||||
jail=
|
||||
bancountries=
|
||||
database=
|
||||
port=443
|
||||
port=
|
||||
|
||||
parse_command_line_args "$@"
|
||||
validate_parameter_values
|
||||
|
||||
check_dependencies
|
||||
dependencies_ok=$?
|
||||
|
|
|
@ -1,16 +1,24 @@
|
|||
#!/usr/bin/python3.8
|
||||
#!/usr/bin/python
|
||||
|
||||
import getopt
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
try:
|
||||
import geoip2.database
|
||||
import geoip2.errors
|
||||
except ImportError:
|
||||
print(
|
||||
"Required module geoip2.database not found. On Gentoo Linux, please install dev-python/geoip2 from the 'fritteli' overlay.",
|
||||
"Required modules geoip2.database and geoip2.errors not found. On Gentoo Linux, please install dev-python/geoip2 from the 'fritteli' overlay.",
|
||||
file=sys.stderr)
|
||||
exit(1)
|
||||
|
||||
try:
|
||||
import maxminddb.errors
|
||||
except ImportError:
|
||||
print(
|
||||
"Required module maxminddb.errors not found. On Gentoo Linux, please install dev-python/maxminddb from the 'fritteli' overlay.",
|
||||
file=sys.stderr)
|
||||
exit(1)
|
||||
|
||||
class LookupException(Exception):
|
||||
"""
|
||||
|
@ -47,6 +55,14 @@ def get_county_code(ipaddress, dbfile):
|
|||
raise LookupException("Unsupported DB type: " + dbtype)
|
||||
|
||||
return country.iso_code
|
||||
except FileNotFoundError as e:
|
||||
raise LookupException(e.args)
|
||||
except maxminddb.errors.InvalidDatabaseError as e:
|
||||
raise LookupException(e.args)
|
||||
except geoip2.errors.AddressNotFoundError as e:
|
||||
raise LookupException(e.args)
|
||||
except ValueError as e:
|
||||
raise LookupException(e.args)
|
||||
finally:
|
||||
if reader:
|
||||
reader.close()
|
||||
|
@ -60,21 +76,12 @@ def parse_command_line(argv):
|
|||
:return:
|
||||
"""
|
||||
dbfile = None
|
||||
try:
|
||||
opts, args = getopt.getopt(argv, "f:")
|
||||
except getopt.GetoptError as e:
|
||||
raise LookupException("Error parsing command line") from e
|
||||
|
||||
for opt, arg in opts:
|
||||
if opt == "-f":
|
||||
dbfile = arg
|
||||
else:
|
||||
raise LookupException("Unknown command line argument")
|
||||
|
||||
if len(args) == 0:
|
||||
raise LookupException("No address given on command line")
|
||||
return dbfile, args[0]
|
||||
parser = argparse.ArgumentParser(description='Get the country code from an IP address')
|
||||
parser.add_argument('-f', dest='dbfile', required=True, help="Path to the GeoIP2 database file")
|
||||
parser.add_argument('address', help="The IP address to check")
|
||||
args = parser.parse_args()
|
||||
|
||||
return args.dbfile, args.address
|
||||
|
||||
def main(argv):
|
||||
"""
|
||||
|
@ -83,14 +90,18 @@ def main(argv):
|
|||
:param argv: Format: "-f /path/to/database.mmdb ip.v4.add.ress"
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
(dbfile, ipaddress) = parse_command_line(argv)
|
||||
code = get_county_code(ipaddress, dbfile)
|
||||
print(code)
|
||||
except LookupException as e:
|
||||
print(e.args, file=sys.stderr)
|
||||
print("Unknown")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main(sys.argv[1:])
|
||||
except BaseException as e:
|
||||
print("Usage: geoip-lookup.py -f /path/to/geoip2-database.mmdb 192.168.1.1", file=sys.stderr)
|
||||
print("Unknown")
|
||||
raise e
|
||||
|
|
Loading…
Reference in a new issue