iptables防火墙

1.1防火墙简介

在普通l服务器上用iptables提供几百号人的网关和路由功能上,iptables 丝毫不输于真实的交换机和路由器。

1.2 iptables 名词和术语

1.2.1 什么是容器?

在iptables中是用来描述包含和属于的关系。

1.2.2 什么是netfilter/iptables?

netfilter是表(tables)的容器。把小区中的一栋楼比作netfilter,表比作你们家的一套房子,那么表tables 就属于netfilter。

1.2.3什么是表(tables)?

tables是链的容器,即 所有的链chains都属于表tables。把链chains比作房间,那么chains就属于tables。

1.2.4 什么是链chains?

链chains是规则policys的容器,链属于表。把tables比作一间房,那么chains就相当于是房子中的桌子或者柜子等家具。

1.2.5 什么是规则policy?

规则policy 属于chains, 就是iptables一系列过滤信息规范和具体操作方法。可以理解为规定各个家具的摆放。

 

 

 

1.3.防火墙的执行规则表:

 

 

 

小结:

1)匹配规则,从上到下,已经匹配,不再向下。

2)如果最终还没找到匹配项,则根据防火墙的默认匹配规则来决定是否通过还是丢弃。

 

1.4 iptables的表tables和连chains的分类

常用表和链的对应关系:

 

1.4.2filter表介绍

 

1.4.3 NAT表介绍

 

 

1.4.4 mangle表

 

 

1.5iptables的表和链的工作流程

 

 

由于工作中很少用到mangle表,为了学习需要学习一下工作流程就够了:

 

 

 


[root@wr ~]# iptables -h

iptables v1.4.7

Usage: iptables -[AD] 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

--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:

[!] --proto -p proto protocol: by number or name, eg. `tcp' or 'TCP' or 'ALL'

[!] --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

--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.


常用命令:(不指定-t, 默认就是fillter表)

1.查看表规则

# iptables -L -n -t nat //列出相应表中的规则。

2.清楚表规则(命令行的清除,iptabls重启后不会生效

# iptables -F //清除所有

# iptables -X //删除用户自定义的链

# iptables -Z //链的计数器清零

# iptables -n nat -F //清空nat的所有表。

 

3.禁止ssh默认的22端口:

# iptables -A INPUT -p tcp --dport 22 -j DROP

# iptables -D INPUT -p TCP --dport 22 -j DROP

* 其中 INPUT和DROP等关键字,必须是大写。

* 行为参数:

--jump -j target

提示:target常见处理方法有ACCEPT、DROP(丢弃)、REJECT(拒绝),其中,一般不用REJECT ,存在安全隐患。更多跳转,如:SNAT(源地址转换)、DNAT(目的地址转换)、MASQUERADE(伪装),这几个长用于处理nat表的规则。

 

4.通过显示行号,然后行号删除

# iptables -L -n --line-numbers

# iptables -D INPUT 1 删除第二条规则

 

5.禁止网段连入:

# iptables -t filter -A INPUT -i eth0 -s 10.1.0.0/24 -j DROP

6.除了10.1.0.152发入的请求,其他都拒绝。

# iptables -t filter -A INPUT -i eth0 -s ! 10.1.0.152 -j DROP

7.企业实战:封IP(-I, 插入到第一行)

# iptables -I INPUT -s 10.1.0.15 -j DROP 封某个IP地址。

非内部机器禁ping:

# iptables -I INPUT -p icmp -s 10.1.0.0/24 -j DROP

 

匹配端口范围:

# iptables -A INPUT -p tcp --dport 22:80 -j DROP //端口范围

# iptables -A INPUT -p tcp -m multiport --dport 21,23,27 -j DROP //多个端口

 

 


 

电影院模式设置防火墙规则(最小化设置)

 

1.清空所有

# iptables -F //清除所有链的规则,默认是filter

# iptables -X //删除用户自定义的链,默认是filter

# iptables -Z //链的计数器清零,默认是filter

# iptables -n nat -F //清空nat的所有表。

2.配置允许SSH登陆端口进入

# iptables -A INPUT -p tcp --dport 22 -s 10.1.0.0/24 -j ACCEPT

3.配置允许lo接口的进入和流出

# iptables -A INPUT -i lo -j ACCEPT

# iptables -A OUTPUT -o lo -j ACCEPT

4.设置默认的禁止和允许规则(改成了电影院模式)

# 设置默认DROP掉FORWARD ,INPUT,允许output。

# iptables --policy OUTPUT ACCEPT

# iptables -P FORWARD DROP

# iptables -P INPUT DROP

5.开启信任的IP网段(允许自己人通过)

#允许IDC LAN和办公网IP的访问

# iptables -A INPUT -s 211.166.0.0/16 -p all -j ACCEPT

# ptables -A INPUT -s 172.16.1.0/24 -p all -j ACCEPT

# iptables -A INPUT -s 10.1.0.0/24 -p all -j ACCEPT

6.允许http服务通过

#允许外部访问80端口

# iptables -A INPUT -p tcp --dport 80 -j ACCEPT

7.允许icmp类型协议通过

iptables -A INPUT -p icmp -s 10.1.0.0/24 -j ACCEPT //允许指定ping

iptables -A INPUT -p icmp -j ACCEPT //允许所有ping

8.允许关联的状态包通过(类似ftp的服务)

# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

9. 保存

# service iptables save

10.最小化的一个配置

#以后添加允许规则,直接在配置文件中添加规则就好了。

 

# Generated by iptables-save v1.4.7 on Thu Feb 23 20:55:43 2017

*nat

:PREROUTING ACCEPT [125:19490]

:POSTROUTING ACCEPT [12:1029]

:OUTPUT ACCEPT [12:1029]

COMMIT

# Completed on Thu Feb 23 20:55:43 2017

# Generated by iptables-save v1.4.7 on Thu Feb 23 20:55:43 2017

*filter

:INPUT DROP [0:0]

:FORWARD DROP [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -s 10.1.0.0/24 -p tcp -m tcp --dport 22 -j ACCEPT

-A INPUT -i lo -j ACCEPT

-A INPUT -s 10.1.0.0/24 -j ACCEPT

-A INPUT -s 211.166.0.0/16 -j ACCEPT

-A INPUT -s 172.16.1.0/24 -j ACCEPT

-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

-A OUTPUT -o lo -j ACCEPT

-A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

COMMIT

# Completed on Thu Feb 23 20:55:43 2017


1.防止ddos攻击:

iptables -I INPUT -s 202.10.5.9 -j DROP

2.通过脚本去设置iptables

3.直接更改配置文件。

 

 

 

 

posted @ 2017-02-27 23:07  谢小花❀  阅读(255)  评论(0编辑  收藏  举报