史萊姆論壇

返回   史萊姆論壇 > 專業主討論區 > 網路疑難應用技術研討區
忘記密碼?
論壇說明

歡迎您來到『史萊姆論壇』 ^___^

您目前正以訪客的身份瀏覽本論壇,訪客所擁有的權限將受到限制,您可以瀏覽本論壇大部份的版區與文章,但您將無法參與任何討論或是使用私人訊息與其他會員交流。若您希望擁有完整的使用權限,請註冊成為我們的一份子,註冊的程序十分簡單、快速,而且最重要的是--註冊是完全免費的!

請點擊這裡:『註冊成為我們的一份子!』

Google 提供的廣告


發文 回覆
 
主題工具 顯示模式
舊 2004-07-28, 11:25 PM   #1
psac
榮譽會員
 
psac 的頭像
榮譽勳章
UID - 3662
在線等級: 級別:30 | 在線時長:1048小時 | 升級還需:37小時級別:30 | 在線時長:1048小時 | 升級還需:37小時級別:30 | 在線時長:1048小時 | 升級還需:37小時級別:30 | 在線時長:1048小時 | 升級還需:37小時級別:30 | 在線時長:1048小時 | 升級還需:37小時
註冊日期: 2002-12-07
住址: 木柵市立動物園
文章: 17381
現金: 5253 金幣
資產: 33853 金幣
預設 Linux代理伺服器設定和站點過濾技巧

Linux代理伺服器設定和站點過濾技巧

一. 前言

  本文主要介紹了在linux使用squid和squidGuard配置代理伺服器,以www代理服務為例介紹如何過濾有害站點和限制用戶對internet的訪問。

二. 介紹

  Squid是Linux下最為流行的代理伺服器軟體,它功能強大,支持對HTTP,FTP,Gopher,SSL和WAIS等傳輸協定的代理;設定簡單,只需對配置文件中稍稍改動就可使代理伺服器運轉起來。而且Squid具有頁面快取功能,它接收用戶的下載申請,並自動處理所下載的資料。也就是說,當一個用戶像要下載一個主頁時,它向Squid發出一個申請,要Squid替它下載,然後Squid連接所申請網站並請求該主頁,接著把該主頁傳給用戶同時保留一個制作備份,當別的用戶申請同樣的頁面時,Squid把儲存的制作備份立即傳給用戶,使用戶覺得速度相當快。

  squidGuard則是作為squid的輔助軟體,完成過濾、重轉發IP和訪問控制的功能。它是一個自由軟體,功能強,便於安裝、易於配置、而且處理速度快。功能主要包括:根據web伺服器或URLs列表限制一些用戶的訪問;阻塞某些用戶對黑名單上的web伺服器和URLs的訪問;阻塞某些用戶對正則陳述式匹配的URLs的訪問;在URL路徑加強了使用域名訪問而禁止用IP訪問;重轉發IP阻塞的URLs到一個智能CGI的資訊頁;重轉發IP非授權用戶到一個註冊頁面;具有關於日期、每週、每天具體時間的訪問規則;對不同用戶組有不同的規則。但是不能過濾、檢查我的文件中的文本以及HTML中的JavaScript或Vbscript指令碼語言。


三. 安裝

  1. 安裝squid:

  從www.squid-cache.org下載squid-2.4.ST...l/squid/src下。

  在編譯Squid之前,建立一個專門執行Squid的用戶和組,這裡建立了名為squid的組和用戶,用戶目錄設為

/usr/local/squid
#su squid
$cd /usr/local/squid/src
$tar xvzf squid-2.4.STABLE2-src.tar.gz
$ cd squid-2.4.STABLE2
$./configure
$make
$make install

  (預設安裝到/usr/local/squid目錄下)

  2. 安裝Berkeley DB 2.x:

  從http://www.sleepycat.com 下載db-2.7.7.tar.gz並存在/usr/local/squidGuard/src/目錄下
$su
#cd /usr/local/squidGuard/src/
#tar xvzf db-2.7.7.tar.gz
#cd db-2.7.7
#cd build_unix
#../dist/configure
#make
#make install

  (預設安裝到/usr/local/BerkeleyDB目錄下)

  注意:squidGuard不支持Berkeley DB 3.x版本

  3. 安裝squidGuard

從http://ftp.ost.eltele.no/pub/www/pr...squidGuard/src/
#cd /usr/local/squidGuard/src/
#tar xvzf squidGuard-1.1.4.tar.gz
#cd squidGuard-1.1.4
#./configure --with-sg-config=/usr/local/squidGuard/squidGuard.conf
--with-sg-logdir=/usr/local/squidGuard/logs
--with-sg-dbhome=/usr/local/squidGuard/db
#make
#make test //測試ok,即可進行下一步安裝
#make install
四. 配置


  1、配置squid:

  修改squid的配置文件/usr/local/squid/etc/squid.conf:
http_port 8080

  #squid的代理連接阜,使用1024以下的連接阜,squid必須以root身份執行
  http_access allow all

  #允許所有的用戶通過代理進行http訪問
  redirect_program /usr/local/squid/bin/squidGuard -c /usr/local/squidGuard/squidGuard.conf

  #squid啟用squidGuard進行過濾和轉發

  其它參數:

  cache_mem:設定代理服務使用的記憶體大小,一般推薦為實體記憶體的三分之一

  cache_dir:指定cache目錄的路徑,預設為/usr/local/squid/cache。

  maximum_object_size: 指定Squid可以接收的最大對象的大小。Squid預設值為4M,可以根據自己的需要進行設定。

  cache_dir:設定快取的位置、大小。一般格式如下:

  cache_dir /usr/local/squid/cache 100 16 256

  /usr/local/squid/cache代表快取的位置;100代表快取最大為100M;16和256代表一級和二級目錄數。
cache_effective_user:設定使用快取的有效用戶。預設為用戶nobody,如果系統中沒有用戶nobody,最好建一個或以非root用戶執行Squid。這裡是以squid身份執行的

  cache_effective_group:設定使用快取的有效用戶組。預設組為nogroup,如果系統中沒有組nogroup,最好建一個組。這裡是squid組。

   (其餘參數用預設值即可!)

  2.配置squidGuard:

  修改squidGuard的配置文件/usr/local/squidGuard/squidGuard.conf文件:

logdir /usr/local/squidGuard/logs #日誌目錄定義

dbhome /usr/local/squidGuard/db #db目錄定義

time testtime { #時間規則定義

weekly mtwhf 05:00 - 10:30
weekly as 08:00 - 19:00
date *-*-01 08:00 - 16:30
date 2001.10.01 - 2001.10.09
}

src admin { #源組定義
ip 192.168.100.18
}

src client{
ip 192.168.100.20 192.168.100.21 192.168.100.22
ip 192.168.200.0/24
}

dest porn { #目標組定義
domainlist porn/domains
urllist porn/urls
expressionlist porn/expressions
}

acl { #訪問規則定義
admin within testtime {
pass !porn all
} else {
pass all
}

client {
pass !in-addr !porn all
}

default {
pass none
redirect http://admin.foo.com

  (#也可以重轉發IP到一個含有一些資訊的cgi頁面,如下:
http://admin.foo.com/cgi/blocked?cl...=%n&clientuser=
%i&clientgroup=%s&targetgroup=%t&url=%u)
}
}

  # vi db/porn/domains
  (域列表文件:主要是阻塞一些定義的站點)
  co.za
  sex.com
  (如上,可以阻塞如hack.co.za、sex.com、www.sex.com、whatever.sex.com,但是不同於.*[^.]sex.com,不匹配ssex.com)

  # vi db/porn/urls
  (url列表文件,主要是阻塞一些站點及其一些欄目)

  qihui.com/sex
  valen.sohu.com/album
  (如上可阻塞http://qihui.com/sex、http://qihui....qihui.com/sex等)

  # vi db/porn/expressions
  (陳述式列表文件,主要是阻塞一些與陳述式匹配的URL訪問)

  (^|[\?+=/])(.*)(girl)(.*)([\?+=/]|$)
  (上面的正則陳述式可以阻塞URL中包括girl站點的訪問,如:www.girlzine.com、girl.huabao.net、...siti.com/girl等 )

  注意:squidGuard對配置文件的語法要求很嚴,如果配置文件語法有誤,squidGuard仍能執行,但是squidGuard已進入應急模式,此時代理服務不具有任何阻塞作用,所有通過該代理的訪問都可通過,可以檢視logs/squidGuard的日誌文件,即可發現錯誤,例如:

  2001-12-20 17:08:44 [2430] parse error in configfile /usr/local/squidGuard/squidGuard.conf line 8
  2001-12-20 17:08:44 [2430] going into emergency mode
…….

  其中配置文件第8行有誤,squidGuard進入應急模式。

  配置的具體說明詳見http://www.squidguard.org/

五. 執行:

  $ chmod 777 /usr/local/squid/logs
  設定logs對所有用戶為可寫。這樣,不特定的squid代理客戶才能正常訪問代理伺服器,並能在logs目錄、產生access.log、cache.log等文件。)

  $ /usr/local/squid/bin/squid -z
  (手工建立squid的快取目錄/usr/local/squid/cache。)

  #/usr/local/squid/bin/squid
  (後台執行squid。如果想前台執行squid:如果你想前台執行Squid執行指令:

  $/usr/local/squid/bin/squid -NCd1
  該指令正式啟動Squid。如果一切正常,你會看到一行輸出:

  Ready to serve requests
  # ps ax|grep squid
  20198 ? S 0:00 /usr/local/squid/bin/squid
  20200 ? S 0:27 (squid)
  20310 ? S 0:00 (squidGuard) -c /usr/local/squidGuard/squidGuard.conf
  20311 ? S 0:00 (squidGuard) -c /usr/local/squidGuard/squidGuard.conf
  20312 ? S 0:00 (squidGuard) -c /usr/local/squidGuard/squidGuard.conf
  20313 ? S 0:00 (squidGuard) -c /usr/local/squidGuard/squidGuard.conf
  20314 ? S 0:00 (squidGuard) -c /usr/local/squidGuard/squidGuard.conf

  (此時squidGuard也已啟用,每次修改配置後squid -k reconfigure重新起用即可,要殺掉squid執行squid -k kill)

  檢視squidGuard日誌文件:

  init domainlist /usr/local/squidGuard/db/porn/domains
  2001-12-20 16:14:43 [2270] init domainlist /usr/local/squidGuard/db/porn/domains
  2001-12-20 16:14:43 [2270] init urllist /usr/local/squidGuard/db/porn/urls
  2001-12-20 16:14:43 [2270] init expressionlist /usr/local/squidGuard/db/porn/expressions
  2001-12-20 16:14:43 [2270] squidGuard 1.1.4 started (1008836083.022)
  2001-12-20 16:14:43 [2270] recalculating alarm in 917 seconds
  2001-12-20 16:14:43 [2270] squidGuard ready for requests (1008836083.044)

  表示squidGuard已正常啟動
六. 測試:

  配置客戶端,然後測試代理服務:

  在另一台win2k上,(以Internet Explore5.0為例)執行IE,按下"工具",接著按下"Internet選項",再按下"連接"選擇項,按下"區域網路設定";在"區域網路設定"視窗中,?quot;位址"處填上squid伺服器的IP位址192.168.100.16,在"連接阜"處填上"8080"(修改後squid代理使用的連接阜號,也就是squid.conf中的http_port,預設值為3128),確定後退出。

  接下來,先把IP改成192.168.100.20,瀏覽一些網站,如sohu,163等,然後再試試domains和urls中定義的,如hack.co.za、qihui.com/sex,會發現主頁被重轉發IP到[url]http://admin.foo.com。然後再試試瀏?..荒蓯褂胕p訪問︴/url]^

  再下來,把IP改成192.168.100.18,然後時間改為testtime外的時間,瀏覽網頁,試試結果,然後再將時間改為testtime內瀏覽網頁!

  最後,把IP改成192.168.100.30,瀏覽網頁測試。

  (可以檢視logs下的access.log和cache.log,看看是否代理執行正常以及訪問的站點記錄)

七. 總結:

  由上可見,用squid和squidGuard建立的代理伺服器,配置比較簡單,而且功能強大,可以有效的限制一些用戶對internet的訪問並過濾一些黑名單列出的站點(如色情站點等)。

  這裡只簡單的介紹了關於http代理的例子,其它的套用和功能大家可以自己試試。
The database
squidGuard uses a database that can be devided into an unlimited number of distinct categories like "local", "customers", "vendors", "banners", "banned" etc. Each category may consist of separate unlimited lists of domains, URLs and/or regular expressions. For easy revision the lists are stored in separate plain text files that. The lists are for efficiency stored in in-memory-only B-trees at startup.

Note: All URLs are converted to lowercase before match search. So the lists should not contain uppercase leters.

Domainlists
The domainlist file format is simply domainnames/zonenames separated by a newline. The length of these lists have neglectable influence on the performance.

For instance a start for a financial category:

amex.com
asx.com.au
bourse-de-paris.fr
exchange.de
londonstockex.co.uk
nasdaq.com
nyse.com
ose.no
tse.or.jp
xsse.se

Note: squidGuard will match any URL with the domainname itself an any subdomains and hosts (i.e. amex.com, www.amex.com, whatever.amex.com and www.what.ever.amex.com but not .*[^.]amex.com (i.e. aamex.com etc.)).

URLlists
The urllist file format is simply URLs separated by newline but with the "proto://((www|web|ftp)[0-9]*)?" and "(ort)?" parts and normally also the ending "(/|/[^/]+\.[^/]+)$" part (i.e. ending "/" or "/filename") choped off. (i.e. "http://www3.foo.bar.com:8080/what/ever/index.html" => "foo.bar.com/what/ever")

For instance a category for banned sites:

foo.com/~badguy
bar.com/whatever/suspect

Note: The removed parts above are ignored by squidGuard in URL matching. Thus all these URLs will match the above urllist:

http://foo.com/~badguy
http://foo.com/~badguy/whatever
ftp://foo.com/~badguy/whatever
wais://foo.com/~badguy/whatever
http://www2.foo.com/~badguy/whatever
http://web56.foo.com/~badguy/whatever

but not:

http://barfoo.com/~badguy
http://bar.foo.com/~badguy
http://foo.com/~goodguy

New in 1.0.0 is the ability to do 1-1 redirects on url basis with "key new_url". Thus as an alternative to using rewrites to redirect to local distributions you can have a destination group with an urllist like:

netscape.com/pub/communicator/4.51/english/windows/windows95_or_nt/complete_install/cc32e451.exe http://ftp.teledanmark.no/pub/www/cl...s/cc32e451.exe
netscape.com/pub/communicator/4.51/english/windows/windows95_or_nt/base_install/cb32e451.exe http://ftp.teledanmark.no/pub/www/cl...s/cb32e451.exe

and an acl with pass ... !download .... This may be a faster alternative than using lots of s@from@to@ rewrites for 1-1 mapping since it will be faster to search the B-tree than perform a bunch of string edits.

Expressionlists
The expressionlist file format is lines with regular expressions as described in regex(5). Of most interrest is:

. Matches any single character (use "\." to match a ".").
[abc] Matches one of the characters ("[abc]" matches a single "a" or "b" or "c").
[c-g] Matches one of the characters in the range ("[c-g]" matches a single "c" or "d" or "e" or "f" or "g".
"[a-z0-9]" matches any single letter or digit.
"[-/.:?]" matches any single "-" or "/" or "." or ":" or "?".).
? None or one of the preceding ("words?" will match "word" and "words".
"[abc]?" matches a single "a" or "b" or "c" or nothing (i.e. "")).
* None or more of the preceding ("words*" will match "word", "words" and "wordsssssss". ".*" will match anything including nothing).
+ One or more of the preceding ("xxx+" will match a sequence of 3 or more "x").
(expr1|expr2) One of the expressions, which in turn may contain a similar construction ("(foo|bar)" will match "foo" or "bar".
"(foo|bar)? will match "foo" or "bar" or nothing (i.e. "")).
$ The end of the line ("(foo|bar)$" will match "foo" or "bar"only at the end of a line).
\x Disable the special meaning of x where x is one of the special regex characters ".?*+()^$[]{}\" ("\." will match a single ".", "\\" a single "\" etc.)

Thus a start to block possible sexual material by expression match could look like:

(^|[-\?+=/_])(bondage|boobs?|busty?|hardcore|porno?|sex|xxx+)([-\?+=/_]|$)

Notes:
Unless you build your expressions very very carefully there is a high risk you will have annoyed users on your neck. Typically you might accidentally block "Essex", "Sussex", "breastcancer", "www.x.org" etc. in your eagerness for blocking pornographic material. In practice you would probably replace some of the words in the example above with some more clearly pornographic related words that I don't find appropriate to list here.


While the size of the domain and urllists only has marginal influence on the performance, too many large or complex expressions will quickly degrade the performance of squidGuard. Though it may depend heavily on the performance of the regex library you link with.


There is a rich set of sample files for a group of supposedly pornographic sites under samples/dest/adult in the source tree that you can use as a start if porn blocking is one of your tasks. Please note: We recommend that you review these lists before using them. Those domains and urls have been collected automagically by a robot. No manual evaluation of the corresponding contents has been performed. Therefor there is a chance some nonpornographic sites have sliped in. Please report such errors but don't blame us if your fine site is on the list. (Blame those who have pointers to appropriate sites mixed in on their heavy porn link pages!)


To avoid publishing to your users a complete guide to banned sites, you probably want to have some or all of these files protected by for instance:
chmod 640 /wherever/filter/db/dest/adult/*
chown cache_effective_user /wherever/filter/db/dest/adult/*
chgrp cache_effective_group /wherever/filter/db/dest/adult/*
where cache_effective_user and cache_effective_group are the values for the corresponding tags as defined in squid.conf.
psac 目前離線  
送花文章: 3, 收花文章: 1631 篇, 收花: 3205 次
回覆時引用此帖
發文 回覆



發表規則
不可以發文
不可以回覆主題
不可以上傳附加檔案
不可以編輯您的文章

論壇啟用 BB 語法
論壇啟用 表情符號
論壇啟用 [IMG] 語法
論壇禁用 HTML 語法
Trackbacks are 禁用
Pingbacks are 禁用
Refbacks are 禁用


所有時間均為台北時間。現在的時間是 04:33 PM


Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2024, Jelsoft Enterprises Ltd.


SEO by vBSEO 3.6.1