ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
& G# \5 m B. F: `' N, E& j% X, f" y, m' p6 M% C5 X8 `3 |
/ U3 k6 e, w3 u) x% S
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
) r* N1 T! O! O
1 W9 W2 Y7 l2 f) q1 k7 }5 r
3 y, s- q E3 I, g- e6 f什么是ModSecurity% _# p( r: H$ a! l$ y
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
8 ^/ n$ p6 t( Q- K1 G
/ f; S# H! o- d0 C: \
7 z: ]2 K; `6 K! ?7 |* p, PModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。8 E+ W. D3 E4 b: O
# p" t: v3 ^1 o$ H9 W
6 z$ e, u) k# Y* {. ]9 [/ {/ XModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。/ E9 e! T. n2 N _- [2 H6 B
, r* U& E9 |+ V, b: b
1 u# x2 Q6 C8 ?官网: https://www.modsecurity.org/
- D6 U0 C/ W) p( K+ d( _: m0 X
9 I6 {& u( s4 d8 G" G
8 r/ G# }5 v [% n什么是OWASP CRS4 E6 ^% \5 |& \8 A7 q1 n# f, ]- P
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。# w# y; r! ]% ~6 p7 d- A
1 [. A2 Y# u' K3 Y% h: W8 }$ Q
+ p& B8 o6 ^# s0 o/ ]# S& bModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
6 j9 B1 R$ F3 ]
0 u# p9 M* Y" k5 Y6 Q3 N
# W8 ~5 T1 ~0 ?. O o/ U$ lHTTP Protection(HTTP防御)3 \) T: u; Q* j0 d! k6 B9 x# W _
HTTP协议和本地定义使用的detectsviolations策略。' F* I9 E" X/ K4 y u9 E
+ H% f* ^( m7 S1 k" e" J; q2 B3 _6 ^+ l. [: W# S) W
Real-time Blacklist Lookups(实时黑名单查询)
s& u1 t. v' I( t: |2 M0 j利用第三方IP名单。+ g. q& Y2 o2 A0 ]' Q4 f/ T
* ~2 X; P- I5 G- M* I
% Z' V2 E0 h5 ^" X8 a: B3 T! OHTTP Denial of Service Protections(HTTP的拒绝服务保护)
6 i) t" z9 }! e0 P- ~4 f' A9 }防御HTTP的洪水攻击和HTTP Dos攻击。" ?9 H9 ?8 h/ z0 U
' C$ T% G6 T' T6 ~6 K$ v
% a" V" `% h8 VCommon Web Attacks Protection(常见的Web攻击防护)
$ _5 L; M, G2 ?! V' B( ?3 U8 n检测常见的Web应用程序的安全攻击。6 a( ?) m0 g/ |6 i) a% H# y; S
: u! @6 X9 c1 h9 E
0 Q7 _7 g) m0 W& h% aAutomation Detection(自动化检测)
, S$ w) {6 }3 Z k检测机器人,爬虫,扫描仪和其他表面恶意活动。
: z5 c; H. @0 h
5 w7 e* W2 j6 D: Y( a- i( a0 M7 }) O# ?/ s; p+ y2 D
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
( k& p1 S z. {: ?; h检测通过Web应用程序上传的恶意文件。
5 I0 |1 c$ P- q: [
' F; J6 K6 |" N
& i* t8 R: L5 t K3 e. d; i9 TTracking Sensitive Data(跟踪敏感数据)
. j( _- \, a. j" ^+ x信用卡通道的使用,并阻止泄漏。
6 c$ Y# F4 f6 j% F; C1 X' K- V' \+ g
, k% L) _$ R0 v; CTrojan Protection(木马防护)( }+ a! U4 Y: M! ^! A% P; @
检测访问木马。
; X3 |( f$ k' H; ?" u. u, l; V' C* F+ i& K" u
c& ]4 r. p }1 [: l+ h# T1 a G" u0 ~Identification of Application Defects(应用程序缺陷的鉴定)
+ R. f6 o8 v# a: A% P检测应用程序的错误配置警报。
6 C+ c N& P1 k( ]
) _$ h0 y# e, x. V) L3 I5 f+ y% f7 S/ d9 P7 i5 R4 m. Q
Error Detection and Hiding(错误检测和隐藏)' Q7 J7 }# f- f7 A
检测伪装服务器发送错误消息。+ e" A3 h7 Q9 w7 |0 y
4 { v7 H0 k& Q- c1 s# K8 u2 a% y x
安装ModSecurity
/ T5 D# z* T3 n5 \软件基础环境准备% i7 n7 R' y9 ?& ^& f( P
下载对应软件包 |# y& W! R2 A! s# g8 F
$ cd /root; c9 F* Y1 e# a" g) t
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'9 j6 d9 R! `/ r4 t* ]$ N
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
6 I6 P. \: N- T1 {9 I安装Nginx和ModSecurity依赖包5 _! ]6 Y) P G
Centos/RHEL8 P0 e" q6 V2 w% s- v9 W
# ~. _# m! N4 R! Q
7 ^; _; K9 C, T5 K" d% k$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel, G ]9 T( Z' O, B: D
Ubuntu/Debian8 X. c. F- W: G( y
( a. u! U1 ?$ K0 P4 W. I4 `$ V. n; x, X9 P; L
$ 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! g) V' u! K7 w, m+ a+ c& [8 f4 ]
编译安装ModSecurity6 K# ]" V) y. U! @$ y1 J% v6 i' }
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。# ^" I; ~3 O$ K, X& i6 g
' V6 x* C; T1 N5 s7 V- n& {# Z
* Y: _" v% M: `方法一:编译为Nginx静态模块9 {/ O/ Z$ v" Y5 c4 e" i( c
# E6 o0 W. g" m, A3 W' Y) J! i$ Y. H, G4 t
编译为独立模块(modsecurity-2.9.1)
. m+ V5 Q) @2 M6 E8 b+ x. k$ tar xzvf modsecurity-2.9.1.tar.gz
. x3 @4 i0 @* D1 k0 x5 O m$ cd modsecurity-2.9.1/9 k. E' `/ V5 k2 L* O
$ ./autogen.sh
' I f, A1 z; c& Z* Z$ ./configure --enable-standalone-module --disable-mlogc
. O$ K: ]# }1 L- f$ F( F$ make$ E+ n. s1 T9 T e' f0 n. Z0 _
编译安装Nginx并添加ModSecurity模块
) K9 I6 a# ?0 k+ W1 @" F$ tar xzvf nginx-1.9.2.tar.gz
' f8 r3 ~$ @8 L3 p* P* b g$ cd nginx-1.9.2
: f& C. H' w% Q2 I+ w$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/" H" v+ ~5 z! i+ k8 b- B$ ^# p
$ make && make install+ C' _+ |. L. |
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
+ f) C5 ~$ O% F6 C, f6 a3 I5 S& s" V, }& f9 V) B$ I* Z+ v+ B) c
+ E/ I/ O3 o' R: |/ U! ?% N+ s" A编译LibModSecurity(modsecurity-3.0)
3 T' D* d: c F9 [" H0 u$ cd /root5 D7 j1 n# s: N
$ git clone https://github.com/SpiderLabs/ModSecurity
) z5 K/ t( E! a( j! K$ cd ModSecurity
0 h9 h/ S- b8 r& O, g0 s$ git checkout -b v3/master origin/v3/master
& X$ v6 z6 t' k+ A& k* a% M+ C$ B$ sh build.sh
# n1 G+ } x9 _$ git submodule init& v3 a7 f' ^5 x2 H
$ git submodule update( f% E9 J( O8 q) Q/ d( i
$ ./configure
2 S4 K3 a5 q: o& P, K$ make; h7 S* `7 t% d
$ make install
: `. g1 _/ ^' I/ ^% N- n8 l0 {9 o7 |LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
) p7 x7 h N1 O! o$ ?, s" [/ l0 _; S
/ Z7 }( Y0 b# W' @
$ ls /usr/local/modsecurity/lib4 S" x5 l' T8 R t& o2 U
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
2 c8 C" i. g0 T' a8 C% i( a编译安装Nginx并添加ModSecurity-Nginx Connector模块) J5 Q: i6 f: f9 ?: a( q. K
使用ModSecurity-Nginx模块来连接LibModSecurity
( A: e; o3 N8 B- V( s) f' b6 H+ O+ v8 Y, Z0 x
! ^* D S* f. y+ B
$ cd /root0 F5 \3 K, K! ~
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
% E/ V( F% _% T3 k& T: _) E$ tar xzvf nginx-1.9.2.tar.gz- I1 e% j( p/ c4 h, b4 T
$ cd nginx-1.9.20 o, G7 |' O2 `4 _
$ ./configure --add-module=/root/modsecurity-nginx: [' E' l. g$ [9 z
$ make
# V. ?2 f1 I% w$ make && make install
7 }' Y$ u" _+ N& C7 `1 K添加OWASP规则! [! ?. a( M5 v# o
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
- X4 J: A, ^- K5 D3 }% L- \- b* S6 l% h. k) V0 F9 c* ^
/ l3 y) b. | {
下载OWASP规则并生成配置文件
2 I# i }, U. g1 n7 N5 e$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
7 q$ n4 w" h0 _/ \3 ?( K$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/% T% `& t8 x& F+ x! O5 @
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
: J; v6 \8 c+ a% r7 i, |5 W1 f$ cp crs-setup.conf.example crs-setup.conf
; k Y) Y* e5 I/ j配置OWASP规则
0 r- ^/ O/ C, g编辑crs-setup.conf文件
# t7 M& h# X* S; @+ o5 U- D" a% I0 c7 { p- S+ i
4 \/ O. {5 v1 S" z! h2 P* _
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf, H/ Y# Z+ }0 U5 X5 _4 M! I
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
: y/ u; H0 [+ T9 l$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf2 M# s+ _. m5 O/ C$ S; [
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf0 Y0 t. k3 A- F
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
- ?+ b; C; r7 v* x
$ u% W# t' h0 k( U, f0 t% E+ e3 l5 x- ]: F1 d& ?. K
启用ModSecurity模块和CRS规则
4 ?% B. \' [% X7 G3 w0 F复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( Q0 ~, K4 V/ @6 S! D& C j
/ a f) T: R& _1 p
- U$ v; s. D- B/ w8 e+ Dmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。+ p" x( O$ K, @% I4 a U9 J9 a
+ F( O* c# ^) Y# p
1 p% N7 x K" G3 t* q' y9 Y0 v$ cd /root/modsecurity-2.9.1/' l! q: s/ R N% x& `
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
7 v6 ^2 D9 m3 Z. ?! M$ cp unicode.mapping /usr/local/nginx/conf/
+ l- s* \( l5 |2 m5 A6 B将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
( e3 z6 K! f( P; r0 K
+ F( n4 f$ n- T* `. T" b- f* B: Z7 e5 m
$ vim /usr/local/nginx/conf/modsecurity.conf
( o( H) a% o1 ?, H! BSecRuleEngine On$ K) x" _( O& Q( f. g$ I9 `4 G
ModSecurity中几个常用配置说明:8 b) Y$ \) t8 f8 }
$ e: w$ e% B% ?( ~) I4 ?7 v
# P( P' ]8 K. O) ]% R1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。: \6 _' O2 P/ U* e. m
7 F7 q! u9 p `3 X( K! E
+ u7 r" p" r$ p2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。7 y/ [4 h7 B- D* G8 l
5 t; q* P0 H1 E# F, Z
, e7 R7 l* y; M5 Q+ T2 F! ]4 y3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
1 f ]! O; G! x* L0 n9 g
3 b K3 M% s$ U/ j! _0 e* O) U5 \* b+ @# j b
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。) l1 O/ |: g: \) B9 B
7 {) b2 p1 i, o' z8 b: }0 O9 j
6 Z: ]9 D' f. Y1 G; q在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。/ A8 v n0 T) @+ Y' R
7 n' S- c. P. v5 x" u8 ^" `3 P2 ~- v) z( V5 ?
3.x版本CRS
0 M7 T' o% [, U9 j$ cd /usr/local/nginx/conf/owasp-modsecurity-crs# Q, Z6 z! ^8 u1 T9 [6 k
# 生成例外排除请求的配置文件& w( V$ ]. D Q# s8 G
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf$ I8 E' |. F! o- U, |1 q% _
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf' i& k5 ^1 T( H5 E$ v/ e! h
$ cp rules/*.data /usr/local/nginx/conf
0 @5 I$ V; o# X6 T/ `; A7 c为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。$ X' n" W6 Y1 {
' |3 j$ b" m& k9 F+ ]4 `: Z# U8 U9 E/ C; [ b4 q$ {' t4 g- l
$ vim /usr/local/nginx/conf/modsec_includes.conf
, m5 _* K0 G! z" h# q9 y1 }; O) o' I) A
[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 % j4 t. I8 i# w. [, N
+ D, i" I6 b+ Z" c; r7 P! z( B$ E注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
* Y5 F# e8 o- s* s8 I( q7 h2 l, G& @4 |5 P, O* \
. B9 Q) B9 R) e& x8 J
配置Nginx支持Modsecurity- h1 ~: e# `! O: v( \9 B8 w0 ~
启用Modsecurity
# F0 |/ }; w' K3 e8 A( z' b8 S! X使用静态模块加载的配置方法) P. ?/ P4 Z( I' g
在需要启用Modsecurity的主机的location下面加入下面两行即可:$ l! ^8 r! w$ a$ O* F
, ~' Z/ \6 X: D( t w% I l9 _; S' o* r- A2 ]- q, J
ModSecurityEnabled on;. F3 w: V6 Q4 e' H; V) J0 [ _1 h
ModSecurityConfig modsec_includes.conf;9 g: ~/ p$ @; {& \
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
1 a, j( T, `1 b( D" X' s, x2 F: ]1 L
5 c* h' E, @+ c, j4 p$ h% B8 y* z' Y K* M
$ vim /usr/local/nginx/conf/nginx.conf
& P( U' G) ^* n: Q
3 |4 D$ }, p, ^0 T0 w( R7 b- H8 @+ G
server {
0 p. T7 {5 [ F4 e0 ?9 r listen 80;
) c5 U4 b% X1 _! x4 g server_name example.com;
; q" `* i4 V( u& ]/ I$ K. `- }
$ @% Q* f0 X X% p( }% c: u, n, \( d$ }6 b7 r; O$ K
location / {8 k& {. d! R$ Z0 B, R
ModSecurityEnabled on;& M, _0 h* W# E- U9 m4 k% e( E
ModSecurityConfig modsec_includes.conf;
6 ]1 m! K# s8 U* R) u root html;
+ H3 l# i; W6 x8 b index index.html index.htm;
7 v! _/ U; c8 ~( t }% d C1 B* m$ p1 O* u$ Q* [
}
2 H8 ]: S: {; ~- O: ?使用动态模块加载的配置方法
4 j7 J+ P9 ~, ]在需要启用Modsecurity的主机的location下面加入下面两行即可:
% L( q3 q; s2 V. f
3 l1 l. F$ V6 r- z. f3 \, {4 S& [' R4 v1 J* Z; c
modsecurity on;' D2 Z/ g9 j9 R) \$ J$ a0 Z
modsecurity_rules_file modsec_includes.conf;
' F$ W$ `5 i6 o& d& o5 o& n修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。" R+ B Y% Z7 H- E- j
& T. d( C2 x0 j! c `2 S+ d( i
% H" ^0 ?5 E+ p9 @& z$ vim /usr/local/nginx/conf/nginx.conf
& r/ i' s1 r- M% O" j5 k
# y# Q& Q% ?; u
8 y* Z9 l$ b! h5 ^. ~% A0 H4 tserver {
4 a, m- K& `* b& { listen 80;! O! l! i) D9 e, z: ~% ~ x- L6 C' B) s+ F
server_name localhost mike.hi-linux.com;) Q, q- m( F7 o& }+ `) G, P
access_log /var/log/nginx/yourdomain.log;
8 H. R2 ]4 I- J" x% n; e/ C7 N: j
" K" C" h1 V/ N- R/ N9 h v' T
7 m7 I3 v' `# u" j( V/ ]: ~3 B% Z location / {
- ~) U) j" L" C9 i- ?% c8 Z/ h4 d; @6 ?( B
. r0 E$ V% O# w$ J' X" U6 y9 N) B
modsecurity on;. X0 U0 N6 @# k- i) N
modsecurity_rules_file modsec_includes.conf;- }! h$ f7 R6 ?: P4 V
root html;; |' d2 l" k& }" s( ^* R
index index.html index.htm;0 s! O& S/ `) \% G3 k& B
}
' ?& n' ^, r. Y [! ~% o7 `}
. a9 _3 R8 P4 C! W- w3 ~验证Nginx配置文件1 P. q1 C- e3 v; ]
$ /usr/local/nginx/sbin/nginx -t
+ R6 K [& r/ n: d% mnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
e8 O0 b: u% o; y/ Inginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful' q. T3 W) V q& o3 o
启动Nginx
& }/ h2 v1 C8 q( ^$ F$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
4 ?% c" N- c( ]( m. W, a( \! h
- C' [* h; u2 E7 A) N q测试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能够战胜更多复杂/未知/混淆的攻击模式。 , k2 j" Y: H/ P" b8 {4 o- E
|