Giter Site home page Giter Site logo

haojie06 / selfhelp-iptables Goto Github PK

View Code? Open in Web Editor NEW
127.0 3.0 24.0 10.75 MB

通过http api自助添加iptables白名单与黑名单的工具,防止nmap等程序的端口扫描和恶意主动探测,防止ssh、mysql等敏感服务受到攻击,并能对探测进行记录。

Home Page: https://aoyouer.com/posts/selfhelp-iptables/

Go 100.00%

selfhelp-iptables's Introduction

selfhelp-iptables 通过http api 添加iptables黑/白名单

自助添加iptables白名单的工具,可以通过http请求来向iptables添加白名单,防止不怀好意之人的端口扫描和恶意探测。

暂时不支持docker bridge network模式(host模式可用)

只在debian/ubuntu上测试过,另外需要关闭自带的一些防火墙管理程序,仅使用iptables。有ufw的话先执行ufw disable,有firewalld(如centos)的话先执行systemctl stop firewalld

wget https://github.com/haojie06/selfhelp-iptables/releases/download/[version]/selfhelp-iptables
chmod +x selfhelp-iptables && setcap 'cap_net_admin=+ep' selfhelp-iptables
# 查看帮助
./selfhelp-iptables help
#拦截1080端口与1081端口
./selfhelp-iptables start -u userkey -a adminkey -p 1080 -p 1081
#或者全端口拦截 放行 22 80 443, 以及局域网内的ip
./selfhelp-iptables start -u userkey -a adminkey -w 22 -w 80 -w 443 --allow 192.168.0.1/24

有两种运行模式

  1. 全端口访问白名单限制

    默认阻止任何外部ip访问本机的任何端口(除了22以及程序http api监听的端口

  2. 特定端口访问白名单限制

    默认阻止任何外部ip访问本地的特定端口 带上参数 -protect后自动启用 建议使用该模式

使用第一种运行模式,执行程序后,所有的端口都会被禁止访问(默认放行了22端口的访问和icmp请求),之后请求 http://example.com:8080/api/add?key=[你设置的key] 可以将你的ip添加到白名单里面。

使用第二种运行模式,启动程序时还要指定一个参数 -protect [端口号 多个端口的话需要重复多次] 如 --protect 80 --protect 443 ,程序启动后会阻断对80 443的访问,在访问 http://example.com:8080/api/add?key=[你设置的key] 后可以添加白名单

注意,程序需要能获取到访问者的ip才能添加白名单,需要确保真实的ip能传递给该服务,如果使用nginx等程序对http api进行反向代理,需要带上 --reverse 参数以开启反向代理支持,然后在nginx中添加 X-Real-Ip 或者 X-Forwarded-For 字段传递请求者的ip。

建议使用第二种方法,全端口拦截会出现很多问题,如DNS无法查询之类的。

实时日志设置

~~开始运行后,程序会尝试去寻找iptables的日志文件,并实时读取文件,当文件更新时,实时输出内容。不同系统的日志目录有差异。~~

在新版本中,程序已经改为从nflog中读取日志,不再依赖系统日志文件。

参数介绍

启动参数介绍

  • -a adminkey

    必须带上,用于控制api的key

  • -u userkey

    userkey仅作为访问 /api/add?key= 时添加访问者ip到白名单的密钥使用,可分享给他人

  • -l port

    可选参数,该程序监听的端口,默认8080

  • -p port1

    可选参数,如果 带上了该参数,程序以第二种模式运行,即只限制部分端口的访问 多个端口的话,使用多个 -p 参数

  • --allow ip1

    可选参数,放行的ip,使用多个 --allow 参数 以添加多个ip, 支持 cidr(如192.168.0.1/24)

  • -w port1

    可选参数,放行的端口

  • -t n

    可选参数 自动添加白名单的阈值(依赖日志读取),当连接失败次数超过n次时,自动为该ip添加白名单。

  • -r [param]

    可选参数, 如果开启了自动添加白名单的功能时,是否需要进行自动重置,并指定重置周期,可选周期如下:

    • hh 每半小时重置
    • h 每小时重置
    • hd 每半天重置
    • d 每天重置
    • w 每周重置
  • --reject -d 可选参数,开启后对于被拦截端口的访问会返回一个拒绝连接的icmp包

  • --reverse 开启反向代理支持,读取http请求头获取客户端ip,请仅在使用可信反向代理的情况下使用,nginx示例:

            location /api {
             proxy_set_header  X-real-ip $remote_addr;
             proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_pass http://127.0.0.1:8080/api;
          }
    

    需要注意,在开启反向代理支持之后,不会默认为http api端口添加白名单,所以可以使用 -p [http api 监听的端口] 来阻止外部直接请求http api(必须走反向代理)

  • --trigger [counts]/[seconds]

    包速率触发器,一个有意思的小功能,可以实现基本无感的白名单添加,当某一ip向指定端口发送包的速率超过某一个阈值的时候(比如 --trigger 9/3 表示平均三秒内有多于9个包),为其添加白名单。 不过这个添加方式存在误判的可能性,但是对于大范围的nmap扫描等探测方式,可以实现既能够拦截恶意探测,也能够为用户自动地添加白名单。

控制台参数介绍

程序启动后,可以直接通过标准输入输入命令进行操作

  • help 显示帮助

  • add 添加ip白名单

  • list 列出当前添加的ip

  • remove 移除添加的ip

  • record 列出 探测ip以及次数记录

  • reset 重置所有记录 包括连接记录和白名单 几种请求 (不通过浏览器,直接使用curl也可以)

  • 添加白名单

    http://example.com:8080/api/add?key=[你设置的userkey] 程序会获取访问者的ip,并添加到iptables白名单中。

  • 列出当前白名单

    http://example.com:8080/api/list?key=[你设置的adminkey]

  • 删除白名单

    http://example.com:8080/api/remove/[要删除的ip]?key=[你设置的adminkey]

  • 查看探测记录

    http://example.com:8080/api/log?key=[你设置的adminkey]

  • 查看探测计数

    http://example.com:8080/api/record?key=[你设置的adminkey]

程序对系统的影响

该程序的拦截规则是在一条新的链里面进行的,在退出程序后程序会清空这条规则链,所以如果设置错了导致连不上ssh,那么可以尝试使用vnc连接到机器上,ps aux |grep self 找到进程并kill即可, 或者直接重启系统,也能清空规则链。

或者手动清理规则链

iptables -vnL # 查看多出来的链
iptables -D INPUT -j PROTECT_LIST
iptables -F PROTECT_LIST
iptables -X PROTECT_LIST

systemd

mv selfhelp-iptables /usr/bin/
chmod +x /usr/bin/selfhelp-iptables
[Unit]
Description=selfhelp iptables
After=iptables.service

[Service]
Type=simple
#ExecStart=/bin/bash /root/start.sh
ExecStart=/usr/bin/selfhelp-iptables start -a 123 -u 123 -p 123 -l 81 -p 80 --reverse
Restart=always
StandardInput=tty
StandardOutput=append:/var/log/selfhelp_iptables.log
StandardError=append:/var/log/selfhelp_iptables.log
TTYPath=/dev/tty12

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable selfhelp-iptables
systemctl start selfhelp-iptables

配置文件支持

项目目录中提供了一个示例配置文件 config.yaml,可以通过 -c 参数指定配置文件路径:

protectPorts:  # 需要保护的端口, 默认会拦截外部对这些端口的请求
  - 8001
  - 8002
whitelistedPorts: # 白名单端口
  - 22
  - 23
allowIPs: # 白名单IP,支持cidr形式
  - 192.168.0.1/24
adminKey: 1234 # 用于执行管理的key
userKey: 123 # 用于执行添加白名单的key
listenPort: 8081 # http api监听端口
autoAddThreshold: -1 # 自动添加的阈值,当接收到的包超过这个值时自动添加白名单,-1时不会自动添加
# autoReset: d  # 自动重置已添加的白名单和黑名单, 可以指定周期时常
reject: false # 采用reject进行响应,而不是默认的drop
# rateTrigger: 10/3 # 包速率触发器, 当包速率超过这个值时自动添加白名单,不设置时不会自动添加
reverseProxySupport: false # 是否开启反向代理header的支持(x-forwarded-for等header)

除此之外,程序还会尝试在当前目录及 /etc/selfhelp-iptables/ 中寻找config.yaml文件,如果找到了,会自动加载。配置文件中的选项可以通过url flag来覆盖, 在使用配置文件的情况下,systemd的service文件可以修改ExecStart的部分为: ExecStart=/usr/bin/selfhelp-iptables start

selfhelp-iptables's People

Contributors

haojie06 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

selfhelp-iptables's Issues

systemd service启动程序,CPU占用100%

由于shell启动程序后关掉似乎会停止程序,也为了方便开机启动,自己写了一个service启动,但是启动后cpu占用一直维持100%,在三台vps上皆如此,手动命令不会出现该问题。
系统:ubuntu 20
vps:甲骨文
相关:ufw开启(尝试关闭ufw再systemctl启动,无改善)
service配置如下:
[Unit]

Description=whitelist

After=network.target

[Service]

Type=simple

ExecStart= /root/selfhelp-iptables start -u 用户密码 -a 管理密码 -l web端口 -p 白名单管理端口 -r w --trigger 10/3

Restart=always

PrivateTmp=true

[Install]

WantedBy=multi-user.target

image

这个错了吧

-a [param]

可选参数, 如果开启了自动添加白名单的功能时,是否需要进行自动重置,并指定重置周期,可选周期如下:

能否增加内网网段白名单?

内网网段白名单能自己添加吗?或者后台运行程序了,可以通过什么方式添加白名单进去,因为每次重启selfhelp-iptables,白名单都会被清空。

两个问题:GET /log 错误;反代后怎样屏蔽 8080 端口?

有两个问题:
1、CentOS8 上,GET /log 一直空白。终端提示:执行命令cat | grep netfilter出错;exit status 1。根据你文中的说明改了 iptables.log 的路径后一样错误。其他 api 如 GET /record 等功能正常。
我自己尝试执行 cat /var/log/iptables.log |grep netfilter 并无问题,能显示有记录。

2、开启 --reverse 参数后,目的是用 NGINX 反代并隐藏掉本身的 api 接口才对吧?但这时 8080 端口依然可以直接访问,理应开启 reverse 后只允许 127.0.0.1 或指定 IP 访问才对吧?

有什么更好的方法可以做到吗?

我现在是在软件启动后,删掉原本的 iptable 规则,再加上新的规则:

RuleLine=`iptables -L -n --line-number | grep 8080|awk '{ print $1}'`
echo "Delete Rule NO.${RuleLine}"
iptables -D SELF_WHITELIST ${RuleLine}

iptables -I SELF_WHITELIST -p tcp --dport 8080 -j DROP
iptables -I SELF_WHITELIST -s 127.0.0.1 -p tcp --dport 8080 -j ACCEPT

--trigger [counts]/[seconds] 请教

本人小白,请问 --trigger [counts]/[seconds] 使用方式:

我执行命令 ./selfhelp-iptables start -u jdsxSjfimoPsx -a ooppee3A --trigger 9/3 -p 13292 后:

在浏览器通过访问 google.com 并且在三秒内刷新九次以上,会自动添加当前 IP 白名单吗?

因为我通过上述方式发现没有添加白名单,请问怎么使用?

端口号配置有 bug

更新:似乎是没用 root 权限启动引起的。建议软件在启动时检查权限,非 root 提示错误。

参数 -p 端口1 端口2 的方式似乎无效?

selfhelp-iptables start -l 8888 -u user -a password **-p 8888 1234** --reverse
这样,运行结果:

enable reverse proxy support
protected ports: [8888] whitelisted ports: []
whitelisted ips: []

这种写法似乎仅读取到第一个端口号。实际没保护到 1234,而且 8888 也依然能被直接访问。

而当我改成
selfhelp-iptables start -l 8888 -u user -a password -p 8888 -p 1234 --reverse
结果为:

enable reverse proxy support
protected ports: [8888 1234] whitelisted ports: []
whitelisted ips: []

虽然日志上显示保护了 2 个端口,但实际上,8888 依然能被外界直接访问到。

Readme部分文字不理解其含义

不会默认为http api端口添加白名单,所以可以使用 -p [http api 监听的端口] 来阻止外部直接请求http api(必须走反向代理)

-p httpapi端口之后 反代不也无法请求了吗?
而且反代和不反代,访问方式不是一样的吗?还是说反代只是为了加个域名或者套个cdn

新版本又出现 GLIBC 错误

/selfhelp-iptables: /lib64/libc.so.6: version GLIBC_2.32' not found /selfhelp-iptables: /lib64/libc.so.6: version GLIBC_2.34' not found

OS: CentOS8

ipv6支持

在支持ipv6环境下,程序无法拦截通过ipv6连接的请求,也无法触发包速率记录。
随着网络环境的监察检测的越发严重,以及ipv6的普及,希望能加上对ipv6的支持。

启动失败:GLIBC_2.32 not found

新版本启动失败:

/selfhelp-iptables: /lib64/libc.so.6: version 'GLIBC_2.32' not found (required by ...)

CentOS 8,已经通过 dnf update glibc-devel 到最新。目前最新只有 2.28。网上查了下,似乎需要手工下载 glibc 源码编译新版本,但如果机器比较多,一个个去编译安装不方便。

我问了下 ChatGPT 的回答:

如果您无法升级或安装 GNU C 库,则可以考虑将 GO 应用程序编译为静态二进制文件。这样,您的应用程序将包含所有必要的库文件,不需要在运行时从系统中加载。要将 GO 应用程序编译为静态二进制文件,请使用以下命令:

go build -a -tags netgo -ldflags '-w -extldflags "-static"' -o your-binary-name your-source-file.go

将 "your-binary-name" 替换为您的二进制文件名称,将 "your-source-file.go" 替换为您的 GO 源代码文件。

IPv6尝试添加白名单时报错

IPv4正常, IPv6有以下提示, 也没有添加成功

[绿色] 2023-04-21 13:48:21 ip: [IPv6 ADDRESS] try to request API: AddWhitelist has been allowed [/绿色] 

[红色] 
running [/usr/sbin/iptables -t filter -I BANDWIDTH_IN 1 -s [IPv6 ADDRESS] -j RETURN --wait]: exit status 2: iptables v1.8.7 (nf_tables): host/network `[IPv6 ADDRESS]' not found
Try `iptables -h' or 'iptables --help' for more information.

running [/usr/sbin/iptables -t filter -I BANDWIDTH_OUT 1 -d [IPv6 ADDRESS] -j RETURN --wait]: exit status 2: iptables v1.8.7 (nf_tables): host/network `[IPv6 ADDRESS]' not found
Try `iptables -h' or 'iptables --help' for more information.

running [/usr/sbin/iptables -t filter -I PROTECT_LIST 1 -s [IPv6 ADDRESS] -j ACCEPT --wait]: exit status 2: iptables v1.8.7 (nf_tables): host/network `[IPv6 ADDRESS]' not found
Try `iptables -h' or 'iptables --help' for more information.
[/红色] 

[绿色] add whitelisted ip: [IPv6 ADDRESS] [/绿色] 

退出没有清空 iptable 规则

看到你新版说到清理 chain 的问题,我看了下,问题似乎依然存在。

我使用 systemctl 的方式启动,停止服务后,iptable 规则不会清除。

[Unit]
Description=iptable_whitelist
After=network.target

[Service]
Type=simple
ExecStart=/path/to/script/start.sh
Restart=always
#PrivateTmp=true
StandardInput=tty
StandardOutput=tty
StandardError=null
TTYPath=/dev/tty12

[Install]
WantedBy=multi-user.target

start.sh

#!/bin/bash
/path/to/script/selfhelp-iptables start ...bla bla... --reverse > ./out.txt 2>&1

另外,之前这样可以输出内容到 out.txt,但现在似乎不输出任何东西。

问个参数问题

--reverse 开启反向代理支持,读取http请求头获取客户端ip,请仅在使用可信反向代理的情况下使用,nginx示例:
这个反向代理是指什么呢,反代网页吗?能简单说明下吗

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.