Home > Articles > Security > Network Security

  • Print
  • + Share This
This chapter is from the book

3.5 Rule Headers

As mentioned earlier, a rule header consists of the section of the rule before starting parentheses and has many parts. Let us take a detailed look at different parts used in the rule header, starting with rule actions.

3.5.1 Rule Actions

The action is the first part of a Snort rule. It shows what action will be taken when rule conditions are met. An action is taken only when all of the conditions mentioned in a rule are true. There are five predefined actions. However, you can also define your own actions as needed. As a precaution, keep in mind that Snort versions 1.x and 2.x apply rules in different ways. In Snort 1.x, if multiple rules match a given packet, only the first one is applied. After applying the first rule, no further action is taken on the packet. However in Snort version 2, all rules are applied before generating an alert message. The most severe alert message is then generated. Pass

This action tells Snort to ignore the packet. This action plays an important role in speeding up Snort operation in cases where you don't want to apply checks on certain packets. For example, if you have a vulnerability assessment host on your own network that you use to find possible security holes in your network, you may want Snort to ignore any attacks from that host. The pass rule plays an important part in such a case. Log

The log action is used to log a packet. Packets can be logged in different ways, as discussed later in this book. For example, a message can be logged to log files or in a database. Packets can be logged with different levels of detail depending on the command line arguments and configuration file. To find available command line arguments with your version of Snort, use “snort -?” command. Alert

The alert action is used to send an alert message when rule conditions are true for a particular packet. An alert can be sent in multiple ways. For example, you can send an alert to a file or to a console. The functional difference between Log and Alert actions is that Alert actions send an alert message and then log the packet. The Log action only logs the packet. Activate

The activate action is used to create an alert and then to activate another rule for checking more conditions. Dynamic rules, as explained next, are used for this purpose. The activate action is used when you need further testing of a captured packet. Dynamic

Dynamic action rules are invoked by other rules using the “activate” action. In normal circumstances, they are not applied on a packet. A dynamic rule can be activated only by an “activate” action defined in another role. User Defined Actions

In addition to these actions, you can define your own actions. These rule actions can be used for different purposes, such as:

  • Sending messages to syslog. Syslog is system logger daemon and creates log file in /var/log directory. Location of these files can be changed using /etc/syslog.conf file. For more information, use “man syslog” and “man syslog.conf” commands on a UNIX system. Syslog may be compared to the event logger on Microsoft Windows systems.

  • Sending SNMP traps. SNMP traps are sent to a network management system like HP OpenView or Open NMS at http://www.opennms.org.

  • Taking multiple actions on a packet. As you have seen earlier in the structure of Snort rules, a rule only takes one action. User defined rules can be used to take multiple actions. For example, a user defined rule can be used to send an SNMP trap as well as to log the alert data to the syslog daemon.

  • Logging data to XML files.

Logging messages into a database. Snort is able to log messages to MySQL, Postgress SQL, Oracle and Microsoft SQL server.

These new action types are defined in the configuration file snort.conf. A new action is defined in the following general structure:

ruletype action_name
     action definition

The ruletype keyword is followed by the action name. Two braces enclose the actual definition of the action, just like a function in C programming. For example, an action named smb_db_alert that is used to send SMB pop-up window alert messages to hosts listed in workstation.list file and to MySQL database named “snort” is defined below:

ruletype smb_db_alert
   type alert
   output alert_smb: workstation.list
   output database: log, mysql, user=rr password=rr      dbname=snort host=localhost

Theses types of rules will be discussed in the next chapter in detail. Usually they are related to configuration of output plug-ins.

3.5.2 Protocols

Protocol is the second part of a Snort rule. The protocol part of a Snort rule shows on which type of packet the rule will be applied. Currently Snort understands the following protocols:

  • IP

  • ICMP

  • TCP

  • UDP

If the protocol is IP, Snort checks the link layer header to determine the packet type. If any other type of protocol is used, Snort uses the IP header to determine the protocol type. Different packet headers are discussed in Appendix C.

The protocols only play a role in specifying criteria in the header part of the rule. The options part of the rule can have additional criteria unrelated to the specified protocol. For example, consider the following rule where the protocol is ICMP.

alert icmp any any -> any any (msg: "Ping with TTL=100";  ttl: 100;)

The options part checks the TTL (Time To Live) value, which is not part of the ICMP header. TTL is part of IP header instead. This means that the options part can check parameters in other protocol fields as well. Header fields for common protocols and their explanation is found in Appendix C.

3.5.3 Address

There are two address parts in a Snort rule. These addresses are used to check the source from which the packet originated and the destination of the packet. The address may be a single IP address or a network address. You can use any keyword to apply a rule on all addresses. The address is followed by a slash character and number of bits in the netmask. For example, an address represents C class network with 24 bits in the network mask. A network mask with 24 bits is Keep the following in mind about number of bits in the netmask:

  • If the netmask consists of 24 bits, it is a C class network.

  • If the netmask consists of 16 bits, it is a B class network.

  • If the netmask consists of 8 bits, it is an A class network.

  • For a single host, use 32 bits in the netmask field.

You can also use any number of bits in the address part allowed by Classless Inter-Domain Routing or CIDR. Refer to RFC 791 at http://www.rfc-editor.org/rfc/rfc791.txt for structure of IP addresses and netmasks and to RFC 1519 at http://www.rfc-editor.org/rfc/rfc1519.txt for more information on CIDR.

As mentioned earlier, there are two address fields in the Snort rule. One of them is the source address and the other one is the destination address. The direction part of the rule determines which address is source and which one is destination. Refer to the explanation of the direction part to find more information about how this selection is made.

Following are some examples of how addresses are mentioned in Snort rules:

  • An address defines a single host with IP address

  • An address defines a class C network with addresses ranging from to There are 24 bits in the netmask, which is equal to

  • An address defines a class B network with addresses ranging from to There are 16 bits in the netmask, which is equal to

  • An address defines a class A network with addresses ranging from to There are 8 bits in the netmask, which is equal to

  • An address defines an address range of to There are 28 bits in the netmask field, which is equal to, and the network consists of 16 addresses. You can place only 14 hosts in this type of network because two of the total 16 addresses are used up in defining the network address and the broadcast address. Note that the first address in each network is always the network address and the last address is the broadcast address. For this network is the network address and is the broadcast address.

For example, if you want to generate alerts for all TCP packets with TTL=100 going to web server at port 80 from any source, you can use the following rule:

alert tcp any any -> 80 (msg: "TTL=100";  ttl: 100;)

This rule is just an example to provide information about how IP addresses are used in Snort rules. Address Exclusion

Snort provides a mechanism to exclude addresses by the use of the negation symbol !, an exclamation point. This symbol is used with the address to direct Snort not to test packets coming from or going to that address. For example, the following rule is applied to all packets except those that originate from class C network

alert icmp ![] any -> any any    (msg: "Ping with TTL=100";  ttl: 100;)

This rule is useful, for instance, when you want to test packets that don't originate from your home network (which means you trust everyone in your home network!). Address Lists

You can also specify list of addresses in a Snort rule. For example, if your home network consists of two C class IP networks and and you want to apply the above rule to all addresses but hosts in these two, you can use the following modified rule where the two addresses are separated by a comma.

alert icmp ![,] any -> any     any (msg: "Ping with TTL=100";  ttl: 100;)

Note that a square bracket is used with the negation symbol. You don't need to use brackets if you are not using the negation symbol.

3.5.4 Port Number

The port number is used to apply a rule on packets that originate from or go to a particular port or a range of ports. For example, you can use source port number 23 to apply a rule to those packets that originate from a Telnet server. You can use the keyword any to apply the rule on all packets irrespective of the port number. Port number is meaningful only for TCP and UDP protocols. If you have selected IP or ICMP as the protocol in the rule, port number does not play any role. The following rule is applied to all packets that originate from a Telnet server in, which is a class C network and contains the word “confidential”:

alert tcp 23 -> any any     (content: "confidential"; msg: "Detected confidential";)

The same rule can be applied to traffic either going to or originating from any Telnet server in the network by modifying the direction to either side as shown below:

alert tcp 23 <> any any     (content: "confidential"; msg: "Detected confidential";)

Port numbers are useful when you want to apply a rule only for a particular type of data packet. For example, if a vulnerability is related to only a HTTP (Hyper Text Transfer Protocol) web server, you can use port 80 in the rule to detect anybody trying to exploit it. This way Snort will apply that rule only to web server traffic and not to any other TCP packets. Writing good rules always improves the performance of IDS. Port Ranges

You can also use a range of ports instead of only one port in the port field. Use a colon to separate starting and ending port numbers. For example, the following rule will create an alert for all UDP traffic coming from ports 1024 to 2048 from all hosts.

alert udp any 1024:2048 -> any any (msg: "UDP ports";) Upper and Lower Boundaries

While listing port numbers, you can also use only the starting port number or the ending port number in the range. For example, a range specified as :1024 includes all port numbers up to and including port 1024. A port range specified as 1000: will include all ports numbers including and above port 1000. Negation Symbol

As with addresses, you can also use the negation symbol with port numbers to exclude a port or a range of ports from the scope of the Snort rule. The following rule logs all UDP traffic except for source port number 53.

log udp any !53 -> any any log udp

You can't use comma character in the port filed to specify multiple ports. For example, specifying 53,54 is not allowed. However you can use 53:54 to specify a port range. Well-Known Port Numbers

Well-known port numbers are used for commonly used applications. Some of these port numbers and their applications are listed in Table 3-1.

Table 3-1. Well-Known Port Numbers

Port Number



FTP data




SSH or Secure shell




SMTP, used for e-mail server like Sendmail


NTP (Network Time Protocol) used for synchronizing time on network hosts


DNS server


BootP/DHCP client


BootP/DHCP server




HTTP, used for all web servers


POP3, used for e-mail clients like Microsoft Outlook




SNMP traps


HTTPS or Secure HTTP





You can also look into /etc/services file on the UNIX platform to see more port numbers. Refer to RFC 1700 for a detailed list at http://www.rfc-editor.org/rfc/rfc1700.txt. The Internet Corporation for Assigned Names and Numbers (ICANN) now keeps track of all port numbers and names. You can find more information at http://www.icann.org.

3.5.5 Direction

The direction field determines the source and destination addresses and port numbers in a rule. The following rules apply to the direction field:

  • A -> symbol shows that address and port numbers on the left hand side of the direction field are the source of the packet while the address and port number on the right hand side of the field are the destination.

  • A <- symbol in the direction field shows that the packet is traveling from the address and port number on the right hand side of the symbol to the address and port number on the left hand side.

  • A <> symbol shows that the rule will be applied to packets traveling on either direction. This symbol is useful when you want to monitor data packets for both client and server. For example, using this symbol, you can monitor all traffic coming from and going to a POP or Telnet server.

  • + Share This
  • 🔖 Save To Your Account