Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] firewall on an nfs/ssh server
View unanswered posts
View posts from last 24 hours

Goto page 1, 2  Next  
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
jyoung
Guru
Guru


Joined: 20 Mar 2007
Posts: 445

PostPosted: Fri Mar 17, 2023 5:39 pm    Post subject: [SOLVED] firewall on an nfs/ssh server Reply with quote

I'm setting up an iptables firewall on an nfs/ssh sever, but I'm seeing entries in /var/log/messages which suggest that it's not working properly. Here is the
script I'm using to generate rules for iptables:

Code:
#!/bin/bash

iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -m geoip ! --src-cc US -j DROP
iptables -A INPUT -p TCP --dport ssh -j ACCEPT
iptables -A INPUT -p TCP --dport 2049 -j ACCEPT
iptables -A OUTPUT -p TCP --dport 2049 -j ACCEPT


I've added iptables to the default run level, and as far I can tell the firewall is up. However, /var/log/messages shows repeated failed passwords from a root login from an IP address in China. This would seem to violate the forth rule down, which restricts remote logins to US IPs (I'm local to US, and it's basically just me logging in). Worse yet, root login is disallowed in /etc/ssh/sshd_config. I've confirmed this -- I can't log in remotely as root, and when I try the failure message in /var/log/messages doesn't report a failed password, it reports that root login is not allowed. Does anyone know what's going on? [/code]


Last edited by jyoung on Sat Mar 25, 2023 5:16 pm; edited 1 time in total
Back to top
View user's profile Send private message
jyoung
Guru
Guru


Joined: 20 Mar 2007
Posts: 445

PostPosted: Fri Mar 17, 2023 5:51 pm    Post subject: Reply with quote

I even just put the hacker's IP address in /etc/hosts.deny, but I'm still seeing failed password messages.
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 21918

PostPosted: Fri Mar 17, 2023 6:55 pm    Post subject: Reply with quote

Rule 2 allows ssh from anywhere if rule 1 has not dropped it. Have you verified that the Chinese IP address is in fact considered ! --src-cc US? If rule 1 is confused and not dropping that traffic, then rule 2 would allow it.

How did you disallow root in sshd_config? How did your ssh client try to authenticate as root?
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1000
Location: Richmond Hill, Canada

PostPosted: Fri Mar 17, 2023 9:52 pm    Post subject: Reply with quote

jyoung,

The "/etc/hosts.deny" is only used by sys-apps/tcp-wrappers, not firewall. Do you have sys-apps/tcp-wrappers? Is your sshd build with tcp-wrappers support? or your sshd will only start by tcpd? I think gentoo net-misc/openssh dropped support for tcp-wrappers

For firewall to work with geoip, You need kernel support. Do you have package net-firewall/xtables-addons installed? is your xtables-addons have USE flag for "geoip", Did you install geoip database? have a look at Gentoo wiki iptables for installation procedure

I found a example on how to configure iptables to manage geoip may be you can review it to see if it can help you.

I am sorry I hit you with so many questions. But we need to establish a base line in order for further discussion.
Back to top
View user's profile Send private message
jyoung
Guru
Guru


Joined: 20 Mar 2007
Posts: 445

PostPosted: Fri Mar 17, 2023 10:20 pm    Post subject: Reply with quote

Hu, is there a way I could trace the IP through the rules, and if it's considered by ! --src-cc US?

In sshd_config I have
Code:
PermitRootLogin no


Yet, the hacker is still failing with
Code:
Failed password for root from 61.177.173.2


When I try to login remotely as root via ssh, no matter what password I enter the message in /var/log/messages is
Code:
error: PAM: Permission denied for root from



pingtoo, thanks for the info on /etc/hosts.deny. I'm not using any tcp wrappers.

For geoip, I followed that tutorial, although not all of the kernel options were available. xtables-addons is installed, and the geoip flag is enabled.
Back to top
View user's profile Send private message
jyoung
Guru
Guru


Joined: 20 Mar 2007
Posts: 445

PostPosted: Fri Mar 17, 2023 10:55 pm    Post subject: Reply with quote

One more item: I also can't ssh from the server to another local machine. I'm new to iptables, so I suspect I've setup the rules incorrectly.
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 21918

PostPosted: Fri Mar 17, 2023 11:41 pm    Post subject: Reply with quote

jyoung wrote:
Hu, is there a way I could trace the IP through the rules, and if it's considered by ! --src-cc US?
I don't understand the question. Your rules as shown are simple enough that the only explanation I can see for the reported results is that the ! --src-cc US line does not do what you intend. If it were working as you desire, then the offending client could not connect. To understand why it does not do what you want, you would need to examine how geoip is supposed to detect that something is or is not US. That is not something on which I can advise you, as I do not use geoip. Perhaps someone else will have useful advice here.
jyoung wrote:
In sshd_config I have
Code:
PermitRootLogin no
Yet, the hacker is still failing with
Code:
Failed password for root from 61.177.173.2
When I try to login remotely as root via ssh, no matter what password I enter the message in /var/log/messages is
Code:
error: PAM: Permission denied for root from
That is a bit odd. I have never looked at exactly how PermitRootLogin diverts the authentication flow for a root login attempt. I recall that the openssh code is generally readable, so you might try tracing this in the source code.
jyoung wrote:
One more item: I also can't ssh from the server to another local machine. I'm new to iptables, so I suspect I've setup the rules incorrectly.
Yes. You have no rule to permit return traffic on locally initiated connections, so you can connect out, but you cannot receive the peer's response when it agrees to talk to you. Add an INPUT rule to ACCEPT ESTABLISHED traffic.
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1000
Location: Richmond Hill, Canada

PostPosted: Sat Mar 18, 2023 12:20 pm    Post subject: Reply with quote

jyoung,

I think you asked two questions, One is iptables rule to log access from unknown origin and second sshd configuration for not allow root password login.

First I will suggest sshd_config setup then review iptables rule

sshd_config, I suggest you just turn off password authentication method altogether by default, only allow use publickey by default. Only allow Matched specific host(IP address(es)) use other authentication methods

Example /etc/ssh/sshd_config. Partial config:

AuthenticationMethods publickey
PermitRootLogin prohibit-password (or no)
PasswordAuthentication no
KbdInteractiveAuthentication no

Match Address 192.168.100.0/24,172.16.0.0/16,11.22.33.44
  AuthenticationMethods publickey,keyboard-interactive(use PAM) or password(no PAM)
  PasswordAuthentication yes
  KbdInteractiveAuthentication yes
  PermitRootLogin yes
  PubkeyAuthentication yes

Match Host *.us
  AuthenticationMethods publickey,keyboard-interactive(use PAM) or password(no PAM)
  PasswordAuthentication yes
  KbdInteractiveAuthentication yes
  PermitRootLogin yes
  PubkeyAuthentication yes


Please use
Code:
/usr/sbin/sshd -T -f /etc/ssh/sshd_config -C addr=192.168.100.1
/usr/sbin/sshd -T -f /etc/ssh/sshd_config -C host=somewhere.in.us
to verify your sshd_config before restart sshd

iptables rule, if you look at the example link I provide in previous post, you will find it suggest setup a accounting custom chain.
Code:
#> iptables -N SSH_GEOIP

#> iptables -A SSH_GEOIP -m geoip --src-cc CA -j LOG --log-prefix '<geoip from CA>:'
#> iptables -A SSH_GEOIP -m geoip --src-cc DE -j LOG --log-prefix '<geoip from DE>:'
#> iptables -A SSH_GEOIP -m geoip --src-cc US -j LOG --log-prefix '<geoip from US>:'
#> iptables -A SSH_GEOIP -m geoip --src-cc JP -j LOG --log-prefix '<geoip from JP>:'
#> iptables -A SSH_GEOIP -m geoip --src-cc FR -j LOG --log-prefix '<geoip from FR>:'
#> iptables -A SSH_GEOIP -m geoip ! --src-cc CA,DE,US,JP,FR -j LOG --log-prefix '<geoip from UNKNOWN>:'
#> iptables -A INPUT -p tcp --dport 22 -j SSH_GEOIP

After applying above you should be able to get more clairity for all connections to destination port '22'
Back to top
View user's profile Send private message
jyoung
Guru
Guru


Joined: 20 Mar 2007
Posts: 445

PostPosted: Sun Mar 19, 2023 1:22 am    Post subject: Reply with quote

I've added the geoip logging to the script

Code:
#!/bin/bash                                                                                                                                                                                           

iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

iptables -N SSH_GEOIP
iptables -A SSH_GEOIP -m geoip --src-cc CA
iptables -A SSH_GEOIP -m geoip --src-cc DE
iptables -A SSH_GEOIP -m geoip --src-cc US
iptables -A SSH_GEOIP -m geoip --src-cc JP
iptables -A SSH_GEOIP -m geoip --src-cc FR
iptables -A SSH_GEOIP -m geoip ! --src-cc CA,DE,US,JP,FR
iptables -A INPUT -p tcp --dport 22 -j SSH_GEOIP

iptables -A INPUT -m geoip ! --src-cc US -j DROP
iptables -A INPUT -p TCP --dport ssh -j ACCEPT
iptables -A INPUT -p TCP --dport 2049 -j ACCEPT
iptables -A OUTPUT -p TCP --dport 2049 -j ACCEPT


Just to check, when I modify these rules, I stop the iptables service, run iptables -F to clear the rules, run this script, run iptables-save, and the restart the iptables service. But, where do the log entries go? I'm not seeing anything much different than before in /var/log/messages. Here's recent sample, which is fairly typical, and shows the hacker's persistent attempts:

Code:
Mar 18 21:12:37 homebox sshd[4014]: Failed password for root from 61.177.173.2 port 37005 ssh2
Mar 18 21:12:40 homebox sshd[4014]: Received disconnect from 61.177.173.2 port 37005:11:  [preauth]
Mar 18 21:12:40 homebox sshd[4014]: Disconnected from authenticating user root 61.177.173.2 port 37005 [preauth]
Mar 18 21:13:19 homebox sshd[3958]: fatal: Timeout before authentication for 61.177.173.2 port 23368
Mar 18 21:13:20 homebox sshd[3959]: fatal: Timeout before authentication for 61.177.173.2 port 32842
Mar 18 21:13:20 homebox sshd[3960]: fatal: Timeout before authentication for 61.177.173.2 port 33664
Mar 18 21:13:21 homebox sshd[3961]: fatal: Timeout before authentication for 61.177.173.2 port 35456
Mar 18 21:13:21 homebox sshd[3962]: fatal: Timeout before authentication for 61.177.173.2 port 39682
Mar 18 21:13:22 homebox sshd[3963]: fatal: Timeout before authentication for 61.177.173.2 port 48150
Mar 18 21:13:22 homebox sshd[2090]: exited MaxStartups throttling after 00:00:49, 2 connections dropped
Mar 18 21:13:23 homebox sshd[4071]: Invalid user pi from 71.172.103.242 port 41902
Mar 18 21:13:23 homebox sshd[4071]: pam_faillock(sshd:auth): User unknown
Mar 18 21:13:23 homebox sshd[4071]: pam_unix(sshd:auth): check pass; user unknown
Mar 18 21:13:23 homebox sshd[4071]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=71.172.103.242
Mar 18 21:13:23 homebox sshd[4071]: pam_faillock(sshd:auth): User unknown
Mar 18 21:13:23 homebox sshd[4073]: Invalid user pi from 71.172.103.242 port 41910
Mar 18 21:13:23 homebox sshd[4073]: pam_faillock(sshd:auth): User unknown
Mar 18 21:13:23 homebox sshd[4073]: pam_unix(sshd:auth): check pass; user unknown
Mar 18 21:13:23 homebox sshd[4073]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=71.172.103.242
Mar 18 21:13:23 homebox sshd[4073]: pam_faillock(sshd:auth): User unknown
Mar 18 21:13:25 homebox sshd[4071]: Failed password for invalid user pi from 71.172.103.242 port 41902 ssh2
Mar 18 21:13:25 homebox sshd[4071]: Connection closed by invalid user pi 71.172.103.242 port 41902 [preauth]
Mar 18 21:13:25 homebox sshd[4073]: Failed password for invalid user pi from 71.172.103.242 port 41910 ssh2
Mar 18 21:13:26 homebox sshd[4073]: Connection closed by invalid user pi 71.172.103.242 port 41910 [preauth]
Mar 18 21:13:46 homebox sshd[4092]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.177.173.2  user=root
Mar 18 21:13:47 homebox sshd[4095]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.177.173.2  user=root
Mar 18 21:13:49 homebox sshd[4092]: Failed password for root from 61.177.173.2 port 37816 ssh2
Mar 18 21:13:49 homebox sshd[4095]: Failed password for root from 61.177.173.2 port 40828 ssh2
Mar 18 21:13:51 homebox sshd[4092]: Failed password for root from 61.177.173.2 port 37816 ssh2
Mar 18 21:13:51 homebox sshd[4095]: Failed password for root from 61.177.173.2 port 40828 ssh2
Mar 18 21:13:54 homebox sshd[4092]: Failed password for root from 61.177.173.2 port 37816 ssh2
Mar 18 21:13:54 homebox sshd[4092]: error: maximum authentication attempts exceeded for root from 61.177.173.2 port 37816 ssh2 [preauth]
Mar 18 21:13:54 homebox sshd[4092]: Disconnecting authenticating user root 61.177.173.2 port 37816: Too many authentication failures [preauth]
Mar 18 21:13:54 homebox sshd[4092]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.177.173.2  user=root
Mar 18 21:13:55 homebox sshd[4095]: Failed password for root from 61.177.173.2 port 40828 ssh2
Mar 18 21:13:55 homebox sshd[4095]: error: maximum authentication attempts exceeded for root from 61.177.173.2 port 40828 ssh2 [preauth]
Mar 18 21:13:55 homebox sshd[4095]: Disconnecting authenticating user root 61.177.173.2 port 40828: Too many authentication failures [preauth]
Mar 18 21:13:55 homebox sshd[4095]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.177.173.2  user=root
Mar 18 21:14:33 homebox sshd[4026]: fatal: Timeout before authentication for 61.177.173.2 port 46439
Mar 18 21:14:33 homebox sshd[4032]: fatal: Timeout before authentication for 61.177.173.2 port 47755
Mar 18 21:14:33 homebox sshd[4033]: fatal: Timeout before authentication for 61.177.173.2 port 49197


Previously, I hadn't considered it practice to have authorization by ssh key only; now I'm wonder if that's for the best. Still, it would be great to get iptables working.
Back to top
View user's profile Send private message
jyoung
Guru
Guru


Joined: 20 Mar 2007
Posts: 445

PostPosted: Sun Mar 19, 2023 1:44 am    Post subject: Reply with quote

Hu, correct me if I'm wrong, but accepting an established connection would let me ssh back into the machine that I ssh'd from, right? Or would it really allow all connections on the LAN?
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 21918

PostPosted: Sun Mar 19, 2023 3:46 am    Post subject: Reply with quote

jyoung wrote:
I've added the geoip logging to the script
Only in a very basic sense. You have hit counters, but no log records.
jyoung wrote:
But, where do the log entries go?
The iptables LOG target writes to the kernel log, which your syslog daemon will pick up.
jyoung wrote:
I'm not seeing anything much different than before in /var/log/messages.
You did not tell the kernel to log anything, so there are no new logs. You could still check the per-rule hit counters.
jyoung wrote:
Previously, I hadn't considered it practice to have authorization by ssh key only; now I'm wonder if that's for the best.
Yes, key-only authentication has been a recommended security practice for many years. Passwords can be guessed, but keys are large enough to be immune to guessing (except when using a Debian OpenSSL weak key).
jyoung wrote:
Hu, correct me if I'm wrong, but accepting an established connection would let me ssh back into the machine that I ssh'd from, right? Or would it really allow all connections on the LAN?
Neither. It allows the return traffic associated with the established connection to return successfully. Without it, you send to the peer, the peer responds, and you ignore the peer's answers, causing the connection to fail.
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1000
Location: Richmond Hill, Canada

PostPosted: Sun Mar 19, 2023 1:51 pm    Post subject: Reply with quote

jyoung wrote:
I've added the geoip logging to the script

Code:
#!/bin/bash                                                                                                                                                                                           

iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

iptables -N SSH_GEOIP
iptables -A SSH_GEOIP -m geoip --src-cc CA
iptables -A SSH_GEOIP -m geoip --src-cc DE
iptables -A SSH_GEOIP -m geoip --src-cc US
iptables -A SSH_GEOIP -m geoip --src-cc JP
iptables -A SSH_GEOIP -m geoip --src-cc FR
iptables -A SSH_GEOIP -m geoip ! --src-cc CA,DE,US,JP,FR
iptables -A INPUT -p tcp --dport 22 -j SSH_GEOIP

iptables -A INPUT -m geoip ! --src-cc US -j DROP
iptables -A INPUT -p TCP --dport ssh -j ACCEPT
iptables -A INPUT -p TCP --dport 2049 -j ACCEPT
iptables -A OUTPUT -p TCP --dport 2049 -j ACCEPT


Just to check, when I modify these rules, I stop the iptables service, run iptables -F to clear the rules, run this script, run iptables-save, and the restart the iptables service. But, where do the log entries go? I'm not seeing anything much different than before in /var/log/messages. Here's recent sample, which is fairly typical, and shows the hacker's persistent attempts:
iptables log output use 'kern' syslog facility so depend on your syslog daemon configuration. It may have rule place 'kern' facility to a file different from messages file.

You seems to choose not include the portion in example that create logs. i.e. '-j LOG --log-prefix ...' in this case I am not sure where of if any logs will create.

However you should be able to use this command
Code:
iptables -v -L SSH_GEOIP
To see the 'pkts' count to give you some idea of where packets came from.

jyoung wrote:
Previously, I hadn't considered it practice to have authorization by ssh key only; now I'm wonder if that's for the best. Still, it would be great to get iptables working.


May be you can consider the dynamic system fail2ban which can manage the iptables dynamically based on log entries in log file.
Back to top
View user's profile Send private message
jyoung
Guru
Guru


Joined: 20 Mar 2007
Posts: 445

PostPosted: Sun Mar 19, 2023 6:19 pm    Post subject: Reply with quote

Hmm, when I run

Code:
iptables -v -L SSH_GEOIP


I get
Code:
iptables: No chain/target/match by that name.


That sounds like iptables isn't actually running, or, if it is running, it's ignoring the rules. Searching for the daemon process with

Code:
ps axu | grep iptables


returns nothing. But, when I restart the firewall with new rules

Code:
rc-service iptables stop
iptables -F
./rules_script.bash
iptables-save
rc-service iptables start


I get a message saying that the firewall is up

Code:
* Saving iptables state ...       
 * Stopping firewall ...                     
# Generated by iptables-save v1.8.8 on Sun Mar 19 14:17:05 2023
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
:SSH_GEOIP - [0:0]
-A INPUT -p tcp -m tcp --dport 22 -j SSH_GEOIP
-A INPUT -m geoip ! --source-country US  -j DROP
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 2049 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 2049 -j ACCEPT
-A SSH_GEOIP -m geoip --source-country CA
-A SSH_GEOIP -m geoip --source-country DE
-A SSH_GEOIP -m geoip --source-country US
-A SSH_GEOIP -m geoip --source-country JP
-A SSH_GEOIP -m geoip --source-country FR
-A SSH_GEOIP -m geoip ! --source-country CA,DE,US,JP,FR
COMMIT
# Completed on Sun Mar 19 14:17:05 2023
 * Loading iptables state and starting firewall ... 
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1000
Location: Richmond Hill, Canada

PostPosted: Sun Mar 19, 2023 6:32 pm    Post subject: Reply with quote

jyoung,

iptables, is not a user space process. It just a tool to manipulate in kernel netfilter module.

everytime you issue a iptables command it does something to the netfilter's rule, it is active right away.

The procedure you described is a way to persist the netfilter rules you defined.

if you ran your iptables script than reboot without some sort of persistent strategy you will lost you rules that store in the iptable script until you ran it again.
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 21918

PostPosted: Sun Mar 19, 2023 6:59 pm    Post subject: Reply with quote

iptables-save "saves" the rules by printing them to stdout. You do not need to run it unless you want to see the rules or intend to redirect the output somewhere. As I interpret that output, your start overwrote whatever your custom script did, so the rules loaded now are not the ones you posted.
Back to top
View user's profile Send private message
jyoung
Guru
Guru


Joined: 20 Mar 2007
Posts: 445

PostPosted: Sun Mar 19, 2023 8:51 pm    Post subject: Reply with quote

Interesting, that would track with the seeming lack of dropped connections. So, iptables-save is basically the same as iptables -L. Here's what iptables -L prints for me after restarting the iptables service

Code:
iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:nfs
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:nfs
DROP       all  --  anywhere             anywhere             -m geoip ! --source-country US
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:nfs

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:nfs
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:nfs
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:nfs


If restarting the service wipes the rules, then does anyone know why they're still printed?
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 21918

PostPosted: Sun Mar 19, 2023 10:21 pm    Post subject: Reply with quote

I would say iptables-save is an inferior alternative to iptables -L, because iptables -L by design omits quite a bit of detail in the interest of brevity. Brevity is bad for troubleshooting posts. iptables-save shows almost all details by default. Only hit counters are omitted, and those can be shown with -c. As a matter of practice, I ask people to provide their iptables-save output when they want me to review their rules, because too many important details are hidden by iptables -L.

Starting the Gentoo iptables service replaces the rules with the ones saved from a prior run, which may be very permissive if nothing better was saved.

According to the limited output provided, you now have a rule which accepts all ssh traffic before considering its country of origin, which is consistent with your original complaint. It then accepts all that traffic 2 more times (uselessly, since a decision was already made), checks the country of origin, and accepts it again if the country check does not drop it (again, uselessly). It's possible that these rules are not what they seem, since you did not show the interface restriction, if any.
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1000
Location: Richmond Hill, Canada

PostPosted: Sun Mar 19, 2023 10:32 pm    Post subject: Reply with quote

jyoung wrote:
Interesting, that would track with the seeming lack of dropped connections. So, iptables-save is basically the same as iptables -L
Please note, iptables-save is not the same as iptables -L in term of output format.

jyoung wrote:
If restarting the service wipes the rules, then does anyone know why they're still printed?
You must have at some point in the past executed
Code:
rc-service iptables save
After you ran your iptables script. Using rc-service iptables save command cause the current active rules save in to /var/lib/iptables/rules-save. a "reload" command for rc-service or restart iptables service will cause the rules in /var/lib/iptables/rule-save load back in to kernel.

In anyway, you really need to use '-v' option in combind with -L option to get the packet count.
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4439
Location: Bavaria

PostPosted: Sun Mar 19, 2023 10:41 pm    Post subject: Reply with quote

jyoung,

have a look into "man iptables" ... ;-) You can delete all rules with "iptables -F" (from kernel; not from a saved file; you must do a save to have empty rules also in your file).

If you want your rules being activ again after your next reboot you (really) should add rc-script "iptables" to your runlevel "default" (if OpenRC; do the same if using systemd). This rc-script SAVES all rules into a file at shutdown and read this file again when booting (if you are using OpenRC you can do this save manually with "/etc/init.d/iptables save").

I recommend to view your rules with "iptables -L -v -n" (look into manpage) and then do sometimes a "iptables -Z" which set the packet count to zero, so you will see in which rule your counter rises up again.


Do you know what "stateful inspection" mean ? If no, let me explain shortly:

Your kernel inspects EVERY data packet. There was a time where you had only ONE chance to do firewalling; this one was (and still is):

1) If you want allow SSH traffic FROM your machine to another you must allow all packets FROM your machine with DESTINATION port 22 AND you must also allow all packets TO your machine with SOURCE port 22 ... BOTH WAYS ... THIS you have to do for every communication you want allow.

If you want allow that someone can connect to your sshd, you must allow packets TO your machine with DESTINATION port 22 AND also ...


TODAY we have a 2nd method: Statefull inspection:

2) You allow only the FIRST packet of your desired communication and your kernel knows which answer BELONGS to this packet. Now you can allow ALL ANSWERS with ONE rule: "iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT" for incoming packets and a 2nd rule for outgoing packets (if you do forwarding you need a 3rd one). These both rules will handle the COMPLETE session

You surely can imagine that this is easier than the old stateless inspection. Maybe you might look my firewall rules in (the 1st post):
https://forums.gentoo.org/viewtopic-t-1112806.html (its german; just look to my bash script for the INITIAL configuration)
Back to top
View user's profile Send private message
jyoung
Guru
Guru


Joined: 20 Mar 2007
Posts: 445

PostPosted: Tue Mar 21, 2023 5:41 pm    Post subject: Reply with quote

Okay, I think I see part of my issue. I was mixing up "rc-service iptables save" with "iptables-save". I'm now able to make edits to the firewall. That one hacker in China seems to have given up. However, I'm still having some trouble. Hackers with non-US IPs are still showing up in with "Failed password for invalid user", and also I can't ssh from the server to another machine on the LAN.

Here are the current firewall settings.

Code:
iptables-save
# Generated by iptables-save v1.8.8 on Tue Mar 21 13:34:07 2023
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [18:1560]
-A INPUT -s 192.168.0.0/24 -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -s 192.168.0.0/24 -p tcp -m tcp --dport 2049 -j ACCEPT
-A INPUT -m geoip ! --source-country US  -j DROP
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 2049 -j ACCEPT
-A OUTPUT -s 192.168.0.0/24 -p tcp -m tcp --dport 22 -j ACCEPT
-A OUTPUT -s 192.168.0.0/24 -p tcp -m tcp --dport 2049 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 2049 -j ACCEPT
COMMIT


pietinger, I noticed these lines in your setup:

Code:

iptables -A OUTPUT      -d 192.168.2.0/24 -p tcp --dport 22 ${logit} "!!! ACCEPT OUT SSH "
iptables -A OUTPUT      -d 192.168.2.0/24 -p tcp --dport 22 -j ACCEPT


I'm my setup, I'm attempting to allow local traffic with the lines including 192.168.2.0/24. What is 192.168.2.0/24?
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 21918

PostPosted: Tue Mar 21, 2023 7:10 pm    Post subject: Reply with quote

jyoung wrote:
Hackers with non-US IPs are still showing up in with "Failed password for invalid user",
Have you checked that the netfilter geoip logic agrees that these are non-US IP addresses? Geolocation has never been completely reliable.
jyoung wrote:
and also I can't ssh from the server to another machine on the LAN.
Please describe this in more detail. From what source IP does the connection come, to what destination IP does it go, and have you confirmed that the connection works when the local firewall is down?
jyoung wrote:
Code:
-A OUTPUT -s 192.168.0.0/24 -p tcp -m tcp --dport 22 -j ACCEPT
-A OUTPUT -s 192.168.0.0/24 -p tcp -m tcp --dport 2049 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 2049 -j ACCEPT
The address-unaware version makes the address-restricted version irrelevant. Additionally, checking the source IP on an outbound packet is probably not useful here. There are cases where it can be helpful, but I doubt your scenario is one of those cases.
jyoung wrote:
I'm my setup, I'm attempting to allow local traffic with the lines including 192.168.2.0/24. What is 192.168.2.0/24?
This is the iptables shortcut for representing an address using CIDR notation. A less compact form will be used if your mask cannot be represented this way.
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4439
Location: Bavaria

PostPosted: Tue Mar 21, 2023 7:21 pm    Post subject: Reply with quote

jyoung,

you surely know that targets like ACCEPT or DROP (and REJECT) are "end-station" targets ... as soon as one rule fits kernel immediately allow (or forbid) this packet and stops checking rules after this rule. The LOG target is NOT an "end-station" rule: Kernel just logs something and proceed with checking next rules ... and IF NO rule fits kernel does what is DEFAULT for this chain ... You have as default for your OUTPUT accepting all ... so, every rule which accepts special outgoing traffic is ... senseless ;-) ... because it would be allowed by your default. For INPUT you have default DROP (=good) ... so, HERE you must allow something or you will have no communication.

In my firewall I forbid ALL (also OUTPUT) as default ... so, I must allow not only incoming traffic; I must allow also outgoing traffic.

jyoung wrote:
I'm my setup, I'm attempting to allow local traffic with the lines including 192.168.2.0/24. What is 192.168.2.0/24?


With theses rules I LOG (no end-station) all outgoing SSH traffic first and then I allow it (end-station) ... BUT I allow only SSH to my local stations (destination) in my local network. I have 193.168.2.0 as my local network (I see you have 192.168.0.0 instead). See also more here: https://en.wikipedia.org/wiki/Private_network

You can not do outgoing SSH BECAUSE ... yes, you allow 1. packet out with destination port 22 ... BUT you have no rule to ALLOW answers FROM other machine. Here you have two choices:

1. stateless:

Code:
-A INPUT -p tcp -m tcp --sport 22 -j ACCEPT

(watch: --sport is source port)

2. stateful:

Code:
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT


This is MORE SECURE than stateless !

BTW: You are also missing to allow internal communication. Because you allow ALL outgoing as default you will need only one rule for incoming traffic on loopback. This is:
Code:
-A INPUT -i lo -j ACCEPT


(I have two rules because I have both - incoming and outgoing - with DROP default)
Back to top
View user's profile Send private message
jyoung
Guru
Guru


Joined: 20 Mar 2007
Posts: 445

PostPosted: Tue Mar 21, 2023 9:37 pm    Post subject: Reply with quote

Okay, Hu, I think pietinger's comment echos a comment you made earlier, but I think I understand now. I'm on on the server and try to ssh into another machine, that's the "established" connection, whereas earlier I assumed that the established connection would be the ssh link that I used to get into the server.

As of now I can ssh into machines in the LAN. Fantastic! And, I am actually not seeing any new hackers in /var/log/messages since this morning.

Here are the current rules. Does this look reasonable?

Code:
iptables-save
# Generated by iptables-save v1.8.8 on Tue Mar 21 17:36:57 2023
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT DROP [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -s 192.168.0.0/24 -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -s 192.168.0.0/24 -p tcp -m tcp --dport 2049 -j ACCEPT
-A INPUT -m geoip ! --source-country US  -j DROP
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 2049 -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
-A OUTPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A OUTPUT -s 192.168.0.0/24 -p tcp -m tcp --dport 22 -j ACCEPT
-A OUTPUT -s 192.168.0.0/24 -p tcp -m tcp --dport 2049 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 2049 -j ACCEPT
COMMIT
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 21918

PostPosted: Tue Mar 21, 2023 10:00 pm    Post subject: Reply with quote

You are getting closer. Allowing NFS over the Internet is unusual, so I wonder if you really meant to do that. As I noted before, your output rules are weird. You don't need source-specific rules if you reiterate them without a source qualifier. As I also wrote above, checking the source IP in the output rules is rarely useful. Did you mean to check the destination IP? Also, I should note that you presently do not allow outbound http/https, which may break the system package manager's ability to download updates.
Back to top
View user's profile Send private message
jyoung
Guru
Guru


Joined: 20 Mar 2007
Posts: 445

PostPosted: Tue Mar 21, 2023 10:07 pm    Post subject: Reply with quote

Quote:
You don't need source-specific rules if you reiterate them without a source qualifier.


Okay, if I understand correctly, the lines

Code:
-A OUTPUT -s 192.168.0.0/24 -p tcp -m tcp --dport 22 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 22 -j ACCEPT


are unecessary since any connections they would accept are already accepted by
Code:
-A OUTPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT


I do actually use NFS over the internet, so I need those connections.


Last edited by jyoung on Tue Mar 21, 2023 11:45 pm; edited 1 time in total
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum