Parse FortiGate rules into CSV using PowerShell
FortiGate firewalls built by Fortinet provide organizations with a robust next-generation firewall platform to secure networks with Unified Threat Management (UTM) features. I routinely perform audits on FortiGate firewalls. The syntax is not difficult to read, but when a larger device ends up with 100+ rules it can be cumbersome to try and read all of them at once.
tl;dr: the script is on Github.
There are a few things I look for in firewall rules, including:
- Number of rules with ALL allowed in interfaces and/or addresses - Looking through rules with permissive ingress/egress rules helps see where an organization is not properly limiting its traffic. Helping organizations move toward a zero-trust architecture is an important goal.
- Number of rules with ALL allowed in services - While application-aware rules on the FortiGate can help to limit services on rules appropriately, rules with ALL services allowed can be a sign that least functionality principles may not have been considered in a network. The PCI DSS specifically requires documentation of business justification for allowed ports, protocols, and services, and requires insecure protocols to not be used for sensitive or administrative purposes.
- Rules with DNS and NTP egress allowed - Secure DNS architecture in an organization requires that DNS queries are being monitored and logged appropriately. Devices should not be allowed to make external DNS queries without passing through the organization’s internal DNS servers. Organizations need to also ensure that they are synchronizing their devices to the same internal NTP sources in order to make sure that events can be correlated during an incident investigation. DNS and NTP are also being used as methods of command and control for adversaries.
- Number of rules with UTM features enabled - Organizations should ensure they are taking advantage of the full set of features in a FortiGate firewall, including antivirus, application signatures, and intrusion protection (IPS) functions.
- Number of rules with names and comments - Organizations that aren’t properly naming and tracking the rationale for rules being enabled run the risk of having configuration sprawl. Regular review of firewall rules and the justification for these rules existing is important for configuration management. Pointing out the rules without names or comments helps show an organization where they may not have appropriately documented a change they made.
In addition to different items to find in firewall rules, I also wanted to make it easier to filter various config files I started reviewing to determine how the devices are being configured.
Turning the rules in a FortiGate configuration file into a CSV that can be easily reviewed in Excel seemed to be the best way to handle these requirements. As I work primarily in Windows, the preferred scripting language of choice is PowerShell.
FortiGate configs do not easily parse because they are in a text format rather than a markup language like XML or JSON, so the first step to building a config parser is to figure out where the rules start and then start looking at each subsequent rule for patterns. A small rule set in a FortiGate config file might look like:
So within the rule, there are several items we can use to parse the rule set:
config firewall policyindicates the beginning of the rule set in the config file
- Each rule will start
edit n, where n is the rule number. This can function as the ID of a rule in our CSV.
- Each rule property will start
setand define some property (IP addresses, service, etc.)
- Each rule will end
nextand we can then look at the next rule
- The rule set stops at
One thing I noticed is that some rules will not define all properties. Some may have default actions, and other versions of FortiOS may not support a particular feature. To accommodate this, I have built a dummy rule that includes all the properties I could find. If a rule set is parsed that doesn’t begin with all the properties, then only those properties in the first rule would be defined in the CSV and the rest would be dropped. I attempted to build custom PowerShell objects to handle this, but had difficulty and ended up creating a standalone text file to be added into the rule during parsing.
The script would then do the following:
- Load the config
- Identify the VDOM it was reviewing
- Find the beginning of the VDOM rule set
- Add a dummy rule to the beginning of the rule set
- Parse through the rules in the rule set
- Create a CSV for each VDOM.
PowerShell script is on Github
To run from command line:
This would create a csv of the config.conf rules in c:\temp
To quickly run through multiple config files:
Any questions or comments feel free to create an issue on Github or tweet @drewhjelm.