Xây dựng Linux router và proxy với iptables + squid Linux router là một hệ thống đứng giữa Internal network và Internet hoặc phân cách một network và một network khác.. Linux router là m
Trang 1Xây dựng Linux router và proxy với iptables + squid
Linux router là một hệ thống đứng giữa Internal network và Internet hoặc phân cách một network và một network khác Linux router là một firewall được xây dựng bằng Netfilter/iptables, luôn có sẵn trên hầu hết các bản phân phối của Linux
Nếu bạn là một người dùng bình thường truy cập internet thông qua modem thông thường, bạn không cần thiết phải có một firewall vì IP của bạn thường xuyên thay đổi, bạn không sử dụng một dịch vụ nào đặc biệt cần được bảo vệ,
và hạn chế bởi một tường lửa chuyên dụng
Đối với môi trường doanh nghiệp, việc triển khai nhiều hệ thống, dịch vụ cần
sự bảo mật, và tối ưu trong quản lí, cả mạng Lan và các truy xuất từ bên ngoài vào, tường lửa là một nhu cầu thiết yếu và tối cần thiết Các sản phẩm router phần cứng thường rất mắc và cần sự hỗ trợ tương đối từ phía cung cấp, trong khi việc xây dựng riêng một Linux firewall là việc không quá khó, tốn ít tài nguyên + tiền bạc, mà còn có thể xây dựng một hệ thống all in one tùy theo nhu cầu và mục đích, quản lí chặt chẽ và quan trọng là khả năng bền bỉ mà bản thân Linux mang lại
Bài viết này sẽ cố gắng đưa ra một mô hình cụ thể, cách cài đặt và vận hành một Linux Firewall, transparent proxy, DHCP server trên một máy chủ Linux
Mô hình thực hiện với máy chủ có cấu hình : Pentium Dual Core 2.8 GB, 1GB Ram và 80GB hard disk, sử dụng centos 5.3 enterprise với các gói cài đặt chuẩn, không có giao diện đồ họa
Trước tiên hãy mô tả một chút về mô hình network mà ta cố gắng xây dựng: Một công ty sử dụng đường truyền cáp quang từ FPT, với đường truyền này công ty được cung cấp một đia chỉ IP tĩnh 111.111.111.111, đường truyền đi qua một router, sau đó đi vào mạng bên trong Nhu cầu của công ty cần một vài dịch vụ web, mail, ftp, vpn và chia cách, quản lí truy cập từ bên ngoài vào LAN bằng một firewall, proxy
1 Kiểm sóat luồng truy cập từ bên ngoài vào mạng LAN
2 Cấm các truy cập bất hợp lệ từ LAN ra internet
Trang 23 Cấm sử dụng các dịch vụ chat (Instant message) từ LAN
4 Xây dựng một DHCP server tự động cung cấp IP cho mạng LAN
Mô hình mạng được miêu tả như hình
Cách thức họat động: Linux router có 2 netwok cards:
eth0 là địa chỉ mặt ngoài tiếp xúc với Modem có địa chỉ là 192.168.1.2
eth1 là địa chỉ mặt trong tiếp xúc với LAN có địa chỉ là 172.16.0.1
Trên Router ta triển khai DHCP server cung cấp IP cho toàn bộ mạng LAN
Cũng trên server này ta xây dựng router và squid proxy để thực hiện các yêu
cầu đưa ra
1 DHCP server: Đây là dịch vụ đơn giản và dễ dàng nhất trong bài viết
này Gói cài đặt được cung cấp sẵn + một vài thay đổi nhỏ trong cấu hình
là ta đã cấp được IP cho toàn bộ mạng LAN
Trang 3Dùng yum để download và cài đặt dhcpd:
view plaincopy to clipboardprint?
1 [hungnv@home ~]$ sudo yum
2 install dpcp dhcp-devel
3 [sudo] password for hungnv:
[hungnv@home ~]$ sudo yum
install dpcp dhcp-devel
[sudo] password for hungnv:
Mở file /etc/dhcpd.conf, xóa trắng nếu có nội dung và thêm vào tương tự:
view plaincopy to clipboardprint?
1
2 [hungnv@home ~]$ vim /etc/dhcpd.conf
3
4 #
5 # DHCP Server Configuration file
6 # see /usr/share/doc/dhcp*/dhcpd.conf.sample
7 #
8 ddns-update-style none;
9 deny bootp;
10 authoritstive;
11 subnet 172.16.0.0 netmask 255.255.0.0
12 {
13 option subnet-mask 255.255.0.0;
14 option domain-name "opensource.com.vn";
15 option routers 172.16.0.1;
16 option
domain-name-servers ns1.opensource.com.vn, ns2.opensource.com.vn, dns.fpt.vn;
17 #option netbios-name-servers 192.168.0.2;
18 range dynamic-bootp 172.16.0.20 172.16.0.120;
19 default-lease-time 31200;
20 max-lease-time 62400;
21 }
22
Trang 4
[hungnv@home ~]$ vim /etc/dhcpd.conf
#
# DHCP Server Configuration file
# see /usr/share/doc/dhcp*/dhcpd.conf.sample
#
ddns-update-style none;
deny bootp;
authoritstive;
subnet 172.16.0.0 netmask 255.255.0.0
{
option subnet-mask 255.255.0.0;
option domain-name "opensource.com.vn";
option routers 172.16.0.1;
option domain-name-servers ns1.opensource.com.vn, ns2.opensource.com.vn, dns.fpt.vn;
#option netbios-name-servers 192.168.0.2;
range dynamic-bootp 172.16.0.20 172.16.0.120;
default-lease-time 31200;
max-lease-time 62400;
}
Khởi động dhcp server
view plaincopy to clipboardprint?
1
2 [root @home ~]#
3 service dhcpd restart
4 Shutting down dhcpd: [ OK ]
5 Starting dhcpd: [ OK ]
6
[root @home ~]#
service dhcpd restart
Trang 5Shutting down dhcpd: [ OK ]
Starting dhcpd: [ OK ]
Ở client, dùng dhclient để lấy IP:
view plaincopy to clipboardprint?
1
2 [user@client~]# dhclient eth0
3
[user@client~]# dhclient eth0
Sau đó dùng ifconfig để xem IP hiện tại của client này
Squid như là một transparent proxy:
Cài đặt:
view plaincopy to clipboardprint?
1
2 #yum install squid
3
#yum install squid
Mở file /etc/squid/squid.conf
Thêm vào nội dung bên dưới
view plaincopy to clipboardprint?
Trang 61
2 acl manager proto cache_object
3 acl localhost src 127.0.0.1/32
4 acl localnet src 172.16.0.0/16
5 acl all src 0/0
6 http_port 172.16.0.1:1234
7 # Define safe ports to be allowed by transparent proxy
8 acl SSL_ports port 443 563
9 acl Safe_ports port 25 #smtp server
10 acl Safe_ports port 143 #Imap mail server
11 acl Safe_ports port 6789 #ssh mapped port
12 acl Safe_ports port 80 # http
13 acl Safe_ports port 21 # ftp
14 acl Safe_ports port 443 # https
15 acl Safe_ports port 1025-65535 # unregistered ports
16 acl Safe_ports port 6222 #Jabber server
17 acl Safe_ports port 993 #imap over ssl
18 acl Safe_ports port 7025 #Local mail delivery
19 acl Safe_ports port 7036 #Mysql local
20 acl Safe_ports port 7071 #Zimbra admin cosole
21
22 # Deny yahoo messsenger
23 # Yahoo! Messenger
24 acl ym dstdomain yahoo.com
25 acl ym dstdomain us.il.yimg.com msg.yahoo.com pager.yahoo.c
om
26 acl ym dstdomain rareedge.com ytunnelpro.com chat.yahoo.com
27 acl ym dstdomain voice.yahoo.com address.yahoo.com
28
29 acl ymregex url_regex yupdater.yim ymsgr myspaceim
30
31 #Other protocols Yahoo!Messenger uses ??
32 acl ym dstdomain skype.com imvu.com
33
34 http_access deny ym
35 http_access deny ymregex
36
37 acl CONNECT method CONNECT
38 http_access allow localnet
Trang 739 http_access allow manager localhost
40 http_access allow proxypass
41 http_access deny manager
42 http_access deny !Safe_ports
43 http_access deny CONNECT !SSL_ports
44
45 http_access deny all
46
47 icp_access deny all
48 hierarchy_stoplist cgi-bin ?
49 cache_mem 256 MB
50 cache_dir ufs /var/spool/squid 2048 16 256
51 cache_mgr hungnv@opensource.com.vn
52 cache_effective_user squid
53 cache_effective_group squid
54
55 access_log /var/log/squid/access.log squid
56
57 refresh_pattern ^ftp: 1440 20% 10080
58 refresh_pattern ^gopher: 1440 0% 1440
59 refresh_pattern (cgi-bin|\?) 0 0% 0
60 refresh_pattern 0 20% 4320
61
62 visible_hostname opensource.com.vn
63
64 icp_port 3130
65
66 always_direct allow all
67
68 forwarded_for off
69
70 coredump_dir /var/spool/squid
71
acl manager proto cache_object
acl localhost src 127.0.0.1/32
acl localnet src 172.16.0.0/16
acl all src 0/0
http_port 172.16.0.1:1234
Trang 8# Define safe ports to be allowed by transparent proxy
acl SSL_ports port 443 563
acl Safe_ports port 25 #smtp server
acl Safe_ports port 143 #Imap mail server
acl Safe_ports port 6789 #ssh mapped port
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 6222 #Jabber server
acl Safe_ports port 993 #imap over ssl
acl Safe_ports port 7025 #Local mail delivery
acl Safe_ports port 7036 #Mysql local
acl Safe_ports port 7071 #Zimbra admin cosole
# Deny yahoo messsenger
# Yahoo! Messenger
acl ym dstdomain yahoo.com
acl ym dstdomain us.il.yimg.com msg.yahoo.com pager.yahoo.com acl ym dstdomain rareedge.com ytunnelpro.com chat.yahoo.com acl ym dstdomain voice.yahoo.com address.yahoo.com
acl ymregex url_regex yupdater.yim ymsgr myspaceim
#Other protocols Yahoo!Messenger uses ??
acl ym dstdomain skype.com imvu.com
http_access deny ym
http_access deny ymregex
acl CONNECT method CONNECT
http_access allow localnet
http_access allow manager localhost
http_access allow proxypass
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access deny all
Trang 9
icp_access deny all
hierarchy_stoplist cgi-bin ?
cache_mem 256 MB
cache_dir ufs /var/spool/squid 2048 16 256
cache_mgr hungnv@opensource.com.vn
cache_effective_user squid
cache_effective_group squid
access_log /var/log/squid/access.log squid
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern (cgi-bin|\?) 0 0% 0
refresh_pattern 0 20% 4320
visible_hostname opensource.com.vn
icp_port 3130
always_direct allow all
forwarded_for off
coredump_dir /var/spool/squid
Trong đoạn cấu hình trên, ta đã cấu hình cho squid chặn các dịch vụ IM như yahoo và skype, danh sách các protocol bị chặn các bạn có thể xem ở:
http://wiki.squid-cache.org/ConfigExamples/ phần instant message
Tiếp theo là phần cài đặt router Nếu ta đã biết rõ được nhu cầu của mình cần
gì và có được sơ đồ những dịch vụ cần thiết, việc thiết lập router/firewall là không hề khó Iptables là dịch vụ có sẵn trên centos, cho nên ta không cần phải cài đặt
view plaincopy to clipboardprint?
1 #!/bin/sh
2 # Script makes a linux box acts as an Linux Router
Trang 103 # 22-10-2009
4 # By hungnv@opensource.com.vn and some others from the internet :)
5 #
6 echo -e "\n\n\n Installing iptables script "
7 # Name of Internal Interface
8 LANIF="eth1"
9 # Enter Lan Network
10 IPLAN="172.16.0.0/16"
11 # ipaddress of internal interface
12 LANIP="172.16.0.1/16"
13 # Name of External Interface
14 WANIF="eth0"
15 # ipaddress of external interface
16 EXTIP="192.168.1.2"
17 echo "Preparing "
18 /sbin/depmod -a
19 /sbin/modprobe ip_tables
20 /sbin/modprobe ip_conntrack
21 /sbin/modprobe ip_conntrack_ftp
22 /sbin/modprobe ip_conntrack_irc
23 /sbin/modprobe iptable_nat
24 /sbin/modprobe ip_nat_ftp
25 /sbin/modprobe ip_nat_irc
26
27 echo " Enabling IP forwarding "
28 echo "1" > /proc/sys/net/ipv4/ip_forward
29 echo "1" > /proc/sys/net/ipv4/ip_dynaddr
30
31 echo " External interface: $EXTIF"
32 echo " External interface IP address is: $EXTIP"
33 echo " Loading firewall server rules "
34 ALL="0.0.0.0/0"
35 # Set default policy to DROP
36 iptables -P INPUT DROP
37 iptables -F INPUT
38 iptables -P OUTPUT DROP
39 iptables -F OUTPUT
40 iptables -P FORWARD DROP
41 iptables -F FORWARD
42 iptables -F -t nat
Trang 1143 # Flush the user chain if it exists
44 if [ "`iptables -L | grep drop-and-log-it`" ]; then
45 iptables -F drop-and-log-it
46 fi
47 # Delete all User-specified chains
48 iptables -X
49
50 # Reset all IPTABLES counters
51 iptables -Z
52
53 # Creating a DROP chain
54 iptables -N drop-and-log-it
55 iptables -A drop-and-log-it -j LOG log-level info
56 iptables -A drop-and-log-it -j REJECT
57 echo -e " - Loading INPUT rulesets"
58
59 # loopback interfaces are valid
60 iptables -A INPUT -i lo -s $ALL -d $ALL -j ACCEPT
61
62 # local interface, local machines, going anywhere is valid
63 iptables -A INPUT -i $INTIF -s $INTNET -d $ALL -j ACCEPT
64 # remote interface, claiming to be local machines, IP spoofing, get lost
65 iptables -A INPUT -i $EXTIF -s $INTNET -d $ALL -j drop-and-log-it
66 # remote interface, any source, going to permanent PPP address is valid
67 iptables -A INPUT -i $EXTIF -s $ALL -d $EXTIP -j ACCEPT
68 # Allow any related traffic coming back to the MASQ server in
69 iptables -A INPUT -i $EXTIF -s $ALL -d $EXTIP -m state state ESTABLISHED,RELATED -j ACCEPT
70
71 # Catch all rule, all other incoming is denied and logged
72 iptables -A INPUT -s $ALL -d $ALL -j drop-and-log-it
73 echo -e " - Loading OUTPUT rulesets"
74 #####################################################
##################
75 # OUTPUT: Outgoing traffic from various interfaces All rulesets are
76 # already flushed and set to a default policy of DROP
Trang 1277 #
78 # loopback interface is valid
79 iptables -A OUTPUT -o lo -s $ALL -d $ALL -j ACCEPT
80
81 # local interfaces, any source going to local net is valid
82 iptables A OUTPUT o $INTIF s $EXTIP d $INTNET
-j ACCEPT
83
84 # local interface, any source going to local net is valid
85 iptables A OUTPUT o $INTIF s $INTIP d $INTNET
-j ACCEPT
86 # outgoing to local net on remote interface, stuffed routing, deny
87 iptables -A OUTPUT -o $EXTIF -s $ALL -d $INTNET -j drop-and-log-it
88 # anything else outgoing on remote interface is valid
89 iptables A OUTPUT o $EXTIF s $EXTIP d $ALL
-j ACCEPT
90 # Catch all rule, all other outgoing is denied and logged
91 iptables -A OUTPUT -s $ALL -d $ALL -j drop-and-log-it
92 echo -e " - Loading FORWARD rulesets"
93 iptables -A FORWARD -i $EXTIF -o $INTIF -m state
state ESTABLISHED,RELATED -j ACCEPT
94 iptables -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT
95
96 # Catch all rule, all other forwarding is denied and logged
97 iptables -A FORWARD -j drop-and-log-it
98
99 # Enable SNAT (MASQUERADE) functionality on $EXTIF
100 iptables -t nat -A POSTROUTING -o $EXTIF -j SNAT
to $EXTIP
101
102 #####################################################
##################
103 # For Squid Server
104 ###
105 #
106 INTIP="172.16.0.1"
107 iptables -t nat -A PREROUTING -i $INTIF protocol tcp
dport 80 -j DNAT to $INTIP:$SQUID_PORT
108 # if it is same system
Trang 13109 iptables -t nat -A PREROUTING -i $EXTIF protocol tcp dport 80 -j REDIRECT to-port $SQUID_PORT
110
111 echo -e " Setting up firewall complete\n\n"
112
#!/bin/sh
# Script makes a linux box acts as an Linux Router
# 22-10-2009
# By hungnv@opensource.com.vn and some others from the internet :)
#
echo -e "\n\n\n Installing iptables script "
# Name of Internal Interface
LANIF="eth1"
# Enter Lan Network
IPLAN="172.16.0.0/16"
# ipaddress of internal interface
LANIP="172.16.0.1/16"
# Name of External Interface
WANIF="eth0"
# ipaddress of external interface
EXTIP="192.168.1.2"
echo "Preparing "
/sbin/depmod -a
/sbin/modprobe ip_tables
/sbin/modprobe ip_conntrack
/sbin/modprobe ip_conntrack_ftp
/sbin/modprobe ip_conntrack_irc
/sbin/modprobe iptable_nat
/sbin/modprobe ip_nat_ftp
/sbin/modprobe ip_nat_irc
echo " Enabling IP forwarding "
echo "1" > /proc/sys/net/ipv4/ip_forward
echo "1" > /proc/sys/net/ipv4/ip_dynaddr
echo " External interface: $EXTIF"
echo " External interface IP address is: $EXTIP"
echo " Loading firewall server rules "
ALL="0.0.0.0/0"