modSecurity規則學習(八)——防止CC攻擊

modSecurity日誌收集:在phase 5階段處理。css

因爲CC攻擊主要考慮對動態請求的防禦,因此要排除靜態資源的請求,或者自定義動態請求的後綴或者關鍵字作接口針對性的防禦。web

定義須要排除的請求url後綴名稱mvc

SecAction \
 "id:900260,\
  phase:1,\
  nolog,\
  pass,\
  t:none,\
  setvar:'tx.static_extensions=/.mvc/ /.jpg/ /.jpeg/ /.png/ /.gif/ /.js/ /.css/ /.ico/ /.svg/ /.webp/'"

 

而後是怎麼判斷是CC攻擊?根據訪問頻率來定義,某個ip在dos_burst_time_slice的時間(單位秒)內dos_counter_threshold次請求算一次攻擊嫌疑,若是超過2次,咱們就認定是CC攻擊的IP,對該IP封禁,解封時間定義爲dos_block_timeout。app

例如,定義60s內100次動態請求算一次攻擊嫌疑,封禁時間爲600s。ide

SecAction \
 "id:900700,\
  phase:1,\
  nolog,\
  pass,\
  t:none,\
  setvar:'tx.dos_burst_time_slice=60',\
  setvar:'tx.dos_counter_threshold=100',\
  setvar:'tx.dos_block_timeout=600'"

 接下來就是關鍵的攔截策略了。svg

整體思路以下:url

一、若是一個請求訪問的是非靜態(TX:STATIC_EXTENSIONS)資源,那咱們就定義一個變量(IP:DOS_COUNTER)+1,IP爲客戶端IP日誌

 

SecRule REQUEST_BASENAME ".*?(\.[a-z0-9]{1,10})?$" \
	"phase:5,\
	id:912150,\
	t:none,\
	t:lowercase,\
	nolog,\
	pass,\
	tag:'application-multi',\
	tag:'language-multi',\
	tag:'platform-multi',\
	tag:'attack-dos',\
	capture,\
   	setvar:tx.extension=/%{TX.1}/,\
	chain"
	SecRule TX:EXTENSION "!@within %{tx.static_extensions}" \
		"setvar:ip.dos_counter=+1"

  

二、若是IP:DOS_COUNTER大於TX:DOS_COUNTER_THRESHOLD閾值,就建立一個CC嫌疑次數IP:DOS_BURST_COUNTER=1,若是存在則該值設置爲2。且IP:DOS_COUNTER置0,CC嫌疑次數IP:DOS_BURST_COUNTER有一個超時時間TX:DOS_BURST_TIME_SLICE。orm

SecRule IP:DOS_COUNTER "@ge %{tx.dos_counter_threshold}" \
	"phase:5,\
	id:912160,\
	t:none,\
	nolog,\
	pass,\
	tag:'application-multi',\
	tag:'language-multi',\
	tag:'platform-multi',\
	tag:'attack-dos',\
	chain"
	SecRule &IP:DOS_BURST_COUNTER "@eq 0" \
		"setvar:ip.dos_burst_counter=1,\
		expirevar:ip.dos_burst_counter=%{tx.dos_burst_time_slice},\
		setvar:!ip.dos_counter"


SecRule IP:DOS_COUNTER "@ge %{tx.dos_counter_threshold}" \
	"phase:5,\
	id:912161,\
	t:none,\
	nolog,\
	pass,\
	tag:'application-multi',\
	tag:'language-multi',\
	tag:'platform-multi',\
	tag:'attack-dos',\
	chain"
	SecRule &IP:DOS_BURST_COUNTER "@ge 1" \
		"setvar:ip.dos_burst_counter=2,\
		expirevar:ip.dos_burst_counter=%{tx.dos_burst_time_slice},\
		setvar:!ip.dos_counter" 

三、若是CC嫌疑次數IP:DOS_BURST_COUNTER大於2就認定爲CC攻擊,定義一個值IP:DOS_BLOCK,該變量超時時間爲TX:DOS_BLOCK_TIMEOUT。blog

定義大於等於兩次封IP的規則:若是IP:DOS_BURST_COUNTER大於等於2,設置變量ip.dos_block=1,且設置該變量的超時時間爲TX:DOS_BLOCK_TIMEOUT。在phase5發生。

SecRule IP:DOS_BURST_COUNTER "@ge 2" \
	"phase:5,\
	id:912170,\
	t:none,\
	log,\
	pass,\
	tag:'application-multi',\
	tag:'language-multi',\
	tag:'platform-multi',\
	tag:'attack-dos',\
	msg:'Potential Denial of Service (DoS) Attack from %{tx.real_ip} - # of Request Bursts: %{ip.dos_burst_counter}',\
	setvar:ip.dos_block=1,\
	expirevar:ip.dos_block=%{tx.dos_block_timeout}"

四、在phase1,若是IP:DOS_BLOCK爲1,則阻斷該請求,且IP:DOS_BLOCK_COUNTER+=1。若是一個IP第一次被阻斷,記錄阻斷日誌並設置一個IP:DOS_BLOCK_FLAG 標誌,該標誌的超時時間爲60s,設置該標誌的後面不記錄日誌,避免刷屏。也就是說若是一直攻擊,最多60s記錄一第二天志。爲了顯示IP:DOS_BLOCK_COUNTER的同時重置該值,將IP:DOS_BLOCK_COUNTER複製到TX:DOS_BLOCK_COUNTER這個變量。

#
# Block and track # of requests and log
#
SecRule IP:DOS_BLOCK "@eq 1" \
	"chain,\
	phase:1,\
	id:912120,\
	drop,\
	tag:'application-multi',\
	tag:'language-multi',\
	tag:'platform-multi',\
	tag:'attack-dos',\
	msg:'Denial of Service (DoS) attack identified from %{tx.real_ip} (%{tx.dos_block_counter} hits since last alert)'"
	SecRule &IP:DOS_BLOCK_FLAG "@eq 0" \
		"setvar:ip.dos_block_counter=+1,\
		setvar:ip.dos_block_flag=1,\
		expirevar:ip.dos_block_flag=60,\
		setvar:tx.dos_block_counter=%{ip.dos_block_counter},\
		setvar:ip.dos_block_counter=0"

  

SecRule IP:DOS_BLOCK "@eq 1" \
	"phase:1,\
	id:912130,\
	t:none,\
	drop,\
	nolog,\
	tag:'application-multi',\
	tag:'language-multi',\
	tag:'platform-multi',\
	tag:'attack-dos',\
	setvar:ip.dos_block_counter=+1"
相關文章
相關標籤/搜索