Sunday, August 30, 2015

Switching Linux Mint on Thinkpad T61p

I've been wanting to try and switch to Linux full-time on my home desktop/laptop machines for a while, and the amount of spyware / tracking / report back in Windows 10 is pushing me to make a real effort this year.  So, I'm taking my old Thinkpad T61p and putting Linux Mint 17.2 on it.  My requirements were:

  • Full disk encryption
  • Virtualization for a Win7 or Win10 guest
  • Running as much as possible inside of Linux
  • 64bit OS
A lot of my day-to-day software is already multi-platform or open-source.  For instance:
  • Mozilla Firefox and Google Chrome for web browsing
  • Mozilla Thunderbird for email
  • SVN or git for version control
  • GPG for encryption
  • Pidgin for instant messaging and IRC
  • Audacity, Handbrake, VLC for multimedia work
  • About half of my Steam games are Linux / SteamOS
  • OpenVPN
  • pgAdmin III
  • Synergy
The troublesome applications will be:
  • Microsoft Office is iffy under WINE
  • Microsoft Access does not run under WINE
  • Skype might be a problem
  • ODBC connections for use with MSAccess
  • The half of my Steam games that are not cross-platform
  • Cisco AnyConnect VPN client for access to my virtual servers
Because I'm doing this on my old laptop (until the T550p comes out later this year), I'm not too worried about video games that won't run under Steam for Linux.  The only games that this old Core2Duo system will support are the lighterweight / older games that don't need much CPU or video graphics performance.

Sunday, July 26, 2015

Installing atticmatic/borgmatic on Cygwin

There's a wrapper project for Attic / Borg backup called "Atticmatic" on GitHub.  It helps simplify the process of doing daily backups using attic/borg.

Packages needed on Cygwin (in addition to those needed for attic/borg):

  • murcurial (hg)
Creation of the SSH key (assumes that you have the 'openssh' package installed):
  1. mkdir ~/.ssh
  2. chmod 700 ~/.ssh
  3. ssh-keygen.exe -t rsa -b 4096 -N '' -C 'Backup Key 2015' -f ~/.ssh/ssh-backup
  4. chmod 600 ~/.ssh
Then create a ~/.ssh/config file to point at the server:

Port 22
User usernameonbackupserver
IdentityFile ~/.ssh/ssh-backup

Add the new public key to the remote server, to the ~/.ssh/authorized_keys file for the 'usernameonbackupserver' account.

Now test that you can login to the remote server, using "ssh" (use the "Host" line entry from ~/.ssh/config).  And make sure that you can create files/directories in the location where you want to store the backups.

Make sure that your Attic/Borg backup has been initialized.

$ borg init ssh://

Now you'll need to create an empty /etc/borgmatic/exclude file and edit the sample /etc/borgmatic/config file.  Once those files are setup, you can run "borgmatic" (or "borgmatic -v 1" to see details) on a regular basis.

Thursday, July 16, 2015

SSH keygen under Cygwin

You will need to install the "openssh" package using the Cygwin installer before doing this.


Typical steps for creating SSH keys:

$ mkdir ~/.ssh
$ chmod 700 ~/.ssh
$ cd ~/.ssh
$ ssh-keygen.exe -t rsa -b 3200 -C 'Borg backup key thomast61p July 2015' -N '' -f ~/.ssh/ssh-borg-backup-july2015

In this particular case, I am creating a RSA/3200 key with no password (-N '') and with a comment indicating that it will only be used for Borg backups.  Because the key has no password, I should only use it in conjunction with the borg backup command on a separate server-side account that has very limited permissions on the server.

Saturday, July 11, 2015

Linux KVM shutting down all virtual guests

On my current virtualization server running Linux KVM (QEMU), I want to shutdown all guests so that I can unmount the file system containing the VM image files and make it larger.

#1 - See what guests are running

# virsh list
 Id    Name                           State
 1     dc1                            running
 3     cfmc87                         running
 4     win7c                          running

#2 - Use the libvirt-guests service to suspend all of them.

# service libvirt-guests stop

Running guests on default URI: dc1, cfmc87, win7c

Suspending guests on default URI...
Suspending dc1: ...
Suspending dc1: done
Suspending cfmc87: ...
Suspending cfmc87: ...
Suspending cfmc87: done
Suspending win7c: ...
error: Failed to save domain bb0d169d-a373-544e-a0f7-99e338673177 state
error: internal error unable to execute QEMU command 'migrate': An undefined error has ocurred

#3 Dealing with troublesome guests

Oops, looks like win7c is being difficult.  So let's just force it down.

# virsh shutdown win7c
Domain win7c is being shutdown
# virsh list      
 Id    Name                           State
 4     win7c                          running

Even with issuing the shutdown command and waiting a few minutes, the "win7c" guest is still running.  So we'll have to "destroy" the instance.

# virsh destroy win7c 
Domain win7c destroyed

#4 Checking that all files are released

Now I can use the "lsof" command to verify that there are no open files on the mount point.

# lsof /srv/vms
(returns nothing)

#5 Umount, fsck, resize, fsck, remount

The VM image LV is stored on /dev/md127, inside a LVM thin pool.  It's currently 200GB and is about 70% full, so I want to add another 100GB.

# umount /srv/vms
# fsck -f /dev/vg10/vms
# lvextend -L+100G /dev/vg10/vms
# resize2fs /dev/vg10/vms
# fsck -f /dev/vg10/vms
# mount /srv/vms

#6 Restart the guests

# service libvirt-guests restart
# virsh list

And now my /srv/vms filesystem is no longer having space issues.

Wednesday, July 08, 2015

Installing borg backup (fork of Attic) on Cygwin (Windows)

I've used Cygwin with rdiff-backup before to backup a Windows box to a backend Linux server over SSH, but given the success that I've had with Attic backup, I'm going to try this with the fork of Attic which is called "Borg".

Step #1 - Download and install Cygwin.  The following packages that need to be installed before you can install borg backup.

binutils (not sure)
libuuid-devel (not sure)

Step #2 is to install "pip" inside the Cygwin environment.

$ cd ~/
$ mkdir -p ~/downloads/python
$ cd ~/downloads/python
$ wget
$ python3
$ pip3 --version

The "pip" command should return a version number string if it is installed.

Step #3

$ pip3 install borgbackup

Step #4

At this point, if you type "borg" at the command prompt, you should see information about Borg such as the version and list of commands.

Tuesday, July 07, 2015

pfSense RRD graphs for NTP - system jitter vs clock jitter

Since installing pfSense and setting up the NTP server, I've been wondering for a while what the difference is between "System Jitter" (sjit) and "Clock Jitter" (cjit) in the RRD graphs.  For instance, in the following graph, we can see that the system jitter value has gone way up.

So what causes system jitter? Looking at the Status -> NTP page gives us a bit of a clue.

At least two of our upstream time servers have huge jitter numbers, 20-40ms, while all of the other upstream time clocks are reporting jitter of less than 1ms.  So something is happening to those two time servers, or a common route between us and those time servers.

That leads me to believe that the green line in the NTP graphs (sjit) indicates how much jitter there is between our NTP server and the upstream servers.  In general, it probably means there is congestion between us and that server and that packets are arriving out of order or late.

Saturday, June 27, 2015

Inexpensive and power efficient refurb PCs for firewalls

This is a follow-up on my earlier post about the Lenovo M58p (Intel Core2 Duo E8400 @ 3GHz) that I'm using for my home firewall.  It clocks in at 38-40W idle and 50-60W under load (which is rare).

If you go to NewEgg's site and go into the Desktop Computers category, you can find all sorts of refurbished boxes for under $150.

So what sort of CPUs are there and how do they stack up in terms of TDP and idle TDP?

Well, according to various charts on the net:

Intel Core 2 Duo E6600 and E6750 should both draw less power then the E8400 that I'm using.  At a guess, that would put total power draw closer to 30W idle rather then 40W.  The Core2Duo line also performs better then the old PentiumD and Pentium4 chips when under load.

So Core2Duo is better then a Pentium D or Pentium 4 chip, and the slower E6600/E6750 C2D are better then the 3GHz E8400.

The i3/i5 series (Sandy Bridge or later) should offer lower power-draw then the older Core2Duo chips.  Power draw dropped again slightly with Ivy Bridge and Haswell.

That being said, the least expensive i3 refurb is $220-$250, while a Core2Duo unit can be found for $80 or less.  So I think until the i3 units start being retired in another 2 years, the C2D units are going to be the best choice.

Monday, June 22, 2015

Using aliases pfSense to create rules for protocols with multiple port ranges

File this one under "things I wish I had known sooner".  When setting up pfSense firewall rules on an interface, you'll run into protocols which have multiple ports that are not in a contiguous range.  One example of this is the common web server (HTTP) ports of 80, 443 and 8080-8081.

This leaves you with two options.

  1. Setup multiple rules.  This is the best option because you only specify the exact ports that you want, with no extras thrown in.  The downside is that for some protocols, you will end up with multiple rules that have to be maintained.
  2. Specify a rule with a broad port range.  Which is sort of okay if you are only allowing a handful of extra ports, but it is not ideal.
Enter the concept of aliases (under the Firewall -> Aliases menu) in the pfSense web UI.  Here you can create an alias which lists out all of the ports associated with a particular protocol.

After creating the alias, you then create or edit a rule and use that alias in any fields with a red background.  Such as the destination port field.

After clicking the "Save" button, rules that are using port aliases will show up in the rule list looking like:

Needless to say, that can make your life much easier when maintaining large lists of ports as long as all of the ports in question are using the same protocols.

Mail client ports (IMAP/POP3/SMTP) are also good candidates for an alias rule.  One caution is to never allow 25/tcp to egress your network, only your mail server in the DMZ should be allowed to contact other mail servers via port 25.  Every internal client should be forced to either use tcp/465 (SMTP/SSL) or tcp/587 (SMTP Submission) or route their SMTP traffic through your mail server.

Saturday, June 20, 2015

Using badblocks to prepare an offsite USB backup drive

Part of my backup strategy is to write my backups to external USB drives which are protected by LUKS encryption.  However, before I will put a drive into service, I like to heavily test any mechanical drive for a few days to see whether it will hold up to the wear-and-tear of being a portable drive.

(There's little or no point in doing this on a SSD.)

Currently, my preferred method is to use "badblocks" in destructive write-testing mode to test the drive.  For example:

# badblocks -p 3 -wsv -t random /dev/disk/by-id/usb-SAMSUNG_HM502JX_C######-0\:0

The "-p 3" tells badblocks that the drive has to survive (3) passes without finding any new bad sectors before badblocks will stop running.  Most modern mechanical hard drives have spare sectors that can be used when a bad spot is located on the surface.  By repeatedly writing to bad or dying parts of the drive surface, we can force the drive's firmware to remap those failing areas to the spare sectors.

The downside of "-p 3" is that this increases the amount of time needed to test the drive before placing it into service.  A rough estimate is that a 1TB drive over USB 2.0 will require 3-4 days of testing with "-p 3".  If you are using a USB 3.0 drive and it is hooked up to a USB 3.0 port, then it might only take 20-30 hours to test.

The "-wsv" tells badblocks to do write-testing (which destroys all data on the drive), as well as giving status output and being verbose about what it is doing.

The "-t random" specifies that we want to use a random pattern for the test.  Please note that this is not a suitable replacement for "shred" when wiping a disk or preparing it for LUKS.  You should still run "shred" on the drive prior to using it (or giving it away to someone else).

Drives that have started to fail will often sound like they are seeking with big pauses during the badblocks write pass.  If you are seeing big pauses in reads/writes from a drive during testing, it's possible that the disk is damaged or about to permanently fail.  You will have to use your best judgement whether you trust the disk for backups.

(If I think a drive is failing, then it gets a second or third pass with badblocks and the "-p 3" option.  It will usually die during the 2nd or 3rd pass, or all of the bad spots will have been remapped and successive runs will go quickly.)

pfSense rate limiting, egress filtering, opendns filtering for wifi hotspot

One of the experiments that I'm running with the new network is running an open / unsecured WiFi hotspot for the neighbors.

Some of the protections that I'm using:

  • Uses OpenDNS servers with some categories of websites blocked.  I'm using the "OpenDNS Home" service which lets me pick and choose which categories are blocked by default.  In addition, the OpenDNS server will display a "blocked content" page for regular HTTP traffic where users can request an unblock.  Unfortunately, this feature does not work well for HTTPS (SSL) sites, but it still blocks the site.
  • Access to other DNS servers is blocked, clients can only access the two OpenDNS server IP addresses.
  • All rules on the interface are rate-limited to 3Mbps down and 1Mbps up.  This limits bandwidth abuse and slows down file sharing.
  • Heavy egress filtering.  All outbound traffic is blocked by default except for the whitelisted ports/protocols.

Now, this is not 100% foolproof.  But I at least want to limit the possible damage and take at least some steps against abuse.  I'll probably use this setup at a company that I'm consulting for where they want to offer open WiFi in their waiting area.

One thing I would like to do is setup a "Captive Portal" on the interface which forces the user to enter their cell phone number and receive a voucher code via SMS that is good for 3 or 7 days.  I have to figure out how to do that with pfSense and see how it works in practice.

The other thing I plan on doing is setting up a similar SSID/VLAN, but with higher bandwidth limits, more ports and no OpenDNS filtering for authenticated guests.  That would probably be a 20Mbps down / 5Mbps up setup protected by WPA2/PSK.  Think along the lines of "neighbors" or "friends" who you want to allow use of the internet pipe, but do not want to allow onto your interior network.  This would also be a good setup to use in an office environment for BYODs that only need internet access (such as clients).

Sunday, June 14, 2015

pfSense on Core2Duo E8400 refurbished PC

A rough power estimate for my little SFF (small form factor) refurbished PC that I'm using for a pfSense firewall:

  • Intel Core2 Duo E8400 @ 3GHz
  • 4GB RAM
  • 120GB SSD
  • Dual-port Intel PCIe x4 NIC
At idle, it consumes about 38W when the CPU throttles back to 1.8GHz.  That's pretty good for a PC that is not designed to be a low-power / fanless unit.  Under load, that goes up to about 60-65W.

38W @ $0.15/kWh = $50/yr
65W @ $0.15/kWh = $85/yr

So even if I got something that stayed below 15W, I'd only save $50-$60/yr.  A lot of the low power units are $300-$500, which would be a very long time before they'd pay off.

Note: You do need to enable PowerD under System -> Advanced -> Miscellaneous -> Power Savings in order to get the CPU to throttle down when idle.  I recommend "Hiadaptive" for an office firewall, but you might want to experiment.

The biggest CPU hog on my current pfSense setup is "ntopng".  While doing bi-directional gigabit testing, that ate up 10-12% of my CPU power.

Tuesday, June 09, 2015

pfSense Firewall CPU load estimate

According to the pfSense dashboard, I have:

Intel(R) Core(TM)2 Duo CPU E8400 @ 3.00GHz
2 CPUs: 1 package(s) x 2 core(s)

When running a quick speed test, "top" shows about 5% system load at 60Mbps.  That gives a rough upper-end of around 1200Mbps (1.2Gbps) for switching speed.  At a guess, that might be closer to only 1Gbps performance under heavy traffic.

1Gbps of capacity is plenty for the moment where I have:

- 50/50 Mbps service from Verizon FIOS (seems to peak at 60/60)
- 802.11 b/g/n (11-54Mbps)
- 802.11ac (tops out at around 1Gbps)

But it may not be enough for connecting together multiple gigabit LAN segments.  So I will need to keep all high bandwidth traffic on the same VLAN so that the traffic gets handled by the switches without touching the pfSense firewall.

Update #1 (Jul 10 2015): Suricata cuts the performance of the WAN interface (in terms of CPU load per Mbps) by a factor of 5x-10x.  While I could probably route 1.2-1.5Gbps with this firewall, a 30Mbps load on the WAN, which is monitored by Suricata, resulted in 20% CPU load.  That puts my upper-bound for WAN traffic at only 150Mbps.

VLAN adventures with Netgear GS108T and TrendNet TEW-814DAP

As part of setting up my new home network, I'm experimenting with VLANs.  The pfSense firewall has the following user-defined VLANs on the interior port.  Each of these VLANs has a separate address range (all are IPv4 with a 24-bit netmask, i.e. The pfSense firewall is always the ".1" address on each network segment and routes traffic between the segments.

em0 / 12 - Unsecured Guest WiFi
- This will (probably) be the VLAN used for an access point that is not password protected or that encrypts traffic.  I plan on limiting it to 1Mbps, plus put a SMS-authentication captive portal on it, plus point it at OpenDNS with heavy filtering.

em0 / 24 - Secured Guest WiFi
- This will be protected with an easy-to-enter WPA2/PSK password.  Suitable for handing out to people that I marginally trust or know.  No access to the internal LAN, and only selected ports allowed out.

em0 / 36 - Internal Guest WiFi
- Protected with a moderate strength WPA2/PKS password.  Suitable for friends.  Wide open access to the internet, limited access to the internal LAN.

em0 / 48 - Internal WiFi
- Protected via WPA2/PSK with a strong password.  Has full access to the internal LAN.

em0 / 87 - LAN
- This is the internal LAN network.

em0 / 100 - Infrastructure.  In a normal shop, all switches / APs would be members of this VLAN and management would only be allowed via this VLAN.

em0 / 999 - Blackhole VLAN ID (nothing should ever listen here).

That's the easy part, defining the various VLANs.  Those same VLAN IDs have to be configured in the GS108T switch as well.  This is done under Switching / VLAN / VLAN Configuration.

Note that the first three defined VLANs (1/2/3) are hard-coded into the GS108T firmware and cannot be removed.  As I indicated in a previous post on the subject of VLAN security, you should avoid using VLAN #1 for anything.  And now I would amend that to say that you should avoid VLAN IDs 1-9.

There are a few key pieces to think about when setting up VLANs:

Inbound #1 - Are the packets already tagged when they reach the switch (inbound) from another device (i.e. switch or WiFi Access Point)?

Inbound #2 - Should untagged (no VLAN header) packets inbound to the switch be blocked/dropped?

Inbound #3 - What VLAN should untagged packets be assigned to on inbound?

In the GS108T, the inbound concerns are handled under Switching / VLAN / Port PVID Configuration.  This screen will allow you to apply a VLAN tag as packets enter the switch.

In the case of the WiFi Access Point which is attached to "g2", it does not support VLAN tagging of the various SSID networks, so we have to treat it as a "dumb" device.  So when the WiFi sends packets to the switch they get assigned to VLAN #48.

Outbound #1 - Should the VLAN header be stripped as the packets leave the switch via a particular port?

Outbound #2 - Does the device attached to this port understand VLAN tags?

Outbound #3 - Should untagged packets be blocked from exiting via this port?

Egress handling is configured through Switching / VLAN / VLAN Membership and is somewhat unintuitive in the GS108T user interface.  You need to read this screen as:

"If a packet that belongs to VLAN ## is traversing the switch and about to egress (exit/outbound), what ports is it allowed to leave by and what should happen to the VLAN header?"

In the case of VLAN #48 (Internal WiFi), the answers to that are:
  • VLAN #48 is only allowed to egress via port "g2" and port "g3". 
  • "g2" is our "dumb" WiFi Access Point
  • "g3" is the "smart" pfSense firewall that understands VLAN tags
  • Packets going to the WiFi Access Point need to have VLAN headers stripped
  • Packets going to the pfSense firewall should have VLAN headers left intact

The above shows that any packets on VLAN 48 are only allowed to leave untagged (U) via "g2" (WiFi AP) or tagged (T) via "g3" (pfSense firewall).

Monday, June 08, 2015

Checking authorized_keys for duplicate SSH key lines

After a while, unless you are using Puppet or some other tool, your ~/.ssh/authorized_key file will end up with half a dozen or dozens of different SSH public key lines.  And depending on how careful you were, some of them may be duplicates or screwed up.

One way to make sense of the madness is to look at the first N bytes of each line in the ~/.ssh/authorized_keys file and look for strangeness.

$ cut --bytes=1-80 ~/.ssh/authorized_keys 
ssh-dss AAAAB3NzaC1kc3MAAACBAP0090dCcnFwtuP9Rmjgf7eHR20JdmHASXS+un4cAKNYpwHIDlA9

ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAZB+mI3xeVeYo3B2yJqvQYUpVBrNtMmtd3iAj6O6pMIvRGzm

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsrOtkkIXu0ci/8h79/zCFgAoDZgw6yQExBs4o/KjfmB/

Just by looking at the above output, I can see that the second ssh-rsa key line was not placed on a single line as it should have been, but has line breaks.  After a quick edit of the file, now the output looks like:

$ cut --bytes=1-80 ~/.ssh/authorized_keys
ssh-dss AAAAB3NzaC1kc3MAAACBAP0090dCcnFwtuP9Rmjgf7eHR20JdmHASXS+un4cAKNYpwHIDlA9

ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAZB+mI3xeVeYo3B2yJqvQYUpVBrNtMmtd3iAj6O6pMIvRGzm

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsrOtkkIXu0ci/8h79/zCFgAoDZgw6yQExBs4o/KjfmB/

Now I can run the output of that through sort/uniq to see whether I have any duplicate SSH public key lines:

$ cut --bytes=1-80 ~/.ssh/authorized_keys | sort | uniq -c -d
      2 ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAZB+mI3xeVeYo3B2yJqvQYUpVBrNtMmtd3iAj6O6pMIvRGzm
      2 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC54VI+7J1DoEEiJml8JusdM4M9UNNIA8gv/JER7rQ7

Looks like I do have a pair of duplicated SSH public key lines.  This is a good thing to know because if was trying to remove a particular SSH key pair, I might remove one line but not see the other.

Friday, May 22, 2015

Firewall build (part 3) VLAN Security

Since I plan on using VLANs on the WiFi Access Points to separate guest vs friend vs trusted traffic, I need to make sure that I'm doing VLANs in a secure fashion and not leaving any large holes.

The primary recommendations from the listed sources are:

  • Don't use VLAN 1 (the default VLAN) for anything
  • Any ports that do VLAN trunking should use a dedicated VLAN ID
  • Do explicit tagging of the native VLAN on trunking ports
  • Set all end-user ports to non-trunking (a.k.a. "access ports"?)
  • Disable unused ports and put them in a separate VLAN
  • Disable Spanning Tree Protocol (STP) on end-user ports
  • Use MD5 authentication for Virtual Trunking Protocol (VTP)
  • Physically secure the switch and control access to the management functions

Reference Links:

  1. Virtual LAN Security: weaknesses and countermeasures (SANS)
  2. VLAN Hacking (InfoSec Institute)
  3. VLAN Hopping (Wikipedia)

Thursday, May 21, 2015

Firewall build (part 2) hardware needed for VPN duties

One thing to think about when sizing the hardware for a firewall is how much CPU power will be needed for OpenVPN (or IPSec / L2TP).  OpenVPN comes with a built in "speed" command which will benchmark your system and give you an idea of maximum possible bandwidth.

Just run "openssl speed" at the command line and look for the AES-128 and/or Blowfish results.  I prefer to look at the 1024 byte or 8192 byte columns in the output to figure out the upper range.  While Blowfish is good at the smaller block sizes, AES-128 catchs up and surpasses it with the larger block sizes.

Values at or above 100000k should indicate that the firewall has enough performance to drive an OpenVPN connection at close to gigabit speeds.  Or handle multiple OpenVPN connections at the same time, without completely saturating the CPU.

AMD Opteron 2210 HE @ 1.8GHz
type             16 bytes     64 bytes    256 bytes   1024 bytes   8192 bytes
blowfish cbc     68358.84k    74350.46k    75845.03k    76373.67k    76556.97k
aes-128 cbc      50477.29k    53816.28k    55093.08k   128709.63k   130465.79k

AMD Phenom II X4 810 @ 2.6GHz
type             16 bytes     64 bytes    256 bytes   1024 bytes   8192 bytes
blowfish cbc     94477.65k   101825.28k   103154.35k   103857.83k   104060.25k
aes-128 cbc      76376.65k    81608.09k    83915.50k   213516.45k   216016.95k

AMD Opteron 4180 @ 2.6GHz
type             16 bytes     64 bytes    256 bytes   1024 bytes   8192 bytes
blowfish cbc     93781.75k   101154.41k   102983.68k   103730.52k   103923.71k
aes-128 cbc      76233.64k    81631.07k    83197.52k   213366.89k   215309.87

103720k = 98 MiB/s or ~980Mbps, which is pretty close to gigabit speeds

General guidelines/notes:

  • I'm a firm believer in multi-core for servers and desktops.  So look for hardware that is at least dual-core when shopping.  An inexpensive quad-core would be even better and give a bit of headroom for monitoring tasks.
  • For the AMD CPUs (Opteron / Athon64 / Phenom) made in 2007-2011, you'll want at least a 2.2GHz core.  For Intel Core2 CPUs or 1st/2nd generation i3/i5, try to get at least a 2.0GHz core.
  • Intel Atom CPUs are underpowered, the 1.8GHz dual-core units are reported to top out at around 500Mbps for general routing and definitely can't handle gigabit speed OpenVPN.  But they are low power, so maybe that outweighs the performance issue.  A rule of thumb is that the Atom CPUs are about 1/3 to 1/2 as powerful as i3/i5 for the same clock speed.

Resource links:

Sunday, May 17, 2015

md5sum bash script to create check file for a directory tree

Just a quick script that will run through the current directory and all descendant directories, creating a single "verify-tree.md5" file (using md5sum). If the check file already exists, then the existing one gets moved out of the way to "verify-tree.yyyymmdd-hhmmss.md5" for safekeeping.

Check files are useful for whenever you have a set of files that will not (or should not) change over time.  Such as files written off to an archive tape / disk / optical media / flash drive.  While the media and file system might also do some checking, it's good to have a second layer that is under your control and which can be queried.

(If you want file recovery features, you should look into MultiPar or par2j.)

The script's output looks like:

The file (verify-tree.md5) that is created is a standard md5sum file and can be read by just about any compatible software.  Some software cannot handle sub-folders, however, so you may have to use the md5sum program to do the verification.


# stop script on errors
set -e


if [[ -e "${FILENAME}.md5" ]]; then
    mv "${FILENAME}.md5" \
    "${FILENAME}.$(date --reference=${FILENAME}.md5 '+%Y%m%d-%H%M%S').md5"

echo ""
echo "Output Filename: ${FILENAME}.md5"
echo "Files Found: $(find . -type f -not -name "${FILENAME}*.md5" | wc -l)"
echo "Size: $(du -chs . | grep 'total')"

time find . -type f -not -name "${FILENAME}*.md5" \
    -exec ${PROG} "{}" \; >> "${FILENAME}.md5"

echo ""
echo "Files Processed: $(wc -l ${FILENAME}.md5)"

echo ""
echo "Checking..."
time ${PROG} -c --quiet "${FILENAME}.md5"
echo ""
echo "All files verified."

Note: There are multiple ways to write the md5sum line.  It's probably better to use the xargs method, but I have not tested it out.

find . -type f -not -name "${FILENAME}*.md5" -exec ${PROG} "{}" \; >> "${FILENAME}.md5

find . -type f -not -name "${FILENAME}*.md5" -print0 | xargs -0 ${PROG} >> "${FILENAME}.md5"

find . -type f -not -name "${FILENAME}*.md5" | xargs ${PROG} >> "${FILENAME}.md5"

It's also easy to adapt this script to work with sha256sum or sha1sum.

The verification script is a trimmed down version of the original script:


# stop script on errors
set -e


echo ""
echo "Checking: ${FILENAME}.md5"
echo "File Hashes: $(wc -l ${FILENAME}.md5)"
time ${PROG} -c --quiet "${FILENAME}.md5"
echo ""
echo "All files verified."

I have tested this in CygWin 64-bit on Windows 7 64-bit Professional, but it should also work fine on Linux/Unix/OSX systems as long as the md5sum command is available.  The script is conservative in design with very few "tricks" so it should be portable.

Sunday, May 10, 2015

MD5 vs SHA-1 vs SHA-256 performance

I was curious this weekend about how MD5 vs SHA-1 vs SHA-256 performance stacks up.  If you have the OpenSSL libraries installed, you can run a short test to calculate performance on your CPU.  It gives ballpark estimates, which may or may not carry over to real-world performance on actual file data.

$ openssl speed md5 sha1 sha256


SHA-1 is about 55-75% the speed of MD5
SHA-256 is about 25-40% the speed of MD5
SHA-256 is about 50-60% the speed of SHA-1

Whether or not you will be CPU-bound in computing the file hashes will depend on where you are reading the files from.  Over gigabit LAN or from older mechanical hard drives, or external USB2/USB3 drives, most modern CPUs can keep up even with SHA-256.  But if you are reading the files off of a local SSD or really fast mechanical drive (or RAID) then you are likely to be bottlenecked by the CPU.

Details (openssl test):

AMD Opteron 2210 HE @ 1.8GHz
type             16 bytes     64 bytes    256 bytes   1024 bytes   8192 bytes
md5              25350.32k    80336.17k   191208.72k   295806.29k   353239.04k
sha1             27497.51k    77933.74k   168168.28k   235850.41k   268205.53k
sha256           22543.94k    51445.66k    88818.90k   108275.37k   116416.81k

AMD Opteron 6212 @ 2.6GHz
type             16 bytes     64 bytes    256 bytes   1024 bytes   8192 bytes
md5              45318.86k   134308.27k   324486.83k   493194.58k   582216.36k
sha1             47027.79k   128927.42k   278593.02k   410244.44k   471425.02k
sha256           28690.18k    62662.12k   106333.61k   127998.98k   136325.80k

AMD Opteron 1214 @ 2.2GHz
type             16 bytes     64 bytes    256 bytes   1024 bytes   8192 bytes
md5              30417.85k    96951.65k   235868.24k   361845.33k   433094.16k
sha1             34126.39k    98072.04k   208456.70k   291041.35k   328654.85k
sha256           27362.92k    63127.50k   108537.35k   132564.38k   142727.86k

AMD Opteron 4180 @ 2.6GHz
type             16 bytes     64 bytes    256 bytes   1024 bytes   8192 bytes
md5              43180.73k   129437.27k   298339.15k   442248.19k   519602.18k
sha1             45699.96k   123160.41k   255322.20k   347329.88k   390250.50k
sha256           33757.18k    75191.85k   128929.37k   157659.82k   168790.70k

Intel Xeon E5520 @ 2.27GHz
type             16 bytes     64 bytes    256 bytes   1024 bytes   8192 bytes
md5              25945.50k    88897.43k   220292.01k   354356.91k   430339.41k
sha1             26568.69k    78716.78k   174506.35k   251495.08k   288858.11k
sha256           20495.77k    49206.73k    90735.59k   114904.75k   124605.78k

AMD Phenom II X4 810 @ 2.6GHz
type             16 bytes     64 bytes    256 bytes   1024 bytes   8192 bytes
md5              42430.27k   130101.07k   301091.11k   444996.84k   522918.05k
sha1             46015.86k   125298.40k   256391.07k   349968.95k   393227.03k
sha256           34432.84k    76378.03k   130828.67k   158859.76k   168782.78k

Additional tests/experiments:

Tests using "(sha256|sha1|md5)sum" programs on Opteron 4180 w/ SSDs, using 133GB of large files.  The CPU core assigned to the task was pegged at 100% utilization according to "atop".  The SSD used has an estimated top speed of 490 MB/s.

MD5: ~318 MiB/s (428 sec)
SHA-1: ~187 MiB/s (729 sec) - 1.7x slower then MD5
SHA-256: ~113 MiB/s (1204 sec) - 2.8x slower then MD5, 1.65x slower then SHA-1

Sunday, April 26, 2015

Firewall build (part 1)

Part of moving to a new place is reevaluating your network.  On my current network, I have a fairly basic setup:

  • One WiFi Access Point (WAP) running 802.11 b/g
  • A Linux server acting as firewall / file share / backup storage
  • A few laptops
  • A few tablets/phones
  • A few other PCs
When I set this all up a few years ago I kept it very simple.  The Linux server is the gateway device with routing / filtering / NAT and other features.  The WAP is part of the internal network running in WPS/PSK mode with a very long and randomly generated password.

After I move, I want to accomplish a few things:
  • Use a refurb or low-power PC to run just the firewall / VPN
  • Put the WiFi access point on a separate NIC
  • Possibly run a DMZ
  • Provide limited guest WiFi
  • Evaluate pfSense instead of Linux+Shorewall
To do all that, I need a minimum of four network ports for perfect security or something with two ports if I use VLANs (not as safe, more difficult to configure and get right).

I've done some looking around and while a low-power 25-35W compact PC for the firewall would be nice, it would cost me around $600.  Maybe $400-$500 if I shop around.  There are also the really tiny units that will run monowall (m0n0wall), but those are also $200-$300 for something that will handle the faster WiFi / FIOS / cable modems.  Plus it can be difficult to find something with four network ports.

Firewalls don't need a lot of CPU power, but a dual/quad CPU Intel Atom isn't enough.  An i5/i7 would likely be complete overkill, even for 802.11ac / 802.11n or gigabit traffic.  The older Pentium / Celeron / Core Duo are probably a bit on the slow side.  The AMD Phenoms or Athlon64 chips are probably okay.

So what I've settled on is a refurbished PC that is at least a Core 2 Duo (2 cores) with 4GB of RAM, along with a refurbished NIC.  The pfSense distro only needs a handful of gigabytes to install, so any unit with at least 40GB of space will be plenty.  The base units can be picked up for as little as $50-$125 for the base computer, and add-in NIC cards are $10-$40 depending on what you use.  If the box dies, I get another and move the drive over.  If one of the NICs fry, I can pickup another NIC.  Power requirements will probably be around 80W to 120W.

For the smaller sized PCs, you might only have 1-2 expansion slots which means you'll need a multi-port NIC. The cost of the dual-port NICs is likely to be more then what you pay for the base PC.  I've seen dual-port refurbished NICs for as low as $50, but paying $100-$150 is more likely.  However, good NICs tend to work fine for close to a decade, and it can be moved from PC to PC.

Friday, August 08, 2014

Postfix: Calculate number of TLS encrypted SMTP sessions

I was curious as to what amount of SMTP traffic is encrypted to our servers.

This assumes that you are running Postfix, and you might need to adjust smtpd_tls_loglevel to be 1 or 2.  I'm not sure if this catches all instances where the SMTP connection switches to SSL, or just those that support TLS.

# fgrep 'postfix/smtpd' maillog* | fgrep ': connect from' | wc -l
# fgrep 'postfix/smtpd' maillog* | fgrep ': setting up TLS connection' | wc -l

One box #1 that we have at the office:

16151 out of 293746 connections were TLS (5.5%)

On box #2:

27485 out of 654294 connections were TLS (4.2%)

A very rough estimate is that one connection = one message delivered to the server.  Assuming that is true, only 4-5% of SMTP traffic to our domains (via port 25/tcp) is sent over an encrypted channel.  On the other hand, probably 90% of all of our connections are spam zombies who probably don't do TLS.  In order to dig deeper, I would have to tie every non-spam message to a specific connection in the Postfix log file.