<?xml version="1.0"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>CRIN Trac: Ticket #2: Set up firewall and fail2ban to stop brute force ssh attacks</title>
    <link>https://trac.crin.org/trac/ticket/2</link>
    <description>&lt;p&gt;
The servers need only listen for HTTP, HTTPS and SSH traffic and brute force attacks on ssh accounts should be blocked using &lt;a class="ext-link" href="https://packages.debian.org/jessie/fail2ban"&gt;&lt;span class="icon"&gt;​&lt;/span&gt;https://packages.debian.org/jessie/fail2ban&lt;/a&gt;
&lt;/p&gt;
</description>
    <language>en-us</language>
    <image>
      <title>CRIN Trac</title>
      <url>https://trac.crin.org/trac/chrome/site/logo.gif</url>
      <link>https://trac.crin.org/trac/ticket/2</link>
    </image>
    <generator>Trac 1.0.2</generator>
    <item>
      
        <dc:creator>chris</dc:creator>

      <pubDate>Tue, 28 Apr 2015 13:41:10 GMT</pubDate>
      <title>hours changed; totalhours set</title>
      <link>https://trac.crin.org/trac/ticket/2#comment:1</link>
      <guid isPermaLink="false">https://trac.crin.org/trac/ticket/2#comment:1</guid>
      <description>
          &lt;ul&gt;
            &lt;li&gt;&lt;strong&gt;hours&lt;/strong&gt;
                changed from &lt;em&gt;0&lt;/em&gt; to &lt;em&gt;0.25&lt;/em&gt;
            &lt;/li&gt;
            &lt;li&gt;&lt;strong&gt;totalhours&lt;/strong&gt;
                set to &lt;em&gt;0.25&lt;/em&gt;
            &lt;/li&gt;
          &lt;/ul&gt;
        &lt;p&gt;
Install:
&lt;/p&gt;
&lt;pre class="wiki"&gt;aptitude install iptables-persistent fail2ban
&lt;/pre&gt;&lt;p&gt;
Create &lt;tt&gt;/etc/fail2ban/jail.local&lt;/tt&gt; containing:
&lt;/p&gt;
&lt;pre class="wiki"&gt;# See http://www.pontikis.net/blog/fail2ban-install-config-debian-wheezy
[DEFAULT]
ignoreip  = 127.0.0.1 93.95.228.179
bantime   = 86400
#destemail = chris@webarchitects.co.uk
banaction = iptables-multiport
#action    = %(action_mwl)s
# JAILS
[ssh]
enabled   = true
maxretry  = 3
&lt;/pre&gt;&lt;p&gt;
Restart:
&lt;/p&gt;
&lt;pre class="wiki"&gt;service fail2ban restart
&lt;/pre&gt;&lt;p&gt;
Edit &lt;tt&gt;/etc/iptables/rules.v4&lt;/tt&gt; which originally contained:
&lt;/p&gt;
&lt;pre class="wiki"&gt;# Generated by iptables-save v1.4.21 on Tue Apr 28 13:27:23 2015
*filter
:INPUT ACCEPT [2:104]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [2:336]
:fail2ban-ssh - [0:0]
-A INPUT -p tcp -m multiport --dports 22 -j fail2ban-ssh
-A fail2ban-ssh -j RETURN
COMMIT
# Completed on Tue Apr 28 13:27:23 2015
&lt;/pre&gt;&lt;p&gt;
To config from existing servers:
&lt;/p&gt;
&lt;pre class="wiki"&gt;# Generated by iptables-save v1.4.21 on Tue Apr 28 13:27:23 2015
*filter
:INPUT ACCEPT [2:104]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [2:336]
:fail2ban-ssh - [0:0]
-A INPUT -p tcp -m multiport --dports 22 -j fail2ban-ssh
-A fail2ban-ssh -j RETURN
# Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT
# Accepts all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allows all outbound traffic
# You could modify this to only allow certain traffic
-A OUTPUT -j ACCEPT
# Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT
# Allows SSH connections
# THE -dport NUMBER IS THE SAME ONE YOU SET UP IN THE SSHD_CONFIG FILE
#-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
# Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
# mysql allow 93.95.228.180
#-A INPUT -p tcp -d 93.95.228.180 --dport 3306 -j ACCEPT
-A INPUT -p tcp -s 93.95.228.180 --dport 3306 -j ACCEPT
-A INPUT -p tcp --dport 3306 -j DROP
# log iptables denied calls (access via 'dmesg' command)
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
# Reject all other inbound - default deny unless explicitly allowed policy:
-A INPUT -j REJECT
-A FORWARD -j REJECT
COMMIT
# Completed on Tue Apr 28 13:27:23 2015
&lt;/pre&gt;&lt;p&gt;
Reload and check:
&lt;/p&gt;
&lt;pre class="wiki"&gt;iptables-restore &amp;lt; /etc/iptables/rules.v4
iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
fail2ban-ssh  tcp  --  anywhere             anywhere             multiport dports ssh
ACCEPT     all  --  anywhere             anywhere
REJECT     all  --  anywhere             loopback/8           reject-with icmp-port-unreachable
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
ACCEPT     icmp --  anywhere             anywhere             icmp echo-request
ACCEPT     tcp  --  crin2.crin.org       anywhere             tcp dpt:mysql
DROP       tcp  --  anywhere             anywhere             tcp dpt:mysql
LOG        all  --  anywhere             anywhere             limit: avg 5/min burst 5 LOG level debug prefix "iptables denied: "
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
Chain fail2ban-ssh (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere
&lt;/pre&gt;&lt;p&gt;
Disable IPv6 as we are not setting it up right now, add the following to &lt;tt&gt;/etc/sysctl.conf&lt;/tt&gt;:
&lt;/p&gt;
&lt;pre class="wiki"&gt;net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
&lt;/pre&gt;&lt;p&gt;
The above now needs doing for crin2.
&lt;/p&gt;
      </description>
      <category>Ticket</category>
    </item><item>
      
        <dc:creator>chris</dc:creator>

      <pubDate>Tue, 28 Apr 2015 13:48:53 GMT</pubDate>
      <title>hours, totalhours changed</title>
      <link>https://trac.crin.org/trac/ticket/2#comment:2</link>
      <guid isPermaLink="false">https://trac.crin.org/trac/ticket/2#comment:2</guid>
      <description>
          &lt;ul&gt;
            &lt;li&gt;&lt;strong&gt;hours&lt;/strong&gt;
                changed from &lt;em&gt;0&lt;/em&gt; to &lt;em&gt;0.05&lt;/em&gt;
            &lt;/li&gt;
            &lt;li&gt;&lt;strong&gt;totalhours&lt;/strong&gt;
                changed from &lt;em&gt;0.25&lt;/em&gt; to &lt;em&gt;0.3&lt;/em&gt;
            &lt;/li&gt;
          &lt;/ul&gt;
        &lt;pre class="wiki"&gt;aptitude install iptables-persistent fail2ban
vi /etc/fail2ban/jail.local
service fail2ban restart
vi /etc/iptables/rules.v4
iptables-restore &amp;lt; /etc/iptables/rules.v4
vi /etc/sysctl.conf
&lt;/pre&gt;&lt;p&gt;
The following was added to &lt;tt&gt;rules.v4&lt;/tt&gt;:
&lt;/p&gt;
&lt;pre class="wiki"&gt;# https://wiki.debian.org/iptables
# Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT
# Accepts all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allows all outbound traffic
# You could modify this to only allow certain traffic
-A OUTPUT -j ACCEPT
# Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT
# Allows SSH connections
# THE -dport NUMBER IS THE SAME ONE YOU SET UP IN THE SSHD_CONFIG FILE
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
# Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
# log iptables denied calls (access via 'dmesg' command)
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
# Reject all other inbound - default deny unless explicitly allowed policy:
-A INPUT -j REJECT
-A FORWARD -j REJECT
&lt;/pre&gt;&lt;p&gt;
Check the rules:
&lt;/p&gt;
&lt;pre class="wiki"&gt;iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
fail2ban-ssh  tcp  --  anywhere             anywhere             multiport dports ssh
ACCEPT     all  --  anywhere             anywhere
REJECT     all  --  anywhere             loopback/8           reject-with icmp-port-unreachable
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
ACCEPT     icmp --  anywhere             anywhere             icmp echo-request
LOG        all  --  anywhere             anywhere             limit: avg 5/min burst 5 LOG level debug prefix "iptables denied: "
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
Chain fail2ban-ssh (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere
&lt;/pre&gt;&lt;p&gt;
That should do for now for this issue.
&lt;/p&gt;
      </description>
      <category>Ticket</category>
    </item><item>
      
        <dc:creator>chris</dc:creator>

      <pubDate>Tue, 28 Apr 2015 13:58:50 GMT</pubDate>
      <title>hours, status, totalhours changed; resolution set</title>
      <link>https://trac.crin.org/trac/ticket/2#comment:3</link>
      <guid isPermaLink="false">https://trac.crin.org/trac/ticket/2#comment:3</guid>
      <description>
          &lt;ul&gt;
            &lt;li&gt;&lt;strong&gt;hours&lt;/strong&gt;
                changed from &lt;em&gt;0&lt;/em&gt; to &lt;em&gt;0.1&lt;/em&gt;
            &lt;/li&gt;
            &lt;li&gt;&lt;strong&gt;status&lt;/strong&gt;
                changed from &lt;em&gt;new&lt;/em&gt; to &lt;em&gt;closed&lt;/em&gt;
            &lt;/li&gt;
            &lt;li&gt;&lt;strong&gt;resolution&lt;/strong&gt;
                set to &lt;em&gt;fixed&lt;/em&gt;
            &lt;/li&gt;
            &lt;li&gt;&lt;strong&gt;totalhours&lt;/strong&gt;
                changed from &lt;em&gt;0.3&lt;/em&gt; to &lt;em&gt;0.4&lt;/em&gt;
            &lt;/li&gt;
          &lt;/ul&gt;
        &lt;p&gt;
Just tested the ssh servers and I had to uncomment this for crin1:
&lt;/p&gt;
&lt;pre class="wiki"&gt;-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
&lt;/pre&gt;&lt;p&gt;
Think this ticket can now be closed as resolved.
&lt;/p&gt;
      </description>
      <category>Ticket</category>
    </item><item>
      
        <dc:creator>chris</dc:creator>

      <pubDate>Thu, 18 Jun 2015 11:57:05 GMT</pubDate>
      <title>cc changed</title>
      <link>https://trac.crin.org/trac/ticket/2#comment:4</link>
      <guid isPermaLink="false">https://trac.crin.org/trac/ticket/2#comment:4</guid>
      <description>
          &lt;ul&gt;
            &lt;li&gt;&lt;strong&gt;cc&lt;/strong&gt;
              &lt;em&gt;jenny&lt;/em&gt; &lt;em&gt;gillian&lt;/em&gt; added; &lt;em&gt;jonas&lt;/em&gt; removed
            &lt;/li&gt;
          &lt;/ul&gt;
        &lt;p&gt;
CCs changed.
&lt;/p&gt;
      </description>
      <category>Ticket</category>
    </item>
 </channel>
</rss>