Effectively Using Suricata

LAB 5

Scenario

The organization you work for is considering deploying Suricata to enhance its traffic inspection capabilities. The IT Security manager provided you with some Suricata rules to get familiar with their structure. He also provided you with PCAP files containing malicious traffic so that you can experiment with writing Suricata detection rules.

A test instance of Suricata has already been set up and is waiting for you!

Learning Objectives

The learning objective of this lab is not only to get familiar with the detection capabilities of Suricata but also to learn effective rule writing of Suricata rules.

Specifically, you will learn how to use Suricata's capabilities in order to:

Introduction To Suricata Rules

What is a Suricata rule

Rules are essentially instructions to the Suricata engine to look for specific markers in network traffic that we want to be notified about if they are seen.

Rules are not always oriented towards detecting malicious traffic. Rules can also be written to provide blue team members with actionable and/or contextual information regarding activity that is occurring on a network.

Rules can be as specific or as broad as we want them to be. Finding the perfect balance is important in order, for example, to be able to detect variations of a given malware but also avoid false positives. They can be seen as a big AND statement (multiple contents are specified, and the rule will trigger an alert if and only if all of these contents are seen in the passing traffic).

Threat intelligence and infosec communities usually offer critical information based on which rules are developed.

Each active rule consumes some of the host's CPU and memory. Specific guidelines exist to assist effective Suricata rule writing.

action protocol from_ip port -> to_ip port (msg:"we are under attack by X"; content:"something"; content:"something else"; sid:10000000; rev:1;)

Header (action - port part): This rule portion includes what action we want the rule to have along with the protocol Suricata should expect this rule to be found in. It also includes IPs and ports, as well as an arrow indicating directionality.

Rule message & Contents [(msg -else";) part]: The rule message is the message we want the analysts (or our self) to be presented with, whereas the contents are the portions of the traffic that we have deemed critical in order to detect activity that we want to be informed of.

Rule metadata [(sid - 1;) part]: This rule portion mainly helps to keep track of rule modifications. Sid is a unique rule identifier.

Let's dive into the Header.

action tells the Suricata engine what to do should the contents are matched. It can be:

protocol can be: tcp, udp, icmp, ip, http, tls, smb, dns

Then, we need a way to declare the directionality of the traffic; this can be done through:

Rule Ports declare the port(s) in which traffic for this rule will be evaluated. Examples:

Now, let's suppose you were required to create a header based on the PCAP below.

The rule header should be alert dns $HOME_NET any -> any any

any was used instead of $EXTERNAL_NET because some networks include internal DNS resolvers and you don't want to rule out that traffic.

Let's now dive into Rule message & Contents.

Rule Message. Arbitrary text that appears when the rule is triggered. For better understanding, it would nice if the rule messages you create contain malware architecture, malware family and malware action.

Rule Content. Values that identify a specific network traffic or activity. Suricata matches this unique content in packets for detection. Note that for certain characters, the use of hex is required. Examples:

content:"some thing";

content:"some|20|thing";

content:"User-Agent|3a 20|";

Consider the below rule content.

content:"POST"; content:"/trigger.php"; content:"DetoxCrypto";

content:"publickey";

By using buffers it will be transformed to the below.

content:"POST"; http_method; content:"/trigger.php"; http_uri;

content:"DetoxCrypto"; http_user_agent; content:"publickey"; http_client_body;

Here are some additional examples to better comprehend what we covered so far.

Finally, let's dive into Rule metadata.

Note: To conclude covering Suricata rules, let's talk about PCRE (Pearl Compatible Regular Expression). PCRE is a very powerful ally while developing rules. One can use PCRE through the pcre statement (followed by a regular expression). Note that PCRE must be wrapped in leading and trailing forward slashes and any flags will go after the last slash. Examples:

Note that anchors go after and before wrapped slashes and certain characters need to be escaped with a backslash. Additionally, never write a PCRE-only rule.

Managing Suricata Rules

Recommended tools

SOUTIONS

Below, you can find solutions for every task of this lab. As a reminder, you can follow your own strategy, which may be different from the one explained in the following lab.

Task 1: Analyze the provided Suricata rules and describe what they look for

Let's start with Rule 1.

alert dns $HOME_NET any -> any any (msg:"TROJAN X Rogue DNS Query Observed" dns_query; content:"default27061330-a.akamaihd.net"; isdataat:!1,relative; reference:url,threatintelprovider.com/trojanx; classtype:trojan-activity; sid:1; rev:1;)

Let's continue with Rule 2.

alert tls $EXTERNAL_NET any -> $HOME_NET any (msg:"TROJAN Z malicious SSL Cert"; flow:established,to_client; tls_cert_subject; content:"CN=uniquestring"; classtype:trojan-activity; sid:1; rev:1;)
- This rule specifies the TLS protocol. tls is a Suricata protocol keyword that allows the engine to detect TLS traffic based on the protocol and not the port. By looking further down the rule, we are obviously checking for the delivery of a TLS certificate from an external server, hence the $EXTERNAL_NET any -> $HOME_NET any directionality. Once again, any is set for both ports since the tls keyword is utilized.

Task 2: Analyze the provided PCAP files and develop your own rules (Level: Beginner)

Let's start with Sofacy.pcap

To learn more about Sofacy, refer to the following link https://securelist.com/sofacy-apt-hits-high-profile-targets-with-updated-toolset/72924/. We'll develop a Suricata rule based on our analysis of the Sofacy.pcap file, considering that what we see inside the PCAP is malicious.

By opening Sofacy.pcap in Wireshark, the first thing we notice is two (2) DNS queries and the respective DNS responses.

Let's create a Suricata rule to detect those two (2) C2 domains.

alert dns $HOME_NET any -> any any (msg:"TROJAN Activity Detected DNS Query to Known Sofacy Domain 1"; dns_query; content:"drivres-update.info"; nocase; isdataat:!1,relative; sid:1; rev:1;)
alert dns $HOME_NET any -> any any (msg:"TROJAN Activity Detected DNS Query to Known Sofacy Domain 2"; dns_query; content:"softupdates.info"; nocase; isdataat:!1,relative; sid:2; rev:1;)

Put these two (2) rules inside the local.rules file residing in the /etc/suricata/rules directory. You can do so, by executing-

nano /etc/suricata/rules/local.rules

On the machine's Desktop, you will find a bash script (automate_suricata.sh), by OISF's J Williams, that will automate running Suricata with the local.rules file, logging in a different location (/tmp/suricata), echoing the contents of fast.log and also cleaning previous logs before each new Suricata execution occurs.

Now before running "automate_suricata.sh" script, make sure to check whether the following path: /etc/suricata/rules/local.rules is added in suricata.yaml configuration file or not, as shown in the below image.

Now, it's time to test the rule above. You can do so, as follows.

cd Desktop
./automate_suricata.sh ./PCAPs/Sofacy.pcap

You should see the following.


Let's continue with the Citi.pcap file. If you are unfamiliar with Citi, Citi is a global bank.

By opening the Citi.pcap in Wireshark, the first thing we notice is a phishy-looking DNS query (note that online.citi.com is a legitimate URL).

Let's create a Suricata rule to detect such phishing-related DNS traffic against online.citi.com.

alert dns $HOME_NET any -> any any (msg:"Possible Citi Phishing Attempt Observed in DNS Query "; dns_query; content:"online.citi.com"; nocase; isdataat:1,relative; sid:3; rev:1;)
isdataat:1,relative will inform us if any data exist after online.citi.com. There shouldn't be any data after it. If data are identified after online.citi.com, then, we are dealing with a phishing URL, similar to the one you can see in the PCAP file above (inside the red rectangle)

It's time to test the rule above. You can do so, as follows.

cd Desktop
./automate_suricata.sh ./PCAPs/Citi.pcap
You should see the following.


Now, it's time to analyze the Qadars.pcap file.

To learn more about Qadars, refer to the following links:

We'll develop a Suricata rule based on our analysis of the Qadars.pcap file, considering that what we see inside the PCAP is malicious.

By opening Qadars.pcap in Wireshark, the first thing we notice is a DNS query and a DNS response regarding susana24.com. Then, we notice TLS traffic initiating.

Let's focus on the TLS certificate this time and try to create a rule based on the CN portion of it (susana24.com).

alert tls $EXTERNAL_NET any -> $HOME_NET any (msg:"TROJAN Activity Observed Malicious SSL Cert (Qadars CnC)"; flow:established,to_client; tls_cert_subject; content:"CN=susana24.com"; classtype:trojan-activity; sid:4; rev:1;)
It's time to test the rule above. You can do so, as follows.

cd Desktop
./automate_suricata.sh ./PCAPs/Qadars.pcap
You should see the following.


Now, it's time to analyze the 7ev3n.pcap

To learn more about the 7ev3n ransomware, refer to the following link https://www.vmray.com/cyber-security-blog/7ev3n-honet-ransomware-rest-us/.

We'll develop a Suricata rule based on our analysis of the 7ev3n.pcap file, considering that what we see inside the PCAP is malicious.

By opening 7ev3n.pcap in Wireshark and filtering so that we can see HTTP traffic only, the first thing we notice is this curious-looking request.

Let's follow the whole stream.

The requests above can provide us with a lot of clues on how to develop an effective Suricata rule.

Viable rules can be found below.

alert http $HOME_NET any -> $EXTERNAL_NET any (msg:" TROJAN 7ev3n Ransomware CnC Checkin"; flow:established,to_server; content:"GET"; http_method; content:".php?RIGHTS="; http_uri; content:"&WIN="; http_uri; distance:0; content:"&WALLET="; http_uri; distance:0; content:"&ID="; http_uri; distance:0; content:"&UI="; http_uri; distance:0; content:"Internet|20|Explorer"; http_user_agent; depth:17; isdataat:!1,relative; http_header_names; content:!"Referer"; content:!"Accept"; sid:5; rev:1;)
alert http $HOME_NET any -> $EXTERNAL_NET any (msg:" TROJAN 7ev3n Ransomware Encryption Activity"; flow:established,to_server; content:"GET"; http_method; content:".php?SSTART="; http_uri; content:"&CRYPTED_DATA="; http_uri; distance:0; content:"&ID="; http_uri; distance:0; content:"&FILES="; http_uri; distance:0; content:"&UI="; http_uri; distance:0; content:"Internet|20|Explorer"; http_user_agent; depth:17; isdataat:!1,relative; http_header_names; content:!"Referer"; content:!"Accept"; sid:6; rev:1;)
depth:17; isdataat:!1,relative; is looking to see if there are any data after the last "r" of the "Internet Explorer" string, ensuring that the User Agent field only contains "Internet Explorer". http_header_names; content:!"Referer"; content:!"Accept"; is leveraging the lack of usual headers for detection purposes.

It's time to test the rule above. You can do so, as follows.

cd Desktop
./automate_suricata.sh ./PCAPs/7ev3n.pcap
You should see the following.


Finally, let's analyze the Malicious_document.pcap

Typically, malicious Office documents rely on macro execution, embedded objects, or exploits to deliver a payload onto the victim machine. In such cases, the URI can be so characteristic as to be used as solid rule content. The same applies for the User-Agent.

By opening the Malicious_document.pcap in Wireshark, the first thing we notice is this curious-looking HTTP request.

The request above can provide us with a lot of clues on how to develop an effective Suricata rule. A viable rule can be found below.

alert http $HOME_NET any -> $EXTERNAL_NET any (msg:"Malicious Document Retrieving Payload"; flow:established,to_server; content:".exe?q="; fast_pattern; content:"Microsoft BITS/"; http_user_agent; depth:15; pcre:"/\.exe\?q=[0-9]{3,5}$/U"; http_header_names; content:!"Referer"; sid:7; rev:1;)

flow:established,to_server; is used since we are dealing with TCP and the directionality is towards the malicious server. We won't focus on the curious-looking HEAD HTTP method so that we can catch variations. fast_pattern; is used so that the Suricata engine "focuses more" on the .exe?q= content ('User-Agent:' will be a match very often, but .exe?q= appears less often in network traffic). pcre:"/\.exe\?q=[0-9]{3,5}$/U"; means the sensor should match every time it observes three to five numbers after q=. Finally, we are once again leveraging the lack of usual headers for detection purposes.

It's time to test the rule above. You can do so, as follows.

cd Desktop
./automate_suricata.sh ./PCAPs/Malicious_document.pcap
You should see the following.

Task 3: Analyze the provided PCAP files and develop your own rules (Level: Intermediate)

Let's start with DDoSClient.pcap

We'll develop a Suricata rule based on our analysis of the DDoSClient.pcap file, considering that what we see inside the PCAP is malicious.

By opening DDoSClient.pcap in Wireshark, the first thing we notice is some curious-looking TCP traffic, containing information such as OS, CPU MHZ, and CPU Architecture.

The traffic above can provide us with enough clues on how to develop an effective Suricata rule. A viable rule can be found below.

alert tcp $HOME_NET any -> $EXTERNAL_NET any (msg:"ET TROJAN DDoS Client Information Checkin"; flow:established,to_server; content:"Windows"; nocase; depth:7; content:"MHZ|00 00 00 00 00 00|"; distance:0; nocase; content:"|00 00 00 00 00 00|Win"; distance:0; nocase; classtype:trojan-activity; sid:8; rev:1;)

Notice that we are using "Windows" and "MHZ" instead of "Windows 7" and "1795 MHZ", to detect variations. depth:7; is used because "Windows" is seven character's long. The first distance:0; is used because the nulls appear right after "Windows" (the number of nulls in the rule was chosen randomly).

It's time to test the rule above. You can do so, as follows.

cd Desktop
./automate_suricata.sh ./PCAPs/DDoSClient.pcap
You should see the following.


Now, it's time to analyze the Adobe.pcap

We'll develop a Suricata rule based on our analysis of the Adobe.pcap, considering that what we see inside the PCAP is malicious.

By opening Adobe.pcap in Wireshark and filtering so that we can see HTTP traffic only, the first thing we notice is this curious-looking POST request. Let's follow the whole stream.

The request above can provide us with a lot of clues on how to develop an effective Suricata rule. Try creating one on your own...

There was another curious-looking HTTP, the following one.

What is suspicious about the portion inside the red rectangle above, is the way in which it redirects the client elsewhere. Typically, you would see either <META HTTP-EQUIV="refresh" or redirection through JavaScript, not both at the same time.

This is quite uncommon, so let's make a rule out of it. A viable rule can be found below.

alert http $EXTERNAL_NET any -> $HOME_NET any (msg:"Adobe Phising Attempt"; flow:established,to_client; content:"200"; http_stat_code; http_content_type; content:"text/html"; nocase; file_data; content:"<META HTTP-EQUIV="; nocase; within:100; content:"refresh"; distance:1; nocase; within:7; content:"self.location.replace"; nocase; distance:0; content:"window.location"; nocase; distance:0; classtype:bad-unknown; sid:9; rev:1;)

file_data; is a buffer including what will be rendered in the browser. within:100 means we want to see the content within the first 100 bytes. The remaining part is easy to comprehend.

It's time to test the rule above. You can do so, as follows.

cd Desktop
./automate_suricata.sh ./PCAPs/Adobe.pcap
You should see the following.

Suricata Resources:

  1. https://www.stamus-networks.com/open-source/

  2. https://resources.sei.cmu.edu/asset_files/Presentation/2016_017_001_449890.pdf

  3. https://blog.inliniac.net/2014/04/08/detecting-openssl-heartbleed-with-suricata/

  4. https://www.trustwave.com/en-us/resources/blogs/spiderlabs-blog/decoding-hancitor-malware-with-suricata-and-lua/

  5. https://www.trustwave.com/en-us/resources/blogs/spiderlabs-blog/advanced-malware-detection-with-suricata-lua-scripting/