iptables日誌與limit參數

在處理工做問題的時候須要查看防火牆的日誌,因爲默認日誌都是在系統日誌裏/var/log/messages裏面。須要對rsyslog作設置。php

首先編輯配置文件/etc/rsyslog.conf以下:html

# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none                /var/log/messages

# The authpriv file has restricted access.
authpriv.*                                              /var/log/secure

# Log all the mail messages in one place.
mail.*                                                  -/var/log/maillog


# Log cron stuff
cron.*                                                  /var/log/cron

# Everybody gets emergency messages
*.emerg                                                 *

# Save news errors of level crit and higher in a special file.
uucp,news.crit                                          /var/log/spooler

# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log
#keepalived -S 0
local0.*                                                /var/log/keepalived.log
#iptables kern.warning /var/log/iptables.log #添加項
# ### begin forwarding rule ###

而後按照iptables官方說明添加參數啓動log日誌。linux

Iptables default log file
For example, if you type the following command, it will display current iptables log from /var/log/messages file: 
# tail -f /var/log/messages 
Output: 
Oct  4 01:14:19 debian kernel: IN=ra0 OUT= MAC=00:17:9a:0a:f6:44:00:08:5c:00:00:01:08:00 SRC=200.142.84.36 DST=192.168.1.2 LEN=60 TOS=0x00 PREC=0x00 TTL=51 ID=18374 DF PROTO=TCP SPT=46040 DPT=22 WINDOW=5840 RES=0x00 SYN URGP=0 
Oct  4 00:13:55 debian kernel: IN=ra0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:18:de:55:0a:56:08:00 SRC=192.168.1.30 DST=192.168.1.255LEN=78 TOS=0x00 PREC=0x00 TTL=128 ID=13461 PROTO=UDP SPT=137 DPT=137 LEN=58 
Procedure to log the iptables messages to a different log file 
Open your /etc/syslog.conf file: 
# vi /etc/syslog.conf 
Append following line : 
kern.warning /var/log/iptables.log
Save and close the file. 
Restart the syslogd (Debian / Ubuntu Linux): 
# /etc/init.d/sysklogd restart On the other hand, use following command to restart syslogd under Red Hat/Cent OS/Fedora Core Linux:

# /etc/init.d/syslog restart 
Now make sure you pass the log-level 4 option with log-prefix to iptables. For example: 
# DROP everything and Log it 
iptables -A INPUT -j LOG -–log-level 4 
iptables -A INPUT -j DROP 
For example, drop and log all connections from IP address 64.55.11.2 to your /var/log/iptables.log file: 
iptables -A INPUT -s 64.55.11.2 -m limit --limit 5/m --limit-burst 7 -j LOG -–log-prefix ‘** HACKERS **’ --log-level 4 
iptables -A INPUT -s 64.55.11.2 -j DROP 
Where, 
    * --log-level 4: Level of logging. The level # 4 is for warning. 
    * --log-prefix ‘*** TEXT ***’: Prefix log messages with the specified prefix (TEXT); up to 29 letters long, and useful for distinguishing messages in the logs. 
You can now see all iptables message logged to /var/log/iptables.log file: 
# tail -f /var/log/iptables.log

例如

iptables -t nat -A POSTROUTING -s 192.168.10.128/25 -o eht0 -j MASQUERADE
iptables -A FORWARD -p tcp -j LOG --log-level info --log-tcp-optinos
iptables -A input -i eth0 -p tcp -j LOG --log-level info --log-prefix "IPTABLES TCP-IN:"

翻譯:tcp

打開你的/etc/syslog.conf文件:
# vi /etc/syslog.conf

在文件末尾加入下面一行信息
kern.warning /var/log/iptables.log

保存和關閉文件.

從新啓動syslogd(若是你使用Debian/Ubuntu Linux):
# /etc/init.d/sysklogd restart

另外, 使用下面命令從新啓動syslogd(若是你使用Red Hat/Cent OS/Fedora Core Linux):
# /etc/init.d/syslog restart

如今確認你的iptables使用了log-level 4參數(前面有一個log-prefix標誌). 例如:
# DROP everything and Log it
iptables -A INPUT -j LOG –log-level 4
iptables -A INPUT -j DROP

舉一個例子, 丟棄和記錄全部來自IP地址65.55.11.2的鏈接信息到/var/log/iptables.log文件.
iptables -A INPUT -s 64.55.11.2 -m limit --limit 5/m --limit-burst 7 -j LOG –log-prefix ‘** HACKERS **’ --log-level 4
iptables -A INPUT -s 64.55.11.2 -j DROP

命令解釋:

. - log-level 4: 記錄的級別. 級別4爲警告(warning).
. - log-prefix ‘*** TEXT ***’: 這裏定義了在日誌輸出信息前加上TEXT前綴. TEXT信息最長能夠是29個字符, 這樣你就能夠在記錄文件中方便找到相關的信息.

如今你能夠經過/var/log/iptables.log文件參考iptables的全部信息:
# tail -f /var/log/iptables.log

實際命令ui

iptables -I INPUT -s 116.3.248.157 -m limit --limit 5/m --limit-burst 5 -j LOG --log-prefix "***TEXT***" --log-level 4
iptables -I INPUT -p tcp --dport 18999 -j ACCEPT

-m --limit命令,是爲了限制每分鐘產生的通行證,--limit-burst 5 設定了通行證的上限。好比,一共有5個通行證,符合的將被放行,不符合的只能等重新生成。生成上限也是5,不能超過5.spa

此處使用limit是爲了防止日誌過多刷屏,也可利用limit防止DDOS攻擊。.net

看到一篇文章解釋很形象,以下:翻譯

Limit match
    這個匹配操做必須由-m limit明確指定才能使用。有了他的幫助,就能對指定的規則的日誌數量加以限制,以避免你被信息的洪流淹沒哦。好比,你能事先設定一個限定值,當符合條件的包的數量不超過他時,就記錄;超過了,就不記錄了。咱們能控制某條規則在一段時間內的匹配次數(也就是能匹配的包的數量),這樣就可以減小DoS syn flood攻擊的影響。這是他的主要做用,固然,更有很是多其餘做用(注:好比,對於某些不經常使用的服務能限制鏈接數量,以避免影響其餘服務)。limit match也能用英文感嘆號取反,如:-m limit ! --limit 5/s表示在數量超過限定值後,全部的包都會被匹配。
    limit match的工做方式就像一個單位大門口的保安,當有人要進入時,須要找他辦理通行證。早上上班時,保安手裏有必定數量的通行證,來一我的,就簽發一個,當通行證用完後,再來人就進不去了,但他們不會等,而是到別的地方去(在iptables裏,這至關於一個包不符合某條規則,就會由後面的規則來處理,若是都不符合,就由缺省的策略處理)。但有個規定,每隔一段時間保安就要簽發一個新的通行證。這樣,後面來的人若是恰巧遇上,也就能進去了。若是沒有人來,那通行證就保留下來,以備來的人用。若是一直沒人來,可用的通行證的數量就增長了,但不是無限增大的,最多也就是剛開始時保安手裏有的那個數量。也就是說,剛開始時,通行證的數量是有限的,但每隔一段時間就有新的通行證可用。limit match有兩個參數就對應這種狀況,--limit-burst指定剛開始時有多少通行證可用,--limit指定要隔多長時間才能簽發一個新的通行證。要注意的是,我這裏強調的是「簽發一個新的通行證」,這是以iptables的角度考慮的。在你本身寫規則時,就要從這個角度考慮。好比,你指定了--limit 3/minute --limit-burst 5 ,意思是開始時有5個通行證,用完以後每20秒增長一個(這就是從iptables的角度看的,要是以用戶的角度看,說法就是每一分鐘增長三個或每分鐘只能過三個)。你要是想每20分鐘過一個,只能寫成--limit 3/hour --limit-burst 5,也就是說你要把時間單位湊成整的。

Table 1?1. Limit match options
Match
--limit
Example
iptables -A INPUT -m limit --limit 3/hour
Explanation
爲limit match設置最大平均匹配速率,也就是單位時間內limit match能匹配幾個包。他的形式是個數值加一個時間單位,能是/second /minute /hour /day 。默認值是每小時3次(用戶角度),即3/hour ,也就是每20分鐘一次(iptables角度)。
Match
--limit-burst
Example
iptables -A INPUT -m limit --limit-burst 5
Explanation
這裏定義的是limit match的峯值,就是在單位時間(這個時間由上面的--limit指定)內最多可匹配幾個包(因而可知,--limit-burst的值要比--limit的大)。默認值是5。
 
假設以下的規則:
iptables -A INPUT -p icmp -m limit --limit 6/m --limit-burst 5 -j ACCEPT
iptables -P INPUT DROP
而後從另外一部主機上ping這部主機,就會發生以下的現象:
首先咱們能看到前四個包的迴應都很是正常,而後從第五個包開始,咱們每10秒能收到一個正常的迴應。這是由於咱們設定了單位時間(在這裏是每分鐘)內容許經過的數據包的個數是每分鐘6個,也即每10秒鐘一個;其次咱們又設定了事件觸發閥值爲5,因此咱們的前四個包都是正常的,只是從第五個包開始,限制規則開始生效,故只能每10秒收到一個正常回應。
假設咱們中止ping,30秒後又開始ping,這時的現象是:
前兩個包是正常的,從第三個包開始丟包,這是由於在這裏個人容許一個包經過的週期是10秒,若是在一個週期內系統沒有收到符合條件的包,系統的觸發值就會恢復1,因此若是咱們30秒內沒有符合條件的包經過,系統的觸發值就會恢復到3,若是5個週期內都沒有符合條件的包經過,系統都觸發值就會徹底恢復。
關於iptables的基本介紹能參見:
Linux iptables 應用手冊(-):
http://fanqiang.chinaunix.net/system/linux/2006-06-20/4595.shtml
Linux iptables 應用手冊(二):
http://fanqiang.chinaunix.net/system/linux/2006-06-20/4596.shtml
Linux iptables 應用手冊(三):
http://fanqiang.chinaunix.net/system/linux/2006-06-20/4597.shtml
Linux iptables 應用手冊(四):
http://fanqiang.chinaunix.net/system/linux/2006-06-20/4598.shtml
Linux iptables 應用手冊(五):
http://fanqiang.chinaunix.net/system/linux/2006-06-20/4599.shtml
Linux iptables 應用手冊(六):
http://fanqiang.chinaunix.net/system/linux/2006-06-20/4600.shtml

關於iptables拓展功能.例如:comment (備註匹配) ,string(字符串匹配,能用作內容過濾),iprang(ip範圍匹配),time(時間匹配),ipp2p(點對點匹配),connlimit(同時鏈接個數匹配),Nth(第n個包匹配),geoip(根據國家地區匹配). ipp2p(點對點匹配), quota(配額匹配)等等,參見:
http://bbs.chinaunix.net/viewthread.php?tid=525493
相關文章
相關標籤/搜索