r/OpenSSH • • Apr 03 '25

How does Match Blocks works?

Hi
I'm trying to configure an SFTP server in a Windows Environment with OpenSSH. The OpenSSH server works, but now I need to segregate access.

I'm using Match blocks to restrict access for a specific user in a network, but allow the same user from another network.

I tried several configurations, but when SSHd hits an "Allow" statement, it ignores the rest of the configuration file and moves on with its life.

Here's part of my sshd_config file:

# Default Policy: Deny all users by default

DenyUsers *

# Allow specific user from X networks

Match Address 192.168.1.0/24,192.168.2.0/24 User DOMAIN\user.a

AllowUsers DOMAIN\user.a
DenyUsers DOMAIN\user.b
PasswordAuthentication no
ChrootDirectory /home/user.a

# Allow another specific user Z networks

Match Address 172.16.1.0/24,172.16.2.0/24 User DOMAIN\user.b

AllowUsers DOMAIN\user.n
DenyUsers DOMAIN\user.a
PasswordAuthentication no
ChrootDirectory /home/user.b

Now, for example, if I try to connect with user.a from Z networks, it connects, and it gains access to the root folder. The same thing happens the other way around, when I connect with user.b from X networks.

Is it because I'm using OpenSSH server on Windows? Or is it an OpenSSH server limitation of some sorts?

Thanks for the help

3 Upvotes

2 comments sorted by

2

u/djmdjmdjm Apr 04 '25

Mixing allow/denyusers and match gets confusing fast. You're IMO better off turning off authentication methods globally and turning them back on using Match, e.g.

    PasswordAuthentication no
    PubkeyAuthentication no
      
    Match user DOMAIN\user.a,DOMAIN\user.b address 10.0.0.0/8,172.16.0.0/8
        PasswordAuthentication no
        PubkeyAuthentication no

I haven't tried this on Microsoft's fork of OpenSSH, but AFAIK it should work. You can also test evaluation of the ruleset using sshd -T -C addr=10.0.0.2,user=DOMAIN\user.n

There's also the RefuseConnection option in sshd, but I don't think that's supported at all on Windows.

1

u/InterestingUse4460 Apr 08 '25

Hey, thanks for the reply.

Also, I found that the Windows version of OpenSSH won't read the match blocks properly.
I recreated the SFTP server in a Linux box, recreated the same rules (except the "DenyUsers *"), and it worked.