å®éã«å®èšŒãããéåžžã«æåããŠããããšãå®èšŒãããŠããDDoSæ»æã«å¯ŸåŠããããã®å®èšŒæžã¿ã®æ¹æ³ã説æ
ããããŠã©ã«Webéçºè
äŒè°çšã«æºåãããè³æã§ãHabréã§ã®ãã¬ãŒã³ã¹ãéå§ããããšã«ããŸããã ãã®èšäºã®å¯Ÿè±¡èªè
ã¯ãvdsãæã£ãŠããããèªç±ã«äœ¿ãããšãã§ããããã°ã©ããŒã§ãã ãã®èšäºã¯ãæ¬æ ŒçãªãªãŒããŒã·ããã®ãµãããããã®ã§ã¯ãªãããã®äžã®å€ãã®ã·ã¹ãã 管çè
ã®ãã¥ã¢ã³ã¹ã¯æå³çã«çç¥ãããŠããŸãã httpãã©ããã£ã³ã°ã¿ã€ãã®DDoSã®ã¿ããDDoSã®æãäžè¬çãªã¿ã€ãã§ããã顧客ã«ãšã£ãŠæãå®äŸ¡ãªãã®ã§ãããšèããŠããŸãã
ãã®èšäºã®å¯Ÿè±¡èªè
ã¯ãVDSãŸãã¯Dedicatedãèªç±ã«äœ¿çšã§ããããã°ã©ããŒã§ãã
nginxã®æ-Apache-fastcgi / wsgiã ããã«ããã¯
Webã¢ããªã±ãŒã·ã§ã³ã®å
žåçãªçµç¹ã¯ããªããŒã¹ãããã·ãµãŒããŒïŒnginxãªã©ïŒãApacheïŒWebãµãŒããŒïŒãfastcgi / wsgi / ...ã¢ããªã±ãŒã·ã§ã³ã®3ã€ã®ã¬ãã«ã§æ§æãããŸãã å®éã«ã¯ãApacheããªãå ŽåããŸãã¯mod_php / mod_pythonã䜿çšããå Žåãå°çšã¢ããªã±ãŒã·ã§ã³ïŒWebãµãŒããŒã«çµã¿èŸŒãŸããŠããïŒããªãå Žåãéåããã±ãŒã¹ããããŸãããã¹ããŒã ã®æ¬è³ªã¯å€æŽãããããã®ã¬ãã«ã®æ°ã®ã¿ãå€æŽãããŸãã
FcgiãµãŒããŒã¯ãçä¿¡èŠæ±ã䞊è¡ããŠåŠçããæ°åã®ããã»ã¹ãå®è¡ã§ããŸãã ããã»ã¹ãã¡ã¢ãªã«é
眮ãããŠããéããã®å€ã¯ç¹å®ã®å¶éãŸã§ããå¢ããããšãã§ããŸããã ããã«å¢å ãããšãã¹ã¯ããã³ã°ãçºçããŸãã DDoSæ»æãŸãã¯é«ãã©ãã£ãã¯ã®å ŽåãçŸåšã®ãã¹ãŠã®fcgiããã»ã¹ããã§ã«çä¿¡èŠæ±ã®åŠçã§ããžãŒã§ããå Žåãapgiã¯fcgiããã»ã¹ã®ããããã解æŸããããããã¥ãŒã«ã¿ã€ã ã¢ãŠããçºçãããŸã§ïŒãã®å Žåã¯ïŒãšã©ãŒ503ãçºçããŸãïŒã
ãŸããApacheã«ã¯æ¥ç¶æ°ã«å¶éããããéåžžã¯æ°çŸïŒfcgiããã1æ¡å€§ããïŒã§ãã Apacheãžã®ãã¹ãŠã®æ¥ç¶ã䜿ãæããããåŸããªã¯ãšã¹ãã¯nginxã«ãã£ãŠãã§ã«ãã¥ãŒã«å
¥ããããŠããŸãã
Nginxã¯ããã®éåæã¢ãŒããã¯ãã£ã«ãããéåžžã«æ§ãããªã¡ã¢ãªæ¶è²»ã§æ°åã®æ¥ç¶ãç°¡åã«ä¿æã§ãããããéåžžã®DDoSæ»æã¯ãnginxãé©åã«æ§æãããŠããå Žåãnginxãæ°ããæ¥ç¶ãåãå
¥ããããªãã¬ãã«ã«éããŸããã
nginxã§ã®ãã©ãã£ãã¯ã®ãã£ã«ã¿ãªã³ã°ã nginxãã°ã®è§£æ
ææ¡ããææ³ã¯ãç¹å®ã®å€ïŒããšãã°ãçŸåšã®ãµãŒããŒå®¹éã§èãããããµã€ããšã³ãžã³ã®ãããæ°ã«å¿ããŠæ¯å1500ãªã©ïŒã䜿çšããŠããµã€ããžã®ãªã¯ãšã¹ãã®åèšæ°ãå¶éããããšã«èŠçŽãããŸãã ãã®å€ãè¶
ãããã¹ãŠã®ãã®ã¯ãæåã«nginxïŒlimit_req_zone $ host zone = hostreqlimitïŒ20m rate = 1500r / m;ïŒã§ãã£ã«ã¿ãªã³ã°ããŸãã
次ã«ãnginxãã°ã調ã¹ãŠãäžå®æéïŒããšãã°ã5åéã«100å以äžïŒäžå®åæ°ä»¥äžé€å€ãããIPã¢ãã¬ã¹ãèšç®ãããã¡ã€ã¢ãŠã©ãŒã«çµç±ã§ãããã®IPã¢ãã¬ã¹ãžã®ã¢ã¯ã»ã¹ãæåŠããŸãã
åãIPã¢ãã¬ã¹ïŒlimit_req_zone $ binary_remote_addr ...ïŒããã®åŸæ¥ã®ããã°ãã°æšå¥šãããæ¥ç¶å¶éã䜿çšããªãã®ã¯ãªãã§ããïŒ ãŸããnatã«åº§ã£ãŠãããããã€ããŒã®ã¯ã©ã€ã¢ã³ãã¯ãã®å¶éã«è©²åœããŸãã 第äºã«ãæ®éçãªãããå€ã確ç«ããããšã¯äžå¯èœã§ãããªããªããajaxãšå€æ°ã®js / css / imagesãåãããµã€ãããããååãšããŠ1ããŒãžãããŒãããã®ã«æ°ååã®ããããå¿
èŠã«ãªãå¯èœæ§ãããããã®ãããªãããå€ã¯åãµã€ãã§åå¥ã«ã®ã¿äœ¿çšã§ããããã§ãã 第äžã«ããããããç·©æ
¢ãªãDDoSæ»æã®å Žåããããã¯ãã®ãããå€ããŸã£ããäžåããªã-å€ãã®ããããååšããŸããããããããåå¥ã«çæéã§å°æ°ã®ãªã¯ãšã¹ããè¡ããããäœããã£ã«ã¿ãªã³ã°ã§ããŸããããµã€ãã¯æ©èœããŸããã
ãã®æ¹æ³ã䜿çšããã«ã¯ãnginxãApacheã®ãªããŒã¹ãããã·ãšããŠæ©èœããå Žåãnginxæ§æãã¡ã€ã«ã¯æ¬¡ã®ããã«ãªããŸãã
http { limit_req_zone $host zone=hostreqlimit:20m rate=1500r/m; ... server { listen 1.2.3.4; server_name domain.ru www.domain.ru; limit_req zone=hostreqlimit burst=2500 nodelay; location / { proxy_pass http://127.0.0.1:80; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; } } }
ãã®æ§æã¯ãapacheã127.0.0.1:80ã®ã«ãŒãããã¯ã€ã³ã¿ãŒãã§ã€ã¹ãããã³å€éšIPã¢ãã¬ã¹ïŒ1.2.3.4ïŒã®80çªç®ã®ããŒããš127.0.0.1ã®ããŒã8080ã§nginxããªãã¹ã³ããŠããããšãæå³ããŸãã
nginxã«ãã£ãŠãã£ã«ã¿ãªã³ã°ããããããã®åŸã«ã¯ãnginxã®error.logã«æ¬¡ã®ãããªãšã³ããªãç¶ããŸãã
2012/01/30 17:11:48 [error] 16862
error.logãããã¹ãŠã®ãããã¯ãããIPã¢ãã¬ã¹ã®ãªã¹ããååŸããã«ã¯ã次ã®ããã«ããŸãã
cat error.log | awk '/hostreqlimit/ { gsub(", ", " "); print $14}' | sort | uniq -c | sort -n
ãã ãããã®å Žåããããã«ãŠã³ã¿ãŒã1åéã«1,500åã«ãŠã³ããããåŸã«ãµã€ãã«ã¢ã¯ã»ã¹ãããã¹ãŠã®ãŠãŒã¶ãŒããããã¯ããããããããã¯ããããã¹ãŠããããã§ãããšã¯éããŸããã ãã ããããã¯ã®æ°ã«æ¡ä»¶ä»ãã®ç·ãåŒããšãããããåºå¥ã§ããŸãã éåžžãå€ã¯5ã15åã§æ°çŸåéžæãããŸãã ããšãã°ããããã®ãªã¹ãã5åããšã«è£å
ããnginxã200åãè¶
ããŠãããã¯ãã人ã¯ãã¹ãŠãããã§ãããšèŠãªããŸãã
çŸåšã2ã€ã®åé¡ã«çŽé¢ããŠããŸãã
- ãã°ããæéãæåŸã®5åéããéžæããæ¹æ³ã¯ïŒ
- Nå以äžãããã¯ããã人ã ãããœãŒãããæ¹æ³ã¯ïŒ
æåã®åé¡ã¯ãtail -c + OFFSETã䜿çšããŠè§£æ±ºãããŸãã ã€ãŸããerror.logã解æããåŸãçŸåšã®ãµã€ãºããã€ãåäœã§è£å©ãã¡ã€ã«ã«æžã蟌ã¿ïŒstat -c 'ïŒ
s' error.log>ãªãã»ããïŒã次ã®åæäžã«error.logãæåŸã«è¡šç€ºãããäœçœ®ã«å·»ãæ»ããŸãïŒæ«å°Ÿ-c + $ïŒcat offsetïŒïŒã ãããã£ãŠã5åããšã«ãã°ã®åæãéå§ããæåŸã®5åéãåç
§ãããã°ã®éšåã®ã¿ã調ã¹ãŸãã
2çªç®ã®åé¡ã¯ãawkã¹ã¯ãªããã䜿çšããŠè§£æ±ºãããŸãã ãã®çµæã次ã®ããã«ãªããŸãïŒTHRESHOLDã¯ããã¯æ°ã®å¶éãšåãã§ãããã®åŸã察å¿ããIPã¢ãã¬ã¹ã¯æ»æãããã«å±ããŠãããšèŠãªãããŸãïŒã
touch offset; (test $(stat -c '%s' error.log) -lt $(cat offset) 2>/dev/null && echo 0 > offset) || echo 0 > offset; \ tail -c +$(cat offset) error.log | awk -v THRESHOLD=200 '/hostreqlimit/ { gsub(", ", " "); a[$14]++; } \ END { for (i in a) if (a[i]>THRESHOLD) printf "%s\n", i; }' ; stat -c '%s' error.log > offset
ãã®äžé£ã®ã³ãã³ãã¯ãnginxã®error.logãååšãããã£ã¬ã¯ããªã§å®è¡ãããŸããã€ãŸããååãšããŠ/ var / log / nginxã§ãã çµæã®ãªã¹ãããã¡ã€ã¢ãŠã©ãŒã«ã«éä¿¡ããŠããããã¯ããŸãïŒè©³çŽ°ã¯ä»¥äžãåç
§ïŒã
çŠæ¢ãããããã¯ãŒã¯ã®ãªã¹ããäœæããã®ã¯ç°¡åã§ã
ããããããã«ã¯äœäžå°ãã®ã³ã³ãã¥ãŒã¿ãŒãå«ãŸããããšãããããã¹ãŠã®ãããããã£ããããããããµããããå
šäœã§äžèŠãªIPã¢ãã¬ã¹ãé®æããæ¹ãç°¡åãªå Žåãå€ããããDDoSã§çŽé¢ãããã1ã€ã®ã¿ã¹ã¯ã¯ãæœåšçãªèšªåè
ã§ã¯ãªããŠãŒã¶ãŒã«ãµã€ããžã®ã¢ã¯ã»ã¹ãå¶éããããšã§ãåå¥ã«ã
æåã«åœ¹ç«ã€ã®ã¯ã
NOCãã¹ã¿ãŒãã¹ã Webãµã€ãã®Runetãããã¯ãŒã¯ã®ãªã¹ãã§ãã çŸåšããã®ãªã¹ãã«ã¯çŽ5,000ã®ãããã¯ãŒã¯ããããŸãã ã»ãšãã©ã®ãã·ã¢ã®ãµã€ãã¯ãã·ã¢ããã®èšªåè
ã«çŠç¹ãåœãŠãŠããããããã¹ãŠã®å€åœäººèšªåè
ãããã³ãã¹ãŠã®å€åœäººããããé®æããããšã¯è«ççãªæ±ºå®ã®ããã«èŠããŸãã ããããæè¿ããŸããŸãå€ãã®ç¬ç«ããããããããããã·ã¢ã®ãããã¯ãŒã¯å
ã«ç»å ŽããŠããããããã®æ±ºå®ã¯æ£åœåãããŠãããã®ã®ãæ»æããæãããããšã¯ã»ãšãã©ãããŸããã
ãµã€ãã«ç¢ºç«ãããã³ãã¥ããã£ïŒã³ã¢ïŒãããå Žåãéå»3ã4é±éã®WebãµãŒããŒãã°ããå®æ蚪åè
ã®IPã¢ãã¬ã¹ã®ãªã¹ããéžæã§ããŸãã æ°ãã蚪åè
ã¯æ»æäžã«ãµã€ãã«ã¢ã¯ã»ã¹ã§ããŸããããå€ãã¢ã¯ãã£ããŠãŒã¶ãŒã¯æ»æã«æ°ä»ããªãã§ãããã ããã«ããããã¯éåžžã®èšªåè
ã«ã¯å«ãŸããªãå¯èœæ§ãé«ãããããã®æ¹æ³ã¯ååãšããŠããã°ããã®éæ»æãé»æ¢ã§ããŸãã
ãµã€ããããŒã«ã«ã§éèŠãªå Žåã¯ãããŒã«ã«ãããã€ããŒã®ãããã¯ãŒã¯ãšæ€çŽ¢ãšã³ãžã³ãããã¯ãŒã¯ïŒYandexïŒãé€ããã¹ãŠã®ãŠãŒã¶ãŒããã¡ã€ã¢ãŠã©ãŒã«ã§çŠæ¢ã§ããŸãã
iptablesã®æŠèŠãåçŽãªãã¡ã€ã¢ãŠã©ãŒã«ã®äŸ
Linuxã§ã¯ããã¡ã€ã¢ãŠã©ãŒã«ã¯iptablesã§å®è¡ãããŸãã å®éãiptablesã®æ¬è³ªã¯ãå€éšããåä¿¡ãŸãã¯ãµãŒããŒããéä¿¡ããããã©ãã£ãã¯ã®åãã±ããã«å¯ŸããŠããã®ãã±ããã®éåœã«åœ±é¿ãäžããå¯èœæ§ã®ããç¹å®ã®ã«ãŒã«ã»ãããé©çšãããããã«ããããšã§ãã æãåçŽãªã±ãŒã¹ã§ã¯ãã«ãŒã«ã¯åã«ãã±ãããåä¿¡ããïŒACCEPTïŒããããããããïŒDROPïŒå¿
èŠãããããšã瀺ããŠããŸãã ã«ãŒã«ã¯ãã§ãŒã³ã«åå²ãããŸãã ããšãã°ããµãŒããŒãã€ã³ã¿ãŒãããããåä¿¡ãããã±ããã¯INPUTãã§ãŒã³ã«åé¡ããããã§ãŒã³å
ã®ã«ãŒã«ã®æåããã®åãã±ããã«ã€ããŠããã±ãããã«ãŒã«ã§èª¬æãããŠããæ¡ä»¶ã«é©ããŠãããã©ããããã§ãã¯ãããäžèŽããå Žåã¯ãã®ã«ãŒã«ããã±ããã«é©çšãããããã§ãªãå Žåã¯ãã±ãã次ã®ã«ãŒã«ã«æž¡ãããŸãã ããã±ãŒãžã®ã«ãŒã«ãããããé©çšãããŠããªãå Žåãããã©ã«ãã®ããªã·ãŒãããã±ãŒãžã«é©çšãããŸãã
ç°¡åãªäŸãšããŠããªãã£ã¹ïŒIPã¢ãã¬ã¹1.2.3.4ïŒããã®ã¿ãµãŒããŒãžã®sshæ¥ç¶ãèš±å¯ããä»ã®ãã¹ãŠã®ãŠãŒã¶ãŒã®sshãžã®ã¢ã¯ã»ã¹ããããã¯ãããã¡ã€ã¢ãŠã©ãŒã«ã«ãŒã«ãäœæããŸãã
*filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -s 1.2.3.4/32 -m comment --comment "our office" -j ACCEPT -A INPUT -p tcp -m tcp --dport 22 -j DROP COMMIT
ãããã®è¡ã¯ãiptables-restore <firewall.txtã䜿çšããŠããã¹ããã¡ã€ã«ã«æžã蟌ã¿ããã¡ã€ã¢ãŠã©ãŒã«ã«ããŠã³ããŒããããã¡ã€ã¢ãŠã©ãŒã«ã®çŸåšã®ç¶æ
ããã¡ã€ã«ã«ä¿åã§ããŸãïŒiptables-save> firewall.txtã
ãããã®ã«ãŒã«ã¯æ¬¡ã®ããã«æ©èœããŸãã æåã®è¡-ãã§ã«éããŠãããã¹ãŠã®æ¥ç¶ã®ãã¹ãŠã®ãã©ãã£ãã¯ãèš±å¯ããŸãïŒãã³ãã·ã§ã€ã¯ãæž¡ãããŸãïŒã 2è¡ç®-IPã¢ãã¬ã¹1.2.3.4ããã®ãã©ãã£ãã¯ãèš±å¯ããããããªãã£ã¹ã§ããããšãã³ã¡ã³ãã§ããŒã¯ããŸãã å®éãæ¥ç¶ã確ç«ãããã±ãããã€ãŸãsynããã³ackã¿ã€ãã®ãã±ããã®ã¿ãããã«å°éããä»ã®ãã¹ãŠã®ãã±ããã¯æåã®è¡ã®ã¿ãééããŸãã 3è¡ç®-ãã¹ãŠã®ãŠãŒã¶ãŒãtcpãä»ããŠããŒã22ã«æ¥ç¶ããããšãçŠæ¢ããŸãã ç§ãã¡ã®ãªãã£ã¹ãé€ãå
šå¡ããsshçµç±ã§æ¥ç¶ïŒsynãackïŒããè©Šã¿ãããã«æ¥ãŸãã
èå³æ·±ãããšã«ãæåã®è¡ã¯å®å
šã«åé€ã§ããŸãã ãã®ãããªåç·ã䜿çšããå©ç¹ã¯ããã¡ã€ã¢ãŠã©ãŒã«ã§æ¢ã«éããŠããæ¥ç¶ã«å¯ŸããŠ1ã€ã®ã«ãŒã«ã®ã¿ãæ©èœãããã§ã«éããŠããæ¥ç¶å
ã®ãã±ãããåä¿¡ãããã±ããã®å€§éšåã§ãããšããããšã§ããã€ãŸããæåã«ãã®ãããªåç·ãæã€ãã¡ã€ã¢ãŠã©ãŒã«ã¯ãå®éã«ã¯è¿œå ã®é
延ãå°å
¥ããŸãããµãŒããŒã®ãããã¯ãŒã¯ã¹ã¿ãã¯ã«ã ãã€ãã¹-ãã®è¡ã¯conntrackã¢ãžã¥ãŒã«ãã¢ã¯ãã£ãã«ãã確ç«ããããã¹ãŠã®æ¥ç¶ã®ããŒãã«ã®ã³ããŒãã¡ã¢ãªã«ä¿æããŸãã ããé«äŸ¡ãªã®ã¯ãæ¥ç¶ããŒãã«ã®ã³ããŒãä¿æããããåãã±ããã«å¯ŸããŠè€æ°ã®ãã¡ã€ã¢ãŠã©ãŒã«ã«ãŒã«ãåŠçããå¿
èŠãããããã§ãã ããã¯ãåãµãŒããŒã®åã
ã®ãã¥ã¢ã³ã¹ã§ãã ãã¡ã€ã¢ãŠã©ãŒã«ã«å«ãŸããã«ãŒã«ãå°æ°ã®å Žåãconntrackã¢ãžã¥ãŒã«ãã¢ã¯ãã£ãã«ãªããªãããã«ã«ãŒã«ãäœæããæ¹ãæ£ãããšèããŠããŸãã
iptablesã§ã¯ãè¿œå ã®ãŠãŒã¶ãŒå®çŸ©ãã§ãŒã³ãäœæã§ããŸãã ããæå³ã§ã¯ãããã°ã©ãã³ã°èšèªã®é¢æ°åŒã³åºãã®é¡äŒŒç©ã®ããã«èŠããŸãã æ°ãããã§ãŒã³ã®äœæã¯ç°¡åã§ãïŒiptables -N chain_nameã ãã®æ¹æ³ã§äœæããããã§ãŒã³ã¯ããã¡ã€ã¢ãŠã©ãŒã«ãç°ãªãè«çãããã¯ã«åå²ããããã«äœ¿çšãããŸãã
DDoSã«å¯Ÿæããããã®æšå¥šãã¡ã€ã¢ãŠã©ãŒã«æ§é
DDoSã«å¯Ÿæããããã«æšå¥šãããæ§é ã¯ã次ã®è«çãããã¯ã§æ§æãããŠããŸãã
- ãã§ã«ç¢ºç«ãããæ¥ç¶ãä»ãããã©ãã£ãã¯ãèš±å¯ããŸãã
- ãç§ãã¡ã®ãIPã¢ãã¬ã¹ã®èš±å¯ãç»é²ããŸãã
- ãã¯ã€ããªã¹ãããŒãã«ã¯äŸå€ã§ãã
- DDoSããŒãã«ã¯ãç¹å®ãããããã§ãã
- friendsããŒãã«ã¯Runetãããã¯ãŒã¯ã§ãããããã±ãŒãžããã®ã¬ãã«ã«éããå Žåã«ã¢ã¯ã»ã¹ãèš±å¯ããŸãã
- ä»ã®ãã¹ãŠã®ãŠãŒã¶ãŒã¯-j DROPã§ãã
iptablesã®èŠ³ç¹ã§ã¯ã次ã®ããã«ãªããŸãã
*filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] :ddos - [0:0] :friends - [0:0] :whitelist - [0:0] -A INPUT -i lo -j ACCEPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -s 1.2.3.4/32 -m comment --comment "our office" -j ACCEPT -A INPUT -p tcp -m tcp --dport 22 -j DROP -A INPUT -j whitelist -A INPUT -j ddos -A INPUT -j friends -A INPUT -j DROP -A whitelist -s 222.222.222.222 -j ACCEPT -A whitelist -s 111.111.111.111 -j ACCEPT -A ddos -s 4.3.2.0/24 -j DROP -A friends -s 91.201.52.0/22 -j ACCEPT COMMIT
ç¹°ãè¿ãã«ãªããŸããã2è¡ç®ã®é©åæ§ã¯åé¡ã§ããããã¡ã€ã¢ãŠã©ãŒã«ã®ãã«ãµã€ãºã«å¿ããŠãäœæ¥ã®é床ãäžãããé
ããããã§ããŸãã
friendsããŒãã«ã«å
¥åããŸãã
for net in $(curl -s http://noc.masterhost.ru/allrunet/runet); do iptables -A friends -s $net -j ACCEPT; done
ãã®ãããªãã¡ã€ã¢ãŠã©ãŒã«ã®åé¡ã¯ããã®äžé¢šå€ãã£ãæ§è³ªã§ãïŒRunetã®å Žåã®friendsããŒãã«ã«ã¯ãçŽ5000ã®ã«ãŒã«ãå«ãŸããŸãã å€ããå°ãªããå¹³åçãªDDoSã®å ŽåãDDoSããŒãã«ã«ã¯ããã«1ã2å件ã®ã¬ã³ãŒããå«ãŸããŸãã ãã¡ã€ã¢ãŠã©ãŒã«å
šäœã¯5ã7åè¡ã§æ§æãããŸãã åæã«ãå€éšã®éä¿¡è
ããå°çãããã¹ãŠã®ãã±ããã¯ãåã«ç Žæ£ããå¿
èŠããããŸãããæåŸã®ã«ãŒã«ã«å°éãããŸã§ãå®éã«5ã7åã®ã«ãŒã«ããã¹ãŠééããŸãã-A INPUT -j DROPã ããèªäœã§ã¯ããã®ãããªãã¡ã€ã¢ãŠã©ãŒã«ã¯èšå€§ãªéã®ãªãœãŒã¹ã䜿ãæãããŸãã
IPSET-巚倧ãªãã¡ã€ã¢ãŠã©ãŒã«ã®ãœãªã¥ãŒã·ã§ã³
Ipsetã¯ãéä¿¡è
ãŸãã¯åä¿¡è
ã®ã¢ãã¬ã¹ãç°ãªããã±ãããã©ãåŠçãããã説æããäœåè¡ãã®ã¢ã³ã¹ããã€ããã¡ã€ã¢ãŠã©ãŒã«ã®åé¡ãå®å
šã«è§£æ±ºããŸãã Ipsetã¯ãç¹å¥ãªã»ããïŒåãã¿ã€ãã®ããŒã¿ã®ã»ããïŒã管çããããã®ãŠãŒãã£ãªãã£ã§ãããã®ã»ããã§ã¯ãããã€ãã®å®çŸ©æžã¿ããŒã¿ã¿ã€ãã«å¯ŸããŠç¹å¥ãªããã·ã¥ããŒãã«ãäœæãããŠããããã®ããŒãã«å
ã®ç¹å®ã®ããŒã®æç¡ãéåžžã«è¿
éã«ç¢ºç«ã§ããŸãã ããæå³ã§ã¯ãããã¯memcachedã®é¡äŒŒç©ã§ãããã¯ããã«é«éã§ãããç¹å®ã®ããŒã¿åã®ã¿ãæ ŒçŽã§ããŸãã DDoSãããã®IPã¢ãã¬ã¹ã«é¢ããæ
å ±ãä¿åããããã®æ°ããããŒã¿ã»ãããäœæããŸãããã
ipset -N ddos iphash
ããã§ãæåŸã®ãã©ã¡ãŒã¿ãŒã¯äœæãããããŒãã«ã®ã¿ã€ãã瀺ããŸããnethashã¯ãããã¯ãŒã¯ã®ãªã¹ãã«èšå®ãããiphashã¯åã
ã®IPã¢ãã¬ã¹ã«èšå®ãããŸãã ããŸããŸãªããŒãã«ãªãã·ã§ã³ãããã詳现ã¯man ipsetã«ãããŸãã ãããã£ãŠããã¯ã€ããªã¹ããšå人ã¯ãããããã·ã¥ããŒãã«ã§ãããDDoSã¯iphashã§ãã
äœæããipsetããŒãã«ããã¡ã€ã¢ãŠã©ãŒã«ã§äœ¿çšããã«ã¯ã1ã€ã®ã«ãŒã«ïŒãã¡ã€ã¢ãŠã©ãŒã«ã®è¡ïŒã§ååã§ãã次ã«äŸã瀺ããŸãã
-A INPUT -m set --match-set whitelist src -j ACCEPT -A INPUT -m set --match-set ddos src -j DROP
次ã®ããã«ãæ°ããäœæããããŒãã«ã«IPã¢ãã¬ã¹ãè¿œå ã§ããŸãã
ipset -A ddos 1.2.3.4
ãããã£ãŠãipsetã䜿çšããå Žåã®ãã¡ã€ã¢ãŠã©ãŒã«å
šäœã¯æ¬¡ã®ããã«ãªããŸãã
*filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -i lo -j ACCEPT -A INPUT -s 1.2.3.4/32 -m comment --comment "our office" -j ACCEPT -A INPUT -p tcp -m tcp --dport 22 -j DROP -A INPUT -m set --match-set whitelist src -j ACCEPT -A INPUT -m set --match-set ddos src -j DROP -A INPUT -m set --match-set friends src -j ACCEPT -A INPUT -j DROP COMMIT
ã»ããã®åéãå
¥åããŸãïŒnethashã¿ã€ãïŒïŒ
for net in $(curl -s http://noc.masterhost.ru/allrunet/runet); do ipset -A friends $net; done
åã«ç€ºããã³ãã³ãããset ddosââãå
¥åããŸãã
touch offset; (test $(stat -c '%s' error.log) -lt $(cat offset) 2>/dev/null && echo 0 > offset) || echo 0 > offset; \ for ip in $(tail -c +$(cat offset) error.log | awk -v THRESHOLD=300 \ '/hostreqlimit/ { gsub(", ", " "); a[$14]++; } END { for (i in a) if (a[i]>THRESHOLD) printf "%s\n", i; }' ; \ stat -c '%s' error.log > offset); do ipset -A ddos $ip; done
TARPITã¢ãžã¥ãŒã«ã䜿çšããŸã
tarpitãšåŒã°ããiptablesã¢ãžã¥ãŒã«ã¯ãããããããã©ãããã§ãã ã¿ãŒãããã®åçã¯æ¬¡ã®ãšããã§ããã¯ã©ã€ã¢ã³ãã¯ãã³ãã·ã§ã€ã¯ïŒtcpæ¥ç¶ã®éå§ïŒãã€ã³ã¹ããŒã«ããããã«synãã±ãããéä¿¡ããŸãã Tarpitã¯syn / ackãã±ããã§å¿çããŸãããããã«å¿ããŸãã ãã ããå®éã«æ¥ç¶ãéãããããšã¯ãªãããªãœãŒã¹ã¯å²ãåœãŠãããŸããã æåŸã®ACKãã±ããããããããå°çãããšãã¿ãŒãããã¢ãžã¥ãŒã«ã¯ããµãŒããŒã«ããŒã¿ãéä¿¡ããããã®ãŠã£ã³ããŠãµã€ãºããŒãã«èšå®ãããã±ãããéãè¿ããŸãã ãã®åŸãã¿ãŒãããã«ãã£ãŠãããã®åŽãããã®æ¥ç¶ãéããããšããŠãç¡èŠãããŸãã ã¯ã©ã€ã¢ã³ãïŒãããïŒã¯æ¥ç¶ãéããŠãããšä¿¡ããŠããŸããããã¹ã¿ãã¯ãïŒãŠã£ã³ããŠãµã€ãºã¯0ãã€ãïŒãããã®æ¥ç¶ãéããããšããŸãããã¿ã€ã ã¢ãŠããæéåãã«ãªããŸã§äœãã§ããŸãããã¿ã€ã ã¢ãŠãã¯ãèšå®ã«å¿ããŠçŽ12-24ã§ããåã
次ã®ããã«ããã¡ã€ã¢ãŠã©ãŒã«ã§ã¿ãŒãããã䜿çšã§ããŸãã
-A INPUT -p tcp -m set --match-set ddos src -j TARPIT --tarpit -A INPUT -m set --match-set ddos src -j DROP
xtables-addonsããã«ããã
æ®å¿µãªãããipsetããã³tarpitã¢ãžã¥ãŒã«ã¯ãææ°ã®ãã£ã¹ããªãã¥ãŒã·ã§ã³ã®æšæºã»ããããæ¬ èœããŠããŸãã è¿œå ã§ã€ã³ã¹ããŒã«ããå¿
èŠããããŸãã å€ããå°ãªããæè¿ã®Debianããã³Ubuntuãã£ã¹ããªãã¥ãŒã·ã§ã³ã®å Žåãããã¯åçŽã«è¡ãããŸãã
apt-get install module-assistant xtables-addons-source ma ai xtables-addons
ãã®åŸãã·ã¹ãã èªäœããœãããŠã§ã¢ã®ãã«ãã«å¿
èŠãªãã¹ãŠãããŠã³ããŒããããã¹ãŠãåéããŠãã¹ãŠãã€ã³ã¹ããŒã«ããŸãã ä»ã®Linuxãã£ã¹ããªãã¥ãŒã·ã§ã³ã®å Žåãåãããšãè¡ãå¿
èŠããããŸããã詳现ã«ã€ããŠã¯ãªãã¡ã¬ã³ã¹ããã¥ã¢ã«ãåç
§ããããšããå§ãããŸãã
ã³ã¢ãã¥ãŒãã³ã°
ååãšããŠãDDoSæ»æãšã®æŠãã«ã€ããŠã®è©±ã¯ãOSã«ãŒãã«ã®ãã¥ãŒãã³ã°ã«é¢ããæšå¥šäºé
ããå§ãŸããŸãã ãã ããååãšããŠãªãœãŒã¹ãå°ãªãå ŽåïŒããšãã°ãã¡ã¢ãªã1 GBæªæºã®å ŽåïŒãã«ãŒãã«ã¯ã»ãšãã©äœãããªãã®ã§ãã«ãŒãã«ã®ãã¥ãŒãã³ã°ã¯æå³ããããŸããã ãã®å Žåã«æçšãªæ倧å€ã¯ããããããã®ãå«ããããšã§ãã syncookiesã syncookiesãæå¹ã«ãããšããµãŒããŒã«å€æ°ã®synãã±ãããã¹ããŒãããå Žåã«ãsynãã©ãããªã©ã®æ»æã«å¹æçã«å¯ŸåŠã§ããŸãã syn-packetãåä¿¡ãããšããµãŒããŒã¯æ°ããæ¥ç¶ãéãããã®ãªãœãŒã¹ãå²ãåœãŠãå¿
èŠããããŸãã syn-packetã®åŸã«æ¥ç¶ã»ããã¢ããæé ãç¶ããªãå ŽåããµãŒããŒã¯ãªãœãŒã¹ãå²ãåœãŠãã¿ã€ã ã¢ãŠããçºçãããŸã§ïŒæ°åïŒåŸ
æ©ããŸãã æçµçã«ãsyncookieããªããååãªæ°ã®synãã±ããããµãŒããŒã«éä¿¡ããããšãã·ã¹ãã ã¯ãã¹ãŠã®ãªãœãŒã¹ã䜿ãåã£ãŠããŒããªãŒãã³æ¥ç¶ã«é¢ããæ
å ±ãä¿åãããããæ¥ç¶ãåãå
¥ããããªããªããŸãã
説æããã«ãŒãã«ãã©ã¡ãŒã¿ã¯ãsysctlã³ãã³ãã䜿çšããŠä¿®æ£ãããŸãã
sysctl [-w] option
-wãªãã·ã§ã³ã¯ããããã©ã¡ãŒã¿ãŒã«æ°ããå€ãæžãããããšãæå³ãããã®äžåšã¯ããã®ãã©ã¡ãŒã¿ãŒã®çŸåšã®å€ãèªã¿ããããšãæå³ããŸãã 以äžã®ãã©ã¡ãŒã¿ãŒãä¿®æ£ããããšããå§ãããŸãã
net.ipv4.tcp_syncookies=1 net.ipv4.ip_local_port_range = 1024 65535 net.core.netdev_max_backlog = 30000 net.ipv4.tcp_max_syn_backlog = 4096 net.core.somaxconn = 4096 net.core.rmem_default = 124928 net.core.rmem_max = 124928 net.core.wmem_max = 124928
- net.ipv4.tcp_syncookiesãã©ã¡ãŒã¿ãŒã¯ãsyncookiesã¡ã«ããºã ãæå¹ã«ããŸãã net.core.netdev_max_backlogã¯ãã«ãŒãã«ãåŠçã§ãããããéãã€ã³ã¿ãŒãã§ã€ã¹ããã±ãããåä¿¡ããå ŽåãåŠçãã¥ãŒå
ã®ãã±ããã®æ倧æ°ã決å®ããŸãã
- net.ipv4.tcp_max_syn_backlogã¯ãæ¥ç¶ã¯ã©ã€ã¢ã³ããã確èªãåä¿¡ããŠââããªãèšæ¶ãããæ¥ç¶èŠæ±ã®æ倧æ°ãå®çŸ©ããŸãã
- net.core.somaxconnã¯ãæ¥ç¶ãåŸ
æ©ããŠããéããŠãããœã±ããã®æ倧æ°ã§ãã
- æåŸã®è¡ã¯ãtcpæ¥ç¶çšã®ããŸããŸãªãããã¡ãŒã§ãã
ãã®èšäºãVDSãŸãã¯å°çšãµãŒããŒã®ææè
ã«åœ¹ç«ã€ããšãé¡ã£ãŠããŸãã ã³ã¡ã³ããã³ã¡ã³ããæ®ããŠãã ããã