<?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>fail2ban &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/fail2ban/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>fail2ban &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Ubuntu 16.04 安裝 fail2ban</title>
		<link>https://noter.tw/255/ubuntu-16-04-%e5%ae%89%e8%a3%9d-fail2ban/</link>
					<comments>https://noter.tw/255/ubuntu-16-04-%e5%ae%89%e8%a3%9d-fail2ban/#comments</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Mon, 26 Nov 2018 14:37:15 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[fail2ban]]></category>
		<category><![CDATA[安全]]></category>
		<category><![CDATA[security]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=255</guid>

					<description><![CDATA[<p>從小蛙開始碰 VPS 之後，才知道原來一台串上網路的主機，身處那麼多的危險，只要網路是對外公開的，無時無刻都在面臨著一大堆奇奇怪怪的惡意測試或是攻擊。這篇記錄怎麼在 Ubuntu 16.04 上安裝 &#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/255/ubuntu-16-04-%e5%ae%89%e8%a3%9d-fail2ban/" data-wpel-link="internal">Ubuntu 16.04 安裝 fail2ban</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>從小蛙開始碰 VPS 之後，才知道原來一台串上網路的主機，身處那麼多的危險，只要網路是對外公開的，無時無刻都在面臨著一大堆奇奇怪怪的惡意測試或是攻擊。這篇記錄怎麼在 Ubuntu 16.04 上安裝 fail2ban 這套防護套件。</p>



<span id="more-255"></span>



<h2 class="para wp-block-heading">緣由</h2>



<p>如果有人嘗試登入主機失敗，系統就會記錄一次登記失敗的 log，所以在一般的情況下如果有人惡意要去 try 你的服務，他可以一直一直一直 try，這時候如果你的密碼很簡單，尤其是使用了很單純的單字、數字組成，沒過多久很快就會被 try 破了，把密碼弄得複雜一點長一點可以增加被 try 破的難度，但是如果讓惡意連線一直嘗試，一樣會有被 try 破的一天。</p>



<p>fail2ban 是一套 Linux 安全防護套件，運作的原理是讓使用者設定需要監控的服務，例如：<code>ssh</code>、<code>mysql</code>，這些服務如果有使用者登入失敗，會如同上面提到的，寫一條登入失敗的資訊到 log，fail2ban 會根據使用者設定去掃描有沒有像下面這種嘗試登入並且登入失敗的記錄</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">Nov 26 21:37:35 noter sshd[]: Invalid user dbuser from 118.24.117.187
Nov 26 21:37:35 noter sshd[]: input_userauth_request: invalid user dbuser [preauth]
Nov 26 21:37:35 noter sshd[]: pam_unix(sshd:auth): check pass; user unknown
Nov 26 21:37:35 noter sshd[]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=118.24.117.187
Nov 26 21:37:37 noter sshd[]: Failed password for invalid user dbuser from 118.24.117.187 port 47634 ssh2</pre>



<p>使用者可以設定在一段時間內，嘗試登入但發生錯誤幾次，就先把這個 IP 進行封鎖一段時間，這樣應該不難理解，這些設定必須要根據自己的使用情況來設定。 </p>



<h2 class="para wp-block-heading">安裝 fail2ban</h2>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">sudo apt-get update 
sudo apt-get install fail2ban 
# 重新啟動 
service fail2ban stop 
service fail2ban start</pre>



<p>直接安裝完就會以預設值來啟動，這樣就有基本的防護功能，但是基本設定並不能滿足小蛙的需求，因此下面依自己需求修改。 </p>



<h2 class="para wp-block-heading">修改設定檔</h2>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code"># 先備份一份原始設定，避免改壞
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
 
# 編輯設定
vim /etc/fail2ban/jail.conf
 
# 說明
bantime = 改成自己要的秒數，如果被 ban 要多久才能
findtime = 改成自己要的秒數，掃描區間
maxretry = 3 改成自己可以允許被 try 幾次
destemail = 改成自己要收到信的 email
 
# ssh 服務設定區塊
[sshd]
enabled = true
port    = ssh
logpath = %(sshd_log)s
 
# 重新啟動
service fail2ban stop
service fail2ban start</pre>



<p>每個服務都會有自己的區塊，ssh 服務可以在設定檔中找到，如果區塊內沒有上述四個參數，就會套用預設值，有的話則會使用區塊內的設定蓋過預設值，logpath 保留預設就可以了，想開啟其他服務的話，只要找到該區塊直接 enabled 設定成 true 即可。談談小蛙的思考 </p>



<ul class="my-li bg-darkblue wp-block-list"><li>不太可能連續輸入錯密碼 3 次，因此 maxretry 小蛙設定成 3</li><li>第一點可能性很低，因此 findtime 可以設定大一點，設了 7200 秒</li><li>因會發生 1, 2 點的都不會是小蛙自己，因此 bantime 設定成 -1，永久封鎖</li></ul>



<p>意思就是說上面 118.24.117.187 如果在 7200 秒內(findtime)，登入錯誤超過 3 次(maxretry)，那麼這個 IP 就會被小蛙的主機永久設定為拒絕往來戶。雖然 fail2ban 不能夠完完全全阻擋外來的攻擊，但是至少可以避免讓一些惡意嘗試登入可以無限制地一直測試，其實也是個方便又不錯的工具喔！ </p>



<p>2019-12-04</p>



<p>今天裝一台新的發現在 jail.conf 裡的 [DEFAULT] 設定預設並沒有生效 &#8230; 因此如果也有遇到這狀況的人，可以改成在 /etc/fail2ban/jail.d/ 下面設定，例如小蛙一裝完，這個資料夾下原本就有一個 defaults-debian.conf，裡面原本就有 [sshd] enabled = true，預設就把 sshd 打開了，把自己要的 bantime, findtime, maxretry 打在這邊再重啟 fail2ban 就可以了。</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><strong>增加安全性系列：</strong></p>



<ul class="my-li bg-darkblue wp-block-list"><li><a href="https://noter.tw/10420/向-twca-申請-ssl-憑證-ubuntu-apache/" data-wpel-link="internal">向 TWCA 申請 SSL 憑證 (Ubuntu, Apache)</a></li><li><a href="https://noter.tw/34/ubuntu-16-04-%E5%8D%87%E7%B4%9A-phpmyadmin-%E4%B8%A6%E9%96%8B%E5%95%9F%E4%BA%8C%E9%9A%8E%E6%AE%B5%E9%A9%97%E8%AD%89-2fa/" data-wpel-link="internal">Ubuntu 16.04 升級 phpMyAdmin 並開啟二階段驗證( 2FA )</a></li><li><a href="https://noter.tw/255/ubuntu-16-04-安裝-fail2ban/" data-wpel-link="internal">Ubuntu 16.04 安裝 fail2ban</a></li></ul>


<p><strong>網站架設系列文章：</strong></p>



<ul class="my-li bg-darkblue wp-block-list"><li><a href="https://noter.tw/10151/%e7%9b%a4%e9%bb%9e%e9%82%a3%e4%ba%9b%e6%9b%be%e7%b6%93%e9%99%aa%e4%bc%b4%e9%81%8e%e5%b0%8f%e8%9b%99%e7%9a%84-vps-%e8%99%9b%e6%93%ac%e4%b8%bb%e6%a9%9f/" data-wpel-link="internal">盤點那些曾經陪伴過小蛙的 VPS 虛擬主機</a></li><li><a href="https://noter.tw/176/%e5%a6%82%e4%bd%95%e6%8c%91%e9%81%b8-vps-%e6%9c%80%e5%88%92%e7%ae%97%e7%9a%84%e8%b2%b7%e6%b3%95/" data-wpel-link="internal">如何挑選 VPS + 最划算的買法</a></li><li><a href="https://noter.tw/406/%E6%89%8B%E6%8A%8A%E6%89%8B%E6%95%99%E4%BD%A0%E5%A6%82%E4%BD%95%E8%B3%BC%E8%B2%B7-vps/" data-wpel-link="internal">手把手教你如何購買 VPS</a></li><li><a href="https://noter.tw/4463/第一次買-kvm-虛擬主機/" data-wpel-link="internal">第一次買 KVM 虛擬主機</a></li><li><a href="https://noter.tw/4242/%e8%a1%80%e7%9a%84%e6%95%99%e8%a8%93-hiformance-%e8%b7%91%e8%b7%af%e4%ba%86/" data-wpel-link="internal">血的教訓 &#8211; HiFormance 跑路了 &#8230;</a></li><li><a href="https://noter.tw/196/%e7%82%ba%e8%87%aa%e5%b7%b1%e7%94%b3%e8%ab%8b%e4%b8%80%e5%80%8b%e5%9f%9f%e5%90%8d-hinet-%e7%82%ba%e4%be%8b/" data-wpel-link="internal">為自己申請一個域名 – HiNet 為例</a></li><li><a href="https://noter.tw/117/cloudflare-%E8%B6%85%E5%BC%B7%E5%A4%A7%E7%9A%84%E5%85%8D%E8%B2%BB-cdn/" data-wpel-link="internal">Cloudflare – 超強大的免費 CDN (?)</a></li><li><a href="https://noter.tw/195/%e4%bd%bf%e7%94%a8-cloudflare-%e4%bb%a3%e7%ae%a1%e4%bd%a0%e7%9a%84%e5%9f%9f%e5%90%8d/" data-wpel-link="internal">使用 Cloudflare 代管你的域名</a></li><li><a href="https://noter.tw/396/ubuntu-16-04-設定環境語言編碼，讓中文可以正常顯示/" data-wpel-link="internal">Ubuntu 16.04 設定環境語言編碼，讓中文可以正常顯示</a></li><li><a href="https://noter.tw/255/ubuntu-16-04-%e5%ae%89%e8%a3%9d-fail2ban/" data-wpel-link="internal">Ubuntu 16.04 安裝 fail2ban</a></li><li><a href="https://noter.tw/436/%E4%B8%80%E5%80%8B%E8%85%B3%E6%9C%AC%E6%AF%8F%E5%A4%A9%E8%87%AA%E5%8B%95%E5%82%99%E4%BB%BD/" data-wpel-link="internal">一個腳本每天自動備份</a></li><li><a href="https://noter.tw/32/linux-tar-%E5%AE%8C%E6%95%B4%E5%82%99%E4%BB%BD%E3%80%81%E5%A2%9E%E9%87%8F%E5%82%99%E4%BB%BD%E5%8F%8A%E5%B7%AE%E7%95%B0%E5%82%99%E4%BB%BD%E6%AF%94%E8%BC%83/" data-wpel-link="internal">Linux tar 完整備份、增量備份及差異備份比較</a></li><li><a href="https://noter.tw/3704/nginx-ssl-certificate-%E8%AE%93-http-%E8%AE%8A%E8%BA%AB%E6%88%90%E7%82%BA-https/" data-wpel-link="internal">nginx + SSL Certificate – 讓 http 變身成為 https</a></li><li><a href="https://noter.tw/3695/ssl-for-free-%E5%85%8D%E8%B2%BB%E5%8F%88%E6%9C%89%E7%B6%A0%E8%89%B2%E9%8E%96%E9%A0%AD%E7%9A%84-ssl-certificates/" data-wpel-link="internal">SSL For Free – 免費又有綠色鎖頭的 SSL Certificates</a></li><li><a href="https://noter.tw/8596/certbot-error/" data-wpel-link="internal">Certbot 申請 SSL certificate 錯誤問題</a></li><li><a href="https://noter.tw/9107/certbot-error-acmev1-is-deprecated/" data-wpel-link="internal">Certbot 錯誤：ACMEv1 is deprecated and you can …</a></li><li><a href="https://noter.tw/5967/ubuntu-18-04-%e5%ae%89%e8%a3%9d-awstats-%e4%be%86%e7%b5%b1%e8%a8%88%e7%b6%b2%e7%ab%99%e6%b5%81%e9%87%8f/" data-wpel-link="internal">Ubuntu 18.04 安裝 AWStats 來統計網站流量</a></li><li><a href="https://noter.tw/9287/pchome-%e8%b2%b7%e7%b6%b2%e5%9d%80%e7%ba%8c%e7%b4%84%e6%b5%81%e7%a8%8b/" data-wpel-link="internal">PCHome 買網址續約流程</a></li></ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/255/ubuntu-16-04-%e5%ae%89%e8%a3%9d-fail2ban/" data-wpel-link="internal">Ubuntu 16.04 安裝 fail2ban</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/255/ubuntu-16-04-%e5%ae%89%e8%a3%9d-fail2ban/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>自己被 fail2ban 封鎖的解決方法</title>
		<link>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/</link>
					<comments>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/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Mon, 26 Nov 2018 14:31:44 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[封鎖]]></category>
		<category><![CDATA[fail2ban]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=259</guid>

					<description><![CDATA[<p>昨天小蛙在測試 Visual Studio Code 的 SFTP 插件時，因為設定錯誤不小心被自己 VPS 上的 fail2ban 給 ban 掉了 &#8230; 這篇記錄解決的方法。 fail2&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" 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> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>昨天小蛙在測試 Visual Studio Code 的 SFTP 插件時，因為設定錯誤不小心被自己 VPS 上的 fail2ban 給 ban 掉了 &#8230; 這篇記錄解決的方法。 </p>



<span id="more-259"></span>



<p>fail2ban 是一個防護工具，可以設定某些連線方式，錯誤達到特定次數時，將該 IP 封鎖設指定時間，在開放的網路中，只要去看 log 就可以發現，主機是時時被不知道哪來的機器嘗試登入啊！</p>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" src="https://noter.tw/wp-content/uploads/2018/06/fail2ban-jail-1024x537.png" alt="fail2ban jail 自己被 fail2ban 封鎖的解決方法" class="wp-image-3996" title="自己被 fail2ban 封鎖的解決方法"></figure></div>



<p>水可載舟亦可覆舟，昨天小蛙要測試 VSC sftp 插件的時候，設定錯誤造成太多登入失敗的狀況，結果自己的 IP 被自己的 VPS 封鎖了，想連上去都沒辦法，還好一般 VPS 供應商都會提供一個後台可以讓你沒辦法登入的時候，可以透過後台做一些操作，像是重啟、關機、重裝 OS &#8230; 等功能，還有額外提供一個 Serial Console 的功能，直接在網頁上遠端進去主機做一些操作。</p>



<p>先到 <a rel="noreferrer noopener nofollow external" href="http://www.whatismyip.com.tw/" target="_blank" data-wpel-link="external" class="wpel-icon-right">這邊<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 查詢自己的 IP，因為小蛙已經沒有辦法透過 pietty 登入了，只能透過 Serial Console 來操控機器，連進主機之後，透過以下指令可以看到是不是自己的 IP 真的被封鎖了，還是另有其他問題。</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code"># iptables -nvL | grep 目前使用的 IP </pre>



<p>如果有的話，可以用以下指令解禁 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">// 查看目前有哪些 jail 設定 
# fail2ban-client status 

// 解鎖 
# fail2ban-client set sshd unbanip 要解鎖的 IP</pre>



<figure class="wp-block-image"><img decoding="async" src="https://noter.tw/wp-content/uploads/2018/06/fail2ban.png" alt="fail2ban 自己被 fail2ban 封鎖的解決方法" class="wp-image-3997" title="自己被 fail2ban 封鎖的解決方法"></figure>



<h2 class="para wp-block-heading">參考資料</h2>



<ul class="my-li bg-darkblue wp-block-list"><li><a rel="noreferrer noopener nofollow external" href="https://www.phpini.com/linux/manually-unban-fail2ban-ip" target="_blank" data-wpel-link="external" class="wpel-icon-right">手動解除 Fail2ban 封鎖的 IP &#8211; Linux 技術手札<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li></ul>



<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/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> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>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/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>
