iptables練習

iptables實戰

1.開啟防火墻

systemctl start firewalld

2.清空所有的默認規則,定義自己的規則

iptables -F

查看此時的iptables
iptables -nL

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 
略...... 

3.我們準備建立自己的規則

(1) 放行ssh (端口:22)

iptables -t filter -A INPUT  -s 0/0  -d 192.168.42.153  -p tcp --dport 22 -j ACCEPT 或者
iptables -A INPUT -d 192.168.42.153 -p tcp --dport 22 -j ACCEPT
iptables -t filter -A OUTPUT  -s 192.168.42.153  -d 0/0   -p tcp   --sport 22  -j ACCEPT 或者
iptables -A  OUTPUT -s  192.168.42.153  -p tcp  --sport  22 -j ACCEPT

(2)修改默認規則鏈(關閉所有端口)

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

(3)放行web(80)端口 httpd nginx

iptables -I INPUT -d 192.168.42.153 -p tcp --dport 80 -j ACCEPT 或者
iptables -t filter -I INPUT -d 192.168.42.153 -p tcp --dport 80 -j ACCEPT

iptables -I OUTPUT -s 192.168.42.153 -p tcp --sport 80 -j ACCEPT 或者
iptables -t filter -I OUTPUT -s 192.168.42.153  -d 0/0 -p tcp --sport 80 -j ACCEPT

(4)修改默認規則鏈后,我們發現ping不通自己,也ping不通別的主機

iptables -t filter -I INPUT -s 127.0.0.1 -d 127.0.0.1 -i lo  -j ACCEPT 
iptables -t filter -I OUTPUT -s 127.0.0.1 -d 127.0.0.1 -o lo  -j ACCEPT

(5)允許自己ping別的主機

iptables -t filter -I OUTPUT -s 192.168.42.153 -d 0/0  -p icmp --icmp-type 8 -j ACCEPT
iptables -t filter -I INPUT -s 0/0 -d 192.168.42.153 -p icmp --icmp-type 0 -j ACCEPT

(6)允許任何人來ping本機

iptables -t filter -I INPUT -s 0/0 -d 192.168.42.153 -p icmp --icmp-type 8 -j ACCEPT
iptables -t filter -I OUTPUT -s 192.168.42.153 -d 0/0  -p icmp --icmp-type 0 -j ACCEPT

(7)同時開發多個端口(多端口匹配)

iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp -m multiport --dports 22,80,3306 -j ACCEPT
iptables -I INPUT -d 0/0 -s 192.168.42.153 -p tcp -m multiport --sports 22,80,3306 -j ACCEPT

(8)iptables -vnL –line-numbers #顯示數字

iptables  -vnL INPUT  --line-numbers 
Chain INPUT (policy DROP 1 packets, 229 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        8   576 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.42.153       icmptype 8
2       12  1008 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.42.153       icmptype 0
3       16  1226 ACCEPT     all  --  lo     *       127.0.0.1            127.0.0.1           
4       88  7565 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.42.153       tcp dpt:80
5     2135  163K ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.42.153       tcp dpt:22

(9) 源地址,目的地址范圍匹配

iptables -I INPUT -d 192.168.42.153 -p tcp --dport 23 -m iprange --src-range 192.168.42.150-192.168.42.158 -j ACCEPT

iptables -I OUTPUT -s 192.168.42.153 -p tcp --dport 23 -m iprange --dst-range  192.168.42.150-192.168.42.158 -j ACCEPT

(10)禁止包含”old”字符的頁面出來

iptables -I OUTPUT -s 192.168.42.153 -d 0/0 -p tcp --sport 80 -m string --algo bm --string "old" -j DROP

(11)基于時間限定,9點到19點,禁止訪問80端口

iptables -I INPUT -s 0/0  -d 192.168.42.153 -p tcp --dport 80  -m time --timestart 09:00:00 --timestop 19:00:00 --kerneltz  -j DROP

(12)周一到周五9點到19點禁止訪問80端口

iptables -I INPUT  -d 192.168.42.153 -p tcp --dport 80  -m time --timestart 09:00:00 --timestop 19:00:00 --kerneltz --weekdays 1,2,3,4,5  -j DROP

(13)端口大于2個并發連接(禁止)

iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp  --dport 22 -m connlimit --connlimit-above 2 -j DROP

(14)端口同一個客戶端小于3個并發連接

iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp  --dport 22 -m connlimit ! --connlimit-above 3 -j DROP

4.新建自定義鏈 ,開放80

iptables -F
iptables -A INPUT -d 192.168.42.153 -p tcp --dport 22 -j ACCEPT
iptables -A  OUTPUT -s  192.168.42.153  -p tcp  --sport  22 -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -N webinput
iptables -N weboutput
iptables -I webinput -d 192.168.42.153 -p tcp --dport 80 -j ACCEPT 
iptables -I weboutput -s 192.168.42.153 -p tcp --sport 80 -j ACCEPT
iptables -A INPUT -p tcp -j webinput 
iptables -A OUTPUT -p tcp -j weboutput

“`

5.NAT:(工作在網絡和傳輸層) 過載技術

Basic NAT:靜態NAT
    一個內部主機,分配一個外網地址
NAPT:動態NAT,網絡地址端口轉換;net會話表
    源地址轉換:SNAT 用于內網主機訪問互聯網
    目標地址轉換:DNAT 讓互聯網上主機訪問本地內網上的某服務器上的服務(發布)


6.iptables基于SNAT和DNAT這兩個目標(target)實現地址轉換技術

-j SNT --to-source SIP
    規則添加:POSTROUTING鏈
-j MASQUERADE  動態獲取(外網地址是動態的)
-j DNAT --to-destination DIP{:PORT}
    支持端口映射

iptables練習

iptables練習



7.源地址轉換示例

iptables練習

// 此時 filter 表上的 FORWARD 鏈 要打開(會經過此表中的此鏈)
[root@nat ~]# iptables -t nat -F
[root@nat ~]# iptables -t nat -L -n
[root@nat ~]# iptables -t nat -A POSTROUTING -s 10.1.249.158 -j SNAT --to-source 192.168.2.3
[root@nat ~]# iptables -t nat -L -n
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
SNAT       all  --  10.1.249.158         0.0.0.0/0           to:192.168.2.3 

內部主機訪問外部主機
[root@nei ~]# curl http://192.168.2.4/index.html
hello 
查看外部主機中web的訪問日志
[root@wai ~]# tail /var/log/httpd/access_log
192.168.2.3 - - [13/Oct/2016:21:40:58 +0800]
192.168.2.3 - - [13/Oct/2016:21:40:59 +0800]
//主機,我們的內部主機IP為10.1.249.158
//而httpd的訪問日志為192.168.2.3的訪問
//說明成功實現了源地址轉換


8.目標地址轉換示例(對80端口的轉換)

iptables練習

[root@luyou ~]# iptables -t filter -F 
[root@luyou ~]# iptables -t nat -F
[root@luyou ~]# iptables -t nat -A PREROUTING -d 10.1.249.125 -p tcp --dport 80 -j DNAT --to-destination 192.168.2.4
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  0.0.0.0/0            10.1.249.125        tcp dpt:80 to:192.168.2.4

[root@luyou ~]# netstat -tln | grep "\<80\>"  此時本機上并沒有開放80端口
[root@wai ~]# curl http://10.1.249.125
hello  --> 此時我們訪問為 luyou 主機上的80端口  由上面可知,此服務器上并沒有開放80,而是將請求送往 后端服務器


9.目標地址和端口轉換示例(對22端口的轉換)

[root@luyou ~]# iptables -t filter -F  FORWARD
[root@luyou ~]# iptables -t nat -A PREROUTING -d 10.1.249.125 -p tcp --dport 22022 -j DNAT --to-destination 192.168.2.4:22
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  0.0.0.0/0            10.1.249.125        tcp dpt:22022 to:192.168.2.4:22 
[root@wai ~]# ssh -p 22022 10.1.249.125  --> 連接 luyou 設備的22022端口
[root@nei ~]# --> 連接到 nei  內網的主機上來
[root@luyou ~]# iptables -t nat -L -n -v
Chain PREROUTING (policy ACCEPT 42 packets, 4633 bytes)
 pkts bytes target     prot opt in     out     source               destination         --> 有報文成功匹配到
    1    60 DNAT       tcp  --  *      *       0.0.0.0/0            10.1.249.125        tcp dpt:22022 to:192.168.2.4:22 

//此時我們請求luyou這臺NAT服務器的22022端口的ssh服務,此時我們nat路由器的ssh服務并沒有監聽在22022端口,
//而是將目標IP和目標端口都進行了轉換了,使我們連接到了內網的ssh服務器上

 

原創文章,作者:renjin,如若轉載,請注明出處:http://www.www58058.com/78787

(0)
renjinrenjin
上一篇 2015-03-13
下一篇 2015-03-16

相關推薦

  • 集中練習6-bash腳本

    集中練習6-bash腳本

    Linux干貨 2017-12-05
  • linux的tty

    原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://jeffyyko.blog.51cto.com/28563/140012 最近做了一個小測試,得到了以下結論 測試linux發行版本:rhel AS4.0 環境:VMware 5.0 目的:修改 vi /etc/initt…

    Linux干貨 2015-03-26
  • 馬哥教育網絡21期+第6周博客練習

    請詳細總結vim編輯器的使用并完成以下練習題 1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#; :%s@\(^[[:space:]]\+\)@#&@g # Let rhgb know that we&#0…

    Linux干貨 2016-08-15
  • 如何在微軟云上搭建mysql主從

    大家好: 今天我分享下在生產環境的微軟云服務器上如何搭建mysql主從: 環境如下圖: 我把cnux06-testing設為Mysql主服務器,暫時沒需求的cnux04-web1db2設為Mysql從服務器 1–首先要在master上開啟binlog日志功能并使主從庫中的 server-id保持不同 操作步驟如下: A: 先查看mysql的主目錄…

    Linux干貨 2017-02-14
  • 創建yum源及httpd源碼編譯

    創建yum源及源碼編譯httpd yum本身相比于rpm來說,能夠將有依賴的包文件一次性的安裝完成,是相當的方便的。 yum的服務器支持的幾種格式: http、https、ftp、file 1、yum基礎命令 1、yum命令 yum [options] [command] [package …] [options]: 基本不用 [command]: re…

    Linux干貨 2017-08-08
  • N26-肉肉-第二周作業

    1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。 常用文件管理命令有cp,mv,rm  cp :copy        -i:交互式復制,覆蓋之前提醒用戶確認;         &n…

    Linux干貨 2017-01-03
欧美性久久久久