2011年5月28日 星期六

如何自訂 Apache Log

分享:如何自訂 Apache Log
« 於: 2009-10-11 01:12 »

每當在看 apache 的 log 檔時,總是覺得畫面密密麻麻的,好多字都看不完,雖然預設的 log 已經提供足夠的訊息讓我們來偵錯或查詢異狀,但總是覺得不夠人性化,也許您會覺得看多了就會習慣,可是換個角度想,如果可以按照自己想要的訊息來紀錄,不是更好嗎,所以才會有這篇文章。

話不多說,先來看以下的資料 (資料來源:http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#formats)

Apache Custom Log Formats (如果翻譯的不對,請糾正。)

Format String Description
%% 百分比符號
%a 遠端 IP 位址
%A 本機 IP 位址
%B 回應一個 request 的大小 (bytes),含 HTTP headers
%b 以 CLF 格式回應一個 request 的大小 (bytes),含 HTTP headers
%{Foobar}C 傳送到伺服器的 Foobar cookie 的內容
%D request 傳送到伺服器的時間 (microseconds)
%{FOOBAR}e 伺服器環境變數 FOOBAR 的內容
%f 檔名
%h 遠端主機名稱
%H request 的通訊協定
%{Foobar}i The contents of Foobar: header line(s) in the request sent to the server.
Changes made by other modules (e.g. mod_headers) affect this.
%k 在一個 connection requests handled 中 keepalive 的數量
%l 遠端的 logname (This will return a dash unless mod_ident is present and Identity Check is set On).
%m The request method.
%{Foobar}n The contents of note Foobar from another module.
%{Foobar}o The contents of Foobar: header line(s) in the reply.
%p 伺服器回應一個 request 的 canonical port number
%{format}p The canonical port of the server serving the request or the server's actual port or the client's actual port.
Valid formats are canonical, local, or remote.
%P 回應 request 時的 PID
%{format}P The process ID or thread id of the child that serviced the request. Valid formats are pid, tid, and hextid. hextid requires APR 1.2.0 or higher.
%q The query string (prepended with a ? if a query string exists, otherwise an empty string).
%r First line of request.
%s 狀態. 若是由內部轉址來的 request,則將顯示原始 request 的狀態
%t 伺服器收到 request 的時間 (standard english format)
%{format}t 同上,但以自訂的格式來顯示時間
%T 伺服器回應 request 的時間 (in seconds)
%u 遠端的使用者 (from auth)
%U The URL path requested (not including any query string)
%v 伺服器回應 request 時的 canonical ServerName
%V The server name according to the UseCanonicalName setting.
%X Connection status when response is completed :
 X:connection aborted before the response completed.
 +:connection may be kept alive after the response is sent.
 -:connection will be closed after the response is sent.
This directive was %c in late versions of Apache 1.3, but this conflicted with the historical ssl %{var}c syntax.
%I Bytes received, including request and headers, cannot be zero. You need to enable mod_logio to use this.
%O Bytes sent, including headers, cannot be zero. You need to enable mod_logio to use this.

我的 httpd.conf 中 log 的設定:

引用
LogFormat '--------------------\n%{%Y-%m-%d}t%{%H:%M:%S}t from %h\nrequest url : %U\nquery string : %q\nstatus : %s\n%{User-Agent}i' localDefine

 # \n:代表換行
 # {%Y-%m-%d}t:代表將時間以 日期 來顯示
 # %{%H:%M:%S}:代表將時間以 時:分:秒 來顯示

‥‥
CustomLog '/var/log/httpd-access.log' localDefine

套用設定所顯示的效果:

引用
--------------------
2009-10-11 00:37:41 from 163.19.150.208
request url : /albumThumb.php
query string : ?s=35&i=recorderTeam-03.jpg&pr=/webhd/share/photo&pd=2009-09-25-Recorder_Concert
status : 200
Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
--------------------
2009-10-11 00:37:41 from 163.19.150.208
request url : /albumThumb.php
query string : ?s=35&i=listenStory-20.jpg&pr=/webhd/share/photo&pd=2009-08-29-Listen_Story
status : 200
Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
--------------------
2009-10-11 00:37:41 from 163.19.150.208
request url : /albumThumb.php
query string : ?s=35&i=leofoo-19.jpg&pr=/webhd/share/photo&pd=2009-09-19-LeoFoo_Village
status : 200
Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
‥‥

用apache限定訪客來源IP

用apache限定訪客來源IP

設定檔: /usr/local/etc/apache/httpd.conf

舉例來說:
我希望限制 http://ohaha.ks.edu.tw/secret/ 資料夾,
只能給某些IP連結(此例為允許163.19.163.99).

如範例:

#設定連結位址
Order deny,allow
Deny from all #先拒絕全部IP連結
Allow from 163.19.163.99 #開放允許的IP位址


也可以使用區段的方式表示或是分開表示,如範例:

Allow from 163.19.163.0/24 #區段
Allow from 163.19.163.97 163.19.163.98 #分開表示


若不屬於此授權IP的使用者,連結此網站就會看到如下畫面

Forbidden
You don't have permission to access /secret/ on this server.


若希望連結此網頁的人得到稍微詳細的資訊,則可以在 ErrorDocument 設定
如範例:


Order deny,allow
Deny from all
Allow from 163.19.163.99
ErrorDocument 403 /error/deny.htm #希望提供顯示的錯誤頁面


並記得新增您要顯示的錯誤頁面.

也可以使用 方式限定
如範例:


Order deny,allow
Deny from all
Allow from 163.19.163.99
ErrorDocument 403 /error/deny.htm


- 已使用 Google 工具列寄出"

2011年5月27日 星期五

防 ssh 暴力攻擊 使用iptables & shell @ CentOS

Owen's: [轉錄][Linux] 防 ssh 暴力攻擊 使用iptables & shell @ CentOS: "原則上 ssh port 能不要全部開啟事最好的,如果一定要開啟的話,必需要作些防護措施, port 22 一旦開啟後最容易遇到的就是會有人不斷的try密碼 設計原理: 1. 抵擋 ssh 暴力攻擊 2. 如果連續攻擊3次以上則抵擋 3. 利用 TCP-Wrapper 偵測是否有登入失敗 ,有失敗才執行shell mkdir /etc/firewall/ touch /etc/firewall/sshd.sh chmod 755 /etc/firewall/sshd.sh vi /etc/firewall/sshd.sh 寫入以下資料 #!/bin/bash # 檔案存放的路徑 basedir='/etc/firewall' # log 存放的地方 sshlog='/var/log/auth.log' # mail for mailfor='root' # 登入錯誤訊息 faildMsg='sshd.*Failed password' faildMsgInvalid='sshd.*Failed password for invalid user' # 登入幾次失敗 就擋掉 loginCountFail=3 # 計算前 XXXX 行是否有登入失敗的狀況 failcount=`/usr/bin/tail -n 50 '$sshlog' | /bin/grep '$faildMsg' | wc -l` #cat '$sshlog' | grep 'sshd.*Failed password for' | grep -v 'invalid' | cut -d ' ' -f11 #cat '$sshlog' | grep 'sshd.*Failed password for invalid user' | cut -d ' ' -f13 # 如果有登入失敗的話 if [ '$failcount' -gt '0' ]; then # 建立 登入失敗 暫存檔 #cat /var/log/auth.log | grep 'sshd.*Failed password for' | grep -v 'invalid' cat '$sshlog' | grep 'sshd.*Failed password for' | grep -v 'invalid' > /tmp/ssh_tmp.log #cat /var/log/auth.log | grep 'sshd.*Failed password for' | grep -v 'invalid' cat '$sshlog' | grep 'sshd.*Failed password for invalid user' > /tmp/ssh_tmp2.log # 建立 登入失敗 IP 暫存檔 #cat '$sshlog' | grep '$faildMsg' | cut -d ' ' -f13 | sort | uniq > /tmp/ssh_ip_tmp.log cat /tmp/ssh_tmp.log | cut -d ' ' -f11 > /tmp/ssh_ip_tmp.log cat /tmp/ssh_tmp2.log | cut -d ' ' -f13 >> /tmp/ssh_ip_tmp.log # 取得要封鎖的IP位址 #blockip=`cat /tmp/ssh_ip_tmp.log` blockip=`cat /tmp/ssh_ip_tmp.log | sort | uniq` # iptables中,找出定義的ruleexistchar中的ip。 #iptables -L INPUT -n | grep 'tcp dpt:22' | grep 'DROP' | awk '{print $4}' | sort | uniq ruleexistip=`iptables -L INPUT -n | grep 'tcp dpt:22' | grep 'DROP' | awk '{print $4}' | sort | uniq` blacklist=`cat '$basedir'/ssd_blacklist_d` for ip in $blacklist do `iptables -D INPUT -p TCP -s $ip --dport 22 -j DROP` > /dev/null done rm -rf '$basedir'/ssd_blacklist_d # 利用 for 迴圈去跑 drop IP for ip in $blockip do # 計算此IP登入幾次失敗 count=`cat /tmp/ssh_ip_tmp.log | grep '$ip' | wc -l` # 如果登入失敗次數大於 預設值 則執行 drop if [ '$count' -gt '$loginCountFail' ] ; then #echo $ip $count # 如果從secure找出的ip已存在於iptables,就不需要加這條rule了。 if [ '$ip' = '$ruleexistip' ]; then : else `iptables -I INPUT -p TCP -s $ip --dport 22 -j DROP` fi # if [ '$ip' = '$ruleexistip' ]; # 建立黑名單 echo '$ip' >> '$basedir'/ssd_blacklist_d cat '$basedir'/ssd_blacklist_d | sort | uniq > '$basedir'/ssd_blacklist_d fi # if [ '$count' -gt '$loginCountFail'] ; done # for loop done # 刪除暫存檔 rm -rf /tmp/ssh*.log mail -s 'ssh hacker 抵擋的IP位址' $mailfor < '$basedir'/ssd_blacklist_d fi # if [ '$failcount' -gt '0' ]; 然後在 vi /etc/hosts.allow 加入 sshd : ALL : spawn /etc/firewall/sshd.sh 參考資料: * http://www.andowson.com/posts/list/33.page * http://ssorc.tw/rewrite.php/read-93.html

- 已使用 Google 工具列寄出"

2011年5月25日 星期三

安裝Tripwire確保系統安全

舊油燈裡的巨人: 安裝Tripwire確保系統安全: "安裝Tripwire確保系統安全

前陣子有個朋友的機器被駭客入侵了,雖然資料都救回來了,但是系統裡到底有沒有殘留的木馬其實很難講,除了用chkrootkit掃掃看有沒有木馬之外, 幫系統做一個指紋系統應該會比掃描木馬的結果來的正確,因為木馬千奇百怪,掃描軟體是不是可以正確掃出來其實還滿難說的,而如果替系統建立一個指紋資料 庫,對重要的檔案產生指紋編碼,當哪天檔案被人竄改時,我們也可以在第一時間察覺,避免威脅擴散。而這篇記錄要說明的就是tripwire的設定方式。網路上有很多關於tripwire的安裝及設定方法,但很神奇的,他們講的都是舊版的,新版的已經不是這樣安裝了,所以僅利用此篇文章紀錄安裝方式,以免日後忘了又找不到相關說明。

Tripwire是一個opensource的專案,不過也有商業版,目前就我所知只有商業版的支援windows系統,而open source的專案則僅適用於Unix-like系統,台灣目前沒有商業版的代理商。

先從http://sourceforge.net/projects/tripwire/下載最新版的tripwire,目前是2.4.1.2的版本,先下載source code版本,就是tripwire-src那個專案。
上傳到機器上之後,由於檔案是bz2壓縮的,所以請先用bunzip2解壓縮:
bunzip2 tripwire-2.4.1.2-src.tar.bz2
再把tar檔案解開:
tar xvf tripwire-2.4.1.2-src.tar
解開後進入tripwire安裝檔資料夾,使用configure設定要安裝的目的:
./configure --prefix=/usr/local/tripwire (讓它安裝在/usr/local底下)
跑完後系統會告訴你是不是缺少什麼東西,沒有的話就直接輸入:
make clean; make
make之後,需要稍微修改一下install.cfg,這個檔案就位於<>\install底下,找到TWMAILPROGRAM,然後將它指到你的sendmail主程式的目錄,例如:
if [ -z '$path_to_sendmail' ] ; then
TWMAILPROGRAM='/usr/sbin/sendmail'
else
TWMAILPROGRAM='${path_to_sendmail} -oi -t'
fi
完成後就可以開始make install了,系統會出現授權條款,看完之後要輸入accept才能繼續安裝。
接下來要設定幾個密碼
Enter the site keyfile passphrase:
Enter the local keyfile passphrase:
將資料庫初始化,執行以下指令對指紋資料庫進行第一次初始化
/usr/local/tripwire/sbin/tripwire -m
檢查etc底下的twcfg.txt檔案,裡面包含了一些tripwire的設定
修改/usr/local/tripwire/etc底下的twpol.txt,這個檔案是tripwire附的設定範例檔,使用者可以針對需要檢測的資料夾進行設定,如果整各系統都要做指紋檔的話,將會花費很長的時間,我們可以針對較重要的資料夾進行控管。
修改完畢後存檔,利用以下的指令更新policy
/usr/local/tripwire/sbin/tripwire -m p -Z low ../etc/twpol.txt
建立一個cron job,定期掃描系統,並將報告寄出
#!/bin/sh
/usr/local/tripwire/sbin/tripwire -m c -M >
然後就看報告了,資訊化的背後最後處理的還是人啊。

- 已使用 Google 工具列寄出"

2011年5月24日 星期二

讓 Spamassassin 學習辨別垃圾郵件

Spamassassin 的 Spam 分數可以由“學習”的方式來調整, 最好的方式是分別建立 blacklist (黑名單, 垃圾郵件) 和 whitelist (白名單, 非垃圾郵件) 的信箱:

CODE:

#useradd -M blacklist

#useradd -M whitelist
以後如果收到垃圾信的話, 就把信 "以附加檔案方式轉寄" 給自己 Server 上的 blacklist 信箱; 如果是被 Spamassassin 誤判為垃圾信的郵件, 就把它轉寄給 whitelist. 也就是說, 讓這兩個信箱收集可以給 Spamassassin 學習的範本. 學習的指令如下:

學習黑名單:

CODE:

#sa-learn -D --showdots --spam --mbox /var/mail/blacklist學習白名單:

CODE:

#sa-learn -D --showdots --ham --mbox /var/mail/whitelist學習完成後, 即可將 blacklist 與 whitelist 信箱清空:

CODE:

#true > /var/mail/blacklist; true > /var/mail/whitelist讓系統每小時自動學習黑/白名單: 於 /etc/cron.hourly 建立一個可執行的 shell 檔, 內容為:

CODE:

#!/bin/sh

if [ -s /var/spool/mail/blacklist ]; then

sa-learn --spam --mbox /var/spool/mail/blacklist > /dev/null 2>&1
true > /var/spool/mail/blacklist

fi

if [ -s /var/spool/mail/whitelist ]; then

sa-learn --ham --mbox /var/spool/mail/whitelist > /dev/null 2>&1
true > /var/spool/mail/whitelist

fi

CentOS 5.3 Mail建置流程1_Postfix+dovect+Mailscanner+Clamav+spamassassin

"更改預設開機run level
vi /etc/inittab
id:5:initdefault: 改成---> id:3:initdefault:
開機直接進入Full multi-user mode(完整含有網路功能的純文字模式)


SSH基本設定
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak #備份設定檔
vi /etc/ssh/sshd_config
AllowUsers sysadm
在檔案最後面加上這一行,這樣就只有sysadm可以使用ssh連線了
若要設定多人,就用空格隔開:AllowUsers sysadmsysadm2
/etc/rc.d/init.d/sshd restart(重新讀取ssh設定檔)

修改IP、FQDN
查看IP:ifconfig
修改IP:/etc/sysconfig/network-scripts/ifcfg-eth0
FQDN 設定:/etc/sysconfig/network
DNS 設定: /etc/resolv.conf
重新啟動網卡:/etc/rc.d/init.d/network restart
ping:ping -c 5 tw.yahoo.com.tw
traceroute:traceroute tw.yahoo.com.tw

關閉SELinux
vi /etc/sysconfig/selinux
SELINUX=enforcing 改成---> SELINUX=disabled

關閉、啟動基本服務
執行ntsysv指令,只留下以下服務:
acpid anacron cpuspeed crond network sshd syslog yum-updatesd
service XXX start #啟動服務
service XXX stop #停止服務
chkconfig XXX on #開機啟動
chkconfig XXX off #開機不啟動

安裝RPMforge
rpm -Uhv http://apt.sw.be/redhat/el5/en/i386/RPMS.dag/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
RPMforge是Dag, Dries和其他的打包者合作的套件庫,提供CentOS超過4000種以上的套件

更新套件
yum -y update

網路校時
yum -y install ntp #安裝NTP
/usr/sbin/ntpdate tock.stdtime.gov.tw #執行網路校時針對tock.stdtime.gov.tw
/usr/sbin/hwclock -w #將時間寫入BIOS
vi /etc/crontab
10 5 * * * root /usr/sbin/ntpdate tock.stdtime.gov.tw && /sbin/hwclock –w #排程自動校時每天5:10校時

RPM基本指令
安裝:rpm -ivh xxx
移除:rpm -e xxx
升級:rpm -Uvh xxx
查詢:rpm -qa | grep xxx

安裝Postfix

1.移除sendmail安裝postfix
/etc/init.d/sendmail stop #停用sendmail
yum install postfix #安裝postfix
rpm -e sendmail #移除sendmail
service postfix start #啟動postfix服務

2. postfix設定
版本: postfix- 2.3.3 -2.1.el5_2
設定檔位置 /etc/postfix/main.cf
設定 postfix 可接收外部連線
vi /etc/postfix/main.cf
將inet_interfaces = all 這一行#號拿掉
將inet_interfaces = localhost 這行前面加上#號註解
mydomain = no-ip.org
myhostname = pororo.no-ip.org
service postfix restart #重新啟動postfix服務
測試連線
[root@mail2 postfix]# telnet 192.168.1.1 25
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.
220 pororo.no-ip.org ESMTP Postfix

3. 設定postfix SMTP 驗證
安裝 cyrus-sasl-md5 cyrus-sasl-plain cyrus-sasl 套件
yum install cyrus-sasl-md5 cyrus-sasl-plain cyrus-sasl
vi /etc/postfix/main.cf #加入下面設定
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
chkconfig saslauthd on #設定開機啟動
service saslauthd restart #啟動saslauthd服務
利用telnet 測式smtp認證功能是否生效(要有出現LOGIN PLAIN才算)
[root@mail2 postfix]# telnet 192.168.1.1 25
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.
220 pororo.no-ip.org ESMTP Postfix
ehlo localhost
250-mail2.pmail.idv.tw
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN

安裝dovecot pop3服務
yum install dovecot #安裝dovecot
vi /etc/dovecot.conf
protocols = imap imaps pop3 pop3s 將#註解拿掉
設定啟動 dovecot 服務,並設定開機自動啟動
chkconfig dovecot on
service dovecot start

安裝MailScanner
官方網站 : http://www.mailscanner.info/
安裝版本 : Mailscanner-4.77.10-1
1. yum install gcc rpm-build perl-MIME-tools #安裝必要套件

2. wget http://www.mailscanner.info/files/4/rpm/MailScanner-4.77.10-1.rpm.tar.gz

3. tar -zxvf MailScanner-4.77.10-1.rpm.tar.gz #解壓縮

4. cd MailScanner-4.77.10-1 #切換至該目錄下

5. ./install.sh #安裝需要一點時間,安裝好後使用sendmail不需特別設定,以下為搭配postfix設定
service postfix stop #停用postfix
chkconfig postfix off # postfix設定開機不啟動
chkconfig MailScanner on #設定開機啟動(MailScanner會自動呼叫postfix Spamassassin&clamav)
service MailScanner start #啟用MailScanner

6. MailScanner和postfix搭配使用時所需的設定:
vi /etc/postfix/main.cf
header_checks = regexp:/etc/postfix/header_checks 將#註解拿掉
vi /etc/postfix/header_checks
/^Received:/ HOLD #加上這一行
這是為了讓Postfix收進來的mail 先儲存在某一個Directory裡面,等待MailScanner 來進行Scan
vi /etc/MailScanner/MailScanner.conf如下:
Run As User = postfix #設定執行postfix 的使用者名稱
Run As Group = postfix #設定執行postfix 的群組名稱
Incoming Queue Dir = /var/spool/postfix/hold #內送郵件的佇列目錄,設定為postfix目前的佇列目錄下的hold目錄
Outgoing Queue Dir = /var/spool/postfix/incoming #外送郵件的佇列目錄,設定為postfix目前的佇列目錄下的incoming目錄
MTA = postfix #設定mailscanner 和 postfix 搭配使用

ps:可利用下面這一串指令查詢
postconf|grep -E 'mail_owner|setgid_group|queue_directory' #執行這一段指令
mail_owner = postfix --->這是執行postfix 的使用者名稱
queue_directory = /var/spool/postfix --->這是postfix的佇列目錄
setgid_group = postdrop --->這是執行postfix的群組名稱

修改權限:
chown postfix.postdrop /var/spool/MailScanner/incoming
chown postfix.postdrop /var/spool/MailScanner/quarantine

註:我在安裝完MailScanner檢視一下Maillog都會出下以下錯誤訊息
MailScanner[2248]: Could not create Processing Attempts Database '/var/spool/MailScanner/incoming/Processing.db'
解決方法: chown postfix.postfix Processing.db

安裝Clamav
套件可以到以下網站來下載 http://packages.sw.be/clamav/
安裝版本clamav-0.95.2-4
1.下載安裝套件
wget http://packages.sw.be/clamav/clamav-0.95.2-4.el5.rf.i386.rpm
wget http://packages.sw.be/clamav/clamav-db-0.95.2-4.el5.rf.i386.rpm
wget http://packages.sw.be/clamav/clamav-devel-0.95.2-4.el5.rf.i386.rpm
wget http://packages.sw.be/clamav/clamd-0.95.2-4.el5.rf.i386.rpm
如果你是使用sendmail的話要多下載clamav-milter <= for sendmail only此套件如果使用postfix就只要下載上面4個rpm就可以了

2.下載完後先將所有舊的clamav移除
yum remove clamav-* #移除,如果先前有安裝舊版本
rpm -ivh clam*.rpm #安裝
freshclam  #更新病毒碼
service clamd start  #啟動服務(有搭配MailScanner就不需要做這一步驟)

3. 設定MailScanner使用的掃毒軟體 vi /etc/MailScanner/MailScanner.conf
將Virus Scanners = auto改為 Virus Scanners = clamav

4. 排程每天0,12點更新病毒碼
vi /etc/crontab
0 0,12 * * * root /usr/bin/freshclam

5. 測試,EICAR病毒測試檔http://tw.trendmicro.com/tw/support/virus-primer/eicar-test-files/

過濾廣告信軟體spamassassin
1.安裝spamassassin
yum install spamassassin
service spamassassin start #啟動服務

2. 修改MailScanner設定檔
vi /etc/MailScanner/MailScanner.conf
Use SpamAssassin = yes #預設是YES 若不搭配spamassassin使用改為no
Required SpamAssassin Score = 6 #超過6等級就視為廣告信,當然可以依照使用者自已設定啦
High SpamAssassin Score = 10 #超過10分列為高等級廣告信
SpamAssassin Auto Whitelist = yes #可以測量從不同位址發出的廣告信及非廣告信的比率如果寄信位的信件大多不為廣告信,系統會自動的增加位址至它本身擁有的非廣告信清單(whitelist)
SpamAssassin Timeout = 75 #處理的郵件的最大等待時間(秒)
Spam Score Character = * #設定分數用*號表示,預設是用s,要如何表示當然由自已設定囉
修改主旨設定(如果被判定為spam時會在信件主旨加上特註記)
Spam Modify Subject = start #no就是不修改主旨
Spam Subject Text = {Spam?} #可以自行修改想要的註記
High Scoring Spam Modify Subject = start
High Scoring Spam Subject Text = {Spam?} #如果被判定為病毒信時會在信件主旨加上特註記)
Virus Modify Subject = start #no就是不修改主旨
Virus Subject Text = {Virus?} #可以自行修改想要的註記
設定spamassass 狀態目錄
SpamAssassin User State Dir = /var/spool/MailScanner/spamassassin (將#取消)
SpamAssassin User State Dir = (加上#)

3.建立目錄
mkdir /var/spool/MailScanner/spamassassin
chown postfix.postdrop spamassassin #修改此目錄權限
MailScanner中的白名單設定方法:
用白名單功能時請請檢查MailScanner.conf 中的白名單設定是否有開啟喔
vi /etc/MailScanner/MailScanner.conf
Is Definitely Not Spam = %rules-dir%/spam.whitelist.rules
vi /etc/MailScanner/rules/spam.whitelist.rules針對你希望不被標示的範圍進行加註
常用語法:
對象:
From: 針對信件的寄件者進行比對
To: 針對信件的收件者進行比對
FromOrTo: 針對寄件者或收件者進行比對
FromAndTo: 針對寄件者及收件者共同進行比對
比對的樣本
user@sub.domain.com 單一的郵件位址
user@* 單一帳號(不限定領域)
*@*.domain.com domain.com下轄 subdomin下的任何帳號
*@donain.com 特定 domain.com 的任何帳號
192.168. SMTP client IP 範圍 192.168.0.0/16
/^192.168.1[4567]./ SMTP client IP 範圍 192.168.14~17
範例
希望今天所有來自 *@*.mlc.edu.tw 及 *@mlc.edu.tw 的信件都不被標示為spam
值為yes表示不會被標記為spam
From: *@*.mlc.edu.tw yes
From: *@mlc.edu.tw yes

- 已使用 Google 工具列寄出"

CentOS yum的詳細使用方法

yum是什麽
yum = Yellow dog Updater, Modified
主要功能是更方便的添加/刪除/更新RPM包.
它能自動解決包的倚賴性問題.
它能便於管理大量系統的更新問題

yum特點
可以同時配置多個資源庫(Repository)
簡潔的配置文件(/etc/yum.conf
自動解決增加或刪除rpm包時遇到的倚賴性問題
使用方便
保持與RPM數據庫的一致性

yum安裝
CentOS自帶(yum-*.noarch.rpm)
#rpm -ivh yum-*.noarch.rpm
在第一次啟用yum之前首先需要導入系統的RPM-GPG-KEY:
#rpm --import /usr/share/doc/centos-release-3(4)/RPM-GPG-KEY-CentOS-3(4)

yum指令
註:當第一次使用yum或yum資源庫有更新時,yum會自動下載所有所需的headers放置於/var/cache/yum目錄下,所需時間可能較長.

rpm包的更新
檢查可更新的rpm包
#yum check-update
更新所有的rpm包
#yum update
更新指定的rpm包,如更新kernel和kernel source
#yum update kernel kernel-source
大規模的版本升級,與yum update不同的是,連舊的淘汰的包也升級
#yum upgrade

rpm包的安裝和刪除
安裝rpm包,如xmms-mp3
#yum install xmms-mp3
刪除rpm包,包括與該包有倚賴性的包
#yum remove licq
註:同時會提示刪除licq-gnome,licq-qt,licq-text

yum暫存(/var/cache/yum/)的相關參數
清除暫存中rpm包文件
#yum clean packages
清除暫存中rpm頭文件
#yum clearn headers
清除暫存中舊的rpm頭文件
#yum clean oldheaders
清除暫存中舊的rpm頭文件和包文件
#yum clearn 或#yum clearn all
註:相當於yum clean packages + yum clean oldheaders

包列表
列出資源庫中所有可以安裝或更新的rpm包
#yum list
列出資源庫中特定的可以安裝或更新以及已經安裝的rpm包
#yum list mozilla#yum list mozilla*
註:可以在rpm包名中使用匹配符,如列出所有以mozilla開頭的rpm包
列出資源庫中所有可以更新的rpm包
#yum list updates
列出已經安裝的所有的rpm包
#yum list installed
列出已經安裝的但是不包含在資源庫中的rpm包
#yum list extras
註:通過其它網站下載安裝的rpm包

rpm包信息顯示(info參數同list)
列出資源庫中所有可以安裝或更新的rpm包的信息
#yum info
列出資源庫中特定的可以安裝或更新以及已經安裝的rpm包的信息
#yum info mozilla#yum info mozilla*
註:可以在rpm包名中使用匹配符,如列出所有以mozilla開頭的rpm包的信息
列出資源庫中所有可以更新的rpm包的信息
#yum info updates
列出已經安裝的所有的rpm包的信息
#yum info installed
列出已經安裝的但是不包含在資源庫中的rpm包的信息
#yum info extras
註:通過其它網站下載安裝的rpm包的信息

搜索rpm包
搜索匹配特定字符的rpm包
#yum search mozilla
註:在rpm包名,包描述等中搜索
搜索有包含特定文件名的rpm包
#yum provides realplay

增加資源庫
例如:增加rpm.livna.org作為資源庫
安裝Livna.org rpms GPG key
#rpm --import http://rpm.livna.org/RPM-LIVNA-GPG-KEY
檢查GPG Key
# rpm -qa gpg-pubkey*
顯示Key信息
#rpm -qi gpg-pubkey-a109b1ec-3f6e28d5
(註:如果要刪除Key,使用#rpm -e gpg-pubkey-a109b1ec-3f6e28d5)

yum常用的命令

# yum install xxx 安裝xxx軟件

# yum info xxx 查看xxx軟件的信息

# yum remove xxx 刪除軟件包

# yum list 列出軟件包

# yum clean 清除緩衝和就的包

# yum provides xxx 以xxx為關鍵字搜索包(提供的信息為關鍵字)

# yum search xxx 搜索軟件包(以名字為關鍵字)

# yum groupupdate xxx

# yum grouplist xxx

# yum groupremove xxx

這三個都是一組為單位進行升級 列表和刪除的操作。。比如 'Mysql Database'就是一個組會同時操作相關的所有軟件包;

# yum update 系統升級

# yum list available 列出所有升級源上的包;

# yum list updates 列出所有升級源上的可以更新包;

# yum list installed 列出已經安裝的包;

# yun update kernel 升級內核;

yum常用的源

1) 自動選擇最快的源

由於yum中有的mirror速度是非常慢的,如果yum選擇了這個mirror,這個時候yum就會非常慢,對此,可以下載fastestmirror插件,它會自動選擇最快的mirror:

#yum install yum-fastestmirror

配置文件:(一般不用動)/etc/yum/pluginconf.d/fastestmirror.conf

你的yum鏡像的速度測試記錄文件:/var/cache/yum/timedhosts.txt

(2)使用圖形界面的yum

如果覺得命令行的yum不方便,那麽可以使用圖形化的yumex,這個看起來更方便,因為可以自由地選擇軟件倉庫:

#yum install yumex

然後在系統工具中就可以看到yum extender了。實際上系統自帶的“添加/刪除程序“也可以實現圖形化的軟件安裝,但有些yumex的功能它沒有。

- 已使用 Google 工具列寄出"

2011年5月23日 星期一

為 Linux 安裝 telnet 遠端連線系統

為 Linux 安裝 telnet 遠端連線系統

☆說明☆
遠端連線伺服器對一般管理員來說是非常方便又好用的工具,若你的伺服器位在不同的地方,透過遠端管理系統,只要一台電腦就能管理數個不同位置的伺服器系統,但安全性稍低,所以有需要的人要好好考慮啊~

☆telnet 安裝步驟☆

1.利用 YUM 安裝 telnet
指令:yum -y install telnet telnet-server

2. 確認系統是否有載入 telnet
指令:rpm -qa telnet | grep telnet
項目:telnet-0.17-39.el5
telnet-server-0.17-39.el5

3.修改 telnet
指令:vi /etc/xinetd.d/telnet
預設:disable = yes
修改:disable = no

4.啟動、停止方法
啟動指令:service xinetd start
停止指令:service xinetd stop

5.設定開機啟動 telnet 或 開機停止啟動 telnet
啟動指令:chkconfig xinetd on
停止指令:chkconfig xinetd off

2011年5月19日 星期四

Linux讓記錄檔內容歸零的方法

Linux讓記錄檔內容歸零的方法

如果要將記錄內容清空,移除再建立是一個方法,但不是很好的方法,可以採用下面這個指令

cat /dev/null > /var/log/log_filename

例如:

# cat /dev/null > /var/log/xferlog

# cat /dev/null > /var/log/httpd-error.log

就可以輕鬆的將記錄檔清空
END

- 已使用 Google 工具列寄出"

解決 SSH 登入延遲

解決 SSH 登入延遲

sshd login delay 登入要等很久,才會跳出輸入密碼的視窗,怎麼解決?

後來看到這一篇的解決法,也順利了解決了,真是感謝。

修改 /etc/ssh/sshd_config

# linux 預設是 yes

UseDNS no

- 已使用 Google 工具列寄出"

SSH 限制登入的帳號或群組 次要群組

"SSH 限制登入的帳號或群組 次要群組 [網際技術::IPv6] 列印 推薦
作者或來源 瘦河馬 2010-02-12 11:52:18
關鍵字 1ssh 2網路安全
此文完整連結 http://note.tcc.edu.tw/375.html


SSH 限制登入的帳號或群組

在unix系統中常用的SSH,常會遭到駭客的掃描和暴力法破解密碼,就算密碼夠強,我還是希望駭客一點機會也沒有,所以要限制可以登入的使用者和來源 IP/ IP6

修改 /etc/ssh/sshd_config

允許root從192.168.1.32登入:
AllowUsers root@192.168.1.32
如果要讓 root 只能從192.168.1.32登入,找到這行並修改如下:
PermitRootLogin yes

允許admin 任何地方登入,只需列出用戶名:
AllowUsers admin

可以在同一行指定多個帳戶登入用,例如:
AllowUsers root@192.168.1.32 admin

允許整個網段登入
AllowUsers root@10.200.*

注意IPv6 的寫法
AllowUsers axer@163.* axer@120.109.* axer@2001:288:*

只有允許群組 user 登入

AllowGroups user

設定完記得重啟 ssh

[LINUX] # service sshd restart
[FREEBSD] # /etc/rc.d/sshd reload

* 重啟 ssh 後原有連線不會斷,請再開連線測試,確定正確再關閉現有連線,否則設定錯誤,機器又在遠端的話,就沒救了
END

- 已使用 Google 工具列寄出"

限制一般 user 變成 root

限制一般 user 變成 root: "限制一般 user 變成 root

說明文件

man pam
pinfo pam

在 Linux 下,系統預設大家都可以變成超級使用者-root,玩過 Unix-Like 的人,也都知道 root 的權限之大。

有一天,小弟在一個偶然的機會裡,能有機會管理 FreeBSD 系統,結果發現不是每個人都是可以變成 root 的,也就是說,不是每個人都可以使用 su 轉換成別人的帳號,如果要可以使用 su 的功能,就必需為 wheel 的群組。

後來想一想,雖然 FreeBSD 有那麼一點點的不方便,但相對的,也是增加了一些安全的機制。而 Linux 現在也可以使用這種機制,限制非 wheel 的 group 都無法使用 wheel,然而,這就要使用 pam 的機制來幫我們實現了。現在我們就來看看如何讓這種機制在 Linux 中也可以使用!

讓系統中的人員中,只有 steven 才能使用 su。

編輯 /etc/pam.d/su,並把 pam_wheel.so 引用進來。
[root@rhel pam.d]# vi /etc/pam.d/su
_____________________________________________________
auth required /lib/security/$ISA/pam_wheel.so use_uid
:____________________________________________________


不用重開,什麼都不用,立既生效!不信的話,使用 steven 帳號看看是否還有能力變成 root 或其它帳號!
[steven@rhel steven]$ su - test
Password:
su: incorrect password
[steven@rhel steven]$ su -
Password:
su: incorrect password
[steven@rhel steven]$

看到了吧,就算變成一般的 test 或是 root 都無法讓你登入。

那麼要如何讓 steven 可以使用 su 呢?其實只要把 steven 加入 wheel 群組就可以了!
[root@rhel pam.d]# usermod -G wheel steven

Try again!看看 steven 是不可以使用 su 來切換使用者身份!
[steven@rhel steven]$ su -
Password:
[root@rhel root]#

看,這不就過了嗎?

雖然這樣是有點麻煩,但是確可以讓系統多了一些安全機制,其實安全性與方便是成反比的,越方便就越不安全,然而在不同的主機上,端看管理者如何去應用了。

- 已使用 Google 工具列寄出"
限制 root 不能使用ssh :Linux主機設定SSH限制
說明:
1.限制 root 不可使用ssh :
因為開放root就等於開放大門給有心人來試 root的密碼,如果猜對的話!
你的主機就變成對方的工具了!!

2.限制使用SU指令:
限制登入的使用者使用SU這指令,可以防止使用者用SU 變成root

3.設定某帳號才能使用ssh:
可以做控管,開放了哪些帳號可以用ssh登入主機

方法:
1.
編輯sshd_config檔
vi/etc/ssh/sshd_config
設定限制root不能使用ssh
將PermitRootLogin “YES” 改成 PermitRootLogin “NO”
存檔離開

2.
編輯su
vi /etc/pam.d/su
#auth required /lib/security/$ISA/pam_whelle.so use_uid
將前面的”#”刪除 存檔離開
設定只有wheel群組的成員才能使用su命令
usermod -G 10 帳號

3.
編輯sshd_config
vi/etc/ssh/sshd_config
在最後加入 AllowUsers 帳號

將上面設定好之後,重新讀取sshd
service sshd restart
----------------------------------------------------
Linux主機設定-SSH限制只能用protocol 2連線的設定
限制只能用SSH protocol 2連線的方式
1.登入主機後,
pico -w /etc/ssh/sshd_config

2.找到有井號的Protocol 2, 1等文字,然後移除井號及,1等文字之後,變成下面這樣
Protocol 2

3.儲存修正資料然後重新啟動SSH
/etc/rc.d/init.d/sshd restart

從此以後你就必須用SSH Protocol 2才能連上主機
---------------------------------------------
Linux主機設定-Apache修改與重新起動

1.登入主機後, 修改http.conf參數檔
vi /etc/httpd/conf/http.conf

2.重新起動 Apache 程式
/etc/rc.d/init.d/httpd restart

ref: http://apache.org
--------------------------------------------------
Linux主機設定-DNS與Bind修改與重新起動

1.登入主機後, 修改named.conf參數檔
vi /etc/named.conf

2.重新起動 Bind 程式
/etc/rc.d/init.d/named restart

ref: http://www.isc.org
---------------------------------
[文件]Linux主機設定-MySQL參數修改與重新起動

1.登入主機後, 修改my.cnf參數檔
vi /etc/my.cnf

2.重新起動 MySQL 程式
/etc/rc.d/init.d/mysql restart

ref: http://mysql.org

[文件]Linux主機設定-Mail_Exim參數修改與重新起動

1.登入主機後, 修改exim.conf參數檔
vi /etc/exim.conf

2.重新起動 Mail 程式
/etc/rc.d/init.d/exim restart

ref: http://exim.org
--------------------------------------------
限制root直接SSH登入
1.確認WHM裡已將另一位使用者名稱加入wheel裡

2.用pico修改sshd_config
pico -w /etc/ssh/sshd_config

3.尋找下列字串
#PermitRootLogin yes

4.移除井號並將yes改為no,修正後如下
PermitRootLogin no

5.重新啟動ssh

6.SSH登入時請使用已經加入wheel的帳號及密碼,再以su -變成管理者。