<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Lawrence Teo</title>
    <description>OpenBSD, security, software development</description>
    <link>https://lteo.net/</link>
    <atom:link href="https://lteo.net/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 29 Aug 2021 22:21:25 -0400</pubDate>
    <lastBuildDate>Sun, 29 Aug 2021 22:21:25 -0400</lastBuildDate>
    <generator>Jekyll v3.2.1</generator>
    
      <item>
        <title>Exploiting and Mitigating SeriousSAM / HiveNightmare</title>
        <description>&lt;p&gt;The Microsoft Patch Tuesday update on August 10, 2021 “addressed” SeriousSAM a.k.a. HiveNightmare (&lt;a href=&quot;https://msrc.microsoft.com/update-guide/vulnerability/CVE-2021-36934&quot;&gt;CVE-2021-36934&lt;/a&gt;). So it’s all fixed right? Not quite!&lt;/p&gt;

&lt;p&gt;It turns out that the update is only a partial fix, as acknowledged by &lt;a href=&quot;https://msrc.microsoft.com/update-guide/en-US/vulnerability/CVE-2021-36934&quot;&gt;Microsoft’s advisory&lt;/a&gt;.  Because of this, it is still possible to exploit the SeriousSAM vulnerability on patched systems under certain conditions.&lt;/p&gt;

&lt;p&gt;In this blog post, I will first show how the SeriousSAM vulnerability can be exploited in its original form. Next, I will demonstrate how Microsoft’s defenses can be bypassed to exploit SeriousSAM even on systems that are fully-patched by Microsoft’s Patch Tuesday update.  Finally, I will discuss mitigation measures.  This information should hopefully be useful to both pentesters and defenders.&lt;/p&gt;

&lt;p&gt;As a side note, throughout this blog post I will just refer to the vulnerability “SeriousSAM” for the sake of brevity, even though it is also known as “HiveNightmare” elsewhere.&lt;/p&gt;

&lt;h2 id=&quot;what-is-serioussam&quot;&gt;What is SeriousSAM?&lt;/h2&gt;

&lt;p&gt;So let’s start with what SeriousSAM is. On July 19, 2021, security researcher &lt;a href=&quot;https://twitter.com/jonaslyk/status/1417205166172950531&quot;&gt;Jonas Lyk&lt;/a&gt; discovered that recent versions of Windows 10 and 11 allowed regular users to read the Security Accounts Manager (SAM) file. This is bad since only administrators are supposed to be able to access the SAM file.&lt;/p&gt;

&lt;p&gt;For those who may not be familiar, the SAM file on a Windows system stores the password hashes of all local users of that system. It is similar to &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/master.passwd&lt;/code&gt; on BSD systems or &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/shadow&lt;/code&gt; on Linux – but with one key difference: On UNIX, gaining access to the password hashes in the shadow file does not grant the attacker immediate access to the user accounts that those password hashes belong to. On Windows, they might!&lt;/p&gt;

&lt;p&gt;The hashes in the SAM file are Windows NTLM hashes. They are different from UNIX password hashes in two ways:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;They are not salted, meaning that two users who happen to use the same password will also have the same password hash&lt;/li&gt;
  &lt;li&gt;It is possible to use Pass-the-Hash attacks to gain access to the user with just the hash alone (no password cracking required)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The password hashes in the SAM file include the hash for the local administrator. Therefore, a threat actor with local user access who obtains the SAM file would be able to elevate their privilege to that of a local administrator on the compromised machine.  From there, they can use it to perform further nefarious activities, such as:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Compromising other machines in the network (lateral movement) either by using Pass-the-Hash or cracking the local administrator’s password (if shared/weak passwords are being used)&lt;/li&gt;
  &lt;li&gt;Potentially compromising the entire Active Directory domain if they are able to dump the Domain Admin’s credentials from the LSASS process as the local administrator&lt;/li&gt;
  &lt;li&gt;Attacking all compromised systems with ransomware&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So even though SeriousSAM is classified as a “local vulnerability,” when it is abused along with other weak security practices, it can be used to carry out devastating attacks against a network.&lt;/p&gt;

&lt;h3 id=&quot;i-have-good-news-and-bad-news&quot;&gt;I have good news and bad news…&lt;/h3&gt;

&lt;p&gt;The good news is, Windows places a persistent file-lock on the SAM file, so the threat actor would not be able to actually read it under normal circumstances even if they have read permissions on the file.&lt;/p&gt;

&lt;p&gt;The bad news is, if the Windows system is set up to create volume shadows (similar to filesystem snapshots on BSD/Linux), a readable copy of the SAM file may still be present in the volume shadow copies. If the copy of the SAM file in the shadow copy is also readable, it &lt;em&gt;can&lt;/em&gt; be read by the threat actor since it is not file-locked.&lt;/p&gt;

&lt;h2 id=&quot;exploiting-serioussam-in-its-original-form&quot;&gt;Exploiting SeriousSAM in its Original Form&lt;/h2&gt;

&lt;p&gt;Now I will show how SeriousSAM can be exploited when it was first discovered in its original form, that is before Patch Tuesday (August 10, 2021). The following commands and output are from an isolated Windows system that I have intentionally disconnected from the Internet to prevent it from receiving Windows updates, so it is still vulnerable to SeriousSAM.&lt;/p&gt;

&lt;p&gt;The SAM file is located in the &lt;code class=&quot;highlighter-rouge&quot;&gt;%windir%\system32\config&lt;/code&gt; directory, which is usually &lt;code class=&quot;highlighter-rouge&quot;&gt;c:\windows\system32\config&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can use the &lt;a href=&quot;https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/icacls&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;icacls&lt;/code&gt; command&lt;/a&gt; which displays or modifies discretionary access control lists (DACLs) on files.&lt;/p&gt;

&lt;p&gt;Using a regular (non-administrator) user, check if you are able to read the SAM file:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;icacls c:\windows\system32\config\sam
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;For example, here’s what the output looks like if &lt;code class=&quot;highlighter-rouge&quot;&gt;C:\Windows\System32\config\sam&lt;/code&gt; is readable by regular users:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/serious-sam-icacls-sam.png&quot; alt=&quot;icacls.exe output showing a SAM file that is readable by regular users&quot; class=&quot;align-center&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;BUILTIN\Users:(I)(RX)&lt;/code&gt; line in the output indicates that regular users can read the SAM file, thus this system is vulnerable to SeriousSAM.  Conversely, if you see “Access denied”, that means the SAM file is not readable (but volume shadow copies could still be – more on this later!).&lt;/p&gt;

&lt;p&gt;However, even if &lt;code class=&quot;highlighter-rouge&quot;&gt;icacls.exe&lt;/code&gt; says the file is readable by regular users, attempting to copy the file will still fail:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;C:\Users\testuser&amp;gt;copy C:\Windows\System32\config\sam sam.copy
C:\Windows\System32\config\sam
Access is denied.
        0 file(s) copied.
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This is due to the persistent file-lock that Windows places on the SAM file.&lt;/p&gt;

&lt;h3 id=&quot;copying-from-the-shadows&quot;&gt;Copying from the Shadows&lt;/h3&gt;

&lt;p&gt;As mentioned earlier, there is still a way to obtain the SAM file if the system has been set up to create volume shadows, since the volume shadow copies may contain a user-readable copy of the SAM file.&lt;/p&gt;

&lt;p&gt;Even though the regular user (or a threat actor running in the context of the regular user) is not able to check if volume shadow copies exist, they can still guess the name of the volume shadow copies.&lt;/p&gt;

&lt;p&gt;To see why this is the case, run the following as administrator, which shows all volume shadow copies on the system:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vssadmin list shadows
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Here’s an example output:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;C:\Windows\system32&amp;gt;vssadmin list shadows
vssadmin 1.1 - Volume Shadow Copy Service administrative command-line tool
(C) Copyright 2001-2013 Microsoft Corp.

Contents of shadow copy set ID: {f475d904-854b-4374-989c-a8751d07f214}
   Contained 1 shadow copies at creation time: 8/12/2021 6:00:31 AM
      Shadow Copy ID: {7a995a8d-8cfa-4de1-8b27-50b456869b39}
         Original Volume: (C:)\\?\Volume{9d757f23-3cac-48ff-95e6-0fd9f4380732}\
         Shadow Copy Volume: \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy2
         Originating Machine: LAPTOP-RSLG7MF8
         Service Machine: LAPTOP-RSLG7MF8
         Provider: 'Microsoft Software Shadow Copy provider 1.0'
         Type: ClientAccessibleWriters
         Attributes: Persistent, Client-accessible, No auto release, Differential, Auto recovered
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;On this example system, there is one volume shadow copy called &lt;code class=&quot;highlighter-rouge&quot;&gt;\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy2&lt;/code&gt;. The names of the shadow copies are numbered sequentially, so the regular user can simply guess the names like &lt;code class=&quot;highlighter-rouge&quot;&gt;\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy2&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy3&lt;/code&gt;, and so forth.&lt;/p&gt;

&lt;p&gt;For example, a regular user could try to run this &lt;code class=&quot;highlighter-rouge&quot;&gt;icacls.exe&lt;/code&gt; command:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;icacls \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy2\windows\system32\config\sam
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Here’s what the output may look like:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/serious-sam-icacls-shadow-copy.png&quot; alt=&quot;Running icacls.exe on a volume shadow copy&quot; class=&quot;align-center&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Note how they are able to obtain the same output as if they ran it directly on the original SAM file.  The &lt;code class=&quot;highlighter-rouge&quot;&gt;icacls&lt;/code&gt; output also shows that the SAM file in the shadow copy is readable.&lt;/p&gt;

&lt;p&gt;Even so, the SAM file still cannot be copied using the &lt;code class=&quot;highlighter-rouge&quot;&gt;copy&lt;/code&gt; command:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;C:\Users\testuser&amp;gt;copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy2\windows\system32\config\sam sam.copy
\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy2\windows\system32\config\sam
Access is denied.
        0 file(s) copied.
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;However, the SAM file &lt;em&gt;can&lt;/em&gt; be copied via this PowerShell command that reads the file into standard input, and then redirects it to standard output and subsequently saves it to a new file:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;powershell.exe -c &quot;[Console]::OpenStandardInput().CopyTo([Console]::OpenStandardOutput())&quot; &amp;lt; &quot;\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy2\Windows\System32\Config\SAM&quot; &amp;gt; sam.copy
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Here’s an example showing a regular user called &lt;code class=&quot;highlighter-rouge&quot;&gt;testuser&lt;/code&gt; copying the SAM file from the volume shadow copy with PowerShell:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/serious-sam-copy-sam-shadow.png&quot; alt=&quot;Copying the SAM file from the shadow copy using PowerShell as a regular user&quot; class=&quot;align-center&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Note that even though the name of the vulnerability is “SeriousSAM”, the threat actor actually needs more than just the SAM file in order to decode it. The other file is &lt;code class=&quot;highlighter-rouge&quot;&gt;C:\Windows\System32\config\SYSTEM&lt;/code&gt;, which typically has the same permissions as the SAM file. Copying this SYSTEM file is equally trivial:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/serious-sam-copy-system-shadow.png&quot; alt=&quot;Copying the SYSTEM file from the shadow copy using PowerShell&quot; class=&quot;align-center&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The regular user (or a pentester or threat actor operating in the context of a regular user) now has access to the SAM and SYSTEM files.  Since these files grant them the password hashes, including that of the local administrator, they can then use the hash to impersonate the local admin.&lt;/p&gt;

&lt;h2 id=&quot;exploiting-serioussam-by-bypassing-defenses&quot;&gt;Exploiting SeriousSAM by Bypassing Defenses&lt;/h2&gt;

&lt;p&gt;Now that we’ve covered how SeriousSAM in its original form can be exploited, let’s talk about what Microsoft has done about SeriousSAM and how the defenses can still be bypassed..&lt;/p&gt;

&lt;p&gt;On Patch Tuesday (August 10, 2021), Microsoft has “addressed” this vulnerability by changing the permission of the &lt;code class=&quot;highlighter-rouge&quot;&gt;C:\Windows\System32\config\SAM&lt;/code&gt; file.  For the demos in this section, I will switch to a different Windows system that has been patched.&lt;/p&gt;

&lt;p&gt;As a result of the patch, the original &lt;code class=&quot;highlighter-rouge&quot;&gt;icacls&lt;/code&gt; command no longer works:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/serious-sam-post-patch-tuesday-icacls-sam.png&quot; alt=&quot;Running icacls on the SAM file on a patched system&quot; class=&quot;align-center&quot; /&gt;&lt;/p&gt;

&lt;p&gt;What about volume shadow copies?  It turns out that Microsoft did not make any changes to existing volume shadow copies that were created prior to Patch Tuesday.  Therefore, a user-readable SAM file in an existing shadow copy would still be readable.  However, they did make a change to Defender. Defender will now trigger alerts if users attempt to use &lt;code class=&quot;highlighter-rouge&quot;&gt;icacls.exe &lt;/code&gt; to check the permissions of the SAM file in a volume shadow copy (note: it is not clear to me at what point in time in the past Defender was updated to do this).&lt;/p&gt;

&lt;p&gt;So let’s see what happens when a regular user uses &lt;code class=&quot;highlighter-rouge&quot;&gt;icacls.exe&lt;/code&gt; to read the permissions of the SAM file in a shadow copy:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/serious-sam-post-patch-tuesday-icacls-sam-shadow-readable.png&quot; alt=&quot;Running icacls on the SAM file in a shadow copy on a patched system&quot; class=&quot;align-center&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It “worked,” but right after the &lt;code class=&quot;highlighter-rouge&quot;&gt;icacls.exe&lt;/code&gt; command completed, Defender terminated the &lt;code class=&quot;highlighter-rouge&quot;&gt;cmd.exe&lt;/code&gt; process since it was running an &lt;code class=&quot;highlighter-rouge&quot;&gt;icacls.exe&lt;/code&gt; process that attempted to read the SAM permissions from a volume shadow copy. The threat was identified as “Win32/Vessam.A”:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/serious-sam-post-patch-tuesday-defender-win32-vessam-cropped.png&quot; alt=&quot;Defender&quot; class=&quot;align-center&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;bypassing-defenses&quot;&gt;Bypassing Defenses&lt;/h3&gt;

&lt;p&gt;At first glance, these defenses seem effective, but unfortunately they fall short in three ways.&lt;/p&gt;

&lt;p&gt;First, even though the process was terminated, it was terminated “after the fact”; &lt;strong&gt;there is a short lag of a few seconds between the time the threat actor ran &lt;code class=&quot;highlighter-rouge&quot;&gt;icacls.exe&lt;/code&gt; and the time Defender terminates the process.&lt;/strong&gt;  In that short timeframe, the threat actor or their malware could have already recorded the permissions of the SAM file. Nevertheless, an alert would still have triggered so this may not be the attack mode of choice for experienced threat actors.&lt;/p&gt;

&lt;p&gt;Second, even though attempting to read the permissions of the SAM file in the volume shadow copy gets terminated, &lt;strong&gt;using &lt;code class=&quot;highlighter-rouge&quot;&gt;icacls.exe&lt;/code&gt; on the SYSTEM file still works&lt;/strong&gt;! Defender does not attempt to terminate this &lt;code class=&quot;highlighter-rouge&quot;&gt;icacls.exe&lt;/code&gt; attempt at all.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/serious-sam-post-patch-tuesday-icacls-system-shadow-time.png&quot; alt=&quot;Defender does not block icacls.exe from reading the SYSTEM file in the shadow copy.&quot; class=&quot;align-center&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As shown, I waited a full five minutes after &lt;code class=&quot;highlighter-rouge&quot;&gt;icacls.exe&lt;/code&gt; has completed but Defender did not terminate &lt;code class=&quot;highlighter-rouge&quot;&gt;cmd.exe&lt;/code&gt;, unlike earlier when I used &lt;code class=&quot;highlighter-rouge&quot;&gt;icacls.exe&lt;/code&gt; on the SAM file. Since the SYSTEM file usually has the same permissions as the SAM file, the threat actor can assume that the SAM file has the same permissions as SYSTEM.  And they can do this without triggering any Defender threat alert notifications.&lt;/p&gt;

&lt;p&gt;Third, and most seriously, &lt;strong&gt;the threat actor can still copy the SAM and SYSTEM files from the volume shadow copies without triggering any Defender alerts&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/serious-sam-post-patch-tuesday-copy-sam-system-shadow.png&quot; alt=&quot;Defender does not block the threat actor from copying the SAM and SYSTEM files in the shadow copy.&quot; class=&quot;align-center&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As a result, the threat actor can simply guess the names of the shadow copies and attempt to copy the SAM and SYSTEM files directly.&lt;/p&gt;

&lt;h2 id=&quot;mitigations&quot;&gt;Mitigations&lt;/h2&gt;

&lt;p&gt;Now that we have discussed exploitation, let’s talk about what we can do to mitigate against the SeriousSAM vulnerability.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Confirm that your SAM file is not readable by a regular user.&lt;/strong&gt; The most obvious thing to do is to check if the root cause of the issue has been addressed, which is by confirming that the SAM file is not readable by a regular user.  This is pretty straightforward; simply use a regular user account with no administrative privileges, and execute &lt;code class=&quot;highlighter-rouge&quot;&gt;icacls.exe C:\Windows\System32\config&lt;/code&gt;.  If you see “Access denied”, you’re good – but don’t forget to check the volume shadow copies which we will discuss next.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Inspect any volume shadow copies for readable SAM and SYSTEM files.&lt;/strong&gt;  If volume shadow copies are enabled, first check if there are any volume shadow copies by running &lt;code class=&quot;highlighter-rouge&quot;&gt;vssadmin list shadows&lt;/code&gt; as administrator.  If there are, run &lt;code class=&quot;highlighter-rouge&quot;&gt;icacls \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopyX\windows\system32\config\system&lt;/code&gt; (replace &lt;code class=&quot;highlighter-rouge&quot;&gt;HarddiskVolumeShadowCopyX&lt;/code&gt; with the name of the shadow copy) to check if any of the shadow copies return &lt;code class=&quot;highlighter-rouge&quot;&gt;BUILTIN\Users:(I)(RX)&lt;/code&gt;. If there are, these need to be deleted – see &lt;a href=&quot;https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/vssadmin-delete-shadows&quot;&gt;Microsoft’s documentation on &lt;code class=&quot;highlighter-rouge&quot;&gt;vssadmin delete shadows&lt;/code&gt;&lt;/a&gt; for details.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Don’t forget systems that are not regularly updated, like new computers and suspended virtual machines.&lt;/strong&gt;  Microsoft may have partially addressed the underlying vulnerability, but systems such as newly purchased computers and suspended virtual machines may not have received the patch.  These systems need to be investigated as well.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Do not use shared local administrator passwords on multiple systems.&lt;/strong&gt; This will help prevent a threat actor from reusing the credentials they obtained from a system that was compromised using SeriousSAM to log in to other systems in the network (lateral movement).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Do not allow the Domain Admin accounts to log in to workstations.&lt;/strong&gt; When domain accounts log in to systems, they leave behind credentials in the LSASS process.  A threat actor who has compromised the local administrator account on a workstation may have the ability to dump the credentials within LSASS process.  If a Domain Admin has recently logged in to such a workstation, the threat actor may be able to obtain the credentials of the Domain Admin and use that to compromise the entire Active Directory domain.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;wrapping-things-up&quot;&gt;Wrapping Things Up&lt;/h2&gt;

&lt;p&gt;The SeriousSAM / HiveNightmare vulnerability allows regular users, or threat actors operating in the context of a regular user, to read the SAM file which contains password hashes.  This includes the password hash of the local administrator, which the threat actor can then use to impersonate the local administrator to potentially compromise other machines and perhaps even the entire AD domain.&lt;/p&gt;

&lt;p&gt;Microsoft has applied defenses by modifying the permission of the SAM file; however, volume shadow copies are untouched.  Therefore, administrators should examine the existing volume shadow copies to ensure that the SAM and SYSTEM files are not readable by regular users.&lt;/p&gt;

&lt;p&gt;I hope you’ve found this blog post helpful.  I hope to perform more research on similar security topics and present my results on this blog.  If you would like to be notified of any new posts, please feel free to &lt;a href=&quot;https://twitter.com/lteo&quot;&gt;follow me on Twitter (@lteo)&lt;/a&gt; or &lt;a href=&quot;/atom.xml&quot;&gt;subscribe to the RSS feed&lt;/a&gt; of this blog.&lt;/p&gt;
</description>
        <pubDate>Sun, 29 Aug 2021 21:38:45 -0400</pubDate>
        <link>https://lteo.net/blog/2021/08/29/exploiting-and-mitigating-serious-sam-hive-nightmare/</link>
        <guid isPermaLink="true">https://lteo.net/blog/2021/08/29/exploiting-and-mitigating-serious-sam-hive-nightmare/</guid>
        
        
      </item>
    
      <item>
        <title>CarolinaCon 15: Writing Exploit-Resistant Code With OpenBSD</title>
        <description>&lt;p&gt;On April 27, 2019, I gave a talk on how to use OpenBSD to write better software at &lt;a href=&quot;http://www.carolinacon.org&quot;&gt;CarolinaCon 15&lt;/a&gt; in Charlotte.&lt;/p&gt;

&lt;h2 id=&quot;abstract&quot;&gt;Abstract&lt;/h2&gt;

&lt;p&gt;OpenBSD is renowned for its security innovations and code quality. With its emphasis on code correctness, exploit mitigation techniques, and a rigorous development process, OpenBSD provides a rich platform and environment for developers to create robust software.&lt;/p&gt;

&lt;p&gt;This talk explores various OpenBSD programs, exploit mitigation techniques, tools, and development practices to show how you can use them to write code that is safe, robust, and resistant to exploits – even if your code is meant for platforms other than OpenBSD.&lt;/p&gt;

&lt;h2 id=&quot;slides&quot;&gt;Slides&lt;/h2&gt;

&lt;p&gt;You can &lt;a href=&quot;/assets/pdf/lteo-openbsd-carolinacon15-20190427.pdf&quot;&gt;download my slides here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video&quot;&gt;Video&lt;/h2&gt;

&lt;p&gt;You can see a &lt;a href=&quot;https://www.pscp.tv/w/1kvKpOgEyROxE&quot;&gt;video of the talk here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Huge thanks to &lt;a href=&quot;https://twitter.com/MagisterQuis&quot;&gt;Stuart McMurray&lt;/a&gt; who kindly recorded my talk with Periscope!  Stuart also gave an excellent talk on DNS tunneling at the same conference; be sure to check it out (&lt;a href=&quot;https://twitter.com/MagisterQuis/status/1122173359113084928&quot;&gt;slides&lt;/a&gt;, &lt;a href=&quot;https://www.pscp.tv/w/1yoJMEQjDgpJQ&quot;&gt;video&lt;/a&gt;)!&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;p&gt;These are the resources that I used to prepare my talk.  They are listed according to the sections used in the talk.  Thanks to all the authors, without whom this talk would not have been possible!&lt;/p&gt;

&lt;h3 id=&quot;exploit-mitigation-techniques-in-openbsd&quot;&gt;Exploit Mitigation Techniques in OpenBSD&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Theo de Raadt, &lt;a href=&quot;https://www.openbsd.org/papers/bsdtw.pdf&quot;&gt;Mitigations and other real security features&lt;/a&gt;, BSDTW 2017.&lt;/li&gt;
  &lt;li&gt;Theo de Raadt, &lt;a href=&quot;https://www.openbsd.org/papers/hackfest2014-arc4random&quot;&gt;arc4random: randomization for all occasions&lt;/a&gt;, Hackfest 2014.&lt;/li&gt;
  &lt;li&gt;Theo de Raadt, &lt;a href=&quot;https://www.openbsd.org/papers/ven05-deraadt/&quot;&gt;Exploit Mitigation Techniques&lt;/a&gt;, OpenCON 2005.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;setting-up-your-openbsd-environment&quot;&gt;Setting up your OpenBSD environment&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Ted Unangst, &lt;a href=&quot;https://www.openbsd.org/papers/dev-sw-hostile-env.html&quot;&gt;Developing Software in a Hostile Environment&lt;/a&gt;, EuroBSDCon 2014.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://man.openbsd.org/malloc&quot;&gt;malloc man page&lt;/a&gt; - shows malloc options and how you can use them&lt;/li&gt;
  &lt;li&gt;Mike Larkin, &lt;a href=&quot;https://www.openbsd.org/papers/hackfest2015-w-xor-x.pdf&quot;&gt;Kernel W^X Improvements In OpenBSD&lt;/a&gt;, Hackfest 2015. (&lt;a href=&quot;https://www.youtube.com/watch?v=A7vtAAeW6zo&quot;&gt;Video&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.openbsd.org/plat.html&quot;&gt;OpenBSD platforms&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Peter Valchev, &lt;a href=&quot;http://www.openbsd.org/papers/rp2007-slides.pdf&quot;&gt;Using OpenBSD Security Features to Find Software Bugs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;auditing-your-code&quot;&gt;Auditing your code&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://man.openbsd.org/style&quot;&gt;OpenBSD style(9) coding style&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://man.openbsd.org/snprintf&quot;&gt;OpenBSD snprintf(3) man page&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Henning Brauer and Sven Dehmlow, &lt;a href=&quot;http://quigon.bsws.de/papers/2010/bsdcan/&quot;&gt;Puffy at Work: Code Right and Secure, the OpenBSD Way&lt;/a&gt;, BSDCan 2010.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;secure-api-alternatives&quot;&gt;Secure API alternatives&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://man.openbsd.org/strlcpy&quot;&gt;strlcpy(3)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://man.openbsd.org/strtonum&quot;&gt;strtonum(3)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://man.openbsd.org/explicit_bzero&quot;&gt;explicit_bzero(3)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;The &lt;a href=&quot;https://man.openbsd.org/malloc&quot;&gt;malloc(3)&lt;/a&gt; man page also describes the freezero(3) and reallocarray(3) functions.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://man.openbsd.org/arc4random&quot;&gt;arc4random(3)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;libtls&quot;&gt;libtls&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://man.openbsd.org/tls_init&quot;&gt;libtls man page&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/bob-beck/libtls/blob/master/TUTORIAL.md&quot;&gt;Bob Beck’s libtls tutorial&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;tedu-your-code&quot;&gt;tedu your code&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Ted Unangst, &lt;a href=&quot;https://www.openbsd.org/papers/pruning.html&quot;&gt;Pruning and Polishing: Keeping OpenBSD Modern&lt;/a&gt;, AsiaBSDCon 2015.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;pledge2&quot;&gt;pledge(2)&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://man.openbsd.org/pledge&quot;&gt;pledge(2)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Theo de Raadt, &lt;a href=&quot;https://www.openbsd.org/papers/eurobsdcon2017-pledge.pdf&quot;&gt;Pledge and Privsep&lt;/a&gt;, EuroBSDCOn 2017. (&lt;a href=&quot;https://www.youtube.com/watch?v=fYgG0ds2_UQ&quot;&gt;Video&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Theo de Raadt, &lt;a href=&quot;http://www.openbsd.org/papers/dot2016.pdf&quot;&gt;Privilege Separation and Pledge&lt;/a&gt;, dotSecurity 2016.&lt;/li&gt;
  &lt;li&gt;Bob Beck, &lt;a href=&quot;https://www.openbsd.org/papers/BeckPledgeUnveilBSDCan2018.pdf&quot;&gt;Pledge, and Unveil, in OpenBSD&lt;/a&gt;, BSDCan 2018. (&lt;a href=&quot;https://www.youtube.com/watch?v=bXO6nelFt-E&quot;&gt;Video&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Theo de Raadt, &lt;a href=&quot;http://www.openbsd.org/papers/hackfest2015-pledge/&quot;&gt;pledge(): a new mitigation mechanism&lt;/a&gt;, Hackfest 2015. (&lt;a href=&quot;https://www.youtube.com/watch?v=F_7S1eqKsFk&quot;&gt;Video&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Giovanni Bechis, &lt;a href=&quot;https://www.snb.it/downloads/seccomp-pledge-oss2017.pdf&quot;&gt;Linux seccomp(2) vs OpenBSD pledge(2)&lt;/a&gt;, Open Source Summit 2017.&lt;/li&gt;
  &lt;li&gt;pledge(2) bindings
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/qbit/node-pledge&quot;&gt;Node pledge&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/afresh1/OpenBSD-Pledge&quot;&gt;Perl pledge&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/jeremyevans/ruby-pledge&quot;&gt;Ruby pledge&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://crates.io/crates/pledge&quot;&gt;Rust pledge&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://gist.github.com/ligurio/f6114bd1df371047dd80ea9b8a55c104&quot;&gt;Others&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;unveil2&quot;&gt;unveil(2)&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://man.openbsd.org/unveil&quot;&gt;unveil(2)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Bob Beck, &lt;a href=&quot;https://www.openbsd.org/papers/BeckPledgeUnveilBSDCan2018.pdf&quot;&gt;Pledge, and Unveil, in OpenBSD&lt;/a&gt;, BSDCan 2018. (&lt;a href=&quot;https://www.youtube.com/watch?v=bXO6nelFt-E&quot;&gt;Video&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Bob Beck, &lt;a href=&quot;https://www.openbsd.org/papers/eurobsdcon2018-unveil/&quot;&gt;Unveil in OpenBSD&lt;/a&gt;, EuroBSDCon 2018.&lt;/li&gt;
  &lt;li&gt;unveil(2) bindings
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/jcs/ruby-unveil&quot;&gt;Ruby unveil&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://crates.rs/crates/unveil&quot;&gt;Rust unveil&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;privilege-dropping-and-privilege-separation&quot;&gt;Privilege Dropping and Privilege Separation&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;chroot
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://man.openbsd.org/tzset&quot;&gt;tzset(3)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://man.openbsd.org/getpwnam&quot;&gt;getpwnam(2)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://man.openbsd.org/setpgid&quot;&gt;setpgid(2)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://man.openbsd.org/chroot.2&quot;&gt;chroot(2)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://man.openbsd.org/setgroups&quot;&gt;setgroups(2)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://man.openbsd.org/setresuid&quot;&gt;setresuid(2)&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Theo de Raadt, &lt;a href=&quot;http://www.openbsd.org/papers/dot2016.pdf&quot;&gt;Privilege Separation and Pledge&lt;/a&gt;, dotSecurity 2016.&lt;/li&gt;
  &lt;li&gt;Reyk Floeter, &lt;a href=&quot;https://github.com/openbsd/src/commit/3d77879f07011e5cf17cde6a63b023bc777c8cac#diff-eed07da139bada69017ed2c970f25862&quot;&gt;relayd commit: Introduce privsep for private keys&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Reyk Floeter, &lt;a href=&quot;https://github.com/openbsd/src/commit/c4df3bf27090249fec8edcd55b8fb30d01283bdf#diff-3e284920210befbf4ffd58451c18f454&quot;&gt;smtpd commit: Implement RSA privilege separation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Henning Brauer, &lt;a href=&quot;http://quigon.bsws.de/papers/21c3/&quot;&gt;OpenBGPD and OpenNTPD&lt;/a&gt;, 21C3 - Chaos Communication Congress 2004.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/openbsd/src/tree/master/usr.sbin/ntpd&quot;&gt;ntpd source&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Ken Westerback, &lt;a href=&quot;https://github.com/krwesterback/newd&quot;&gt;Skeletal OpenBSD daemon - three processes, priv separated&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;related-resources&quot;&gt;Related Resources&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.openbsd.org/events.html&quot;&gt;OpenBSD events and papers&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.undeadly.org/&quot;&gt;OpenBSD Journal&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.openbsd.org/innovations.html&quot;&gt;OpenBSD innovations&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://twitter.com/canadianbryan&quot;&gt;Bryan Steele’s Twitter feed on new OpenBSD features&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Andrea Bittau, Adam Belay, Ali Mashtizadeh, David Mazieres, and Dan Boneh, &lt;a href=&quot;http://www.scs.stanford.edu/brop/bittau-brop.pdf&quot;&gt;Hacking Blind&lt;/a&gt;, 2014 IEEE Symposium on Security and Privacy.&lt;/li&gt;
  &lt;li&gt;Chris Anley, John Heasman, Felix Lindner, and Gerardo Richarte, &lt;a href=&quot;https://amzn.to/2ZqnJyB&quot;&gt;The Shellcoder’s Handbook: Discovering and Exploiting Security Holes (2nd Edition)&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope you find my talk and this content useful.  Happy hacking!&lt;/p&gt;
</description>
        <pubDate>Sat, 27 Apr 2019 21:09:02 -0400</pubDate>
        <link>https://lteo.net/blog/2019/04/27/carolinacon-15-writing-exploit-resistant-code-with-openbsd/</link>
        <guid isPermaLink="true">https://lteo.net/blog/2019/04/27/carolinacon-15-writing-exploit-resistant-code-with-openbsd/</guid>
        
        
      </item>
    
      <item>
        <title>Testing Your Snort Rules Redux</title>
        <description>&lt;p&gt;Exactly four years ago, I blogged about &lt;a href=&quot;/blog/2012/10/26/an-easy-way-to-test-your-snort-rules/&quot;&gt;testing Snort rules on
OpenBSD&lt;/a&gt;. That post
described a quick way to test if Snort has correctly loaded your rules and
whether it will emit an alert when it reads a matching packet.&lt;/p&gt;

&lt;p&gt;But things have changed since then; for starters, the Snort rules I suggested in
that post have been disabled in the official Snort ruleset for a long time now,
so the instructions I wrote no longer work as-is.&lt;/p&gt;

&lt;p&gt;That post also
suggested the installation of an ancient webserver called &lt;code class=&quot;highlighter-rouge&quot;&gt;thttpd&lt;/code&gt;, which has
been &lt;a href=&quot;http://marc.info/?l=openbsd-ports-cvs&amp;amp;m=137635020832258&amp;amp;w=2&quot;&gt;removed&lt;/a&gt;
(for a very good reason!) from the OpenBSD ports tree.&lt;/p&gt;

&lt;p&gt;A new version of that blog post is sorely needed, so here it is!&lt;/p&gt;

&lt;p&gt;One note before we begin: While this post talks about testing Snort on
OpenBSD (specifically OpenBSD 6.0-current), the principles discussed here can
be applied to testing Snort on other operating systems like Linux. I highly
recommend OpenBSD though. :)&lt;/p&gt;

&lt;h2 id=&quot;test-goals&quot;&gt;Test goals&lt;/h2&gt;

&lt;p&gt;Just like the earlier post, the goals of the test are to:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Confirm that you have actually loaded some rules&lt;/li&gt;
  &lt;li&gt;Ensure that your Snort setup can actually trigger alerts&lt;/li&gt;
  &lt;li&gt;Confirm that you are logging alerts to the right file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that this is just a simple test – we’re simply trying to confirm the above
instead of doing a thorough test against a Snort installation. After
all, it would be unwise to attempt a full all-out assault against Snort without
confirming the above!&lt;/p&gt;

&lt;p&gt;We will do this simple test by setting up two hosts: the Snort host and an
“attacker” host. The Snort host will run &lt;code class=&quot;highlighter-rouge&quot;&gt;snort&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;httpd&lt;/code&gt; (OpenBSD’s new
webserver; more on that below!), while the attacker host will generate a
“malicious” HTTP request that will match one of the rules on the Snort host and
trigger an alert.&lt;/p&gt;

&lt;p&gt;Here’s how it looks like graphically:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/testing-snort-rules.png&quot; alt=&quot;A simple Snort testbed&quot; class=&quot;align-center&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Note the sample IP addresses because we’ll be referring to them below.&lt;/p&gt;

&lt;p&gt;Now that you have admired my MoMA-worthy artistic creation, it’s time to
move on to installing Snort!&lt;/p&gt;

&lt;h2 id=&quot;installing-snort&quot;&gt;Installing Snort&lt;/h2&gt;

&lt;p&gt;Installing Snort is really easy on OpenBSD:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;pkg_add snort&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;pkg_add&lt;/code&gt; command will pull in dependent packages just like you expect it to.&lt;/p&gt;

&lt;h2 id=&quot;fetching-snort-rules&quot;&gt;Fetching Snort rules&lt;/h2&gt;

&lt;p&gt;Next, we grab some Snort rules to test. The steps here differ depending on
whether you have &lt;a href=&quot;https://www.snort.org/users/sign_up&quot;&gt;signed up for an oinkcode&lt;/a&gt;
at Snort.org or not.&lt;/p&gt;

&lt;p&gt;For those unfamiliar with what an oinkcode is, it’s basically a registration
code that lets you fetch registered Snort rules from Snort.org. There are two
types of free rulesets at Snort.org:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A registered ruleset that needs the oinkcode; and&lt;/li&gt;
  &lt;li&gt;A community ruleset that does not need the oinkcode&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that the registered ruleset also contains all the rules in the community
ruleset. The good news is the test rule that I will use in this blog post
is a community rule so it’s available in both rulesets. So it’s up to you
whether you would like to sign up for an oinkcode or not.&lt;/p&gt;

&lt;p&gt;If you’re serious about learning and using Snort though, I recommend signing up
for the oinkcode.&lt;/p&gt;

&lt;h3 id=&quot;fetching-test-snort-rules-with-an-oinkcode&quot;&gt;Fetching test Snort rules with an oinkcode&lt;/h3&gt;

&lt;p&gt;If you have signed up for an oinkcode, simply read
&lt;code class=&quot;highlighter-rouge&quot;&gt;/usr/local/share/doc/pkg-readmes/snort-2.x.x.x&lt;/code&gt; for the steps to
activate those rules.  To summarize, here are the steps (assuming you’re
running Snort 2.9.8.3, the latest version at the time of writing):&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ftp -o snortrules-snapshot-2983.tar.gz \
    https://www.snort.org/reg-rules/snortrules-snapshot-2983.tar.gz/&amp;lt;oinkcode&amp;gt;
tar -C /etc/snort -xzf snortrules-snapshot-2983.tar.gz rules preproc_rules
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;fetching-test-snort-rules-without-an-oinkcode&quot;&gt;Fetching test Snort rules without an oinkcode&lt;/h3&gt;

&lt;p&gt;If you have NOT signed up for an oinkcode, fetch the community ruleset using
these commands:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ftp https://www.snort.org/downloads/community/community-rules.tar.gz
tar -C /etc/snort -s '/community-//' -xzf community-rules.tar.gz
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;You now have to edit &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/snort/snort.conf&lt;/code&gt; and comment out all the
&lt;code class=&quot;highlighter-rouge&quot;&gt;include $RULE_PATH/*.rules&lt;/code&gt; lines (since they are meant for the registered
ruleset) by adding a &lt;code class=&quot;highlighter-rouge&quot;&gt;#&lt;/code&gt; character in front of those lines.&lt;/p&gt;

&lt;p&gt;Now activate the community ruleset by adding this line:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;include $RULE_PATH/community.rules
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;starting-snort&quot;&gt;Starting Snort&lt;/h2&gt;

&lt;p&gt;Before starting Snort, check if you’re running Snort on a system with
multiple network interfaces. If you are, you’ll need to specify the network
interface you want Snort to listen on.&lt;/p&gt;

&lt;p&gt;The best way to do this is to edit &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/snort/snort.conf&lt;/code&gt; and add a line
like this (for example, to make Snort listen on the &lt;code class=&quot;highlighter-rouge&quot;&gt;em0&lt;/code&gt; interface):&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;config interface: em0
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now let’s fire up Snort!&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/rc.d/snort start
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;start-the-webserver&quot;&gt;Start the webserver&lt;/h2&gt;

&lt;p&gt;Now we start the webserver. As mentioned earlier, the good news is OpenBSD
now comes with its own HTTP server called &lt;code class=&quot;highlighter-rouge&quot;&gt;httpd&lt;/code&gt;, which makes it really
straightforward to get an OpenBSD box to serve HTTP without installing any
packages.&lt;/p&gt;

&lt;p&gt;First, create a simple &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/httpd.conf&lt;/code&gt; file:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ext_addr=&quot;*&quot;

server &quot;default&quot; {
        listen on $ext_addr port 80
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;That’s crazy simple, right?&lt;/p&gt;

&lt;p&gt;Then start &lt;code class=&quot;highlighter-rouge&quot;&gt;httpd&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/rc.d/httpd -f start
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;You’ll need &lt;code class=&quot;highlighter-rouge&quot;&gt;-f&lt;/code&gt; to force &lt;code class=&quot;highlighter-rouge&quot;&gt;httpd&lt;/code&gt; to start because the line &lt;code class=&quot;highlighter-rouge&quot;&gt;httpd_flags=NO&lt;/code&gt; is
in your &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/rc.conf&lt;/code&gt; by default. If you wish to start &lt;code class=&quot;highlighter-rouge&quot;&gt;httpd&lt;/code&gt;
on a permanent basis (i.e. for it to persist across reboots), change that line
in &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/rc.conf&lt;/code&gt; to:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;httpd_flags=
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;For more information on &lt;code class=&quot;highlighter-rouge&quot;&gt;httpd&lt;/code&gt;, read up the man pages for
&lt;a href=&quot;http://man.openbsd.org/httpd.8&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;httpd(8)&lt;/code&gt;&lt;/a&gt; and
&lt;a href=&quot;http://man.openbsd.org/httpd.conf.5&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;httpd.conf(5)&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;triggering-an-alert&quot;&gt;Triggering an alert&lt;/h2&gt;

&lt;p&gt;We’re finally at the fun part!&lt;/p&gt;

&lt;p&gt;To get Snort to trigger an alert, you’ll need to look for a rule that allows you
to create a packet that would trigger an alert from it. The easiest way is to
look for simple HTTP-based rules:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;grep -v '# alert' /etc/snort/rules/*.rules | \
    grep '&amp;gt; \$HOME_NET \$HTTP_PORTS' | less
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Fortunately I’ve done this for you, and found this simple one in
&lt;code class=&quot;highlighter-rouge&quot;&gt;blacklist.rules&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;alert tcp $EXTERNAL_NET any -&amp;gt; $HOME_NET $HTTP_PORTS
(msg:&quot;BLACKLIST User-Agent ASafaWeb Scan&quot;; flow:to_server,established;
content:&quot;User-Agent|3A| asafaweb.com&quot;; fast_pattern:only; http_header;
metadata:policy balanced-ips alert, policy security-ips drop,
ruleset community, service http; reference:url,asafaweb.com;
classtype:network-scan; sid:21327; rev:6;)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now on the Snort host, run this to watch its alert log file:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;tail -f /var/snort/log/alert
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;If your attacker host is also an OpenBSD system, you can use OpenBSD’s
&lt;code class=&quot;highlighter-rouge&quot;&gt;ftp(1)&lt;/code&gt; command to generate the HTTP request (remember to replace the
IP address below with the IP of your Snort host):&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ftp -U 'asafaweb.com' http://172.16.10.8/index.html
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;If your attacker host is not an OpenBSD system, you could use &lt;code class=&quot;highlighter-rouge&quot;&gt;curl&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;wget&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl -A 'asafaweb.com' http://172.16.10.8/index.html
wget -U 'asafaweb.com' http://172.16.10.8/index.html
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The following alert should show up in your &lt;code class=&quot;highlighter-rouge&quot;&gt;tail&lt;/code&gt; session on the Snort host:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[**] [1:21327:6] BLACKLIST User-Agent ASafaWeb Scan [**]
[Classification: Detection of a Network Scan] [Priority: 3]
09/29-23:36:38.000142 172.16.10.3:52234 -&amp;gt; 172.16.10.8:80
TCP TTL:64 TOS:0x0 ID:63384 IpLen:20 DgmLen:128 DF
***AP*** Seq: 0x2E060816  Ack: 0x53AAAF16  Win: 0x43C0  TcpLen: 32
[Xref =&amp;gt; http://asafaweb.com]
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Congratulations! You have successfully confirmed that Snort has loaded its rules
and triggered an alert in the correct log file. Time to celebrate with a nice
cup of coffee.&lt;/p&gt;

&lt;h2 id=&quot;next-steps&quot;&gt;Next steps&lt;/h2&gt;

&lt;p&gt;This post just describes a simple way to test a Snort rule. If you want to
fetch your Snort rules and manage them in a proper production environment,
I recommend using &lt;a href=&quot;https://github.com/shirkdog/pulledpork&quot;&gt;PulledPork&lt;/a&gt;.
This post from the Snort blog post describes &lt;a href=&quot;http://blog.snort.org/2012/01/importance-of-pulledpork.html&quot;&gt;why using PulledPork is
important&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For further information on setting up Snort, check out:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://snort.org/documents/snort-users-manual-html&quot;&gt;Snort manual&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://snort.org/faq&quot;&gt;Snort FAQ&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://snort.org/documents&quot;&gt;Other Snort documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy testing! (I would have said “Happy Snorting” but that felt a
little weird.)&lt;/p&gt;
</description>
        <pubDate>Wed, 26 Oct 2016 00:01:00 -0400</pubDate>
        <link>https://lteo.net/blog/2016/10/26/testing-your-snort-rules-redux/</link>
        <guid isPermaLink="true">https://lteo.net/blog/2016/10/26/testing-your-snort-rules-redux/</guid>
        
        
        <category>OpenBSD</category>
        
        <category>Snort</category>
        
        <category>httpd</category>
        
      </item>
    
      <item>
        <title>Dissecting OpenBSD's divert(4) Part 1: Introduction</title>
        <description>&lt;p&gt;For more than four years I have been using and tinkering with OpenBSD’s
&lt;a href=&quot;http://www.openbsd.org/cgi-bin/man.cgi?query=divert&quot;&gt;divert(4)&lt;/a&gt;.
At one point after OpenBSD 4.9 was released, I ran into an annoying bug in
divert(4) that totally prevented me from using it.  At the time I had no idea
how to fix it, so I did the next best thing by filing a
&lt;a href=&quot;http://marc.info/?l=openbsd-bugs&amp;amp;m=131805777610565&amp;amp;w=2&quot;&gt;detailed bug report&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Eventually I realized that the bug isn’t going to fix itself, so I decided it
was time to
roll up my sleeves and wade into the code.  So after 2.5 years of on-and-off
tinkering and staring at the code and head-scratching and facedesking I finally
&lt;a href=&quot;http://marc.info/?l=openbsd-cvs&amp;amp;m=136543514928571&amp;amp;w=2&quot;&gt;fixed it&lt;/a&gt;, thanks to a
ton of help from Bret Lambert (blambert@).  The problem turned out to be due to
checksums, which is another interesting topic but that’s a story for another
day.&lt;/p&gt;

&lt;p&gt;In that whole process I had a chance to study divert(4)’s internals in detail,
and it was also my first successful attempt at kernel hacking (or rather, 33
unsuccessful attempts and one successful final attempt :)).&lt;/p&gt;

&lt;p&gt;I have since moved on to fixing and improving divert(4) in other ways.  But I
thought it might be worthwhile to do a brain dump what I learned, both to
refresh myself when I start hacking on divert(4) again and for others who may
be interested. So today I’m starting (or attempting to start) a series of blog
posts on divert(4).&lt;/p&gt;

&lt;h2 id=&quot;what-exactly-is-divert4&quot;&gt;What exactly is divert(4)?&lt;/h2&gt;

&lt;p&gt;OpenBSD’s divert(4) is a relatively unknown but powerful feature in PF
that allows whole network packets to be diverted to userspace for Layer
7 inspection. The whole packet here means everything from the IP header
to the payload. If you’ve ever wanted to intercept and inspect every
single packet of a session in userspace, including the TCP handshake
packets themselves in the case of a TCP session, you can use divert(4)
to do so (there are other ways to do this, but divert(4) makes it much
easier in my opinion).&lt;/p&gt;

&lt;p&gt;Unlike a libpcap-based packet sniffer which works by listening passively
to copies of packets, a divert(4) userspace program receives the whole
packet itself and gets to actively decide whether the packet should be sent on
its way.&lt;/p&gt;

&lt;p&gt;divert(4) was first released as part of &lt;a href=&quot;http://www.openbsd.org/47.html&quot;&gt;OpenBSD 4.7&lt;/a&gt;.
It was &lt;a href=&quot;http://marc.info/?l=openbsd-cvs&amp;amp;m=125467287101327&amp;amp;w=2&quot;&gt;originally committed&lt;/a&gt;
by Michele Marchetto (michele@).  As mentioned in the commit message,
OpenBSD’s divert(4) is compatible with FreeBSD’s divert sockets:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;OpenBSD divert(4) is meant to be compatible with software running on
top of FreeBSD's divert sockets even though they are pretty
different and will become even more with time.
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;One of the most popular programs that uses divert(4) is
&lt;a href=&quot;http://www.snort.org/&quot;&gt;Snort&lt;/a&gt; when it is used in inline mode (thus
running Snort as what the industry calls an “intrusion prevention
system”).  Since OpenBSD’s divert(4) is currently compatible with
FreeBSD’s divert(4), Snort can work with OpenBSD’s divert sockets as well.&lt;/p&gt;

&lt;p&gt;One key difference though is that OpenBSD’s divert(4) works with
OpenBSD’s PF, while FreeBSD’s works with IPFW (but &lt;em&gt;not&lt;/em&gt; FreeBSD’s PF).
Note that I have never used IPFW, so I won’t be able to do a full
comparison with IPFW’s divert.&lt;/p&gt;

&lt;p&gt;Another program in the OpenBSD world that uses divert(4) is
Florian Obser’s (florian@) &lt;a href=&quot;http://www.sha256.net/dnsfilter/&quot;&gt;dnsfilter&lt;/a&gt;.
Florian wrote an interesting post about
&lt;a href=&quot;http://marc.info/?l=openbsd-misc&amp;amp;m=134187877220567&amp;amp;w=2&quot;&gt;why he wrote dnsfilter&lt;/a&gt;.
There’s also an &lt;a href=&quot;http://cvsweb.openbsd.org/cgi-bin/cvsweb/ports/net/dnsfilter/&quot;&gt;OpenBSD port&lt;/a&gt;
available for dnsfilter.&lt;/p&gt;

&lt;h2 id=&quot;an-example&quot;&gt;An example&lt;/h2&gt;

&lt;p&gt;I tend to learn best by example so let’s start with one.  Packets are diverted
to divert(4) sockets using the &lt;code class=&quot;highlighter-rouge&quot;&gt;divert-packet&lt;/code&gt; PF parameter. For example, the
following rule queues all outbound TCP port 80 traffic to a divert(4) socket:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pass out quick on egress inet proto tcp to port 80 divert-packet port 700
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT: &lt;code class=&quot;highlighter-rouge&quot;&gt;divert-packet&lt;/code&gt; should not be confused with the &lt;code class=&quot;highlighter-rouge&quot;&gt;divert-to&lt;/code&gt; and
&lt;code class=&quot;highlighter-rouge&quot;&gt;divert-reply&lt;/code&gt; PF parameters.&lt;/strong&gt;  It is unfortunate that they share 
similar names, but they are different features.&lt;/p&gt;

&lt;p&gt;For the above PF rule to be useful, you will need to run a userspace program
that listens to divert(4) sockets.  For a quick test you can use the example
program on the divert(4) man page.&lt;/p&gt;

&lt;p&gt;Here’s an abominable one-liner that you can use to extract that program
out of the man page:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mandoc -Tascii /usr/share/man/man4/divert.4 | &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    awk &lt;span class=&quot;s1&quot;&gt;'/#include/,/^     }/ { gsub(/^     /, &quot;&quot;); print}'&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &amp;gt;divert-example.c
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;If you compile and run that program, and use a browser on the same
host where the above PF rule is loaded, you should be able to see
activity from that example program.&lt;/p&gt;

&lt;p&gt;The example program simply reads every packet and reinjects it back into
the kernel as-is.  A more sophisticated program could perform further
inspection of the packet and decide to discard the packet by not
reinjecting it (or even modifying it).&lt;/p&gt;

&lt;p&gt;Also, note how divert(4) operates its own port number space. This allows
you to run multiple divert(4) userspace programs. For example, if you
choose to, you could run one program on divert port 700 to handle HTTP,
and another program on divert port 800 to handle a different protocol.&lt;/p&gt;

&lt;h2 id=&quot;parts-of-divert4&quot;&gt;Parts of divert(4)&lt;/h2&gt;

&lt;p&gt;divert(4) has roughly three parts to it:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Diverting packets from the kernel to userspace&lt;/li&gt;
  &lt;li&gt;Processing in userspace&lt;/li&gt;
  &lt;li&gt;Processing reinjected packets&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the planets align, I will write the next few blog posts in this series in
that order, where I will dissect each part of divert(4) in detail.&lt;/p&gt;

&lt;p&gt;I also plan to discuss some pitfalls with divert(4) including some bugs that
I’m still trying to fix.&lt;/p&gt;

&lt;p&gt;If this stuff sounds like your cup of tea, stay tuned and don’t divert
your attention away from this blog! (pun obviously intended)  An easy
way to do this is to &lt;a href=&quot;/atom.xml&quot;&gt;subscribe to my RSS feed&lt;/a&gt; and/or
&lt;a href=&quot;https://twitter.com/lteo&quot;&gt;follow me on Twitter&lt;/a&gt; where I will announce
new blog posts.&lt;/p&gt;
</description>
        <pubDate>Tue, 06 Jan 2015 00:02:00 -0500</pubDate>
        <link>https://lteo.net/blog/2015/01/06/dissecting-openbsds-divert-4-part-1-introduction/</link>
        <guid isPermaLink="true">https://lteo.net/blog/2015/01/06/dissecting-openbsds-divert-4-part-1-introduction/</guid>
        
        
        <category>OpenBSD</category>
        
        <category>divert</category>
        
        <category>PF</category>
        
      </item>
    
      <item>
        <title>reallocarray() in OpenBSD: Integer Overflow Detection for Free</title>
        <description>&lt;p&gt;The upcoming &lt;a href=&quot;http://www.openbsd.org/56.html&quot;&gt;OpenBSD 5.6&lt;/a&gt; release introduces a
new libc function called &lt;a href=&quot;http://www.openbsd.org/cgi-bin/man.cgi?query=reallocarray&quot;&gt;reallocarray(3)&lt;/a&gt;
that extends
&lt;a href=&quot;http://www.openbsd.org/cgi-bin/man.cgi?query=realloc&quot;&gt;realloc(3)&lt;/a&gt;
with built-in integer overflow detection.
In this post, I’ll discuss why it’s useful and how it can be used to
fix unsafe code.&lt;/p&gt;

&lt;p&gt;If you’re not familiar with integer overflows or need a refresher,
&lt;a href=&quot;http://undeadly.org/cgi?action=article&amp;amp;sid=20060330071917&quot;&gt;Ray Lai’s Undeadly article on integer overflows&lt;/a&gt;
is a good place to start, especially if you would like to read about it in
the context of OpenBSD.&lt;/p&gt;

&lt;p&gt;In his article which was written in 2006 (way before reallocarray() existed),
Ray wrote, “Unfortunately there is no safe replacement for realloc(3).  To
prevent multiplication overflow, a check must be added”, where the check
is as follows:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newsize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SIZE_T_MAX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;errno&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ENOMEM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;newsize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;realloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newsize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Adding that check before every realloc() call is clunky, not to mention
error-prone. Ray recognized this and recommended the use of a function
called xrealloc() – an early effort that attempts to solve what
reallocarray() now solves. xrealloc() is implemented in parts of the OpenBSD
source tree but is not part of OpenBSD’s libc.&lt;/p&gt;

&lt;p&gt;reallocarray() solves this integer overflow problem more thoroughly by wrapping
around realloc() like this:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;cm&quot;&gt;/*
     * This is sqrt(SIZE_MAX+1), as s1*s2 &amp;lt;= SIZE_MAX
     * if both s1 &amp;lt; MUL_NO_OVERFLOW and s2 &amp;lt; MUL_NO_OVERFLOW
     */&lt;/span&gt;
    &lt;span class=&quot;cp&quot;&gt;#define MUL_NO_OVERFLOW (1UL &amp;lt;&amp;lt; (sizeof(size_t) * 4))
&lt;/span&gt;    
    &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;reallocarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nmemb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nmemb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MUL_NO_OVERFLOW&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MUL_NO_OVERFLOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;nmemb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SIZE_MAX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nmemb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;errno&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ENOMEM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;realloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nmemb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;As you can see, reallocarray() checks to see if the attempted
multiplication will result in an overflow before calling
realloc().  This means you can avoid writing checks every time 
you need to call realloc() - thus gaining integer overflow detection
virtually for free.&lt;/p&gt;

&lt;p&gt;Another advantage over xrealloc() is that reallocarray() is
available in OpenBSD’s libc, which means all C programs in OpenBSD
can easily use it.&lt;/p&gt;

&lt;h2 id=&quot;but-wait-theres-moretrade&quot;&gt;“But wait! There’s more!”™&lt;/h2&gt;

&lt;p&gt;At this point, you may be thinking, “Okay, it’s a wrapper around
realloc(). What’s the big deal?” Well, it &lt;em&gt;is&lt;/em&gt; a wrapper around realloc() with
built-in integer overflow detection, but it’s also so much more than that!&lt;/p&gt;

&lt;p&gt;In my opinion, reallocarray() is useful in three cases:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Replacing unsafe malloc() calls&lt;/li&gt;
  &lt;li&gt;Avoiding unneeded calloc() calls&lt;/li&gt;
  &lt;li&gt;Replacing unsafe realloc() calls&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s look at each case in turn.&lt;/p&gt;

&lt;h2 id=&quot;replacing-unsafe-malloc-calls&quot;&gt;1. Replacing unsafe malloc() calls&lt;/h2&gt;

&lt;p&gt;malloc() as we all know is used to allocate memory for an object with a
given size.&lt;/p&gt;

&lt;p&gt;Its prototype is simple:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;malloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;When a programmer wants to allocate memory for multiple objects, the
obvious but unsafe way is to call malloc() like this:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;malloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;		&lt;span class=&quot;cm&quot;&gt;/* unsafe! potential overflow */&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;malloc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;As noted in the comment, the multiplication of &lt;code class=&quot;highlighter-rouge&quot;&gt;num&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;size&lt;/code&gt; could result
in a potential overflow.&lt;/p&gt;

&lt;p&gt;With reallocarray(), the above malloc(num * size) call can be replaced with:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reallocarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;reallocarray&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Since reallocarray() does the overflow check for you, you’re done!&lt;/p&gt;

&lt;h2 id=&quot;avoiding-unneeded-calloc-calls&quot;&gt;2. Avoiding unneeded calloc() calls&lt;/h2&gt;

&lt;p&gt;Before reallocarray() came along, the recommended way in OpenBSD to fix
malloc(num * size) calls is to replace them with calloc() instead:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;calloc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;calloc() comes with built-in integer overflow detection, at least on
OpenBSD.  But calloc() also zeroes out the allocated memory.
There are times when you don’t need or want to zero out the memory,
for example if you’re going to intialize the entire allocated memory
immediately after the calloc() call anyway.&lt;/p&gt;

&lt;p&gt;This is where reallocarray() shines again. Just like the above case,
the call can be replaced with:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reallocarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;reallocarray&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This allows the memory to be allocated without the cost of zeroing the
memory, while at the same time detecting integer overflows. Neat, huh?&lt;/p&gt;

&lt;h2 id=&quot;replacing-unsafe-realloc-calls&quot;&gt;3. Replacing unsafe realloc() calls&lt;/h2&gt;

&lt;p&gt;As noted by Ray, there is no safe replacement for realloc().  realloc()
is frequently called like this:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;realloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;	&lt;span class=&quot;cm&quot;&gt;/* unsafe! potential overflow */&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;realloc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Like the malloc(num * size) case, the multiplication occurs without a
check, which could result in an integer overflow.&lt;/p&gt;

&lt;p&gt;Replacing the unsafe realloc() call with reallocarray() is very
straightforward:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reallocarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;reallocarray&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Bam! Integer overflow detection for free. :-)&lt;/p&gt;

&lt;h2 id=&quot;whats-next&quot;&gt;What’s next?&lt;/h2&gt;

&lt;p&gt;reallocarray() is a non-portable OpenBSD extension. But thanks
to its &lt;a href=&quot;http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/lib/libc/stdlib/reallocarray.c&quot;&gt;liberal ISC license&lt;/a&gt;, you are free to use it with your own code as
long as you adhere to the terms of the license.  So not only do you get
integer overflow for free, you get the code for free as well!&lt;/p&gt;

&lt;p&gt;Due to its usefulness, at the time of writing,
&lt;a href=&quot;http://freshbsd.org/search?project=openbsd&amp;amp;q=reallocarray&quot;&gt;more than 130 commits&lt;/a&gt;
have been made to the OpenBSD tree that involve reallocarray(). 
&lt;a href=&quot;http://freshbsd.org/search?committer=deraadt&amp;amp;project=openbsd&amp;amp;q=reallocarray&quot;&gt;Theo himself&lt;/a&gt;
has been on a merciless reallocarray() rampage this past month. :-)
Some of the fixes have been easy and obvious; others have been very
tricky. The audit continues…&lt;/p&gt;

&lt;p&gt;Happy hacking!&lt;/p&gt;
</description>
        <pubDate>Tue, 28 Oct 2014 00:08:00 -0400</pubDate>
        <link>https://lteo.net/blog/2014/10/28/reallocarray-in-openbsd-integer-overflow-detection-for-free/</link>
        <guid isPermaLink="true">https://lteo.net/blog/2014/10/28/reallocarray-in-openbsd-integer-overflow-detection-for-free/</guid>
        
        
        <category>OpenBSD</category>
        
        <category>programming</category>
        
        <category>security</category>
        
      </item>
    
      <item>
        <title>A Sneak Peek at the Upcoming OpenBSD 5.6 Release</title>
        <description>&lt;p&gt;In exactly one month from now, OpenBSD 5.6 will be released on November
1, 2014 – like clockwork as always. But, you don’t have to wait until
November 1 if you want it earlier, because &lt;a href=&quot;https://openbsdstore.com&quot;&gt;pre-orders are now up at the brand new OpenBSD store&lt;/a&gt;!
&lt;img src=&quot;/images/CD56.gif&quot; alt=&quot;OpenBSD 5.6 CD cover&quot; class=&quot;align-right&quot; /&gt;&lt;/p&gt;

&lt;p&gt;OpenBSD 5.6 is of course the first OpenBSD release with
&lt;a href=&quot;http://www.libressl.org&quot;&gt;LibreSSL&lt;/a&gt;, the now-famous fork of the OpenSSL
library. But while LibreSSL is an important milestone for OpenBSD, there
are many other things in the OpenBSD 5.6 release that warrant attention as
well.&lt;/p&gt;

&lt;p&gt;The developers are still busy preparing the &lt;a href=&quot;http://www.openbsd.org/56.html&quot;&gt;OpenBSD 5.6 release notes&lt;/a&gt;.
Meanwhile, if you’re curious
about what’s new in OpenBSD 5.6, you can get a sneak peek from
various places on the Internet if you know where to look! I would like
to highlight three places in particular.&lt;/p&gt;

&lt;h2 id=&quot;plus56html&quot;&gt;plus56.html&lt;/h2&gt;

&lt;p&gt;The first place to look at is the &lt;a href=&quot;http://www.openbsd.org/plus56.html&quot;&gt;big list of changes between OpenBSD 5.5 and 5.6&lt;/a&gt;
that has been painstakingly compiled by Brett Mahar (brett@) throughout
the development cycle. I think Brett does an amazing job of collating
all this information together for every release! I have no idea how he
manages to go through every source-changes@ post to produce this list so
consistently. :)&lt;/p&gt;

&lt;h2 id=&quot;hackathon-reports&quot;&gt;Hackathon reports&lt;/h2&gt;

&lt;p&gt;The second place that you can look at are the hackathon reports written
by the developers. Two &lt;a href=&quot;http://www.openbsd.org/hackathons.html&quot;&gt;hackathons&lt;/a&gt;
were held between OpenBSD 5.5 and 5.6: m2k14 in Marrakech, Morocco
(April 17-22, 2014) and g2k14 in Ljubljana, Slovenia (July 8-14, 2014).&lt;/p&gt;

&lt;p&gt;The hackathon reports are published on the &lt;a href=&quot;http://www.undeadly.org/&quot;&gt;OpenBSD Journal website&lt;/a&gt;.
I have listed them here:&lt;/p&gt;

&lt;h3 id=&quot;m2k14-hackathon-reports&quot;&gt;m2k14 hackathon reports&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.undeadly.org/cgi?action=article&amp;amp;sid=20140421052218&amp;amp;mode=expanded&quot;&gt;m2k14: henning@ - VLAN improvements&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.undeadly.org/cgi?action=article&amp;amp;sid=20140422171701&amp;amp;mode=expanded&quot;&gt;m2k14: schwarze@ - Faster and more capable whatis(1)/apropos(1)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.undeadly.org/cgi?action=article&amp;amp;sid=20140425115340&amp;amp;mode=expanded&quot;&gt;m2k14: sthen@ - Stuart Henderson on Triage&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.undeadly.org/cgi?action=article&amp;amp;sid=20140430155940&amp;amp;mode=expanded&quot;&gt;m2k14: reyk@ - Privilege Separated Key Handling added to relayd(8) and smtpd(8)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.undeadly.org/cgi?action=article&amp;amp;sid=20140429121423&amp;amp;mode=expanded&quot;&gt;m2k14: krw@ - Ken Westerback on Installation, Disklabel Bugs, and Experiments in Sleep Deprivation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.undeadly.org/cgi?action=article&amp;amp;sid=20140501185019&amp;amp;mode=expanded&quot;&gt;m2k14: aja@ - Antoine Jacoutot on GNOME, Heimdal, and Further Heartbleed Fallout&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.undeadly.org/cgi?action=article&amp;amp;sid=20140502092427&amp;amp;mode=expanded&quot;&gt;m2k14: jasper@ on puppet, misc ports and Octeon&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;g2k14-hackathon-reports&quot;&gt;g2k14 hackathon reports&lt;/h3&gt;

&lt;p&gt;As for the g2k14 hackathon reports, thanks to Undeadly editor tbert,
they are all listed in a single article on the OpenBSD Journal! Here it
is:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://undeadly.org/cgi?action=article&amp;amp;sid=20140808085943&quot;&gt;g2k14: Reports Roundup&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;slides-and-papers&quot;&gt;Slides and papers&lt;/h2&gt;

&lt;p&gt;The third place to look at are the slides and papers written by OpenBSD
developers. Two notable events that took place between OpenBSD 5.5 and 5.6
are &lt;a href=&quot;http://www.bsdcan.org/2014/&quot;&gt;BSDCan 2014&lt;/a&gt; and
&lt;a href=&quot;http://2014.eurobsdcon.org/&quot;&gt;EuroBSDCon 2014&lt;/a&gt;. You can check out
OpenBSD-related slides and papers for these events and more at:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;http://www.openbsd.org/events.html&lt;/li&gt;
  &lt;li&gt;http://www.openbsd.org/papers/&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.undeadly.org/cgi?action=article&amp;amp;sid=20140929075248&amp;amp;mode=expanded&quot;&gt;OpenBSD Journal: EuroBSDCon 2014 Papers Online&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you can see there’s a wealth of information out there on what’s new
in OpenBSD 5.6 that really shows the amazing amount of work that is done
for every release.&lt;/p&gt;

&lt;p&gt;I would like to end by highlighting &lt;a href=&quot;https://twitter.com/_mcbride/status/511845076314509312&quot;&gt;this tweet/retweet&lt;/a&gt;
from Ryan McBride (mcbride@), which I think summarizes the awesomeness of the
OpenBSD release process like no other:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/mcbride-openbsd-releases.png&quot; alt=&quot;mcbride on OpenBSD releases&quot; class=&quot;align-center&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So remember to &lt;a href=&quot;https://openbsdstore.com&quot;&gt;pre-order OpenBSD 5.6&lt;/a&gt; or
&lt;a href=&quot;http://www.openbsd.org/donations.html&quot;&gt;donate to OpenBSD&lt;/a&gt; to keep this awesome
project alive and thriving!&lt;/p&gt;

</description>
        <pubDate>Wed, 01 Oct 2014 00:22:00 -0400</pubDate>
        <link>https://lteo.net/blog/2014/10/01/a-sneak-peek-at-the-upcoming-openbsd-5-dot-6-release/</link>
        <guid isPermaLink="true">https://lteo.net/blog/2014/10/01/a-sneak-peek-at-the-upcoming-openbsd-5-dot-6-release/</guid>
        
        
        <category>OpenBSD</category>
        
      </item>
    
      <item>
        <title>A small MPLS test network built with OpenBSD</title>
        <description>&lt;p&gt;A few months ago, I wrote a diff to
&lt;a href=&quot;http://marc.info/?l=openbsd-cvs&amp;amp;m=137143632015922&amp;amp;w=2&quot;&gt;simplify the calculation of ICMP extension header checksums&lt;/a&gt;
in the OpenBSD kernel.  It so happened that the code is only used by the
OpenBSD MPLS subsystem.  I didn’t have access to an OpenBSD-based MPLS
network at the time, nor was I familiar with MPLS in general; so in the
spirit of the OpenBSD hacker mantra “shut up and hack,” I set out to
build a small MPLS test network to test my diff. :)&lt;/p&gt;

&lt;p&gt;This blog post documents my experience setting up the MPLS network;
hopefully someone out there will find it useful.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WARNING:&lt;/strong&gt; I set up this network a few months ago and it’s very likely
that I have forgotten a bunch of details. Also shortly after I committed
my diff, one of the nodes in the network died from a power
brownout/surge during a crazy thunderstorm and I haven’t had a chance to
rebuild the network.  So most of this blog post is based on my notes and
fuzzy memory. You’ve been warned!&lt;/p&gt;

&lt;p&gt;If you’re not familiar with OpenBSD’s implementation of MPLS, you’ll
first need to read Claudio Jeker’s (claudio@) EuroBSDCon 2011 paper:
&lt;a href=&quot;http://2011.eurobsdcon.org/papers/jeker/MPLS.pdf&quot;&gt;Demystifying MPLS: The MPLS Framework in OpenBSD&lt;/a&gt; (PDF).
I designed my small (tiny?) test network as a subset of Claudio’s
example network (see page 6 of the PDF for a diagram of his network).
To minimize variables, I used the exact same IP addresses and MPLS
labels where possible. If you’re just getting your feet wet with MPLS on
OpenBSD like I was, I suggest doing the same.&lt;/p&gt;

&lt;p&gt;Here’s how my simple four-node test network looked like:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/mpls.png&quot; alt=&quot;A small MPLS test network&quot; class=&quot;align-center&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If you’re not familiar with MPLS terminology, P is Provider, and PE is
Provider Edge. What they are is explained in the paper. :)&lt;/p&gt;

&lt;p&gt;The goal is to get the Customer at 192.168.237.4 talking to the IP
address behind PE2 (192.168.237.242).&lt;/p&gt;

&lt;p&gt;By the way, if you feel like trying this out, I highly recommend using
OpenBSD -current (or &lt;a href=&quot;http://www.openbsd.org/54.html&quot;&gt;OpenBSD 5.4&lt;/a&gt; when
it is released in November 1, 2013); there were a bunch of
&lt;a href=&quot;http://www.openbsd.org/cgi-bin/man.cgi?query=ldpd&quot;&gt;ldpd(8)&lt;/a&gt; commits
made at the t2k13 hackathon in June 2013 so it helps to be as up-to-date
as possible.&lt;/p&gt;

&lt;p&gt;Here’s how I set up each system. PE1, P1, and PE2 will all run
&lt;a href=&quot;http://www.openbsd.org/cgi-bin/man.cgi?query=ospfd&quot;&gt;ospfd(8)&lt;/a&gt;
and
&lt;a href=&quot;http://www.openbsd.org/cgi-bin/man.cgi?query=ldpd&quot;&gt;ldpd(8)&lt;/a&gt;.
PE1 and PE2 will run
&lt;a href=&quot;http://www.openbsd.org/cgi-bin/man.cgi?query=bgpd&quot;&gt;bgpd(8)&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;customer-setup&quot;&gt;Customer Setup&lt;/h2&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ifconfig rl0 192.168.237.4/28
route add default 192.168.237.2
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;pe1-setup&quot;&gt;PE1 Setup&lt;/h2&gt;

&lt;p&gt;PE1 interface config:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ifconfig em0 rdomain 1
ifconfig em0 192.168.237.2/28
route -T1 add default 192.168.237.1
ifconfig lo1 10.42.42.1/32
ifconfig em1 10.42.0.1/24 mpls
ifconfig mpe0 rdomain 1
ifconfig mpe0 mplslabel 666
ifconfig mpe0 192.168.237.2/32
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;PE1 /etc/ospfd.conf:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;router-id 10.42.42.1
area 0.0.0.0 {
    interface em1
    interface lo1
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;PE1 /etc/ldpd.conf:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;router-id 10.42.42.1
interface em1
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;PE1 /etc/bgpd.conf:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;router-id 10.42.42.1
AS 3.10
rdomain 1 {
    descr &quot;CUSTOMER1&quot;
    rd 3.10:1
    import-target rt 3.10:1
    export-target rt 3.10:1
    depend on mpe0
    network inet connected
    network 0.0.0.0/0
}
group ibgp {
    announce IPv4 unicast
    announce IPv4 vpn
    remote-as 3.10
    local-address 10.42.42.1
    neighbor 10.42.42.2 {
        descr PE2
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;pe2-setup&quot;&gt;PE2 Setup&lt;/h2&gt;

&lt;p&gt;PE2 interface config:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ifconfig rl0 rdomain 1
ifconfig rl0 192.168.237.242/28
ifconfig lo1 10.42.42.2/32
ifconfig rl1 10.42.6.2/24 mpls
ifconfig mpe0 rdomain 1
ifconfig mpe0 mplslabel 666
ifconfig mpe0 192.168.237.242/32
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;PE2 /etc/ospfd.conf:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;router-id 10.42.42.2
area 0.0.0.0 {
    interface rl1
    interface lo1
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;PE2 /etc/ldpd.conf:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;router-id 10.42.42.2
interface rl1
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;PE2 /etc/bgpd.conf:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;router-id 10.42.42.2
AS 3.10
rdomain 1 {
    descr &quot;CUSTOMER1&quot;
    rd 3.10:1
    import-target rt 3.10:1
    export-target rt 3.10:1
    depend on mpe0
    network inet connected
}
group ibgp {
    announce IPv4 unicast
    announce IPv4 vpn
    remote-as 3.10
    route-reflector
    local-address 10.42.42.2
    neighbor 10.42.42.1 {
        descr PE1
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;p1-setup&quot;&gt;P1 Setup&lt;/h2&gt;

&lt;p&gt;P1 interface config:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ifconfig lo1 10.42.21.1/32
ifconfig fxp2 10.42.0.2/24 mpls
ifconfig fxp3 10.42.6.1/24 mpls
sysctl net.inet.ip.forwarding=1
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;P1 /etc/ospfd.conf:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;router-id 10.42.21.1
area 0.0.0.0 {
    interface fxp2
    interface fxp3
    interface lo1
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;P1 /etc/ldpd.conf:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;router-id 10.42.21.1
interface fxp2
interface fxp3
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;putting-it-all-together&quot;&gt;Putting it all together&lt;/h2&gt;

&lt;p&gt;Now that all the config files are in place, it’s time to start up the
daemons and give the network a whirl.  At this point, I really wish I
still have the test network so that I can provide specific instructions
on starting the daemons and confirming that they work as expected.&lt;/p&gt;

&lt;p&gt;But I do recall an important bit of information.  The daemons need to be
started in this order: (1) ospfd, (2) ldpd, and (3) bgpd.
(see the end of &lt;a href=&quot;http://marc.info/?l=openbsd-misc&amp;amp;m=130278893121806&amp;amp;w=2&quot;&gt;Claudio’s misc@ post&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;So if I recall correctly, this is what I did:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Set up the network as above.&lt;/li&gt;
  &lt;li&gt;Start ospfd on PE1, P1, and PE2.&lt;/li&gt;
  &lt;li&gt;Test that PE1 and ping P1 and P1 can ping PE2.&lt;/li&gt;
  &lt;li&gt;Start ldpd on PE1, P1, and PE2.&lt;/li&gt;
  &lt;li&gt;Start bgpd on PE1 and PE2.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If all goes well, the Customer system should now be able to access the
IP address behind PE2 (192.168.237.242) over the MPLS network.&lt;/p&gt;

&lt;p&gt;If I ever set up the test network again, I’ll update this blog post with
more specific details.&lt;/p&gt;
</description>
        <pubDate>Tue, 03 Sep 2013 00:01:00 -0400</pubDate>
        <link>https://lteo.net/blog/2013/09/03/a-small-mpls-test-network-built-with-openbsd/</link>
        <guid isPermaLink="true">https://lteo.net/blog/2013/09/03/a-small-mpls-test-network-built-with-openbsd/</guid>
        
        
        <category>OpenBSD</category>
        
        <category>MPLS</category>
        
      </item>
    
      <item>
        <title>Reinstalling all your OpenBSD packages with pkg_add's fuzzy matching feature</title>
        <description>&lt;p&gt;Last week, in classic Lawrence fashion, I somehow hosed my OpenBSD ports
tree while doing some crazy experiments.  It got to the point where
&lt;em&gt;make package&lt;/em&gt; would fail on certain ports like archivers/xz; if I
tried it on those ports, I would get this funky error:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Error: /usr/ports/pobj/xz-5.0.4/fake-amd64/usr/local/share/locale/cs/LC_MESSAGES/xz.mo does not exist
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Since MO files are related to &lt;a href=&quot;http://www.gnu.org/software/gettext/manual/gettext.html#Binaries&quot;&gt;GNU gettext&lt;/a&gt;,
I tried to delete my gettext package along with all its dependencies and
reinstalling them but it still produced the same error.&lt;/p&gt;

&lt;p&gt;At that point, I decided to use a bigger hammer.  The plan was to
uninstall all packages and then reinstall them.  That sounds tedious,
but then I remembered reading that OpenBSD’s pkg_add tool has a “fuzzy
adding” feature.  I have never tried it before, but OpenBSD’s tools tend
to Just Work&lt;sup&gt;TM&lt;/sup&gt; so I decided to give it a shot.&lt;/p&gt;

&lt;p&gt;The two relevant pkg_add flags are -l and -z, and they are described on
the &lt;a href=&quot;http://www.openbsd.org/cgi-bin/man.cgi?query=pkg_add&quot;&gt;pkg_add man page&lt;/a&gt; as follows:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-l file  Installs packages from the raw output of pkg_info(1), as saved
	 in file.  Generally, use with pkg_info -m &amp;gt;file, to reproduce an
	 installation from machine to machine.  With -z and -l pkg_add
	 will try its best to reproduce the installation, even if the
	 version numbers don't quite match and even if some packages
	 cannot be found.

-z	 Fuzzy package addition: pkg_add should do its best to match
	 package names passed on the command line, even if the versions
	 don't match and it will proceed even if some packages can't be
	 found.
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;That was pretty self-explanatory.  The &lt;em&gt;pkg_info -m&lt;/em&gt; command that is
mentioned in the text is used to show only packages that have been
manually installed.  For example, if you install git with &lt;em&gt;pkg_add git&lt;/em&gt;
and it pulls down rsync as a dependency, git is considered the manually
installed package while rsync is the automatically installed package.&lt;/p&gt;

&lt;p&gt;So, armed with that knowledge, I proceeded with saving a list of
manually installed packages:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pkg_info -m &amp;gt;packages.txt
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And then came the scary step of uninstalling &lt;em&gt;ALL&lt;/em&gt; packages!&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pkg_delete -X
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Next, I reinstalled all my packages using pkg_add’s fuzzy adding
feature:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;export PKG_PATH=${my_favorite_mirror}
pkg_add -z -l packages.txt
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And off it went!  It worked just like the usual &lt;em&gt;pkg_add -ui&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;At the end of the entire process, all my original packages have been
reinstalled.  And &lt;em&gt;make package&lt;/em&gt; on those problematic ports works again!&lt;/p&gt;

&lt;p&gt;As mentioned in the man page excerpt above, this feature can be used to
quickly install the same set of packages on similar systems.  For
example, if you always install a selected set of packages on a laptop,
you can quickly install the same set of packages on another laptop with
this technique.&lt;/p&gt;

&lt;p&gt;It’s always fun to find useful time-saving features in the excellent
OpenBSD pkg_* tools.  Hope this will help someone out there!&lt;/p&gt;
</description>
        <pubDate>Thu, 08 Nov 2012 00:01:00 -0500</pubDate>
        <link>https://lteo.net/blog/2012/11/08/reinstalling-all-your-openbsd-packages-with-pkg-adds-fuzzy-matching-feature/</link>
        <guid isPermaLink="true">https://lteo.net/blog/2012/11/08/reinstalling-all-your-openbsd-packages-with-pkg-adds-fuzzy-matching-feature/</guid>
        
        
        <category>OpenBSD</category>
        
      </item>
    
      <item>
        <title>An easy way to test your Snort rules</title>
        <description>&lt;p&gt;&lt;strong&gt;UPDATE&lt;/strong&gt;: An updated version of this blog post is
&lt;a href=&quot;/blog/2016/10/26/testing-your-snort-rules-redux/&quot;&gt;now available&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Have you ever wondered if your Snort rules are actually working after
you have set up &lt;a href=&quot;http://www.snort.org/&quot;&gt;Snort&lt;/a&gt;?&lt;/p&gt;

&lt;p&gt;You may have seen this line in your log file…&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Oct 24 22:24:20 foo snort[4520]: 1745 Snort rules read
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;…but you may still be wondering if Snort can actually trigger alerts.&lt;/p&gt;

&lt;p&gt;You could always let Snort run on a live network and hope that something
shows up that would trigger an alert, but you might be waiting a long time,
especially if you’ve accidentally misconfigured Snort so rules aren’t even
being loaded in the first place!&lt;/p&gt;

&lt;p&gt;Here’s a quick and easy way to test your Snort installation to confirm
that it has loaded the Snort rules and can trigger alerts. Please note
that the emphasis is on &lt;em&gt;quick&lt;/em&gt; and &lt;em&gt;easy&lt;/em&gt;; this is not meant to be a
comprehensive guide to test each and every Snort rule that you have
loaded!&lt;/p&gt;

&lt;p&gt;The goal of this simple guide is to help you:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Confirm that you have actually loaded some rules&lt;/li&gt;
  &lt;li&gt;Ensure that your Snort setup can actually trigger alerts&lt;/li&gt;
  &lt;li&gt;Confirm that you are logging alerts to the right file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I assume that you are using the &lt;a href=&quot;http://www.snort.org/snort-rules/&quot;&gt;official Snort rules&lt;/a&gt;
from Sourcefire for this test, and that you have loaded all the rules as
a starting point.&lt;/p&gt;

&lt;p&gt;A lot of the rules are complex and there is no easy way to test each
and every one of them.  So what I have done is to pick two simple rules
that you can use to test Snort by making it trigger both an outbound and
inbound alert. Triggering an outbound alert is much easier so let’s
start there.&lt;/p&gt;

&lt;h2 id=&quot;triggering-an-outbound-alert&quot;&gt;Triggering an outbound alert&lt;/h2&gt;

&lt;p&gt;To trigger an outbound alert, simply run the following command from the
host running Snort:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ftp http://$EXTERNAL_HOST/cmd.exe
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Where $EXTERNAL_HOST is a host that you own or have permission to
access for this exercise.  Also, if for some reason you aren’t using a
BSD system, you can always use wget or curl instead of ftp. :)&lt;/p&gt;

&lt;p&gt;It does not matter if cmd.exe actually exists on that external host;
Snort will see that you are attempting to download cmd.exe and should
trigger the following Snort alert:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[**] [1:1002:15] WEB-IIS cmd.exe access [**]
[Classification: Web Application Attack] [Priority: 1] 
10/24-22:25:13.937084 x.x.x.x:42200 -&amp;gt; y.y.y.y:80
TCP TTL:56 TOS:0x0 ID:15236 IpLen:20 DgmLen:288 DF
***AP*** Seq: 0x1B16EAE9  Ack: 0x905BABE1  Win: 0x43E0  TcpLen: 32
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;On an &lt;a href=&quot;http://www.openbsd.org/&quot;&gt;OpenBSD&lt;/a&gt; system, that alert would appear
in /var/snort/log/alert if you used the default settings in the Snort
package.&lt;/p&gt;

&lt;h2 id=&quot;triggering-an-inbound-alert&quot;&gt;Triggering an inbound alert&lt;/h2&gt;

&lt;p&gt;If you would like to test an inbound alert, it is slightly more
complicated but still doable.  You will first need to install a simple
webserver. I like to install a simple lightweight webserver like
&lt;a href=&quot;http://acme.com/software/thttpd/&quot;&gt;thttpd&lt;/a&gt;.
On OpenBSD you can do this by setting PKG_PATH and then run:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pkg_add thttpd
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And then make thttpd listen in a dummy directory like:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mkdir /tmp/xyz
cd /tmp/xyz
thttpd -D -l /dev/stdout
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Then, from an &lt;em&gt;external&lt;/em&gt; host, run the following command:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ftp http://x.x.x.x/NessusTest
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;x.x.x.x in the above command is the “public” IP address of the Snort
host.  This should trigger the following alert:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[**] [1:2585:5] WEB-MISC nessus 2.x 404 probe [**]
[Classification: Attempted Information Leak] [Priority: 2] 
10/24-22:26:06.355207 y.y.y.y:63514 -&amp;gt; x.x.x.x:80
TCP TTL:64 TOS:0x0 ID:2995 IpLen:20 DgmLen:362 DF
***AP*** Seq: 0x44C5AC41  Ack: 0x4D9B5997  Win: 0x43E0  TcpLen: 32
[Xref =&amp;gt; http://cgi.nessus.org/plugins/dump.php3?id=10386]
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;When you are done with the test, make sure to stop thttpd by pressing
Ctrl+C to break out of it.&lt;/p&gt;

&lt;p&gt;That’s all there is to it!  Before deploying Snort in an actual
production environment, please remember to carefully review your Snort
rules to pick those that are applicable to your environment, and tweak
your Snort configuration file accordingly.&lt;/p&gt;

&lt;p&gt;Have fun!&lt;/p&gt;
</description>
        <pubDate>Fri, 26 Oct 2012 22:14:00 -0400</pubDate>
        <link>https://lteo.net/blog/2012/10/26/an-easy-way-to-test-your-snort-rules/</link>
        <guid isPermaLink="true">https://lteo.net/blog/2012/10/26/an-easy-way-to-test-your-snort-rules/</guid>
        
        
        <category>Snort</category>
        
      </item>
    
  </channel>
</rss>
