Guión de iptables súper detallado

#!/bin/bash

# 定义变量
IPTABLES=/sbin/iptables
LOCAL_NET=192.168.0.0/24
EXTERNAL_NET=enp0s3

# 清空iptables规则
$IPTABLES -F
$IPTABLES -X
$IPTABLES -Z

# 设置默认策略
$IPTABLES -P INPUT DROP
$IPTABLES -P FORWARD DROP
$IPTABLES -P OUTPUT ACCEPT

# 允许回环接口 (lo) 的任何流量通过
$IPTABLES -A INPUT -i lo -j ACCEPT
$IPTABLES -A OUTPUT -o lo -j ACCEPT

# 允许已经建立的、相关的连接通过
$IPTABLES -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

# 拒绝来自外部网络的ping请求
$IPTABLES -A INPUT -p icmp --icmp-type echo-request -j DROP

# 允许HTTP和HTTPS流量
$IPTABLES -A INPUT -p tcp --dport 80 -j ACCEPT
$IPTABLES -A INPUT -p tcp --dport 443 -j ACCEPT

# 允许SSH流量 (请根据需要修改端口号)
$IPTABLES -A INPUT -p tcp --dport 22 -j ACCEPT

# 防止SYN Flood攻击
$IPTABLES -N syn-flood
$IPTABLES -A syn-flood -m limit --limit 1/s --limit-burst 3 -j RETURN
$IPTABLES -A syn-flood -j DROP
$IPTABLES -A INPUT -p tcp --syn -j syn-flood

# 允许来自本地网络的流量通过
$IPTABLES -A INPUT -s $LOCAL_NET -i $EXTERNAL_NET -j ACCEPT
$IPTABLES -A OUTPUT -d $LOCAL_NET -o $EXTERNAL_NET -j ACCEPT
$IPTABLES -A FORWARD -s $LOCAL_NET -j ACCEPT
$IPTABLES -A FORWARD -d $LOCAL_NET -j ACCEPT

# 允许SMTP、POP3和IMAP等邮件服务流量 (请根据需要修改端口号)
$IPTABLES -A INPUT -p tcp --dport 25 -j ACCEPT
$IPTABLES -A INPUT -p tcp --dport 110 -j ACCEPT
$IPTABLES -A INPUT -p tcp --dport 143 -j ACCEPT
$IPTABLES -A INPUT -p tcp --dport 465 -j ACCEPT
$IPTABLES -A INPUT -p tcp --dport 993 -j ACCEPT

# 允许DNS流量 (请根据需要修改端口号)
$IPTABLES -A INPUT -p udp --dport 53 -j ACCEPT
$IPTABLES -A INPUT -p tcp --dport 53 -j ACCEPT

# 允许FTP流量
$IPTABLES -A INPUT -p tcp --dport 20 -j ACCEPT
$IPTABLES -A INPUT -p tcp --dport 21 -j ACCEPT
$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 允许MySQL流量 (请根据需要修改端口号)
$IPTABLES -A INPUT -p tcp --dport 3306 -j ACCEPT

# 允许NTP流量
$IPTABLES -A INPUT -p udp --dport 123 -j ACCEPT

# 允许SNMP流量
$IPTABLES -A INPUT -p udp --dport 161 -s $LOCAL_NET -j ACCEPT
$IPTABLES -A OUTPUT -p udp --dport 161 -d $LOCAL_NET -j ACCEPT

# 允许ICMP协议包通过
$IPTABLES -A INPUT -p icmp -j ACCEPT

# 查看iptables规则
$IPTABLES -nvL

# 保存iptables设置
service iptables save

Esta secuencia de comandos describe con más detalle cómo establecer y aplicar varias reglas en iptables, incluidas las políticas predeterminadas, las interfaces de bucle invertido, las conexiones relacionadas establecidas, los protocolos y los puertos, como HTTP/HTTPS/SSH, los ataques SYN Flood, las redes locales, el servicio de correo y el servicio DNS. , servicio FTP, servicio MySQL, paquetes de protocolo NTP, SNMP e ICMP, etc. Puede modificarlo y usarlo de acuerdo con sus necesidades reales.

Supongo que te gusta

Origin blog.csdn.net/m0_55877125/article/details/130849192
Recomendado
Clasificación