iptables-rules iptables防火墙策略模板

iptables 参数说明
使用方式:iptables -[ACD] 链名 规则说明 [选项]
iptables -I 链名 [规则号] 规则说明 [选项]
iptables -R 链名 规则号 规则说明 [选项]
iptables -D 链名 规则号 [选项]
iptables -[LS] [链名 [规则号]] [选项]
iptables -[FZ] [链名] [选项]
iptables -[NX] 链名
iptables -E 旧链名 新链名
iptables -P 链名 目标 [选项]
iptables -h(打印此帮助信息)

命令:
可以使用长选项或短选项。
–append -A 链名 追加到链
–check -C 链名 检查规则是否存在
–delete -D 链名 从链中删除匹配的规则
–delete -D 链名 规则号
从链中删除规则规则号(1表示第一个)
–insert -I 链名 [规则号]
在规则号处插入到链中(默认为1,即第一个)
–replace -R 链名 规则号
替换链中的规则规则号(1表示第一个)
–list -L [链名 [规则号]]
列出链或所有链中的规则
–list-rules -S [链名 [规则号]]
打印链或所有链中的规则
–flush -F [链名] 删除链或所有链中的所有规则
–zero -Z [链名 [规则号]]
将链或所有链中的计数器归零
–new -N 链名 创建一个新的用户定义链
–delete-chain
-X [链名] 删除用户定义的链
–policy -P 链名 目标
将链的策略更改为目标
–rename-chain
-E 旧链名 新链名
更改链的名称(同时移动任何引用)
选项:
–ipv4 -4 无操作(ip6tables-restore会忽略该行)
–ipv6 -6 错误(iptables-restore会忽略该行)
[!] --protocol -p 协议 协议:可以是协议号或名称,例如tcp
[!] --source -s 地址[/掩码][…]
源地址规范
[!] --destination -d 地址[/掩码][…]
目标地址规范
[!] --in-interface -i 输入名称[+]
网络接口名称([+]表示通配符)
–jump -j 目标
规则的目标(可能加载目标扩展)
–goto -g 链名
跳转到链,无返回
–match -m 匹配
扩展匹配(可能加载扩展)
–numeric -n 输出地址和端口的数字形式
[!] --out-interface -o 输出名称[+]
网络接口名称([+]表示通配符)
–table -t 表名 要操作的表(默认为`filter’)
–verbose -v 详细模式
–wait -w [秒] 在放弃之前尝试获取xtables锁的最长等待时间
–wait-interval -W [微秒] 尝试获取xtables锁的等待时间
默认为1秒
–line-numbers 在列出时打印行号
–exact -x 扩展数字(显示精确值)
[!] --fragment -f 仅匹配第二个或更多的分片
–modprobe=<命令> 尝试使用此命令插入模块
–set-counters 数据包 字节 在插入/追加期间设置计数器
[!] --version -V 打印软件包版本。

Modify this file accordingly for your specific requirement.

(根据您的具体要求相应地修改此文件)

1. 删除所有现有规则 (Delete all existing rules)

iptables -F

2. 设置默认链策略 (Set default chain policies)

iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP

3. 阻止特定的IP地址 (Block a specific IP address)

BLOCK_THIS_IP=“x.x.x.x”
iptables -A INPUT -s “$BLOCK_THIS_IP” -j DROP

4. 允许所有传入的SSH连接 (Allow ALL incoming SSH)

iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

5. 只允许特定网络的传入SSH连接 (Allow incoming SSH only from a specific network)

iptables -A INPUT -i eth0 -p tcp -s 192.168.200.0/24 --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

6. 允许传入的HTTP连接 (Allow incoming HTTP)

iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT

允许传入的HTTPS连接 (Allow incoming HTTPS)

iptables -A INPUT -i eth0 -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT

7. 多端口允许传入SSH、HTTP和HTTPS连接 (MultiPorts: Allow incoming SSH, HTTP, and HTTPS)

iptables -A INPUT -i eth0 -p tcp -m multiport --dports 22,80,443 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp -m multiport --sports 22,80,443 -m state --state ESTABLISHED -j ACCEPT

8. 允许传出的SSH连接 (Allow outgoing SSH)

iptables -A OUTPUT -o eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

9. 只允许传出的SSH连接到特定网络 (Allow outgoing SSH only to a specific network)

iptables -A OUTPUT -o eth0 -p tcp -d 192.168.101.0/24 --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

10. 允许外发的HTTPS连接 (Allow outgoing HTTPS)

iptables -A OUTPUT -o eth0 -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT

11. 加载均衡传入的HTTPS流量 (Load balance incoming HTTPS traffic)

iptables -A PREROUTING -i eth0 -p tcp --dport 443 -m state --state NEW -m nth --counter 0 --every 3 --packet 0 -j DNAT --to-destination 192.168.1.101:443
iptables -A PREROUTING -i eth0 -p tcp --dport 443 -m state --state NEW -m nth --counter 0 --every 3 --packet 1 -j DNAT --to-destination 192.168.1.102:443
iptables -A PREROUTING -i eth0 -p tcp --dport 443 -m state --state NEW -m nth --counter 0 --every 3 --packet 2 -j DNAT --to-destination 192.168.1.103:443

12. 内部到外部的Ping (Ping from inside to outside)

iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT

13. 外部到内部的Ping (Ping from outside to inside)

iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT

14. 允许回环访问 (Allow loopback access)

iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

15. 允许内部网络访问外部网络 (Allow packets from internal network to reach external network)

if eth1 is connected to external network (internet)

if eth0 is connected to internal network (192.168.1.x)

iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT

16. 允许出站DNS (Allow outbound DNS)

iptables -A OUTPUT -p udp -o eth0 --dport 53 -j ACCEPT
iptables -A INPUT -p udp -i eth0 --sport 53 -j ACCEPT

17. 允许NIS连接 (Allow NIS Connections)

rpcinfo -p | grep ypbind ; This port is 853 and 850

iptables -A INPUT -p tcp --dport 111 -j ACCEPT
iptables -A INPUT -p udp --dport 111 -j ACCEPT
iptables -A INPUT -p tcp --dport 853 -j ACCEPT
iptables -A INPUT -p udp --dport 853 -j ACCEPT
iptables -A INPUT -p tcp --dport 850 -j ACCEPT
iptables -A INPUT -p udp --dport 850 -j ACCEPT

18. 允许特定网络的rsync (Allow rsync from a specific network)

iptables -A INPUT -i eth0 -p tcp -s 192.168.101.0/24 --dport 873 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 873 -m state --state ESTABLISHED -j ACCEPT

19. 仅允许特定网络的MySQL连接 (Allow MySQL connection only from a specific network)

iptables -A INPUT -i eth0 -p tcp -s 192.168.200.0/24 --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 3306 -m state --state ESTABLISHED -j ACCEPT

20. 允许Sendmail或Postfix (Allow Sendmail or Postfix)

iptables -A INPUT -i eth0 -p tcp --dport 25 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 25 -m state --state ESTABLISHED -j ACCEPT

21. 允许IMAP和IMAPS (Allow IMAP and IMAPS)

iptables -A INPUT -i eth0 -p tcp --dport 143 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 143 -m state --state ESTABLISHED -j ACCEPT

iptables -A INPUT -i eth0 -p tcp --dport 993 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 993 -m state --state ESTABLISHED -j ACCEPT

22. 允许POP3和POP3S (Allow POP3 and POP3S)

iptables -A INPUT -i eth0 -p tcp --dport 110 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 110 -m state --state ESTABLISHED -j ACCEPT

iptables -A INPUT -i eth0 -p tcp --dport 995 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 995 -m state --state ESTABLISHED -j ACCEPT

23. 防止DoS攻击 (Prevent DoS attack)

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

24. 端口转发422到22 (Port forwarding 422 to 22)

iptables -t nat -A PREROUTING -p tcp -d 192.168.102.37 --dport 422 -j DNAT --to 192.168.102.37:22
iptables -A INPUT -i eth0 -p tcp --dport 422 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 422 -m state --state ESTABLISHED -j ACCEPT

25. 记录被丢弃的数据包 (Log dropped packets)

iptables -N LOGGING
iptables -A INPUT -j LOGGING
iptables -A LOGGING -m limit --limit 2/min -j LOG --log-prefix "IPTables Packet Dropped: " --log-level 7
iptables -A LOGGING -j DROP

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值