iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 29
2
DevOps

Oops Step ( Home lab of a kind ) 系列 第 31

Nginx naxsi + 色情守門員

曾與李小龍武術交流的Chuke Norris有許多fact(笑話),例如Chuck Norris can kill two stones with one bird.

Once a cobra bit Chuck Norris' leg. After five days of excruciating pain, the cobra died.
Yes

如果現實世界也有這樣的WAF就好了。

有,Naxsi是Nginx一個module,主要目標是藉由偵測http GET和POST內容,做"Nginx Anti XSS & SQL Injection"。
這麼好的東西,怎麼這麼晚才講?!/images/emoticon/emoticon33.gif至少我有講了啊~
https://ithelp.ithome.com.tw/upload/images/20190924/20094403YS8f2b1nx4.jpg

這是Open Source的
https://ithelp.ithome.com.tw/upload/images/20190924/20094403HIpGbxl1Vm.jpg

如果你有細看Day 13的編譯過程nginx設定圖裡面被截掉的那部分就有勾選naxsi module (誰看得到啊?!/images/emoticon/emoticon40.gif

所以花不到兩千塊買的路由器現在上面就有在跑WAF功能?/images/emoticon/emoticon77.gif是的,而我要講怎樣發揮它的超能力。
首先寫引用規則檔,我偷懶放在conf.d下,但是又不給它.conf副檔名(原因是*.conf已經在http定義會被include)

icekimo@Bosley:~$ cat /etc/nginx/conf.d/naxsi
        SecRulesEnabled; #enable naxsi
        LearningMode; #enable learning mode
        LibInjectionSql; #enable libinjection support for SQLI
        LibInjectionXss; #enable libinjection support for XSS

        DeniedUrl "/RequestDenied"; #the location where naxsi will redirect the request when it is blocked
        CheckRule "$SQL >= 8" BLOCK; #the action to take when the $SQL score is superior or equal to 8
        CheckRule "$RFI >= 8" BLOCK;
        CheckRule "$TRAVERSAL >= 5" BLOCK;
        CheckRule "$UPLOAD >= 5" BLOCK;
        CheckRule "$XSS >= 8" BLOCK;

        error_log /var/log/nginx/naxsi_attach.log;
# 可以看到naxsi_core.rules已經準備好了
icekimo@Bosley:~$ls /etc/nginx/
LICENSE            koi-utf            modules-available  nginx.conf         sites-enabled
conf.d             koi-win            modules-enabled    nginx.conf.old     win-utf
fastcgi_params     mime.types         naxsi_core.rules   sites-available
icekimo@Bosley:~$

然後在nginx.conf宣告引入naxsi_core.rules
https://ithelp.ithome.com.tw/upload/images/20190924/20094403Gx0gpwuvgB.png
接下來在各需要的server段加入,例如
https://ithelp.ithome.com.tw/upload/images/20190924/20094403fOjCUJWX5X.png
那麼,還有一些功夫要處理但是我們先測測看,首先在自己合法url後面加上 ?id=1 and 1=1 試試看。如果你照做,你會發現什麼都沒發生。因為請看上面/etc/nginx/conf.d/naxsi裡面有LearningMode,所以人家是實習生,會放行的啦。但是觀察/var/log/nginx/naxsi_attach.log

icekimo@Bosley:~$ tail -n1 /var/log/nginx/naxsi_attach.log
2019/09/24 15:11:07 [error] 26559#0: *33 NAXSI_FMT: ip=175.96.108.228&server=lab.icekimo.idv.tw&uri=/api/settings/public&learning=1&vers=0.55&total_processed=1&total_blocked=1&block=1&cscore0=$SQL&score0=4&cscore1=$XSS&score1=88&zone0=HEADERS&id0=1000&var_name0=cookie&zone1=HEADERS&id1=1315&var_name1=cookie, client: 175.96.108.228, server: lab.icekimo.idv.tw, request: "GET /api/settings/public HTTP/1.1", host: "lab.icekimo.idv.tw", referrer: "https://lab.icekimo.idv.tw/"

嗯,有工作。但如同上面所說,在學習模式中都會放行,那什麼時候魔界的大門會關起來呢?就等你把功能都玩過一遍,記錄好足夠log,然後我們要產製白名單,用nxutil
安裝python指令稿

git clone git@github.com:prajal/nxutil.git && cd nxutil && \
python setup.py install

然後產製白名單

python nx_util.py -l "/var/log/nginx/naxsi_attach.log" -o -p 1 > /etc/nginx/conf.d/wl-example.rules

記得要在server區塊include /etc/nginx/conf.d/wl-example.rules;

再來我要準備傳送門DeniedUrl "/RequestDenied",放在server區塊

這個威力強大,沒有中華電信專利的色情守門員,不用錢喔!! 如果你用在紅藍對抗,紅隊會找你簽名的

    location /RequestDenied {
        return 301 https://www.pornhub.com/ ; #色情守門員BJ4
    }

最後記得關掉學習模式,至於要不要關掉log,因為我不想log搞爆我的The sword of a thousand truths千真之劍,我會選擇關閉。

後記:異曲同工之妙的梗
Jeff Dean's Facts: Jeff Dean once bit a spider, the spider got super powers and C++ readability.

REF:
https://dotblogs.com.tw/eric_obay_talk/2018/10/22/150521
https://www.hi-linux.com/posts/3282.html
https://vpsfix.com/9114/analyze-naxsi-logs-create-whitelist-rules-nxutil/


上一篇
Piping in ssh
下一篇
SonarQube for Code Quality, (CE )
系列文
Oops Step ( Home lab of a kind ) 34
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
froce
iT邦大師 1 級 ‧ 2019-09-30 11:40:25

呃,最後那串網址,我沒有勇氣用。哈

icekimo iT邦新手 3 級 ‧ 2019-09-30 15:50:19 檢舉

這就像獵人在山上架設野豬陷阱,絕對不是架設給自己用的嘛。但是自己測試要小心,301重導可不是暫時的溫吞吞「客人請往左移動,考慮一下我們新的服務」,而是「這裡沒有東西,你去那裡」的指引,自己測試誤中的話可能要清掉瀏覽器網站快取資訊才會重新回來。

我要留言

立即登入留言