代码之家  ›  专栏  ›  技术社区  ›  Pcsl

将我当前的IP添加到iptables的白名单?

  •  0
  • Pcsl  · 技术社区  · 10 年前

    我对设置游戏服务器很陌生,但我想阻止每个ip的rcon,除了那些被列入白名单的ip。

    首先,我将使用SSH:

    iptables -A INPUT -p tcp --destination-port 27015 -j LOG --log-prefix "SRCDS-RCON " -m limit --limit 1/m --limit-burst 1
    iptables -A INPUT -p tcp --destination-port 27015 -j DROP
    

    之后,我希望当用户运行bash脚本或类似脚本时,它检测用户IP并自动将其添加到白名单中。

    我该怎么做?

    1 回复  |  直到 10 年前
        1
  •  1
  •   leucos Matt Huggins    10 年前

    假设:

    • bash脚本在服务器上运行
    • 用户使用ssh登录

    您可以创建ipset:

    首先,在iptables中添加此规则:

    iptables -A INPUT -i eth0 -m set --match-set whitelist src -p tcp --destination-port 27015 -j ACCEPT
    

    然后创建集合:

    sudo ipset -N whilelist iphash
    

    最后,使用SSH_CONNECTION环境变量添加如下脚本:

    #!/bin/bash
    USER_IP=$(echo $SSH_CONNECTION | cut -f1 -d' ')
    sudo ipset -A whitelist $USER_IP
    

    你甚至可以在 /root/.bash_profile 所以当有人连接到 root .

    然而 ,这假设您的朋友通过ssh以root身份连接。由于这是不可取的,您可以使用临时目录来保存ip地址,并添加cron作业来填充ipset哈希:

    • 创造 /etc/cron.d/check_ipset 具有:

      * * * * *   root    /usr/local/bin/check_ipset
      
    • 创造 /usr/local/bin/check_ipset (和 chmod 700 ) :

      #!/bin/bash
      for i in `cat /tmp/ipset_pending | sort -u`; do 
        ipset -A whitelist $i
      done
      cat /dev/null > /tmp/ipset_pending
      
    • 将其添加到每个用户的.bash_profile中:

      ...
      echo $SSH_CONNECTION | cut -f1 -d' ' >> /tmp/ipset_pending
      ...
      

    没有测试,所以YMMV,但这应该足够接近。