Linux操作系统-10-Iptables

一、iptables防火墙介绍

无论IPtables还是Firewalld都是对netfilter防火墙框架进行的命令层的封装

1、三件事

防火墙做的三件事:

  • 获取流量
  • 匹配规则
  • 给出反馈(accept、drop)

iptables的特性:表、链、规则

2、五张表

五张表:filter、nat、mangle、raw、security,表主要用于将不同的规则存储到不同的表中

(1)filter表:默认表,负责过滤数据包,使用频率最高

(2)nat表:用于网络地址转换(IP、端口)和流量转发,使用频率较低

(3)mangle表:主要应用在修改数据包、流量整形、给数据包打标识

(4)raw表:这个表很少被用到,主要用于配置连接跟踪相关内容,使用频率较少

(5)security表:这个表用于安全Linux的防火墙规则,是iptables最近新增的表,使用频率较少

3、五条链

防火墙的链决定流量的方向,规则里边一定要定义好这条规则是入站规则input,还是出站规则output,还是转发规则,还是路由前规则prerouting,还是路由后规则postrouting,规则里边很重要的一个信息就是数据包的方向是什么。

(1)input:匹配目标Ip是本机的数据包,入站

(2)output:出口数据包,出站

(3)forward:匹配流经本机的数据包、转发

(4)prerouting:修改目的地址,用来做DNAT,如:把内网中的80端口映射到互联网端口

(5)postrouting:修改源地址,用来做SNAT,如:局域网共享一个公网IP接入Internet

4、规则

基于防火墙策略设置的各类防护规则,防火墙规则的执行顺序认为从前到后依次执行,遇到匹配的规则就不再继续向下检查,如果遇到不匹配的规则则会继续向下进行。

二、安装配置iptables

systemctl stop firewalld
yum install iptables iptables-services
systemctl start iptables 

#如果遇到Failed to start iptables\xc2\xa0.service: Unit not found的问题,莫慌。这是因为在最新的Linux系统中,iptables服务已经被iptables.service所取代。因此,你应该使用以下命令来启动iptables服务,使用下面命令即可启动iptables:
systemctl start iptables.service

firewalld默认在没有设置规则的情况下,是拒绝所有流量,而iptables默认没有设定规则的情况下,是允许所有流量,通常情况下来说防火墙是条件白名单,所以使用iptables时先把防火墙所有端口都关闭。

三、基础命令使用

1、iptables -L

iptables -L列出目前已有的规则,显示服务名称

iptables -nL列出目前已有的规则,显示端口号

#List the rules in a chain or all chains
[root@bogon ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         


#将端口号以数字的形式显示默认filter表中的规则
[root@bogon ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

2、iptables -t

iptables -t filter -nL

[root@bogon ~]# iptables -t filter -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@bogon ~]# 

iptables -t nat -nL

[root@bogon ~]# iptables -t nat -nL
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination 

3、iptables -A 与iptables -I

  • -A选项代表append追加的方式     

  • -I input   插入到最前面,优先级最高

  • 通常情况下来说,使用iptables防火墙要先把所有端口都关闭,然后根据需要再添加放行规则

#所有入站的流量全被丢弃,包括ssh的流量  
[root@bogon ~]# iptables -A INPUT -j DROP
[root@bogon ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
DROP       all  --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@bogon ~]# 

两条规则合在一起就是只允许外边的机器访问22号端口,其他一概丢弃
iptables -A INPUT -j DROP             

iptables -I INPUT -p tcp --dport 22 -j ACCEPT      #打开目标端口22,接受流经该端口的流量

添加之后,iptables -nl看下,在DROP之前加了22号端口的ACCEPT

[root@bogon ~]# iptables -I INPUT -p tcp --dport 22 -j ACCEPT
[root@bogon ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
DROP       all  --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@bogon ~]# 

iptables -I INPUT -j DROP               #所有入站的流量全被丢弃,包括ssh的流量

iptables -I OUTPUT -j DROP #所有出站流量全部丢弃,包括SSH响应

这两条命令一旦执行,所有流量无法进来,所有流量无法出去,断网状态

  • xshell此时也连不上了,因为请求可以到达服务器但是响应发不出去
  • 允许22号端口访问

iptables -I OUTPUT -p tcp --sport 22 -j ACCEPT#允许22端口,

注意:dport和sport的方向

小知识点:

 反弹shell:我访问不了目标,被目标服务器的防火墙挡了,考虑反弹shell,让它来访问我。

允许80端口

注意:dport和sport的方向

iptables -I INPUT -p tcp --dport 80 -j ACCEPT#允许80端口,

iptables -I OUTPUT -p tcp --sport 80 -j ACCEPT#允许80端口。

不能访问外网,如www.woniuxy.com

因为OUTPUT规则里面是sport 80,也就是允许自己的80端口可以被访问

要想访问外网OUTPUT规则里面应该是dport 80开放才行

所以还是没办法访问www.woniuxy.com

对外提供服务的同时,阻止服务器主动去连外网,是不是我们一些攻击手段里的反弹shell这种情况,攻击之后通过执行一些命令去访问外网下载木马程序的这些情况就可以杜绝了。

关于DROP和REJECT

DROP:直接丢弃数据包,不会向源端任何回复

REJECT:拒绝接收数据包,并向源端发送拒绝响应

(1)禁ping 

a、通过DROP实现禁PING
[root@server234 ~]# iptables -I INPUT -p icmp --icmp-type echo-request -j DROP
[root@server234 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
DROP       all  --  anywhere             anywhere            
DROP       icmp --  anywhere             anywhere             icmp echo-request

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
#保存iptables规则
[root@server234 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
#重启iptables
[root@server234 ~]# service iptables restart
Redirecting to /bin/systemctl restart iptables.service
[root@server234 ~]# 

客户端请求:

root123@agent90:~$ ping 192.168.1.234
PING 192.168.1.234 (192.168.1.234) 56(84) bytes of data

如果你想禁止服务器响应 Ping 请求,你应该添加一条规则来拒绝 ICMP Echo Reply
而不是将所有 ICMP 流量都允许通过。你可以通过添加如下规则来实现:
iptables -A OUTPUT -p icmp --icmp-type echo-reply -j DROP

iptables -A OUTPUT -p icmp --icmp-type echo-reply -j REJECT

b、通过REJECT实现禁PING
[root@server234 ~]# iptables -A INPUT -p icmp --icmp-type echo-request -j REJECT --reject-with icmp-host-prohibited
[root@server234 ~]# iptables -L 
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
REJECT     icmp --  anywhere             anywhere             icmp echo-request reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
REJECT     icmp --  anywhere             anywhere             icmp echo-reply reject-with icmp-port-unreachable
[root@server234 ~]# 

客户端请求

root123@agent90:~$ ping 192.168.1.234
PING 192.168.1.234 (192.168.1.234) 56(84) bytes of data.
来自 192.168.1.234 icmp_seq=1 Destination Host Prohibited
来自 192.168.1.234 icmp_seq=2 Destination Host Prohibited
来自 192.168.1.234 icmp_seq=3 Destination Host Prohibited
来自 192.168.1.234 icmp_seq=4 Destination Host Prohibited
来自 192.168.1.234 icmp_seq=5 Destination Host Prohibited
来自 192.168.1.234 icmp_seq=6 Destination Host Prohibited
^C
--- 192.168.1.234 ping 统计 ---
已发送 6 个包, 已接收 0 个包, +6 错误, 100% 包丢失, 耗时 5066 毫秒
c、允许某个IP地址PING

如果你要允许某些流量通过防火墙,你需要在这个默认规则之前添加允许的规则。例如,如果要允许某个特定IP的Ping请求通过防火墙,可以添加如下规则:

iptables -A INPUT -s 允许的IP地址 -p icmp --icmp-type echo-request -j ACCEPT

iptables -I INPUT -s 允许的IP地址 -p icmp --icmp-type echo-request -j ACCEPT

这个规则将允许来自特定IP的 Ping 请求通过防火墙。记得将 允许的IP地址 替换为实际的 IP 地址。因为防火墙规则是从上往下匹配的,A代表Append会添加在后面,I代表Insert会插入到最前面。

d、取消禁ping 

iptables -I INPUT -p icmp -j ACCEPT

[root@server234 ~]# iptables -I INPUT -p icmp -j ACCEPT
[root@server234 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     icmp --  anywhere             anywhere            
DROP       icmp --  anywhere             anywhere             icmp echo-request
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
DROP       all  --  anywhere             anywhere            
DROP       icmp --  anywhere             anywhere             icmp echo-request

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@server234 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@server234 ~]# service iptables restart

4、删除规则

iptables -L INPUT --line-numbers                       #查看入站规则的行号

iptables -L OUTPUT -p icmp -j ACCEPT            #查看出站规则的行号

iptables -D OUTPUT 3          #删除出站规则的第三条规则

iptables -nl                         

[root@server234 ~]# iptables -L OUTPUT --line-numbers
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     icmp --  anywhere             anywhere            
2    DROP       icmp --  anywhere             anywhere             icmp echo-reply
3    ACCEPT     icmp --  anywhere             anywhere            
[root@server234 ~]# iptables -D OUTPUT 1
[root@server234 ~]# iptables -D OUTPUT 3
iptables: Index of deletion too big.
[root@server234 ~]# iptables -D OUTPUT 2
[root@server234 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     icmp --  anywhere             anywhere            
DROP       icmp --  anywhere             anywhere             icmp echo-request
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
DROP       all  --  anywhere             anywhere            
DROP       icmp --  anywhere             anywhere             icmp echo-request

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       icmp --  anywhere             anywhere             icmp echo-reply
[root@server234 ~]# 

5、保存iptables规则 

所有规则,一旦重启iptables,所有的规则全会消失

yum install iptables-service        #需要提前安装iptables-services

service iptables save                  #将iptables规则保存下来

cat /etc/sysconfig/iptables                    #保存的规则都写到了这个文本里面

[root@server234 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@server234 ~]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.21 on Sun Mar 31 18:08:36 2024
*mangle
:PREROUTING ACCEPT [2651:253097]
:INPUT ACCEPT [2638:251924]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1952:127059]
:POSTROUTING ACCEPT [1952:127059]
-A POSTROUTING -o virbr0 -p udp -m udp --dport 68 -j CHECKSUM --checksum-fill
COMMIT
# Completed on Sun Mar 31 18:08:36 2024
# Generated by iptables-save v1.4.21 on Sun Mar 31 18:08:36 2024
*nat
:PREROUTING ACCEPT [1684:116553]
:INPUT ACCEPT [1316:85560]
:OUTPUT ACCEPT [27:1684]
:POSTROUTING ACCEPT [27:1684]
-A POSTROUTING -s 192.168.122.0/24 -d 224.0.0.0/24 -j RETURN
-A POSTROUTING -s 192.168.122.0/24 -d 255.255.255.255/32 -j RETURN
-A POSTROUTING -s 192.168.122.0/24 ! -d 192.168.122.0/24 -p tcp -j MASQUERADE --to-ports 1024-65535
-A POSTROUTING -s 192.168.122.0/24 ! -d 192.168.122.0/24 -p udp -j MASQUERADE --to-ports 1024-65535
-A POSTROUTING -s 192.168.122.0/24 ! -d 192.168.122.0/24 -j MASQUERADE
COMMIT
# Completed on Sun Mar 31 18:08:36 2024
# Generated by iptables-save v1.4.21 on Sun Mar 31 18:08:36 2024
*filter
:INPUT ACCEPT [865:68720]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1232:81545]
-A INPUT -p icmp -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j DROP
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Sun Mar 31 18:08:36 2024

6、清空规则

iptables -F            #清空规则

iptables -F 命令用于清空所有防火墙规则,但是这些改变不会永久保存到磁盘,只会在当前会话中生效。如果你重启服务器,这些改变会被丢弃,并且恢复到之前的防火墙规则配置状态。无论是Flush清空还是Input新增,只要不保存,重启之后都不会生效,只是实时生效。

四、iptables进阶用法

1、设置服务器环境与外网隔离

要设置服务器环境无法访问外网,你可以通过配置防火墙规则来实现。具体来说,你可以使用 iptables 或 firewalld 在服务器上设置防火墙规则,阻止所有对外部网络的访问。

以下是使用 iptables 设置服务器无法访问外网的示例:

  • 首先,确保清空现有的 iptables 规则以免影响到你的配置:
iptables -F
  • 接下来,设置默认策略为拒绝所有输出流量:
iptables -P OUTPUT DROP
  • 然后,允许回环流量(本地主机之间的通信):
iptables -A OUTPUT -o lo -j ACCEPT
  • 最后,允许已经建立的连接的流量通过。这可以确保已经建立的连接(例如 SSH 连接)不会被阻止:
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  • 保存 iptables 规则以便重启后生效(如果可以访问外网的话):
service iptables save

完成以上步骤后,你的服务器将无法访问外网,但仍然可以进行本地通信和已建立的连接。

[root@server234 ~]# iptables -F
[root@server234 ~]# iptables -P OUTPUT DROP
[root@server234 ~]# iptables -A OUTPUT -o lo -j ACCEPT
[root@server234 ~]# iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@server234 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@server234 ~]# ping www.baiidu.com
ping: www.baiidu.com: Name or service not known
[root@server234 ~]# ping 192.168.1.90
PING 192.168.1.90 (192.168.1.90) 56(84) bytes of data.
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted
^C
--- 192.168.1.90 ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 4109ms

[root@server234 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED


2、允许服务器访问指定网络 

思考:别人可以访问我的80端口,但我访问不了别人

服务器环境:
出站入站:只要匹配不到规则的全部DROP

服务器和其他服务器通信,比如站库分离,访问另一台电脑上的数据库,设定只能访问某一台电脑(添加白名单)

实验:

让服务器可以访问别的某台电脑,可以是内网也可以是外网

curl http://www.woniuxy.com/

对外访问提供http://www.woniuxy.com/网站服务的这台电脑,别的访问不了,添加白名单,避免攻击者使用curl、weget命令去访问其他电脑,下载其他内容。

画图:

  • 根据画图分析来看,出站配dp和d,入站配sp和s
  • 主要是搞清楚方向,不要空想画出来

允许本地访问外部IP地址和端口号,通过设定白名单的方式可以本机去访问别的服务器

#通过这种场景的设置,可以最大可能避免反弹shell和挖矿程序试图通过本地访问目标服务器下载恶意程序或执行恶意指令。

根据画图分析来看,出站配dp和d,入站配sp和s,先配出去的,再配进来的

iptables -I INPUT -i ens33 -p tcp -s 101.37.65.91 --sport 8088 -j ACCEPT

允许来自特定源 IP 地址(101.37.65.91)且源端口为 8088 的 TCP 流量通过指定的网络接口(ens33)。

规则解释:

  • -I INPUT:表示插入规则到 INPUT 链的顶部,这意味着这个规则将会在已有的规则之前生效。
  • -i ens33:指定接口为 ens33,即规则仅适用于从 ens33 网络接口接收到的数据包。
  • -p tcp:指定协议为 TCP。
  • -s 101.37.65.91:指定源 IP 地址为 101.37.65.91,即规则仅适用于来自该 IP 地址的数据包。
  • --sport 8088:指定源端口为 8088,即规则仅适用于源端口为 8088 的数据包。
  • -j ACCEPT:如果数据包符合上述条件,则接受它。

这个规则的作用是允许来自 IP 地址为 101.37.65.91 且源端口为 8088 的 TCP 数据包通过 ens33 网络接口进入服务器。

iptables -I OUTPUT -o ens33 -p tcp -d 101.37.65.91 --dport 8088 -j ACCEPT

[root@server234 ~]# iptables -I INPUT -i ens33 -p tcp -s 101.37.65.91 --sport 443 -j ACCEPT
[root@server234 ~]# iptables -I OUTPUT -o ens33 -p tcp -d 101.37.65.91 --dport 443 -j ACCEPT
[root@server234 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  101.37.65.91         anywhere             tcp spt:https
ACCEPT     tcp  --  101.37.65.91         anywhere             tcp spt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
DROP       all  --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             101.37.65.91         tcp dpt:https
ACCEPT     tcp  --  anywhere             101.37.65.91         tcp dpt:http
ACCEPT     tcp  --  anywhere             101.37.65.91         tcp dpt:radan-http

访问测试

3、防止DDOS攻击

(1)限制来自客户端的TCP流量

iptables -A INPUT -p tcp --sport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT

下面是你提供的规则的含义:

  • -A INPUT:将规则附加到 INPUT 链,即所有进入服务器的数据包。
  • -p tcp:指定协议为 TCP。
  • --sport 80:指定源端口为 80,即来自客户端的流量。
  • -m limit --limit 25/minute --limit-burst 100:使用限速模块限制数据包速率,允许每分钟最多 25 个数据包,允许突发 100 个数据包。
  • -j ACCEPT:如果数据包符合上述条件,则接受它。

这个规则的作用是限制来自客户端(源端口为 80)的 TCP 连接请求速率,以防止过多的连接请求导致服务器过载。

可以同时添加多个端口,在 INPUT 链上添加了允许特定端口的 TCP 连接。

iptables -I INPUT -p tcp -m multiport --dport 80,22,443,3306 -j ACCEPT

规则解释:

  • -I INPUT:表示插入规则到 INPUT 链的顶部,这意味着这个规则将会在已有的规则之前生效。
  • -p tcp:指定协议为 TCP。
  • -m multiport --dport 80,22,443,3306:使用 multiport 模块来匹配多个目标端口,这里指定了端口号 80、22、443 和 3306。这意味着允许进入服务器的 TCP 流量中的目标端口是这些端口的都会被接受。
  • -j ACCEPT:如果数据包符合上述条件,则接受它。

这是一个常见的防火墙设置,允许 HTTP(80)、SSH(22)、HTTPS(443)和 MySQL(3306)等服务的连接请求通过防火墙。这样设置可以确保这些常用服务能够正常运行。

(2)限定服务器上发出的TCP流量 

 如果你想要限制服务器上发出的 TCP 流量(例如限制对外部 Web 服务器的访问速率),你应该将规则应用到 OUTPUT 链而不是 INPUT 链。例如:

iptables -A OUTPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT

这样的规则将限制服务器对外部 Web 服务器的连接请求速率。请根据你的需求选择适当的链来应用规则

4、端口转发   

(1)本地端口转发

本地端口转发    比如80端口关闭,开放一个莫名其妙的如45692端口对外访问,不知道80端口,可以避免协议猜测,本机端口转发也可直接把服务器网站端口设置成45692端口达到一样的效果。

应用场景 :外网端口和内网端口访问的不一样,这种情况就用端口转发,端口转发相对更灵活。

iptables -t nat -A PREROUTING -p tcp --dport 7777 -j REDIRECT --to-port 80

(2)远程端口转发

远程端口转发,把本机接收到的请求转发到远程电脑和对应端口上(远程可以是本地局域网,也可以是公网服务器)。

远程端口转发(功能类似于负载均衡)

a、确保端口转发功能是启用的

vim /etc/sysctl.conf

net.ipv4.ip_forward = 1

sysctl -p /etc/sysctl.conf                 #修改生效

b、发得出去

PREROUTING上访问8888时,转发给目标服务器和目标端口

PREROUTING是先于FILTER执行的,所以不需要转发时允许8888端口ACCEPT

当别人访问本机192.168.112.188的8888端口的时候转发到101.37.65.91的80端口。

iptables -t nat -A PREROUTING -d 192.168.112.188 -p tcp --dport 8888 -j DNAT --to-destination 101.37.65.91:80

c、回得来

远程转发的服务器101.37.65.91的80端口路由到本机192.168.112.188,本机响应给客户端。

iptables -t nat -A POSTROUTING -d 101.37.65.91 -p tcp --dport 80 -j SNAT --to-destination 192.168.112.188

iptables -t nat -A POSTROUTING -d 101.37.65.91 -p tcp --dport 80 -j SNAT --to-destination 192.168.112.188

此时访问192.168.112.188:8888端口就访问到了蜗牛学院的官网

从客户端来说是不知道192.168.112.153这台远程电脑的存在的

钓鱼网站,社会工程学的内容

改本地host文件:访问的就是正常的URL网址,但是我把域名的DNS解析到我的IP地址上

DNS欺骗,看到的是攻击者做的钓鱼网站的页面

五、iptables命令参数

参数说明
-A添加一条规则,即添加在规则的最后
INPUT链名、常用、大写
RPREROUTING链名、大写
OUTPUT链名、大写
-I指定链中插入规则,即添加到规则的最前面
-s指定源地址,可以是IP地址,也可以是网段“192.168.1.10、24”;“-s”为空表示所有
-d目标地址
-p指定协议
--dport指定主机端口(本机开放或拒绝端口)
--sport指定主机端口(如:禁止连接对方某接口)
-i指定网卡名,表示报文流入的接口
-o指定网卡名,表示报文流出的接口
-j指定所需要的操作
ACCEPT允许
REJECT拒绝、拒绝提供服务
DROP拒绝,丢弃数据包不回应
--src-range源地址范围(如:拒绝某IP段访问)
--dsc-range目标地址的范围
--mac-source源主机的MAC地址
-t指定表名,默认是filter表
-v查看详细信息
-nvL --line-numbers查看filter表中规则的顺序
-nvL -t mangle查看mangle表中的防火墙规则
-F清空filter表
-I指定链中插入规则
-R替换规则
-m指定模块

希望通过IPtables在网络安全意识上有所加深!!!

这是后面做应急响应做安全加固,做防御,甚至做攻击分析过程中必不可少的核心技术!!!

  • 23
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zkaisen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值