<?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>永久 &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/%E6%B0%B8%E4%B9%85/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Sat, 10 Jul 2021 08:24:29 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>永久 &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>讓 fail2ban 可以設定永久 ban 名單</title>
		<link>https://noter.tw/54/%e8%ae%93-fail2ban-%e5%8f%af%e4%bb%a5%e8%a8%ad%e5%ae%9a%e6%b0%b8%e4%b9%85-ban-%e5%90%8d%e5%96%ae/</link>
					<comments>https://noter.tw/54/%e8%ae%93-fail2ban-%e5%8f%af%e4%bb%a5%e8%a8%ad%e5%ae%9a%e6%b0%b8%e4%b9%85-ban-%e5%90%8d%e5%96%ae/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Tue, 25 Sep 2018 16:18:46 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[fail2ban]]></category>
		<category><![CDATA[jail.conf]]></category>
		<category><![CDATA[永久]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=54</guid>

					<description><![CDATA[<p>小蛙重啟 fail2ban 的時候，發現重啟要非常久，一開始不怎麼了解 fail2ban 的運作，後來從 /var/log/fail2ban.log 看見，每次重啟都會先把所有的 IP unban，重&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/54/%e8%ae%93-fail2ban-%e5%8f%af%e4%bb%a5%e8%a8%ad%e5%ae%9a%e6%b0%b8%e4%b9%85-ban-%e5%90%8d%e5%96%ae/" data-wpel-link="internal">讓 fail2ban 可以設定永久 ban 名單</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>小蛙重啟 fail2ban 的時候，發現重啟要非常久，一開始不怎麼了解 fail2ban 的運作，後來從 /var/log/fail2ban.log 看見，每次重啟都會先把所有的 IP unban，重啟完成之後再去掃描 log，把符合的加入 ban 清單中。 </p>



<span id="more-54"></span>



<p>小蛙一直有一個疑問，貌似 fail2ban 重啟之後加回 ban 清單的 IP 列表，是重新從目前的 log 中掃出來的，對小蛙來說，這些「不速之客」就是應該被永久 ban 掉，如果不小心自己被 ban 了，也可以透過&nbsp;<a href="https://noter.tw/259/%e8%87%aa%e5%b7%b1%e8%a2%ab-fail2ban-%e5%b0%81%e9%8e%96%e7%9a%84%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%b3%95/" target="_blank" rel="noreferrer noopener" data-wpel-link="internal">自己被 Fail2ban 封鎖的解決方法</a> 來解救自己，但是 fail2ban 重啟或重開機之後，得到的 ban 清單卻是當下的 log，而小蛙的 log 會自己截斷，也就是幾天前、上周、上月 &#8230; 等被 ban 的 IP，因為重開機之後又起死回生了 &#8230; ( 小蛙沒有實際測試，不過看起來沒有地方儲存這些資料，有很大的這種可能性 )</p>



<p>於是在網路上找到了這篇 <a rel="noreferrer noopener nofollow external" href="https://arno0x0x.wordpress.com/2015/12/30/fail2ban-permanent-persistent-bans/" target="_blank" data-wpel-link="external" class="wpel-icon-right">Configure Fail2Ban for permanent and persistent bans<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 教學，這裡強調需要修改的東西很簡單，小蛙實際做了也很簡單，閱讀英文沒有問題的朋友可直接點過去，這邊小蛙記錄作法跟遇到的問題。</p>



<h2 class="para wp-block-heading">設定永久封鎖</h2>



<p>以小蛙的 Ubuntu 16.04 為例，fail2ban 設定檔放在 /etc/fail2ban/jail.conf，在&nbsp;<code>[DEFAULT]</code> 以下，直到下一個區塊(例如：<code>[sshd]</code>) 之前都是屬於上面區塊的設定，而下方區塊的設定會覆蓋上方區塊的，例如：<code>[DEFAULT]</code>設定了 bantime = -1，但是在&nbsp;<code>[sshd]</code>區塊中又設定 bantime = 60，則 sshd 服務中會以該區塊地為主，總之<code>[DEFAULT]</code>就是所有的基礎設定，要設定永久封鎖很簡單，只要設定 <code>bantime = -1</code> 就可以了。 </p>



<h2 class="para wp-block-heading">建立永久封鎖清單</h2>



<p>上面提到說只要重啟或是重新開機，所有的封鎖清單就會 unban 掉，也就是下次開啟之後，封鎖清單是空白的，fail2ban 再依照設定的時間、服務類型、log 位置 &#8230; 等資訊慢慢一個一個 ban 回去，該文章使用的方式是在 ban 的當下，讓 fail2ban 多寫一個叫做<code>persistent.bans</code>的檔案，這樣下次重開機或是重啟 fail2ban 的時候，雖然原本 ban 的會先 unban，但是手上還是有封鎖清單，再讓 fail2ban 把它們 ban 回去就好了，雖不完美，但是的確可以達到小蛙的需求，確定之後就著手設定。</p>



<p>首先，建立 /etc/fail2ban/persistent.bans 檔案，要改別的名字或放別的地方也可以，只是後面的路徑要記得跟著改就是了。</p>



<h2 class="para wp-block-heading">設定載入及增加清單</h2>



<p>fail2ban 是透過&nbsp;/etc/fail2ban/action.d/iptables-multiport.conf 來做 ban IP 的，我們需要做 </p>



<ul class="my-li bg-darkblue wp-block-list"><li>在 fail2ban 啟動時呼叫的 actionstart 中加載永久封鎖清單</li><li>在 fail2ban 做 ban IP 動作的時候，將該 IP 寫入永久封鎖清單中 </li></ul>



<p>在上述檔案中找到 actionstart 在既有的動作下，加入 4, 5 兩行 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">actionstart = &lt;iptables&gt; -N f2b-&lt;name&gt;
			  &lt;iptables&gt; -A  f2b-&lt;name&gt; -j &lt;returntype&gt;
			  &lt;iptables&gt; -I &lt;chain&gt; -p &lt;protocol&gt; -m multiport --dports  &lt;port&gt; -j &lt;name&gt; cat  /etc/fail2ban/persistent.bans | awk '/^fail2ban-&lt;name&gt;/ {print $2}' \ | while read IP; do iptables -I fail2ban-&lt;name&gt; 1 -s $IP -j &lt;blocktype&gt;; done</pre>



<p>接著在 actionban 既有動作下，加入第二行 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">actionban = &lt;iptables&gt; -I f2b-&lt;name&gt; 1 -s &lt;ip&gt; -j &lt;blocktype&gt; echo "fail2ban-&lt;name&gt; &lt;ip&gt;" &gt;&gt; /etc/fail2ban/persistent.bans</pre>



<p>再來重啟 fail2ban 就大功告成了，小蛙這邊之前一直測試都失敗 … 後來才發現 … 自己加錯加到 actionunban 後面 … 難怪怎麼樣 persistent.bans 都不會有內容啊 …</p>



<h2 class="para wp-block-heading">後續更新</h2>



<p>2018-11-19 發現會有重複 IP 的問題，找到解決方法後會再更新上來。</p>



<p><strong>fail2ban 系列：</strong></p>



<ul class="my-li bg-darkblue wp-block-list"><li><a href="https://noter.tw/255/ubuntu-16-04-安裝-fail2ban/" data-wpel-link="internal">Ubuntu 16.04 安裝 fail2ban</a></li><li><a href="https://noter.tw/54/%e8%ae%93-fail2ban-%e5%8f%af%e4%bb%a5%e8%a8%ad%e5%ae%9a%e6%b0%b8%e4%b9%85-ban-%e5%90%8d%e5%96%ae/" data-wpel-link="internal">讓 fail2ban 可以設定永久 ban 名單</a></li><li><a href="https://noter.tw/259/%E8%87%AA%E5%B7%B1%E8%A2%AB-fail2ban-%E5%B0%81%E9%8E%96%E7%9A%84%E8%A7%A3%E6%B1%BA%E6%96%B9%E6%B3%95/" data-wpel-link="internal">自己被 fail2ban 封鎖的解決方法</a></li></ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/54/%e8%ae%93-fail2ban-%e5%8f%af%e4%bb%a5%e8%a8%ad%e5%ae%9a%e6%b0%b8%e4%b9%85-ban-%e5%90%8d%e5%96%ae/" data-wpel-link="internal">讓 fail2ban 可以設定永久 ban 名單</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/54/%e8%ae%93-fail2ban-%e5%8f%af%e4%bb%a5%e8%a8%ad%e5%ae%9a%e6%b0%b8%e4%b9%85-ban-%e5%90%8d%e5%96%ae/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
