首页 文章

如何只允许隧道连接到端口?

提问于
浏览
5

我想让一个git-daemon通过一个永久的ssh隧道 . 我完成了这项任务 . 如何阻止任何与GIT_DAEMON端口的远程无连接连接(在我的情况下为9418)?

我已经在iptables中尝试了简单的规则(阻止除localhost之外的所有内容):

$ iptables -A INPUT -p tcp -d ! localhost --destination-port 9418 -j DROP

但它也会阻塞隧道(因为它节省了源IP地址) . 如果我还有一个主机用于防火墙,可以通过阻止与该端口的任何远程连接来完成,但是我需要这个主机来完成这项工作 .

隧道以两种方式之一创建:

对于Windows:

plink.exe -N -i <key> -L 127.0.0.1:9418:192.168.1.69:9418 tunnel@192.168.1.69

对于Linux:

ssh -N -i <key> -L 127.0.0.1:9418:192.168.1.69:9418 tunnel@192.168.1.69

2 回答

  • 7

    你可以试试这个( untested ):

    # accept localhost
    iptables -A INPUT -p tcp -d localhost --destination-port 9418 -j ACCEPT
    
    # send everyone else packing
    iptables -A INPUT -p tcp --destination-port 9418 -j DROP
    

    使用 iptables -L 说:

    ACCEPT     tcp  --  anywhere             localhost.localdomain tcp dpt:git
    DROP       tcp  --  anywhere             anywhere            tcp dpt:git
    

    EDIT

    这(可能)是如何设置隧道的:

    ssh -N -i <key> -L 127.0.0.1:9418:127.0.0.1:9418 tunnel@192.168.1.69
    

    It's important that the second half is 127.0.0.1 and NOT a normal IP

  • 3

    实际上你可以在不使用iptables的情况下实现这一点,只需将 git-daemon 绑定到loopback接口,例如 .

    git daemon --listen=127.0.0.1
    

    这将使它只能从localhost连接,并且不需要root权限来设置 .

相关问题