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

<channel>
	<title>PF &#8211; Johnny Morano&#039;s Tech Articles</title>
	<atom:link href="https://jmorano.moretrix.com/tag/pf/feed/" rel="self" type="application/rss+xml" />
	<link>https://jmorano.moretrix.com</link>
	<description>Ramblings of an old-fashioned space cowboy</description>
	<lastBuildDate>Sat, 09 Apr 2022 07:11:26 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.2</generator>

<image>
	<url>https://jmorano.moretrix.com/wp-content/uploads/2022/04/cropped-jmorano_emblem-32x32.png</url>
	<title>PF &#8211; Johnny Morano&#039;s Tech Articles</title>
	<link>https://jmorano.moretrix.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Time based network access control on OpenBSD</title>
		<link>https://jmorano.moretrix.com/2022/03/time-based-network-access-control-on-openbsd/</link>
					<comments>https://jmorano.moretrix.com/2022/03/time-based-network-access-control-on-openbsd/#respond</comments>
		
		<dc:creator><![CDATA[Johnny Morano]]></dc:creator>
		<pubDate>Tue, 01 Mar 2022 13:14:17 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[OpenBSD]]></category>
		<category><![CDATA[PF]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[SysAdmin]]></category>
		<category><![CDATA[UNIX]]></category>
		<guid isPermaLink="false">https://jmorano.moretrix.com/?p=1293</guid>

					<description><![CDATA[Time based ACL (access control lists) features do not exist in BSD&#8217;s packet filter (PF). Having your network&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Time based ACL (access control lists) features do not exist in BSD&#8217;s packet filter (<code>PF</code>). Having your network &#8220;shut down&#8221; at certain times (for instance, allow certain network ranges or specific IP addresses only during &#8220;business hours&#8221; or a specific time range), can be achieved with a simple <code>PF</code> table and a <code>cronjob</code>.</p>



<p>First, let&#8217;s set up the <code>PF</code> table which will control the traffic. Add the following to your <code>/etc/pf.conf</code> :</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># add time block table
table &lt;time_block> { } persist
</pre>



<p>Next, create a <code>PF</code> rule which block traffic for all entries in the <code>time_block</code> table:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># block all CIDR addresses in the time block table
block in quick log from &lt;time_block> to any
</pre>



<p>Since the <code>time_block</code> table is still empty, no traffic is actually blocked.</p>



<p>The last thing to implement, is periodically manipulating the <code>time_block</code> table. This could be done by creating two <code>cronjobs</code>:</p>



<ol class="wp-block-list"><li>allow traffic at the beginning of &#8220;business hours&#8221;</li><li>block traffic at the end of &#8220;business hours&#8221;</li></ol>



<pre class="EnlighterJSRAW" data-enlighter-language="shell" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">crontab -e
# Allow traffic
0 7 * * * /usr/local/scripts/allow_employees.sh > /dev/null 2>&amp;1
# Block traffic
0 17 * * * /usr/local/scripts/block_employees.sh > /dev/null 2>&amp;1
﻿</pre>



<p>The <code>allow_employees.sh</code> script will allow certain network ranges by ensuring those are removed from the <code>time_block</code> table:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="shell" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#!/bin/sh

/sbin/pfctl -Td -t time_block 10.1.0.0/24
/sbin/pfctl -Td -t time_block 10.2.0.0/24
</pre>



<p>The <code>block_employees.sh</code> script will do the exact opposite: it will add ranges to the <code>time_block</code> table so that their network access will be blocked by the firewall:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="shell" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#!/bin/sh

/sbin/pfctl -Ta -t time_block 10.1.0.0/24
/sbin/pfctl -Ta -t time_block 10.2.0.0/24
</pre>



<p>Finally deploy your new PF rules (first test them!)</p>



<pre class="EnlighterJSRAW" data-enlighter-language="shell" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pfctl -nf /etc/pf.conf
pfctl -f /etc/pf.conf</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://jmorano.moretrix.com/2022/03/time-based-network-access-control-on-openbsd/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Block countries on OpenBSD using pf</title>
		<link>https://jmorano.moretrix.com/2022/03/block-countries-on-openbsd-using-pf/</link>
					<comments>https://jmorano.moretrix.com/2022/03/block-countries-on-openbsd-using-pf/#respond</comments>
		
		<dc:creator><![CDATA[Johnny Morano]]></dc:creator>
		<pubDate>Tue, 01 Mar 2022 12:28:09 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[OpenBSD]]></category>
		<category><![CDATA[PF]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[SysAdmin]]></category>
		<category><![CDATA[UNIX]]></category>
		<guid isPermaLink="false">https://jmorano.moretrix.com/?p=1291</guid>

					<description><![CDATA[Same as in the previous article, full countries can be easily blocked on OpenBSD firewall using the pf&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Same as in the <a href="https://jmorano.moretrix.com/2022/03/block-countries-using-iptables/" data-type="post" data-id="1285">previous article</a>, full countries can be easily blocked on OpenBSD firewall using the <code>pf</code> command and <a rel="noreferrer noopener" href="https://ipdeny.com/" target="_blank">https://ipdeny.com/</a>.</p>



<p>The zone files provided by <a rel="noreferrer noopener" href="https://ipdeny.com/" target="_blank">https://ipdeny.com/</a> need to be stored locally. A simple way to achieve this is by having a <code>cronjob</code> downloading those periodically (for instance once per day):</p>



<pre class="EnlighterJSRAW" data-enlighter-language="shell" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#!/bin/sh

# download the latest country zone files
curl -s https://www.ipdeny.com/ipblocks/data/countries/ru.zone > /etc/ru.zone
curl -s https://www.ipdeny.com/ipblocks/data/countries/cn.zone > /etc/cn.zone
﻿</pre>



<p>We store them directly to <code>/etc</code> in the above example.</p>



<p>In the <code>/etc/pf.conf</code>, first add a table based on the files you have generated with the above statements:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># add a bad hosts table based on local disk text files
# one CIDR per line
table &lt;badhosts> persist file "/etc/ru.zone" file "/etc/cn.zone"
</pre>



<p>In the above example, we have created a table called <code>badhosts</code> based on two local files.</p>



<p>Finally we need some rules which actually blocks from and to these network ranges, an example <code>PF</code> block rule could be:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># block bad IP addresses
block from &lt;badhosts> to any
block from any to &lt;badhosts>
</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://jmorano.moretrix.com/2022/03/block-countries-on-openbsd-using-pf/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
