相似於論壇型的網站常常會被黑掉,除了增長硬件防禦感受效果仍是不太好,還會偶爾被黑,waf的功能正好實現了這個需求。php
waf的做用:html
防止sql注入,本地包含,部分溢出,fuzzing測試,xss,SSRF等web攻擊 防止svn/備份之類文件泄漏 防止ApacheBench之類壓力測試工具的攻擊 屏蔽常見的掃描黑客工具,掃描器 屏蔽異常的網絡請求 屏蔽圖片附件類目錄php執行權限 防止webshell上傳
nginx 的話我選擇春哥開源的:OpenResty一個偉大的項目。nginx
OpenResty® 是一個基於 Nginx 與 Lua 的高性能 Web 平臺,其內部集成了大量精良的 Lua 庫、第三方模塊以及大多數的依賴項。用於方便地搭建可以處理超高併發、擴展性極高的動態 Web 應用、Web 服務和動態網關。git
OpenResty® 經過匯聚各類設計精良的 Nginx 模塊(主要由 OpenResty 團隊自主開發),從而將 Nginx 有效地變成一個強大的通用 Web 應用平臺。這樣,Web 開發人員和系統工程師可使用 Lua 腳本語言調動 Nginx 支持的各類 C 以及 Lua 模塊,快速構造出足以勝任 10K 乃至 1000K 以上單機併發鏈接的高性能 Web 應用系統。github
OpenResty® 的目標是讓你的Web服務直接跑在 Nginx 服務內部,充分利用 Nginx 的非阻塞 I/O 模型,不只僅對 HTTP 客戶端請求,甚至於對遠程後端諸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都進行一致的高性能響應。web
yum install -y readline-devel pcre-devel openssl-devel
wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
tar -xzf LuaJIT-2.0.5.tar.gz && cd LuaJIT-2.0.5
make && make install
export LUAJIT_LIB=/usr/local/lib && export LUAJIT_INC=/usr/local/include/luajit-2.0
ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
#必定建立此軟鏈接,不然會報錯 若是不建立符號連接,可能出現如下異常: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
wget https://openresty.org/download/openresty-1.11.2.2.tar.gz
tar -zxf openresty-1.11.2.2.tar.gz && cd openresty-1.11.2.2
./configure --prefix=/usr/local/openresty \ --user=www \ --group=www \ --with-luajit \ --with-http_v2_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_gzip_static_module \ --with-ipv6 --with-http_sub_module \ --with-pcre \ --with-pcre-jit \ --with-file-aio \ --with-http_dav_module
gmake && gmake install
vim /usr/local/openresty/nginx/conf/nginx.conf 可在server{..}段添加location規則
測試並啓動nginx
/usr/local/openresty/nginx/sbin/nginx -t
/usr/local/openresty/nginx/sbin/nginx
測試一下訪問是否輸出hello world,後面應該會有一些列的簡介。
cd /usr/local/openresty/nginx/conf/
https://github.com/bongmu/ngx_lua_waf.git
lua_package_path "/usr/local/openresty/nginx/conf/ngx_lua_waf/?.lua"; ###相關項目存放地址
lua_shared_dict limit 10m; ###存放limit表的大小
init_by_lua_file /usr/local/openresty/nginx/conf/ngx_lua_waf/init.lua; ###相應地址
access_by_lua_file /usr/local/openresty/nginx/conf/ngx_lua_waf/waf.lua; ##相應地址
cd ngx_lua_waf/ && vim config.lua
RulePath = "/usr/local/openresty/nginx/conf/ngx_lua_waf/wafconf" ##指定相應位置
attacklog = "on" ##開啓日誌
logdir = "/tmp" ##日誌存放位置
UrlDeny="on" ##是否開啓URL防禦
Redirect="on" ##地址重定向
CookieMatch="on" ##cookie攔截
postMatch="on" ##post攔截
whiteModule="on" ##白名單
black_fileExt={"php","jsp"}
ipWhitelist={"127.0.0.1"} ##白名單IP
ipBlocklist={"1.0.0.1"} ##黑名單IP
CCDeny="on" ##開啓CC防禦
CCrate="100/60" ##60秒內容許同一個IP訪問100次
mkdir /usr/local/openresty/nginx/conf/ngx_lua_waf/logs
chown -R nobody:nobody /usr/local/openresty/nginx/conf/ngx_lua_waf/logs
把congfig.lua的頻率改爲以下:sql
CCDeny="on"shell
CCrate="50/60"vim
測試結果:後端
處處已經構建成功了一套waf防護系統,很是感謝loveshell提供這麼棒的waf開源項目,還有春哥的openresty.