Thứ Ba, 20 tháng 9, 2011

Xây dựng Linux router và proxy với iptables + squid


  1. Cấm các truy cập bất hợp lệ từ LAN ra internet
  2. Cấm sử dụng các dịch vụ chat (Instant message) từ LAN.
  3. 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.
alt
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.
Dùng yum để download và cài đặt dhcpd:
  1. [hungnv@home ~]$ sudo yum  
  2. install dpcp dhcp-devel   
  3. [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ự:

  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.    
 

Khởi động dhcp server
  1.    
  2. [root @home ~]#  
  3. service dhcpd restart  
  4. Shutting down dhcpd: [ OK ]  
  5. Starting dhcpd: [ OK ]  
  6.    

Ở client, dùng dhclient để lấy IP:
  1.    
  2.  [user@client~]# dhclient eth0  
  3.    

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:
  1.    
  2. #yum install squid  
  3.    

Mở file /etc/squid/squid.conf
Thêm vào nội dung bên dưới
  1.    
  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.com  
  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  
  39. 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.    

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.
  1.  #!/bin/sh  
  2. # Script makes a linux box acts as an Linux Router.  
  3. # 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  
  43. # 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.  
  77. #  
  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  
  109. 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.    

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