- Cấm các truy cập bất hợp lệ từ LAN ra internet
- Cấm sử dụng các dịch vụ chat (Instant message) từ LAN.
- 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.
- 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.
Dùng yum để download và cài đặt dhcpd:
- [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ự:
- [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
- [root @home ~]#
- service dhcpd restart
- Shutting down dhcpd: [ OK ]
- Starting dhcpd: [ OK ]
Ở client, dùng dhclient để lấy IP:
- [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:
- #yum install squid
Mở file /etc/squid/squid.conf
Thêm vào nội dung bên dưới
- 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
- # 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
- 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.
- #!/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"
- # Set default policy to DROP
- iptables -P INPUT DROP
- iptables -F INPUT
- iptables -P OUTPUT DROP
- iptables -F OUTPUT
- iptables -P FORWARD DROP
- iptables -F FORWARD
- iptables -F -t nat
- # Flush the user chain.. if it exists
- if [ "`iptables -L | grep drop-and-log-it`" ]; then
- iptables -F drop-and-log-it
- fi
- # Delete all User-specified chains
- iptables -X
- # Reset all IPTABLES counters
- iptables -Z
- # Creating a DROP chain
- iptables -N drop-and-log-it
- iptables -A drop-and-log-it -j LOG --log-level info
- iptables -A drop-and-log-it -j REJECT
- echo -e " - Loading INPUT rulesets"
- # loopback interfaces are valid.
- iptables -A INPUT -i lo -s $ALL -d $ALL -j ACCEPT
- # local interface, local machines, going anywhere is valid
- iptables -A INPUT -i $INTIF -s $INTNET -d $ALL -j ACCEPT
- # remote interface, claiming to be local machines, IP spoofing, get lost
- iptables -A INPUT -i $EXTIF -s $INTNET -d $ALL -j drop-and-log-it
- # remote interface, any source, going to permanent PPP address is valid
- iptables -A INPUT -i $EXTIF -s $ALL -d $EXTIP -j ACCEPT
- # Allow any related traffic coming back to the MASQ server in
- iptables -A INPUT -i $EXTIF -s $ALL -d $EXTIP -m state --state ESTABLISHED,RELATED -j ACCEPT
- # Catch all rule, all other incoming is denied and logged.
- iptables -A INPUT -s $ALL -d $ALL -j drop-and-log-it
- echo -e " - Loading OUTPUT rulesets"
- #######################################################################
- # OUTPUT: Outgoing traffic from various interfaces. All rulesets are
- # already flushed and set to a default policy of DROP.
- #
- # loopback interface is valid.
- iptables -A OUTPUT -o lo -s $ALL -d $ALL -j ACCEPT
- # local interfaces, any source going to local net is valid
- iptables -A OUTPUT -o $INTIF -s $EXTIP -d $INTNET -j ACCEPT
- # local interface, any source going to local net is valid
- iptables -A OUTPUT -o $INTIF -s $INTIP -d $INTNET -j ACCEPT
- # outgoing to local net on remote interface, stuffed routing, deny
- iptables -A OUTPUT -o $EXTIF -s $ALL -d $INTNET -j drop-and-log-it
- # anything else outgoing on remote interface is valid
- iptables -A OUTPUT -o $EXTIF -s $EXTIP -d $ALL -j ACCEPT
- # Catch all rule, all other outgoing is denied and logged.
- iptables -A OUTPUT -s $ALL -d $ALL -j drop-and-log-it
- echo -e " - Loading FORWARD rulesets"
- iptables -A FORWARD -i $EXTIF -o $INTIF -m state --state ESTABLISHED,RELATED -j ACCEPT
- iptables -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT
- # Catch all rule, all other forwarding is denied and logged.
- iptables -A FORWARD -j drop-and-log-it
- # Enable SNAT (MASQUERADE) functionality on $EXTIF
- iptables -t nat -A POSTROUTING -o $EXTIF -j SNAT --to $EXTIP
- #######################################################################
- # For Squid Server
- ###
- #
- INTIP="172.16.0.1"
- iptables -t nat -A PREROUTING -i $INTIF --protocol tcp --dport 80 -j DNAT --to $INTIP:$SQUID_PORT
- # if it is same system
- iptables -t nat -A PREROUTING -i $EXTIF --protocol tcp --dport 80 -j REDIRECT --to-port $SQUID_PORT
- echo -e " Setting up firewall complete\n\n"
Lưu script trên với tên tùy ý, cho tùy chọn execute ( +x ) và đặt vào /etc/rc.local để script chạy lúc khởi động,
Lúc này bạn test thử truy cập internet và thêm vào các phần khác theo nhu cầu riêng của mình.
Xin lưu ý rằng các script cũng như lệnh nói đến trong bài viết này chỉ nhằm mục đích tạo ra một dhcp server, proxy và firewall “có thể” làm việc, không hề có bất cứ tính năng gì kèm theo. Muốn đầy đủ, phải hiểu được nhu cầu cụ thể của từng môi trường, như tiêu chí của bài viết nói đến.
Không có nhận xét nào:
Đăng nhận xét