AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 754030
Accepted
fadedbee
fadedbee
Asked: 2016-02-05 07:11:19 +0800 CST2016-02-05 07:11:19 +0800 CST 2016-02-05 07:11:19 +0800 CST

如何使用 iptables 或 tc 来限制每个客户端的数据包。

  • 772

我有一些发送数据太快的问题网络客户端。

我想减慢他们的速度,使用 iptables,或者可能是 tc。

我见过 iptables 解决方案,例如:

sudo iptables -A INPUT -m state --state RELATED,ESTABLISHED -m limit --limit 50/second --limit-burst 50 -j ACCEPT

但我认为该限制适用于与规则匹配的所有内容,而不是每个客户地址。

有没有办法让这个规则限制每个客户端地址的数据包?

linux
  • 3 3 个回答
  • 4930 Views

3 个回答

  • Voted
  1. parkamark
    2016-02-05T13:06:41+08:002016-02-05T13:06:41+08:00

    我已经使用 TC 和 iptables hashlimit 的组合来做到这一点。我在 LAN 接口(以下载流量为目标)上创建了一个 TC 出站带宽限制器,设置为 5 Mbits/秒。然后我在接口的输出 mangle 链中使用 iptables hashlimit 模块,这样如果数据包速率超过某个阈值,在任何两个不同的源 IP 地址和目标 IP 地址之间,它开始将这些数据包分类到 TC 5 Mbit/s 流量整形类.

    但是,您必须正确设置数据包阈值,并且我基于我的 MTU 为 1500 字节这一事实,因此使用它来计算每秒有多少数据包将创建一个每秒 1.5 Mbits 的阈值(1000 个数据包/秒)。再加上在 hashlimit iptables 模块上设置可能的最高突发值(在我的设置中,这似乎是 10,000),所有这一切的结果是短时间下载以全速运行,但较长时间的下载开始变慢,因为一些数据包进入了 TC 带宽限制类,这显然会降低数据包速率等。这有点像 hack,但令人惊讶的是它确实有效,而且效果很好。并且因为使用了 TC,所以不会丢弃任何数据包 - 一旦达到数据包速率,就会因为被推入 TC 类而简单地延迟和减速。

    这是在 CentOS 6 机器上。我认为更现代的东西允许 hashlimit 模块支持每秒字节数,而不仅仅是每秒数据包,这甚至更好,但我在我的设置中尝试了这个,它只是恢复为每秒使用数据包。

    我刚刚在手机上,所以无法粘贴任何配置,但是如果您想要一些示例,请告诉我,我将编辑此答案。我真的很喜欢这个解决方案,因为我所追求的限制是基于源 IP 和目标 IP。因此系统将每个不同的 src+dst IP 组合视为自己的数据包流进行限制。hashlimit 模块也支持源端口和目标端口(基本上是源 IP、源端口、目标 IP、目标端口的任意组合),因此您甚至可以在源 IP+端口和目标 IP+端口之间基于每个会话进行限制.

    更新

    因此,以下是您的大致操作方式;将需要进行实验。

    tc:

    /sbin/tc qdisc add dev eth0 root handle 1: htb
    /sbin/tc class add dev eth0 parent 1: classid 1:1 htb rate 5Mbit
    

    iptables:

    #!/bin/bash
    
    [[ "$1" =~ ^[ADI]$ ]] || exit 1
    
    for IPT in /sbin/ip{,6}tables
    do
      $IPT -t mangle -$1 POSTROUTING -o eth0 -m hashlimit --hashlimit-above 1000/second --hashlimit-burst 10000 --hashlimit-mode srcip,dstip --hashlimit-name limiter -j CLASSIFY --set-class 1:1
    done
    
    • 4
  2. Best Answer
    c4f4t0r
    2016-02-05T10:49:58+08:002016-02-05T10:49:58+08:00

    你可以用简单的方法解决这个问题,尝试使用带iptables的recent模块,最近跟踪源地址:

    iptables -m recent -h
    recent match options:
    [!] --set                       Add source address to list, always matches.
    [!] --rcheck                    Match if source address in list.
    [!] --update                    Match if source address in list, also update last-seen time.
    [!] --remove                    Match if source address in list, also removes that address from list.
        --seconds seconds           For check and update commands above.
                                    Specifies that the match will only occur if source address last seen within
                                    the last 'seconds' seconds.
        --reap                      Purge entries older then 'seconds'.
                                    Can only be used in conjunction with the seconds option.
        --hitcount hits             For check and update commands above.
                                    Specifies that the match will only occur if source address seen hits times.
                                    May be used in conjunction with the seconds option.
        --rttl                      For check and update commands above.
                                    Specifies that the match will only occur if the source address and the TTL
                                    match between this packet and the one which was set.
                                    Useful if you have problems with people spoofing their source address in order
                                    to DoS you via this module.
        --name name                 Name of the recent list to be used.  DEFAULT used if none given.
        --rsource                   Match/Save the source address of each packet in the recent list table (default).
        --rdest                     Match/Save the destination address of each packet in the recent list table.
        --mask netmask              Netmask that will be applied to this recent list.
    

    阻止 ssh 暴力破解的示例:

    iptables -A INPUT -i eth0 -p tcp --syn --dport 22 -m recent --name ssh --set
    iptables -A INPUT -i eth0 -p tcp --syn --dport 22 -m recent --name ssh --rcheck --seconds  30 --hitcount 2 -j DROP
    
    • 3
  3. remote mind
    2016-02-05T10:38:58+08:002016-02-05T10:38:58+08:00

    您也许可以使用 connlimit 模块解决此问题:

    http://www.cyberciti.biz/faq/iptables-connection-limits-howto/

    • 0

相关问题

  • Linux 主机到主机迁移

  • 如何在 Linux 机器上找到有关硬件的详细信息?

  • 如何在 Linux 下监控每个进程的网络 I/O 使用情况?

  • 在 RHEL4 上修改 CUPS 中的现有打印机设置

  • 为本地网络中的名称解析添加自定义 dns 条目

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve