iptables初探

一、概念

iptables是Linux下功能强大的防火墙工具,由于它集成于Linux内核,所以效率极高。

二、分类

按照对数据包的操作类别分类,iptables可以分为4个表,按照不同的Hook点可区分为5个链。
其中4个表分别是filter表(用于一般的过滤)、nat表(地址或端口映射)、mangle表(对特定数据包的修改)、raw表,这里面最常用的是filter表;
5个链分别是PREROUTING链(数据包进入路由决策之前)、INPUT(路由决策为本机的数据包)、FORWARD(路由决策不是本机的数据包)、OUTPUT(由本机产生的向外发送的数据包)、POSTROUTING(发送给网卡之前的数据包),最常用的是INPUT、OUTPUT链。

在这里插入图片描述
在这里插入图片描述
Packet flow in Netfilter and general networking can be summarized as follows:

Incoming Packet: When a packet arrives at a network interface, it goes through the following stages:

Link Layer Processing: The packet is received by the network interface, which performs link layer processing, such as checking for errors, stripping off Ethernet headers, and checking the destination MAC address.

Network Layer Processing: The network stack processes the packet at the network layer, where it checks the IP header and performs routing to determine the next hop.

Netfilter Framework: In Linux, the Netfilter framework is responsible for handling packet filtering, network address translation (NAT), and other packet manipulation operations. Netfilter provides hooks at different points in the packet flow, allowing various operations to be performed on packets.

Pre-Routing Hook: The packet enters the Netfilter framework through the pre-routing hook. This hook allows the packet to be manipulated or redirected before any routing decision is made. For example, NAT can be applied here to change the destination IP address.

Routing Decision: After passing through the pre-routing hook, the routing decision is made based on the packet’s destination IP address. The routing table is consulted to determine the appropriate outgoing interface.

Input Hook: Once the routing decision is made, the packet enters the input hook, where further processing can be performed. This includes applying filters, such as firewall rules, which can either drop or accept the packet.

Local Process: If the packet is accepted by the input hook, it is then forwarded to the local process that is bound to the specified port or protocol.

Output Hook: Outgoing packets generated by local processes or from the system itself go through the output hook. Here, additional packet manipulations can be performed, such as modifying the header or applying NAT to change the source IP address.

Post-Routing Hook: After passing through the output hook, the packet reaches the post-routing hook. This is the last opportunity to modify or redirect the packet before it is sent out on the wire.

Outgoing Packet: Finally, the packet is encapsulated in the appropriate link layer header (e.g., Ethernet) and sent out on the outgoing interface.

It’s important to note that the exact packet flow can vary depending on the specific networking configuration, firewall rules, and other factors. However, this general overview provides a good understanding of how packets are handled in both Netfilter and general networking environments.

三、工作策略

第一种是仅接受允许的数据,这种策略一般是设置防火墙的默认策略为拒绝所有数据包(也就是拒绝网卡上出入的数据包),然后有针对性地放开特定的访问;第二种是只防止不允许的数据访问请求,这种策略一般是设置防火墙的默认策略为允许所有数据包,只拒绝已知的非法访问数据。从安全效果而言,前一种防火墙策略表现更为优秀/

四、配置规则

@1

#cat /etc/sysconfig/iptables
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT #允许已经建立和相关的数据包进入 / 代表目前已经与本机建立连接的主机,依然保持连接状态
-A INPUT -p icmp -j ACCEPT #在INPUT规则链中添加允许ICMP流量进入
-A INPUT -i lo -j ACCEPT
-A INPUT -i bond0 -s x.x.x.x -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT #允许固定的IP能SSH到该服务器上
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT #允许访问80端口

-A INPUT -s x.x.x.x -p tcp -m multiport --dports 80,10011,3000,6379,9307 -j ACCEPT #允许固定的IP能访问该服务器某些端口

-A INPUT -p tcp --dport 3000 -d 127.0.0.1 -j ACCEPT
-A INPUT -p tcp --dport 3000 -j DROP #DROP(丢弃)

-A OUTPUT -p tcp --sport 5900:5930 -j DROP

-A INPUT -j REJECT --reject-with icmp-host-prohibited #REJECT(拒绝)
-A FORWARD -j REJECT --reject-with icmp-host-prohibited

@2

iptables -P FORWARD ACCEPT 命令用于设置默认的转发策略为接受(ACCEPT)。这意味着,当系统收到一个通过路由器进行转发的网络数据包时,如果没有匹配任何进一步的规则,该数据包将被允许通过并转发到目标地址。

默认情况下,iptables 的转发策略是拒绝(DROP),这意味着所有经过路由器的转发数据包都会被丢弃而不被转发。使用 iptables -P FORWARD ACCEPT 将转发策略设置为接受后,转发功能将启用,并且数据包将根据其他规则进行处理或直接转发到目标。

这个命令通常在需要启用路由器或防火墙的转发功能时使用。然而,在实际应用中,您可能需要更复杂的规则来控制数据包的转发行为,以提供更好的安全性和网络管理。

五、直接配置并保存规则

iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT #添加
iptables -D INPUT -p tcp -m tcp --dport 80 -j ACCEPT #清理
iptables -L #查看

保存

service iptables save

重启后永久生效

service iptables restart
iptables-save

iptables-save 命令用于将当前正在运行的 iptables 防火墙规则保存到文件中。该命令可以生成一个包含所有防火墙规则的文本文件,以便在需要时恢复或备份这些规则。

具体来说,iptables-save 命令的作用如下:

  1. 将防火墙规则保存到文件:使用 iptables-save > filename 命令将当前的 iptables 规则保存到指定的文件中。可以将文件名替换为任何你喜欢的名称和路径。

  2. 备份和恢复防火墙规则:通过将规则保存到文件中,你可以轻松地备份和恢复防火墙设置。如果需要重新安装操作系统或迁移服务器,可以使用 iptables-restore 命令从保存的文件中恢复防火墙规则。

  3. 分享和调试规则:将规则保存到文件后,你可以与其他人共享它们,以便他们可以了解你的防火墙配置。此外,你还可以使用 iptables-save 的输出进行调试和分析,以便更好地了解当前的防火墙规则。

需要注意的是,iptables-save 仅保存当前正在运行的规则,而不会保存在 /etc/iptables/rules.v4/etc/sysconfig/iptables 等持久性位置上的规则。因此,在系统重启后,需要使用其他方法(如 iptables-restore)来加载保存的规则。

六、参考链接

iptables配置规则
关于iptables的规则配置方法
linux系统中查看己设置iptables规则

七、DNAT和SNAT

DNAT和SNAT是网络中的两种地址转换技术,用于处理网络数据包的源地址和目的地址。

  1. DNAT(Destination Network Address Translation):目标网络地址转换。在网络中,当数据包经过一个网络设备(如防火墙或路由器)时,DNAT将目标IP地址和端口号进行修改,以便将数据包发送到不同的目标地址。这通常用于实现端口映射、负载均衡或者将外部请求重定向到内部服务器等功能。

  2. SNAT(Source Network Address Translation):源网络地址转换。当数据包从内部网络发送到外部网络时,SNAT将源IP地址和端口号进行修改,以便隐藏实际的源地址并提供更好的安全性。这通常用于多台主机共享一个公网IP地址的情况下,将内部主机的私有IP地址映射为共享的公网IP地址。

总结:

  • DNAT用于修改数据包的目标地址,常用于实现端口映射、负载均衡等功能。
  • SNAT用于修改数据包的源地址,常用于隐藏实际的源地址并提供更好的安全性。

这些地址转换技术在网络中扮演着重要的角色,并且在配置网络设备时需要小心使用,以确保网络的正常运行和安全性。

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值