Skip to content

Bài Lab Về Iptables#

Yêu cầu bài lab#

Môi trường bài lab#

  • Sử dụng hệ điều hành Centos 7
  • Sử dụng firewall iptables
  • Thực hành bài lab theo mô hình sau:

Mô hình bài lab

Yêu cầu#

  • PC2 chỉ có thể liên hệ với PC1 trên port 22.
  • PC1 ping được tới địa chỉ của ServerDC.
  • ServerDC có thể kết nối với PC1 qua port 22 thông qua ip public của Server IPtables(10.33.33.30) bằng cơ chế dnat.

2. Thực hiện bài lab#

Cấu hình trên PC1#

Kiểm tra trạng thái, và xóa hết các rule cũ:

[root@pc1 ~]# systemctl status iptables
iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
   Active: active (exited) since T3 2019-07-09 23:51:03 EDT; 1s ago
  Process: 1951 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
 Main PID: 1951 (code=exited, status=0/SUCCESS)

Th07 09 23:51:03 pc1 systemd[1]: Starting IPv4 firewall with iptables...
Th07 09 23:51:03 pc1 iptables.init[1951]: iptables: Applying firewall rules: [  OK  ]
Th07 09 23:51:03 pc1 systemd[1]: Started IPv4 firewall with iptables.
[root@pc1 ~]# iptables -F
[root@pc1 ~]# iptables  -L
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      

Tạo rule cho phép PC2 kết nối đến PC1 qua cổng 22 tcp, và tạo rule chặn tất cả những kết nối khác:

[root@pc1 ~]# iptables -t filter -A INPUT -p tcp --dport 22 -s 10.11.11.20 -j ACCEPT
[root@pc1 ~]# iptables -t filter -A INPUT -s 10.11.11.20 -j REJECT 
[root@pc1 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  10.11.11.20          anywhere             tcp dpt:ssh
REJECT     all  --  10.11.11.20          anywhere             reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination        

Test trên PC2, ping đến PC1 bị từ chối và nhận thông báo Destination Port Unreachable, nhưng vẫn có thể ssh qua port 22

Lưu cấu hình iptables trên PC1

[root@pc1 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

Cấu hình trên Server IPtables#

Kiểm tra trạng thái iptables, cấu hình xóa rule iptables

[root@iptables ~]# systemctl status   iptables
 iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
   Active: active (exited) since T4 2019-07-10 06:31:34 EDT; 1h 6min ago
  Process: 445 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
 Main PID: 445 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/iptables.service

Th07 10 06:31:33 iptables systemd[1]: Starting IPv4 firewall with iptables...
Th07 10 06:31:34 iptables iptables.init[445]: iptables: Applying firewall rules: [  OK  ]
Th07 10 06:31:34 iptables systemd[1]: Started IPv4 firewall with iptables.
[root@iptables ~]# iptables -F
[root@iptables ~]# iptables -L
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         

Mặc định, chuyển tiếp IP Ipv4 trên Centos 7 bị tắt, để bật chức năng này, tiến hành chỉnh sửa file /etc/sysctl.conf. Thêm dòng sau:

net.ipv4.ip_forward = 1

Sau đó chạy lệnh sau để áp dụng cấu hình:

sysctl -p /etc/sysctl.conf

Kiểm tra ping từ Server DC đến PC1 thành công và ngược lại:

Cấu hình Nat port#

Tiến hành cấu hình Nat port 22 của PC 1 đến Port 22 của Server Iptable

Cấu hình trên server Iptable

Kiểm tra rule trên table nat:

[root@iptables ~]# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
[root@iptables ~]# 

Cấu hình rule trên table nat, chain PREROUTING thực hiện destination nat các kết nối tcp đến port 22 của Địa chỉ ip 10.33.33.30 (Địa chỉ ip public của server iptables) đến địa chỉ port 22 của địa chỉ ip 10.22.22.10 ( địa chỉ ip của PC1):

[root@iptables ~]# iptables -t nat -A PREROUTING -d 10.33.33.30 -p tcp --dport 22 -j DNAT --to 10.22.22.10:22
[root@iptables ~]# iptables -L -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  anywhere             iptables             tcp dpt:ssh to:10.22.22.10:22

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination       
[root@iptables ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

Trên Server DC, ssh đến địa chỉ ip 10.33.33.30 thì có thể kết nối tới PC1

Kết thúc bài lab