คลังบทความของบล็อก

วันอังคารที่ 20 พฤษภาคม พ.ศ. 2551

squid

Proxy (a2)

Q:อยากทราบ วิธีทำ transparent Proxy และการกำหนด ขนาดของ bandwidth เวลา download

A:เป็นคำตอบที่ค่อนข้างยาวสักนิดหนึ่งนะครับ แต่เพื่อความ clear จะอธิบายแบบค่อนข้างละเอียดเลย เพื่อ จะได้นำไปใช้งานได้ทันทีครับ ดังนั้นคำถามอื่นที่ไม่ได้ลง เล่มนี้ต้องขอยกไปเล่มหน้าแล้วกันนะครับ : )

การทำ Transparent Proxy นั้นมันจะต้องใช้ทางผ่านของข้อมูล หรือ Gateway ในการกำหนด protocol ชนิดของข้อมูล ไปในทิศทางที่ต้องการจะให้ไป (redirect port) ดังนั้น คุณจะต้องทราบ เสียก่อน ว่า Gateway ที่คุณกำลังใช้อยู่นั้น support ในการ ทำ redirect หรือไม่ ถ้าไม่ก็จัดแจงหาโปรแกรมจำพวก redirect port มาติดตั้งไว้ที่ Gateway เสียก่อน ในหนังสือเล่มนี้จะเป็นเรื่องของ Linux ดังนั้น การทำ Transparent proxy บน OS อื่นจะไม่กล่าวถึงนะครับ

ก่อนอื่นต้องเตรียมเครื่องสำหรับติดตั้ง Linux เพื่อเป็น Gateway ก่อน ส่วนจะลง Linux ตัวไหนก็ตามสบายครับ แต่ขอให้ Kernel เป็น 2.4 ขึ้นไป เพื่อจะใช้ Feature ของ iptables ที่สามารถทำ Redirect port ได้ด้วยตัวของมันเองได้เลย เมื่อเตรียมเครื่องสำหรับทำ Gateway เรียบร้อยแล้ว ก็ทำตาม Step ต่อไปนี้ได้เลยครับ

Step 1 : Compile Kernel ให้ Support Netfilter (iptables)

ก่อนอื่นก็จะต้อง Compile ให้ Kernel support iptables เสียก่อน โดย เข้าไปที่ “/usr/src/linux” และเรียก “make menuconfig” เลื่อน hi-light มาที่ menu “Networking options — >” แล้วกด Enter ก็จะเข้ามาสู่ sub menu ของ Networking options เลื่อน hi-light มาที่ “[ ] Network packet filtering (replaces ipchains)” ทำการเคาะ space bar ให้มี “[*]” ขึ้นเพื่อเป็นการยืนยันขอกว่าจะใช้ iptables กับ kernel นี้ เมื่อ mark เสร็จจะมี sub menu ขึ้นมาอีกอันคือ “IP : Netfilter Configuration — >” ให้เลื่อน hi-light มาที่ sub menu นี้แล้วกด Enter เพื่อเข้าสู่ sub menu นี้ เมื่อเข้ามาแล้วให้ space bar [*] ของทุก menu ย่อย ทั้งหมด ตัวโปรแกรม menuconfig จะมีการเพิ่ม menu มาเอง ให้ enable ให้ครบ หลังจากนั้นให้ออกและ save config ที่พึ่งมีการแก้ไขไป หลังจากนั้นก็เรียกคำสั่ง “make dep” แล้วก็ “make; make install” เพื่อ install kernel ใหม่ พอทำเสร็จพิธีนี้ก็ reboot เครื่องแล้วรอลุ้นว่า kernel จะตกใจหรือไม่ ( kernel panic )

Step 2 : download Proxy program และ install

ผมจะพูดถึง proxy ตัวเก่งที่ใช้กันมานมนานแล้ว คือ squid เจ้าปลาหมึก ส่วน Feature ของเจ้า proxy server ตัวนี้มีเด่นๆ หลาย อย่าง เช่น

• Proxying and caching of HTTP, FTP, and other URL’s

• Proxying for SSL

• Cache hierarchies

• ICP, HTCP, CARP, Cache Digests

• Transparent Caching

• WCCP

• Extensive access controls

• HTTP server acceleration

• SNMP

• Caching of DNS lookups

สามารถ download source ได้ที่ “ http://www.squid-cache.org “ ล่าสุดก็จะเป็น version 2.4 ซึ่งจะ stable สุดตอนนี้ “http://www.squidcache.org/Versions/v2/2.4/squid-2.4.STABLE4-src.tar.gz “ เมื่อ download มาแล้วก็ทำการแตกโปรแกรมด้วยคำสั่ง “ tar xvfz squid-2.4.STABLE4-src.tar.gz “

ต่อจากนี้จะเป็น การ config และ compile โปรแกรม squid proxy server ให้ “cd” เข้าไปใน directory ที่แตก program squid ออกมาแล้วใช้คำสั่งตามนี้

# ./configure -prefix=/squid \

--disable-ident-lookups \

--enable-linux-netfilter \

--enable-delay-pools \

--enable-pool \

--enable-truncate \

- - enable-removal-policies

# make all

# make install

เมื่อทำตามข้างต้นเรียบร้อยแล้ว จะได้โปรแกรม squid proxy server ไว้ที่ “/squid” และ file config จะอยู่ที่ “/squid/etc/squid.conf”

Step 3 :

Config ตัว proxy Server

หลักจริงๆ ของการ Config Transparent proxy ด้วย Squid นั้นจะใช้ iptables ในการ redirect port 80 (http port) เพื่อให้ชี้มาที่ port 8080 ซึ่งเป็น port proxy มาตรฐาน (มาตรฐานคนไทย) เมื่อใช้ iptables ในการชี้มาเสร็จแล้วก็จะต้อง add config นี้ลงไปใน config ของ squid เพื่อให้ squid ยอมรับการทำ redirect port

httpd_accel_host virtual

httpd_accel_port 80

httpd_accel_with_proxy on

httpd_accel_uses_host_header on

Step4 : การกำหนด Bandwidth ในการ download ข้อมูล

การกำหนด Bandwidth ในการ download โปรแกรม หรือการใช้ข้อมูลผ่าน squid นั้นจะใช้ Feature ที่ชื่อว่า Delay Pools ซึ่งตอนแรกที่เราได้ทำการ configure และ install ไปนั้น เราได้ทำไปแล้วซึ่งจะแบ่งการกำหนดออกเป็น 3 ลักษณะดังนี้

แบบแรก : การ กำหนด การ download จาก ตัวอักษรหรือคำ จาก website

acl magic_words url_regx –i .exe

delay_pools 1

delay_class 1 1

delay_parameters 1 4000/4000

delay_access 1 allow magic_words

อธิบายได้ดังนี้

magic_words เป็นตัวแปลบอกว่าถ้ามีนามสกุล .exe (ส่วน –i คือเป็นตัวบอกว่าให้เป็น case-insensitive) ให้ download มาด้วยความเร็วไม่เกิน 32Kbit/sec ก็ประมาณ 4KByte/sec 32Kbit/sec มาจาก 4000 * 8 ถ้าต้องการจะหา ความเร็วที่ต้องการจะให้ download ไม่เกินสามารถ หาได้ด้วยวิธีการคำนวณแบบนี้ สมมติ ให้ต้องการให้ download ไม่เกิน 2KByte/sec คือ (2000*8)/8 = 2000 (จะแสดงวิธีคิดให้ดูทำไมล่ะนี่ ?) ก็คือนำเอาจำนวน KByte/sec ที่ต้องการใส่เข้าไปเลยนะครับ :P~

แบบสอง : กำหนดให้ ขนาด Bandwidth ให้กับคนที่ใช้ภายใน network

acl a_ll src 0.0.0.0/0.0.0.0

delay_pool_count 1

delay_class 1 2

delay_parameters 1 12500/12500 2500/2500

delay_access 1 allow a_ll

อธิบายได้ดังนี้

จากตัวอย่างข้างต้นสมมติว่าคุณมี Lease Line ขนาด 128Kbit/sec และต้องการจะเหลือไว้สักนิดหนึ่งสำหรับ SMTP server ซึ่งเป็น IP จริงไม่ได้ต่อผ่านกับ gateway ตัวนี้ และ ให้ใช้สำหรับ NAT ภายในองค์กร 100Kbit/sec เพื่อให้ทุกคน share กันใช้ เท่าๆ กันและต้องการจะให้แต่ละคนใช้ bandwidth ได้ไม่เกิน 2.5KByte/sec (สังเกตดีๆ นะครับหน่วยไม่เหมือนกัน เพื่อง่ายต่อการเข้าใจควรตั้งใจอ่านทุกตัวอักษรครับ)

แบบสาม : กำหนดขนาด Bandwidth ให้กับ group network

acl a_ll src 0.0.0.0/0.0.0.0

delay_pool_count 1

delay_class 1 3

delay_parameters 1 56000/56000 18750/18750 500/500

delay_access 1 allow all

อธิบายได้ดังนี้

ถ้าคุณมี Lease Line ขนาด 512Kbit/sec และต้องการ เหลือ bandwidth สำหรับ SMTP ไว้ 8KByte/sec ใช้ภายใน network ไม่เกิน 448Kbit/sec และ แบ่งให้แต่ละ class ไม่เกิน 150Kbit/sec และให้แต่ละ IP ไม่เกิน 4kbit/sec (หน่วยไม่ผิดนะครับ ถ้าเป็น KByte เอา 8 หารจะเป็น Kbit ครับ)

เพื่อป้องกันความสับสน ผมจะเอาตัวอย่าง proxy ที่ผมใช้ภายใน Office เป็นตัวอย่างครับ ดังนี้

ตัวอย่าง squid.conf

#/squid/etc/squid.conf

# IP ของเครื่อง gateway ที่ใช้คือ 10.20.1.181

# eth1 10.20.1.181

# กำหนด port เพื่อใช้ในการ connect proxy http_port 8080

# กำหนด user ที่ใช้ในการ control cache squid ห้ามใช้ root เป็นอันขาด (เพราะมันจะใช้งานไม่ได้)

cache_effective_user squid

cache_effective_group squid

# Cache objects no larger than this size maximum_object_size 40960 KB

# Use this amount of memory (1/3rd of total memory).

# Note squid will use MORE than this for other tasks

cache_mem 16 MB

# กำหนดขนาด cache ที่นี้กำหนดให้ขนาด 4GB cache_dir ufs /squid/cache 4000 16 256

# กำหนดการ fake header

forwarded_for off

# What to do when a user aborts a download.

# If the transfer has less than the “quick_abort_min” value, it will continue to download

# the data. If it has more than the “quick_abort_max” value, it will abort the download.

# If more than “quick_abort_pct” percentage of the download has occurred, it won’t abort

# the download either. These values are the default settings.

quick_abort_min 16 KB

quick_abort_max 16 KB

quick_abort_pct 95

# กำหนด คำกับ website ที่ไม่ต้องการให้ cache

acl QUERY urlpath_regex cgi-bin \?

acl website dst 10.20.2.14/32

acl PHP url_regex .php?

no_cache deny website

no_cache deny QUERY

no_cache deny PHP

# Define some access control list (acl) “aliases”

acl Safe_ports port 80 21 443 563 591 70 210 1025-65535

acl manager proto cache_object

acl all src 0.0.0.0/0.0.0.0

acl allowed_hosts src 10.20.0.0/255.255.0.0

acl localhost src 127.0.0.1/255.255.255.255

acl SSL_ports port 443 563

acl CONNECT method CONNECT

# Generate some rules based on the aliases we just made

# This prevents access from those who shouldn’t have it

http_access allow manager localhost

http_access deny manager

http_access deny !Safe_ports

http_access deny CONNECT !SSL_ports

http_access allow allowed_hosts

http_access deny all

# Name to appear on error documents

cache_mgr yourname@yourdomain.com

# Append this domain to non-resolvable websites before giving up

append_domain .uoregon.edu

# Transparent proxy settings. Requires iptabes to be

# set up for transparent proxy as well (see below).

httpd_accel_host virtual

httpd_accel_port 80

httpd_accel_with_proxy on

httpd_accel_uses_host_header on

httpd_accel_single_host off

acl a_ll src 0.0.0.0/0.0.0.0

delay_pool_count 1

delay_class 1 2

delay_parameters 1 12500/12500 2500/2500

delay_access 1 allow a_ll

# หลอก header ไม่ให้ show X-Forward

anonymize_headers deny From Referer Server

anonymize_headers deny User-Agent WWW-Authenticate Link

fake_user_agent Thailand_ChaiYoo/1.0

Step 5 : ทำการ transparent ด้วย iptables

ทำการ redirect port 80 ที่ผ่าน Gateway ทั้งหมดไป ที่ port 8080 เพื่อบังคับให้ทุกคนใช้ proxy ครับ ด้วยคำสั่งดังนี้เลย

# iptables -t nat -A PREROUTING -i eth0 -p tcp —dport 80 -j REDIRECT -- to-port 8080

เมื่อทำตาม ปกติเรียบร้อยแล้ว ก็ทำการทดสอบใช้โดยเรียก IE หรือ mozilla เพื่อ เรียกดู website ส่วนที่ Gateway นั้น ก็ “tail-f /squid/logs/access.log” เพื่อดูการเปลี่ยนแปลงของ log ครับ ทดลองดูกันนะครับ ขอให้โชคดี : )

squid

Proxy (a2)

Q:อยากทราบ วิธีทำ transparent Proxy และการกำหนด ขนาดของ bandwidth เวลา download

A:เป็นคำตอบที่ค่อนข้างยาวสักนิดหนึ่งนะครับ แต่เพื่อความ clear จะอธิบายแบบค่อนข้างละเอียดเลย เพื่อ จะได้นำไปใช้งานได้ทันทีครับ ดังนั้นคำถามอื่นที่ไม่ได้ลง เล่มนี้ต้องขอยกไปเล่มหน้าแล้วกันนะครับ : )

การทำ Transparent Proxy นั้นมันจะต้องใช้ทางผ่านของข้อมูล หรือ Gateway ในการกำหนด protocol ชนิดของข้อมูล ไปในทิศทางที่ต้องการจะให้ไป (redirect port) ดังนั้น คุณจะต้องทราบ เสียก่อน ว่า Gateway ที่คุณกำลังใช้อยู่นั้น support ในการ ทำ redirect หรือไม่ ถ้าไม่ก็จัดแจงหาโปรแกรมจำพวก redirect port มาติดตั้งไว้ที่ Gateway เสียก่อน ในหนังสือเล่มนี้จะเป็นเรื่องของ Linux ดังนั้น การทำ Transparent proxy บน OS อื่นจะไม่กล่าวถึงนะครับ

ก่อนอื่นต้องเตรียมเครื่องสำหรับติดตั้ง Linux เพื่อเป็น Gateway ก่อน ส่วนจะลง Linux ตัวไหนก็ตามสบายครับ แต่ขอให้ Kernel เป็น 2.4 ขึ้นไป เพื่อจะใช้ Feature ของ iptables ที่สามารถทำ Redirect port ได้ด้วยตัวของมันเองได้เลย เมื่อเตรียมเครื่องสำหรับทำ Gateway เรียบร้อยแล้ว ก็ทำตาม Step ต่อไปนี้ได้เลยครับ

Step 1 : Compile Kernel ให้ Support Netfilter (iptables)

ก่อนอื่นก็จะต้อง Compile ให้ Kernel support iptables เสียก่อน โดย เข้าไปที่ “/usr/src/linux” และเรียก “make menuconfig” เลื่อน hi-light มาที่ menu “Networking options — >” แล้วกด Enter ก็จะเข้ามาสู่ sub menu ของ Networking options เลื่อน hi-light มาที่ “[ ] Network packet filtering (replaces ipchains)” ทำการเคาะ space bar ให้มี “[*]” ขึ้นเพื่อเป็นการยืนยันขอกว่าจะใช้ iptables กับ kernel นี้ เมื่อ mark เสร็จจะมี sub menu ขึ้นมาอีกอันคือ “IP : Netfilter Configuration — >” ให้เลื่อน hi-light มาที่ sub menu นี้แล้วกด Enter เพื่อเข้าสู่ sub menu นี้ เมื่อเข้ามาแล้วให้ space bar [*] ของทุก menu ย่อย ทั้งหมด ตัวโปรแกรม menuconfig จะมีการเพิ่ม menu มาเอง ให้ enable ให้ครบ หลังจากนั้นให้ออกและ save config ที่พึ่งมีการแก้ไขไป หลังจากนั้นก็เรียกคำสั่ง “make dep” แล้วก็ “make; make install” เพื่อ install kernel ใหม่ พอทำเสร็จพิธีนี้ก็ reboot เครื่องแล้วรอลุ้นว่า kernel จะตกใจหรือไม่ ( kernel panic )

Step 2 : download Proxy program และ install

ผมจะพูดถึง proxy ตัวเก่งที่ใช้กันมานมนานแล้ว คือ squid เจ้าปลาหมึก ส่วน Feature ของเจ้า proxy server ตัวนี้มีเด่นๆ หลาย อย่าง เช่น

• Proxying and caching of HTTP, FTP, and other URL’s

• Proxying for SSL

• Cache hierarchies

• ICP, HTCP, CARP, Cache Digests

• Transparent Caching

• WCCP

• Extensive access controls

• HTTP server acceleration

• SNMP

• Caching of DNS lookups

สามารถ download source ได้ที่ “ http://www.squid-cache.org “ ล่าสุดก็จะเป็น version 2.4 ซึ่งจะ stable สุดตอนนี้ “http://www.squidcache.org/Versions/v2/2.4/squid-2.4.STABLE4-src.tar.gz “ เมื่อ download มาแล้วก็ทำการแตกโปรแกรมด้วยคำสั่ง “ tar xvfz squid-2.4.STABLE4-src.tar.gz “

ต่อจากนี้จะเป็น การ config และ compile โปรแกรม squid proxy server ให้ “cd” เข้าไปใน directory ที่แตก program squid ออกมาแล้วใช้คำสั่งตามนี้

# ./configure -prefix=/squid \

--disable-ident-lookups \

--enable-linux-netfilter \

--enable-delay-pools \

--enable-pool \

--enable-truncate \

- - enable-removal-policies

# make all

# make install

เมื่อทำตามข้างต้นเรียบร้อยแล้ว จะได้โปรแกรม squid proxy server ไว้ที่ “/squid” และ file config จะอยู่ที่ “/squid/etc/squid.conf”

Step 3 :

Config ตัว proxy Server

หลักจริงๆ ของการ Config Transparent proxy ด้วย Squid นั้นจะใช้ iptables ในการ redirect port 80 (http port) เพื่อให้ชี้มาที่ port 8080 ซึ่งเป็น port proxy มาตรฐาน (มาตรฐานคนไทย) เมื่อใช้ iptables ในการชี้มาเสร็จแล้วก็จะต้อง add config นี้ลงไปใน config ของ squid เพื่อให้ squid ยอมรับการทำ redirect port

httpd_accel_host virtual

httpd_accel_port 80

httpd_accel_with_proxy on

httpd_accel_uses_host_header on

Step4 : การกำหนด Bandwidth ในการ download ข้อมูล

การกำหนด Bandwidth ในการ download โปรแกรม หรือการใช้ข้อมูลผ่าน squid นั้นจะใช้ Feature ที่ชื่อว่า Delay Pools ซึ่งตอนแรกที่เราได้ทำการ configure และ install ไปนั้น เราได้ทำไปแล้วซึ่งจะแบ่งการกำหนดออกเป็น 3 ลักษณะดังนี้

แบบแรก : การ กำหนด การ download จาก ตัวอักษรหรือคำ จาก website

acl magic_words url_regx –i .exe

delay_pools 1

delay_class 1 1

delay_parameters 1 4000/4000

delay_access 1 allow magic_words

อธิบายได้ดังนี้

magic_words เป็นตัวแปลบอกว่าถ้ามีนามสกุล .exe (ส่วน –i คือเป็นตัวบอกว่าให้เป็น case-insensitive) ให้ download มาด้วยความเร็วไม่เกิน 32Kbit/sec ก็ประมาณ 4KByte/sec 32Kbit/sec มาจาก 4000 * 8 ถ้าต้องการจะหา ความเร็วที่ต้องการจะให้ download ไม่เกินสามารถ หาได้ด้วยวิธีการคำนวณแบบนี้ สมมติ ให้ต้องการให้ download ไม่เกิน 2KByte/sec คือ (2000*8)/8 = 2000 (จะแสดงวิธีคิดให้ดูทำไมล่ะนี่ ?) ก็คือนำเอาจำนวน KByte/sec ที่ต้องการใส่เข้าไปเลยนะครับ :P~

แบบสอง : กำหนดให้ ขนาด Bandwidth ให้กับคนที่ใช้ภายใน network

acl a_ll src 0.0.0.0/0.0.0.0

delay_pool_count 1

delay_class 1 2

delay_parameters 1 12500/12500 2500/2500

delay_access 1 allow a_ll

อธิบายได้ดังนี้

จากตัวอย่างข้างต้นสมมติว่าคุณมี Lease Line ขนาด 128Kbit/sec และต้องการจะเหลือไว้สักนิดหนึ่งสำหรับ SMTP server ซึ่งเป็น IP จริงไม่ได้ต่อผ่านกับ gateway ตัวนี้ และ ให้ใช้สำหรับ NAT ภายในองค์กร 100Kbit/sec เพื่อให้ทุกคน share กันใช้ เท่าๆ กันและต้องการจะให้แต่ละคนใช้ bandwidth ได้ไม่เกิน 2.5KByte/sec (สังเกตดีๆ นะครับหน่วยไม่เหมือนกัน เพื่อง่ายต่อการเข้าใจควรตั้งใจอ่านทุกตัวอักษรครับ)

แบบสาม : กำหนดขนาด Bandwidth ให้กับ group network

acl a_ll src 0.0.0.0/0.0.0.0

delay_pool_count 1

delay_class 1 3

delay_parameters 1 56000/56000 18750/18750 500/500

delay_access 1 allow all

อธิบายได้ดังนี้

ถ้าคุณมี Lease Line ขนาด 512Kbit/sec และต้องการ เหลือ bandwidth สำหรับ SMTP ไว้ 8KByte/sec ใช้ภายใน network ไม่เกิน 448Kbit/sec และ แบ่งให้แต่ละ class ไม่เกิน 150Kbit/sec และให้แต่ละ IP ไม่เกิน 4kbit/sec (หน่วยไม่ผิดนะครับ ถ้าเป็น KByte เอา 8 หารจะเป็น Kbit ครับ)

เพื่อป้องกันความสับสน ผมจะเอาตัวอย่าง proxy ที่ผมใช้ภายใน Office เป็นตัวอย่างครับ ดังนี้

ตัวอย่าง squid.conf

#/squid/etc/squid.conf

# IP ของเครื่อง gateway ที่ใช้คือ 10.20.1.181

# eth1 10.20.1.181

# กำหนด port เพื่อใช้ในการ connect proxy http_port 8080

# กำหนด user ที่ใช้ในการ control cache squid ห้ามใช้ root เป็นอันขาด (เพราะมันจะใช้งานไม่ได้)

cache_effective_user squid

cache_effective_group squid

# Cache objects no larger than this size maximum_object_size 40960 KB

# Use this amount of memory (1/3rd of total memory).

# Note squid will use MORE than this for other tasks

cache_mem 16 MB

# กำหนดขนาด cache ที่นี้กำหนดให้ขนาด 4GB cache_dir ufs /squid/cache 4000 16 256

# กำหนดการ fake header

forwarded_for off

# What to do when a user aborts a download.

# If the transfer has less than the “quick_abort_min” value, it will continue to download

# the data. If it has more than the “quick_abort_max” value, it will abort the download.

# If more than “quick_abort_pct” percentage of the download has occurred, it won’t abort

# the download either. These values are the default settings.

quick_abort_min 16 KB

quick_abort_max 16 KB

quick_abort_pct 95

# กำหนด คำกับ website ที่ไม่ต้องการให้ cache

acl QUERY urlpath_regex cgi-bin \?

acl website dst 10.20.2.14/32

acl PHP url_regex .php?

no_cache deny website

no_cache deny QUERY

no_cache deny PHP

# Define some access control list (acl) “aliases”

acl Safe_ports port 80 21 443 563 591 70 210 1025-65535

acl manager proto cache_object

acl all src 0.0.0.0/0.0.0.0

acl allowed_hosts src 10.20.0.0/255.255.0.0

acl localhost src 127.0.0.1/255.255.255.255

acl SSL_ports port 443 563

acl CONNECT method CONNECT

# Generate some rules based on the aliases we just made

# This prevents access from those who shouldn’t have it

http_access allow manager localhost

http_access deny manager

http_access deny !Safe_ports

http_access deny CONNECT !SSL_ports

http_access allow allowed_hosts

http_access deny all

# Name to appear on error documents

cache_mgr yourname@yourdomain.com

# Append this domain to non-resolvable websites before giving up

append_domain .uoregon.edu

# Transparent proxy settings. Requires iptabes to be

# set up for transparent proxy as well (see below).

httpd_accel_host virtual

httpd_accel_port 80

httpd_accel_with_proxy on

httpd_accel_uses_host_header on

httpd_accel_single_host off

acl a_ll src 0.0.0.0/0.0.0.0

delay_pool_count 1

delay_class 1 2

delay_parameters 1 12500/12500 2500/2500

delay_access 1 allow a_ll

# หลอก header ไม่ให้ show X-Forward

anonymize_headers deny From Referer Server

anonymize_headers deny User-Agent WWW-Authenticate Link

fake_user_agent Thailand_ChaiYoo/1.0

Step 5 : ทำการ transparent ด้วย iptables

ทำการ redirect port 80 ที่ผ่าน Gateway ทั้งหมดไป ที่ port 8080 เพื่อบังคับให้ทุกคนใช้ proxy ครับ ด้วยคำสั่งดังนี้เลย

# iptables -t nat -A PREROUTING -i eth0 -p tcp —dport 80 -j REDIRECT -- to-port 8080

เมื่อทำตาม ปกติเรียบร้อยแล้ว ก็ทำการทดสอบใช้โดยเรียก IE หรือ mozilla เพื่อ เรียกดู website ส่วนที่ Gateway นั้น ก็ “tail-f /squid/logs/access.log” เพื่อดูการเปลี่ยนแปลงของ log ครับ ทดลองดูกันนะครับ ขอให้โชคดี : )

ldap มั่วๆ

For example, if the directory contained entries named:

0: o=suffix

1: cn=Manager,o=suffix

2: ou=people,o=suffix

3: uid=kdz,ou=people,o=suffix

4: cn=addresses,uid=kdz,ou=people,o=suffix

5: uid=hyc,ou=people,o=suffix

Then:

dn.base="ou=people,o=suffix" match 2;
dn.one="ou=people,o=suffix" match 3, and 5;
dn.subtree="ou=people,o=suffix" match 2, 3, 4, and 5; and
dn.children="ou=people,o=suffix" match 3, 4, and 5.

Entries may also be selected using a filter:

    to filter=

where is a string representation of an LDAP search filter, as described in RFC4515. For example:

    to filter=(objectClass=person)

Note that entries may be selected by both DN and filter by including both qualifiers in the clause.

    to dn.one="ou=people,o=suffix" filter=(objectClass=person)

7.2.2. Who to grant access to

The part identifies the entity or entities being granted access. Note that access is granted to "entities" not "entries." The following table summarizes entity specifiers:

Table 6.3: Access Entity Specifiers

Specifier

Entities

*

All, including anonymous and authenticated users

anonymous

Anonymous (non-authenticated) users

users

Authenticated users

self

User associated with target entry

dn[.]=

Users matching a regular expression

dn.=

Users within scope of a DN

The DN specifier behaves much like clause DN specifiers.

7.2.3. The access to grant

The kind of granted can be one of the following:

Table 6.4: Access Levels

Level

Privileges

Description

none =

0

no access

disclose =

d

needed for information disclosure on error

auth =

dx

needed to authenticate (bind)

compare =

cdx

needed to compare

search =

scdx

needed to apply search filters

read =

rscdx

needed to read search results

write =

wrscdx

needed to modify/rename

manage =

mwrscdx

needed to manage

Each level implies all lower levels of access. So, for example, granting someone write access to an entry also grants them read, search, compare, auth and disclose access. However, one may use the privileges specifier to grant specific permissions.

radius + ldap

Radius+Ldap

การติดตั้ง radius server ด้วยโปรแกรม freeradius
วิธีติดตั้ง radius server ด้วยโปรแกรม freeradius
*** แหล่งข้อมูลต้นฉบับ freeradius คือ http://www.freeradius.org/

1. ติดตั้งโปรแกรม freeradius ด้วยคำสั่ง
apt-get install freeradius

ในกรณีที่ใช้ yum ให้เปลี่ยนคำว่า apt-get เป็น yum ดังตัวอย่าง
yum install freeradius

2. แก้ไขให้ทำงานทุกครั้งที่บูทเครื่อง
chkconfig radiusd on

3. สั่งให้ทำงานด้วยคำสั่งว่า
/etc/init.d/radiusd start
Starting RADIUS server: [ OK ]

4. ตรวจสอบว่าโปรแกรมทำงานแล้วด้วยคำสั่ง
/etc/init.d/radiusd status
ได้ผลออกมาดังตัวอย่าง
radiusd (pid 19180) is running...

5. แฟ้มที่เกี่ยวข้อง
* /var/log/radius/radius.log
* /etc/raddb/radiusd.conf
* /etc/raddb/clients.conf

6. เมื่อให้ radiusd ทำงานแล้ว เริ่มขั้นตอนทดสอบระบบโดยป้อนตัวอย่างคำสั่งดังนี้

radtest root password-root localhost 0 testing123
*** จากตัวอย่าง password-root คือรหัสผ่านของ root
ได้ผลข้อความตอบกลับดังตัวอย่าง
Sending Access-Request of id 43 to 127.0.0.1:1812
User-Name = "root"
User-Password = "password-root"
NAS-IP-Address = 10.0.1.2
NAS-Port = 0
Re-sending Access-Request of id 43 to 127.0.0.1:1812
User-Name = "root"
User-Password = "{\277\033\t\217\224\n\021\036\271$\301\026\234\353\225"
NAS-IP-Address = 10.0.1.2
NAS-Port = 0
rad_recv: Access-Reject packet from host 127.0.0.1:1812, id=43, length=20

แสดงว่าระบบใช้งานได้แล้ว ถึงแม้ว่าจะมีการแจ้งว่า Access-Reject
เป็นสาเหตุเนื่องจากไม่มีสิทธิในการอ่านแฟ้ม /etc/shadow ของระบบ

7. หากต้องการให้ใช้กับบัญชีผู้ใช้ระบบ UNIX
ต้องตั้งค่าให้อ่านแฟ้ม /etc/shadow ได้
โดยแก้ไขแฟ้ม /etc/raddb/radiusd.conf
ทำการ comment ยกเลิกบรรทัดข้อความจากเดิม
user = radiusd
group = radiusd
ให้เป็น
#user = radiusd
#group = radiusd
เสร็จแล้วให้ restart ระบบ radiusd ใหม่ด้วยคำสั่ง /etc/init.d/radiusd restart
ต่อไปลองป้อนตัวอย่างคำสั่งเดิมเพื่อทดสอบดังนี้
radtest root password-root localhost 0 testing123
คราวนี้จะได้ผลข้อความว่า
Sending Access-Request of id 82 to 127.0.0.1:1812
User-Name = "root"
User-Password = "password-root"
NAS-IP-Address = 10.0.1.2
NAS-Port = 0
rad_recv: Access-Accept packet from host 127.0.0.1:1812, id=82, length=20
โดยมีผลการแจ้งว่า Access-Accept ถูกต้องตามต้องการ

8. หากต้องการให้ radius client เข้ามาติดต่อใช้งานระบบ radiusd นี้ได้
ให้แก้ไขแฟ้ม /etc/raddb/clients.conf ให้มีค่าดังตัวอย่างนี้
client 10.0.1.0/24 {
secret = mytestkey
shortname = private-network
}
ตัวอย่างคือให้ radius client ที่มีหมายเลข ip 10.0.1.x
สามารถเข้ามาใช้ด้วย secret key ว่า mytestkey
เมื่อแก้ไขเสร็จแล้วให้ reload ใหม่ด้วยคำสั่ง /etc/init.d/radiusd reload
ลองทดสอบด้วยคำสั่งตัวอย่าง
radtest root passwordroot 10.0.1.1 0 mytestkey
*** ค่า passwordroot คือ รหัสผ่านของ root และ 10.0.1.1 คือหมายเลข ip ของ radius server
ได้ผลแสดงข้อความดังตัวอย่าง
Sending Access-Request of id 246 to 10.0.1.1:1812
User-Name = "root"
User-Password = "passwordroot"
NAS-IP-Address = 10.0.1.2
NAS-Port = 0
rad_recv: Access-Accept packet from host 10.0.1.1:1812, id=246, length=20

ถึงขั้นตอนนี้เป็นอันเปิดใช้ระบบ radiusd ได้แล้ว

9. หากต้องการให้ใช้ได้ดับ LDAP ด้วยให้แก้ไขแฟ้ม /etc/raddb/radiusd.conf
เพื่อให้รองรับและเชื่อมต่อกับ LDAP server ดังนี้

ต้องแก้ไขส่วนของ modules โดยค้นเข้าไปที่บริเวณพื้นที่ขึ้นต้นด้วยข้อความว่า
modules {
...
ldap {
**** ให้แก้ไขค่าของ server , basedn ดังตัวอย่าง
server = "myldap.net"
basedn = "dc=my, dc=net"
**** ให้ comment บรรทัด access_attr เพื่อยกเลิกค่า เนื่องจากไม่ได้ใช้รูปแบบนี้
# access_attr = "dialupAccess"

*** ในส่วนของ modules แก้ไขเพียงแค่นี้
ต่อไปแก้ไขส่วนของ authorize โดยค้นเข้าไปที่บริเวณพื้นที่ขึ้นต้นด้วยข้อความว่า
authorize {
...
# Read the 'users' file
files
*** ให้แก้ไขโดยแทรกเพิ่มบรรทัดข้อความว่า ldap ก่อนบรรทัดคำว่า files
เพื่อให้ระบบทำการลำดับ authorize ด้วยแบบ ldap ก่อน
หากไม่พบก็จะต่อด้วยแบบ files (แฟ้ม /etc/shadow)
ถ้ากลับลำดับการ authorize อาจพบปัญหาผิดพลาดได้
ดังตัวอย่างหลังการแทรกเพิ่มข้อความ
# Read the 'users' file
ldap
files
*** ในส่วนของ authorize แก้ไขเพียงแค่นี้
ต่อไปแก้ไขส่วนของ authenticate โดยค้นเข้าไปที่บริเวณพื้นที่ขึ้นต้นด้วยข้อความว่า
authenticate {
...
# Auth-Type LDAP {
# ldap
# }
*** ให้แก้ไขด้วยการ uncomment บรรทัดข้อความเพื่อให้ระบบกำหนดวิธี LDAP
Auth-Type LDAP {
ldap
}
*** ในส่วนของ authenticate แก้ไขเพียงแค่นี้

เสร็จแล้วให้ reload ใหม่ด้วยคำสั่ง /etc/init.d/radiusd reload
ทดสอบใช้งานได้เลย

10. แนะนำให้ยกเลิกการกำหนดค่าการทำงานบางตัวที่ไม่ได้ใช้งาน
โดยการ comment บรรทัดข้อความในแฟ้ม /etc/raddb/radiusd.conf

ถ้าไม่ได้ใช้เป็น proxy radius ควรยกเลิก
โดยเปลี่ยนจากเดิม proxy_requests = yes
ให้เป็น proxy_requests = no

ควรยกเลิกบาง authorize ที่ไม่ได้ใช้เช่น
authorize {
# preprocess
# chap
# mschap
# suffix
# ldap ---> ระบบ ldap
# files --> ระบบ UNIX หรือแฟ้ม /etc/shadow

11. การแสดงผลรายงานจำเป็นต้องหาโปรแกรมมาต่างหาก
ขอแนะนำตัวอย่างโปรแกรมแสดงผลรายงาน
* ต้นแหล่งข้อมูลคือ http://www.tummy.com/radiusContext/
สามารถได้ดาวน์โหลดโปรแกรมได้ที่
ftp://ftp.psu.ac.th/pub/freeradius/radiusContext-1.93.tar.gz

* ให้ดาวน์โหลดมาแล้วขยายแฟ้มเก็บไว้ที่ /root ด้วยตัวอย่างคำสั่ง
tar -C /root -zxvf radiusContext-1.93.tar.gz

* สร้าง directory สำหรับแสดงผลบนเวบ ดังตัวอย่างคือ
mkdir /var/www/html/radiusreport
จะแสดงผลบนโฮมพจ http://x.x.x.x/radius-report

* ตัวอย่าง ขั้นตอนที่ใช้สำหรับประมวลผลรวมข้อมูลจาก
/var/log/radius/radacct ไปเก็บไว้เพื่อแสดงผลที่ /var/www/html/radius-report
*** ภายใน /var/log/radius/radacct จะแยกเก็บข้อมูลเป็น directory ของ
แต่ละหมายเลข ip ทำให้อาจยุ่งยากต่อการรวบรวมข้อมูล

/root/radiusContext-1.93/raddetail /var/log/radius/radacct/*/*
/root/radiusContext-1.93/stdreport -D /var/www/html/radius-report

*** ต้องใช้คำสั่งเหล่านี้ทุกครั้งเพื่อปรับปรุงผลรายงาน

* ทดสอบผลรายงานได้เลยที่ http://x.x.x.x/radius-report

Ref :: http://rd.cc.psu.ac.th/content/view/35/46/

เป็นคำแนะนำติดตั้งจัดการปรับแต่ง radius server
ต้นแบบทดสอบด้วย linux fedora core 4,3 ใช้งานภายในมหาวิทยาลัยสงขลานครินทร์