針對Nginx日志中出現的漏洞掃描與爬蟲的三種措施


0x001

使用fail2ban工具結合防火牆(iptables | firewalld),將大量404請求的IP地址封了。(詳見fail2ban使用說明:https://www.cnblogs.com/bestOPS/p/10616401.html)

 

0x002

將所有非法請求跳轉至首頁:
在nginx.conf 或 虛擬主機的配置文件中的server 配置段落里執行:
a. 把返回錯誤頁面配置 打開注釋

 error_page 403 404 500 502 503 504 = /error.html;

b. 加入下面配置:

location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
}

c. 編寫error.html頁面

  略。
d. 平滑重啟nginx systemctl nginx reload

 

0x003

指定User-Agent 並返回403

a. 在nginx.conf配置文件中加入:

include agent_deny.conf;

b. 在/../nginx/conf目錄下新建agent_deny.conf

vi agent_deny.conf
    # 禁止Scrapy等工具的抓取
    if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) {
      return 403;
    }

    # 禁止指定UA及UA為空的訪問
    if ($http_user_agent ~ "FeedDemon|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|YisouSpider|HttpClient|MJ12bot|heritrix|EasouSpider|LinkpadBot|Googlebot|Ezooms|^$" )
    {
      return 403;
    }

    # 禁止非GET|HEAD|POST的方式抓取
    if ($request_method !~ ^(GET|HEAD|POST)$) {
      return 403;
    }

c. 修改Nginx配置文件后重啟或重載

systemctl reload nginx  |  /etc/init.d/nginx reload  | service nginx reload

該方法會屏蔽大量爬蟲和自動化無差別攻擊,使自己的服務器更加安全和高可用。

 

附錄一:

在上述的 0x003中,可以將指定的User_agent加入到配置列表中,重載Nginx后,新加的User_agent將不能訪問網站。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM