Last active
August 26, 2024 10:06
-
-
Save NiceRath/54ead58ae29bd67e680edc1767578e06 to your computer and use it in GitHub Desktop.
OPNSense - Backup Rules to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from csv import DictWriter | |
import xml.etree.ElementTree as ET | |
# reads unencrypted OPNSense backup file and extracts its rules in CSV format | |
FILE_IN = 'firewall.xml' | |
FILE_OUT = 'firewall.csv' | |
FIELDS = [ | |
'uuid', 'type', 'interface', 'ipprotocol', 'statetype', 'descr', 'direction', 'floating', 'log', 'quick', | |
'protocol', 'source', 'destination', 'category', 'disabled', 'gateway', 'icmptype', 'associated-rule-id', | |
'updated', 'created', | |
] | |
tree = ET.parse(FILE_IN) | |
rules = [] | |
for raw in tree.getroot().find('filter'): | |
if 'uuid' not in raw.attrib: | |
continue | |
rules.append({'uuid': raw.attrib['uuid'], **{cnf.tag: cnf.text.strip() for cnf in raw}}) | |
print('RULE COUNT:', len(rules)) | |
with open(FILE_OUT, 'w', encoding='utf-8') as target: | |
writer = DictWriter(target, fieldnames=FIELDS) | |
writer.writeheader() | |
for entry in rules: | |
writer.writerow(entry) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment