ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。/ _, ~9 @( D7 M" c
1 ^1 Y. z8 W/ W$ |: `$ ]
" H6 U& R! J! t; D# H! E& F: t在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。* X4 g. ]+ l; r2 p0 T
2 E. u. @# R n+ x0 j3 o1 t& S5 H
+ @" m2 u9 j- ~- w- e3 }3 @什么是ModSecurity8 T; z5 v; m5 h! J" v7 N
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。" p& s% \' e3 f# n) I
; X# _1 F" p& G0 l0 u
7 t8 E% I; Q( j- X* D8 W+ s: Z* xModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
/ ^* X' X1 g) S3 ?' J" Z h3 W: h1 U0 i0 d3 ?! N8 g
+ C' ?$ ?; Z- Q" l% _( _! uModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
; g+ X( b4 m: J- F6 A5 q# H: p) H# U' B8 ?! {
6 R/ x i! o" Q; d2 j3 E官网: https://www.modsecurity.org/" q8 V" x; n# i
7 l7 B/ P* ]8 ?9 ~! m6 e5 f# e* H' ?; x- X
什么是OWASP CRS4 X6 J5 j2 @( a1 E. k; p h& R" n
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
3 z4 ? ]. ^8 h1 V& f2 {# ^# @
& |9 p1 s/ r& P1 b$ w) V, V$ J6 H
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
' a t! n* b2 l' B6 C, M4 }( k6 }
! M2 C* t# N" L N+ V4 L7 z/ p; B% H
HTTP Protection(HTTP防御)6 M# Z B3 w! I% A* j! ~& z, }
HTTP协议和本地定义使用的detectsviolations策略。
- t" j1 U6 B& f( Z" [" O* e( |$ D2 p7 I" \+ o; M
' v+ q( a& ~* {* GReal-time Blacklist Lookups(实时黑名单查询). B8 Y" C/ {9 T: i* F. D, ]$ E6 h6 T
利用第三方IP名单。. M, x0 b" z# q3 w
6 d9 w8 S6 U% _4 b0 ~* h+ M
: a K6 g$ ~; \2 {4 b' r9 s0 g
HTTP Denial of Service Protections(HTTP的拒绝服务保护)4 o% `: c, {* I# a
防御HTTP的洪水攻击和HTTP Dos攻击。
. P* K% K5 ~6 R( w( F
2 R2 \, M, x" c( @3 n
" q$ h- z" \: B* b/ Q3 T7 ICommon Web Attacks Protection(常见的Web攻击防护)$ B( o/ l" u' x9 D
检测常见的Web应用程序的安全攻击。 l8 r3 @" x& j5 w: l+ W5 U9 h
8 ^9 v* {- q) D0 W$ p" Z
2 x4 E& R+ ^# g/ WAutomation Detection(自动化检测)
' G; }; z" y8 o8 s! i) `检测机器人,爬虫,扫描仪和其他表面恶意活动。
0 o" `: ]. w8 y/ i. L6 M9 J
% i, m6 }2 v5 e8 X# J3 m* ?: G" L) t- Y" W
Integration with AV Scanning for File Uploads(文件上传防病毒扫描), u7 t9 l+ V7 h7 O# C! f
检测通过Web应用程序上传的恶意文件。
* G3 r8 f7 c5 Q6 J J$ H& B; a5 u( H8 N# g$ ?% l4 ^
( z$ w# F7 R2 t$ e/ _0 p7 STracking Sensitive Data(跟踪敏感数据)6 `2 o+ n7 E: o9 B2 K1 |* i& [
信用卡通道的使用,并阻止泄漏。7 r' Q/ \$ b& y6 C& n
8 `4 Z5 u& ?( s( y
0 D- p, j& g' H# d9 hTrojan Protection(木马防护) w8 X7 w$ z; y0 C: P: H- d1 L
检测访问木马。
x# C, B3 P: O7 F3 i7 h2 T' U! p: E/ d2 a+ g
& M L( h1 I4 k m, r
Identification of Application Defects(应用程序缺陷的鉴定)
* n# ?9 J) |% D' i! v: e检测应用程序的错误配置警报。
0 {6 E6 n! V! I D+ W* m1 @# x& r5 s$ Q( f* |! y
" x" ~: T0 e, H) M9 k
Error Detection and Hiding(错误检测和隐藏)
- B; S" _' u5 \% a! a检测伪装服务器发送错误消息。
/ I6 I' S) G% p& S0 Z9 _8 }; F0 ]* d- p. z. `6 p/ B
0 F5 N1 o- S: [
安装ModSecurity
8 M6 }; f; Z5 G$ `5 z1 }3 ?' u软件基础环境准备
6 |* _8 D) x- s4 L: v下载对应软件包2 x4 D" Z; R; e* ?1 \
$ cd /root
: I8 [) D" Z8 Q# i/ Y3 u. W$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'. Q6 Q1 H9 {+ y: O/ u( f
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz# x# T5 a. R( K4 l G! X
安装Nginx和ModSecurity依赖包: t! j5 l6 x! q
Centos/RHEL2 q3 l! g; }+ K
& b, V# R( T# n- u8 k
2 N6 A4 [: ~4 D- j1 G- E$ ]5 Y$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel( H5 t, l2 P4 g
Ubuntu/Debian* h; b& @5 \ e9 z: D j1 z
; ?, E* e4 _8 Y4 a- Z+ y9 n3 L4 R3 [# c
$ apt-get install libreadline-dev libncurses5-dev libssl-dev perl make build-essential git libpcre3 libpcre3-dev libtool autoconf apache2-dev libxml2 libxml2-dev libcurl4-openssl-dev g++ flex bison curl doxygen libyajl-dev libgeoip-dev dh-autoreconf libpcre++-dev
/ V5 ]( V) ^2 X* v* {+ c编译安装ModSecurity
( T( L/ Y( B( F+ |: UNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
8 J/ e3 s6 K9 c/ a/ ~6 Q3 K( v, u V/ d6 G! o
' w/ a, A* D+ r7 |! ~+ I$ L7 r
方法一:编译为Nginx静态模块 L* G( X8 i$ n# P/ _& |5 z
4 V3 ?! y/ m9 L$ E% W r! i3 Q
1 y: a0 z! b# s9 P" T
编译为独立模块(modsecurity-2.9.1)5 p; s2 q. L% ^$ Q
$ tar xzvf modsecurity-2.9.1.tar.gz/ Z& I$ r, |+ k2 C y- l8 X4 }0 q
$ cd modsecurity-2.9.1/
( M1 N+ d4 B' Y6 J$ ./autogen.sh
: i/ } O U, W+ j! N$ ./configure --enable-standalone-module --disable-mlogc
0 c V5 e# Z9 O" U$ make3 z' s/ \' i: j( s- A" S: B
编译安装Nginx并添加ModSecurity模块: @& k) v# a( n& ], E' g, {9 I* h1 x9 v
$ tar xzvf nginx-1.9.2.tar.gz) }5 Q2 ]- [. X8 t3 N4 f
$ cd nginx-1.9.2* D* v' V7 c. b( c' m& y" |
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/) w g5 Y" ~; d, i
$ make && make install: S1 w( B( f4 p, y$ q
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
3 J0 ^2 L; k" ?' x
; ~8 W2 F" J1 @" c
- K# `2 G+ E& F' C- w编译LibModSecurity(modsecurity-3.0)& V e8 m2 m" s% g# f, j: ^
$ cd /root4 ^& f0 }. ]# ^# N5 t. p% O1 m
$ git clone https://github.com/SpiderLabs/ModSecurity) J* d5 t& A, _2 B) s+ u
$ cd ModSecurity
c! j8 {; V$ _0 p9 F$ git checkout -b v3/master origin/v3/master
& N0 Y& P9 y; i0 e Y3 L$ sh build.sh y; d* ~) h8 r; o ?% u' V) X
$ git submodule init
# W7 W+ K0 Y# V2 [! _7 H0 Z3 h$ git submodule update
' U9 ]2 `2 D; X% O4 f$ ./configure8 Q0 d/ Q% C. |$ A( p1 L
$ make
6 k* G( ~0 f& ?% o2 k$ make install: d4 o7 y* @7 o( U7 p3 ]
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
( h& e4 `6 ?+ w6 V h: I% H
, m, S7 j4 ^. `9 J8 m2 i
* ]* v) G, S" W5 Y5 f2 S$ }$ S$ ls /usr/local/modsecurity/lib2 H1 R8 W2 R6 b8 p1 H2 o n; Z, Q, n
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
- @* s2 K: m$ N# n9 u7 J- X编译安装Nginx并添加ModSecurity-Nginx Connector模块
' \/ e6 f8 G; N$ W- ]使用ModSecurity-Nginx模块来连接LibModSecurity
, \% H, v, h/ {. b- J" H/ d: U& v& f( O
: |* C g; ~, ] R" o9 Y# N0 x$ cd /root
2 n: d) G! C6 T* u9 t' @+ a9 [' Y$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx1 n7 @5 u* F6 A: q) @
$ tar xzvf nginx-1.9.2.tar.gz h; n8 @* U$ p$ i9 d# s: o
$ cd nginx-1.9.26 ?: Z$ l6 j& i2 Z- ^ W1 _
$ ./configure --add-module=/root/modsecurity-nginx* k3 \6 L- Y' y3 z/ [4 o
$ make
: x7 O& o: j3 m$ make && make install, \4 M5 T% ]* r9 i2 c: k4 b# ?' |
添加OWASP规则
6 r/ T# P# ?. L; t5 t7 DModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
. `% E: S; D# h- V C7 J) u, E. L, O' s3 [$ v# _" d8 |5 Z* t6 ^& q
! @+ z5 C+ D! z0 k# F0 L2 V; `下载OWASP规则并生成配置文件$ Z3 N; d! j. q; R5 y
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
. ~) T c* y" R2 J' |; H$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
$ R8 I5 @0 I K1 |' Y$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
! d4 Z0 E& K( k$ cp crs-setup.conf.example crs-setup.conf
! U: {" Y0 {' K. \& b配置OWASP规则4 q3 Y% z2 C5 K+ S, G' e0 X- X
编辑crs-setup.conf文件
9 `7 L. N- [# l7 |& o
& U% X" n( |& M- s Q# @& z9 C- O
# s+ b9 | A3 y7 c w+ v2 c$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf- y* v5 R! O! l4 R
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
1 V9 w% o+ b! u) l/ ?; i0 m$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
% u& J9 `" k) h$ N$ a7 V. t$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
3 ~6 p9 X& t8 w% W4 z3 n9 t( P% a默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。5 s+ g/ V) A1 ]( ~. A$ d6 Z H
9 S6 i2 X b* y6 ~
5 m5 g( }; U$ R4 L4 |0 }/ a/ ?$ f2 H启用ModSecurity模块和CRS规则+ D7 Q+ H* `/ l1 Y" p
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
- K. L! v0 L" l5 s+ b) J1 y6 Q
J! Y- m: _8 d* D
, B% G; Q3 o! ^3 |4 u5 Bmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
6 B+ K! z! f& x4 O+ {) J* n
5 {) Z. h+ r$ ]% g. d4 a* _
; j* B5 d2 G' y" \; y3 b$ cd /root/modsecurity-2.9.1/
' [0 t! c4 H$ A' {& s& u5 ]$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf 0 Z8 n" Z& J; V
$ cp unicode.mapping /usr/local/nginx/conf/
) L g4 H( G+ l7 s+ m* i将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。; _# {! H; J2 c! k$ @% c
: n& O; ~& F- ^ t4 Q% j3 ]9 D& m& V( K. s
$ vim /usr/local/nginx/conf/modsecurity.conf9 {2 F3 y# |4 e0 n
SecRuleEngine On; E# k# w" q, @. F: j; D/ _/ I
ModSecurity中几个常用配置说明:5 G: X; A% U* Y
- g7 W$ N$ D7 s+ ]; l. j @: j
+ R& w- M' `, l4 D, C* Q0 Y
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
, v- c8 c1 U! [+ o+ Y' x$ z6 t; j7 H& I+ x! E/ O U7 I N
4 K3 z8 ]" f0 t- r4 R) Z2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。) c' u% D. G5 C- b
5 g; W( b; Y8 X8 ]
; j' k2 [/ w0 Y _" x+ c) k3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
0 Q) ~5 w; j# [5 ^- o9 |& |0 C" B: L* w3 D& L, u
0 E) L: ^1 ^" m
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。$ g" T" A: S% V/ d$ p) d
r5 `4 A4 c6 l, s$ i! K: B
1 C0 f# i/ K) O5 l' v- R* |在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。' O- s* h4 S- G0 G, U6 `* R P
6 }) r9 m# D ~
) F) n( e V7 y. K/ p
3.x版本CRS
- U! V% j; c% L$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
3 w; u1 p' M! k P9 c. D7 V# 生成例外排除请求的配置文件* t$ b: A" d( Q/ {/ B
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
4 I# m4 f! ]4 Q% d$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf4 W6 o( i* C# [* A
$ cp rules/*.data /usr/local/nginx/conf
# a2 X0 H& R$ S, J; d @为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
0 z" ^4 X* D+ \, \! d7 @ Z4 `+ Y9 r# Z7 Q' z2 R" T' y
0 v) H! @. T1 c7 f$ vim /usr/local/nginx/conf/modsec_includes.conf2 N x& c$ m. U0 D8 w6 _+ Z' F
: B+ L9 J3 v/ E, r' n[Bash shell] 纯文本查看 复制代码 include modsecurity.conf
include owasp-modsecurity-crs/crs-setup.conf
include owasp-modsecurity-crs/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
include owasp-modsecurity-crs/rules/REQUEST-901-INITIALIZATION.conf
Include owasp-modsecurity-crs/rules/REQUEST-903.9002-WORDPRESS-EXCLUSION-RULES.conf
include owasp-modsecurity-crs/rules/REQUEST-905-COMMON-EXCEPTIONS.conf
include owasp-modsecurity-crs/rules/REQUEST-910-IP-REPUTATION.conf
include owasp-modsecurity-crs/rules/REQUEST-911-METHOD-ENFORCEMENT.conf
include owasp-modsecurity-crs/rules/REQUEST-912-DOS-PROTECTION.conf
include owasp-modsecurity-crs/rules/REQUEST-913-SCANNER-DETECTION.conf
include owasp-modsecurity-crs/rules/REQUEST-920-PROTOCOL-ENFORCEMENT.conf
include owasp-modsecurity-crs/rules/REQUEST-921-PROTOCOL-ATTACK.conf
include owasp-modsecurity-crs/rules/REQUEST-930-APPLICATION-ATTACK-LFI.conf
include owasp-modsecurity-crs/rules/REQUEST-931-APPLICATION-ATTACK-RFI.conf
include owasp-modsecurity-crs/rules/REQUEST-932-APPLICATION-ATTACK-RCE.conf
include owasp-modsecurity-crs/rules/REQUEST-933-APPLICATION-ATTACK-PHP.conf
include owasp-modsecurity-crs/rules/REQUEST-941-APPLICATION-ATTACK-XSS.conf
include owasp-modsecurity-crs/rules/REQUEST-942-APPLICATION-ATTACK-SQLI.conf
include owasp-modsecurity-crs/rules/REQUEST-943-APPLICATION-ATTACK-SESSION-FIXATION.conf
include owasp-modsecurity-crs/rules/REQUEST-949-BLOCKING-EVALUATION.conf
include owasp-modsecurity-crs/rules/RESPONSE-950-DATA-LEAKAGES.conf
include owasp-modsecurity-crs/rules/RESPONSE-951-DATA-LEAKAGES-SQL.conf
include owasp-modsecurity-crs/rules/RESPONSE-952-DATA-LEAKAGES-JAVA.conf
include owasp-modsecurity-crs/rules/RESPONSE-953-DATA-LEAKAGES-PHP.conf
include owasp-modsecurity-crs/rules/RESPONSE-954-DATA-LEAKAGES-IIS.conf
include owasp-modsecurity-crs/rules/RESPONSE-959-BLOCKING-EVALUATION.conf
include owasp-modsecurity-crs/rules/RESPONSE-980-CORRELATION.conf
include owasp-modsecurity-crs/rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
( B% W! H* g) j, B
) R: l& D L3 L注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
1 f" J, h }3 ^! K& y5 ]+ d! r5 A; v: O- Y4 Q! G* ?
; ~. ]3 a; y3 \
配置Nginx支持Modsecurity' l- y0 M* s# g+ w) Z a& \" ^
启用Modsecurity b0 r4 A/ O( u r- O2 W! t1 t
使用静态模块加载的配置方法
/ m9 ~, v0 D' i# W8 p在需要启用Modsecurity的主机的location下面加入下面两行即可:( g [! H5 i. f! Y) c
+ m2 ?3 a$ W* N7 T
! f3 A- _' y0 e( j
ModSecurityEnabled on;
" G: L4 J1 a% S, j' Y& QModSecurityConfig modsec_includes.conf; u* A& M. P- A! w& p% p& o$ y
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。: _+ Q# D: r/ L, `6 ?: K9 {7 w
) C4 ]& c ?* Y1 H
* H, [8 w% z* L7 u. G$ vim /usr/local/nginx/conf/nginx.conf( O% `8 y; A B R
3 d5 h. G* [; y& G: ]
+ a0 s& y2 G0 t. r0 J. s! r9 ]# u! ~
server {' y1 A# Y! V$ O. w
listen 80;4 N0 C! L, r$ u; ~4 Y
server_name example.com;0 V) ]8 b! a! J% r1 X' D
( N) @! a# S- L) H: t7 X, _' L0 ? _" p$ L5 c' W- F% Q+ k
location / {
6 M' S6 g- }. l- P ModSecurityEnabled on;
7 c) U3 R) [! _1 q: A ModSecurityConfig modsec_includes.conf;) s% @# u: C; @
root html;
% B' k) W" x3 p% X+ q index index.html index.htm;2 z; Q- f) Y8 f, T( z
}
# }3 _% n8 V5 n* B. g* N1 V}
K3 t3 N$ _9 L* g. L使用动态模块加载的配置方法3 w' J, D- J: g9 W
在需要启用Modsecurity的主机的location下面加入下面两行即可:* f; k- i" n% j f: g
; R* n8 P% f# C% D6 ~. @4 F1 j" u K3 S8 U) B# S4 |/ t# A1 o7 I
modsecurity on;- o0 J H6 H* K6 ]4 f5 b; g
modsecurity_rules_file modsec_includes.conf;
; E/ V, e: {5 r# \: v( m修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。! W* r7 E) D5 B" w6 _$ L
- r, N X# [3 ^5 L2 Y
% f7 q8 j+ J9 C9 I) B. h) \& ^
$ vim /usr/local/nginx/conf/nginx.conf
9 P; F! o2 ?9 r( I/ I
9 j* q) D4 u7 v$ B Z4 u2 K( S* k9 t9 \* |) M0 O2 G
server {( X& i" Z( i0 N O3 H% V
listen 80;; j0 w9 t C: |
server_name localhost mike.hi-linux.com;
H2 y( r5 |; d9 K( b6 V$ p8 k access_log /var/log/nginx/yourdomain.log;) F" G! L' O! [" x! x
! u$ E# B5 k/ I1 u$ j
5 ~0 @, A$ G/ X {3 r* }# j* g location / {" T' W0 r0 X: N& {
' P% J4 M4 G; C0 _" J( e$ D8 J+ }! V
( ?& e% Z4 ]: Y5 |& }2 ^) } modsecurity on;% I& L7 E6 h# [8 X* ^( j2 `7 `, O
modsecurity_rules_file modsec_includes.conf;
2 A5 `' ?( u/ b9 B' }5 i root html;* `# v9 [! Z7 B. L- @
index index.html index.htm;) B" s- o; w) v: u& J
}
. Y U4 V* ~! n; ?' G0 ~. }0 f}7 q4 ^+ U$ O* q$ [9 A4 X
验证Nginx配置文件
- r: t5 b6 i9 g* Y: A$ /usr/local/nginx/sbin/nginx -t
: L; L- b4 G4 K, F. Pnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
% @2 {% x* d5 unginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
8 k. {' ^& Y" S3 d: W启动Nginx, z; O7 n# R2 P# p$ Y! {' F$ Z$ Q) O7 T
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf2 {) f6 v3 M$ H. ^
% F: T3 R: Z/ V9 x3 {6 t测试Modsecurity ModSecurity现在已经成功配置了OWASP的规则。现在我们将测试对一些最常见的Web应用攻击。来测试ModSecurity是否挡住了攻击。这里我们启用了XSS和SQL注入的过滤规则,下面的例子中不正常的请求会直接返回403。 在浏览器中访问默认首页,会看到Nginx默认的欢迎页: [/url] 这时我们在网址后面自己加上正常参数,例如: 。同样会看到Nginx默认的欢迎页: [url=http://img.colabug.com/2017/06/842f48f203c6c2cd30144f29b57af97a.png] 接下来,我们在前面正常参数的基础上再加上 ,整个请求变成: [/url] 就会看到Nginx返回403 Forbidden的信息了,说明Modsecurity成功拦截了此请求。再来看一个的例子,同样会被Modsecurity拦截。 [url=http://img.colabug.com/2017/06/246ce28e95310a32f791893d4f5c55ca.png] 查看Modsecurity日志 [url=http://img.colabug.com/2017/06/ae44dcb58b8a4a0ea761317e398b3101.png][/url] 所有命中规则的外部攻击均会存在modsec_audit.log,用户可以对这个文件中记录进行审计。Log文件位置在modsecurity.conf中SecAuditLog选项配置,Linux默认在 /var/log/modsec_audit.log 。 $ cat /usr/local/nginx/conf/modsecurity.confSecAuditLog /var/log/modsec_audit.logModsecurity主要是规则验证(验证已知漏洞),Nginx下还有另一个功能强大的WAF模块Naxsi。Naxsi最大特点是可以设置学习模式,抓取您的网站产生必要的白名单,以避免误报!Naxsi不依赖于预先定义的签名,Naxsi能够战胜更多复杂/未知/混淆的攻击模式。
( D, @) s, d3 s% {2 Y/ C |