2019-12-09 08:12:48 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import database
|
|
|
|
import argparse
|
|
|
|
import sys
|
2019-12-13 13:54:00 +01:00
|
|
|
import time
|
2019-12-09 08:12:48 +01:00
|
|
|
|
2019-12-13 08:23:38 +01:00
|
|
|
FUNCTION_MAP = {
|
|
|
|
'zone': database.Database.set_zone,
|
2019-12-19 08:05:05 +01:00
|
|
|
'hostname': database.Database.set_hostname,
|
2019-12-13 08:23:38 +01:00
|
|
|
'asn': database.Database.set_asn,
|
2019-12-19 08:05:05 +01:00
|
|
|
'ip4network': database.Database.set_ip4network,
|
|
|
|
'ip4address': database.Database.set_ip4address,
|
2019-12-13 08:23:38 +01:00
|
|
|
}
|
2019-12-09 08:12:48 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
# Parsing arguments
|
|
|
|
parser = argparse.ArgumentParser(
|
2019-12-19 08:05:05 +01:00
|
|
|
description="Import base rules to the database")
|
2019-12-09 08:12:48 +01:00
|
|
|
parser.add_argument(
|
|
|
|
'type',
|
2019-12-13 08:23:38 +01:00
|
|
|
choices=FUNCTION_MAP.keys(),
|
2019-12-09 08:12:48 +01:00
|
|
|
help="Type of rule inputed")
|
|
|
|
parser.add_argument(
|
|
|
|
'-i', '--input', type=argparse.FileType('r'), default=sys.stdin,
|
2019-12-19 08:05:05 +01:00
|
|
|
help="File with one rule per line")
|
2019-12-09 08:12:48 +01:00
|
|
|
parser.add_argument(
|
|
|
|
'-f', '--first-party', action='store_true',
|
|
|
|
help="The input only comes from verified first-party sources")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-12-15 15:56:26 +01:00
|
|
|
DB = database.Database()
|
2019-12-13 00:11:21 +01:00
|
|
|
|
|
|
|
fun = FUNCTION_MAP[args.type]
|
|
|
|
|
2019-12-16 19:07:35 +01:00
|
|
|
source: database.RulePath
|
|
|
|
if args.first_party:
|
|
|
|
source = database.RuleFirstPath()
|
|
|
|
else:
|
|
|
|
source = database.RuleMultiPath()
|
|
|
|
|
2019-12-13 00:11:21 +01:00
|
|
|
for rule in args.input:
|
2019-12-18 21:23:49 +01:00
|
|
|
rule = rule.strip()
|
|
|
|
try:
|
|
|
|
fun(DB,
|
|
|
|
rule,
|
|
|
|
source=source,
|
|
|
|
updated=int(time.time()),
|
|
|
|
)
|
|
|
|
except ValueError:
|
|
|
|
DB.log.error(f"Could not add rule: {rule}")
|
2019-12-13 00:11:21 +01:00
|
|
|
|
2019-12-15 15:56:26 +01:00
|
|
|
DB.save()
|