setup-ipset.sh 828 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #
  2. # 该脚本用于禁止 jsporxy 用户访问内网,防止 SSRF 攻击
  3. # 需要 root 权限运行,依赖 ipset 命令
  4. #
  5. if [[ $(iptables -L | grep "anti ssrf") ]]; then
  6. exit
  7. fi
  8. ipset create ngx-ban-dstip hash:net
  9. iptables \
  10. -m comment --comment "anti ssrf" \
  11. -A OUTPUT \
  12. -p tcp --syn \
  13. -m owner --uid-owner jsproxy \
  14. -m set --match-set ngx-ban-dstip dst \
  15. -j REJECT
  16. # https://en.wikipedia.org/wiki/Reserved_IP_addresses
  17. REV_NET=(
  18. 0.0.0.0/8
  19. 10.0.0.0/8
  20. 100.64.0.0/10
  21. 127.0.0.0/8
  22. 169.254.0.0/16
  23. 172.16.0.0/12
  24. 192.0.0.0/24
  25. 192.0.2.0/24
  26. 192.88.99.0/24
  27. 192.168.0.0/16
  28. 198.18.0.0/15
  29. 198.51.100.0/24
  30. 203.0.113.0/24
  31. 224.0.0.0/4
  32. 240.0.0.0/4
  33. 255.255.255.255/32
  34. )
  35. for v in ${REV_NET[@]}; do
  36. ipset add ngx-ban-dstip $v
  37. done
  38. # 可屏蔽更多的网段:
  39. # ipset add ngx-ban-dstip xxx