idstools.rule module

Module for parsing Snort-like rules.

Parsing is done using regular expressions and the job of this module is to do its best at parsing out fields of interest from the rule rather than perform a sanity check.

The methods that parse multiple rules for a provided input (parse_file, parse_fileobj) return a list of rules instead of dict keyed by ID as its not the job of this module to detect or deal with duplicate signature IDs.

class idstools.rule.FlowbitResolver[source]

Bases: object

get_required_rules(rulemap, flowbits, include_enabled=False)[source]

Returns a list of rules that need to be enabled in order to satisfy the list of required flowbits.

getters = ['isset', 'isnotset']
set_required_flowbits(rules, required)[source]
setters = ['set', 'setx', 'unset', 'toggle']
class idstools.rule.Rule(enabled=None, action=None, group=None)[source]

Bases: dict

Class representing a rule.

The Rule class is a class that also acts like a dictionary.

Dictionary fields:

  • group: The group the rule belongs to, typically the filename.
  • enabled: True if rule is enabled (uncommented), False is disabled (commented)
  • action: The action of the rule (alert, pass, etc) as a string
  • direction: The direction string of the rule.
  • gid: The gid of the rule as an integer
  • sid: The sid of the rule as an integer
  • rev: The revision of the rule as an integer
  • msg: The rule message as a string
  • flowbits: List of flowbit options in the rule
  • metadata: Metadata values as a list
  • references: References as a list
  • classtype: The classification type
  • priority: The rule priority, 0 if not provided
  • raw: The raw rule as read from the file or buffer
  • enabled – Optional parameter to set the enabled state of the rule
  • action – Optional parameter to set the action of the rule
  • group – Optional parameter to set the group (filename) of the rule

A brief description of the rule.

Returns:A brief description of the rule
Return type:string

The ID of the rule.

Returns:A tuple (gid, sid) representing the ID of the rule
Return type:A tuple of 2 ints

Return the gid and sid of the rule as a string formatted like: ‘[GID:SID]’


Rebuild the rule options from the list of options.

idstools.rule.add_option(rule, name, value, index=None)[source]

Helper function to resolve flowbits, wrapping the FlowbitResolver class.


Find the end of an option (;) handling escapes.


Format a rule as a entry.


Format a rule as a v2 entry.

eg: gid || sid || rev || classification || priority || msg || ref0 || refN

idstools.rule.parse(buf, group=None)[source]

Parse a single rule for a string buffer.

Parameters:buf – A string buffer containing a single Snort-like rule
Returns:An instance of of Rule representing the parsed rule
idstools.rule.parse_file(filename, group=None)[source]

Parse multiple rules from the provided filename.

Parameters:filename – Name of file to parse rules from
Returns:A list of Rule instances, one for each rule parsed
idstools.rule.parse_fileobj(fileobj, group=None)[source]

Parse multiple rules from a file like object.

Note: At this time rules must exist on one line.

Parameters:fileobj – A file like object to parse rules from.
Returns:A list of Rule instances, one for each rule parsed
idstools.rule.remove_option(rule, name)[source]