先來一句:好記性不如爛筆頭!
1、iptables簡介
iptables是基於包過濾的防火牆,它主要工作在osi模型的2,3,4層,也可以工作在7層(iptables + squid)
2、原理
防火牆是一層一層過濾的。按照配置的規則的順序從上到下,從前到后。
如果匹配上規則既明確表明是阻止還是通過,此時數據包就不再向下匹配新規則了。
如果所有規則都沒有匹配上,就會一直向下匹配,直到匹配上默認規則得到明確的阻止還是通過
注釋:規則表的先后順序:raw→mangle→nat→filter
3、表和鏈
iptables有4張表filter nat mangle raw ,常用的是filter表和nat表
表(tables) |
鏈(chains) |
||||
INPUT |
OUTPUT |
FORWARD |
PREROUTING |
POSTROUTING |
|
Filter* |
√ |
√ |
√ |
╳ |
╳ |
Nat* |
╳ |
╳ |
√ |
√ |
√ |
Mangle |
√ |
√ |
√ |
√ |
√ |
注:filter表 三個鏈,真正的防火牆功能;Nat表 三個鏈表 input和后邊兩個 負責數據包改寫、端口映射 |
INPUT: 對進入主機的數據包進行過濾。
OUTPUT: 對流出的數據包進行過濾。
FORWARD: 對流經的數據包進行過濾。轉發數據包的功能。
PREROUTING: 在數據包到達防火牆時,進行路由判斷之前執行的規則。改變數據包的目的地址和端口等,映射端口或者是ip。
POSTROUTING: 在數據包離開防火牆時,進行路由判斷之前的規則。改變數據包的源地址和源端口。局域網共享上網。
規則鏈的先后順序
入站順序:PREROUTING→INPUT
出站順序:OUTPUT→POSTROUTING
轉發順序:PREROUTING→FORWARD→POSTROUTING
4、iptables的安裝
Centos7開始已經沒有iptables了,默認使用的是Firewalld
4.1、關閉firewalld
systemctl status firewalld.service #檢測是否開啟了firewall
systemctl stop firewalld.service #關閉firewall
sytsemctl disable firewalld.service #禁止firewall開機自啟
4.2、安裝iptables
yum install iptables-services -y
systemctl start iptables.service systemctl enable iptables.service
service iptables save #所有新加的規則要先保存后再重啟,要不會丟配置
iptables-save
5、常用命令
5.1、清空已有的規則
iptables -F 清除用戶自定義的規則 iptables -X 清除用戶自定義的鏈 iptables -Z 清除鏈的計數器
5.2、查看所有的規則
iptables -nL
5.3、查看版本
iptables -V
5.4 基本操作
CentOS 配置防火牆操作實例(啟、停、開、閉端口):
查詢防火牆狀態 : service iptables status
停止防火牆 : service iptables stop
啟動防火牆 : service iptables start
重啟防火牆 : service iptables restart
永久關閉防火牆 : chkconfig iptables off
永久關閉后啟用 : chkconfig iptables on
6、filter配置示例
6.1、刪除指定規則
[root@redis3 ~]# iptables -nL --line-number Chain INPUT (policy ACCEPT) num target prot opt source destination 1 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:9000 [root@redis3 ~]# iptables -D INPUT 1
注釋:最好選ACCEPT和DROP 因為拒絕會給客戶信息 -t filter 一般不添加,默認就是filter
6.1、開放一個端口
iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
6.2、允許某個IP或段鏈接本機
iptables -A INPUT -s 10.100.0.158/32 -p tcp -j ACCEPT
iptables -A INPUT -s 10.100.0.158/32 -p all -j ACCEPT(內網適合)
6.3、允許某個IP連接某個端口
iptables -A INPUT -s 10.88.89.11/32 -p tcp -m tcp --dport 9102 -j ACCEPT
6.4、插入一個規則
iptables -I INPUT 2 -p tcp --dport 9000 -j DROP # 在序號1后插入一條規則
6.5、匹配端口范圍
iptables -I INPUT -p tcp -m multiport --dport 21,22,23 -j ACCEPT iptables -I INPUT -p tcp --dport 18:80 -j DROP
6.6、取反
除網絡是192.168.0.0/24的網絡外 都允許訪問。 [root@test ~]# iptables -t filter -I INPUT ! -s 192.168.0.0/24 -j ACCEPT
只要主機不是10.0.0.1的都允許訪問80端口 [root@test ~]# iptables -A INPUT ! -s 10.0.0.1 -p tcp --dport 80 -j ACCEPT
iptables -t filter -A INPUT -i etho ! -s 10.0.0.200 -j DROP
6.7、匹配網絡狀態(不是必須的)
-m state --state
NEW: 已經或將啟動的新連接
ESTBILISHED: 已建立的連接
RELATED: 正在啟動的新連接
INVALID: 非法或無法識別的連接
# 允許關聯的包(一般的包不需要,ftp比較特殊)
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
6.8、允許ICMP類型協議通過
iptables -A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT
或
ipttables -A INPUT -p icmp -s 10.0.0./24 -m icmp --icmp-type any -j ACCEPT
6.9、封外部IP
iptables -I INPUT -S 10.0.0.200 -j DROP 或 iptables -I INPUT -p tcp -s 10.0.0.200 --dport 80 -j DROP (細化拒絕)
6.10、一個防DDOS工具腳本(來自於老男孩)

#!/bin/bash # name wangfei # qq 1005487137 # mail wangfei1000@yeah.net # version 2.1 function CelIp(){ CleTime="0000" NowTime=`date +%H%M` YDenyIpFile="/tmp/deny_ip_`date +%F -d "-1days"`.log" if [ "$CleTime" == "$NowTime" ] ;then if [ -f "$YDenyIpFile" ];then while read line do iptables -D INPUT -s $line -j DROP done<$YDenyIpFile mv $YDenyIpFile $YDenyIpFile.bak sleep 60 fi fi } function DenyIp(){ IpList=($(awk -F "[ :/]+" '{ip[$3]=ip[$3]+1}END{for(k in ip) print k,ip[k]}' access.log_06|awk '$2>30{print $1}')) for ip in ${IpList[@]} do if [ `iptables -nL|grep $ip|wc -l` -lt 1 ];then echo $ip>>/tmp/deny_ip_$(date +%F).log iptables -I INPUT -s $ip -j DROP && echo "deny $ip success." fi done } while : do CelIp DenyIp sleep 5 done
7、net配置示例
一定要在安裝防火牆模塊后重啟iptables后再配置
modprobe ip_tables
modprobe iptable_nat
modprobe iptable_filter
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state
1、代理服務器設置
# 需要開啟linux內核轉發 [root@test ~]# sed -i 's@net.ipv4.ip_forward =.*@net.ipv4.ip_forward =1@g' /etc/sysctl.conf [root@test ~]# sysctl -p [root@test ~]# iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -o eth1 -j SNAT --to-source 10.0.0.55 #要代理的網段 指定外網IP地址 如果是映射到多個ip上,可這樣 iptables -t nat -I POSTROUTING -s 172.16.1.0/24 -j SNAT --to-source 10.0.0.99-10.0.0.155 # 適用於外網ip不固定的情況(ADSL) [root@test ~]# iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j MASQUERADE 多外網IP地址或沒有固定的外網IP地址的時候可以自動選擇合適的外網網卡使用 注意: 不僅需要nat設置,還需要將iptables的Forward設置為 ACCEPT
2、客戶端
內網客戶端需要將網關指向代理服務器的內網IP
1、映射端口或IP
注意: 1、 代理端需要打開系統內核轉發。 2、 客戶端的內網網關需要指向代理服務器的內網IP。 3、 客戶端如果是測試環境需要關閉外網網卡。
iptables -t nat -A PREROUTING -d 10.0.0.5 -p tcp --dport 9000 -j DNAT --to-destination 172.16.1.52:80 實現外部10.0.0.99 對映射到172.16.1.5上,如下操作即可。 iptables -t nat -A PREROUTING -d 10.0.0.99 -j DNAT --to-destination 172.16.1.5 iptables -t nat -A POSTROUTING -s 172.16.1.5 -j SNAT --to-source 10.0.0.99
寫的很好的博客: https://blog.csdn.net/chengxuyuanyonghu/article/details/64441374