iptables企業應用

出處:http://linux5588.blog.51cto.com/65280/752084

iptables企業應用,先看拓撲圖:

如圖所示,使用虛擬機搭建的實驗環境:

1.筆記本電腦(簡稱外網用戶)模擬的是外網用戶
2.中間服務器(簡稱網關)系統Linux,使用iptables防火牆,來當作內網的網關
3.Windows2003是內網中的一臺服務器
4.內網的客戶端電腦
其中 :
網關eth0通過橋接方式和筆記本電腦通信,而eth1使用custom (VMnet1 host-only)方式
Windows2003也是使用custom (VMnet1 host-only)方式

測試:
內網客戶端ping 網關

 網關本身可不可以上網

 

實驗開始:
一、開啓網關的內核轉發數據包功能,默認Linux是不開啓數據包轉發功能

  1. [root@server1 ~]# echo "1" /proc/sys/net/ipv4/ip_forward      #可以這麼開啓,不過重啓之後就失效了,因爲這個文件是內存中的 
基於這種情況,要修改內核的配置文件vim /etc/sysctl.conf 

  1. # Controls IP packet forwarding  
  2. net.ipv4.ip_forward = 1                  #找到這行,把0修改爲1 

  1. [root@server1 ~]# sysctl -p        #這條命令可以把這些指令重新加載到內核中去  
  2. net.ipv4.ip_forward = 1 
  3. net.ipv4.conf.default.rp_filter = 1 
  4. net.ipv4.conf.default.accept_source_route = 0 
  5. kernel.sysrq = 0 
  6. kernel.core_uses_pid = 1 
  7. net.ipv4.tcp_syncookies = 1 
  8. kernel.msgmnb = 65536 
  9. kernel.msgmax = 65536 
  10. kernel.shmmax = 4294967295 
  11. kernel.shmall = 268435456 

  1. [root@server1 ~]# cat /proc/sys/net/ipv4/ip_forward         #現在查看這個文件的值爲1  
  2. 1  
  3. [root@server1 ~]#  
二、在網關上做SNAT使內網電腦可以連接到Internet
 
  1. [root@server1 ~]# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to 192.168.18.110 

客戶端上網還有一個前提,就是iptables filter 表中的FORWARD鏈默認是ACCEPT狀態,這麼一條纔可以使客戶端上網。如果默認是DROP狀態的話,那客戶端也是無法上網的。


三、能讓客戶端上網的方法

方法一、把FORWARD的默認策略設置爲DROP,然後一個一個服務開放,只能開放我們知道端口(用工具抓包,然後分析出IP地址和端口)的服務或者軟件,這種情況會造成迅雷等軟件無法使用(因爲不知道迅雷下載時候用什麼端口)

1.能ping
 
  1. iptables -t filter -A FORWARD -p icmp -j ACCEPT 
2.能看網頁
內網客戶機出去的目標端口是80,53都放行
  1. iptables -t filter -A FORWARD -p tcp -i  eth1  --dport 80 -s 192.168.0.0/24 -j ACCEPT  
  2. iptables -t filter -A FORWARD -p tcp  -i  eth1 --dport 53 -s 192.168.0.0/24 -j ACCEPT    
  3. iptables -t filter -A FORWARD -p udp  -i  eth1 --dport 53 -s 192.168.0.0/24 -j ACCEPT  
  4. 者 兩者寫其中一個即可  
  5. iptables -t filter -A FORWARD -p tcp   --dport 80 -s 192.168.0.0/24 -j ACCEPT  
  6. iptables -t filter -A FORWARD -p tcp   --dport 53 -s 192.168.0.0/24 -j ACCEPT    
  7. iptables -t filter -A FORWARD -p udp  --dport  53 -s 192.168.0.0/24 -j ACCEPT  
解析後回來的源端口是80,53的都給與放行
  1. iptables -t filter -A FORWARD -p tcp -i eth0 --sport 80 -d 192.168.0.0/24 -j ACCEPT  
  2. iptables -t filter -A FORWARD -p udp -i eth0 --sport 53 -d 192.168.0.0/24 -j ACCEPT  
  3. iptables -t filter -A FORWARD -p tcp -i eth0 --sport 53 -d 192.168.0.0/24 -j ACCEPT   
  4. 或者,兩者寫其中一個即可  
  5. iptables -t filter -A FORWARD -p tcp  --sport 80 -d 192.168.0.0/24 -j ACCEPT  
  6. iptables -t filter -A FORWARD -p udp  --sport 53 -d 192.168.0.0/24 -j ACCEPT  
  7. iptables -t filter -A FORWARD -p tcp  --sport 53 -d 192.168.0.0/24 -j ACCEPT 
 
3.能收發郵件
 
  1. iptables -t filter -A FORWARD -p tcp --dport 25 -s 192.168.0.0/24 -j ACCEPT   
  2. iptables -t filter -A FORWARD -p tcp --sport 25 -d 192.168.0.0/24 -j ACCEPT   
  3.  
  4. iptables -t filter -A FORWARD -p tcp --sport 110 -d 192.168.0.0/24 -j ACCEPT   
  5. iptables -t filter -A FORWARD -p tcp --dport 110 -s 192.168.0.0/24 -j ACCEPT    
  6.  
  7. iptables -t filter -A FORWARD -p tcp --sport 143 -d 192.168.0.0/24 -j ACCEPT    
  8. iptables -t filter -A FORWARD -p tcp --dport 143 -s 192.168.0.0/24 -j ACCEPT    
有其他端口的類似設置就可以了,一出一進配合。

 
4.上QQ
QQ登錄時可以使用2種情況,udp 8000 /tcp 80、443,會自動檢測那個端口開放着,那個開放,就用那個端口登錄
UDP 8000
 
  1. iptables -t filter -A FORWARD -p udp --dport 8000 -s 192.168.0.0/24 -j ACCEPT  
  2. iptables -t filter -A FORWARD -p udp --sport 8000 -d 192.168.0.0/24 -j ACCEPT     
  3.  
  4. tcp 80(跟網頁端口一樣80,所以只要能看網頁,那就能上QQ)  
  5. iptables -t filter -A FORWARD -p tcp  --sport 80 -d 192.168.0.0/24 -j ACCEPT  
  6.  iptables -t filter -A FORWARD -p tcp   --dport 80 -s 192.168.0.0/24 -j ACCEPT  
  7.    
  8. tcp 443  
  9. iptables -t filter -A FORWARD -p tcp  --sport 443 -d 192.168.0.0/24 -j ACCEPT  
  10. iptables -t filter -A FORWARD -p tcp   --dport 443 -s 192.168.0.0/24 -j ACCEPT  
更嚴格的措施,就是找出QQ登錄服務器的地址,然後設置:
  1. iptables -t filter -A FORWARD -p tcp --dport 443  -s 192.168.0.0/24 -d 119.147.45.43 -j ACCEPT  
  2. iptables -t filter -A FORWARD -p tcp --sport 443 -s 119.147.45.43 -d 192.168.0.0/24 -j ACCEPT  
  3.  
  4. iptables -t filter -A FORWARD -p tcp --dport 80  -s 192.168.0.0/24 -d 112.95.240.16 -j ACCEPT  
  5. iptables -t filter -A FORWARD -p tcp --sport 80 -s 112.95.240.16 -d 192.168.0.0/24 -j ACCEPT  

 
這種情況會造成比較多的軟件無法使用,只有一個一個找出軟件的IP地址和端口,才能使用,不建議這麼設置

 
方法二、也是把FORWARD鏈的默認策略設置爲DROP,然後手工添加要上網的IP地址(當然也可以用腳本批量添加),這樣沒有進行設置的客戶端就無法上網,這種情況可以使用迅雷下載,還可以限制迅雷的速度,推薦使用這種方法

做以上設置是很嚴格了,但是好多應用不能用,而且迅雷這種軟件根本就不知道用什麼端口,連接那個IP地址,而且我們有事又需要用迅雷下載,這時我們就可以限制迅雷的下載速度,按照以下命令來:
a.讓所有出去都可以出去

  1. [root@server1 ~]# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to 192.168.18.110  
  2. [root@server1 ~]# iptables -t filter -A FORWARD -i eth1 -s 192.168.0.0/24 -j ACCEPT 
b.進來的要進行限速,對單個主機進行限速(要在網關的外網卡限制下載的速度,而在內網卡上限制上傳的速度)
 
  1. [root@server1 ~]# iptables -t filter -A FORWARD -i eth0 -d 192.168.0.2 -m limit --limit 10/s --limit-burst 10 -j ACCEPT  
按照這個命令,這個主機最大也就14.6KB/S,根據實際情況對速度進行調整
-m limit --limit 10/s --limit-burst 10 該選項就是對主機進行限速
--limit 10/s:10/s是每秒發個10個包,又因網卡的MTU值爲1500B,換算成KB,1500/1024=1.46KB,也就是每個包大小理想情況下是1.46KB,每秒10個包,那就是每秒1.46*10=14.6KB/S
--limit-burst 10 :最大峯值也是10個包

c.做端口映射,有時間需要把內網的服務器發佈到外網,讓外網的用戶也能訪問,這時就需要端口映射。
訪問網關的8080端口,就是相當於訪問,內網192.168.0.2的80端口

  1. [root@server1 ~]# iptables -t nat -A PREROUTING -p tcp  -d 192.168.18.110  --dport 8080 -j DNAT --to 192.168.0.2:80 
訪問網關的3389端口,就相當於訪問內網192.168.0.2的3389端口,這樣就把3389端口映射出去了
 
  1. [root@server1 ~]# iptables -t nat -A PREROUTING -p tcp  -d 192.168.18.110 --dport 3389 -j DNAT --to 192.168.0.2:3389 
做端口映射時,POSTROUTING鏈其實可以不做SNAT,iptables就能把進來的包交給內網機器,而內網機器也將數據回覆到外網的機器,原因是:iptables是一種狀態型的防火牆,不用我們手動將設置數據包的轉發方向,它也能很好的轉發我們的數據包。
查看nat表和filter表
 
  1. [root@server1 ~]# iptables -t nat -nL  
  2. Chain PREROUTING (policy ACCEPT)  
  3. target     prot opt source               destination          
  4. DNAT       tcp  --  0.0.0.0/0            192.168.18.110      tcp dpt:8080 to:192.168.0.2:80  
  5. DNAT       tcp  --  0.0.0.0/0            192.168.18.110      tcp dpt:3389 to:192.168.0.2:3389  
  6.  
  7. Chain POSTROUTING (policy ACCEPT)  
  8. target     prot opt source               destination          
  9. SNAT       all  --  192.168.0.0/24       0.0.0.0/0           to:192.168.18.110  
  10.  
  11. Chain OUTPUT (policy ACCEPT)  
  12. target     prot opt source               destination          
  13. [root@server1 ~]# iptables -t filter -nL  
  14. Chain INPUT (policy ACCEPT)  
  15. target     prot opt source               destination          
  16.  
  17. Chain FORWARD (policy DROP)  
  18. target     prot opt source               destination          
  19. ACCEPT     all  --  192.168.0.0/24       0.0.0.0/0            
  20. ACCEPT     all  --  0.0.0.0/0            192.168.0.0/24       
  21.  
  22. Chain OUTPUT (policy ACCEPT)  
  23. target     prot opt source               destination          
  24.  
  25. Chain RH-Firewall-1-INPUT (0 references)  
  26. target     prot opt source               destination  
注意:這時候,就算filter中的INPUT和OUTPUT鏈默認策略是DROP,那也不會影響端口映射的,因爲INPUT鏈和OUTPUT鏈只會對進入自己和自己出去的做限制,這時候是轉發,所以不會影響。

d.發佈內網的FTP服務器到外網去
先看FTP的工作模式:主動模式和被動模式
主動模式(Active):客戶端以一個大於1024端口的隨機端口(假設n端口),向服務器的21端口發出請求,服務器用21端口進行迴應,然後服務器會以20端口,來主動去連接客戶端的(n+1)端口,然後就開始傳輸數據.只有這種模式下才會用到20端口。

 

現在通過iptables 來發布FTP,先看主動模式:
 
  1. [root@server1 ~]# iptables -t nat -A PREROUTING -p tcp -d 192.168.18.110 --dport 21 -j DNAT --to 192.168.0.2:21  
  2. [root@server1 ~]# iptables -t nat -A PREROUTING -p tcp -d 192.168.18.110 --dport 20 -j DNAT --to 192.168.0.2:20  
這時客戶端如果設置爲被動模式的話,是連接不到服務器

被動模式(Passive)
客戶端以一個大於1024端口的隨機端口(假設n端口),向服務器的21端口發出請求,服務器用21端口進行迴應(這裏麪包含一個隨機端口m,用於和客戶端進行數據傳輸),然後客戶端以n+1端口,連接服務器的m端口,最後傳輸數據。

 

以被動模式發佈FTP,因爲被動模式服務器端口是用隨機端口和客戶端進行數據傳輸,所以我們就不知道該發佈什麼端口出去,這時就需要藉助2個模塊來實現。ip_nat_ftp和ip_conntrack_ftp這2個模塊,加載到內核當中去,就可以實現FTP的被動模式的發佈。
 
  1. [root@server1 ~]# modprobe ip_nat_ftp  
  2. [root@server1 ~]# modprobe ip_conntrack_ftp  
  3. [root@server1 ~]# lsmod | grep ftp  
  4. 2:ip_nat_ftp              7361  0  
  5. 3:ip_conntrack_ftp       11569  1 ip_nat_ftp  
  6. 14:ip_nat                 20973  2 ip_nat_ftp,iptable_nat  
  7. 15:ip_conntrack           53281  5 ip_nat_ftp,ip_conntrack_ftp,ip_conntrack_netbios_ns,iptable_nat,ip_nat  
  8. 只需要發佈21端口出去就可以了。  
  9. [root@server1 ~]# iptables -t nat -A PREROUTING -p tcp -d 192.168.18.110 --dport 21 -j DNAT --to 192.168.0.2:21  
這樣客戶端就可以以被動模式(FTP客戶端可以設置被動模式和主動模式)來連接到服務器,這時如果用主動模式連接服務器,是連接不到的。

所以FTP工作於什麼模式,完全在於FTP客戶端的設置,而不在於服務器端,大多數情況下服務器端是不開放20端口,所以我們只能用被動模式去連接處於內網中的FTP服務器

方法三、把FORWARD鏈默認設置爲ACCEPT,然後來限制某些服務的運行,這種情況下,無法限制迅雷的速度,因爲一旦FORWARD鏈設置爲ACCEPT的話,其他策略設置都不起作用,不推薦這種方法,還有如果上網行爲嚴格的話,那麼要找到那麼軟件的使用的端口及IP地址很是麻煩。

限制QQ登錄,首先要找到QQ登錄都使用那些地址
 tcpdump -i eth1 host 192.168.0.2 -s 0 -w qq.pcap  使用這個命令可以抓包,就可以知道某個軟件使用什麼端口,IP地址信息,然後把qq.pcap下載下來,用wireshark來分析 (點 Statistics-Conversation-IPv4),就可以看到QQ使用了多個地址,默認使用了UDP 8000 端口,把這些地址記下,使用iptables 來做限制

  1. iptables -t filter -A FORWARD -p udp -s 192.168.0.0/24 -d 112.95.240.16 --dport 8000 -j DROP 
 
  1. iptables -t filter -A FORWARD -p udp -s 192.168.0.0/24 -d 112.95.240.16 -m multiport  --dport 8000,80,443 -j DROP 
多端口要這麼設置
然後把這些地址和這些端口,封掉,但是webQQ無法封,因爲使用80端口,以此類推找到其他不能使用的軟件的端口IP地址,禁止掉。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章