Firewall configuration to restrict ssh access?

Hi,
In the past I used a combination of hosts.allow and hosts.deny with a
“FW_TRUSTED_NETS” variable specifying the computers ip addresses that were
allowed to connect to another computer running the ssh service.

Now I want to do the same, but I realised that 13.1 this does not work any
more and read that a similar thing could be done with iptables.

I do not have experience with iptables and despite reading about it for 2
days I am still confused on how to go about it.

I wonder if anybody has specific hints on how to restrict ssh logging into a
computer to two other computers.

Assuming that the computers ip addresses are 1.2.3.4 and 5.6.7.8
I read that this could be achieved with something like this:

iptables -A INPUT -p tcp -s 1.2.3.4 --dport 22 -j ACCEPT

iptables -A INPUT -p tcp -s 5.6.7.8 --dport 22 -j ACCEPT

iptables -A INPUT -p tcp --dport 22 -j DROP

and then

iptables-save

My questions are:

  1. Is the above correct?

  2. Where is the result stored, in case I make an error or want to edit
    something or revert to the original state?

  3. Is there a better way of doing the above?

Many thanks

-G-

Am Tue, 28 Jun 2016 10:17:22 GMT
schrieb -G-@gospamyourself.com:

> Hi,
> In the past I used a combination of hosts.allow and hosts.deny with a
> “FW_TRUSTED_NETS” variable specifying the computers ip addresses that were
> allowed to connect to another computer running the ssh service.
>
> Now I want to do the same, but I realised that 13.1 this does not work any
> more

I very much doubt that, it works fine here on 13.1 as it did in previous and
still does in newer versions (42.1 for example).

Example:

grep TRUSTED_NETS /etc/sysconfig/SuSEfirewall2

#FW_TRUSTED_NETS=“192.168.0.0/16”

  • commented, so no trusted nets set

On other machine in LAN:

nmap -p 12345 192.168.XXX.YYY

Starting Nmap 7.12 ( https://nmap.org ) at 2016-06-28 12:37 CEST
Nmap scan report for 192.168.XXX.YYY
Host is up (0.0033s latency).
PORT STATE SERVICE
12345/tcp closed netbus

Nmap done: 1 IP address (1 host up) scanned in 11.11 seconds

== Access blocked by firewall.

After uncommenting FW_TRUSTED_NETS

FW_TRUSTED_NETS=“192.168.0.0/16”

and restarting SuSEfirewall2

nmap -p 12345 192.168.XXX.YYY

Starting Nmap 7.12 ( https://nmap.org ) at 2016-06-28 12:36 CEST
Nmap scan report for 192.168.XXX.YYY
Host is up (0.0033s latency).
PORT STATE SERVICE
12345/tcp open netbus

Access granted due to 192.168.xxx.yyy being part of TRUSTED_NETS

Q.E.D.

> and read that a similar thing could be done with iptables.
>
>

SuSEfirewall2 is a frontend for iptables (or to be more precise for
iptables and xtables, but that is just a detail).

BTW, using FW_SERVICES_ACCEPT_$ZONE is much more specific and can also be
extended to allow only certain subnets, see documentation in

/usr/share/doc/packages/SuSEfirewall2/

AK


Never attribute to malice that which can be adequately explained by stupidity.
(R.J. Hanlon)

On 06/28/2016 04:17 AM, -G-@gospamyourself.com wrote:
> Hi,
> In the past I used a combination of hosts.allow and hosts.deny with a
> “FW_TRUSTED_NETS” variable specifying the computers ip addresses that were
> allowed to connect to another computer running the ssh service.
>
> Now I want to do the same, but I realised that 13.1 this does not work any
> more and read that a similar thing could be done with iptables.

Odd… you may want to check again. I’d be surprised if something like
that went away.

> I do not have experience with iptables and despite reading about it for 2
> days I am still confused on how to go about it.

With great power comes great responsibility (and complexity, moral or
technical depending on the number of spiders or chameleons involved).

> I wonder if anybody has specific hints on how to restrict ssh logging into a
> computer to two other computers.
>
> Assuming that the computers ip addresses are 1.2.3.4 and 5.6.7.8
> I read that this could be achieved with something like this:
>
> iptables -A INPUT -p tcp -s 1.2.3.4 --dport 22 -j ACCEPT
>
> iptables -A INPUT -p tcp -s 5.6.7.8 --dport 22 -j ACCEPT
>
> iptables -A INPUT -p tcp --dport 22 -j DROP

If these were your only rules, then that could be fine, as this says to
allow anything from 1.2.3.4 as well as 5.6.7.8 using TCP to destination
port 22, which is what you’re after as long as you have SSH on TCP 22 as
is default, and then it says to drop any SSH-ish stuff from anybody else
trying to reach TCP 22 (don’t forget about other ports).

> and then
>
> iptables-save

iptables-save is a great command to output the current firewall
configuration iptables commands. You can then write those to a file to be
re-read by iptables-restore, perhaps at boot time, but do not be confused
into thinking this is ever done for you because it is not.

> My questions are:
>
> 1. Is the above correct?

Depends on your goals. Yes, the commands work, and no they are not
persistent.

> 2. Where is the result stored, in case I make an error or want to edit
> something or revert to the original state?

In memory, or from the iptables-save output, on your screen.

> 3. Is there a better way of doing the above?

Good question, and maybe the best one overall. More history:

SUSE has its own firewall configuration magic under the hood and unless
you work with it you need to disable it or you will be fighting a battle
forever. The SuSEfirewall2 (the name of the firewall service) service
starts and stops with the system and has a nice Yast-powered interface to
do various configuration things. It is integrated with the system so it
knows when the network starts/stops (you change locations with your
laptop, for example, or you change from wireless to physical NIC, or you
restart, or start up VMs in KVM or Xen, or… you get the idea). It also
keeps track of different zones (external, internal, DMZ) if you want to,
which is neat if you want it.

In your case I would recommend using the SuSEfirewall2 service, but then
modifying it to allow specific systems to do specific things. For now, go
into Yast (sudo /sbin/yast firewall) and then go to ‘Custom Rules’ and see
if you can add your two IP addresses there as allowed to access TCP 22.
The source IPs will be 1.2.3.4/32 and 2.3.4.5/32 and then you can set them
to be allowed, probably on the External (default) zone.

Save, quite, and now see what you see different from the following command:


sudo /usr/sbin/iptables-save

If nothing else, this will help you learn how the iptables commands work.
If you really want to create a firewall from scratch, there are articles
on that which are really good and SUSE-focused, but I do not think it is
worth it for you since you want a little bit of tweaking, not a completely
new firewall service.


Good luck.

If you find this post helpful and are logged into the web interface,
show your appreciation and click on the star below…

Sorry this is in opensuse 13.2, not 13.1, my mistake.

This is what other people found as well about TCP wrappers:

https://forums.opensuse.org/showthread.php/505226-Is-file-etc-hosts-deny-disabled-on-OpensSuse-13-2

I would appreciate any further suggestions.

ab wrote:
> Odd… you may want to check again. I’d be surprised if something like
> that went away.

Yes, I thought so too. I posted just a minute ago mentioning a thread where
it is suggested that the TCP wrapping is not working.
Here is another: http://comments.gmane.org/gmane.linux.suse.general/348119

I am not expert on this so that is why I am asking here :slight_smile:

> iptables-save is a great command to output the current firewall
> configuration iptables commands. You can then write those to a file to be
> re-read by iptables-restore, perhaps at boot time, but do not be confused
> into thinking this is ever done for you because it is not.

OK thanks that is useful.

> SUSE has its own firewall configuration magic under the hood and unless
> you work with it you need to disable it or you will be fighting a battle
> forever. The SuSEfirewall2 (the name of the firewall service) service
> starts and stops with the system and has a nice Yast-powered interface to
> do various configuration things. It is integrated with the system so it
> knows when the network starts/stops (you change locations with your
> laptop, for example, or you change from wireless to physical NIC, or you
> restart, or start up VMs in KVM or Xen, or… you get the idea). It also
> keeps track of different zones (external, internal, DMZ) if you want to,
> which is neat if you want it.
>
> In your case I would recommend using the SuSEfirewall2 service, but then
> modifying it to allow specific systems to do specific things. For now, go
> into Yast (sudo /sbin/yast firewall) and then go to ‘Custom Rules’ and see
> if you can add your two IP addresses there as allowed to access TCP 22.

> The source IPs will be 1.2.3.4/32 and 2.3.4.5/32 and then you can set them
> to be allowed, probably on the External (default) zone.
>
> Save, quite, and now see what you see different from the following
> command:
>
>


> sudo /usr/sbin/iptables-save
> 

>
> If nothing else, this will help you learn how the iptables commands work.
> If you really want to create a firewall from scratch, there are articles
> on that which are really good and SUSE-focused, but I do not think it is
> worth it for you since you want a little bit of tweaking, not a completely
> new firewall service.

Excellent, this is what I was looking for. No, I am not trying to create my
own firewall, just restrict ssh to the two other computers I have.
Many thanks again!
-G-

Just in case it is useful to others, one important thing I learned is that
if the secure shell service is enabled in Yast’s firewall, under “Allow
Services” this seems to override the FW_TRUSTED_NETS variables set in
/etc/sysconfig/SuSEfirewall2

I could log via ssh in even when I commented that variable and was the
source of my confusion. I removed the ssh service from the firewall in yast
and now FW_TRUSTED_NETS restrictions appear to be working as expected.

Thanks to an and AK for their help.

If you compare the iptables-save output before/after trusting the SSH
service you’ll likely see that the allowances for the SSH service are
general allowances on the external interface, meaning anybody accessing
your service via that interface will have that allowance applied to them.
This is a good point you have called out, as it shows that different
sections of the firewall configuration are working as you told them to,
though that may not have been what you meant to do since, while you wanted
SSH access, you ONLY wanted it for those two hosts.

Thanks for following up, as it will likely help others in the future.


Good luck.

If you find this post helpful and are logged into the web interface,
show your appreciation and click on the star below…