Posted by Kevin | Posted in Analysis, Signatures | Posted on 04-03-2010
Malware is a threat for networks of all sizes; from the home user and SOHO (Small Office/Home Office) environment; through to the largest of international businesses. It can seep into your network through the small chinks in your defences, be it a hole in the firewall, un-patched software, a client-side exploit, user interaction, and a seemingly endless reams of other techniques for getting that little piece of code running within your organisation.
Snort has many rules both in the VRT (snort.org) and Emerging threats (emergingthreats.net) rule sets for detecting malicious code and suspicious network activity and these are excellent sources with many excellent rules for detecting a wide range of threats including exploits, scans and malware.
There is an issue however in the detection of internal malware infections that call home. The content based rules within both rulesets detect traffic going from inside your organisation to the outside usually requiring a session to be established between the infected machine and the control server on the outside. This is fine in most cases; however in our example (Figure 1.1) we have strict egress firewall rules allowing only the HTTP proxy to act on behalf of the client and exit the firewall on port 80. There is a trojan which received updates from a control server, this control server is located at the domain www.iamacontrolserver.ru and the infected client goes through the three way handshake (SYN,SYN/ACK,ACK) to established a session over HTTP going out the default HTTP port (80) on your firewall (many firewalls will have this port open ), it then sends a HTTP GET request for the page /control_me_i_am_yours.php.
Figure 1.1 Example Scenario
This is easy to detect with snort. A simple rule like the following will suffice:
alert tcp $HOME_NET any -> $EXTERNAL_NET $HTTP_PORTS (msg:”Trojan Command And Control Request”; flow:established,to_server; uricontent:”/control_me_i_am_yours.php”; nocase; classtype:trojan-activity; sid:1233333; rev:1;)
So now we have a rule to detect a client establishing a client establishing a session to a control server and requesting /control_me_i_am_yours.php. Great! Well sort of; what happens if port 80 is only allowed through your firewall for HTTP proxy servers and the trojan is not aware of client proxy settings? In that case a SYN will be sent to port 80 to try and connect to www.iamacontrolserver.ru and get the page control_me_i_am_yours.php but it will not get past that SYN, the firewall won’t allow it past as the only hosts allowed past port 80 are the proxy servers.
This is a mixed blessing; the infected client cannot get to the control server and is basically contained within the network but you still have an infected client within the network with the possibility of doing harm but the rule we have to detect this trojan will never fire because it will fail at the flow:established part as the infected client cannot establish the session as it cannot get a SYN to the control server. This results in malware sitting within your organisation can possibly infect other machines but will not trigger any rules for outbound communication based on application content as it cannot get past the egress filtering.
So here our problem lies, we need to detect these infections within our network but are limited as the trojan will not fire upon it’s outbound communication, thus we have several solutions to get our IDS sensor setup to help identify infected hosts:
1) We can detect internal infection traffic, for instance if shares are accessed, exploits transferred between hosts and so on, however, what if the first thing the trojan does is outbound communication before moving on? It also can be very difficult to monitor all traffic between hosts to locate all infections, especially in large organisations.
2) Fire upon communication to command and control server IP addresses: This may be particularly effective. The emergingthreats.net rulesets contain rules for known compromised/hostile host IP addresses, the Russian Business Network address space, Botnet control server’s IP addresses and others. This may be effective if the IP address is known and these rules are worth running to detect communication to known control servers and thus eliminate many infections. However, this is reliant on known hostile IP addresses and will not identify IP addresses which have not been identified in the past as being hostile.
3) We look for DNS requests: clients not using DNS servers, making strange and anomalous DNS queries, queries to known hostile DNS names and so on may be infected. While this method itself has its own limitations, adding this to a detection in depth approach may aid in helping your organisation detec##
Detection of anomalous DNS is effective in detecting infections in both small and large organisations because:
- Even if a strict egress firewall policy is enforced not allowing connections iamacontrolserver.ru, the client still must resolve the domain name to an IP address using an internal or external DNS server, this traffic must cross the network to a DNS server and can be inspected by an IDS if it can see the traffic (either inline to network traffic or using a spanned port).
- In large organisations it is much more feasible to detect traffic to DNS servers than between clients to indentify possible malware activity as there will be significantly less DNS servers to monitor.
- Malware usually relies on DNS (unless the control server IP address is hard-coded which would never happen these days); this allows for better control of infected clients and using fast flux DNS; this allows the botnet controllers to keep control and move their infrastructure using large pools of possible domains and moving the IP address allowing for more resiliency (you can read more on fast flux DNS here on honeynet.org (honeynet). Because of reliance on DNS they must make internal or external DNS queries for their respective domains which can be detected.
- Once an anomalous DNS request has been identified and the client isolated for analysis; detection can be put in place to detect other clients trying to resolve the same domains and therefore likely infected with the same malware.
How do you put such a detection mechanism in place I hear you ask? Well I am glad you asked! I will assume you know how to build snort sensors (if not refer to the official snort manual or Google). Before you begin applying any of the techniques discussed you must ensure that you have defined ALL your $DNS_SERVERS variable in snort.conf as follows:
For one DNS server:
var DNS_SERVERS 10.1.1.1
For multiple DNS servers:
var DNS_SERVERS [10.1.1.1, 10.2.1.1, 10.3.1.1, 10.4.1.1]
This step is essential as many rules required for this detection will require the detection of DNS requests from clients going to DNS servers, requests from clients trying to make direct DNS queries without going through an internal DNS server and others. Not accurately filling in this variable therefore may result in many false positives.
So once we have this properly defined on our Snort sensor how to we go about detecting this traffic in the above scenario? First thing to consider is sensor placement. Ideally in this scenario for what we are wanting there will be two Snort sensors (Figure 1.2). One sensor is monitoring the inside interface of the firewall; this is to help monitor inbound traffic once it is past the firewall and outbound traffic. The other is monitoring traffic to and from the DNS server (you can also choose a choke point where a lot client to DNS server traffic passes depending on your organisation). The reasons for this being a desirable basic placement is to provide adequate coverage for malware which attempts to make direct queries to the Internet and those which use legitimate DNS servers. Hardware and OS performance are beyond the scope of this article but generally you can get away with older i386 hardware depending on how much traffic you are monitoring so a detection set-up does not have to be expensive.
Figure 1.2 Example Scenario Snort Sensor Placement
The first thing we want to set-up is egress and ingress monitoring of the firewall. What rule-sets you will run are dependent on your organisation. For an organisation looking to detect malware with strict egress filtering you will want to run in addition to the normal rule-sets to provide coverage for systems you have; it is beneficial to detect connection attempts to known botnet controllers and hostile Ips. This can be done using the emerging threats rule-sets emerging-botcc.rules, emerging-rbn.rules, emerging-compromised.rules and emerging-dshield.rules. The reason why this is useful even in IDS where you are not filtering these IP addresses either through intrusion prevention or on the firewall is because if a client makes a direct connection to a known control server which a IDS rule exists but it requires that the flow be established so the rule never fires, if the control server is known then such activity can be detected during the initial SYN attempt to the control server which the firewall rejects.
Once we have a our IDS set-up with existing rule sets from VRT and emerging-threats we need to create a custom rule and place it in our local.rules file to detect clients attempting to make direct DNS queries. To identify this behaviour look at the DNS query below for the example domain iamacontrolserver.ru. The query falls after the first 2 bytes in the packet and contains the characters 01 00 00 01 00 00 00 00 00 00 as outlined in black in figure 1.3. From this basic principle we can inspect DNS queries accurately for all sorts of DNS queries.
Figure 1.3 The magical 01 00 00 01 00 00 00 00 00 00 of a DNS Query
So from this we actually want to detect direct DNS queries from the inside to outside which does not come from the permitted DNS servers as this is anomalous behaviour and may be indicative of malware. We do this on our sensor on the inside of the firewall using below rule:
alert udp !$DNS_SERVERS any -> $EXTERNAL_NET 53 (msg:”Direct DNS Query By Client”; content:”|01 00 00 01 00 00 00 00 00 00|”; depth:10; offset:2; classtype:trojan-activity; sid:13210032; rev:1;)
What this rule says if a source address which is not defined in the variable DNS_SERVERS (which is why that is important to define otherwise your legitimate DNS servers will trigger this rule) trying to reach the DNS port 53 on an external network then trigger an alert. What ever domain is requested it does not matter, this rule will fire. This will help detect even if a client is attempting to contact a large pool of unknown control servers to known, for instance if a client tries to reach servers like asdjsjaksd.cn, bigbadserver.com, insovietrussiacontrolserveriscontrolledbyYOU.ru and so on; it will still detect it if the client is trying to make direct attempts. You may also want to negate other devices within your network what may cause false positives. These systems may be SMTP servers, proxy servers. You may negate such systems by creating a variable if there is a lot of hosts or listing single IP addresses like below:
alert udp ![$DNS_SERVERS,$SMTP_SERVERS,192.168.1.43] -> $DNS_SERVERS 53
You may define any variables in your snort.conf file. For instance if you have multiple proxy servers you could create a variable like below and include that in the negated rule.
Var PROXY_SERVERS [192.168.1.43,192.168.1.44,192.168.1.45]
alert udp ![$DNS_SERVERS,$SMTP_SERVERS,$PROXY_SERVERS] -> $DNS_SERVERS 53
Now we can move onto detecting anomalous DNS queries for clients trying to resolve possibly hostile domains to their respective IP addresses. Fortunately most of the work is already done in this regard. The website malwaredomains.com (Malwaredomains) maintains a list of known malware related domains and is an excellent resource in the creation of DNS black-holes to block requests for malicious domains (see malwaredomains.com for more information on how to do this). Fortunately; these domains have been converted into snort rules on autoshun.org that are updated frequently and which you can get here from the autoshun website (Autoshun BHDNS). Place these on your DNS monitoring sensors and include it within your snort.conf file. These rules are designed to detect queries for these domains to any DNS servers and can be extremely effective.
There are other rules which may help in detection of DNS related anomalies. For instance resolution of domains with the suffix .cn (China) and .ru (Russia) may be suspicious for some organisations and may be indicative of malware. This can be detected on your DNS sensor using the VRT signatures 15167 and 15168. These rules by default will detect this behaviour from your internal network to external networks so what you want to do is modify the start of the rule to become:
alert udp !$DNS_SERVERS any -> $DNS_SERVERS 53
Once this is done on both these rules on your DNS sensor then rather than detecting your DNS servers querying these domains outbound you will detect suspicious .cn and .ru queries going from your clients to your DNS servers so rather than only spotting the non-infected DNS server you can identify the actual client (this rule alteration is only useful on sensors where you can monitor internal DNS traffic).
What do you do though if you identify a likely malicious domain trying to actively be resolved by malware within your environment and want to detect it with the DNS sensor? For instance; I have detected malware which is trying to resolve the example iamacontrolserver.ru and want to detect other clients within my organisation trying to resolve that same domain name and therefore likely also infected. Well we take the below rule template:
alert udp !$DNS_SERVERS any -> $DNS_SERVERS 53 (msg:”INFORMATIVE_MESSAGE GOES_HERE”; content:”|01 00 00 01 00 00 00 00 00 00|”; depth:10; offset:2; content:”DOMAIN”; nocase; distance:0; classtype:trojan-activity; sid:SNORT_ID; rev:1;)
Using this template we add in a few things to allow for detection of this malicious domain. First thing is to add a description into the msg so it means something to us when it alerts. We also need to apply a unique SID for the alert. If you have snort using unified logging and barnyard then writing these logs to a database (which is advisable as it offers better performance) then you must add a line into your sid-msg.map file (you may create a file and have it scripted to insert the information into any new sid files during any automatic updates you have), this entry in sid-msg.map would look like this for our example rule:
1232313 || DNS request for iamacontrolserver.ru
Now we add in the actual domain. They key to remember is that the hex number before the suffix is the number of characters in it. For instance for our rule it becomes iamacontrolserver|02|ru for 2 characters where if it was actually iamacontrolserver.com then it would become iamacontrolserver|03|com and so on. This simple rule of thumb applies throughout the process of writing rules for DNS domains (i.e iamacontrolserver.co.uk would be iamacontrolserver|02|co|02|uk)
So from the template INFORMATIVE_MESSAGE GOES_HERE becomes DNS request for iamacontrolserver.ru; DOMAIN is replaced iamacontrolserver|02|ru and a unique snort ID is put in place of SNORT_ID and thus we end up with the below complete rule.
alert udp !$DNS_SERVERS any -> $DNS_SERVERS 53 (msg:”DNS request for iamacontrolserver.ru”; content:”|01 00 00 01 00 00 00 00 00 00|”; depth:10; offset:2; content:”iamacontrolserver|02|ru”; nocase; distance:0; classtype:trojan-activity; sid:1232313; rev:1;)
If you have any references they may be put in at this point also for further information about the domain so you can remember why it is there (this may be useful in the message to (for instance msg:”DNS request for iamacontrolserver.ru (malware update and control server)”.
Now that we have the final rule below we can run it on our DNS server sensors to detect DNS queries for this domain from our clients to our DNS servers. The detection can then be viewed immediately by writing alerts to the console as shown in figure 1.4 or stored in a database and correlated examined later using a tool like BASE which allows better forensic analysis as you can see in figures 1.5 and 1.6.
Figure 1.4 The DNS request from Snort running in console mode
Figure 1.5 The DNS requests viewed in BASE
Figure 1.6 The DNS request viewed in BASE
There is a future in DNS and IP reputation within IDS/IPS to help remedy this though and that future is being realised by the Open Information Security Foundation (OISF) in their development of the next generation IDS/IPS Suricata which also has existing Snort rules.
While Suricata is currently in development at time of writing and many features are yet to be implemented there are betas being released and development is moving quickly. Among its impressive list of implemented and planned features which you can find out more about on the site IP and DNS reputation is envisioned to provide a central pillar in the detection of hostile activity, for instance:
- If a client tries to resolve multiple domains with a low TTL (Time To Live) then statistically it can be worked out that this client may be infected with malware which uses fast flux DNS.
- Detecting clients trying to connect to known malware domains
- Using positive and negative information about IP addresses to provide alerts on hostile IP addresses and improve rules which false positive to say only alert on IP addresses which have a negative IP reputation.
…and much more (see OISF site for more information)
Using IP and DNS reputation to complement detection may help in the detection of malware within your organisation for which detection by traditional signatures may not be possible due to egress policies and if the IP and DNS reputation features are implemented as aspired to; then Suricata may become an extremely useful addition to your network security.
As you have seen; using DNS detection you can identify infected clients where they may not have otherwise been easily detected. This idea can be expanded on even further using your existing DNS infrastructure in home and enterprise environments by creating DNS sink-holes and either sending clients to their loop back addresses when they are trying to resolve a malicious domain or setting them to resolve to a web server which then monitors for inbound requests from the clients. Information on how to do this can be found on malwaredomains (Malwaredomains Howto).
In a SOHO or home environment you can accomplish the same thing using a host file or if an automatically updated and centralised point you can use a free network firewall like Smoothwall with the black hole DNS addon (Smothwall BlackholeDNS). This add-on automatically updates from malwaredomains.com and other places and can stop your internal clients connecting to listed hostile domains which may be very effective at containing and preventing infections on a budget within small environments.
Detection of malware by monitoring your DNS traffic and requests may help you find malware that may otherwise not reveal itself to you, especially if you have strict egress filtering policies in place. Of course this is only part of a defence in depth approach and you should always adhere to best practices as well as active monitoring including log for traces of policy violations and suspicious traffic.
References
Autoshun BHDNS. http://www.autoshun.org/downloads/bhdns.rules. Wednesday March 3rd 2010.
emergingthreats.net. www.emergingthreats.net. Wednesday March 3rd 2010.
honenet. www.honeynet.org/papers/ff). Wednesday March 3rd 2010.
Malwaredomains. Http://www.malwaredomains.com. Wednesday March 3rd 2010.
MalwareDomains Howto. http://www.malwaredomains.com/wordpress/?page_id=6, Wednesday March 3rd 2010
OISF. Matt Jonkman, Victor Julien et al. www.openinfosecfoundation.org/. Wednesday March 3rd 2010.
Smoothwall BlackholeDNS. http://community.smoothwall.org/forum/viewtopic.php?f=26&t=26030. Wednesday March 3rd 2010 .
Snort.org. VRT et al. www.snort.org. Wednesday March 3rd 2010.





















