Remove MikroTik block list script and enhance scan.py for firewall synchronization
- Deleted the existing MikroTik block list script (mikrotik-block.rsc) which contained whitelisted and blocked IP addresses. - Updated scan.py to include options for synchronizing the MikroTik blocked list after scanning. - Added functionality to preview firewall changes without applying them. - Integrated the MikrotikFirewall class to manage the synchronization of blocked IPs with the MikroTik device.
This commit is contained in:
1 parent
0f51c40e44
commit
0e2c69579a
5 files changed
+389
-621
No files matched your search
@@ -12,6 +12,7 @@ import os
|
||||
from datetime import datetime
|
||||
|
||||
import utils
|
||||
from firewall import MikrotikFirewall
|
||||
from copyparty import CopypartyAnalyzer, DEFAULT_LOG_PATH as CP_DEFAULT_LOG, DEFAULT_REPORT_DIR as CP_DEFAULT_REPORT
|
||||
from npm import NpmAnalyzer, DEFAULT_LOG_PATH as NPM_DEFAULT_LOG, DEFAULT_REPORT_DIR as NPM_DEFAULT_REPORT
|
||||
|
||||
@@ -49,8 +50,10 @@ def main() -> None:
|
||||
help="rows in the top-IP tables (default: 30)")
|
||||
parser.add_argument("--quiet", action="store_true",
|
||||
help="write files, suppress console reports")
|
||||
parser.add_argument("--no-rsc", action="store_true",
|
||||
help="skip MikroTik consolidated .rsc generation")
|
||||
parser.add_argument("--update-firewall", action="store_true",
|
||||
help="synchronize the MikroTik Blocked list after scanning")
|
||||
parser.add_argument("--dry-run", action="store_true",
|
||||
help="preview firewall changes without applying them")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
@@ -63,7 +66,7 @@ def main() -> None:
|
||||
report_dir=args.copyparty_report_dir,
|
||||
top=args.top,
|
||||
quiet=args.quiet,
|
||||
write_rsc=not args.no_rsc,
|
||||
write_rsc=False,
|
||||
min_denied=args.min_denied,
|
||||
)
|
||||
|
||||
@@ -72,7 +75,7 @@ def main() -> None:
|
||||
report_dir=args.npm_report_dir,
|
||||
top=args.top,
|
||||
quiet=args.quiet,
|
||||
write_rsc=not args.no_rsc,
|
||||
write_rsc=False,
|
||||
min_errors=args.min_errors,
|
||||
error_ratio=args.error_ratio,
|
||||
)
|
||||
@@ -114,6 +117,7 @@ def main() -> None:
|
||||
print("\nGrouping blocked IPs into subnets using WHOIS...")
|
||||
final_blocked_targets = utils.consolidate_by_subnet(
|
||||
final_blocked_ips, threshold=3)
|
||||
blocked_ipv4, blocked_ipv6 = utils.split_versions(final_blocked_targets)
|
||||
|
||||
print("\n=== Consolidated Summary ===")
|
||||
print(
|
||||
@@ -123,19 +127,20 @@ def main() -> None:
|
||||
print(f"Total unified blocked IPs: {len(final_blocked_ips)}")
|
||||
print(f"Total target blocks/subnets: {len(final_blocked_targets)}")
|
||||
|
||||
# 5. Write Consolidated MikroTik RouterOS Script
|
||||
if not args.no_rsc:
|
||||
rsc_path = os.path.join(args.report_dir, "mikrotik-block.rsc")
|
||||
utils.ensure_dirs(args.report_dir)
|
||||
path, n_blocked, n_whitelisted = utils.write_mikrotik(
|
||||
rsc_path,
|
||||
"ids",
|
||||
final_blocked_targets
|
||||
)
|
||||
print(f"\nConsolidated MikroTik script: {path}")
|
||||
print(
|
||||
f" {n_blocked} targets blocked, {n_whitelisted} CIDR ranges whitelisted")
|
||||
|
||||
if args.update_firewall:
|
||||
firewall = MikrotikFirewall()
|
||||
firewall.connect()
|
||||
try:
|
||||
changes = firewall.sync_blocklist(
|
||||
blocked_ipv4, blocked_ipv6, dry_run=args.dry_run,
|
||||
report_dir=args.report_dir)
|
||||
finally:
|
||||
firewall.disconnect()
|
||||
action = "Would" if args.dry_run else ""
|
||||
print(f"{action} add {len(changes['added_ipv4'])} IPv4 and "
|
||||
f"{len(changes['added_ipv6'])} IPv6 addresses; "
|
||||
f"remove {len(changes['removed_ipv4'])} IPv4 and "
|
||||
f"{len(changes['removed_ipv6'])} IPv6 addresses.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user