Hack #110. Write Your Own Snort Rules

Customize Snort for your own needs quickly and easily by leveraging its flexible rule engine and language.

One of the best features of Snort [Hack #106] is its rule engine and language. Snort’s rule engine provides an extensive language that enables you to write your own rules, allowing you to extend it to meet the needs of your network.

A Snort rule can be broken down into two basic parts: the rule header and options for the rule. The rule header contains the action to perform, the protocol that the rule applies to, and the source and destination addresses and ports. The rule options allow you to create a descriptive message to associate with the rule, as well as check a variety of other packet attributes by making use of Snort’s extensive library of plug-ins.

Here’s the general form of a Snort rule:

               action 
               proto 
               src_ip 
               src_port 
               direction 
               dst_ip 
               dst_port (options)

When a packet comes in, its source and destination IP addresses and ports are compared to the rules in the ruleset. If any of the rules is applicable to the packet, its options are then compared to the packet. If all of these comparisons return a match, the specified action is taken.

Snort provides several built-in actions you can use when crafting your rules. To simply log the packet that matches a rule, use the log action. The alert action generates an alert using the method specified in your configuration file or on the command line, in addition to logging the packet.

One nice feature is that you can establish very general rules and then create exceptions by writing rules that use the pass action. This works especially well when you are using the rules distributed with Snort but are getting frequent false positives for some of them. If it’s not a security risk to ignore them, you can simply write pass rules that will exclude the packets in question.

The last two built-in rule actions, activate and dynamic, are used together to dynamically modify Snort’s ruleset at runtime. Rules that use the dynamic action are just like log rules, except they will be considered only after they have been enabled by an activate rule. To determine what dynamic rules to enable once an activate rule has been triggered, Snort enforces the use of the activates and activated_by rule options. In addition, dynamic rules are required to specify a count option so that Snort can limit how many packets the rules will record.

For instance, if you want to start recording packets after an exploit of an SSH daemon on 192.168.1.21 has been noticed, use a couple of rules similar to these:

activate tcp any any -> 192.168.1.21 22 (content:"/bin/sh"; activates:1; \ 
  msg:"Possible SSH buffer overflow"; )
dynamic tcp any any -> 192.168.1.21 22 (activated_by:1; count:100;)

These two rules aren’t completely foolproof, but if someone were to run an exploit with shell code against an SSH daemon, it would most likely send the string /bin/sh in the clear in order to spawn a shell on the system being attacked.

In addition, since SSH is encrypted, strings like that wouldn’t be sent to the daemon under normal circumstances. Once the first rule is triggered, it will activate the second one, which will record 100 packets and then stop. This is useful, since you might be able to catch the intruder downloading or installing a rootkit within those first few packets, and recording them will help you to analyze the compromised system much more quickly.

You can also define custom rule actions, in addition to Snort’s built-in actions. This is done with the ruletype keyword:

ruletype redalert
{
    type alert
    output alert_syslog: LOG_AUTH LOG_ALERT
    output database: log, mysql, user=snort dbname=snort host=localhost
}

This custom rule action tells Snort that it behaves like the alert rule action, but specifies that the alerts should be sent to the syslog daemon, while the packets will be logged to a database. When defining a custom action, you can use any of Snort’s output plug-ins, just as you would if you were configuring them as your primary output method.

The next part of the rule includes the options. This part lets you specify many other attributes to check against. Each option is implemented through a Snort plug-in. When a rule that specifies an option is triggered, Snort runs through the option’s corresponding plug-in to perform the check against the packet. Snort has over 40 plug-ins—too many to cover in detail in this hack—but we will look at some of the more useful ones.

Another useful option is content , which allows you to search a packet for a sequence of characters or hexadecimal values. If you are searching for a string, you can just put it in quotes; to specify a case-insensitive search, add nocase; to the end of all your options. If you are looking for a sequence of hexadecimal digits, you must enclose them in | characters. For example, this rule will trigger when the digit 0x90 is spotted in a packet’s data payload:

alert tcp any any -> any any (msg:"Possible  exploit"; content:"|90|";)

This digit is the hexadecimal equivalent of the NOP instruction on the x86 architecture and is often seen in exploit code because it can be used to make buffer overflow exploits easier to write.

The offset and depth options can be used in conjunction with the content option to limit the searched portion of the data payload to a specific range of bytes. For example, if you want to limit content matches for NOP instructions to between bytes 40 and 75 of the data portion of a packet, you can modify the previously shown rule to look like this:

alert tcp any any -> any any (msg:"Possible  exploit"; content:"|90|"; \
  offset:40; depth:75;)

You can also match against packets that do not contain the specified sequence by prefixing it with a !.

Another thing you might want to check is the size of a packet’s data payload. Many shell code payloads can be large compared to the normal amount of data carried in a packet sent to a particular service. You can check the size of a packet’s data payload by using the dsize option. This option takes a number as an argument. In addition, you can specify an upper bound by using the < operator, or you can choose a lower bound by using the > operator. Upper and lower bounds can be expressed with <>. For example, the following line modifies the previous rule to match only if the data payload’s size is greater than 6000 bytes, in addition to the other options criteria:

alert tcp any any -> any any (msg:"Possible  exploit"; content:"|90|"; \
  offset:40; depth:75; dsize: >6000;)

In practice, you might find that some of your rules are a bit noisy and trigger alerts too often to be useful. A way to overcome this is to use Snort’s thresholding feature. This feature allows you to specify a minimum number of times a rule needs to be matched for a particular IP address before it actually generates an alert, or limit the number of alerts a rule can generate during an arbitrary interval of time.

You can use thresholding in two different ways.

You can specify a threshold for a rule separately by referencing its ID. Threshold statements take the following form and are usually put in threshold.conf (located in the same directory as your snort.conf):

threshold gen_id <generator ID>, sig_id <signature ID>, \ 
  type <limit | threshold | both>, \
  track <by_src | by_dest>, count <n>, seconds <m>
               

The <generator ID> is the portion of Snort that generates the alert you want to threshold. This is used to track which preprocessor an alert came from. Since all alerts for signatures are generated by Snort’s signature engine, this should always be set to 1. The <signature ID> corresponds to the signature’s ID. This, of course, means that you’ll need to specify IDs when writing your own rules. This is done with the sid option. You’ll also want to specify the rule’s revision with the rev option.

For example, here’s a rule that we looked at before, but with sid and rev options added:

alert tcp any any -> any any (msg:"Possible  exploit"; content:"|90|"; \
  offset:40; depth:75; dsize: >6000; sid:1000001; rev:1;)

Note that only signature IDs greater than one million can be used for local rules.

When specifying thresholds, you can choose between three types. limit thresholds cause an alert to be generated up until the signature has been matched a set number of times (specified with the count option) during the chosen interval (specified with the seconds parameter). The threshold type is used to prevent an alert from being generated unless the signature has been matched at least count times during the specified time interval. Specifying both as the type produces a mixture of the two techniques: it will cause one alert to be generated only after count has been reached during the specified time period. To indicate whether the thresholding should apply to the source or destination IP address, use by_src or by_dest, respectively.

If you find that a rule is still too noisy, you can disable it either altogether or for specific IP addresses by using Snort’s suppression feature. Suppression statements take the following form and are usually also kept in threshold.conf:

suppress gen_id <generator ID>, sig_id <sid>, [track <by_src | by_dest>, ip[/mask]]

The IDs are specified just like in a threshold statement; however, the track and ip parameters can be omitted to completely suppress alerts generated by the signature. Use them if you want to limit the suppression to a specific source or destination IP address (or range of addresses). Ranges of IP addresses and networks can be entered in CIDR format.

One of the best features of Snort is that it provides many plug-ins that can be used in the options field of a rule. The options discussed here should get you off to a good start. However, if you want to write more complex rules, consult Snort’s excellent rule documentation, which contains full descriptions and examples for each of Snort’s rule options. The Snort User’s Manual is available at http://www.snort.org/docs/writing_rules/.