Meet the Anti-Nmap: PSAD (EnGarde Secure Linux)
Meet the Anti-Nmap: PSAD (EnGarde Secure Linux)
(by Eckie S. from Linuxsecurity.com)
The Port Scan Attack Detector (psad) is an excellent tool for detecting various types of suspicious traffic, including port scans from popular tools such as Nmap, DDoS attacks, and other efforts to brute force certain protocols on your system. By analyzing firewall logs, psad can not only pick up on certain attack patterns, but even manipulate firewall rules to properly respond to suspicious activity.
This article will walk the reader through an EnGarde Secure Linux implementation of psad, from the initial iptables rules setup to the deployment of psad on the server side. By the end of the article, the user will be able to detect certain Nmap scans and have psad respond to these scans by blocking the source.
You will need:
- A separate machine on the same network with Nmap installed on it. You will be running certain scans on the server from this machine.
Once you have all the above you may log in as root, transition over to sysadm_r, and disable SELinux:
newrole -r sysadm_r
[psad_server]# newrole -r sysadm_r
EnGarde Secure Linux makes the installation of psad a breeze due to its Guardian Digital Secure Network (GDSN). You can install the package through the command line:
apt-get install psad
...or log in to WebTool and download the package from the package manager interface.
We shall get around to the setup of psad after we configure the firewalls on psad_server to log packets:
iptables Rules Setup
Since iptables is installed out of the box on EnGarde Secure Linux, you only have to run two simple commands to start logging packets with iptables:
iptables -A INPUT -j LOG
From here on out incoming packets (especially those of Nmap scans) will be logged. Let's see if we can start detecting such scans by setting up psad to do so.
On psad_server, use your favorite editor to modify the /etc/psad/psad.conf file. We're interested in the following tunables:
- The HOSTNAME tunable will be the hostname of the psad_server machine.
- The SYSLOG_DAEMON refers to the logging daemon for the machine. For EnGarde Secure Linux, this should be set to 'syslog-ng'.
- The ETC_SYSLOGNG_CONF refers to the direct path of the syslog-ng daemon's configuration file. For EnGarde Secure Linux, this should be set to '/etc/syslog-ng.conf'.
- Once you've properly configured those tunables, you can start the psad daemon:
[psad_server]# /etc/init.d/psad start
As far as danger levels are concerned, these range from one to five<br /> and are assigned to the IP addresses from which an attack or scan is detected. They are assigned based on the number of packets sent, port range, thetime interval of the scan, whether or not the signatures of the packets match up with psad signature attacks, and the IP address where the packet originated from. Depending on the number of such packets, a level is assigned as per the configuration file. For more information on danger levels and ideas for fine-tuning them, please refer to the resources at the end of the article.
psad - Active Detection
We will now use psad to detect certain Nmap scans. On the Nmap scanning machine, run a TCP connect() scan by executing the following:
nmap -sT 220.127.116.11
Replace 18.104.22.168 with the IP address of your psad_server.
If we check the /var/log/psad/fwdata file on the psad_server, you will find the following:
Feb 2 11:58:11 psad_server kernel: IN=eth0 OUT=
We can see that SRC will have the IP address of the nmap_scanner machine, and DST will have the address of the psad_server. Also note that PROTO=TCP, showing that the attack was a TCP connect() scan.
If you had previously configured psad to send email alerts, you will begin receiving emails concerning this scan showing lots more data than these log messages can ever produce. There are configuration tunables in the /etc/psad/psad.conf file to limit and even disable email:
EMAIL_LIMIT defines the maximum number of emails a configured user will receive for a given IP address.
ALERTING_METHODS can be set to noemail, nosyslog, and ALL, depending on whether you want only syslog-ng messages, email alerts, or both.
EMAIL_ALERT_DANGER_LEVEL is the minimum danger level that must be hit in order for psad to send email alerts concerning a detection. The default setting is one, so you can expect lots of emails for this tutorial's purpose.
Here is an example email showing psad output of the previous Nmap scan:
Subject: [psad-alert] DL2 src: nmap_scanner.yournetwork.com dst:
As you can see, psad does a wonderful job of taking packet data from logs, analyzing it and producing useful information on the type of scans used.
psad - Active Defense
One of the more prominent features of psad is its active defense implementation - being able to detect Nmap scans is nice, but how do you respond? Let's configure psad to automatically block the source of such scans upon detection.
Before implementing this feature, it is obvious for certain security veterans who are reading this article that there is a definite tradeoff for enforcing an active response policy. Although malicious traffic will be blocked, there is always the risk of blocking out valid traffic. Certain attackers can exploit active defenses and turn it against the target by attempting to spoof valid addresses, thus blocking out otherwise harmless traffic.
This only happens in cases where the active response system has been configured to respond to nearly ALL types of potentially harmful traffic, including port scans or port sweeps. This also applies to traffic which does not require bidirectional communication with the target. A better strategy to employ is to only respond to traffic where bidirectional communication is required i.e. TCP connections. Even then, one must take care to tailor their active response to certain types of TCP connections, such as attempted SQL injection attacks, etc. Please be sure you are absolutely positive of how your detection scheme is working before deploying an active defense.
Using your favorite editor, modify the /etc/psad/psad.conf file. We're interested in the following tunables:
ENABLE_AUTO_IDS should be set to 'Y' to enable the automated IDS response.
AUTO_IDS_DANGER_LEVEL, for this HowTo's sake, will be set to '3'. This danger level is customizable and the setting we use in this HowTo is for demonstration purposes only.
Restart the psad on the psad_server:
[psad_server]# /etc/init.d/psad restart
From the nmap_scanner machine, we'll run an Nmap SYN scan along with the '-P0' switch - this type of scan uses no ping and does not fully complete a TCP connection, resulting in fast scans. This usually requires root privileges, and is considered more of a dangerous scan - just the type of scan that psad detects at a higher danger level.
nmap -sS -P0 -n 22.214.171.124
Replace the '126.96.36.199' with the IP address of your psad_server machine.
psad will detect the SYN scans, and since the danger level of this scan is 3, it manipulates the iptables rules to block the source of the scans. This can be verified on the psad_server by running the following command:
[psad_server]# psad --fw-list
You will even receive an email alerts that inform you of the scan detection, as well as an email informing you that iptables rules have been added to auto-block the nmap_scanner!
Wrapping It All Up
Congratulations, you've successfully implemented psad to actively detect and respond to signature Nmap scans!
Keep in mind this is one of the more basic setups for psad. You can go even further and adjust danger levels to suit degrees of paranoia, put psad into forensics mode, incorporate the software with DShield, and even manually use psad to manipulate iptables rules. A great resource for psad research is 'Linux Firewalls' by Michael Rash. Rash includes several chapters on psad covering not only theory but advanced implementation of psad from start to finish. If you wish to gain suggestions for an advanced, finely-tuned active defense setup with psad, be sure to check this book out!
Have fun implementing an active defense against those who try to scan your system!
"'Linux Firewalls' by Michael Rash"