<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AndrewKMitchell.com</title>
	<atom:link href="http://www.andrewkmitchell.com/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.andrewkmitchell.com/blog</link>
	<description>The online home of Andrew Kenton Mitchell</description>
	<lastBuildDate>Thu, 22 Jul 2010 12:15:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>SELinux from scratch</title>
		<link>http://www.andrewkmitchell.com/blog/?p=313</link>
		<comments>http://www.andrewkmitchell.com/blog/?p=313#comments</comments>
		<pubDate>Thu, 22 Jul 2010 12:15:25 +0000</pubDate>
		<dc:creator>Andrew Mitchell</dc:creator>
				<category><![CDATA[InfoTech]]></category>

		<guid isPermaLink="false">http://www.andrewkmitchell.com/blog/?p=313</guid>
		<description><![CDATA[Introduction SELinux is a mandatory access control (MAC) system available in Linux kernels as of version 2.6. Of the Linux Security Modules available, it is the most comprehensive and well tested, and is founded on 20 years of MAC research. SELinux combines a type-enforcement server with either multi-level security or an optional multi-category policy, and [...]]]></description>
			<content:encoded><![CDATA[<p><a name="N10052">Introduction</a></p>
<p>SELinux is a mandatory access control (MAC) system available in Linux  kernels as of version 2.6. Of the Linux Security Modules available, it  is the most comprehensive and well tested, and is founded on 20 years of  MAC research. SELinux combines a type-enforcement server with either  multi-level security or an optional multi-category policy, and a notion  of role-based access control.</p>
<p>Most people who have used SELinux have done so by using an SELinux-ready  distribution such as Fedora, Red Hat Enterprise Linux (RHEL), Debian,  or hardened Gentoo. These enable SELinux in the kernel, offer a  customizable security policy, and patch a great number of user-land  libraries and utilities to make them SELinux aware.</p>
<p>If you&#8217;re like many users who simply want the system to work as before,  but a bit more securely, you can query and manipulate SELinux by using  familiar applications and by writing security policies using a higher  level language.  However, these methods can be insufficient when  something breaks &#8212; such as when kernel and user-space get out of sync.   Also, these methods might even hinder the UNIX® engineer from  understanding how SELinux is actually working. Finally, the engineer and  the security community should understand that there are appropriate  ways to use SELinux outside of the conventions in use by current  distributions.</p>
<p>In this article, learn how to convert a system that is initially  completely unaware of SELinux into one that enforces SELinux.  You also  learn how to enforce a few simple access policies.</p>
<div>
<hr /></div>
<p><a name="N1006B">Preliminaries</a></p>
<p>To get started, you need:</p>
<ul>
<li>QEMU, a freely available and easy-to-use processor emulator. It is  an excellent way to play with new kernels or system images without doing  harm to your real system. You can download it from the <a href="http://www.qemu.org/download.html">QEMU download page</a>.</li>
<li> Gentoo, a source-based Linux distribution.  Gentoo is ideal for this  exercise, since you can install it under a running system regardless of  the distribution. Find the latest version at the <a href="http://www.gentoo.org/main/en/where.xml">Gentoo download page</a>.</li>
</ul>
<p>Create a disk image by typing:</p>
<p><code> qemu-img create -f raw gentoo.img 2G </code></p>
<p>The next step is to start up QEMU to partition the raw disk image and  format a single partition.  This requires some sort of Linux boot CD.   Knoppix will do, as will a Gentoo liveCD.  You can download a Gentoo  liveCD as follows:</p>
<p><code> wget  ftp://ftp.gtlib.cc.gatech.edu/pub/gentoo/releases/x86/current/installcd/install-x86-minimal-2006.0.iso </code></p>
<p>For easier future reference &#8212; and less typing &#8212; you might want to  rename the image:</p>
<p><code> mv install-x86-minimal-2005.1.iso gentoo.iso </code></p>
<p>The following command instructs QEMU to use gentoo.iso as its CD, use  gentoo.img as its hard disk, and boot from the CDROM:</p>
<p><code> qemu -hda gentoo.img -cdrom gentoo.iso -boot d </code></p>
<p>Just hit <strong>Return</strong> for the default kernel.  Then, type the following  to partition the disk image:<br />
<a name="N100B6"><strong>Listing 1. Partition a disk image</strong></a></p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>fdisk /dev/hda
n
p
1
(return)
w
</pre>
</td>
</tr>
</tbody>
</table>
<p>The above listing creates a single new (n) primary (p) partition  beginning at block 1 (1) and ending at the default ending block, which  is the last block on the file system.  It then writes (w) the new  partition table to the disk image.</p>
<p>Now, you will be returned to the shell prompt in QEMU.  Here, type:</p>
<p><code> mkfs.ext2 /dev/hda1 </code></p>
<p>You may have to do that twice in order to give <code>udev</code> a  chance to create the device. Then, shut down by typing:</p>
<p><code> poweroff </code></p>
<p>This returns you to your real system&#8217;s shell. If it does not, and hangs  for a long time, hit <strong>Ctrl-c</strong> to end it.</p>
<p>The next step is to lay a basic distribution down on the disk image. One  reason Gentoo is a great fit for this exercise is because you can  simply download and extract a &#8220;stage 3&#8243; image, giving you a functional  Gentoo system.  You should find a local mirror from which to download a  stage 3 tarball.  An example location, if you are located near Sandia  National Laboratories, is:</p>
<p><code> wget  ftp://mirror.iawnet.sandia.gov/pub/gentoo/releases/x86/current/stages/stage3-x86-2006.0.tar.bz2 </code></p>
<p>This file contains a compressed archive of a full Gentoo system.  To  extract this into your disk image, first mount the disk image onto your  system. The following listing mounts the empty file system and expands  the tarball onto it.<br />
<a name="N100EE"><strong>Listing 2. Extract Gentoo tarball onto disk  image</strong></a></p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>su  (give root password)
ORIG=`pwd`
mount -oloop,offset=32256 gentoo.img /mnt
cd /mnt
tar jxf $ORIG/stage3-x86-2005.1.tar.bz2
</pre>
</td>
</tr>
</tbody>
</table>
<p>While the image is still mounted, there are a few more preliminaries to  take care of. The /etc/fstab file instructs the system which file  systems to mount where.  For this simple system, no boot or swap entries  are needed.  The following listing removes those and inserts a correct  entry for the root partition.  It also  sets the password for the root  user.  Make sure that when running <code>passwd</code>, you pick a  password you remember.  Security is not a concern for this toy system,  so &#8220;password&#8221; will do.<br />
<a name="N100FF"><strong>Listing 3. System preliminaries</strong></a></p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>mv /mnt/etc/fstab /mnt/etc/fstab.orig
sed -e '/[BR]OOT/d' -e '/SWAP/d' /mnt/etc/fstab.orig &gt; \
   /mnt/etc/fstab

cat &gt;&gt; /mnt/etc/fstab &lt;&lt; EOF
/dev/hda1 / ext2 noatime 0 1
EOF

chroot /mnt
passwd
exit

cd $ORIG
umount /mnt
</pre>
</td>
</tr>
</tbody>
</table>
<p>A kernel is needed for the QEMU image to boot.  Simply compile this with  SELinux support now, even though you won&#8217;t be using SELinux just yet.  Grab a copy of linux-2.6.14.tar.bz2 (or the latest version) from  kernel.org and extract it:</p>
<p><code> wget http://kernel.org/pub/linux/kernel/v2.6/linux-2.6.14.tar.bz2<br />
tar jxf Linux-2.6.14.tar.bz2 </code></p>
<p>Then, change into the Linux-2.6.14 directory and configure the kernel,  like so:</p>
<p><code> cd linux-2.6.14<br />
make defconfig<br />
make menuconfig </code></p>
<p>Make sure to enable at least the following options:<br />
<a name="N10126"><strong>Listing 4. Excerpts from kernel .config</strong></a></p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>CONFIG_EXT2_FS=y
CONFIG_EXT2_FS_XATTR=y
CONFIG_EXT2_FS_SECURITY=y

CONFIG_SECURITY=y
CONFIG_SECURITY_CAPABILITIES=y
CONFIG_SECURITY_SELINUX=y
CONFIG_SECURITY_SELINUX_BOOTPARAM=y
CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE=1
CONFIG_SECURITY_SELINUX_DISABLE=y
CONFIG_SECURITY_SELINUX_DEVELOP=y
CONFIG_SECURITY_SELINUX_AVC_STATS=y
CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE=1
</pre>
</td>
</tr>
</tbody>
</table>
<p>Now, compile this kernel using the following command:</p>
<p><code> make<br />
cp arch/i386/boot/bzImage .. </code></p>
<p>Surely, you&#8217;d like to test this system before converting to SELinux.   You can do this using this command:</p>
<p><code> qemu -hda gentoo.img -kernel bzImage -append "ro root=/dev/hda1" </code></p>
<p>Look around, add some users, and play with your system.  When you&#8217;re  done, shut it down using <code>poweroff</code>.  Now, you&#8217;re ready to  begin converting this system to SELinux.</p>
<div>
<hr /></div>
<p><a name="N10149">Converting to SELinux</a></p>
<p>To convert this system to one that supports and enforces SELinux, you  need to modify the program that begins booting the system, /sbin/init,  and add a few other pieces of software to build SELinux policy and  interact with SELinux.</p>
<p><a name="N10153">SysVinit</a></p>
<p>Begin by grabbing a clean copy of SysVInit. Download version 2.86 from  ftp://ftp.cistron.nl/pub/people/miquels/sysvinit/sysvinit-2.86.tar.gz,  or look for the latest version on the <a href="http://freshmeat.net/projects/sysvinit/">SysVinit page</a> on  freshmeat. You can download a patch (sysvinit-init.c.diff) in the zip  file available in the <a href="http://www.ibm.com/developerworks/linux/library/l-selinux.html?ca=drs-tp1906#download">Downloads</a> section, below; this patch will be applied on top of the file init.c  using the procedure in Listing 5.  This patch causes the SELinux policy  to be loaded at boot.  It introduces a flag <code>no_selinux</code>,  which is set to 1 if the boot command includes <code>-p</code>.  If that  flag is set, a warning is given and boot proceeds as normal.   Otherwise, the function <code>load_policy()</code>, which is defined  further in the patch, is called.<br />
<a name="N10174"><strong>Listing 5. Partition a disk image</strong></a></p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>wget ftp://ftp.cistron.nl/pub/people/miquels/sysvinit/sysvinit-2.86.tar.gz
cd sysvinit-2.86
cd src
patch -p0 &lt; $ORIG/sysvinit-init.c.diff
make init
(su)
mount -oloop,offset=32256 $ORIG/gentoo.img /mnt
mv /mnt/sbin/init /mnt/sbin/init.good
cp init /mnt/sbin/init
umount /mnt
exit
</pre>
</td>
</tr>
</tbody>
</table>
<p>The function <code>load_policy()</code> mounts an instance of the  selinuxfs file system on the directory /selinux, and opens the file  /etc/policy.bin for reading.  Note that both /selinux and  /etc/policy.bin are arbitrary names so long as any userspace tools that  might want to use them agree on the naming.  When an SELinux policy is  compiled, the binary version will need to be copied to /etc/policy.bin.   Next, <code>load_policy()</code> <code>mmap()</code>s the file  /etc/policy.bin, and calls <code>security_load_policy</code>, which is  defined immediately above <code>load_policy</code>. It, in turn, opens  the file /selinux/load, which is a file in the selinuxfs file system,  for writing, and writes the entire contents of /etc/policy.bin into  /selinux/load.  This is how the binary policy will be loaded into the  kernel at boot.</p>
<p><a name="N10191">Creating a policy</a></p>
<p>SELinux is a flexible access control system whose access decisions are  guided by an administrator-definable policy. Policies depend on the  layout and configuration of files on the system.  Distributions that  support SELinux come with a predefined policy.  While it is possible to  edit the policy, you generally edit policy only to the extent of  excluding whole chunks of policy relating to software not installed on  the system.  More detailed policy editing generally belongs to the realm  of SELinux experts.  However, in this exercise, you will define your  own policy.</p>
<p>Because new object classes and permissions are introduced from time to  time, policies end up being somewhat dependent on kernel versions.  The  program located in mdp.tar.gz (in the zip file in the <a href="http://www.ibm.com/developerworks/linux/library/l-selinux.html?ca=drs-tp1906#download">Downloads</a> section, below) will automatically construct a dummy policy based on  your kernel version.  Since it will need to analyze the kernel source,  you will need to point it at the source tree that you used to compile  the kernel for the QEMU image.  Extract mdp.tar.gz into $ORIG/ and enter  the resulting directory, mdp/.</p>
<p>Type:</p>
<p><code> ./build.sh -k ../linux-2.6.14. </code></p>
<p>This compiles the program mdp and runs it with the kernel directory as  an argument. mdp in turn looks at the object classes and permissions  that the kernel knows about and creates an SELinux policy containing  this data. The resulting policy will have one SELinux user, one SELinux  role, and one SELinux type.  The single type will be applied to all  files, kernel objects, and processes, and will have full access to  itself.  We will play with customizations to this policy later, but for  now it is sufficient to initialize and use SELinux.</p>
<p>When build.sh runs mdp, it will create several files. You need  policy.conf and file_contexts.  The former is the text representation of  the policy, the latter contains the instructions for how to label the  file system.</p>
<p>I&#8217;ll defer a comprehensive study of policy.conf for now, so skip the  first five or six hundred lines to this line:</p>
<p><code> type base_t; </code></p>
<p>This is the only type defined in this policy.  The next line:</p>
<p><code> role base_r types { base_t }; </code></p>
<p>defines a single role, <code>base_r</code>, which is associated with  type <code>base_t</code>.  This means that a process in role <code>code_r</code> may run under domain <code>base_t</code>.  Next comes a long list of <code>allow</code> statements, one specifying each object class, giving the process under <code>domain  base_t</code> access to the object classes of type <code>base_t</code> for all permissions defined for the class.</p>
<p>Next, the policy defines a user, <code>user_u</code>, which is  associated with role <code>base_r</code>.  A process that is running as  SELinux user <code>user_u</code>, then, can run under role <code>base_r</code>.   You&#8217;ve already seen that <code>base_r</code> is associated with type <code>base_t</code>,  so these two statements together make <code>user_u:base_r:base_t</code> a valid context, meaning a process will be allowed to run under that  context.</p>
<p>The next few lines are also of interest.  The first:</p>
<p><code> sid kernel user_u:base_r:base_t </code></p>
<p>assigns the single valid context to the first process on the system. Two  lines later, you see:</p>
<p><code> sid unlabeled user_u:base_r:base_t </code></p>
<p>which assigns the same context to any files that are otherwise  unlabeled.</p>
<p>Now take a look at the file called file_contexts.  It contains two  lines.  The format is:</p>
<p><code> &lt;regexp&gt;  &lt;context&gt; </code></p>
<p>where <code>regexp</code> is a regular expression used to compare  against file names, and <code>context</code> is an SELinux context to  apply to a file if it matches the regular expression. The line:</p>
<p><code> /.* user_u:base_r:base_t </code></p>
<p>is used to assign the context to all files on the system.</p>
<p><a name="N10239">Installing SELinux  userspace</a></p>
<p>The next step is to install some code that will compile the SELinux  policy, which you&#8217;ve written in text form, to the binary format, which  the kernel requires, as well as a program to label your root file  system.  The source is available through CVS from SourceForge by using  the command shown in Listing 6:<br />
<a name="N10246"><strong>Listing 6. Check out SELinux userspace source</strong></a></p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>su
mount -oloop,offset=32256 $ORIG/gentoo.img /mnt
cd /mnt/usr/src
cvs -z3 -d:pserver:anonymous@cvs.sf.net:/cvsroot/selinux co -P \
nsa/selinux-usr
</pre>
</td>
</tr>
</tbody>
</table>
<p>Copy the plain text policy onto the disk image, like so:</p>
<p><code> cp -r $ORIG/mdp /mnt/usr/src </code></p>
<p>Enter the nsa/selinux-usr directory, and compile libsepol, checkpolicy,  libselinux, and policycoreutils, as follows:<br />
<a name="N1025D"><strong>Listing 7. Compile SELinux userspace code</strong></a></p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>chroot /mnt
cd /usr/src/nsa/selinux-usr/
cd libsepol/
make &amp;&amp; make install
cd ../libselinux/
make &amp;&amp; make install
cd ../checkpolicy/
make &amp;&amp; make install
cd ../policycoreutils/
make &amp;&amp; make install
</pre>
</td>
</tr>
</tbody>
</table>
<p>If you encounter an error during the last step, make certain that  setfiles is installed by typing this:<br />
<a name="N1026A"><strong>Listing 8. Install setfiles</strong></a></p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>cd setfiles
make
make install
</pre>
</td>
</tr>
</tbody>
</table>
<p>Now, to compile the policy, use the checkpolicy program like this:<br />
<a name="N10277"><strong>Listing 9. Compile SELinux policy</strong></a></p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>cd /usr/src/mdp
checkpolicy -o policy.bin policy.conf
cp policy.bin /etc/
exit  # exit chroot
exit  # exit root shell
</pre>
</td>
</tr>
</tbody>
</table>
<p>Relabeling the file system requires booting into the virtual machine.  Remember to specify the <code>-p</code> option for now, so that  /sbin/init will not attempt to load an SELinux policy:</p>
<p><code> qemu -hda gentoo.img -kernel bzImage -append "ro root=/dev/hda1 -p" </code></p>
<p>SELinux interacts with userspace programs through its own file system,  selinuxfs.  The userspace programs expect this to be mounted under  /selinux.  Create the /selinux directory and relabel the file system, as  follows:<br />
<a name="N10292"><strong>Listing 10. Relabel the file system</strong></a></p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>mkdir /selinux
cd /usr/src/mdp
setfiles file_contexts /
poweroff
</pre>
</td>
</tr>
</tbody>
</table>
<p>Finally, you can reboot under SELinux!</p>
<p><code> qemu -hda gentoo.img -kernel bzImage -append "ro root=/dev/hda1" </code></p>
<p><a name="N102A2">Studying the SELinux policy</a></p>
<p>SELinux makes access decisions based on the security contexts assigned  to processes, files, and other objects.  SELinux provides interfaces for  querying these contexts and, given the required access rights, to set  them.  For instance, SELinux reports process contexts through the <code>procattr</code> interface.  If you type:</p>
<p><code> cat /proc/$$/attr/current </code></p>
<p>you see the context of the current process (<code>$$</code>).  By using  the script pidctx.sh (see the zip file in the <a href="http://www.ibm.com/developerworks/linux/library/l-selinux.html?ca=drs-tp1906#download">Downloads</a> section, below), you can easily view the context of all processes on  the system.  The script simply prints the /proc/&lt;pid&gt;/attr/current  file for each process on the system.</p>
<p>SELinux stores file contexts using extended attributes. Most persistent  file systems under Linux (ext2, ext3, jfs, xfs, etc.) support extended  attributes, though reiserfs is an unfortunate notable exception. These are (name, value) pairs of data that are associated  with inodes, where the name is separated by periods into namespaces. The  SELinux extended attributes are in the security namespace and  identified by &#8220;selinux&#8221;, so the full name of the xattr value is  &#8220;security.selinux&#8221;.  A new set of system calls allows userspace to query  and set extended attributes.  The system call to query an xattr is <code>getxattr(2)</code>.   It takes a file name, an attribute name, a buffer in which to return  the value of the xattr, and the size of the provided buffer.</p>
<p>The showctx.c file simply runs through all the file names provided as  command line arguments and prints the value of the <code>security.selinux</code> extended attribute, provided that it exists, is readable, and fits  within a reasonable size.</p>
<p>You can download showctx.tar.gz from the zip file in the <a href="http://www.ibm.com/developerworks/linux/library/l-selinux.html?ca=drs-tp1906#download">Downloads</a> section, below, and extract showctx.c.  Then get it into your QEMU  machine some way. One way is to shut down the QEMU image and then do  this:<br />
<a name="N102DA"><strong>Listing 11. Install showctx</strong></a></p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>(su)
mount -oloop,offset=32256 $ORIG/gentoo.img /mnt
cp showctx.c /mnt/usr/src
umount /mnt
exit
</pre>
</td>
</tr>
</tbody>
</table>
<p>Now start QEMU, if it is not already running.  To compile showctx, type:</p>
<p><code> gcc -o showctx showctx.c cp showctx /bin/ </code></p>
<p>Now you can type:</p>
<p><code> showctx / /tmp /home /root /usr/src </code></p>
<p>Of course, you&#8217;ll see the same context for each file.  The next section  makes this somewhat more interesting by enhancing the policy.</p>
<div>
<hr /></div>
<p><a href="http://www.ibm.com/developerworks/linux/library/l-selinux.html?ca=drs-tp1906#ibm-pcon">Back to top</a></p>
<p><a name="N102F7">Playing with SELinux policy</a></p>
<p><a name="N102FE">Secret type</a></p>
<p>You&#8217;ll create a secret directory, /secret, under which SELinux should  allow no process to read.  Begin by actually creating the directory and a  few files under the directory:</p>
<p><code> mkdir /secret echo "hello, world" &gt; /secret/helloworld echo "You can't see me" &gt; /secret/dontlook </code></p>
<p>In the SELinux policy, now create a new type, <code>secret_t</code>, to  which the other type, <code>base_t</code>, has no permissions.  First,  you define the type by adding:</p>
<p><code> type secret_t; </code></p>
<p>after the declaration of <code>base_t</code> in policy.conf.  In  addition, since the files of type <code>secret_t</code> will be of role <code>base_r</code>,  the role <code>base_r</code> must be allowed to associate with <code>secret_t</code>.   The line following the one you just added reads:</p>
<p><code> role base_r types { base_t }; </code></p>
<p>Edit this to read:</p>
<p><code> role base_r types { base_t secret_t }; </code></p>
<p>Now, to recompile this policy, type:</p>
<p><code> checkpolicy -o policy.bin policy.conf<br />
cp policy.bin /etc/ </code></p>
<p>Next add the following lines to the file_contexts file:<br />
<a name="N1035B"><strong>Listing 12. File contexts for /secret </strong></a></p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>/secret user_u:base_r:secret_t
/secret/helloworld user_u:base_r:base_t
/secret/.* user_u:base_r:secret_t
</pre>
</td>
</tr>
</tbody>
</table>
<p>This tells the system that the directory /secret, and any file name  underneath it, should be of type <code>secret_t</code>, except for the  file /secret/helloworld, which should still be of type <code>base_t</code>.   To assign these contexts on disk, use <code>setfiles</code>:</p>
<p><code> setfiles file_contexts / </code></p>
<p>Oh no, an error! SELinux doesn&#8217;t yet know about the type <code>secret_t</code>.   It is, in fact, possible to dynamically reload the SELinux policy. For  now, however, for the sake of simplicity, just reboot the QEMU image.   Of course, since a bootloader was never installed, a simple reboot won&#8217;t  work.  Therefore, you need to type <code>poweroff</code> to shut down  the QEMU image.  If the window does not disappear after &#8220;Power down&#8221; is  printed, then <strong>Ctrl-c</strong> the <code>qemu</code> command.  Rerun the <code>qemu</code> command to restart it.  Then try running the above <code>setfiles</code> command again.</p>
<p>Verify that the policy reload worked:</p>
<p><code> showctx / /secret /secret/helloworld /secret/dontlook cat /secret/dontlook </code></p>
<p>But wait!  You can see the secret types.</p>
<p>This is the result of one of the compile time kernel options, <code>CONFIG_SECURITY_SELINUX_DEVELOP</code>.   This option defaults SELinux into non-enforcing mode.  To verify this,  type:</p>
<p><code> cat /selinux/enforce </code></p>
<p>which should return 0.  To set SELinux into enforcing mode, type:</p>
<p><code> echo 1 &gt; /selinux/enforce </code></p>
<p>You could do this automatically at boot through an init script if you  wanted to, as in Listing 13, or simply compile a kernel without <code>SELINUX_DEVELOP</code> support.<br />
<a name="N103C1"><strong>Listing 13. Set SELinux enforcing mode on boot</strong></a></p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<pre>cat &gt;&gt; /etc/rc.d/selinux-enforce &lt;&lt; EOF
#!/bin/sh
echo 1 &gt; /selinux/enforce
EOF
chmod ugo+x /etc/rc.d/selinux-enforce
rc-update add default selinux-enforce
</pre>
</td>
</tr>
</tbody>
</table>
<p>Now that SELinux is in enforcing mode, try browsing under /secret again.   Note that even though your process context has the rights to read  /secret/helloworld, you can&#8217;t actually read it because you cannot get  past /secret.  A hardlink to the file could, of course,  bypass this.  This is not true for other files under /secret/, because the files  themselves are unreadable.</p>
<div>
<hr /></div>
<p><a name="N103CA">Conclusion </a></p>
<p>In this article, you&#8217;ve seen how to reproduce much of the work that  has already been done in some distributions to incorporate SELinux.  If  it seemed daunting, rest assured &#8212; by simply installing a fresh copy of  recent Fedora, you can be running SELinux so transparantly that you may  not even know it is installed.  The point of this excercise, then, was  not to persuade you that security is intrusive, but rather to explain  some of what goes on behind the scenes in a properly integrated SELinux  system.</p>
<p><!-- CMA ID: 111681 --> <!-- Site ID: 1 --><!--XSLT stylesheet used to transform this file: dw-document-html-6.0.xsl--></p>
]]></content:encoded>
			<wfw:commentRss>http://www.andrewkmitchell.com/blog/?feed=rss2&amp;p=313</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FreeBSD review and howtos from a Linux user</title>
		<link>http://www.andrewkmitchell.com/blog/?p=309</link>
		<comments>http://www.andrewkmitchell.com/blog/?p=309#comments</comments>
		<pubDate>Mon, 07 Jun 2010 12:27:02 +0000</pubDate>
		<dc:creator>Andrew Mitchell</dc:creator>
				<category><![CDATA[InfoTech]]></category>

		<guid isPermaLink="false">http://www.andrewkmitchell.com/blog/?p=309</guid>
		<description><![CDATA[I recently decided to give the new 8.0 release of FreeBSD a go and was fairly impressed. I did use BSD along time ago on a home server for a few months but pretty much forgot everything about it from back then.  This time my implementation is for a router/firewall solution.  However, today we&#8217;ll only [...]]]></description>
			<content:encoded><![CDATA[<p>I recently decided to give the new 8.0 release of <a href="http://en.wikipedia.org/wiki/FreeBSD">FreeBSD</a> a go and was  fairly impressed. I did use BSD along time ago on a home server for a  few months but pretty much forgot everything about it from back then.  This time my implementation is for a router/firewall solution.  However, today we&#8217;ll only look at basics.</p>
<p><strong>Introduction</strong><br />
Firstly FreeBSD refers to both a kernel and userspace tools making it a  whole operating system (userspace tools being the basic programs like  shells and copy/move commands), this is different to Linux which is just  a kernel and distros are technically called GNU/Linux to show that it  is using the GNU userspace tools. You can install the GNU userspace  tools on FreeBSD and you can also get GNU/FreeBSD hybrids such as the <a href="http://www.debian.org/ports/freebsd/">Debian GNU/kFreeBSD</a>,  and there was work on a <a href="http://www.gentoo.org/proj/en/gentoo-alt/bsd/fbsd/">Gentoo/FreeBSD</a> but it never went anywhere, although there not really used much. Theres  also not a huge point in either since all the userspace tools are based  on the original Unix ones and try to mee POSIX standards etc %90 of the  functionality is the same. There are some differences, for instance <strong>‘ls  –color’</strong> on FreeBSD is <strong>‘ls -G’</strong>, some commands  require the flags to be in the correct order so <strong>‘cp /directory  /somewhere -rf’</strong> won’t work as <strong>-rf</strong> is at the  end instead of the start befoure the directories, when hitting the down  arrow at the end of a man page it will exit on FreeBSD.</p>
<p>The other important difference between BSD’s and Linux is the license  they use, Linux uses the GPL and BSD uses the BSD license. The GPL is  actually more restrictive but in a way that is designed to guarantee  everyones overall freedoms, by enforcing that the source remains open  when redistributed. The BSD license basically says you can do whatever  you want provided you keep the credits in, including taking the source  and closing it or relicensing it. Apple took the FreeBSD userspace tools  for OSX (and the <a href="http://en.wikipedia.org/wiki/Mach_%28kernel%29">Mach microkernel</a>)</p>
<p>The difference in the kernels means that hardware is going to behave  differently, device names in <strong>/dev/</strong> are diffrent for  starters. Ethernet cards have names that match the device model, for  instance instead of <strong>“/dev/eth0″</strong> you might have <strong>“/dev/re0″</strong>.  My harddrive is <strong>“/dev/ad10s1″</strong> instead of <strong>“/dev/sda1″</strong> There will be differences in the hardware that is supported, wifi is  apparently better on FreeBSD although there has been a lot of work in  the Linux area recently and some of the drivers have been ported across  (leading to some <a href="http://undeadly.org/cgi?action=article&amp;sid=20070829001634">controversy</a> due to the relicensing of BSD code under the GPL, although it has  apparently been <a href="http://www.linux-watch.com/news/NS2902106404.html">resolved</a>).</p>
<p>The overall system feels fairly similar to Slackware and Gentoo,  except with better package management IMHO.</p>
<p><strong>Installing</strong><br />
Installing isn’t particularly hard, if you have installed either, its  not a Ubuntu style userfriendly distro so you do need to partition and  such but it didn’t require heaps of planning and was fairly hassle free.  There is also the handy <a href="http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/">FreeBSD  handbook</a> which is actually fairly good, normally when I get told to  read opensource documentation I find a bunch of out of date or setup in  a completely different way by everyone but the guide writers. You can  get away with only getting the <a href="ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/ISO-IMAGES/7.0/7.0-RELEASE-i386-disc1.iso">1st  CD</a> since that contains the basic system and if you don’t choose  much in the way of packages you won’t need the others. If however you do  get asked to insert the <a href="ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/ISO-IMAGES/7.0/7.0-RELEASE-i386-disc2.iso">2nd</a> or <a href="ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/ISO-IMAGES/7.0/7.0-RELEASE-i386-disc3.iso">3rd</a> CDs, at that stage you can just <strong>ctrl+c</strong> and reboot into  a working distro and use <strong>‘sysinstall’</strong> to make any  additional changes. Set the root password with <strong>‘passwd’</strong> and add a user account with <strong>‘adduser’</strong>.</p>
<p>One installed you need to setup stuff, once again if your a Gentoo  user this is familiar. FreeBSD has binary packages that can be retrieved  automatically via ftp by using the <strong>‘pkg_add -r ‘</strong> command. FreeBSD however also can compile packages from source using  ports, this is similar to Gentoo’s portage which was heavily inspired by  ports. Ports seems a simpler and more stable system than portage with  which I have had problems maintaining when updating packages can break  or lock other packages.</p>
<p>Firstly I had some problems with my USB keyboard, in the end I had to  disable USB legacy in the BIOS which was blocking BSD from using it for  some reason, this has the unfortunate side effect of disabling the  keyboard in the boot loader but its not needed for now.</p>
<p><strong>Ports</strong><br />
Ports is the system that FreeBSD uses to get source code for packages,  compile and install it. It is similar to Portage as it used ports as  inspiration. Compiling from source has advantages over using binary  packages. For instance patent issues aren’t a problem because its the  source code, I am not a lawyer but source code itself cannot be patented  as its a blueprint not an actual thing, and compiling the code your  self is possibly ok since you can apparently violate patents for  personal use (not too sure on that, some talk <a href="http://www.ciphersbyritter.com/NEWS3/PATENT.HTM">here</a>, at the  very least it means that to enfoce the patent requires going after  individuals rather than FreeBSD so home users are unlikly to be at risk  (unless patent lawyers frequent your premises), it also allows for  people in countries without such laws access to the code), but as I said  I’m not a lawyer and haven’t bothered to find much in the way sources.  Secondly you get more control over what features you want in your  program and thirdly you can optimize the program for a specific  architecture.</p>
<p>Setting up ports, If you want to grab a new snapshot of ports you  will need to run <strong>‘portsnap fetch’</strong> followed by <strong>‘portsnap  extract’</strong>. If you already have <strong>“/usr/ports”</strong> perhapses because you chose it when the installer asked you can  apparently <strong>‘cd /usr/ports’</strong> followed by <strong>‘make  update’</strong>. This should use portsnap to update it. The full was  about a 50mb download when I did it with <strong>‘portsnap fetch’</strong>.</p>
<p>Ports has a similar thing to Gentoo’s portage useflags called KNOBS,  you can see a whole list of them in <strong>/usr/ports/KNOBS</strong>. I  believe you can also set CFLAGS like in Gentoo although I havn’t  bothered.</p>
<p><strong>Desktop</strong><br />
If your using it on the desktop you will need to install xorg and a  desktop environment, you can <strong>“pkg_add -r gnome2″</strong> to do  that, which should pull in all the dependencies. Alternatively to build  from source: <code>cd /usr/ports/x11/gnome2 &amp;&amp; make install  clean</code><br />
If you want to use gdm add <strong>gdm_enable=”YES”</strong> to <strong>/etc/rc.conf</strong>.  If you don’t want Gnome you can so whatever other environment you want,  Fluxbox, XFCE, KDE, etc… You will need to edit <strong>~/.xinitrc</strong> and add <strong>“gnome-session”</strong>, <strong>“fluxbox”</strong> or  whatever. You can then <strong>‘startx’</strong>. At this point there  will be a bunch of programs you need to install either from ports or  with <strong>pkg_add -r</strong> such as firefox, pidgin, vlc (although  totem should be in if you did gnome2), music players, etc…</p>
<p><strong>Shell</strong><br />
The default shell csh might not be to your liking, a lot of people are  experienced with bash which is the choice of most Linux distros although  personally I prefer zsh which has better tab completion and fancy  prompts included. <strong>‘pkg_add -r zsh’</strong> or <strong>‘pkg_add  -r bash’</strong> or install them from ports. Then run <strong>‘chsh’</strong> which will give you a vi window, change the <strong>“Shell:  /usr/bin/csh”</strong> to <strong>“Shell: /usr/local/bin/zsh”</strong>.  Bash users will need to change it to bash obviously. If vi is a problem  for you, you can probally edit <strong>‘/etc/passwd’</strong> with  whatever you want. You will need to setup your shell rc, <strong>~/.zshrc</strong> for zsh (although it has a config window on first time login).</p>
<p><strong>Sound</strong><br />
You will probably need to enable sound, Theres a handy howto <a href="http://geekybits.blogspot.com/2006/11/setting-up-sound-card-in-freebsd.html">here</a>,  but basically <strong>‘kldload snd_driver’</strong>, <strong>‘cat  /dev/sndstat’</strong>, look for the driver name right after <strong>“kld”</strong>,  add <strong>DRIVERNAME_load=”YES”</strong> to <strong>/boot/loader.conf</strong></p>
<p><strong>Mount Linux ext3 Drive</strong><br />
Mounting your Linux drive, add the following entry to <strong>/etc/fstab</strong>:  <code>/dev/ad10s2             /mnt/ubuntu     ext2fs  rw              0        0</code> with whatever changes you need for your device name or  mount point. You will need to make the mount point to with <strong>‘mkdir  /mnt/ubuntu’</strong>. If you get the error <strong>“mount: /dev/ad10s2  : Invalid argument”</strong> this could be because your drive is  unclean, you will need to install the <strong>/usr/ports/sysutils/e2fsprogs/</strong> and run <strong>‘fsck.ext3 /dev/ad10s2′</strong> (Assuming your using  ext3).</p>
<p><strong>NTFS read/write with NTFS-3g</strong><br />
Mounting a NTFS drive with read/write is a bit more work, you need to  install <strong>‘/usr/ports/sysutils/fusefs-ntfs’</strong>, however it  needs the userspace source. You can get this with <strong>‘sysinstall’</strong> choosing <strong>“Configure&gt;Distributions&gt;src&gt;All”</strong>,  this will grab all the source its probably not all required but I  couldn’t find the specific package and its handy to have the rest around  anyway. Once you have that you can <strong>‘ntfs-3g /dev/ad10s1  /mnt/windows’</strong>. Adding an fstab entry is again a bit  problematic. Under 7.0 the mount command has had hardlinks to what it  can call, <strong>mount_ntfs-3g</strong> isn’t in that list so you  either need to patch mount or rename the current <strong>mount_ntfs</strong>,  and link <strong>ntfs-3g</strong> in its place. Without that this was  throwing an <strong>“mount: /dev/ad10s1 : Operation not supported by  device”</strong> error.</p>
<p><strong>nVidia drivers</strong><br />
The drivers work quite well on FreeBSD 7.0 but only on i386 (64bit is  out of luck thanks to closed source nvidia drivers, 64bits OS is mostly  useless anyway), the official ones refuse to install for me but the ones  in ports work. Firstly you need the kernel source code installed, if  you followed the NTFS-3g instructions above you will have the source  already. If not you can specify just the base and sys source in  sysinstall which is all thats needed for the drivers. Then <code>cd  /usr/ports/x11/nvidia-driver &amp;&amp; make install clean</code> Add  this to <strong>/boot/loader.conf</strong> <code>nvidia_load="YES"</code> Now you need an <strong>xorg.conf</strong> file, by default BSD just  relies on the autoconfig magic in the latest xorg releases so we need to  force it to generate one with: <code>Xorg -configure</code> then copy  the newly created <strong>xorg.conf.new</strong> to <strong>/etc/X11/xorg.conf</strong>.  If you don’t want to edit the xorg by hand <code>cd  /usr/ports/x11/nvidia-xconfig &amp;&amp; make install clean</code> then <code>nvidia-xconfig</code> will change the <strong>“nv”</strong> driver to <strong>“nvidia”</strong>.</p>
<p><strong>Compiz Fusion</strong><br />
Firstly if your on nVidia hardware you need to enable some xorg.conf  settings, you can do them with nvidia-xconfig if you installed it above <code>nvidia-xconfig  --composite &amp;&amp; nvidia-xconfig --render-accel &amp;&amp;  nvidia-xconfig --add-argb-glx-visuals -d 24</code></p>
<p>Now install Compiz with: <code>cd /usr/ports/x11-wm/compiz-fusion  &amp;&amp; make install clean</code> When you want to start it use <code>LIBGL_ALWAYS_INDIRECT=1  compiz --sm-disable --replace ccp</code> and for the Emerald  decorations<code>emerald --replace</code>. I had some problems with  emerald not starting right away and drawing no decorations but after a  while it loaded fine without me noticing and now starts fine. You will  probably want to make Compiz start automatically, there are various  guides on Compiz around that give you different ways. The config  programs should be in System&gt;Preferences for Compiz and Emerald,  otherwise use <strong>‘ccsm’</strong> and <strong>‘emerald-theme-manager’</strong>.</p>
<p><strong>updatedb</strong><br />
For those of you who use <strong>‘locate’</strong> command to find  files, you will probably want to update the database, you might have  already noticed that <strong>‘updatedb’</strong> isn’t a command. The  actual command is <strong>‘/usr/libexec/locate.updatedb’</strong> but  this will give you a warning as running it as root will expose hidden  files that only root or specific users should see to the database (not  such a problem for single users), you can however run the cron job  manually for a safer database with: <code>/etc/periodic/weekly/310.locate</code></p>
<p><strong>Flash</strong><br />
One of the main problems with Desktop FreeBSD is that Flash has no  native port for FreeBSD thanks to Adobe (Even though there is a Solaris  port thanks to <a href="http://www.adobe.com/macromedia/proom/pr/2004/flash_solaris.html">Sun  working with Adobe</a>, you can have your say on the <a href="http://weblogs.macromedia.com/emmy/">flash development blog</a>,  also an <a href="http://www.petitiononline.com/flash4me/petition.html">online  petition</a> exists for what little use they are with over 5000  signatures(and spam bots)). There has been some talk on the mailing  lists of a <a href="http://lists.freebsd.org/pipermail/freebsd-www/2007-December/005478.html">native  FreeBSD port</a>.</p>
<p>The only real solutions seems to be to use Flash 7 or possibly  Windows Firefox under Wine ☹ (PC-BSD Apparently ship a Windows Firefox  PBI)</p>
<p>If you want to try the Linux version of Flash 9 which freezes for me  (and everyone apparently),  you need to install the Linux plugin <strong>/usr/ports/www/linux-flashplugin9</strong> and the wrapper <strong>/usr/ports/www/nspluginwrapper/</strong>. Then  run <code>nspluginwrapper -v -a -i</code> to register the plugin. Check  in <strong>‘about:plugins’</strong>. Alternatively you can try Flash7  with a wrapper, there is also another wraooer <strong>/usr/ports/www/linuxpluginwrapper</strong> but it wouldn’t install for me.</p>
<p><strong>ETQW [unsolved]</strong><br />
I though I would give ETQW a go under FreeBSD, unfortunately I ran into  problems with the Linux compatibility layer, I’ll post the steps here in  case its of use to someone trying to do something similar or can offer a  solution.</p>
<p>Firstly the installer didn’t give the default path but entering one  works however it then failed to make the path so you need to make it  manually, then it failed to read from the cdrom (although I didn’t  really look into it since you can just copy the files across manually,  possibly manually mounting it manually would have solved it). Copy the  correct files into the base directory.</p>
<p>When running the etqw binary I get an error about missing sdl  libraries <strong>“./etqw.x86: error while loading shared libraries:  libSDL-1.2.so.0: cannot open shared object file: No such file or  directory”</strong>, You can fix this with <code>cd  /usr/ports/devel/linux-sdl12/ &amp;&amp; make install clean</code></p>
<p>You will need to brand you etqw binaries as Linux ones, change to  your etqw directory and run <code>brandelf -t linux *</code> also <code>brandelf  -t linux pb/*</code> might be needed.</p>
<p>Then run: <code>portmaster emulators/linux_base-fc4</code> to get all  the Linux compatibility stuff installed (if using portmaster you need <strong>/usr/ports/ports-mgmt/portmaster/</strong> installed)</p>
<p>Then there I got <strong>“./etqw.x86: error while loading shared  libraries: /usr/local/lib/libX11.so.6: ELF file OS ABI invalid”</strong>,  make sure that <strong>/usr/ports/x11/linux-xorg-libs/</strong> is  installed.</p>
<p>For the error <strong>“./etqw.x86: error while loading shared  libraries: libjpeg.so.62: ELF file OS ABI invalid”</strong>, make sure  you have <strong>“/usr/ports/graphics/linux-jpeg”</strong>. You can also  try <strong>‘sysctl kern.fallback_elf_brand=3′</strong>. But those  didn’t work for me, I notice that etqw ships its own libjpeg, I tried  replacing that with the <strong>/usr/compat/linux/usr/lib/libjpeg.so.62</strong> one it seems to work but now I get another error <strong>“./etqw.x86:  error while loading shared libraries: /usr/local/lib/libX11.so.6: ELF  file OS ABI invalid”</strong>, possible its loading the ones in <strong>/usr/local/lib</strong> instead of <strong>/usr/compat/linux/usr/X11R6/lib/libX11.so.6</strong> but I don’t know and thats about all I can think of now.</p>
<p>Also make sure you have <strong>linux_enable=”YES”</strong> in <strong>/etc/rc.conf</strong> and run <strong>‘/etc/rc.d/abi restart’</strong> to enable the  compatibility layer stuff if you didn’t already have it done.</p>
<p><strong>Wine</strong><br />
Wine works fine under FreeBSD, to install: <code>cd  /usr/ports/emulators/wine &amp;&amp; make install clean</code></p>
<p><strong>Mounting samba shares</strong><br />
Firstly you need to install Samba with <code>/usr/ports/net/samba3/  &amp;&amp; make install clean</code> then you can manually mount it with  the following: <code>mount_smbfs //username@host/share /mnt/mointpoint</code></p>
<p>the -o options that you use in Linux such as username, password,  credentials don’t work on FreeBSD’s version. If you want to make it  automated, edit <strong>/etc/fstab</strong> and add: <code>//username@server/share   /mnt/mountpoint smbfs    rw              0       0</code> Then edit <strong>/etc/nsmb.conf</strong> and check the example near the end. Alternatively you can put them in <strong>~/.nsmb.conf</strong> for a per user solution.</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">http://www.smsthru.com</div>
]]></content:encoded>
			<wfw:commentRss>http://www.andrewkmitchell.com/blog/?feed=rss2&amp;p=309</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting to know PGP/GPG</title>
		<link>http://www.andrewkmitchell.com/blog/?p=304</link>
		<comments>http://www.andrewkmitchell.com/blog/?p=304#comments</comments>
		<pubDate>Sun, 30 May 2010 22:57:09 +0000</pubDate>
		<dc:creator>Andrew Mitchell</dc:creator>
				<category><![CDATA[InfoTech]]></category>

		<guid isPermaLink="false">http://www.andrewkmitchell.com/blog/?p=304</guid>
		<description><![CDATA[If you need or want PGP (Pretty Good Privacy) try the commercial PGP software at : http://www.pgp.com/ or the (free) GNU implementation of the OpenPGP standard at http://www.gnupg.org/. Strong encryption software such as PGP Was (possibly still is) regarded as a sensitive &#8220;munition&#8221; or some such rubbish, and export is not allowed &#8211; as if [...]]]></description>
			<content:encoded><![CDATA[<p>If you need or want PGP (Pretty Good Privacy) try the commercial PGP  software at  : <a href="http://www.pgp.com/">http://www.pgp.com/</a> or  the (free) GNU implementation of the OpenPGP standard at <a href="http://www.gnupg.org/">http://www.gnupg.org/</a>.</p>
<p>Strong encryption software such as PGP Was (possibly still is) regarded  as a sensitive &#8220;munition&#8221; or some such rubbish, and export is not  allowed &#8211; as if publicly available software could possibly be restricted  to national borders. I am told that copies have managed to creep out  and if you are outside Canada or the US and want the software you can  get it.</p>
<p>It seems that the following international site (though it is old)  ignored the wishes and laws of the US government on this matter: <a href="http://www.it.pgpi.com/">The International PGP Home Page.</a> The  search engines can no doubt find hundreds more of these scofflaw sites.  Imagine that!</p>
<p>I believe attempts to restrict encryption software and undermine  its widespread use represent the fear by &#8220;Big Brother&#8221; that the era of  universal easy snooping is coming to an end. I assert that no  organization has the right to read all my mail, listen to all my phone  messages and generally track every aspect of my life at will. This  assertion is meaningless unless I can enforce the remaining shreds of my  privacy using convenient <a href="http://scidiv.bellevuecollege.edu/ls/pgpkey/#Unbreakable">unbreakable </a>encryption.</p>
<p>If EVERYONE encrypted EVERYTHING they sent as a matter of course,  the snoops would be OUT OF BUSINESS and, in my view, the world might be  a <em>slightly</em> better place &#8230;</p>
<p>I guess it depends on who you are more afraid of: bad guys in the  government or bad guys out of the government. Granting sweeping  authority to the government might be useful temporarily (&#8220;our&#8221; bad guys)  but it is virtually impossible to get them to relinquish their powers  once granted. That is, often, how governments become blatant  dictatorships: not at the point of a gun but accepted willingly, a step  at a time, by a fearful populace and impatient leaders.</p>
<p>Sermon and paranoia aside, this stuff is just WAY COOL. MUCH  better than secret decoder rings.</p>
<p>It works as follows:</p>
<p>Encryption keys are created in pairs. <strong>Either  key can</strong> be used to <a href="http://scidiv.bellevuecollege.edu/ls/pgpkey/#Unbreakable">unbreakably</a> <strong>encrypt</strong> a message. However <strong>only the OTHER key in the pair can  decrypt</strong> that message. Typically <strong>one key of the pair is made  public</strong> (such as the one below) and <strong>the other kept very secure and  private</strong>.</p>
<p>If you want to send me a secure message, you use my public key to  encrypt it and then only my private key can unscramble it.</p>
<p>If you want to digitally &#8220;sign&#8221; the message, so I know you actually sent  it and the message was not altered in transit, you encrypt information  about the message  using your private key. This is done automatically by  your copy of the PGP software.</p>
<p>When I receive the message I decrypt the message using my private key. I  know that no one else could have read this encrypted message.</p>
<p>If I wish to verify authenticity (i.e. that <strong>you</strong> sent it and not  someone pretending to be you) I then decrypt the signature using your  public key and compare the information there to the corresponding info  from the message I received. Once again, this is done automatically by  the PGP software. Only you could have signed it using your private key.  The signature contains info about the message to verify that you were  looking at the same message I received when you encrypted it to my  public key.</p>
<p><strong><a href="http://scidiv.bellevuecollege.edu/ls/pgpkey/#Unbreakable">Unbreakable</a> encryption. Unforgeable signature. Verification that the encrypted  message arrived unaltered and came from you. </strong>All of these details  are handled by the software, and most popular mail clients also support  PGP plugins to automate the encryption/decryption process.</p>
<p><span style="color: green; font-size: x-small;"> <img src='http://www.andrewkmitchell.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </span></p>
<hr />
<pre>
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.9 (GNU/Linux)

mQENBEq+yYcBCAC1jiVnvDip7OWnynUp5ATWo9u/h3ji7xGm6uw1SfIGjnJsqd2J
TQi0D4dooCUbDqSXbUvoRqG6+WgcbVBe5i40eBDQGmrrcdXVt/SokZ6fdLOdaXQB
aNgT/+F5DSlSAN//OUlDCBzSFVgubI2UEj6CB8wxzbGSHvRqeBQr1sEJyL8J8/oA
lN4uJ/aSqpNCYPqoGqB04Q4txEJzIW617Du+RBx5KhxD2H4Jjt2ELaMQEiQsZU0+
UwEeAers/fkEMch/72pD+oaJTcQxuPsPYPVTWWaL1t2DxF142GSIC4c4BF31ZtU3
gmu73SwVWfM2hWctGI/1Xk0PS7Ceg0lKbk8jABEBAAG0NEFuZHJldyBLZW50b24g
TWl0Y2hlbGwgPGFuZHJldy5taXRjaGVsbEB3ZGlkYXRhLm5ldD6JATwEEwECACYF
Akq+yYcCGyMFCQlmAYAGCwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRBmyb6MWLa5
T25OB/9e3L6KDBgHc6cjmQMX2qIyJJmM+NtwCmxuKeVWHCGXZwi/ZztMM0z1IARF
1fjV8RJX4QwEtbCovw5ejGZw/NssLHDs8M3s39pEF0KQTD+8MuZlO0PX5AfcWwGE
R8K31BIjBZLWMZK17vsTxZrFuqv0VfJc2s8tds51U6F5k1uVkXPf2R2AaL5q24hm
lK1PrkxljBOovh4Dsi0s1JsQxIbdydEn402mC6IYFc2Bynbr/sFuW8d409LRTKan
1VNR6BwO+yzGOZqL49OhF06Cu6nvo0BJhz/4wA0UR7223DA9UsrW143hKS7fheZ6
PBFGSktXPzG5+ymcsaowBFGLxnaYuQENBEq+yYcBCAD8mCwktNjKjW6B3tG2ylu/
EdT+dLfGpzw4p6hjXoAMfTVqn7pVdJChxnMXOmRiiih6CAlgVt6aCaISMcgENy1A
7oz4diRCNiWIksdgM8HUt/fxgIPotObQPyCPMd1Bd0aFh5g13QotP3K0Ax0BJTPE
W1ECnZxE+dA0kmzpGUikKIQmOEX0SlED+IqTfat4WtKrllJ+SgK/M2jmh7CeTjbq
NghxnIYphccfBzDmzU6VqtMZRULJgOv0KGW4hXBeO0ve1nhMIMOMUrdjQ4prZNdN
JNtMaUuo/4Dl9wF+HAMDInQKPukahu/9XjVrFPc+xWuGq/nU8PlTONqMbHokMjW/
ABEBAAGJASUEGAECAA8FAkq+yYcCGwwFCQlmAYAACgkQZsm+jFi2uU8r1wgAqK4W
B9XcVHp/yD4COebYtUeYNlav1RVIPquCmmYChWDy0gGQ9WF5mfODAdxPFVyn78Ow
cGBd0hvrHGnpM2SpFbsmSGx81T0A4bDRrkht45bawWjoKTLs0vdCPuqmGa1cxDqG
r6bXywS0zkAHqMA43YaNb2iqyp2q/DTS6geIvZB6fSJC4eAX+jSV+s9e+lLnQwOZ
WkHlqhsawp4bR9sWoBaEJEvW0iEmuilRradAt0PKNm1f6KrwVperDj7oQLuPmR6N
nUJWXf5mTatcMtCK9Od6wF06iEhHHV9lf0Erqpxsts7sBzd7+w0yUmAzxv6RnrKT
3PAaKzoAlYyh4Tsrvw==
=XNjJ
-----END PGP PUBLIC KEY BLOCK-----
</pre>
<hr /><span style="color: green; font-size: x-small;">Some Words About The Term <a name="Unbreakable">&#8220;Unbreakable&#8221;</a></span></p>
<p>&#8220;Unbreakable&#8221; is a pretty strong claim. Currently only the smallest key  pairs (perhaps a couple of hundred bits) can be cracked by brute  computational force in the civilian world &#8211; and that only with the  expenditure of costly amounts of computer time. Obviously this is not a  static record and also there are supercomputer resources and techniques  classified by governments that are unavailable to civilians. However  mathematicians think that the difficulty in cracking a key pair is not  linear in the key size, but exponential in the number of bits in the  key. A 3072 bit key would not be 48 times harder to crack than a 64 bit  key &#8211; it might be <strong>1000000000000000 times</strong> harder.</p>
<p>Most  Diffie-Helman keys are 3072 or 4096 bits and RSA keys should be 2048  bits (for backward compatibility with older software.)</p>
<p>Phil Zimmerman, the author of the original PGP software, has this to say  :</p>
<blockquote><p>An expensive and formidable cryptanalytic attack could  possibly be mounted by someone with vast supercomputer resources, such  as a government intelligence agency. They might crack your public key by  using some new secret mathematical breakthrough. But civilian academia  has been intensively attacking public key cryptography without success  since 1978. Perhaps the government has some classified methods of  cracking the conventional encryption algorithms used in PGP. This is  every cryptographers worst nightmare. There can be no absolute security  guarantees in practical cryptographic implementations.Still, some  optimism seems justified. The public key algorithms, message digest  algorithms, and block ciphers used in PGP were designed by some of the  best cryptographers in the world. PGPs algorithms have had extensive  security analysis and peer review from some of the best cryptanalysts in  the unclassified world.</p>
<p>Besides, even if the block ciphers used  in PGP have some subtle unknown weaknesses, PGP compresses the plaintext  before encryption, which should greatly reduce those weaknesses. The  computational workload to crack it is likely to be much more expensive  than the value of the message.</p></blockquote>
<p>Zimmerman&#8217;s conservative and careful statement aside, and with all due  respect to the computing community, no one involved in this business  seems to believe that anyone can now or will in the near future crack  big key encryption by brute force. No one seems to think the encryption  techniques themselves carry subtle weaknesses that would make them  vulnerable to a &#8220;smart&#8221; attack.</p>
<p>There are, however, conventional &#8220;spy&#8221; techniques that could work rather  easily and cheaply if your messages are a valuable target. These all  involve physical invasion, break-ins or other monitoring of the computer  on which the encryption occurs. Each item can be dealt with and  security breaches detected and corrected. Each time you solve a  potential problem you make the software more cumbersome to use but more  secure : &#8220;Bug&#8221; searches, no windows in the room, locks on all doors,  security guards, electromagnetic screening, software checksum  monitoring, keeping keys on different media, no direct internet  connection etc. etc. etc.</p>
<p>You must decide how valuable your data  is and to what lengths you are willing to go to obtain incremental  increases in security. Examples of things that can go wrong:</p>
<ul>
<li><strong>Your Private Key Could Be Compromised</strong><br />
When you access your private key you must enter a passphrase to verify  that you are &#8220;you&#8221; and not someone who has stolen or copied the key file  from your computer. If this passphrase is known and someone has copied  your keyfile they can decrypt messages meant for your eyes only and sign  messages as &#8220;you.&#8221; This passphrase can be found by :</p>
<ul>
<li> Guessing : don&#8217;t use a short or obvious pass phrase.</li>
<li>Finding a &#8220;post-it note&#8221; with the phrase on it</li>
<li>Someone  can look over your shoulder (also through a camera or a telescope  through a window.)</li>
<li>They can watch your fingers type.</li>
<li>Someone with physical access to your computer can run  software on your computer (invisibly in the background and available for  free on the internet) that records ALL keystrokes, thereby compromising  your pass phrase.</li>
<li>Your keyboard when you type and your monitor when it  creates an image emit electronic signals that someone in a van in your  parking lot can pick up with a dish antenna pointed at your office. Yes  they CAN do this.</li>
<li>Someone can replace the copy of the PGP software by  something else that looks and acts the same to you but is NOT and saves  information (such as the passphrase the next time you use it) or sends  information to someone else for retrieval.</li>
</ul>
</li>
</ul>
<ul>
<li><strong>Your Decrypted Messages Can Be Found</strong>
<ul>
<li>After you decrypt a message you might want to save it. If you  save it unencrypted, someone with physical access to the machine can  copy it. PGP allows you to encrypt the contents of a directory on your  hard drive to manage this risk.</li>
<li>Even after you delete a message, it still is on the hard  drive. The only thing deleted is the pointer to the file that shows up  when you do a directory listing. Until it is physically overwritten it  is easy to resurrect. A feature of PGP allows you to take care of this.</li>
<li>If you use a virtual memory scheme, things in RAM are often  swapped out temporarily to the hard drive. You would not know about  this copy, and it would be there for an expert to find until  overwritten. Don&#8217;t use virtual memory if this is a worry.</li>
</ul>
</li>
</ul>
<p>As a final note, I reproduce a comment contributed to a forum on the  topic by an individual who calls himself &#8220;Anonymous Coward.&#8221; It is  interesting to see the things people who spend a lot of time thinking  about security have to say.</p>
<blockquote>
<blockquote><p>It&#8217;s physically impossible to securely remove plaintext data on a  magnetic medium without destroying the media. The data is always there,  no matter how many times you write over it because there is more than  one atom involved on the part of the medium where a given bit is  written, and you never change the magnetic  properties of all the atoms involved in storing that given bit.A well funded attacker will be able to recover every bit of data ever  written to the medium, and establish chronological order of writes per  bit on the disk. The number of times you overwrite or otherwise wipe the  files is irrelevant</p>
<p>Also, random data is a less secure means of obscuring existing data  than random actual day to day files because the entropy of the data  you&#8217;re trying to hide, and the entropy of the noise you&#8217;re using to  cover it up with differ so greatly. With a chronological record of the  bits written in a volume (given, only available to the  most sophisticated and well funded attacker), and a search for entropy,  it is a quite simple task to recover any data that was ever represented  on the media.</p>
<p>These tools only obscure the data from poorly funded, inept attackers.  If those are who concern you given your threat model, then OK. Else the  NSA is having a great belly laugh at your expense.</p>
<p>If you would like true file security, you need a cryptographic  filesystem on a fresh drive. Ensure that the keys to decrypt said  partition and your passphrase are stored on a separate media (cd-r  perhaps) which you can shred using a cross-cut shredder (one that makes  fingernail clipping sized chunks) before being burned.</p>
<p>Keep this disk on you at all times, or be able to account for it at all  times. Your reaction time for removing your key media, turning off your  machine, shredding and then burning said media must be less than the  time between your detecting an attacker on the premises and their  reaching the machine.</p>
<p>Your browser cache, saved emails and chat logs can be used as cribs to  crack the encryption on your partition. never write them to disk, even  in encrypted form.</p>
<p>In general, never write plaintext to your hard drive, and keep the keys  off of your hard drive if keeping people from reading data that was  ever at any time written to it is important to you.</p></blockquote>
</blockquote>
<p><span style="color: green; font-size: x-small;">Happy Encrypting! <img src='http://www.andrewkmitchell.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.andrewkmitchell.com/blog/?feed=rss2&amp;p=304</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Talking About DNSSEC Overhead</title>
		<link>http://www.andrewkmitchell.com/blog/?p=296</link>
		<comments>http://www.andrewkmitchell.com/blog/?p=296#comments</comments>
		<pubDate>Fri, 28 May 2010 16:32:08 +0000</pubDate>
		<dc:creator>Andrew Mitchell</dc:creator>
				<category><![CDATA[InfoTech]]></category>

		<guid isPermaLink="false">http://www.andrewkmitchell.com/blog/?p=296</guid>
		<description><![CDATA[NOTE: Original research conducted in November 2002.  Findings updateded in May 2010 with (slightly) new data collected eariler in the year. Even though the key ideas behind DNSSEC have been intro-duced quite some time ago DNSSEC has not yet seen large scale deployment. This is in large part due to the anticipated overhead of DNSSEC. While [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #ff0000;"><strong>NOTE:</strong> Original research conducted in November 2002.  Findings updateded in May 2010 with (slightly) new data collected eariler in the year.</span></p>
<p>Even though the key ideas behind DNSSEC have been intro-duced quite some time ago DNSSEC has not yet seen large scale deployment. This is in large part due to the anticipated overhead of DNSSEC. While the overheads have been reduced by the introduction of the delegation signer model, it is still not clear if they are bearable.  Therefore we in <a href="http://www.andrewkmitchell.com/blog/wp-content/uploads/2010/05/dnssec-overhead.pdf" target="_blank">this post</a> we examine the actual overheads of DNSSEC. We first examine how the packet sizes of an DNS trace increases if DNSSEC would be used. Then we explore the CPU and memory overheads imposed by DNSSEC by re-playing a DNS client trace in a testbed initialized with roughly 100,000 zones.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andrewkmitchell.com/blog/?feed=rss2&amp;p=296</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenSSL Command-Line HOWTO</title>
		<link>http://www.andrewkmitchell.com/blog/?p=293</link>
		<comments>http://www.andrewkmitchell.com/blog/?p=293#comments</comments>
		<pubDate>Tue, 25 May 2010 21:29:02 +0000</pubDate>
		<dc:creator>Andrew Mitchell</dc:creator>
				<category><![CDATA[InfoTech]]></category>

		<guid isPermaLink="false">http://www.andrewkmitchell.com/blog/?p=293</guid>
		<description><![CDATA[The openssl application that ships with the OpenSSL libraries can perform a wide range of crypto operations. This HOWTO provides some cookbook-style recipes for using it. Table of Contents Introduction How do I find out what OpenSSL version I’m running? How do I get a list of the available commands? How do I get a [...]]]></description>
			<content:encoded><![CDATA[<p>The <strong>openssl</strong> application that ships with the OpenSSL libraries can perform a wide range of crypto operations. This HOWTO provides some cookbook-style recipes for using it.</p>
<hr size="2" /><strong>Table of Contents</strong></p>
<p><a href="#intro">Introduction</a></p>
<p><a href="#intro-version">How do I find out what OpenSSL version I’m running?</a></p>
<p><a href="#intro-commands">How do I get a list of the available commands?</a></p>
<p><a href="#intro-ciphers">How do I get a list of available ciphers?</a></p>
<p><a href="#benchmark">Benchmarking</a></p>
<p><a href="#benchmark-speed">How do I benchmark my system’s performance?</a></p>
<p><a href="#benchmark-connection">How do I benchmark remote connections?</a></p>
<p><a href="#cert">Certificates</a></p>
<p><a href="#cert-self">How do I generate a self-signed certificate?</a></p>
<p><a href="#cert-request">How do I generate a certificate request for VeriSign?</a></p>
<p><a href="#cert-test">How do I test a new certificate?</a></p>
<p><a href="#cert-retrieve">How do I retrieve a remote certificate?</a></p>
<p><a href="#cert-exam">How do I extract information from a certificate?</a></p>
<p><a href="#cert-pkcs12">How do I export or import a PKCS#12 certificate?</a></p>
<p><a href="#verify">Certificate Verification</a></p>
<p><a href="#verify-standard">How do I verify a certificate?</a></p>
<p><a href="#verify-system">What certificate authorities does OpenSSL recognize?</a></p>
<p><a href="#verify-new">How do I get OpenSSL to recognize/verify a certificate?</a></p>
<p><a href="#cs">Command-line clients and servers</a></p>
<p><a href="#cs-smtp">How do I connect to a secure SMTP server?</a></p>
<p><a href="#cs-www">How do I connect to a secure [whatever] server?</a></p>
<p><a href="#cs-server">How do I set up an SSL server from the command line?</a></p>
<p><a href="#digest">Digests</a></p>
<p><a href="#digest-file">How do I create an MD5 or SHA1 digest of a file?</a></p>
<p><a href="#digest-sign">How do I sign a digest?</a></p>
<p><a href="#digest-verify">How do I verify a signed digest?</a></p>
<p><a href="#digest-apache">How do I create an Apache digest password entry?</a></p>
<p><a href="#digest-commands">What other kinds of digests are available?</a></p>
<p><a href="#encrypt">Encryption/Decryption</a></p>
<p><a href="#encrypt-base64">How do I base64-encode something?</a></p>
<p><a href="#encrypt-simple">How do I simply encrypt a file?</a></p>
<p><a href="#error">Errors</a></p>
<p><a href="#error-interp">How do I interpret SSL error messages?</a></p>
<p><a href="#key">Keys</a></p>
<p><a href="#key-rsa">How do I generate an RSA key?</a></p>
<p><a href="#key-rsapublic">How do I generate a public RSA key?</a></p>
<p><a href="#key-dsa">How do I generate a DSA key?</a></p>
<p><a href="#key-ec">How do I create an elliptic curve key?</a></p>
<p><a href="#key-removepass">How do I remove a passphrase from a key?</a></p>
<p><a href="#passwd">Password hashes</a></p>
<p><a href="#passwd-crypt">How do I generate a crypt-style password hash?</a></p>
<p><a href="#passwd-md5">How do I generate a shadow-style password hash?</a></p>
<p><a href="#prime">Prime numbers</a></p>
<p><a href="#prime-test">How do I test whether a number is prime?</a></p>
<p><a href="#prime-generate">How do I generate a set of prime numbers?</a></p>
<p><a href="#random">Random data</a></p>
<p><a href="#random-generate">How do I generate random data?</a></p>
<p><a href="#smime">S/MIME</a></p>
<p><a href="#smime-verify">How do I verify a signed S/MIME message?</a></p>
<p><a href="#smime-encrypt">How do I encrypt a S/MIME message?</a></p>
<p><a href="#smime-sign">How do I sign a S/MIME message?</a></p>
<p><a href="#links">For further reading</a></p>
<p><a href="#comments">Comments welcome</a></p>
<p><strong>Introduction</strong></p>
<p>The <strong>openssl</strong> command-line binary that ships with the <a href="http://www.openssl.org/">OpenSSL</a> libraries can perform a wide range of cryptographic operations. It can come in handy in scripts or for accomplishing one-time command-line tasks.</p>
<p>Documentation for using the <strong>openssl</strong> application is somewhat scattered, however, so this article aims to provide some practical examples of its use. I assume that you’ve already got a functional OpenSSL installation and that the <strong>openssl</strong> binary is in your shell’s PATH.</p>
<p>Just to be clear, this article is strictly practical; it does not concern cryptographic theory and concepts. If you don’t know what an MD5 sum is, this article won’t enlighten you one bit—but if all you need to know is how to use <strong>openssl</strong> to generate a file sum, <a title="How do I  create an MD5 or SHA1 digest of a file?" href="#digest-file">you’re in luck</a>.</p>
<p>The nature of this article is that I’ll be adding new examples incrementally. Check back at a later date if I haven’t gotten to the information you need.</p>
<p><strong>How do I find out what OpenSSL version I’m running?</strong></p>
<p>Use the version option.</p>
<p>$ <strong>openssl version</strong></p>
<p>OpenSSL 0.9.8b 04 May 2006</p>
<p>You can get much more information with the version -a option.</p>
<p>$ <strong>openssl version -a</strong></p>
<p>OpenSSL 0.9.8b 04 May 2006</p>
<p>built on: Fri Sep 29 18:45:58 UTC 2006</p>
<p>platform: debian-i386-i686/cmov</p>
<p>options:  bn(64,32) md2(int) rc4(idx,int) des(ptr,risc1,16,long) blowfish(idx)</p>
<p>compiler: gcc -fPIC -DOPENSSL_PIC -DZLIB -DOPENSSL_THREADS -D_REENTRANT</p>
<p>-DDSO_DLFCN -DHAVE_DLFCN_H -DL_ENDIAN -DTERMIO -O3 -march=i686</p>
<p>-Wa,&#8211;noexecstack -g -Wall -DOPENSSL_BN_ASM_PART_WORDS -DOPENSSL_IA32_SSE2</p>
<p>-DSHA1_ASM -DMD5_ASM -DRMD160_ASM -DAES_ASM</p>
<p>OPENSSLDIR: &#8220;/usr/lib/ssl&#8221;</p>
<p><strong>How do I get a list of the available commands?</strong></p>
<p>There are three built-in options for getting lists of available commands, but none of them provide what I consider useful output. The best thing to do is provide an invalid command (<strong>help</strong> or <strong>-h</strong> will do nicely) to get a readable answer.</p>
<p>$ <strong>openssl help</strong></p>
<p>openssl:Error: &#8216;help&#8217; is an invalid command.</p>
<p>Standard commands</p>
<p>asn1parse      ca             ciphers        crl            crl2pkcs7</p>
<p>dgst           dh             dhparam        dsa            dsaparam</p>
<p>ec             ecparam        enc            engine         errstr</p>
<p>gendh          gendsa         genrsa         nseq           ocsp</p>
<p>passwd         pkcs12         pkcs7          pkcs8          prime</p>
<p>rand           req            rsa            rsautl         s_client</p>
<p>s_server       s_time         sess_id        smime          speed</p>
<p>spkac          verify         version        x509</p>
<p>Message Digest commands (see the `dgst&#8217; command for more details)</p>
<p>md2            md4            md5            rmd160         sha</p>
<p>sha1</p>
<p>Cipher commands (see the `enc&#8217; command for more details)</p>
<p>aes-128-cbc    aes-128-ecb    aes-192-cbc    aes-192-ecb    aes-256-cbc</p>
<p>aes-256-ecb    base64         bf             bf-cbc         bf-cfb</p>
<p>bf-ecb         bf-ofb         cast           cast-cbc       cast5-cbc</p>
<p>cast5-cfb      cast5-ecb      cast5-ofb      des            des-cbc</p>
<p>des-cfb        des-ecb        des-ede        des-ede-cbc    des-ede-cfb</p>
<p>des-ede-ofb    des-ede3       des-ede3-cbc   des-ede3-cfb   des-ede3-ofb</p>
<p>des-ofb        des3           desx           rc2            rc2-40-cbc</p>
<p>rc2-64-cbc     rc2-cbc        rc2-cfb        rc2-ecb        rc2-ofb</p>
<p>rc4            rc4-40</p>
<p>What the shell calls “Standard commands” are the main top-level options.</p>
<p>You can use the same trick with any of the subcommands.</p>
<p>$ <strong>openssl dgst -h</strong></p>
<p>unknown option &#8216;-h&#8217;</p>
<p>options are</p>
<p>-c              to output the digest with separating colons</p>
<p>-d              to output debug info</p>
<p>-hex            output as hex dump</p>
<p>-binary         output in binary form</p>
<p>-sign   file    sign digest using private key in file</p>
<p>-verify file    verify a signature using public key in file</p>
<p>-prverify file  verify a signature using private key in file</p>
<p>-keyform arg    key file format (PEM or ENGINE)</p>
<p>-signature file signature to verify</p>
<p>-binary         output in binary form</p>
<p>-engine e       use engine e, possibly a hardware device.</p>
<p>-md5 to use the md5 message digest algorithm (default)</p>
<p>-md4 to use the md4 message digest algorithm</p>
<p>-md2 to use the md2 message digest algorithm</p>
<p>-sha1 to use the sha1 message digest algorithm</p>
<p>-sha to use the sha message digest algorithm</p>
<p>-sha256 to use the sha256 message digest algorithm</p>
<p>-sha512 to use the sha512 message digest algorithm</p>
<p>-mdc2 to use the mdc2 message digest algorithm</p>
<p>-ripemd160 to use the ripemd160 message digest algorithm</p>
<p>In more boring fashion, you can consult the <a title="For further  reading" href="#links">OpenSSL man pages</a>.</p>
<p><strong>How do I get a list of available ciphers?</strong></p>
<p>Use the ciphers option. The <a href="http://www.openssl.org/docs/apps/ciphers.html">ciphers(1)</a> man page is quite helpful.</p>
<p># list all available ciphers</p>
<p>openssl ciphers -v</p>
<p># list only TLSv1 ciphers</p>
<p>openssl ciphers -v -tls1</p>
<p># list only high encryption ciphers (keys larger than 128 bits)</p>
<p>openssl ciphers -v &#8216;HIGH&#8217;</p>
<p># list only high encryption ciphers using the AES algorithm</p>
<p>openssl ciphers -v &#8216;AES+HIGH&#8217;</p>
<p><strong>Benchmarking</strong></p>
<p><strong>How do I benchmark my system’s performance?</strong></p>
<p>The OpenSSL developers have built a benchmarking suite directly into the <strong>openssl</strong> binary. It’s accessible via the speed option. It tests how many operations it can perform in a given time, rather than how long it takes to perform a given number of operations. This strikes me a quite sane, because the benchmarks don’t take significantly longer to run on a slow system than on a fast one.</p>
<p>To run a catchall benchmark, run it without any further options.</p>
<p>openssl speed</p>
<p>There are two sets of results. The first reports how many bytes per second can be processed for each algorithm, the second the times needed for sign/verify cycles. Here are the results on an 2.16GHz Intel Core 2.</p>
<p>The &#8216;numbers&#8217; are in 1000s of bytes per second processed.</p>
<p>type             16 bytes     64 bytes    256 bytes   1024 bytes   8192 bytes</p>
<p>md2               1736.10k     3726.08k     5165.04k     5692.28k     5917.35k</p>
<p>mdc2                 0.00         0.00         0.00         0.00         0.00</p>
<p>md4              18799.87k    65848.23k   187776.43k   352258.73k   474622.63k</p>
<p>md5              16807.01k    58256.45k   160439.13k   287183.53k   375220.91k</p>
<p>hmac(md5)        23601.24k    74405.08k   189993.05k   309777.75k   379431.59k</p>
<p>sha1             16774.59k    55500.39k   142628.69k   233247.74k   288382.98k</p>
<p>rmd160           13854.71k    40271.23k    87613.95k   124333.06k   141781.67k</p>
<p>rc4             227935.60k   253366.06k   261236.94k   259858.09k   194928.50k</p>
<p>des cbc          48478.10k    49616.16k    49765.21k    50106.71k    50034.01k</p>
<p>des ede3         18387.39k    18631.02k    18699.26k    18738.18k    18718.72k</p>
<p>idea cbc             0.00         0.00         0.00         0.00         0.00</p>
<p>rc2 cbc          19247.24k    19838.12k    19904.51k    19925.33k    19834.98k</p>
<p>rc5-32/12 cbc        0.00         0.00         0.00         0.00         0.00</p>
<p>blowfish cbc     79577.50k    83067.03k    84676.78k    84850.01k    85063.00k</p>
<p>cast cbc         45362.14k    48343.34k    49007.36k    49202.52k    49225.73k</p>
<p>aes-128 cbc      58751.94k    94443.86k   111424.09k   116704.26k   117997.57k</p>
<p>aes-192 cbc      53451.79k    82076.22k    94609.83k    98496.85k    99150.51k</p>
<p>aes-256 cbc      49225.21k    72779.84k    82266.88k    85054.81k    85762.05k</p>
<p>sha256            9359.24k    22510.83k    40963.75k    51710.29k    56014.17k</p>
<p>sha512            7026.78k    28121.32k    54330.79k    86190.76k   104270.51k</p>
<p>sign    verify    sign/s verify/s</p>
<p>rsa  512 bits 0.000522s 0.000042s   1915.8  23969.9</p>
<p>rsa 1024 bits 0.002321s 0.000109s    430.8   9191.1</p>
<p>rsa 2048 bits 0.012883s 0.000329s     77.6   3039.6</p>
<p>rsa 4096 bits 0.079055s 0.001074s     12.6    931.3</p>
<p>sign    verify    sign/s verify/s</p>
<p>dsa  512 bits 0.000380s 0.000472s   2629.3   2117.9</p>
<p>dsa 1024 bits 0.001031s 0.001240s    969.6    806.2</p>
<p>dsa 2048 bits 0.003175s 0.003744s    314.9    267.1</p>
<p>You can run any of the algorithm-specific subtests directly.</p>
<p># test rsa speeds</p>
<p>openssl speed rsa</p>
<p># do the same test on a two-way SMP system</p>
<p>openssl speed rsa -multi 2</p>
<p><strong>How do I benchmark remote connections?</strong></p>
<p>The s_time option lets you test connection performance. The most simple invocation will run for 30 seconds, use any cipher, and use SSL handshaking to determine number of connections per second, using both new and reused sessions:</p>
<p>openssl s_time -connect remote.host:443</p>
<p>Beyond that most simple invocation, s_time gives you a wide variety of testing options.</p>
<p># retrieve remote test.html page using only new sessions</p>
<p>openssl s_time -connect remote.host:443 -www /test.html -new</p>
<p># similar, using only SSL v3 and high encryption (see</p>
<p># ciphers(1) man page for cipher strings)</p>
<p>openssl s_time \</p>
<p>-connect remote.host:443 -www /test.html -new \</p>
<p>-ssl3 -cipher HIGH</p>
<p># compare relative performance of various ciphers in</p>
<p># 10-second tests</p>
<p>IFS=&#8221;:&#8221;</p>
<p>for c in $(openssl ciphers -ssl3 RSA); do</p>
<p>echo $c</p>
<p>openssl s_time -connect remote.host:443 \</p>
<p>-www / -new -time 10 -cipher $c 2&gt;&amp;1 | \</p>
<p>grep bytes</p>
<p>echo</p>
<p>done</p>
<p>If you don’t have an SSL-enabled web server available for your use, you can emulate one using the s_server option.</p>
<p># on one host, set up the server (using default port 4433)</p>
<p>openssl s_server -cert mycert.pem -www</p>
<p># on second host (or even the same one), run s_time</p>
<p>openssl s_time -connect myhost:4433 -www / -new -ssl3</p>
<p><strong>Certificates</strong></p>
<p><strong>How do I generate a self-signed certificate?</strong></p>
<p>You’ll first need to decide whether or not you want to encrypt your key. Doing so means that the key is protected by a passphrase.</p>
<p>On the plus side, adding a passphrase to a key makes it more secure, so the key is less likely to be useful to someone who steals it. The downside, however, is that you’ll have to either store the passphrase in a file or type it manually every time you want to start your web or ldap server.</p>
<p>It violates my normally paranoid nature to say it, but I prefer unencrypted keys, so I don’t have to manually type a passphrase each time a secure daemon is started. (It’s not terribly difficult <a title="How do I  remove a passphrase from a key?" href="#key-removepass">to decrypt your key</a> if you later tire of typing a passphrase.)</p>
<p>This example will produce a file called mycert.pem which will contain both the private key and the public certificate based on it. The certificate will be valid for 365 days, and the key (thanks to the -nodes option) is unencrypted.</p>
<p>openssl req \</p>
<p>-x509 -nodes -days 365 \</p>
<p>-newkey rsa:1024 -keyout mycert.pem -out mycert.pem</p>
<p>Using this command-line invocation, you’ll have to answer a lot of questions: Country Name, State, City, and so on. The tricky question is “Common Name.” You’ll want to answer with the <em>hostname or CNAME by which people will address the server</em>. This is very important. If your web server’s real hostname is mybox.mydomain.com but people will be using www.mydomain.com to address the box, then use the latter name to answer the “Common Name” question.</p>
<p>Once you’re comfortable with the answers you provide to those questions, you can script the whole thing by adding the -subj option. I’ve included some information about location into the example that follows, but the only thing you really need to include for the certificate to be useful is the hostname (CN).</p>
<p>openssl req \</p>
<p>-x509 -nodes -days 365 \</p>
<p>-subj &#8216;/C=US/ST=Oregon/L=Portland/CN=www.madboa.com&#8217; \</p>
<p>-newkey rsa:1024 -keyout mycert.pem -out mycert.pem</p>
<p><strong>How do I generate a certificate request for VeriSign?</strong></p>
<p>Applying for a certificate signed by a recognized certificate authority like VeriSign is a complex bureaucratic process. You’ve got to perform all the requisite paperwork before creating a certificate request.</p>
<p>As in the recipe for <a title="How do I  generate a self-signed certificate?" href="#cert-self">creating a self-signed certificate</a>, you’ll have to decide whether or not you want a passphrase on your private key. The recipe below assumes you don’t. You’ll end up with two files: a new private key called mykey.pem and a certificate request called myreq.pem.</p>
<p>openssl req \</p>
<p>-new -newkey rsa:1024 -nodes \</p>
<p>-keyout mykey.pem -out myreq.pem</p>
<p>If you’ve already <a title="How do I  generate an RSA key?" href="#key-rsa">got a key</a> and would like to use it for generating the request, the syntax is a bit simpler.</p>
<p>openssl req -new -key mykey.pem -out myreq.pem</p>
<p>Similarly, you can also provide subject information on the command line.</p>
<p>openssl req \</p>
<p>-new -newkey rsa:1024 -nodes \</p>
<p>-subj &#8216;/CN=www.mydom.com/O=My Dom, Inc./C=US/ST=Oregon/L=Portland&#8217; \</p>
<p>-keyout mykey.pem -out myreq.pem</p>
<p>When dealing with an institution like VeriSign, you need to take special care to make sure that the information you provide during the creation of the certificate request is <em>exactly</em> correct. I know from personal experience that even a difference as trivial as substituting “and” for “&amp;” in the Organization Name will stall the process.</p>
<p>If you’d like, you can double check the signature and information provided in the certificate request.</p>
<p># verify signature</p>
<p>openssl req -in myreq.pem -noout -verify -key mykey.pem</p>
<p># check info</p>
<p>openssl req -in myreq.pem -noout -text</p>
<p>Save the key file in a secure location. You’ll need it in order to use the certificate VeriSign sends you. The certificate request will typically be pasted into VeriSign’s online application form.</p>
<p><strong>How do I test a new certificate?</strong></p>
<p>The s_server option provides a simple but effective testing method. The example below assumes you’ve combined your key and certificate into one file called mycert.pem.</p>
<p>First, launch the test server on the machine on which the certificate will be used. By default, the server will listen on port 4433; you can alter that using the -accept option.</p>
<p>openssl s_server -cert mycert.pem -www</p>
<p>If the server launches without complaint, then chances are good that the certificate is ready for production use.</p>
<p>You can also point your web browser at the test server, <em>e.g.</em>, <strong>https://yourserver:4433/</strong>. Don’t forget to specify the “https” protocol; plain-old “http” won’t work. You should see a page listing the various ciphers available and some statistics about your connection. Most modern browsers allow you to examine the certificate as well.</p>
<p><strong>How do I retrieve a remote certificate?</strong></p>
<p>If you combine <strong>openssl</strong> and <strong>sed</strong>, you can retrieve remote certificates via a shell one-liner or a simple script.</p>
<p>#!/bin/sh</p>
<p>#</p>
<p># usage: retrieve-cert.sh remote.host.name [port]</p>
<p>#</p>
<p>REMHOST=$1</p>
<p>REMPORT=${2:-443}</p>
<p>echo |\</p>
<p>openssl s_client -connect ${REMHOST}:${REMPORT} 2&gt;&amp;1 |\</p>
<p>sed -ne &#8216;/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p&#8217;</p>
<p>You can, in turn, pipe that information back to <strong>openssl</strong> to do things like check the dates on all your active certificates.</p>
<p>#!/bin/sh</p>
<p>#</p>
<p>for CERT in \</p>
<p>www.yourdomain.com:443 \</p>
<p>ldap.yourdomain.com:636 \</p>
<p>imap.yourdomain.com:993 \</p>
<p>do</p>
<p>echo |\</p>
<p>openssl s_client -connect ${CERT} 2&gt;/dev/null |\</p>
<p>sed -ne &#8216;/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p&#8217; |\</p>
<p>openssl x509 -noout -subject -dates</p>
<p>done</p>
<p><strong>How do I extract information from a certificate?</strong></p>
<p>An SSL certificate contains a wide range of information: issuer, valid dates, subject, and some hardcore crypto stuff. The x509 subcommand is the entry point for retrieving this information. The examples below all assume that the certificate you want to examine is stored in a file named cert.pem.</p>
<p>Using the -text option will give you the full breadth of information.</p>
<p>openssl x509 -text -in cert.pem</p>
<p>Other options will provide more targeted sets of data.</p>
<p># who issued the cert?</p>
<p>openssl x509 -noout -in cert.pem -issuer</p>
<p># to whom was it issued?</p>
<p>openssl x509 -noout -in cert.pem -subject</p>
<p># for what dates is it valid?</p>
<p>openssl x509 -noout -in cert.pem -dates</p>
<p># the above, all at once</p>
<p>openssl x509 -noout -in cert.pem -issuer -subject -dates</p>
<p># what is its hash value?</p>
<p>openssl x509 -noout -in cert.pem -hash</p>
<p># what is its MD5 fingerprint?</p>
<p>openssl x509 -noout -in cert.pem -fingerprint</p>
<p><strong>How do I export or import a PKCS#12 certificate?</strong></p>
<p>PKCS#12 files can be imported and exported by a number of applications, including Microsoft IIS. They are often associated with the file extension .pfx.</p>
<p>To create a PKCS#12 certificate, you’ll need a private key and a certificate. During the conversion process, you’ll be given an opportunity to put an “Export Password” (which can be empty, if you choose) on the certificate.</p>
<p># create a file containing key and self-signed certificate</p>
<p>openssl req \</p>
<p>-x509 -nodes -days 365 \</p>
<p>-newkey rsa:1024 -keyout mycert.pem -out mycert.pem</p>
<p># export mycert.pem as PKCS#12 file, mycert.pfx</p>
<p>openssl pkcs12 -export \</p>
<p>-out mycert.pfx -in mycert.pem \</p>
<p>-name &#8220;My Certificate&#8221;</p>
<p>If someone sends you a PKCS#12 and any passwords needed to work with it, you can export it into standard PEM format.</p>
<p># export certificate and passphrase-less key</p>
<p>openssl pkcs12 -in mycert.pfx -out mycert.pem -nodes</p>
<p># same as above, but you’ll be prompted for a passphrase for</p>
<p># the private key</p>
<p>openssl pkcs12 -in mycert.pfx -out mycert.pem</p>
<p><strong>Certificate Verification</strong></p>
<p>Applications linked against the OpenSSL libraries can verify certificates signed by a recognized certificate authority (CA).</p>
<p><strong>How do I verify a certificate?</strong></p>
<p>Use the verify option to verify certificates.</p>
<p>openssl verify cert.pem</p>
<p>If your local OpenSSL installation recognizes the certificate or its signing authority and everything else (dates, signing chain, etc.) checks out, you’ll get a simple OK message.</p>
<p>$ <strong>openssl verify remote.site.pem</strong></p>
<p>remote.site.pem: OK</p>
<p>If anything is amiss, you’ll see some error messages with short descriptions of the problem, <em>e.g.</em>,</p>
<ul>
<li>error 10 at 0 depth      lookup:certificate has expired.      Certificates are typically issued for a limited period of time—usually      just one year—and <strong>openssl</strong> will complain if a certificate has      expired.</li>
<li>error 18 at 0 depth lookup:self      signed certificate. Unless you <a title="How do I  get OpenSSL to recognize/verify a certificate?" href="#verify-new">make      an exception</a>, OpenSSL won’t verify a self-signed certificate.</li>
</ul>
<p><strong>What certificate authorities does OpenSSL recognize?</strong></p>
<p>When OpenSSL was built for your system, it was configured with a “Directory for OpenSSL files.” (That’s the &#8211;openssldir option passed to the configure script, for you hands-on types.) This is the directory that typically holds information about certificate authorities your system trusts.</p>
<p>The default location for this directory is /usr/local/ssl, but most vendors put it elsewhere, <em>e.g.</em>, /usr/share/ssl (Red Hat/Fedora), /etc/ssl (Gentoo), /usr/lib/ssl (Debian), or /System/Library/OpenSSL (Macintosh OS X).</p>
<p>Use the version option to identify which directory (labeled OPENSSLDIR) your installation uses.</p>
<p>openssl version -d</p>
<p>Within that directory and a subdirectory called certs, you’re likely to find one or more of three different kinds of files.</p>
<ol>
<li>A large file called cert.pem,      an omnibus collection of many certificates from recognized certificate      authorities like VeriSign and Thawte.</li>
<li>Some small files in the certs      subdirectory named with a .pem file extension, each of which contains a certificate      from a single CA.</li>
<li>Some symlinks in the certs      subdirectory with obscure filenames like 052eae11.0.      There is typically one of these links for each .pem file.</li>
</ol>
<p>The first part of obscure filename is actually a hash value based on the certificate within the .pem file to which it points. The file extension is just an iterator, since it’s theoretically possible that multiple certificates can generate identical hashes.</p>
<p>On my Gentoo system, for example, there’s a symlink named f73e89fd.0 that points to a file named vsignss.pem. Sure enough, the certificate in that file generates a hash the equates to the name of the symlink:</p>
<p>$ <strong>openssl x509 -noout -hash -in vsignss.pem</strong></p>
<p>f73e89fd</p>
<p>When an application encounters a remote certificate, it will typically check to see if the cert can be found in cert.pem or, if not, in a file named after the certificate’s hash value. If found, the certificate is considered verified.</p>
<p>It’s interesting to note that some applications, like Sendmail, allow you to specify at runtime the location of the certificates you trust, while others, like Pine, do not.</p>
<p><strong>How do I get OpenSSL to recognize/verify a certificate?</strong></p>
<p>Put the file that contains the certificate you’d like to trust into the certs directory discussed <a title="What  certificate authorities does OpenSSL recognize?" href="#verify-system">above</a>. Then create the hash-based symlink. Here’s a little script that’ll do just that.</p>
<p>#!/bin/sh</p>
<p>#</p>
<p># usage: certlink.sh filename [filename ...]</p>
<p>for CERTFILE in $*; do</p>
<p># make sure file exists and is a valid cert</p>
<p>test -f &#8220;$CERTFILE&#8221; || continue</p>
<p>HASH=$(openssl x509 -noout -hash -in &#8220;$CERTFILE&#8221;)</p>
<p>test -n &#8220;$HASH&#8221; || continue</p>
<p># use lowest available iterator for symlink</p>
<p>for ITER in 0 1 2 3 4 5 6 7 8 9; do</p>
<p>test -f &#8220;${HASH}.${ITER}&#8221; &amp;&amp; continue</p>
<p>ln -s &#8220;$CERTFILE&#8221; &#8220;${HASH}.${ITER}&#8221;</p>
<p>test -L &#8220;${HASH}.${ITER}&#8221; &amp;&amp; break</p>
<p>done</p>
<p>done</p>
<p><strong>Command-line clients and servers</strong></p>
<p>The s_client and s_server options provide a way to launch SSL-enabled command-line clients and servers. There are other examples of their use scattered around this document, but this section is dedicated solely to them.</p>
<p>In this section, I assume you are familiar with the specific protocols at issue: SMTP, HTTP, etc. Explaining them is out of the scope of this article.</p>
<p><strong>How do I connect to a secure SMTP server?</strong></p>
<p>You can test, or even use, an SSL-enabled SMTP server from the command line using the s_client option.</p>
<p>Secure SMTP servers offer secure connections on up to three ports: 25 (TLS), 465 (SSL), and 587 (TLS). Some time around the 0.9.7 release, the <strong>openssl</strong> binary was given the ability to use STARTTLS when talking to SMTP servers.</p>
<p># port 25/TLS; use same syntax for port 587</p>
<p>openssl s_client -connect remote.host:25 -starttls smtp</p>
<p># port 465/SSL</p>
<p>openssl s_client -connect remote.host:465</p>
<p><a href="http://www.ietf.org/rfc/rfc0821.txt">RFC821</a> suggests (although it falls short of explicitly specifying) the two characters &#8220;&lt;CRLF&gt;&#8221; as line-terminator. Most mail agents do not care about this and accept either &#8220;&lt;LF&gt;&#8221; or &#8220;&lt;CRLF&gt;&#8221; as line-terminators, but Qmail does not. If you want to comply to the letter with RFC821 and/or communicate with Qmail, use also the -crlf option:</p>
<p>openssl s_client -connect remote.host:25 -crlf -starttls smtp</p>
<p><strong>How do I connect to a secure [whatever] server?</strong></p>
<p>Connecting to a different type of SSL-enabled server is essentially the same operation as outlined above. As of the date of this writing, <strong>openssl</strong> only supports command-line TLS with SMTP servers, so you have to use straightforward SSL connections with any other protocol.</p>
<p># https: HTTP over SSL</p>
<p>openssl s_client -connect remote.host:443</p>
<p># ldaps: LDAP over SSL</p>
<p>openssl s_client -connect remote.host:636</p>
<p># imaps: IMAP over SSL</p>
<p>openssl s_client -connect remote.host:993</p>
<p># pop3s: POP-3 over SSL</p>
<p>openssl s_client -connect remote.host:995</p>
<p><strong>How do I set up an SSL server from the command line?</strong></p>
<p>The s_server option allows you to set up an SSL-enabled server from the command line, but it’s I wouldn’t recommend using it for anything other than testing or debugging. If you need a production-quality wrapper around an otherwise insecure server, check out <a href="http://www.stunnel.org/">Stunnel</a> instead.</p>
<p>The s_server option works best when you have a certificate; it’s fairly limited without one.</p>
<p># the -www option will sent back an HTML-formatted status page</p>
<p># to any HTTP clients that request a page</p>
<p>openssl s_server -cert mycert.pem -www</p>
<p># the -WWW option &#8220;emulates a simple web server. Pages will be</p>
<p># resolved relative to the current directory.&#8221; This example</p>
<p># is listening on the https port, rather than the default</p>
<p># port 4433</p>
<p>openssl s_server -accept 443 -cert mycert.pem -WWW</p>
<p><strong>Digests</strong></p>
<p>Generating digests with the dgst option is one of the more straightforward tasks you can accomplish with the <strong>openssl</strong> binary. Producing digests is done so often, as a matter of fact, that you can find special-use binaries for doing the same thing.</p>
<p><strong>How do I create an MD5 or SHA1 digest of a file?</strong></p>
<p>Digests are created using the dgst option.</p>
<p># MD5 digest</p>
<p>openssl dgst -md5 filename</p>
<p># SHA1 digest</p>
<p>openssl dgst -sha1 filename</p>
<p>The MD5 digests are identical to those created with the widely available <strong>md5sum</strong> command, though the output formats differ.</p>
<p>$ <strong>openssl dgst -md5 foo-2.23.tar.gz</strong></p>
<p>MD5(foo-2.23.tar.gz)= 81eda7985e99d28acd6d286aa0e13e07</p>
<p>$ <strong>md5sum foo-2.23.tar.gz</strong></p>
<p>81eda7985e99d28acd6d286aa0e13e07  foo-2.23.tar.gz</p>
<p>The same is true for SHA1 digests and the output of the <strong>sha1sum</strong> application.</p>
<p>$ <strong>openssl dgst -sha1 foo-2.23.tar.gz</strong></p>
<p>SHA1(foo-2.23.tar.gz)= e4eabc78894e2c204d788521812497e021f45c08</p>
<p>$ <strong>sha1sum foo-2.23.tar.gz</strong></p>
<p>e4eabc78894e2c204d788521812497e021f45c08  foo-2.23.tar.gz</p>
<p><strong>How do I sign a digest?</strong></p>
<p>If you want to ensure that the digest you create doesn’t get modified without your permission, you can sign it using your <a title="How do I  generate an RSA key?" href="#key-rsa">private key</a>. The following example assumes that you want to sign the SHA1 sum of a file called foo-1.23.tar.gz.</p>
<p># signed digest will be foo-1.23.tar.gz.sha1</p>
<p>openssl dgst -sha1 \</p>
<p>-sign mykey.pem</p>
<p>-out foo-1.23.tar.gz.sha1 \</p>
<p>foo-1.23.tar.gz</p>
<p><strong>How do I verify a signed digest?</strong></p>
<p>To verify a signed digest you’ll need the file from which the digest was derived, the signed digest, and the signer’s <a title="How do I  generate a public RSA key?" href="#key-rsapublic">public key</a>.</p>
<p># to verify foo-1.23.tar.gz using foo-1.23.tar.gz.sha1</p>
<p># and pubkey.pem</p>
<p>openssl dgst -sha1 \</p>
<p>-verify pubkey.pem \</p>
<p>-signature foo-1.23.tar.gz.sha1 \</p>
<p>foo-1.23.tar.gz</p>
<p><strong>How do I create an Apache digest password entry?</strong></p>
<p>Apache’s HTTP digest authentication feature requires a special password format. Apache ships with the <strong>htdigest</strong> utility, but it will only write to a file, not to standard output. When working with remote users, it’s sometimes nice for them to be able to generate a password hash on a machine they trust and then mail it for inclusion in your local password database.</p>
<p>The format of the password database is relatively simple: a colon-separated list of the username, authorization realm (specified by the Apache AuthName directive), and an MD5 digest of those two items and the password. Below is a script that duplicates the output of <strong>htdigest</strong>, except that the output is written to standard output. It takes advantage of the dgst option’s ability to read from standard input.</p>
<p>#!/bin/bash</p>
<p>echo &#8220;Create an Apache-friendly Digest Password Entry&#8221;</p>
<p>echo &#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#8221;</p>
<p># get user input, disabling tty echoing for password</p>
<p>read -p &#8220;Enter username: &#8221; UNAME</p>
<p>read -p &#8220;Enter Apache AuthName: &#8221; AUTHNAME</p>
<p>read -s -p &#8220;Enter password: &#8221; PWORD; echo</p>
<p>printf &#8220;\n%s:%s:%s\n&#8221; \</p>
<p>&#8220;$UNAME&#8221; \</p>
<p>&#8220;$AUTHNAME&#8221; \</p>
<p>$(printf &#8220;${UNAME}:${AUTHNAME}:${PWORD}&#8221; | openssl dgst -md5)</p>
<p><strong>What other kinds of digests are available?</strong></p>
<p>Use the built-in list-message-digest-commands option to get a list of the digest types available to your local OpenSSL installation.</p>
<p>openssl list-message-digest-commands</p>
<p><strong>Encryption/Decryption</strong></p>
<p><strong>How do I base64-encode something?</strong></p>
<p>Use the enc -base64 option.</p>
<p># send encoded contents of file.txt to stdout</p>
<p>openssl enc -base64 -in file.txt</p>
<p># same, but write contents to file.txt.enc</p>
<p>openssl enc -base64 -in file.txt -out file.txt.enc</p>
<p>It’s also possible to do a quick command-line encoding of a string value:</p>
<p>$ <strong>echo &#8220;encode me&#8221; | openssl enc -base64</strong></p>
<p>ZW5jb2RlIG1lCg==</p>
<p>Note that <strong>echo</strong> will silently attach a newline character to your string. Consider using its -n option if you want to avoid that situation, which could be important if you’re trying to encode a password or authentication string.</p>
<p>$ <strong>echo -n &#8220;encode me&#8221; | openssl enc -base64</strong></p>
<p>ZW5jb2RlIG1l</p>
<p>Use the -d (decode) option to reverse the process.</p>
<p>$ <strong>echo &#8220;ZW5jb2RlIG1lCg==&#8221; | openssl enc -base64 -d</strong></p>
<p>encode me</p>
<p><strong>How do I simply encrypt a file?</strong></p>
<p>Simple file encryption is probably better done using a <a href="http://www.madboa.com/geek/gpg-quickstart/">tool like GPG</a>. Still, you may have occasion to want to encrypt a file without having to build or use a key/certificate structure. All you want to have to remember is a password. It can nearly be that simple—if you can also remember the cipher you employed for encryption.</p>
<p>To choose a cipher, consult the <a href="http://www.openssl.org/docs/apps/enc.html">enc(1) man page</a>. More simply (and perhaps more accurately), you can ask <strong>openssl</strong> for a list in one of two ways.</p>
<p># see the list under the &#8216;Cipher commands&#8217; heading</p>
<p>openssl -h</p>
<p># or get a long list, one cipher per line</p>
<p>openssl list-cipher-commands</p>
<p>After you choose a cipher, you’ll also have to decide if you want to base64-encode the data. Doing so will mean the encrypted data can be, say, pasted into an email message. Otherwise, the output will be a binary file.</p>
<p># encrypt file.txt to file.enc using 256-bit AES in CBC mode</p>
<p>openssl enc -aes-256-cbc -salt -in file.txt -out file.enc</p>
<p># the same, only the output is base64 encoded for, e.g., e-mail</p>
<p>openssl enc -aes-256-cbc -a -salt -in file.txt -out file.enc</p>
<p>To decrypt file.enc you or the file’s recipient will need to remember the cipher and the passphrase.</p>
<p># decrypt binary file.enc</p>
<p>openssl enc -d -aes-256-cbc -in file.enc</p>
<p># decrypt base64-encoded version</p>
<p>openssl enc -d -aes-256-cbc -a -in file.enc</p>
<p>If you’d like to avoid typing a passphrase every time you encrypt or decrypt a file, the <em>openssl(1)</em> man page provides the details under the heading “PASS PHRASE ARGUMENTS.” The format of the password argument is fairly simple.</p>
<p># provide password on command line</p>
<p>openssl enc -aes-256-cbc -salt -in file.txt \</p>
<p>-out file.enc -pass pass:mySillyPassword</p>
<p># provide password in a file</p>
<p>openssl enc -aes-256-cbc -salt -in file.txt \</p>
<p>-out file.enc -pass file:/path/to/secret/password.txt</p>
<p><strong>Errors</strong></p>
<p><strong>How do I interpret SSL error messages?</strong></p>
<p>Poking through your system logs, you see some error messages that are evidently related to OpenSSL or crypto:</p>
<p>sshd[31784]: error: RSA_public_decrypt failed: error:0407006A:lib(4):func(112):reason(106)</p>
<p>sshd[770]: error: RSA_public_decrypt failed: error:0407006A:lib(4):func(112):reason(106)</p>
<p>The first step to figure out what’s going wrong is to use the errstr option to intrepret the error code. The code number is found between “error:” and “:lib”. In this case, it’s 0407006A.</p>
<p>$ <strong>openssl errstr 0407006A</strong></p>
<p>error:0407006A:rsa routines:RSA_padding_check_PKCS1_type_1:block type is not 01</p>
<p>If you’ve got a full OpenSSL installation, including all the development documentation, you can start your investigation there. In this example, the <em>RSA_padding_add_PKCS1_type_1(3)</em> man page will inform you that PKCS #1 involves block methods for signatures. After that, of course, you’d need to pore through your application’s source code to identify when it would expect be receiving those sorts of packets.</p>
<p><strong>Keys</strong></p>
<p><strong>How do I generate an RSA key?</strong></p>
<p>Use the genrsa option.</p>
<p># default 512-bit key, sent to standard output</p>
<p>openssl genrsa</p>
<p># 1024-bit key, saved to file named mykey.pem</p>
<p>openssl genrsa -out mykey.pem 1024</p>
<p># same as above, but encrypted with a passphrase</p>
<p>openssl genrsa -des3 -out mykey.pem 1024</p>
<p><strong>How do I generate a public RSA key?</strong></p>
<p>Use the rsa option to produce a public version of your private RSA key.</p>
<p>openssl rsa -in mykey.pem -pubout</p>
<p><strong>How do I generate a DSA key?</strong></p>
<p>Building DSA keys requires a parameter file, and DSA verify operations are slower than their RSA counterparts, so they aren’t as widely used as RSA keys.</p>
<p>If you’re only going to build a single DSA key, you can do so in just one step using the dsaparam subcommand.</p>
<p># key will be called dsakey.pem</p>
<p>openssl dsaparam -noout -out dsakey.pem -genkey 1024</p>
<p>If, on the other hand, you’ll be creating several DSA keys, you’ll probably want to build a shared parameter file before generating the keys. It can take a while to build the parameters, but once built, key generation is done quickly.</p>
<p># create parameters in dsaparam.pem</p>
<p>openssl dsaparam -out dsaparam.pem 1024</p>
<p># create first key</p>
<p>openssl gendsa -out key1.pem dsaparam.pem</p>
<p># and second &#8230;</p>
<p>openssl gendsa -out key2.pem dsaparam.pem</p>
<p><strong>How do I create an elliptic curve key?</strong></p>
<p>Routines for working with <a href="http://www.secg.org/">elliptic curve cryptography</a> were added to OpenSSL in version 0.9.8. Generating an EC key involves the ecparam option.</p>
<p>openssl ecparam -out key.pem -name prime256v1 -genkey</p>
<p># openssl can provide full list of EC parameter names suitable for</p>
<p># passing to the -name option above:</p>
<p>openssl ecparam -list_curves</p>
<p><strong>How do I remove a passphrase from a key?</strong></p>
<p>Perhaps you’ve grown tired of typing your passphrase every time your secure daemon starts. You can decrypt your key, removing the passphrase requirement, using the rsa or dsa option, depending on the signature algorithm you chose when creating your private key.</p>
<p>If you created an RSA key and it is stored in a standalone file called key.pem, then here’s how to output a decrypted version of the same key to a file called newkey.pem.</p>
<p># you&#8217;ll be prompted for your passphrase one last time</p>
<p>openssl rsa -in key.pem -out newkey.pem</p>
<p>Often, you’ll have your private key and public certificate stored in the same file. If they are stored in a file called mycert.pem, you can construct a decrypted version called newcert.pem in two steps.</p>
<p># you&#8217;ll need to type your passphrase once more</p>
<p>openssl rsa -in mycert.pem -out newcert.pem</p>
<p>openssl x509 -in mycert.pem &gt;&gt;newcert.pem</p>
<p><strong>Password hashes</strong></p>
<p>Using the passwd option, you can generate password hashes that interoperate with traditional /etc/passwd files, newer-style /etc/shadow files, and Apache password files.</p>
<p><strong>How do I generate a crypt-style password hash?</strong></p>
<p>You can generate a new hash quite simply:</p>
<p>$ <strong>openssl passwd MySecret</strong></p>
<p>8E4vqBR4UOYF.</p>
<p>If you know an existing password’s “salt,” you can duplicate the hash.</p>
<p>$ <strong>openssl passwd -salt 8E MySecret</strong></p>
<p>8E4vqBR4UOYF.</p>
<p><strong>How do I generate a shadow-style password hash?</strong></p>
<p>Newer Unix systems use a more secure MD5-based hashing mechanism that uses an eight-character salt (as compared to the two-character salt in traditional crypt()-style hashes). Generating them is still straightforward using the -1 option:</p>
<p>$ <strong>openssl passwd -1 MySecret</strong></p>
<p>$1$sXiKzkus$haDZ9JpVrRHBznY5OxB82.</p>
<p>The salt in this format consists of the eight characters between the second and third dollar signs, in this case sXiKzkus. So you can also duplicate a hash with a known salt and password.</p>
<p>$ <strong>openssl passwd -1 -salt sXiKzkus MySecret</strong></p>
<p>$1$sXiKzkus$haDZ9JpVrRHBznY5OxB82.</p>
<p><strong>Prime numbers</strong></p>
<p>Current cryptographic techniques rely heavily on the generation and testing of prime numbers, so it’s no surprise that the OpenSSL libraries contain several routines dealing with primes. Beginning with version 0.9.7e (or so), the prime option was added to the openssl binary.</p>
<p><strong>How do I test whether a number is prime?</strong></p>
<p>Pass the number to the prime option. Note that the number returned by openssl will be in hex, not decimal, format.</p>
<p>$ <strong>openssl prime 119054759245460753</strong></p>
<p>1A6F7AC39A53511 is not prime</p>
<p>You can also pass hex numbers directly.</p>
<p>$ <strong>openssl prime -hex 2f</strong></p>
<p>2F is prime</p>
<p><strong>How do I generate a set of prime numbers?</strong></p>
<p>Pass a bunch of numbers to openssl and see what sticks. The <strong>seq</strong> utility is useful in this capacity.</p>
<p># define start and ending points</p>
<p>AQUO=10000</p>
<p>ADQUEM=10100</p>
<p>for N in $(seq $AQUO $ADQUEM); do</p>
<p># use bc to convert hex to decimal</p>
<p>openssl prime $N | awk &#8216;/is prime/ {print &#8220;ibase=16;&#8221;$1}&#8217; | bc</p>
<p>done</p>
<p><strong>Random data</strong></p>
<p><strong>How do I generate random data?</strong></p>
<p>Use the rand option to generate binary or base64-encoded data.</p>
<p># write 128 random bytes of base64-encoded data to stdout</p>
<p>openssl rand -base64 128</p>
<p># write 1024 bytes of binary random data to a file</p>
<p>openssl rand -out random-data.bin 1024</p>
<p># seed openssl with semi-random bytes from browser cache</p>
<p>cd $(find ~/.mozilla/firefox -type d -name Cache)</p>
<p>openssl rand -rand $(find . -type f -printf &#8216;%f:&#8217;) -base64 1024</p>
<p>On a Unix box with a /dev/urandom device and a copy of GNU <strong>head</strong>, you can achieve a similar effect, often with better entropy:</p>
<p># get 32 bytes from /dev/urandom and base64 encode them</p>
<p>head -c 32 /dev/urandom | openssl enc -base64</p>
<p>Make sure you know the trade-offs between the random and urandom devices before relying on them for truly critical entropy. Consult the <em>random(4)</em> man page on Linux and BSD systems, or <em>random(7D)</em> on Solaris, for further information.</p>
<p><strong>S/MIME</strong></p>
<p><a href="http://www.ietf.org/html.charters/smime-charter.html">S/MIME</a> is a standard for sending and receiving secure MIME data, especially in e-mail messages. Automated S/MIME capabilities have been added to quite a few e-mail clients, though <strong>openssl</strong> can provide command-line S/MIME services using the smime option.</p>
<p>Note that the documentation in the <a href="http://www.openssl.org/docs/apps/smime.html">smime(1)</a> man page includes a number of good examples.</p>
<p><strong>How do I verify a signed S/MIME message?</strong></p>
<p>It’s pretty easy to verify a signed message. Use your mail client to save the signed message to a file. In this example, I assume that the file is named msg.txt.</p>
<p>openssl smime -verify -in msg.txt</p>
<p>If the sender’s certificate is signed by a certificate authority trusted by your OpenSSL infrastructure, you’ll see some mail headers, a copy of the message, and a concluding line that says Verification successful.</p>
<p>If the messages has been modified by an unauthorized party, the output will conclude with a failure message indicating that the digest and/or the signature doesn’t match what you received:</p>
<p>Verification failure</p>
<p>23016:error:21071065:PKCS7 routines:PKCS7_signatureVerify:digest</p>
<p>failure:pk7_doit.c:804:</p>
<p>23016:error:21075069:PKCS7 routines:PKCS7_verify:signature</p>
<p>failure:pk7_smime.c:265:</p>
<p>Likewise, if the sender’s certificate isn’t recognized by your OpenSSL infrastructure, you’ll get a similar error:</p>
<p>Verification failure</p>
<p>9544:error:21075075:PKCS7 routines:PKCS7_verify:certificate verify</p>
<p>error:pk7_smime.c:222:Verify error:self signed certificate</p>
<p>Most e-mail clients send a copy of the public certificate in the signature attached to the message. From the command line, you can view the certificate data yourself. You’ll use the smime -pk7out option to pipe a copy of the PKCS#7 certificate back into the pkcs7 option. It’s oddly cumbersome but it works.</p>
<p>openssl smime -pk7out -in msg.txt | \</p>
<p>openssl pkcs7 -text -noout -print_certs</p>
<p>If you’d like to extract a copy of your correspondent’s certificate for long-term use, use just the first part of that pipe.</p>
<p>openssl smime -pk7out -in msg.txt -out her-cert.pem</p>
<p>At that point, you can either <a title="How do I  get OpenSSL to recognize/verify a certificate?" href="#verify-new">integrate it into your OpenSSL infrastructure</a> or you can save it off somewhere for special use.</p>
<p>openssl smime -verify -in msg.txt -CAfile /path/to/her-cert.pem</p>
<p><strong>How do I encrypt a S/MIME message?</strong></p>
<p>Let’s say that someone sends you her public certificate and asks that you encrypt some message to her. You’ve saved her certificate as her-cert.pem. You’ve saved your reply as my-message.txt.</p>
<p>To get the default—though fairly weak—RC2-40 encryption, you just tell <strong>openssl</strong> where the message and the certificate are located.</p>
<p>openssl smime her-cert.pem -encrypt -in my-message.txt</p>
<p>If you’re pretty sure your remote correspondent has a robust SSL toolkit, you can specify a stronger encryption algorithm like triple DES:</p>
<p>openssl smime her-cert.pem -encrypt -des3 -in my-message.txt</p>
<p>By default, the encrypted message, including the mail headers, is sent to standard output. Use the -out option or your shell to redirect it to a file. Or, much trickier, pipe the output directly to <strong>sendmail</strong>.</p>
<p>openssl smime her-cert.pem \</p>
<p>-encrypt \</p>
<p>-des3 \</p>
<p>-in my-message.txt \</p>
<p>-from &#8216;Your Fullname &lt;you@youraddress.com&gt;&#8217; \</p>
<p>-to &#8216;Her Fullname &lt;her@heraddress.com&gt;&#8217; \</p>
<p>-subject &#8216;My encrypted reply&#8217; |\</p>
<p>sendmail her@heraddress.com</p>
<p><strong>How do I sign a S/MIME message?</strong></p>
<p>If you don’t need to encrypt the entire message, but you do want to sign it so that your recipient can be assured of the message’s integrity, the recipe is similar to that for <a title="How do I encrypt a S/MIME message?" href="#smime-encrypt">encryption</a>. The main difference is that you need to have your own key and certificate, since you can’t sign anything with the recipient’s cert.</p>
<p>openssl smime \</p>
<p>-sign \</p>
<p>-signer /path/to/your-cert.pem \</p>
<p>-in my-message.txt \</p>
<p>-from &#8216;Your Fullname &lt;you@youraddress.com&gt;&#8217; \</p>
<p>-to &#8216;Her Fullname &lt;her@heraddress.com&gt;&#8217; \</p>
<p>-subject &#8216;My signed reply&#8217; |\</p>
<p>sendmail her@heraddress.com</p>
<p><strong>For further reading</strong></p>
<p>Though it takes time to read them all and figure out how they relate to one another, the OpenSSL man pages are the best place to start: <a href="http://www.openssl.org/docs/apps/asn1parse.html">asn1parse(1)</a>, <a href="http://www.openssl.org/docs/apps/ca.html">ca(1)</a>, <a href="http://www.openssl.org/docs/apps/ciphers.html">ciphers(1)</a>, <a href="http://www.openssl.org/docs/apps/config.html">config(5)</a>, <a href="http://www.openssl.org/docs/apps/crl.html">crl(1)</a>, <a href="http://www.openssl.org/docs/apps/crl2pkcs7.html">crl2pkcs7(1)</a>, <a href="http://www.openssl.org/docs/apps/dgst.html">dgst(1)</a>, <a href="http://www.openssl.org/docs/apps/dhparam.html">dhparam(1)</a>, <a href="http://www.openssl.org/docs/apps/dsa.html">dsa(1)</a>, <a href="http://www.openssl.org/docs/apps/dsaparam.html">dsaparam(1)</a>, <a href="http://www.openssl.org/docs/apps/ec.html">ec(1)</a>, <a href="http://www.openssl.org/docs/apps/ecparam.html">ecparam(1)</a>, <a href="http://www.openssl.org/docs/apps/enc.html">enc(1)</a>, <a href="http://www.openssl.org/docs/apps/errstr.html">errstr(1)</a>, <a href="http://www.openssl.org/docs/apps/gendsa.html">gendsa(1)</a>, <a href="http://www.openssl.org/docs/apps/genpkey.html">genpkey(1)</a>, <a href="http://www.openssl.org/docs/apps/genrsa.html">genrsa(1)</a>, <a href="http://www.openssl.org/docs/apps/nseq.html">nseq(1)</a>, <a href="http://www.openssl.org/docs/apps/ocsp.html">ocsp(1)</a>, <a href="http://www.openssl.org/docs/apps/openssl.html">openssl(1)</a>, <a href="http://www.openssl.org/docs/apps/passwd.html">passwd(1)</a>, <a href="http://www.openssl.org/docs/apps/pkcs12.html">pkcs12(1)</a>, <a href="http://www.openssl.org/docs/apps/pkcs7.html">pkcs7(1)</a>, <a href="http://www.openssl.org/docs/apps/pkcs8.html">pkcs8(1)</a>, <a href="http://www.openssl.org/docs/apps/pkey.html">pkey(1)</a>, <a href="http://www.openssl.org/docs/apps/pkeyparam.html">pkeyparam(1)</a>, <a href="http://www.openssl.org/docs/apps/pkeyutl.html">pkeyutl(1)</a>, <a href="http://www.openssl.org/docs/apps/rand.html">rand(1)</a>, <a href="http://www.openssl.org/docs/apps/req.html">req(1)</a>, <a href="http://www.openssl.org/docs/apps/rsa.html">rsa(1)</a>, <a href="http://www.openssl.org/docs/apps/rsautl.html">rsautl(1)</a>, <a href="http://www.openssl.org/docs/apps/s_client.html">s_client(1)</a>, <a href="http://www.openssl.org/docs/apps/s_server.html">s_server(1)</a>, <a href="http://www.openssl.org/docs/apps/s_time.html">s_time(1)</a>, <a href="http://www.openssl.org/docs/apps/sess_id.html">sess_id(1)</a>, <a href="http://www.openssl.org/docs/apps/smime.html">smime(1)</a>, <a href="http://www.openssl.org/docs/apps/speed.html">speed(1)</a>, <a href="http://www.openssl.org/docs/apps/spkac.html">spkac(1)</a>, <a href="http://www.openssl.org/docs/apps/ts.html">ts(1)</a>, <a href="http://www.openssl.org/docs/apps/tsget.html">tsget(1)</a>, <a href="http://www.openssl.org/docs/apps/verify.html">verify(1)</a>, <a href="http://www.openssl.org/docs/apps/version.html">version(1)</a>, <a href="http://www.openssl.org/docs/apps/x509.html">x509(1)</a>, <a href="http://www.openssl.org/docs/apps/x509v3_config.html">x509v3_config(5)</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andrewkmitchell.com/blog/?feed=rss2&amp;p=293</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Talking About DNSSEC</title>
		<link>http://www.andrewkmitchell.com/blog/?p=282</link>
		<comments>http://www.andrewkmitchell.com/blog/?p=282#comments</comments>
		<pubDate>Sun, 09 May 2010 19:42:53 +0000</pubDate>
		<dc:creator>Andrew Mitchell</dc:creator>
				<category><![CDATA[InfoTech]]></category>

		<guid isPermaLink="false">http://www.andrewkmitchell.com/blog/?p=282</guid>
		<description><![CDATA[Goals and nature of DNSSEC Quoting from RFC 4033: The Domain Name System Security Extensions (DNSSEC) add data origin authentication and data integrity to the Domain Name System. The really interesting bit is that DNSSEC strives to do so using the current nearly unmodified DNS protocol, including caching, wildcards, forwarders etc. This is a massive [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Goals and nature of DNSSEC</strong></p>
<p lang="en">Quoting from RFC 4033:</p>
<blockquote lang="en"><p>The Domain Name System Security Extensions (DNSSEC) add data origin authentication and data integrity to the Domain Name System.</p></blockquote>
<p lang="en">The really interesting bit is that DNSSEC strives to do so using the current nearly unmodified DNS protocol, including caching, wildcards, forwarders etc. This is a massive challenge.</p>
<p lang="en">Compare an HTTP proxy, like squid, which makes no attempt to cache encrypted HTTPS data. When accessing HTTPS through a proxy, a direct connection is made through the proxy to the origen webserver.</p>
<p lang="en">DNSSEC in contrast attempts to preserve authentication and integrity even through caches. This is possible because DNSSEC does not guarantee exclusivity, it does not actually encrypt data.</p>
<p lang="en">But it is a massive challenge nonetheless.</p>
<p lang="en"><strong>First steps</strong></p>
<p lang="en">The first step is obvious: sign records, and make sure the signature and the record itself are both cached, and delivered with answers. This is just like what happens when you add a PGP signature to an email message.</p>
<p lang="en">To this goal, DNSSEC has invented the RRSIG record type, the Resource Record Signature, and it is indeed sent with answers, it looks like this:</p>
<pre>   a.z.w.example. 3600 IN MX  1 ai.example.
   a.z.w.example. 3600 RRSIG  MX 5 2 3600 20040509183619 (
                              20040409183619 38519 example.
                              OMK8rAZlepfzLWW75Dxd63jy2wswESzxDKG2
                              f9AMN1CytCd10cYISAxfAdvXSZ7xujKAtPbc
                              tvOQ2ofO7AZJ+d01EeeQTVBPq4/6KCWhqe2X
                              TjnkVLNvvhnc0u28aoSsG0+4InvkkOHknKxw
                              4kX18MMR34i8lC36SR5xBni8vHI= )</pre>
<p>This is the MX record of <code>a.z.w.example</code>, which points to &#8216;ai.example&#8217;, followed by its signature. You can see in the RRSIG that this is a signature of the MX record of a.z.w.example.com, that the signature algorithm is number &#8217;5&#8242;.</p>
<p lang="en">The next number we&#8217;ll address later &#8211; it has to do with wildcards. I think this example, lifted from the RFC, is wrong btw.</p>
<p lang="en">The next number is the original TTL, followed by the expiry of the signature (20040509183619), followed by the date this signature was generated (20040409183619).</p>
<p lang="en">The next number, 38519, is the &#8216;tag&#8217; if the key used to generate this signature. This is needed because there might be more keys available to sign records.</p>
<p lang="en">The next label, &#8216;example.&#8217; is the owner of the key used to sign this record, which should be equal to the name of the zone containing the record.</p>
<p lang="en">This all then is followed by the actual base64 encoded signature of all the above.</p>
<p lang="en"><strong>Where do we get the key?</strong></p>
<p lang="en">We&#8217;ve seen how we can sign a DNS record, but that leaves the important question of where to get, and more importantly, how to trust the key used to sign.</p>
<p lang="en">We&#8217;ll start out simple by describing the DNSKEY record, which as the name implies, contains a key, or in fact the public part of an asymmetrical key.</p>
<pre>   example.com. 86400 IN DNSKEY 256 3 5 ( AQPSKmynfzW4kyBv015MUG2DeIQ3
                                          Cbl+BBZH4b/0PY1kxkmvHjcZc8no
                                          kfzj31GajIQKY+5CptLr3buXA10h
                                          WqTkF7H6RfoRqXQeogmMHfpftf6z
                                          Mv1LyBUgia7za6ZEzOJBOztyvhjL
                                          742iU/TpPSEDhm2SNKLijfUppn1U
                                          aNvv4w==  )</pre>
<p>This specifies the <code>example.com</code> key. 256 means that his is a &#8216;Zone Key&#8217;, 3 is there to signify the protocol version and must always be 3. The next number, 5, is one we saw earlier, and it denotes the algorithms used. This is then followed by the actual key.</p>
<p lang="en">Note that the key tag, 38519 in the previous example, is hidden in the base-64 encoded key. There are tools available that add the tag as a comment to the zone.</p>
<p>This is all fine, but can we trust this key? We can query the key from the <code>example.com</code> zone, but as long as we get both the key and the signature over the same network, no real security is being added.</p>
<p lang="en">There are two interesting ways out of this dilemma. One is to retrieve the key using unspecified out-of-band means, like via a PGP signed message, or via ssh to a trusted host. That key can then become an &#8216;anchor&#8217; for you.</p>
<p lang="en">The second way is to have an existing &#8216;anchor&#8217; sign your keys. In an ideal world the root-zone would be the anchor, and everything would flow from there.</p>
<p lang="en"><strong>So, are we done now?</strong></p>
<p>Sadly, no. We also need to be able to <em>deny</em> the existence of a record securely. Now, this is a real problem. The way DNS denies the existence of a record is to send an empty authoritative answer packet, which is smart as far as it goes, but it leaves us with a problem: there is nothing to sign.</p>
<p lang="en">HTTPS does not have this problem as it does secure transport, not secure message, so it can simply send a &#8217;404&#8242; response over its secure channel.</p>
<p>As noted <a href="http://ds9a.nl/secure-dns" target="_top">the DNSSEC people are among the smartest in the world</a> so they&#8217;ve figured out a solution: the &#8216;Next Secure&#8217; NSEC record, known in its previous flawed incarnation as NXT.</p>
<p lang="en">Fundamentally it works like this: we have three records, called A, B and D. If a question comes in for C, we return the NSEC record that says that the &#8216;Next Secure&#8217; record after B is D, which proves there is no C.</p>
<p lang="en">And because we CAN sign the NSEC record, this is secure. The NSEC record in the case above would look like this:</p>
<pre>   b.example.com. 86400 IN NSEC d.example.com. (
                                   A RRSIG NSEC)</pre>
<p lang="en">Note the &#8216;A RRSIG NSEC&#8217;. This is where things get more complicated. This means that b.example.com has an A, RRSIG and an NSEC record.</p>
<p lang="en">Besides complete non-existence, there is also the case the record exists, but not the associated type. This happens for example when querying for an AAAA record for a host which has only an A record. Again, the normal DNS response is to send out an empty answer, which cannot be signed. In this case we again send out the NSEC record which lists which types DO exist, allowing the recipient to prove there is no AAAA record.</p>
<p lang="en"><strong>The trust chain</strong></p>
<p lang="en">We previously noted that we can designate keys as &#8216;anchors&#8217;, once we have verified their authenticity using non-DNS means. It is impracticable to do this for all millions of domains though. So we need a way to have trusted keys (anchors) sign further keys, in hopes of one day having a signed root.</p>
<p lang="en">The proper place for such a signature is in the parent zone, of course, and for this purpose, DNSSEC has invented the Delegation Signer (DS) record. The size of the ccTLD zones is already worrisomely large, so the DS record only contains a hash of the signed zone.</p>
<p lang="en">The record looks like this:</p>
<pre>   dskey.example.com. 86400 IN DS 60485 5 1 ( 2BB183AF5F22588179A53B0A
                                              98631FAD1A292118 )</pre>
<p>This is the delegation signature, which would typically live in the .com zone, which corresponds to this DNSKEY in the <code>example.com</code> zone below:</p>
<pre>   dskey.example.com. 86400 IN DNSKEY 256 3 5 ( AQOeiiR0GOMYkDshWoSKz9Xz
                                             fwJr1AYtsmx3TGkJaNXVbfi/
                                             2pHm822aJ5iI9BMzNXxeYCmZ
                                             DRD99WYwYqUSdjMmmAphXdvx
                                             egXd/M5+X7OrzKBaMbCVdFLU
                                             Uh6DhweJBjEVv5f2wwjM9Xzc
                                             nOf+EPbtG9DMBmADjFDc2w/r
                                             ljwvFw==
                                             ) ;  key id = 60485</pre>
<p lang="en">Note how the key id was added as a comment here. Also note that the above makes no sense unless the DS record itself is signed with an RRSIG record with a key we do trust.</p>
<p>So if we have configured the .COM key as an anchor, we would also trust the DS record for <code>dskey.example.com</code> and henceforth also the signatures in the <code>example.com</code> zone, and so forth.</p>
<p lang="en">It is all very pretty.</p>
<p lang="en"><strong>This sounds reasonable, are we done?</strong></p>
<p lang="en">Sadly, no. Encryption keys need to be changed every once in a while, especially if they are short, as they tend to be in DNS.</p>
<p lang="en">Using the scheme above, any change in key would have to be agreed with the registry (parent zone), which takes time to to process. Furthermore, the registry is probably not willing to change the key around a lot as they have better things to do.</p>
<p lang="en">So we invoke the magic incantation &#8216;there is no problem in computing that cannot be solved by another layer of indirection&#8217;, and add another layer of indirection.</p>
<p lang="en">This is called the &#8216;Key Signing Key&#8217; (KSK), and that is the one that is &#8216;blessed&#8217; by the parent zone. We then use the KSK to sign our Zone Signing Keys (ZKSs), which are used to sign our actual records. Note the plural, more about this later.</p>
<p lang="en">So the actual setup has, in addition to the DS and DNSKEY records shown above an additional layer in the form of another DNSKEY, which is signed with an RRSET based on the DNSKEY we already had. The key signed by our parent has the &#8216;Secure Entry Point&#8217; flag set to designate it as the key to start with.</p>
<p lang="en"><strong>Key rollovers</strong></p>
<p lang="en">As stated above, there is a real need to change keys every once in a while, both regularly or in a hurry if they keys have been compromised.</p>
<p lang="en">During a change of keys, a situation might exist were caches still carry the old signatures, but resolvers can&#8217;t find the associated keys to authenticate them with.</p>
<p lang="en">The solution for this is to make sure multiple keys and signatures are around, and only change one set at a time.</p>
<p lang="en">This needs to be done very carefully though, as any mistake will lead to &#8216;bogus&#8217; data in DNSSEC parlance, which means that your records have signatures, but they can&#8217;t be verified. Security-aware clients will now drop your data.</p>
<p>There are two proposed modes for ZSK rollover, which are detailed in <a href="http://www.ripe.net/disi/dnssec_howto/#bib_I-D.ietf-dnsop-dnssec-operational-practices" target="_top">DNSSEC Operational Practices</a> by Olaf Kolkman: &#8216;Pre-publish key set Rollover&#8217; and &#8216;Double Signature Zone-signing Key Rollover&#8217;, and yet more modes for KSK rollover.</p>
<p lang="en"><strong>Wildcards</strong></p>
<p>Previously, DNSSEC took the labour-saving decision to not support wildcards. Wildcards in DNS <em>synthesise</em> records, which means that there is an infinite amount of signing to do in theory.</p>
<p lang="en">But then the DNSSEC people had a smart idea: make an RRSET state how many labels it covers. This means that &#8216;www.example.com&#8217;, synthesised by &#8216;*.example.com&#8217; is followed by a synthesised RRSET signing &#8216;www.example.com&#8217;. However, this signature is then invalid, since it was signed as &#8216;*.example.com&#8217;. Now what?</p>
<p lang="en">Remember the unexplained number in the RRSET record? That is the amount of labels the RRSET covers, which would in this case be 2. The receiving nameserver can derive from this that the record &#8216;www.example.com&#8217; was synthesised from &#8216;*.example.com&#8217;, and check the signature accordingly.</p>
<p lang="en"><strong>The problem of privacy, zone enumeration: NSEC3</strong></p>
<p style="text-align: center;" lang="en"><span style="text-decoration: underline;"><strong><span style="color: #ff0000;">Warning:</span> This section has not been proofread and may very well be incorrect! &#8211; However, findings are based on actual lab test results.<br />
</strong></span></p>
<p lang="en"><span style="text-decoration: underline;"><strong></strong></span>This is sort of ironic. DNSSEC was meant to improve DNS Security, but over time it became clear it simultaneously lowered it in other ways.</p>
<p lang="en">Many DNS operators do not allow their zones to be transferred, as the full contents of a zone are considered private, and often provide hackers with clues on how to hack a network.</p>
<p lang="en">Recall how DNSSEC employs the NSEC record to securely deny existence of a zone, by signifying that no records exist between two records that do. So, the NSEC &#8220;covering&#8221; the domains &#8216;a.example.com&#8217; and &#8216;c.example.com&#8217; proves there is no &#8216;b.example.com&#8217; record.</p>
<p lang="en">It turns out that if we start out from one NSEC record, we can find the next, and so on, therefore emulating a full zone transfer! By hopping from the &#8220;lower&#8221; part of the cover to the upper, we find a chain of NSEC records that lists the entire zone contents.</p>
<p lang="en">The upshot of this is that there is no longer the ability to restrict full access to a zone, as malicious users can simply hop all the NSEC records, and get the same thing.</p>
<p lang="en">This has meant that several large domain registries have stated that they cannot deploy DNSSEC in its present form, for example because of strict implementations of the EU privacy directive.</p>
<p lang="en">As stated before, the DNSSEC authors are pretty smart and know a lot about cryptography, so they&#8217;ve found a solution to this problem: NSEC3</p>
<p lang="en">In principle, this works just like regular NSEC, except that it operates on strong cryptographic hashes of domain names. By sending an NSEC3 response that covers the *hashes* that come before and after the hash of the record a query was sent for, the same proof of non-existence is available, without making it possible to enumerate the entire zone. One could enumerate the hashes, but as these are strong, they can&#8217;t be converted back into domain names.</p>
<p lang="en">This is pretty smart, but also means the authoritative nameserver should keep the list of NSEC3 records in correct sorted order available. Things quickly get mighty complicated when wildcards are involved as well.</p>
<p lang="en">Additionally, the authoritative server has to do a lot more work, as it has to hash each question it can&#8217;t immediately find an answer for to see which NSEC3 it should emit to prove non-existence.</p>
<p lang="en"><strong>Yes we are done</strong></p>
<p>Ok, if you&#8217;ve made it through the above, I suggest reading the <a href="http://www.nlnetlabs.nl/dnssec_howto/" target="_top">DNSSEC HOWTO</a> by Olaf Kolkman, which should now make sense.</p>
<p lang="en"><strong>So is it all worth it?</strong></p>
<p lang="en">The big worries to me are:</p>
<ul>
<li>
<p lang="en">Huge answer records, making DNSSEC act as a 	packet-amplifier.</p>
</li>
<li>
<p lang="en">Complexity of key-rollover procedures. Automated 	key-rollover appears to be covered by patents.</p>
</li>
<li>
<p lang="en">Complexity of recursion process. Serving DNSSEC 	records is complex enough as it is, with NSEC taking the 	complexity-crown from the LOC record, but the recursing process is 	truly very hard.</p>
</li>
<li>
<p lang="en">The biggest worry: DNSSEC complexity is sure to 	cause secure unavailability of domain names, leading DNSSEC users to 	either turn it off or ignore its warnings.</p>
</li>
</ul>
<p lang="en">Many of the worries above could be addressed with good toolsets and client library support, but the last and first factors will always remain. DNS often operates under the hood, many applications have no way to communicate with their end users.</p>
<p lang="en">For example, how would a MTA react to &#8216;Bogus&#8217; data? Hold the email? Bounce it back to the original author asking for instructions? Send it anyhow? Policy is hard to set.</p>
<p>So, you decide! <img src='http://www.andrewkmitchell.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.andrewkmitchell.com/blog/?feed=rss2&amp;p=282</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Talking About DNS Security</title>
		<link>http://www.andrewkmitchell.com/blog/?p=278</link>
		<comments>http://www.andrewkmitchell.com/blog/?p=278#comments</comments>
		<pubDate>Sun, 09 May 2010 19:19:53 +0000</pubDate>
		<dc:creator>Andrew Mitchell</dc:creator>
				<category><![CDATA[InfoTech]]></category>

		<guid isPermaLink="false">http://www.andrewkmitchell.com/blog/?p=278</guid>
		<description><![CDATA[What Is DNS? DNS represents the abbreviation for a Domain Name Server which is used to interpret domain names such as www.yourdomain.com into an Internet Protocol (IP) address. The Internet Protocol address consists of numbers such as 107.60.132.4 that give the domain a unique identification. An IP address is one-of-a-kind and unique so it can [...]]]></description>
			<content:encoded><![CDATA[<p><strong>What Is DNS?</strong></p>
<p>DNS represents the abbreviation for a Domain Name Server which is used to interpret domain names such as www.yourdomain.com into an Internet Protocol (IP) address. The Internet Protocol address consists of numbers such as 107.60.132.4 that give the domain a unique identification. An IP address is one-of-a-kind and unique so it can be used to trace Internet activity back to the PC user as well as identify the exact location of a website. Domain names are used to identify websites because they are easier to remember than a series of numbers that make up an IP address.</p>
<p><strong>How DNS Hijacking Works.</strong></p>
<p>DNS hijacking is used by hackers with malicious intent who redirect or &#8220;hijack&#8221; the DNS addresses to bogus DNS servers for the purpose of injecting malware into your PC, promoting phishing scams, advertising on high traffic websites, and any other related form of criminal activity.</p>
<p>Once the DNS address is hijacked to a bogus DNS server, it translates the legitimate IP address or DNS name into the IP addresses of malicious websites. DNS hijacking can occur with any website large or small and turn those websites into malicious websites without the knowledge of the Web surfer.</p>
<p>Since the website owners depend upon legitimate DNS server that are issued by their Internet Service Providers (ISP), DNS hijackers use malware in the form of a Trojan to exchange the legitimate DNS server assignment by the ISP with a manual DNS server assignment from a bogus DNS server.</p>
<p>When Web surfers visit the reputable websites with legitimate domain names, they are automatically hijacked to a malicious website that is disguised as the legitimate one. The switch from the legitimate DNS server to the bogus DNS server goes unnoticed by both the surfer and the legitimate website owner. This opens up the malicious website to perform any criminal act that the hacker wishes because the user thinks they are on the real website.<br />
Other Dangers of DNS Hijacking</p>
<p>Another danger of DNS hijacking occurs when the surfer is unaware that they are on a bogus DNS server. If the user continues to surf on the bogus DNS server and they search for other websites, they most likely will end up visiting more malicious sites.</p>
<p>For example, let&#8217;s say that they search for a website and the domain is non-existent due to a misspelling typed during the search. Generally a legitimate DNS server will display an error message or provide site suggestions that relate to what the user typed into the search. With a bogus DNS server, instead of the error message they will be directed to yet another malicious website because the server has been created for the purpose of performing criminal acts. Regardless of what is typed in, it malicious websites replace the error message.</p>
<p>DNS hijacking also promotes click fraud with such programs as Google Adsense. Since there are numerous DNS servers that are bogus, they form a network of websites which results in a lot of traffic. When you get a lot of traffic you of course get a lot of people clicking which results in click fraud. The hackers can rack up a lot of money with click throughs from programs like Google Adsense who pay a commission for each click.</p>
<p><strong>Preventing DNS Poisoning in Linux</strong></p>
<p>If you don’t think the recent discovery of the DNS cache-poisoning flaw is bad news and needs to be addressed as soon as possible, let me repeat what Paul Mockapetris, DNS’ (Domain Name System) inventor, had to say about this security hole: Patch your DNS servers right now.</p>
<p>CERT can tell you about the technical details of DNS cache-poisoning, here’s what an attack on a DNS server can mean to you according to Dan Kaminsky, a researcher at security services firm IOActive: The vulnerability could allow attackers to redirect Web traffic and e-mails to systems under their control.</p>
<p>In other words, you click on your bookmark for Google and you end up at a site that looks like Google but is loaded down with malware. Or, you go to what looks like your bank site, the URL is the right one for your bank, but when you enter in your account ID you’ve just given it to a rip off artist.</p>
<p>With violated DNS servers you will be unable to trust the Internet. None of your net-based programs — e-mail, Web browser, media-player, whatever – would be trustworthy.</p>
<p>The only solution is to update your DNS servers and to do it now. Almost all the major operating system vendors have already released patches for the problem.</p>
<p>For Linux servers in specific, that means you need to upgrade BIND. If you’re still using BIND 8, or, God-forbid, any earlier versions, since these are hopelessly outdated, you should upgrade to BIND 9.</p>
<p>BIND 9 is years old but some ISPs and companies are still using it. If you’re using an older Linux distribution for DNS, you may need to update BIND by hand from the source code.</p>
<p><strong>More About The Alternatives and Best Practices: What are the preventative measures?</strong></p>
<p>When searching for ways to increase DNS awareness we can look to Dr. Stephen Crocker, Internet pioneer and Chair of the ICANN committee. He highlighted in a recent Computer World interview with Patrick Thibodeau, that good practice involves more diversity and a need for a wider set of implementations of BIND. His suggestion may be more important than ever considering that just last week security researchers discovered new vulnerabilities in BIND 4 and 8. In particular, they found that BIND 8 is vulnerable to two separate DDoS attacks. The first involves the way BIND 8 servers handle invalid DNS lookup requests and the other occurs when an attacker causes a BIND 8 server to cache SIG resource record elements with invalid expirations. &#8220;We know that many people are running obsolete versions of BIND, and the older versions have critical bugs,&#8221; said Crocker. Clearly, keeping up to date with the most recent version of BIND is a fundamental best practice that administrators should follow. But is the most recent version enough?</p>
<p>For example, the version 9.2.2 release of BIND, is indeed a major rewrite of many aspects of the underlying BIND architecture. With regards to DNS security issues, BIND 9.2.2 offers increased vigilance by acting as an authoritative server for DNSSEC secured zones. In brief, the IETF&#8217;s DNSSEC protocol is designed to stop DNS &#8220;hijacking&#8221;, as is the case when a hacker tampers with a site&#8217;s DNS information and misdirects users to an alternate server. Implications of such &#8220;hijacking&#8221; would involve hackers sending users to a &#8216;bogus&#8217; website posing as the company&#8217;s legitimate site and possibly extracting confidential information such as credit card numbers or personal identification. Mark Kosters, VeriSign&#8217;s VP of Research and DNSSEC expert says, &#8220;We are glad to see that Incognito is integrating DNSSEC within DNS Commander. We see the deployment of DNSSEC as one of the most critically important enhancements on core security on the Internet in these times of increased hacker activity and consequent security awareness.&#8221;</p>
<p>Kosters does make a valid point; however, it is important to understand that DNSSEC may be years away from being fully implemented. So, in the short run, the fundamental shortcomings of BIND still remain. We are then left with the burning question, &#8220;What do we do in the meantime?&#8221;</p>
<p>Interactive Week&#8217;s Charles Babcock points out that, &#8220;With few exceptions, Web sites have such a server in front of them running BIND and directing traffic. The DNS server is typically outside the corporate firewall with minimal protection and, thus, is a frequent target for hackers since 80 to 90 percent of the copies of BIND in use contain one of a dozen known vulnerabilities.&#8221; For this reason, it is important for network administrators to be aware of proprietary BIND alternatives.</p>
<p>A lot of times, administrators use BIND because it is free and they&#8217;re either not concerned about reliability or not aware of the risks of open source software. With businesses becoming more dependent on both their internal network and Internet presence to operate efficiently, support customers, and drive new revenue, reliability and quality of service is critical. For this reason, enterprises and ISPs need to consider implementing proprietary DNS software that is available only as compiled code, which makes it more difficult to discover vulnerabilities as compared to open source code. Because DNS is based upon well-established standards, core DNS functionality is not an issue with proprietary solutions as long as they meet these standards. Solutions, such as Incognito&#8217;s DNS Commander, can be highly-extensible, robust, and cost-effective, domain management solution that are secure BIND/MSDNS alternatives. Also, proprietary solutions tend to have performance improvements, feature enhancements, and are easier to deploy and use compared to BIND. After all, these vendors need to have better product in order to maintain a successful business. Often, simply introducing a proprietary secondary DNS server as a backup to the network can greatly increase DNS resilience.</p>
<p>As an example, the UK Ministry of Defense recently communicated their need for a secure DNS solution. General Dynamics, the contractor responsible for the UK MOD&#8217;s BOWMAN project, evaluated several prominent DNS solutions, including BIND, as part of their selection process. It was concluded that, &#8220;Given the needs of the BOWMAN program, the proprietary DNS alternative that Incognito develops had the best fit in terms of features, reliability, and security,&#8221; said Andrew Greenslade, BOWMAN Product Manager at General Dynamics Canada. &#8220;Integrating a DNS alternative into the BOWMAN program will fulfill General Dynamics&#8217; mandate to provide a secure, reliable, and robust DDNS solution.&#8221; With a deployment that includes thousands of administrators directly managing DNS in a possibly hostile combat environment, an easy-to-use management interface with superior redundancy and diagnostic tools are critical requirements that alternative solutions are offering.</p>
<p>Another DNS alternative includes taking advantage of a two-pronged solution. Radware, a Mahwah, N.J.- based company, points out that their Web Server Director (WSD) solution provides comprehensive IP application security and optimization. To illustrate, a combined solution would have a Radware WSD deployed between the Internet and several DNS Commander servers. The WSD would be configured with a Virtual IP address and root server databases are updated to point to this Virtual IP address (VIP) and other VIPs for redundancy. The WSD hides the IP addresses of the DNS Commander servers behind it, providing security from attacks, and WSD also load balances DNS requests across these servers for optimized DNS operation.</p>
<p>Aside from using a two-tiered virtual IP solution that works to prevent DDoS attacks from reaching the DNS server, many DNS experts assert that mirroring the root server data can also be a simple and cost effective way to secure that information. The Washington Post recently reported, &#8220;that many of the nation&#8217;s top e-commerce companies mirror information contained in the root servers to safeguard their operations from DDOS attacks.&#8221;</p>
<p>This is a valid point because firms that mirror root data will inherently be more secure because they will rarely need to query the root servers. In the case of a DDoS attack where the root servers are affected, a company with a root mirror will be able to continue daily operations and will likely not feel the effects of a DDoS.</p>
<p>Allan Liska, Security Engineer for Symantec&#8217;s managed security division, highlights that mirroring the root data is not overly complicated in BIND, but can still be very cumbersome and time consuming. &#8220;If BIND is running on a server with other services there could be a problem with not having enough space to implement it. On the other hand, if the company has a dedicated DNS appliance with more than enough storage space to hold the 30+ million records hosted on the root servers, it is a much simpler task.&#8221;</p>
<p><strong>Looking Forward</strong></p>
<p>Despite the current market&#8217;s lack of enthusiasm with technology, the greatest opportunities lie ahead solving the most pressing issues of the Internet &#8211; security, access, and identity. The most recent DDoS attacks have revealed a great deal about the vulnerabilities of DNS, and although experts assert that the next hackers attempting an attack will use this information for their advantage, it is also necessary to utilize such revelations for improving DNS security. Armed with such knowledge and increased public pressure, the stimulus to incorporate best practices in DNS management should be a number one priority on everyone&#8217;s security agendas. The warning signs are everywhere.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andrewkmitchell.com/blog/?feed=rss2&amp;p=278</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The basics of RSA &amp; DSA Keys</title>
		<link>http://www.andrewkmitchell.com/blog/?p=272</link>
		<comments>http://www.andrewkmitchell.com/blog/?p=272#comments</comments>
		<pubDate>Sun, 28 Mar 2010 19:42:25 +0000</pubDate>
		<dc:creator>Andrew Mitchell</dc:creator>
				<category><![CDATA[InfoTech]]></category>

		<guid isPermaLink="false">http://www.andrewkmitchell.com/blog/?p=272</guid>
		<description><![CDATA[Many of us use the excellent OpenSSH (see Resources later in this course) as a secure, encrypted replacement for the venerable telnet and rsh commands. One of OpenSSH&#8217;s more intriguing features is its ability to authenticate users using the RSA and DSA authentication protocols, which are based on a pair of complementary numerical keys. As [...]]]></description>
			<content:encoded><![CDATA[<p>Many of us use the excellent OpenSSH (see Resources  later in this course) as a secure, encrypted replacement for the venerable telnet and rsh commands. One of OpenSSH&#8217;s more intriguing features is its ability to authenticate users using the RSA and DSA authentication protocols, which are based on a pair of complementary numerical keys. As one of its main appeals, RSA and DSA authentication promise the capability of establishing connections to remote systems without supplying a password. While this is appealing, new OpenSSH users often configure RSA/DSA the quick and dirty way, resulting in passwordless logins, but opening up a big security hole in the process.</p>
<p><strong>What is RSA/DSA authentication?</strong></p>
<p>SSH, specifically OpenSSH (a completely free implementation of SSH), is an incredible tool. Like telnet or rsh, the ssh client can be used to log in to a remote machine. All that&#8217;s required is for this remote machine to be running sshd, the ssh server process. However, unlike telnet, the ssh protocol is very secure. It uses special algorithms to encrypt the data stream, ensure data stream integrity and even perform authentication in a safe and secure way.</p>
<p>However, while ssh is really great, there is a certain component of ssh functionality that is often ignored, dangerously misused, or simply misunderstood. This component is OpenSSH&#8217;s RSA/DSA key authentication system, an alternative to the standard secure password authentication system that OpenSSH uses by default.</p>
<p>OpenSSH&#8217;s RSA and DSA authentication protocols are based on a pair of specially generated cryptographic keys, called the private key and the public key. The advantage of using these key-based authentication systems is that in many cases, it&#8217;s possible to establish secure connections without having to manually type in a password.</p>
<p>While the key-based authentication protocols are relatively secure, problems arise when users take certain shortcuts in the name of convenience, without fully understanding their security implications. In this course, we&#8217;ll take a good look at how to correctly use RSA and DSA authentication protocols without exposing ourselves to any unnecessary security risks. In my next course, I&#8217;ll show you how to use ssh-agent to cache decrypted private keys, and introduce keychain, an ssh-agent front-end that offers a number of convenience advantages without sacrificing security. If you&#8217;ve always wanted to get the hang of the more advanced authentication features of OpenSSH, then read on.</p>
<p><strong>How RSA/DSA keys work</strong></p>
<p>Here&#8217;s a quick general overview of how RSA/DSA keys work. Let&#8217;s start with a hypothetical scenario where we&#8217;d like to use RSA authentication to allow a local Linux workstation (named localbox) to open a remote shell on remotebox, a machine at our ISP. Right now, when we try to connect to remotebox using the ssh client, we get the following prompt:</p>
<p>% ssh drobbins@remotebox</p>
<p>drobbins@remotebox&#8217;s password:</p>
<p>Here we see an example of the ssh default way of handling authentication. Namely, it asks for the password of the drobbins account on remotebox. If we type in our password for remotebox, ssh uses its secure password authentication protocol, transmitting our password over to remotebox for verification. However, unlike what telnet does, here our password is encrypted so that it can not be intercepted by anyone sniffing our data connection. Once remotebox authenticates our supplied password against its password database, if successful, we&#8217;re allowed to log on and are greeted with a remotebox shell prompt. While the ssh default authentication method is quite secure, RSA and DSA authentication open up some new possibilities.</p>
<p>However, unlike the ssh secure password authentication, RSA authentication requires some initial configuration. We need to perform these initial configuration steps only once. After that, RSA authentication between localbox and remotebox will be totally painless. To set up RSA authentication, we first need to generate a pair of keys, one private and one public. These two keys have some very interesting properties. The public key can be used to encrypt a message, and only the holder of the private key can decrypt it. The public key can only be used for encryption, and the private key can only be used for decryption of a message encoded by the matching public key. The RSA (and DSA) authentication protocols use the special properties of key pairs to perform secure authentication, without needing to transmit any confidential information over the network.</p>
<p>To get RSA or DSA authentication working, we perform a single one-time configuration step. We copy our public key over to remotebox. The public key is called &#8220;public&#8221; for a reason. Since it can only be used to encrypt messages for us, we don&#8217;t need to be too concerned about it falling into the wrong hands. Once our public key has been copied over to remotebox and placed in a special file (~/.ssh/authorized_keys) so that remotebox&#8217;s sshd can locate it, we&#8217;re ready to use RSA authentication to log onto remotebox.</p>
<p>To do this, we simply type ssh drobbins@remotebox at localbox&#8217;s console, as we always have. However, this time, ssh lets remotebox&#8217;s sshd know that it would like to use the RSA authentication protocol. What happens next is rather interesting. Remotebox&#8217;s sshd generates a random number, and encrypts it using our public key that we copied over earlier. Then, it sends this encrypted random number back to the ssh running on localbox. In turn, our ssh uses our private key to decrypt this random number, and then sends it back to remotebox, saying in effect &#8220;See, I really do hold the matching private key; I was able to successfully decrypt your message!&#8221; Finally, sshd concludes that we should be allowed to log in, since we hold a matching private key. Thus, the fact that we hold a matching private key grants us access to remotebox.</p>
<p><strong>Two observations</strong></p>
<p>There are two important observations about the RSA and DSA authentication. The first is that we really only need to generate one pair of keys. We can then copy our public key to the remote machines that we&#8217;d like to access and they will all happily authenticate against our single private key. In other words, we don&#8217;t need a key pair for every system we&#8217;d like to access. Just one pair will suffice.</p>
<p>The other observation is that our private key should not fall into the wrong hands. The private key is the one thing that grants us access to our remote systems, and anyone that possesses our private key is granted exactly the same privileges that we are. Just as we wouldn&#8217;t want strangers to have keys to our house, we should protect our private key from unauthorized use. In the world of bits and bytes, this means that no one should be able to read or copy our private key.</p>
<p>Of course, the ssh developers are aware of the private keys&#8217; importance, and have built a few safeguards into ssh and ssh-keygen so that our private key is not abused. First, ssh is configured to print out a big warning message if our key has file permissions that would allow it to be read by anyone but us. Secondly, when we create our public/private key pair using ssh-keygen, ssh-keygen will ask us to enter a passphrase. If we do, our private key will be encrypted using this passphrase, so that even if it is stolen, it will be useless to anyone who doesn&#8217;t happen to know the passphrase. Armed with that knowledge, let&#8217;s take a look at how to configure ssh to use the RSA and DSA authentication protocols.</p>
<p><strong>ssh-keygen up close</strong></p>
<p>The first step in setting up RSA authentication begins with generating a public/private key pair. RSA authentication is the original form of ssh key authentication, so RSA should work with any version of OpenSSH, although I recommend that you install the most recent version available, which was openssh-2.9_p2 at the time this course was written. Generate a pair of RSA keys as follows:</p>
<p>% ssh-keygen</p>
<p>Generating public/private rsa1 key pair.</p>
<p>Enter file in which to save the key (/home/drobbins/.ssh/identity): (hit enter)</p>
<p>Enter passphrase (empty for no passphrase): (enter a passphrase)</p>
<p>Enter same passphrase again: (enter it again)</p>
<p>Your identification has been saved in /home/drobbins/.ssh/identity.</p>
<p>Your public key has been saved in /home/drobbins/.ssh/identity.pub.</p>
<p>The key fingerprint is:</p>
<p>a4:e7:f2:39:a7:eb:fd:f8:39:f1:f1:7b:fe:48:a1:09 drobbins@localbox</p>
<p>When ssh-keygen asks for a default location for the key, we hit enter to accept the default of /home/drobbins/.ssh/identity. ssh-keygen will store the private key at the above path, and the public key will be stored right next to it, in a file called identity.pub.</p>
<p>Also note that ssh-keygen prompted us to enter a passphrase. When prompted, we entered a good passphrase (seven or more hard-to-predict characters). ssh-keygen then encrypted our private key (~/.ssh/identity) using this passphrase so that our private key will be useless to anyone who does not know it.</p>
<p><strong>The quick compromise</strong></p>
<p>When we specify a passphrase, it allows ssh-keygen to secure our private key against misuse, but it also creates a minor inconvenience. Now, every time we try to connect to our drobbins@remotebox account using ssh, ssh will prompt us to enter the passphrase so that it can decrypt our private key and use it for RSA authentication. Again, we won&#8217;t be typing in our password for the drobbins account on remotebox, we&#8217;ll be typing in the passphrase needed to locally decrypt our private key. Once our private key is decrypted, our ssh client will take care of the rest. While the mechanics of using our remote password and the RSA passphrase are completely different, in practice we&#8217;re still prompted to type a &#8220;secret phrase&#8221; into ssh.</p>
<p># ssh drobbins@remotebox</p>
<p>Enter passphrase for key &#8216;/home/drobbins/.ssh/identity&#8217;: (enter passphrase)</p>
<p>Last login: Thu Jun 28 20:28:47 2001 from localbox.gentoo.org</p>
<p>Welcome to remotebox!</p>
<p>%</p>
<p>Here&#8217;s where people are often mislead into a quick compromise. A lot of the time, people will create unencrypted private keys just so that they don&#8217;t need to type in a password. That way, they simply type in the ssh command, and they&#8217;re immediately authenticated via RSA (or DSA) and logged in.</p>
<p># ssh drobbins@remotebox</p>
<p>Last login: Thu Jun 28 20:28:47 2001 from localbox.gentoo.org</p>
<p>Welcome to remotebox!</p>
<p>%</p>
<p>However, while this is convenient, you shouldn&#8217;t use this approach without fully understanding its security impact. With an unencrypted private key, if anyone ever hacks into localbox, they&#8217;ll also get automatic access to remotebox and any other systems that have been configured with the public key.</p>
<p>I know what you&#8217;re thinking. Passwordless authentication, despite being a bit risky does seem really appealing. I totally agree. But there is a better way! Stick with me, and I&#8217;ll show you how to gain the benefits of passwordless authentication without compromising your private key security. I&#8217;ll show you how to masterfully use ssh-agent (the thing that makes secure passwordless authentication possible in the first place) in my next course. Now, let&#8217;s get ready to use ssh-agent by setting up RSA and DSA authentication. Here step-by-step directions.<br />
RSA key pair generation</p>
<p>To set up RSA authentication, we&#8217;ll need to perform the one-time step of generating a public/private key pair. We do this by typing:</p>
<p>% ssh-keygen</p>
<p>Accept the default key location when prompted (typically ~/.ssh/identity and ~/.ssh/identity.pub for the public key), and provide ssh-keygen with a secure passphrase. Once ssh-keygen completes, you&#8217;ll have a public key as well as a passphrase-encrypted private key.<br />
RSA public key install</p>
<p>Next, we&#8217;ll need to configure remote systems running sshd to use our public RSA key for authentication. Typically, this is done by copying the public key to the remote system as follows:</p>
<p>% scp ~/.ssh/identity.pub drobbins@remotebox:</p>
<p>Since RSA authentication isn&#8217;t fully set up yet, we&#8217;ll be prompted to enter our password on remotebox. Do so. Then, log in to remotebox and append the public key to the ~/.ssh/authorized_keys file like so:</p>
<p>% ssh drobbins@remotebox</p>
<p>drobbins@remotebox&#8217;s password: (enter password)</p>
<p>Last login: Thu Jun 28 20:28:47 2001 from localbox.gentoo.org</p>
<p>Welcome to remotebox!</p>
<p>% cat identity.pub &gt;&gt; ~/.ssh/authorized_keys</p>
<p>% exit</p>
<p>Now, with RSA authentication configured, we should be prompted to enter our RSA passphrase (rather than our password) when we try to connect to remotebox using ssh.</p>
<p>% ssh drobbins@remotebox</p>
<p>Enter passphrase for key &#8216;/home/drobbins/.ssh/identity&#8217;:</p>
<p>Hurray, RSA authentication configuration complete! If you weren&#8217;t prompted for a passphrase, here are a few things to try. First, try logging in by typing ssh -1 drobbins@remotebox. This will tell ssh to only use version 1 of the ssh protocol, and may be required if for some reason the remote system is defaulting to DSA authentication. If that doesn&#8217;t work, make sure that you don&#8217;t have a line that reads RSAAuthentication no in your /etc/ssh/ssh_config. If you do, comment it out by pre-pending it with a &#8220;#&#8221;. Otherwise, try contacting the remotebox system administrator and verifying that they have enabled RSA authentication on their end and have the appropriate settings in /etc/ssh/sshd_config.</p>
<p><strong>DSA key generation</strong></p>
<p>While RSA keys are used by version 1 of the ssh protocol, DSA keys are used for protocol level 2, an updated version of the ssh protocol. Any modern version of OpenSSH should be able to use both RSA and DSA keys. Generating DSA keys using OpenSSH&#8217;s ssh-keygen can be done similarly to RSA in the following manner:</p>
<p>% ssh-keygen -t dsa</p>
<p>Again, we&#8217;ll be prompted for a passphrase. Enter a secure one. We&#8217;ll also be prompted for a location to save our DSA keys. The default, normally ~/.ssh/id_dsa and ~/.ssh/id_dsa.pub, should be fine. After our one-time DSA key generation is complete, it&#8217;s time to install our DSA public key to remote systems.</p>
<p><strong>DSA public key install</strong></p>
<p>Again, DSA public key installation is almost identical to RSA. For DSA, we&#8217;ll want to copy our ~/.ssh/id_dsa.pub file to remotebox, and then append it to the ~/.ssh/authorized_keys2 on remotebox. Note that this file has a different name than the RSA authorized_keys file. Once configured, we should be able to log in to remotebox by typing in our DSA private key passphrase rather than typing in our actual remotebox password.<br />
Resources</p>
<p>* Be sure to visit the home of OpenSSH development.</p>
<p>* Take a look at the latest OpenSSH source tarballs and RPMs.</p>
<p>* Check out the OpenSSH FAQ.</p>
<p>* PuTTY is an excellent ssh client for Windows machines.</p>
<p>* You may find O&#8217;Reilly&#8217;s SSH, The Secure Shell: The Definitive Guide to be helpful.</p>
<p>* Read Addressing security issues in Linux on developerWorks for an overview of data encryption and many other security topics.</p>
<p>* Browse more Linux resources on developerWorks.</p>
<p>* Browse more Open source resources on developerWorks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andrewkmitchell.com/blog/?feed=rss2&amp;p=272</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Port knocking implementation through iptables</title>
		<link>http://www.andrewkmitchell.com/blog/?p=236</link>
		<comments>http://www.andrewkmitchell.com/blog/?p=236#comments</comments>
		<pubDate>Sun, 14 Mar 2010 22:38:55 +0000</pubDate>
		<dc:creator>Andrew Mitchell</dc:creator>
				<category><![CDATA[InfoTech]]></category>

		<guid isPermaLink="false">http://www.andrewkmitchell.com/blog/?p=236</guid>
		<description><![CDATA[Basic concept of port knocking In computer networking, port knocking is a method of externally opening ports on a firewall by generating a connection attempt on a set of prespecified closed ports. Once a correct sequence of connection attempts is received, the firewall rules are dynamically modified to allow the host which sent the connection [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Basic concept of port knocking</strong></p>
<p>In computer networking, port knocking is a method of externally opening ports on a firewall by generating a connection attempt on a set of prespecified closed ports. Once a correct sequence of connection attempts is received, the firewall rules are dynamically modified to allow the host which sent the connection attempts to connect over specific port(s). A variant called Single Packet Authentication exists, where only a single &#8216;knock&#8217; is needed, consisting of an encrypted packet.</p>
<p>The primary purpose of port knocking is to prevent an attacker from scanning a system for potentially exploitable services by doing a port scan, because unless the attacker sends the correct knock sequence, the protected ports will appear closed.</p>
<p>This is usually implemented by configuring a daemon to watch the firewall log file for said connection attempts then modify the firewall configuration accordingly. It can also be performed by a process examining packets at a higher level (using packet capture interfaces such as pcap), allowing the use of already &#8220;open&#8221; TCP ports to be used within the knock sequence.</p>
<p>The port &#8220;knock&#8221; itself is similar to a secret handshake and can consist of any number of TCP, UDP or even sometimes ICMP and other protocol packets to numbered ports on the destination machine. The complexity of the knock can be anything from a simple ordered list (e.g. TCP port 1000, TCP port 2000, UDP port 3000) to a complex time-dependent, source-IP-based and other-factor-based encrypted hash.</p>
<p>A portknock daemon on the firewall machine listens for packets on certain ports (either via the firewall log or by packet capture). The client user would carry an extra utility, which could be as simple as netcat or a modified ping program or as complicated as a full hash-generator, and use that before they attempted to connect to the machine in the usual way.</p>
<p>Most portknocks are stateful systems in that if the first part of the &#8220;knock&#8221; has been received successfully, an incorrect second part would not allow the remote user to continue and, indeed, would give the remote user no clue as to how far through the sequence they failed. Usually the only indication of failure is that, at the end of the knock sequence, the port expected to be open is not opened. No packets are sent to the remote user at any time.</p>
<p>While this technique for securing access to remote network daemons has not been widely adopted by the security community, it has been integrated in newer rootkits.</p>
<p><strong>Benefits of port knocking</strong></p>
<p>Consider that, if an external attacker did not know the port knock sequence, even the simplest of sequences would require a massive brute force effort in order to be discovered. A three-knock simple TCP sequence (e.g. port 1000, 2000, 3000) would require an attacker without prior knowledge of the sequence to test every combination of three ports in the range 1-65535, and then to scan each port in between to see if anything had opened. As a stateful system, the port would not open until after the correct three-digit sequence had been received in order, without other packets in between.</p>
<p>That equates to approximately 655354 packets in order to obtain and detect a single successful opening. That&#8217;s 18,445,618,199,572,250,625 or over 18.4 quintillion packets. On the average attempt it would take approximately 9.2 quintillion packets to successfully open a single, simple three-port TCP-only knock by brute force. This is made even more impractical when knock attempt-limiting is used to stop brute force attacks, longer and more complex sequences are used and cryptographic hashes are used as part of the knock.</p>
<p>When a port knock is successfully used to open a port, the firewall rules are generally only opened to the IP address that supplied the correct knock. This is similar to only allowing a certain IP whitelist to access a service but is also more dynamic. An authorised user situated anywhere in the world would be able to open the port they were interested in to only the IP that they are using without needing help from the server administrator. They would also be able to &#8220;close&#8221; the port once they had finished, or the system could be set up to use a timeout mechanism, to ensure that once they change IP&#8217;s, only the IP&#8217;s necessary are left able to contact the server.</p>
<p>Because of port knocking&#8217;s stateful behavior, several users from different source IP addresses can simultaneously be at varying levels of the port knock. Thus it is possible to have a genuine user with the correct knock let through the firewall even in the middle of a port attack from multiple IP&#8217;s (assuming the bandwidth of the firewall is not completely swamped). To all other IP addresses, the ports still appear closed and there is no indication that there are other users who have successfully opened ports and are using them.</p>
<p>Using cryptographic hashes inside the port knock sequence can mean that even sniffing the network traffic in and out of the source and target machines is ineffective against discovering the port knock sequence or using traffic replay attacks to repeat prior port knock sequences.</p>
<p>Even if somebody did manage to guess, steal or sniff the port knock and successfully use it to gain access to a port, the usual port security mechanisms are still in place, along with whatever service authentication was running on the opened ports.</p>
<p>The software required, either at the server or client end, is minimal and can in fact be implemented as simply as a shell script for the server or a Windows batch file and a standard Windows command line utility for the client. Overhead in terms of traffic, CPU and memory consumption is at an absolute minimum. Port knock daemons also tend to be simple enough that any sort of vulnerability is obvious and the code is very easily auditable.</p>
<p>With a portknock system in place on ports such as the SSH port, it can prevent brute force password attacks on logins. The SSH daemon need not even wake up as any attempt that is made without the correct portknock will bounce harmlessly off the TCP/IP stack rather than the SSH authentication. As far as any attacker is concerned, there is no daemon running on that port at all until they manage to correctly knock on the port.</p>
<p>The system is completely customisable and not limited to opening specific ports or, indeed, opening ports at all. Usually a knock sequence description is tied with an action, such as running a shell script, so when a specific sequence is detected by the port knock daemon, the relevant shell script is run. This could add firewall rules to open ports or do anything else that was possible in a shell script. Many portknocks can be used on a single machine to perform many different actions, such as opening or closing different ports.</p>
<p>Because the ports appear closed at all times until a user knowing the correct knock uses it, port knocking can help cut down not only on brute force password attacks and their associated log spam but also protocol vulnerability exploits. If an exploit was discovered that could compromise SSH daemons in their default configuration, having a port knock on that SSH port could mean that the SSH daemon may not be compromised in the time before it was updated. Only authorised users would have the knock and therefore only authorised users would be able to contact the SSH server in any way. Thus, random attempts on SSH servers by worms and viruses trying to exploit the vulnerability would not reach the vulnerable SSH server at all, giving the administrator a chance to update or patch the software. Although not a complete protection, port knocking would certainly be another level of defense against random attacks and, properly implemented, could even stop determined, targeted attacks.</p>
<p>Port knocking generally has some disregard in the security world, given that early implementations basically consisted of a number of ports that had to be hit in order. However, the best of modern portknock systems are much more complex, some using highly secure cryptographic hashes in order to defeat the most common attacks (such as packet sniffing and packet replay). Additionally, portknock systems can include blacklists, whitelists and dynamic attack responses as can any internet service, however, even the simplest of port knocks controls access to a system before attackers are able to hit a service that allocates memory, CPU time or other significant resources and also acts as a barrier against brute-force attempts, automated vulnerability exploits etc.</p>
<p>Port knocking does not generally lower the security of a system overall. Indeed, it provides another layer of security for minimal overhead. In the worst case scenario, however, the port knocking software can introduce a new security problem or lower security due to risk compensation.</p>
<p><strong>Disadvantages of port knocking</strong></p>
<p>If for some reason or other the port knocking daemon dies, you are left with a system you cannot connect with. This is also known as a single point of failure. However, to help mitigate this problem, modern port knocking implementations include a process monitoring daemon that will restart the port knocking daemon if it dies. This issue doesn&#8217;t affect xtables-addons&#8217; in-kernel libxt_pknock module.</p>
<p>Another problem is that an attacker can lock out any known IP addresses e.g. the administrator by sending packages with spoofed IP address to random ports. For server to server communication this could be especially harmful because IPs are well known and sometimes can&#8217;t be changed easily. This could be prevented by using cryptographic hashes.</p>
<p>Also, an important consideration is that due to the way TCP/IP routes packets, there is a very real probability that individual packet components of the individual &#8216;knocks&#8217; arrive out of sequence, or that some are dropped entirely. This means that legitimate requests have the potential to be &#8220;incorrect&#8221; in the eyes of the server, and that this must be dealt with by the client (a resend etc.)</p>
<p><strong>Practical uses of port knocking</strong></p>
<p>Now that we know what port knocking is, Let&#8217;s discuss it in a practical use evironment&#8230;</p>
<p>Port knocking as technique is used for a long time now and many implementations are already out there. Although, for my specific use none of them seemed to be appropriate.</p>
<p>Requirements (or why no other port knocking solution worked for me):<br />
Knocking and service must share the same port (for example &#8211; let’s suppose you have a computer behind some router or firewall and have no access to this device; all you do have is one forwarded port to your computer &#8211; let’s say for ssh as most commonly used for system administration). I guess you can see now where the problem is with other implementations of port knocking: lack of ports that can be used for knocking.</p>
<p>Also, as I prefer a portable solution that is deployable anywhere (without any additional software clients) I want to make it possible so that it can be used with simple tools available on any system.</p>
<p>I’m trying to make this as simple as possible so all I’ll use is iptables. Why? Imagine a following scenario: something gets broken on a port knocking script/program/server and it’s not listening to knocks anymore. There comes a need for a visit to the computer to fix this directly as our ssh connection don’t let us do it remotely anymore. If iptables gets broken there is no firewall anyway so you’ll have a completely different set of problems, at least connecting to fix it wouldn’t be one of them.</p>
<p>One thing that is needed beside plain iptables is their recent module &#8211; either compiled in or as separate module. Few words about recent module parameters used here:</p>
<p><code>–name</code> [name] defines a name of the list<br />
<code>–set</code> puts the source IP matched in a list defined by –name (if name is not defined all matches go in some default list)<br />
<code>–rcheck</code> does a rulecheck on a IPs from list<br />
<code>–seconds [seconds]</code> is used to filter out the list for IPs seen within the indicated number of seconds<br />
<code>–hitcount [hits]</code> filters out the list for minimum number of packets from some IP, can be combined with seconds to make the filtering more accurate</p>
<p>OK, so we’ll start with a standard firewall passing new ssh connections (I’ll use the iptables-save format as it’s easier to tweak the firewall and set comments though you can make this also with usual iptables commands) &#8211; port 22 is passed thru without any additional checks:</p>
<p><code># pass established connections<br />
-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT<br />
# pass new ssh connection<br />
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT</code></p>
<p>For starters we’ll need to start listening on this port:</p>
<p><code># pass established connections<br />
-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT<br />
# listen to new connections on ssh port<br />
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -m recent –set –name sshknock -j LOG “knock knock: ”<br />
# pass new ssh connection<br />
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT</code></p>
<p>Now, after initiating a ssh connection we have a entry in <code>/proc/net/ip_recent/sshknock</code> list showing us the address and main parameters of the packet. As connection was initiated one rule later and all later packets were are picked up by ESTABLISHED state rule that’s all we’re going to get for now.</p>
<p>So let’s start filling in this list with new packets by dropping them instead of accepting and also enabling the port knocking mechanism:</p>
<p><code># pass established connections<br />
-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT<br />
# if at least 2 new packets inside 5 seconds open ssh port<br />
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -m –rcheck –name sshknock –hitcount 2 –seconds 5 -j ACCEPT<br />
# listen to new connections on ssh port<br />
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -m recent –set –name sshknock -j LOG “knock knock: ”<br />
# pass new ssh connection<br />
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j DROP</code></p>
<p>We might stop at this as we have achieved our goal &#8211; 2 packet knock in a short amount of time is needed to open the port which will go under a radar for most port sniffers marking it as filtered port (of course, tweaking with more knocks and probably larger timespan is recommended to be more sure).<br />
But, as we have a very well known port here which is targeted quite often let’s add an additional twist to this. By simply bombing the ssh port it will open without any defense at all.</p>
<p><code># pass established connections<br />
-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT<br />
# if more then 2 new packets inside 3 seconds arrive drop<br />
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -m –rcheck –name sshknock –hitcount 2 –seconds 3 -j ACCEPT<br />
# if at least 2 new packets inside 5 seconds open ssh port<br />
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -m –rcheck –name sshknock –hitcount 2 –seconds 5 -j ACCEPT<br />
# listen to new connections on ssh port<br />
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -m recent –set –name sshknock -j LOG “knock knock: ”<br />
# pass new ssh connection<br />
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j DROP</code></p>
<p>Another line to firewall is added &#8211; but what does it actually do?<br />
If someone attempts to flood the port with packets it will refuse to open; and instead of only having number and time of knocks defined also rate at which these knocks must be employed is set &#8211; sending them at too high or too low frequency has no effect on opening the port &#8211; exactly 2 knocks is needed for 3rd packet to get to ssh server inside 3 &#8211; 5 seconds of the first knock.</p>
<p>So there it is, port knocking with only a few additional rules to iptables. Knocking packets can be generated with a simple telnet or ssh client. It most certainly isn’t the complete solution for the security and maybe isn’t as secure as some other port knocking solutions (additional encryption of the knocks, more complicated schemes of knocking) but it’s easily deployable and rises the security of the system significantly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andrewkmitchell.com/blog/?feed=rss2&amp;p=236</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enabling Internet Connection Sharing in the WindowsMobile 6.1 Registry</title>
		<link>http://www.andrewkmitchell.com/blog/?p=227</link>
		<comments>http://www.andrewkmitchell.com/blog/?p=227#comments</comments>
		<pubDate>Wed, 30 Dec 2009 16:05:35 +0000</pubDate>
		<dc:creator>Andrew Mitchell</dc:creator>
				<category><![CDATA[InfoTech]]></category>

		<guid isPermaLink="false">http://www.andrewkmitchell.com/blog/?p=227</guid>
		<description><![CDATA[Everybody has a mentor.  I&#8217;m no exception.  When I first started working as a Technology Engineer, my mentor told me: Optimist: The glass is half full. Pessimist: The glass is half empty. Engineer: The glass is twice the size it needs to be. I&#8217;ve told that line at parties many times and have always gotten [...]]]></description>
			<content:encoded><![CDATA[<p>Everybody has a mentor.  I&#8217;m no exception.  When I first started working as a Technology Engineer, my mentor told me:</p>
<p style="padding-left: 30px;"><em>Optimist: The glass is half full.<br />
Pessimist: The glass is half empty.<br />
Engineer: The glass is twice the size it needs to be.</em></p>
<p>I&#8217;ve told that line at parties many times and have always gotten a laugh. Tell it to a group of senior engineers who have heard it a hundred times, though, and you’ll get a polite, stony silence at best.</p>
<p>Though it is humorous and when first explained to me it was done so with humour, if you honestly consider the statement, it is in fact very true.  The truth is, as engineers we are expected to think quick, act (certainly not react) quicker and implement solutions even faster.</p>
<p>As such, we must constantly be aware of potential problems.  As well as plausible and realistic solutions.  In my case, my clients expect solutions wherever and whenever needed, and that&#8217;s as it should be.  Today, with laptops this not exactly hard to accomplish.  With that said, an internet connection can sometimes be hard to come by.</p>
<p>Well, a known connection anyway.  Most people look at a cellphone as exactly that, but the truth is, it is likely just a modem.  That&#8217;s right, 96% of the cell providers in the world simply provide a network connection.  Then based on services purchased network access is granted or restricted.</p>
<p>I&#8217;m going to show you what I mean, using a WindowsMobile device as an example.  My carrier is AT&amp;T.  I have a basic &#8220;phone only&#8221; data plan.  A data plan of some sort is required.  This provisions a gateway to your account which grants you access to access networks external to your provider.</p>
<p>Many phones have restrictions which are internally provisioned by your provider.  WindowsMobile 6.1 stores these settings in the registry of your device.  There are two sections we will need to adjust.</p>
<p><a href="http://www.andrewkmitchell.com/blog/wp-content/uploads/2009/12/hklm_comm.png"><img class="aligncenter size-medium wp-image-224" title="hklm_comm" src="http://www.andrewkmitchell.com/blog/wp-content/uploads/2009/12/hklm_comm-300x225.png" alt="" width="300" height="225" /></a></p>
<p>In the ConnectionSharing section make sure your settings match this:</p>
<p><a href="http://www.andrewkmitchell.com/blog/wp-content/uploads/2009/12/hklm_comm_connectionsharing.png"><img class="aligncenter size-medium wp-image-225" title="hklm_comm_connectionsharing" src="http://www.andrewkmitchell.com/blog/wp-content/uploads/2009/12/hklm_comm_connectionsharing-300x225.png" alt="" width="300" height="225" /></a></p>
<p>The above settings ensure no phone based restrictions are preventing your phone from sharing its connection.</p>
<p>With AT&amp;T I also needed to enable the connection &#8220;AlwaysOn&#8221; flag.</p>
<p><a href="http://www.andrewkmitchell.com/blog/wp-content/uploads/2009/12/hklm_comm_connmgr_providers.png"><img class="aligncenter size-medium wp-image-226" title="hklm_comm_connmgr_providers" src="http://www.andrewkmitchell.com/blog/wp-content/uploads/2009/12/hklm_comm_connmgr_providers-300x225.png" alt="" width="300" height="225" /></a></p>
<p>Next, you&#8217;ll want to check and ensure your default phone data connection is being used for InternetSharing requests.</p>
<p><a href="http://www.andrewkmitchell.com/blog/wp-content/uploads/2009/12/hklm_comm_internetsharing_settings.png"><img class="aligncenter size-medium wp-image-231" title="hklm_comm_internetsharing_settings" src="http://www.andrewkmitchell.com/blog/wp-content/uploads/2009/12/hklm_comm_internetsharing_settings-300x225.png" alt="" width="300" height="225" /></a></p>
<p>At this point you need an Internet Connection Sharing software package on your phone if it did not ship standard.  Google can assist you there.</p>
<p>Then you&#8217;re all set. Enjoy <img src='http://www.andrewkmitchell.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.andrewkmitchell.com/blog/?feed=rss2&amp;p=227</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
