<?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>Michele Manzotti &#187; pf</title>
	<atom:link href="http://www.manzotti.eu/tag/pf/feed" rel="self" type="application/rss+xml" />
	<link>http://www.manzotti.eu</link>
	<description></description>
	<lastBuildDate>Thu, 19 Jan 2012 09:56:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Ssh brute force bloccato con pf e regole di filtering su OpenBSD</title>
		<link>http://www.manzotti.eu/ssh-brute-force-bloccato-con-pf-e-regole-di-filtering</link>
		<comments>http://www.manzotti.eu/ssh-brute-force-bloccato-con-pf-e-regole-di-filtering#comments</comments>
		<pubDate>Sat, 10 Jan 2009 14:21:38 +0000</pubDate>
		<dc:creator>manzotti.eu</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[brute]]></category>
		<category><![CDATA[force]]></category>
		<category><![CDATA[OpenBSD]]></category>
		<category><![CDATA[pf]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://manzotti.eu/?p=98</guid>
		<description><![CDATA[Poco tempo fa stavo stavo dando un occhiata ai miei log e mi sono subito accorto di un attacco brute force sulla porta ssh. #tail /var/log/authlog Dec 26 19:22:31 maxbsd sshd&#91;12620&#93;: Failed password for root from 218.23.105.25 port 45219 ssh2 Dec 26 19:22:31 maxbsd sshd&#91;13293&#93;: Received disconnect from 218.23.105.25: 11: Bye Bye Dec 26 19:22:35 [...]]]></description>
			<content:encoded><![CDATA[<p>Poco tempo fa stavo stavo dando un occhiata ai miei log e mi sono subito accorto di un attacco brute force sulla porta ssh.</p>
<p>#tail /var/log/authlog</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">Dec <span style="color: #cc66cc;">26</span> <span style="color: #cc66cc;">19</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">22</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">31</span> maxbsd sshd<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">12620</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span> Failed password <span style="color: #b1b100;">for</span> root from 218<span style="color: #339933;">.</span>23<span style="color: #339933;">.</span>105<span style="color: #339933;">.</span>25 port <span style="color: #cc66cc;">45219</span> ssh2
Dec <span style="color: #cc66cc;">26</span> <span style="color: #cc66cc;">19</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">22</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">31</span> maxbsd sshd<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">13293</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span> Received disconnect from 218<span style="color: #339933;">.</span>23<span style="color: #339933;">.</span>105<span style="color: #339933;">.</span>25<span style="color: #339933;">:</span> <span style="color: #cc66cc;">11</span><span style="color: #339933;">:</span> Bye Bye
Dec <span style="color: #cc66cc;">26</span> <span style="color: #cc66cc;">19</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">22</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">35</span> maxbsd sshd<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6301</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span> <span style="color: #000066;">reverse</span> mapping checking getaddrinfo <span style="color: #b1b100;">for</span> 25<span style="color: #339933;">.</span>105<span style="color: #339933;">.</span>23<span style="color: #339933;">.</span>218<span style="color: #339933;">.</span>broad<span style="color: #339933;">.</span>static<span style="color: #339933;">.</span>hf<span style="color: #339933;">.</span>ah<span style="color: #339933;">.</span>cndata<span style="color: #339933;">.</span>com <span style="color: #009900;">&#91;</span>218<span style="color: #339933;">.</span>23<span style="color: #339933;">.</span>105<span style="color: #339933;">.</span>25<span style="color: #009900;">&#93;</span> failed <span style="color: #339933;">-</span> POSSIBLE BREAK<span style="color: #339933;">-</span>IN ATTEMPT<span style="color: #339933;">!</span>
Dec <span style="color: #cc66cc;">26</span> <span style="color: #cc66cc;">19</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">22</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">35</span> maxbsd sshd<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6301</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span> Failed password <span style="color: #b1b100;">for</span> root from 218<span style="color: #339933;">.</span>23<span style="color: #339933;">.</span>105<span style="color: #339933;">.</span>25 port <span style="color: #cc66cc;">45474</span> ssh2
Dec <span style="color: #cc66cc;">26</span> <span style="color: #cc66cc;">19</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">22</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">35</span> maxbsd sshd<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2351</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span> Received disconnect from 218<span style="color: #339933;">.</span>23<span style="color: #339933;">.</span>105<span style="color: #339933;">.</span>25<span style="color: #339933;">:</span> <span style="color: #cc66cc;">11</span><span style="color: #339933;">:</span> Bye Bye
Dec <span style="color: #cc66cc;">26</span> <span style="color: #cc66cc;">19</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">22</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">39</span> maxbsd sshd<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3801</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span> <span style="color: #000066;">reverse</span> mapping checking getaddrinfo <span style="color: #b1b100;">for</span> 25<span style="color: #339933;">.</span>105<span style="color: #339933;">.</span>23<span style="color: #339933;">.</span>218<span style="color: #339933;">.</span>bro</pre></div></div>

<p>Per eliminare questo genere di attacchi possiamo:</p>
<ul>
<li>Abilitare ssh in modo che dopo 3 tentativi sbagliati cada la connessione</li>
<li>Inserire una regola in pf in modo che non accetti più di 3 connessioni contemporaneamente dallo stesso Ip, per poi bannarlo in una blacklist</li>
<li>Creare uno script in modo che legga l&#8217;ip dell&#8217;attaccante dai log e lo inserisca nella blacklist.</li>
</ul>
<p>Per risolvere il primo punto è sufficiente modificare il file di configurazione di ssh, sshd_config, decommentando l&#8217;opzione:</p>
<p># nano /etc/ssh/sshd_config</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">MaxAuthTries <span style="color: #cc66cc;">3</span></pre></div></div>

<p>Per il secondo punto è necessario definire una tabella che contenga gli ip bloccati (la nostra blacklist). Dunque editiamo il file di configurazione di pf che si trova in /etc/pf.conf:</p>
<p>#nano /etc/pf.conf</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">table  persist file <span style="color: #ff0000;">&quot;/etc/spammers&quot;</span></pre></div></div>

<p>Ora invece scriviamo la regola in modo che non accetti più di 3 connessioni contemporaneamente dallo stesso Ip e lo inserisca nella blacklist:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">int</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;fxp0&quot;</span>
ssh_limit<span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;(max-src-conn-rate 3/30, overload  flush global)&quot;</span>
pass in <span style="color: #000066;">log</span> quick on <span style="color: #0000ff;">$int</span> inet proto tcp from any to <span style="color: #0000ff;">$int</span> port ssh flags S<span style="color: #339933;">/</span>SA keep state <span style="color: #0000ff;">$ssh_limit</span></pre></div></div>

<p>Infine per il terzo punto è sufficiente creare uno script, ssh_block_attack, come il seguente, in modo che legga il file di log e metta in blacklist l&#8217;indirizzo ip che ha sbagliato la password o l&#8217;username:</p>
<p># nano /root/ssh_block_attack</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #000066;">read</span> mm dd hms localhostname sshd word1 word2 word3 word4 host1 host2 rest<span style="color: #339933;">;</span>
<span style="color: #b1b100;">do</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#91;</span> <span style="color: #ff0000;">&quot;$word1 $word2 $word4&quot;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;Invalid user from&quot;</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">then</span>
data<span style="color: #339933;">=</span><span style="color: #ff0000;">`date`</span>
pfctl <span style="color: #339933;">-</span>t spammers <span style="color: #339933;">-</span>T add <span style="color: #0000ff;">$host1</span>
echo <span style="color: #ff0000;">&quot;Aggiunto $host1 a spammers il $data&quot;</span> <span style="color: #339933;">&gt;&gt;</span> <span style="color: #339933;">/</span>root<span style="color: #339933;">/</span>ssh_log
&nbsp;
elif <span style="color: #009900;">&#91;</span> <span style="color: #ff0000;">&quot;$word1 $word2 $word3 $host1&quot;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;Failed password for from&quot;</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">then</span>
pfctl <span style="color: #339933;">-</span>t spammers <span style="color: #339933;">-</span>T add <span style="color: #0000ff;">$host2</span>
echo <span style="color: #ff0000;">&quot;Aggiunto $host2 a spammers il $data&quot;</span> <span style="color: #339933;">&gt;&gt;</span> <span style="color: #339933;">/</span>root<span style="color: #339933;">/</span>ssh_log
fi
done</pre></div></div>

<p>Per lanciarlo avvio dandogli in pasto il log da monitorare aggiungiamo questa riga in /etc/rc.local .</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">tail <span style="color: #339933;">-</span>f <span style="color: #339933;">/</span>var<span style="color: #339933;">/</span><span style="color: #000066;">log</span><span style="color: #339933;">/</span>authlog <span style="color: #339933;">|</span> sh ssh_block_attack <span style="color: #339933;">&amp;</span></pre></div></div>

<p>Infine se vogliamo vedere la tabella degli ip bloccati è necessario utilizzare questo comando</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">pfctl <span style="color: #339933;">-</span>t spammers <span style="color: #339933;">-</span>T show</pre></div></div>

<p>Ovviamente per non saturare la nostra macchina ogni tanto andrebbe svuotata:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">pfctl <span style="color: #339933;">-</span>t spammers <span style="color: #339933;">-</span>T <span style="color: #000066;">delete</span> indirizzo_ip</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.manzotti.eu/ssh-brute-force-bloccato-con-pf-e-regole-di-filtering/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

