Skip to content

Instantly share code, notes, and snippets.

@Epivalent
Created July 10, 2024 08:52
Show Gist options
  • Save Epivalent/2722b2563b2abd6f86728be0b4fa5336 to your computer and use it in GitHub Desktop.
Save Epivalent/2722b2563b2abd6f86728be0b4fa5336 to your computer and use it in GitHub Desktop.

In Suricata, a popular open-source network threat detection engine, rules are typically written to match specific patterns in network traffic. While Suricata does support regular expressions (regex) for complex pattern matching, you asked about matching a literal string repeated twice or more without using regex.

Suricata's rule language includes various keywords and options for pattern matching, but it doesn't natively support the concept of repeating strings directly without using regex. However, you can achieve similar functionality by writing multiple content matches within the same rule.

Here's an example of how you might write a Suricata rule to match a literal string "example" that appears at least twice in the payload:

alert tcp any any -> any any (msg:"Example string repeated"; content:"example"; content:"example"; sid:1000001;)

In this rule:

  • alert specifies the action to take when the rule matches.
  • tcp specifies the protocol.
  • any any -> any any specifies that the rule applies to traffic from any source IP/port to any destination IP/port.
  • msg:"Example string repeated" is a message that will be logged when this rule matches.
  • content:"example" is used twice to specify that "example" must appear at least twice in the payload.
  • sid:1000001 is a unique identifier for this rule.

This approach works because each content keyword looks for its specified pattern independently. By including multiple content keywords with the same value, you're effectively requiring that value to appear multiple times in order for the rule to match.

However, note that this method has limitations:

  1. It requires exact positioning if there are constraints on where these strings should appear relative to each other.
  2. It may not be as efficient as using regex for more complex patterns or larger datasets.

For more advanced scenarios where precise control over repetitions and positions is needed, using regex would be more appropriate despite your preference against it.

Wed 10 Jul 10:52:09 CEST 2024 - is it possible in a suricata rule to match on a literal string repeated twice or more without using regex?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment