iptables学习

强烈推荐读下这篇文章

《一篇搞懂》系列之一 —— iptables_iptables优先级_祁祁不正经的博客-CSDN博客

image.png

1. iptables -t filter -A与iptables -t filter -I的区别。

-A会追加到当前表的最后,-I会添加到当前表的第一行。

2. iptables典型命令

如果没有指定表,默认表为filter.  table to manipulate (default: `filter')

iptables -t filter -A INPUT -i eth0  -s  10.0.0.2 -p tcp --dport 8080 -j ACCEPT

iptables -nvL   // n表示不对ip进行反向解析,v-verbose, L list

//下面这条规则的执行效果怎么样,得看在规则链执行过程中,在表中该规则在哪个位置,有时候该规则甚至都没有机会执行。

iptables -t filter -A INPUT -j drop

  • 查看Filter表中所有链以及规则
iptables -nL --line-numbers

-n:将主机信息(IP地址,端口等)以数字的形式打印出来。默认会以hostname等方式打印出来;
-L:显示规则链中已有的条目;
–line-numbers:显示条目序号。

3. iptables使用的一些场景

3.1 修改规则链的默认策略

  --policy  -P chain target
                                Change policy on chain to target

iptables -P  INPUT DROP

3.2 全部允许,只阻止某些ip访问。可以用来测试程序访问不通某些程序时,软件的运行是否正常。

3.3 清除规则

只能清除添加的规则,默认规则是不受影响的。

iptables -F

3.4 当在云主机上操作iptables,注意不要因为修改iptables导致与云主机的连接断开。

3.5 iptables -D 删除规则。

3.6 自定义规则链。

3.7 一般情况下,filter表中,一般只限制INPUT链中对一些来源IP或端口进行限制。

在INPUT链中,指定目标IP是无影响的。

3.8 Linux IPTABLES中默认是阻止所有端口访问的,如果要开放服务的网络访问,一般要开通相关IP或端口访问。

4. 防火墙的分类

- 软件防火墙,可分为包过滤防火墙和应用层防火墙,相对而言,包过滤防火墙效率更高。

- 硬件防火墙

5. iptables、Firewalld、NetFilter这三者的关系?

由于iptables配置网络比较复杂,且参数较多,而且实际控制效果依赖规则链中规则的顺序。

iptables实际上只是位于用户空间的一个面向系统管理员的Linux防火墙的管理工具而已,而真正实现防火墙功能的是netfilter,它是Linux内核中实现包过滤的内核模块,iptables对应在内核中的模块应该是ip_tables,我们查看系统内核中ip_tables的信息的时候可以看到ip_tables.ko这个模块是在netfilter这个目录下的。

实际上除了iptables还有如nftablesfirewalld等防火墙工具都是在用户空间(用户层)对相应的内核空间中对应的netfilter相关的模块进行操作的工具。

6. 规则表和规则链

规则表中最常用的就filter和nat,也是iptables早期支持的两个表。

之所以叫做链就是因为在访问该链的时候会按照每个链对应的表依次进行查询匹配执行的操作,如PREROUTING链对应的就是(raw->mangle->nat),每个表按照优先级顺序进行连接,每个表中还可能有多个规则,因此最后看起来就像链一样,因此称为链。而iptables的表中存储的就是对应的规则和需要执行的操作。

注意每一个链对应的表都是不完全一样的,表和链之间是多对多的对应关系。但是不管一个链对应多少个表,它的表都是按照下面的优先顺序来进行查找匹配的。

表的处理优先级:raw>mangle>nat>filter

四表

iptables的四个表iptable_filteriptable_mangleiptable_natiptable_raw,默认表是filter(没有指定表的时候就是filter表)。

  • filter 表:用来对数据包进行过滤,具体的规则要求决定如何处理一个数据包。
    对应的内核模块为:iptable_filter,其表内包括三个链:inputforwardoutput;
  • nat 表:nat 全称:network address translation 网络地址转换,主要用来修改数据包的 IP 地址、端口号信息。
    对应的内核模块为:iptable_nat,其表内包括三个链:preroutingpostroutingoutput;
  • mangle 表:主要用来修改数据包的服务类型,生存周期,为数据包设置标记,实现流量整形、策略路由等。
    对应的内核模块为:iptable_mangle,其表内包括五个链:preroutingpostroutinginputoutputforward;
  • raw 表:主要用来决定是否对数据包进行状态跟踪。
    对应的内核模块为:iptable_raw,其表内包括两个链:outputprerouting;
raw表只使用在 PREROUTING链和 OUTPUT链上,因为优先级最高,从而可以对收到的数据包在系统进行ip_conntrack(连接跟踪)前进行处理。一但用户使用了raw表,在某个链上,raw表处理完后,将跳过NAT表和ip_conntrack处理,即不再做地址转换和数据包的链接跟踪处理了。RAW表可以应用在那些不需要做nat的情况下,以提高性能。
  • 链(chains):每一张表包含若干,其规定了相关规则在什么时候执行。内核中内置有5条链,分别对应netfilter提供的5个Hook点。能够让管理员在数据包传输过程中的某一个点通过相关规则控制数据包的走向。

7. 在iptables中设置的对数据包的处理方式会对请示响应报错的现象产生影响 。特别是drop和reject两者的效果是不一样的。

ACCEPT允许数据包通过
DROP直接丢弃数据包,不给任何回应信息,这时候客户端会感觉自己的请求没有响应,过了超时时间才会有反应。
REJECT拒绝数据包通过,必要时会给数据发送端一个响应的信息,客户端刚请求就会收到拒绝的信息

8. iptables实战

实战篇
禁止源地址192.168.1.1访问服务器的22,53,80端口:
iptables -I INPUT -s 192.168.1.1/32 -p tcp --dport 22,53,80 -j DROP
-s:source address,匹配源地址
-p:protocol,匹配协议
–dport:destination port,匹配目的端口。逗号分隔多个端口。

对于访问量比较大的服务器,例如192.168.1.1,可以通过raw表配置绕过连接跟踪:
iptables -t raw -A PREROUTING -d 192.168.1.1/32 -p tcp --dport 80 -j NOTRACK

将访问192.168.1.1:80的请求转到192.168.1.2:9000上
iptables -t nat -A PREROUTING -d 192.168.1.1 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.2:9000

实现所有192.168.1.0/24的地址通过123.123.123.123公网地址访问公网
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT --to-source 123.123.123.123

拒绝一分钟内新建超过4次SSH连接的IP再次连接
iptables -A INPUT -p tcp -m tcp --dport 22 -m state --state NEW -m recent --set --name SSH --rsource
iptables -A INPUT -p tcp -m tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 4 --name SSH --rsource -j DROP


 

===========

iptables的四表五链与NAT工作原理 - 知乎

Linux内核中数据包的传输过程_linux内核网络回复报文转发路径_白-胖-子的博客-CSDN博客

《一篇搞懂》系列之一 —— iptables_iptables优先级_祁祁不正经的博客-CSDN博客

iptables详解及一些常用规则 - 简书

Iptables Tutorial: Ultimate Guide to Linux Firewall

iptables(8) - Linux man page

关于在iptables里面使用-m参数,内容特别多,有一些常用的参数可以了解一下,全部记住不太现实,但可以通过下面的方式查看帮助文档 。比如下面查看iprange模块的详细使用方法。

# iptables -I input -m iprange -h
iptables v1.6.1

Usage: iptables -[ACD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]
       iptables -R chain rulenum rule-specification [options]
       iptables -D chain rulenum [options]
       iptables -[LS] [chain [rulenum]] [options]
       iptables -[FZ] [chain] [options]
       iptables -[NX] chain
       iptables -E old-chain-name new-chain-name
       iptables -P chain target [options]
       iptables -h (print this help information)

Commands:
Either long or short options are allowed.
  --append  -A chain            Append to chain
  --check   -C chain            Check for the existence of a rule
  --delete  -D chain            Delete matching rule from chain
  --delete  -D chain rulenum
                                Delete rule rulenum (1 = first) from chain
  --insert  -I chain [rulenum]
                                Insert in chain as rulenum (default 1=first)
  --replace -R chain rulenum
                                Replace rule rulenum (1 = first) in chain
  --list    -L [chain [rulenum]]
                                List the rules in a chain or all chains
  --list-rules -S [chain [rulenum]]
                                Print the rules in a chain or all chains
  --flush   -F [chain]          Delete all rules in  chain or all chains
  --zero    -Z [chain [rulenum]]
                                Zero counters in chain or all chains
  --new     -N chain            Create a new user-defined chain
  --delete-chain
            -X [chain]          Delete a user-defined chain
  --policy  -P chain target
                                Change policy on chain to target
  --rename-chain
            -E old-chain new-chain
                                Change chain name, (moving any references)
Options:
    --ipv4      -4              Nothing (line is ignored by ip6tables-restore)
    --ipv6      -6              Error (line is ignored by iptables-restore)
[!] --protocol  -p proto        protocol: by number or name, eg. `tcp'
[!] --source    -s address[/mask][...]
                                source specification
[!] --destination -d address[/mask][...]
                                destination specification
[!] --in-interface -i input name[+]
                                network interface name ([+] for wildcard)
 --jump -j target
                                target for rule (may load target extension)
  --goto      -g chain
                              jump to chain with no return
  --match       -m match
                                extended match (may load extension)
  --numeric     -n              numeric output of addresses and ports
[!] --out-interface -o output name[+]
                                network interface name ([+] for wildcard)
  --table       -t table        table to manipulate (default: `filter')
  --verbose     -v              verbose mode
  --wait        -w [seconds]    maximum wait to acquire xtables lock before give up
  --wait-interval -W [usecs]    wait time to try to acquire xtables lock
                                default is 1 second
  --line-numbers                print line numbers when listing
  --exact       -x              expand numbers (display exact values)
[!] --fragment  -f              match second or further fragments only
  --modprobe=<command>          try to insert modules using this command
  --set-counters PKTS BYTES     set the counter during insert/append
[!] --version   -V              print package version.

iprange match options:
[!] --src-range ip[-ip]    Match source IP in the specified range
[!] --dst-range ip[-ip]    Match destination IP in the specified range
#


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux系统中,可以使用iptables命令实现端口转发。iptables是用于实现防火墙、数据转发等功能的工具。iptables具有不同的表(tables),每个表都包含多个链(chains),每条链都包含一个或多个规则(rules)。 要在Linux系统中进行端口转发,可以使用以下命令: 1. 端口转发到本地其他端口: iptables -t nat -A PREROUTING -p tcp --dport [源端口号] -j REDIRECT --to-port [目标端口号] 例如,要将本机的2222端口转发到本机的22端口,可以使用以下命令: iptables -t nat -A PREROUTING -p tcp --dport 2222 -j REDIRECT --to-port 22 2. 端口转发到其他机器: iptables -t nat -A PREROUTING -d [本机IP地址] -p tcp --dport [源端口号] -j DNAT --to-destination [目标IP地址]:[目标端口号] iptables -t nat -A POSTROUTING -d [目标IP地址] -p tcp --dport [目标端口号] -j SNAT --to [本机IP地址] 例如,要将本机的192.168.172.130的8000端口转发到192.168.172.131的80端口,可以使用以下命令: iptables -t nat -A PREROUTING -d 192.168.172.130 -p tcp --dport 8000 -j DNAT --to-destination 192.168.172.131:80 iptables -t nat -A POSTROUTING -d 192.168.172.131 -p tcp --dport 80 -j SNAT --to 192.168.172.130 注意,以上命令只是示例,实际使用时需要根据实际情况进行相应的修改。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Linux端口转发的几种常用方法](https://blog.csdn.net/u010680373/article/details/124779749)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [iptables学习笔记:端口转发之“外网访问内网”](https://blog.csdn.net/subfate/article/details/52659446)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值