Hardening Freebsd: an Approach to a Secure System Remy
Total Page:16
File Type:pdf, Size:1020Kb
Hardening FreeBSD: An Approach to a Secure System Remy Baumgarten University of Hawaii [email protected] October 4th, 2009 Hardening FreeBSD 知己知彼 百戰不殆 If you know both yourself and your enemy, you can win a hundred battles without a single loss. Sun Tzu's The Art of War “Operating System are not broken into; the programs running on the operating system are.” -Micheal W. Lucas General Summary: Hardware and Software Specifications Foreword and Forewarn Who are our Adversaries? Updating and Package Auditing Mailing Lists Permissions Networking and Firewalls Kernel Security Remote Logging Command Logging Secure Remote Access with SOCKS Proxy Forwarding Web Browsing Encryption Auditing Further Reading Hardware Specifications: Intel Core i7 920 Nehalem 2.66GHz 4 x 256KB L2 Cache 8MB L3 Cache LGA 1366 130W Quad-Core Processor ASUS P6T6 WS Revolution LGA 1366 Intel X58 ATX Intel Motherboard CORSAIR XMS3 6GB (3 x 2GB) 240-Pin DDR3 SDRAM DDR3 1333 (PC3 10666) Triple Channel CORSAIR CMPSU-850TX 850W ATX12V 2.2 / EPS12V 2.91 SLI Ready CrossFire Ready Active PFC Power Supply 2x - EVGA 896-P3-1260-TR GeForce GTX 260 896MB 448-bit GDDR3 PCI Express 2.0 x16 HDCP Ready SLI Supported Video Card LG W2452T-TF Black 24" 2ms(GTG) Widescreen LCD Monitor 400 cd/m2 10000:1 DCR with HDCP support LG Black 22X (CAV) DVD+R 8X DVD+RW 16X DVD+R DL 22X (CAV) DVD-R 6X DVD-RW 12X DVD-RAM 16X DVD-ROM 48X CD-R 32X CD-RW 48X CD-ROM 2MB Cache SATA 22X DVD¬±R DVD Burner Antec Nine Hundred Black Steel ATX Mid Tower Computer Case Software Specifications: FreeBSD 7.3 AMD64 Minimal Customized Compiled Kernel Installed Packages of Relevance: tripwire-2.4.1.2 lynis-1.2.6 snort-2.8.4.1_1 john-1.7.2_1 scponly-4.8 munin-main-1.2.6 postfix-2.6.5,1 mysql-server-5.0.86 portupgrade-2.4.6_3,2 portaudit-0.5.13 netcat-1.10_2 nmap-5.00 oinkmaster-2.0_1 Foreword and Forewarn: Security tips are mostly useless unless one understands why and how that tip works with other practices to protect your system from attacks. While securing your operating system may make it a little bit harder in some aspects for the attacker, a process listening on the network with root privileges with just one small bug could for example make a lot of the following I am going to talk about largely irrelevant. Security must to be covered as a whole, starting with the person or persons managing the system to the most intricate technical detail about the way the kernel manages the addresses of memory in the stack and the various mechanisms that make it work and everything in between. This paper is not about how to install or use FreeBSD. I assume you are mildly familiar with this operating system and are attempting to secure it against attacks. This is a brief overview meant to be starting guide to point you in the right direction and give you an idea of what security features FreeBSD has to offer. It is by no means a comprehensive or advanced guide. It would take many books worth of information to cover each of topics covered here and there simply isn’t enough room or time for anyone to cover all of these in depth. Who are our Adversaries? Skiddies (script kiddies) Botnets Internal Users Skilled Attackers Updating and Package Auditing: The most effective defense against attacks is simply keeping your system up to date. FreeBSD offers many tools for this. I prefer to use portsnap and portupgrade. If you are not able to login to your system daily, you should place these commands to be executed by cron and use the ports random delay flag as to not overload the FreeBSD servers. Portaudit is another great tool that checks your installed packages against known vulnerabilities. Simply install portaudit from ports and follow the instructions. Here is a sample run. Note that it performs the following execution daily and when you update your ports. sudo portaudit -Fda Password: auditfile.tbz 100% of 57 kB 66 kBps New database installed. Database created: Mon Oct 5 03:05:02 UTC 2009 0 problem(s) in your installed packages found. If a problem is found it will advise you to upgrade your package and include a link for more information. FreeBSD also sends out daily security mail if your mail server is correctly setup and you add your alias to your mailers configuration. This feeds the output of portaudit into your mail daily. It is also very important to read or grep the UPDATING file located in /usr/ports for information or warnings about packages that you are about to upgrade. If you don’t do this and install a new version of PERL for example, you may start to wonder why your system is not acting correctly. Do yourself a favor and look at the latest changes before upgrading. Subscribe to FreeBSD Security Mailing List I can’t emphasize this enough. Reading the listings daily on your commute will assist in understanding what attacks may be headed on the way. For example, if there is a 0day discovery and it affects a package you’re using, it may be wise to suspend its service until there is a patch or workaround. Also subscribe to BugTraq and CERT mailing lists. These have a higher volume then FreeBSD, but well worth the time. Permissions FreeBSD users are created with their group the same as their user, so it is ok if you want to chmod your /home/$user directory to 660. This makes sense on a system which is shared with other users or when you have daemons listening on ports as privileged users. For example, if you have a webserver and its running as the www user and an attacker performs RFI (remote file inclusion) against a vulnerable php script that spawns a perl connect back shell to their remote host to bypass your firewall, the shell will be running as user www. User www may have access, depending on how apache was configured, to write to /tmp, so arbitrary files may be downloaded there and executed. It’s best to prevent the attacker from looking in your /home/$user directory and chmod 660 will prevent this from happening if they are not able to perform a local privilege escalation attack. It is also a good idea to not allow other users to see what is in your crontabs. Issue the following commands to make them unreadable to other users: chmod 0640 /etc/crontab While your at it, also chmod /root to 750 chmod 0750 /root Recognize that if an attacker exploits one of your programs and escalates to root, all the strict permissions in the world won’t be able to help you. Networking and Firewalls Attacks typically start here. It’s best practice to disable any listening sockets that you do not use. Sockstat is a great tool for this. Use sockstat -4 to see what sockets are listening on ipv4 and ipv6. Disable any processes that you are not using. Don’t ignore ipv6. Using ipv6 is a great way for attackers to exfiltrate data without the user recognizing whats going on, and many firewall rule sets don’t take this into consideration. FreeBSD comes with an excellent way to control your network traffic, its called Packet Filtering or PF for short. It works at the kernel level and does a comparison of all traffic against your PF ruleset. $cat /etc/mypf.conf ext_if = "re0" services = "{5734}" # services on ports set skip on lo0 scrub in all antispoof for $ext_if block drop all pass out all keep state pass proto tcp from any to $ext_if port $services flags S/SA synproxy state This is an example of a packet filter ruleset. It has been minimized for the convenience of the reader. Note that pinging your server will result in no response. To learn more about creating a good PF ruleset, you should read the various online tutorials on PF or get your hands on a copy of The Book of PF (No Starch Press). For computers where you will be using openssh server (sshd), you should change the port it listens on to a non-standard port to mitigate bots that will fill your logs up with brute force attempts. If you want to leave openssh on its standard port of 22 you can use PF to block repeated attempts. I recommend doing this even if you listen on a different port for obvious reasons. Append the following to your rc.conf: pf_enable="YES" pf_rules="/etc/mypf.conf" pf_flags="" pflog_enable="YES" pflog_logfile="/var/log/pflog" pflog_flags="" Then add this to your /etc/mypf.conf: table <bruteforce> persist file "/var/db/blacklist" block quick from <bruteforce> This states that: There exists a table called bruteforce, which should be loaded from /var/db/blacklist. Everything in table bruteforce is immediately blocked. Everything incoming to SSH is allowed (if not already blocked from the bruteforce table). Now lets create (as root) our blacklist file that contains the IP’s of repeat offenders. touch /var/db/blacklist chmod 644 /var/db/blacklist Download the script located at http://home.earthlink.net/~valiantsoul/pf.html There are other options instead of using PF for this, but I provided the above as a demonstration of how you can script with PF. Personally I recommend using SSHGuard, it does all the above for you. Simply use ports to install: cd /usr/ports/security/sshguard && make install clean It’s a good idea to enable some configuration options if you decide not to use PF.