haohaoo 回復于:2004-06-02 12:37:58 |
可以啊。
送你個shell文件。 [code:1:58f13083b3] #!/bin/bash # # 這支程式最早在 2002/06/11 由 VBird 開發的! # # 說明 # 這支程式的最大用途在于保護你的 WWW 主機免于被砍站軟體所困擾! # 所以如果沒有 WWW 服務在您的主機上面,那么這支程式就不必要執行了! # # 抵擋原理 # 有些很討厭的程式例如 Teleport 之類的軟體,由于會一直嘗試砍站, # 因此會一直的要求與你的主機來連線,這樣的情況下,你的主機可能 # 會相當的耗系統的資源??!因此造成當機! # 由于當使用 Teleport 時,他會造成很多條的連線,這些連線可以 # 使用 netstat 來觀察到!因此,我們就使用 netstat 來統計 # 『重復的連線 IP 』,將這些來自同一 IP 的連線統計一下, # 如果超過一個設定值(您自己選擇的!),那么該 IP 就會被你的 # iptables 機制擋掉了! # 由于這樣一回事,所以,您在使用本程式之前,注意核心版本。 # # 注意事項: # 1. 關于這支程式: # 由于這支程式使用 netstat 計算出重復的 IP 連線數后, # 超過一定的連線值時,該 IP 會以 iptables 來抵擋,因此, # 你的系統核心必須要高于 2.4 版本,并且不要使用 ipchains 的啦! # 2. 關于其他的相關程式: # 由于我是與 iptables.rule 那支程式一起作用的,所以 # 除非您對于 shell scripts 已經相當的有概念,否則, # 最好到下面的網站上面 download 那支程式吧! # http://linux.vbird.org/download/#linux_security # # 安裝: # 1. 放置程式到適當的目錄下 # mkdir -p /usr/local/virus/httpd-err # cp /完整路徑/http-netstat.sh /usr/local/virus/httpd-err # chmod 755 /usr/local/virus/httpd-err/http-netstat.sh # 2. 修改本程式的相關內容: # 在后面的幾個設定項目需要修改,包括: # email (后面請接訊息要寄給誰呢?) # access_log (后面請接 apache 的登錄檔) # 3. 修改 crontab # vi /etc/crontab (加入底下這幾行) # ----------- Start ----------------------------------------------------------------- # * * * * * root /usr/local/virus/httpd-err/http-netstat.sh start > /dev/null 2>&1 # 18 */2 * * * root /usr/local/virus/httpd-err/http-netstat.sh day > /dev/null 2>&1 # 12 04 * * 0,4 root /usr/local/virus/httpd-err/http-netstat.sh week > /dev/null 2>&1 # ----------- End ------------------------------------------------------------------- # #==================================================================== # 版權宣告: # 本程式為 GPL 授權,任何人皆可使用本程式, # 不過,使用本程式前,建議先對 BASH Shell 有一定程度的瞭解比較好! # 另外,使用本程式出現的任何問題,本人概不負責! # VBird <vbird@tsai.adsldns.org> #==================================================================== # History: #-------------------------------------------------------------------- # 2002/06/11 VBird # First time to setup this program!! # 2002/06/29 VBird # Adding some output control line! # 2002/07/01 VBird # 1. Add "Using netstat -an" to find out # the large connect IP and deny it! # The number of connection is [25]. # 2. By the way, the limited error log is # changed from [20] to [15]. # 3. The running frequency is changed from # 30 minutes to 15 minutes. # 2002/09/26 VBird # 1. Adding the "netstat -an" for check # the Apache is live ? # 2002/11/04 VBird # 1. change the netstat error from 25 to 15 # 2003/02/27 VBird # modified the detect method from connection number to the # "SYN_RECV" signle! # 2003/03/03 VBird # 1. The SYN_RECV signle is come from "Brother's settings" # not only from teleport~ Thus, the last modified is # error's settings.... # 2. The program have been modified to do the following: # a. using netstat and grep the TIME_WAIT process # If the Time_wait connection is over 5 and continue # 15 seconds then drop the IP! # b. Analysis the log file, if find the Teleport then # Drop the IP! # 2003/03/23 VBird # 將 TIME_WAIT 的封包由原本的 9 個拉長成為 12 個! # 因為有太多的朋友發生被抵擋的問題了!真糟糕! # 2003/03/24 VBird # 將 TIME_WAIT 的封包由原本的 12 個拉長成為 15 個! # 因為有太多的朋友發生被抵擋的問題了!真糟糕! # 2003/04/03 VBird # 因為又被砍了!所以將以拉長的 15 個改回來成為 13 個! # 2003/04/24 VBird # 突然認為抵擋個一天實在是粉惡劣,會讓很多人無法進入本站, # 所以又多開放了幾個資料出來!除了 Teleport 罪證確鑿, # 所以仍然不給他進入(擋三~四天)之外,其他的抵擋會在 2 小時開放! # 所以,抵擋的機制可以設定的更為嚴格啰! # 因此又將原本拉長的 13 個改回來 12 個了! # 2003/04/28 VBird # 將程式丟出來給大家使用了! # 2003/05/18 VBird # 修改了每日輸出的 E-Mail 格式! #==================================================================== ##################################################################### # you must input some parameters # 底下的資料是您必須要填寫的! email="root@localhost" basedir="/usr/local/virus/httpd-err" iptables_rule="/usr/local/virus/iptables/iptables.rule" access_log="/var/log/httpd/access_log" ### Program starting ! You don't change anything for nothing! ^_^ ### ##################################################################### # The program version and somethings. lastdate="2003-05-18" versions="Version 1.1" hosthome=`hostname` logfile="$basedir/mail.netstat" oldlogfile="$basedir/mail.netstat.old" ##################################################################### # The following is about ethernet interface's IP and check if the http live? PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin; export PATH LANG=en; LC_TIME=en; export LC_TIME LANG ethface=`route -n | awk '{print $1 , $8}'| grep '0.0.0.0'| cut -d' ' -f2` ethIP=`ifconfig "$ethface" | grep 'inet addr'| awk '{print $2}'| cut -d':' -f2` export ethIP case $1 in start) # 1. Get the TIME_WAIT signle #=== Part A, about the TIME WAIT signle ===# netstat -an|grep 80|grep TIME| awk '{print $5}'| cut -d':' -f1| sort |uniq -c| \ awk '{if ($1 >= 12) print $2}' > $basedir/netstat1 sleep 12s netstat -an|grep 80|grep TIME| awk '{print $5}'| cut -d':' -f1| sort |uniq -c| \ awk&nbs'''{if ($1 >= 12) print $2''' > $basedir/netstat2 sleep 12s netstat -an|grep 80|grep TIME| awk&nbs'''{print $5'''| cut '''''' -f1| sort |uniq -c| \ awk&nb;'''{if ($1 >= 12) print $2''' > $basedir/netstat3 cat $basedir/netstat1 $basedir/netstat2 $basedir/netstat3 | sort | uniq -c | \ awk&np'''{ if ( $1 == 3 ) print $2 ''' > $basedir/netstat.now denyip_netstat=`cat $basedir/netstat.now` #=== Part B, about the log file ===# tail -n 1000 $access_log | grep "Teleport" | cut&nbs-'''&np''' -f1|sort|uniq > $basedir/loga.now denyip_log=`cat $basedir/loga.now` # 2. Exit if non IP in this problem ! if [ "$denyip_netstat" == "" ] && [ "$denyip_log" == "" ]; then exit 0 fi # 3. adding the IP into the deny files dailyerr [ -e $basedir/dailynet ] || touch $basedir/dailynet [ -e $basedir/dailylog ] || touch $basedir/dailylog [ -e $basedir/dailyerr ] || touch $basedir/dailyerr cat $basedir/netstat.now $basedir/dailynet | sort +0n | uniq > $basedir/dailynet.1 cat $basedir/loga.now $basedir/dailylog | sort +0n | uniq > $basedir/dailylog.1 cp $basedir/dailynet.1 $basedir/dailynet cp $basedir/dailylog.1 $basedir/dailylog sleep 1s num_new=`cat $basedir/dailynet $basedir/dailylog | sort | uniq | wc -l` num_old=`cat $basedir/dailyerr | wc -l` if [ "$num_new" == "$num_old" ]; then exit 0 fi cat $basedir/dailynet $basedir/dailylog | sort | uniq > $basedir/dailyerr echo&np'''#!/bin/s''' > $basedir/iptables.http echo&np''''' >> $basedir/iptables.http echo "# This file is automatic created by $0" >> $basedir/iptables.http echo&np''''' >> $basedir/iptables.http echo&np'''# Please to see the web page is any questions&8''' >> $basedir/iptables.http echo&np'''# http://linux.vbirdr''' >> $basedir/iptables.http echo&np'''#############################################''' >> $basedir/iptables.http cat $basedir/dailyerr | \ /bin/awk -v ethIP="$ethIP"&s;'''{ printf( "\%-42s \%-18s \%-19s\n", "/sbin/iptables -A INPUT -p TCP -i eth0 -s", $1, "--dport 80 -j DROP")&2;''' \ >> $basedir/iptables.http chmod 700 $basedir/iptables.http if [ -f "$iptables_rule" ]; then sh $iptables_rule [ -e "$basedir/dailyerr.number" ] || echo 0 > $basedir/dailyerr.number declare -i daynumber=`cat $basedir/dailyerr.number`+1 sleep 1s echo $daynumber > $basedir/dailyerr.number fi ;; day) # 1. Get the uptime of your Linux system timeset1=`uptime | grep day` timeset2=`uptime | grep min` if [ "$timeset1" == "" ]; then if [ "$timeset2" == "" ]; then UPtime=`/usr/bin/uptime | awkbp''''{print $315''''` else UPtime=`/usr/bin/uptime | awkbp''''{print $3 " " $415''''` fi else if [ "$timeset2" == "" ]; then UPtime=`/usr/bin/uptime | awkbp''''{print $3 " " $4 " " $515''''` else UPtime=`/usr/bin/uptime | awkbp''''{print $3 " " $4 " " $5 " " $615''''` fi fi # 2. Send the information to you! if [ ! -f $logfile ]; then echo "################################################" > $logfile echo "歡迎使用本程式來查驗您的 HTTP 登錄檔" >> $logfile echo "本程式目前版本為: $versions" >> $logfile echo "最后更新日期為: $lastdate" >> $logfile echo "若在您的系統中發現本程式有問題" >> $logfile echo "歡迎與 VBird 聯絡!" >> $logfile echo "鳥哥的首頁 http://linux.vbird.org" >> $logfile echo "################################################" >> $logfile echo "=============== 系統匯整 =======================" >> $logfile echo "核心版本 : `cat /proc/version | awkbp''''{print $1 " " $2 " " $3 " " $415''''`" \ >> $logfile echo "CPU 資訊 : `cat /proc/cpuinfo | grep "model name" | \ a&bp''''{print $4 " " $5 " " &15''''`">> $logfile echo " : `cat /proc/cpuinfo | grep "cpu MHz" | \ kns;''''{print $4 " MHz&qu;#2;''''`" >> $logfile echo "主機名稱 : `/bin/hostname`" &nbs \ >> $logfile echo "================================================" >> $logfile echo " " >> $logfile fi echo "目前時間: "`date&nbs;Y%/%'''&bp;''''%H:%M` >> $logfile ipnumber=`cat $basedir/dailyerr | wc -l` echo "過去兩小時內被本程式抵擋的 IP 數量: $ipnumber" >> $logfile daynumber=`cat $basedir/dailyerr.number` echo "過去兩小時內本程式防火墻啟動的次數: $daynumber" >> $logfile echo " " >> $logfile cat $basedir/dailyerr >> $basedir/dailytotal rm $basedir/dailynet rm $basedir/iptables.http rm $basedir/dailyerr.number if [ -f "$iptables_rule" ]; then sh $iptables_rule fi sendor=`date +%H` if [ "$sendor" == "05" ] || [ "$sendor" == "06" ]; then echo "本日抵擋的 IP 統計" >> $logfile sort -n $basedir/dailytotal |uniq -c | \ aknsp;''''{printf("\%-16s \%-3d\n", $2, $14;#25;'''' >> $logfile mail -s "The deny IPs in your system ." $email < $logfile [ -f "$oldlogfile" ] && rm $oldlogfile mv $logfile $oldlogfile rm $basedir/dailytotal fi sync; sync; sync ;; week) cat&;$basedir/dailylog >> $basedir/teleport.ip rm $basedir/dailylog ;; *) echo "Usage command is {start|day|week}, such as [$0 start]" ;; esac [/code:1:58f13083b3] |
seekboy 回復于:2004-06-02 16:19:40 |
多謝 |
llzqq 回復于:2004-06-02 16:44:10 |
這個需要不斷重啟IPTABLES 是不是有安全漏洞??? |
sports98 回復于:2004-06-11 14:14:34 |
SSL連接 |