已加载的 iptables 模块列表

发布于 2024-08-17 00:29:11 字数 128 浏览 8 评论 0原文

有没有方便的方法来显示加载的 iptables 模块列表?我可以通过列出 /lib/iptables/ (或 /lib64/iptables/)目录来显示已安装的模块,但我需要活动模块列表。

Is there any convenient way to show loaded iptables module list? I can show installed modules by listing /lib/iptables/ (or /lib64/iptables/) directory but I need active modules list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

紫竹語嫣☆ 2024-08-24 00:29:11

加载的 iptables 模块可以在 /proc/net/ip_tables_matches proc 文件系统条目中找到。

cat /proc/net/ip_tables_matches

在 PHP 中,我可以通过加载和分解文件内容来访问加载的 iptables 模块:

$content = file_get_contents('/proc/net/ip_tables_matches');
$modules = explode("\n", $content);

当然,它需要安装 proc 文件系统(大多数 GNU Linux 发行版默认安装它)

Loaded iptables modules can be found in /proc/net/ip_tables_matches proc filesystem entry.

cat /proc/net/ip_tables_matches

In PHP I can access the loaded iptables modules by loading and exploding file contents:

$content = file_get_contents('/proc/net/ip_tables_matches');
$modules = explode("\n", $content);

Of course it requires proc filesystem to be mounted (Most GNU Linux distros mount it by default)

白龙吟 2024-08-24 00:29:11

查看以下目录(根据您的内核版本进行替换):

ls /lib/modules/2.6.32-504.8.1.el6.x86_64/kernel/net/netfilter/

您可以使用以下方式加载模块(删除目录中列出的 .ko):

modprobe nf_conntrack_ftp

或者,您可以确保它在启动时加载将其添加到:

/etc/sysconfig/iptables-config (RHEL/CENTOS) 

IPTABLES_MODULES="nf_conntrack_ftp"

这似乎没有很好的记录。

Take a look in the following directory (replace per your kernel version):

ls /lib/modules/2.6.32-504.8.1.el6.x86_64/kernel/net/netfilter/

You can load the module using (dropping the .ko as listed in the directory):

modprobe nf_conntrack_ftp

Alternatively, you can ensure it's loaded at boot by adding it to:

/etc/sysconfig/iptables-config (RHEL/CENTOS) 

IPTABLES_MODULES="nf_conntrack_ftp"

This seems to be poorly documented.

一瞬间的火花 2024-08-24 00:29:11

尝试此操作以快速概述系统上存在的 netfilter 模块,这里是用于粘贴的一行:

for i in /lib/modules/$(uname -r)/kernel/net/netfilter/*; do echo -e "\e[33;1m$(basename "$i")\e[0m"; strings "$i" | \grep -e description -e depends| sed -e 's/Xtables: //g' -e 's/=/: /g' -e 's/depends=/depends on: /g'; echo; done

再次为了可读性,添加了换行符:

#!/bin/bash
for i in /lib/modules/$(uname -r)/kernel/net/netfilter/*
do 
    echo -e "\e[33;1m$(basename "$i")\e[0m"
    strings "$i" | \grep -e description -e depends | sed -e 's/Xtables: //g' -e 's/=/: /g' -e 's/depends=/depends on: /g'
    echo
done

文件名将以黄色显示,从中您可以猜测有问题的模块是否存在。描述和依赖项是下面接下来的两行。

这不会涵盖所有内容(因为这太容易了,ofc)。仅手动查找模块,看看它们是否存在,即可为您提供 100% 准确的信息。

iptables -m <match/module name> --help

如果您的系统上存在模块,则在帮助文本的末尾,您将获得一些有关如何使用它的信息:

ctr-014# iptables -m limit --help
iptables v1.4.14

Usage: iptables -[ACD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]
  

...


[!] --version   -V              print package version.

limit match options:
--limit avg                     max average match rate: default 3/hour
                                [Packets per second unless followed by 
                                /sec /minute /hour /day postfixes]
--limit-burst number            number to match in a burst, default 5
ctr-014# 

如果您的系统上不存在该模块:

ctr-014# iptables -m iplimit --help
iptables v1.4.14: Couldn't load match `iplimit':No such file or directory

Try `iptables -h' or 'iptables --help' for more information.
ctr-014#

Try this for a fast overview on the netfilter modules present on your system, here a one-liner for pasting:

for i in /lib/modules/$(uname -r)/kernel/net/netfilter/*; do echo -e "\e[33;1m$(basename "$i")\e[0m"; strings "$i" | \grep -e description -e depends| sed -e 's/Xtables: //g' -e 's/=/: /g' -e 's/depends=/depends on: /g'; echo; done

Again for readability, with added newlines:

#!/bin/bash
for i in /lib/modules/$(uname -r)/kernel/net/netfilter/*
do 
    echo -e "\e[33;1m$(basename "$i")\e[0m"
    strings "$i" | \grep -e description -e depends | sed -e 's/Xtables: //g' -e 's/=/: /g' -e 's/depends=/depends on: /g'
    echo
done

Filename will appear in yellow, from which you can guess if the module in question exists or not. Description and dependencies are the next two lines below.

This will not cover everything (because this would be too easy, ofc). Only looking up the modules manually, to see if they exist, gives you 100% accurate information.

iptables -m <match/module name> --help

If a module exists on your system, at the end of the help text you will get some info on how to use it:

ctr-014# iptables -m limit --help
iptables v1.4.14

Usage: iptables -[ACD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]
  

...


[!] --version   -V              print package version.

limit match options:
--limit avg                     max average match rate: default 3/hour
                                [Packets per second unless followed by 
                                /sec /minute /hour /day postfixes]
--limit-burst number            number to match in a burst, default 5
ctr-014# 

It the module is not present on your system:

ctr-014# iptables -m iplimit --help
iptables v1.4.14: Couldn't load match `iplimit':No such file or directory

Try `iptables -h' or 'iptables --help' for more information.
ctr-014#
诺曦 2024-08-24 00:29:11

这是一篇非常旧的帖子,但我们开始吧:

# lsmod | grep ip

显示已加载模块的列表,我认为大多数与 iptables 相关......
/proc/net/ip_tables_matches 不显示模块(至少在 RHEL 6 中不显示)

This is a really old post but here we go:

# lsmod | grep ip

shows a list of loaded modules, which I think most are related to iptables...
/proc/net/ip_tables_matches doesn't show modules (at least not in RHEL 6)

独孤求败 2024-08-24 00:29:11

正如 Gonio 建议 lsmod 列出所有加载的内核模块,但 grepping “ip” 不会为您提供所有 iptables 模块。

我宁愿使用

lsmod|grep -E "nf_|xt_|ip"

,但我不确定该列表是否完整。

As Gonio has suggested lsmod lists all loaded kernel modules, but grepping "ip" won't give you all iptables modules.

I would rather use

lsmod|grep -E "nf_|xt_|ip"

and still, I'm not sure the list will be complete.

冰雪之触 2024-08-24 00:29:11

作为替代方法,也可以使用 Python 脚本来完成此操作。

首先确保您有 iptc 库。
sudo pip install --upgrade python-iptables

(假设Python3是你的版本)

import iptc
table = iptc.Table(iptc.Table.FILTER)
for chain in table.chains:
    print("------------------------------------------")
    print("Chain ", chain.name)
    for rule in chain.rules:
        print("Rule ", "proto", rule.protocol, "src:", rule.src, "dst:" , rule.dst, "in:", rule.in_interface, "out:", rule.out_interface)
        print("Matches:")
        for match in rule.matches:
            print(match.name)
        print("Target:")
        print(rule.target.name)
print("------------------------------------------")

As an alternative method, this can also be done with a Python script.

First make sure you have the iptc library.
sudo pip install --upgrade python-iptables

(Assuming Python3 is your version)

import iptc
table = iptc.Table(iptc.Table.FILTER)
for chain in table.chains:
    print("------------------------------------------")
    print("Chain ", chain.name)
    for rule in chain.rules:
        print("Rule ", "proto", rule.protocol, "src:", rule.src, "dst:" , rule.dst, "in:", rule.in_interface, "out:", rule.out_interface)
        print("Matches:")
        for match in rule.matches:
            print(match.name)
        print("Target:")
        print(rule.target.name)
print("------------------------------------------")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文