iptables 命令学习

iptables 命令学习


摘要

Linux 早起版本使用netfilter进行数据包过滤.
最新的版本开始改用 ebpf的方式进行内核编程式的包过滤.

netfilter 可以理解为内核态的一个处理机制
iptables 是在用户态进行管理netfilter配置的工具. 

也就可以理解为:
iptables 是管理netfilter的一个工具
便于实现安全以及路由等功能. 

四表

raw 表中的规则可以被哪些链使用:PREROUTING,OUTPUT
mangle 表中的规则可以被哪些链使用:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
nat 表中的规则可以被哪些链使用:PREROUTING,OUTPUT,POSTROUTING(centos7中还有INPUT,centos6中没有)
filter 表中的规则可以被哪些链使用:INPUT,FORWARD,OUTPUT

五链

PREROUTING 的规则可以存在于:raw表,mangle表,nat表。
INPUT 的规则可以存在于:mangle表,filter表,(centos7中还有nat表,centos6中没有)。
FORWARD 的规则可以存在于:mangle表,filter表。
OUTPUT 的规则可以存在于:raw表mangle表,nat表,filter表。
POSTROUTING 的规则可以存在于:mangle表,nat表。

四表五链的关系--来自朱双印的blog

image


一些命令注意事项

关于关闭防火墙
很多blog说可以使用 iptables -F 的方式来清理防火墙的规则.
但是这一点有很多问题. 

建议需要先查看一下默认规则. 
iptables -L -v -n
注意最好是如下规则再进行-F 
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

如果是drop 后者是 reject 的话建议不要如此使用
建议可以使用如下命令进行修改
iptables -P INPUT ACCEPT

需要注意后面其实有很多注意事项: 
-L 罗列相关信息
-v 冗余模式
-P 设置链的策略 -P 链路 策略

后面会将帮助打出来一遍进行学习与查看. 

一些常用命令

1. 查看, 注意可以通过 -t 查看特定的filter
iptables -L -n -v
2. 屏蔽IP地址
iptables -A INPUT -s xxx.xxx.xxx.xxx -j DROP
3. 屏蔽指定的ip指定的协议
iptables -A INPUT -p tcp -s xxx.xxx.xxx.xxx -j DROP
4. 解封某个地址
iptables -D INPUT -s xxx.xxx.xxx.xxx -j DROP
5. 阻止特定的传出连接
iptables -A OUTPUT -p tcp --dport xxx -j DROP
6. 阻止特定的传入连接
iptables -A INPUT -p tcp --dport xxx -j ACCEPT
7. 一次性处理多个端口
iptables -A INPUT  -p tcp -m multiport --dports 22,80,443 -j ACCEPT
iptables -A OUTPUT -p tcp -m multiport --sports 22,80,443 -j ACCEPT
8. 使用IP地址范围
iptables -A OUTPUT -p tcp -d 192.168.100.0/24 --dport 22 -j ACCEPT
9. 设置端口转发
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 25 -j REDIRECT --to-port 2525
10. 放置flood攻击
iptables -A INPUT -p tcp --dport 80 -m limit --limit 100/minute --limit-burst 200 -j ACCEPT
11. 屏蔽ping
iptables -A INPUT -p icmp -i eth0 -j DROP
12. 开放loopback
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
13. 屏蔽指定mac地址
iptables -A INPUT -m mac --mac-source 00:00:00:00:00:00 -j DROP
14. 限制并发连接数
iptables -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 3 -j REJECT
15. 清空规则, 可以指定链表进行清除.
iptables -t nat –F
iptables -f
16. 保存和缓存iptables规则
iptables-save > ~/iptables.rules
iptables-restore < ~/iptables.rules
17. 允许建立相关连接
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT
18. 丢弃无效数据包
iptables -A INPUT -m conntrack --ctstate INVALID -j DROP
19. 组织发送邮件
iptables -A OUTPUT -p tcp --dports 25,465,587 -j REJECT
20. 组织连接到某块网卡
iptables -A INPUT -i eth0 -s xxx.xxx.xxx.xxx -j DROP

注意以上命令来源:csdn. 感谢原作者. 


关于防火墙的功能

iptables 不仅可以实现防火墙的策略
还可以实现 路由转发(软路由)
K8S比较早的版本网络栈都是采用的iptables
但是因为如果K8S的集群扩大之后会导致严重的性能衰退
iptables 管理起来非常复杂. 后来直接使用ipvs进行网络栈处理
所以iptables的功能是非常强大的. 

附录: 命令帮助

iptables -h
iptables v1.8.4

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)
[!] --proto     -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.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章