ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
' y) `7 f' s6 b! N: o5 S' z( M, U5 Z
/ J; q. _$ P1 j在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
; \# Y, l7 g m8 X. ~
' O+ b; c, H, A; f _* D4 B; e7 j. M$ t7 L+ \, T4 W; [
什么是ModSecurity; w% U* l5 o2 f! w% L
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
( Y. |- \8 p- {
, ?' z( M* g1 o) k) y' O% ?: u3 F# _; T
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。. K1 w8 g% L' u. U4 f6 [* s& r- F$ |- |
( U0 g$ E4 d2 i! h, ~
~3 V: y6 {7 s) {ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。5 l# t" Q3 `. Y! S3 w O; b+ \3 I
7 t! N/ R3 m% I! W
& @: Q) ~ W# i6 l' b5 n$ @官网: https://www.modsecurity.org/
2 o% L; Y# p' R8 P: c9 v: u/ \! Y
8 f, |) o# i( v3 t! f1 t) w" a( f7 ~+ w
什么是OWASP CRS
S' u' W% ~( k+ e. }( {OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
, x5 E/ a( [7 w& t. |8 t3 [1 Y1 P0 u b$ T, P
* Q0 m2 Q, m2 K. c5 K/ |
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
- d% r% r5 f( T F6 x/ R5 y+ `5 T! Y' N7 \
+ o* R, B3 S) I; `; c
HTTP Protection(HTTP防御)
4 r' M' K5 Q* W" j" x7 vHTTP协议和本地定义使用的detectsviolations策略。
1 ~) D. K/ Q/ i$ p" R8 D
& N. v2 V' x+ z' _
4 k" V. h' ~2 C# UReal-time Blacklist Lookups(实时黑名单查询)
. _. q' w; Q; R x8 _利用第三方IP名单。# l; `3 `6 l5 E$ ^& F* V
$ h1 s R5 z* C3 c) d5 l5 R5 i" ]8 D0 G v) ]% `- w
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
' S) g( d5 H1 q, s# ? U防御HTTP的洪水攻击和HTTP Dos攻击。
5 H7 Z( @. |7 I' Q
, \, w9 k! T+ A; H
2 j8 v- @, J6 |/ y( W |Common Web Attacks Protection(常见的Web攻击防护)1 ]/ U' g X$ ` S/ z! n
检测常见的Web应用程序的安全攻击。3 a* H! m; Z9 T) T7 k: I8 X
) U5 {) U5 ], Z- }/ p% q$ W: |: z
( E' F7 S3 }$ t4 L3 a8 j4 p' }Automation Detection(自动化检测)" c- F- R; H* q
检测机器人,爬虫,扫描仪和其他表面恶意活动。* r' d. k) Z. \# q6 e }1 X9 w( X* C
2 D0 J, @5 q9 f- n) [& R
" x2 I" V" }8 w5 \7 Z( W
Integration with AV Scanning for File Uploads(文件上传防病毒扫描): j4 J/ d) y$ b* [
检测通过Web应用程序上传的恶意文件。
D# u3 l& B) [! G; l' h1 `% [" s& } a
$ X- {8 X: |. ~& t; Z5 ATracking Sensitive Data(跟踪敏感数据)8 V+ [! |' \$ d7 j" c( T1 {4 C2 N
信用卡通道的使用,并阻止泄漏。
, b' w5 n0 c9 g- P& u' m1 G1 x( g! R
* X' ]; V" {3 i1 d( u7 T( W
+ r! }' O J* ^( MTrojan Protection(木马防护)* Q; ?% d( ~2 W( w5 _% u
检测访问木马。% p( f$ R( ]4 @' [/ @ h
( Z; g* j5 o1 ?
4 ]7 O% q" z% V8 r! rIdentification of Application Defects(应用程序缺陷的鉴定)" N$ L5 \' y" S" C7 i# W6 y
检测应用程序的错误配置警报。% U4 ~; |1 f3 ~! x+ F
- j% X: E! g7 d
$ S8 o* y F- N* L
Error Detection and Hiding(错误检测和隐藏)
. T6 _0 b& v0 Z检测伪装服务器发送错误消息。4 l4 |1 ^6 }8 i: l, o
" y; v# t! N% N5 a7 D
3 D' ?/ v, O. I, y# ~
安装ModSecurity: A+ Y# v- L3 a9 I2 B. U' {5 u n5 J
软件基础环境准备9 ?- q, K3 D* I& l7 w J
下载对应软件包2 T3 `; e. r+ g/ d% j+ x
$ cd /root8 o6 Q- x$ R) O5 o( z: y6 ~
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz', {! h' x; X4 P2 U
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz& h$ i S4 F6 U# r* y% i
安装Nginx和ModSecurity依赖包+ x! k2 V+ n- k
Centos/RHEL( T' c8 m8 U3 p! v3 |2 U7 E2 _
/ \. M; H9 ]# }
1 q0 J M& o8 p: [( Z$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
- j) X! L4 E J) S% Z. L: @Ubuntu/Debian4 [( c& y( A3 j# p3 a
) T% M) ~' n1 o3 {7 U' j W# E! Y+ C/ |% r
$ 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. l- `. q5 P& S; C" g& S' \
编译安装ModSecurity
$ ^! C$ B, B- y \Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
6 r7 S, ^) |6 o3 r2 ~/ e/ { X
5 r" y5 d9 }! u; j$ B4 {# Z2 N
: l, x3 Q) D2 s方法一:编译为Nginx静态模块
" U8 K3 [" p% \. Z$ F! [: B* o; B. y) f1 p8 u: V
L+ |0 J* e5 o& |, e2 a编译为独立模块(modsecurity-2.9.1)' d! W0 _* K( k/ C+ p* g* A
$ tar xzvf modsecurity-2.9.1.tar.gz7 ]+ Y% Q8 @7 e. n2 o% q
$ cd modsecurity-2.9.1/- i; C3 U- f# V# b5 G# N
$ ./autogen.sh
' i+ l, H2 ^+ H1 v$ ./configure --enable-standalone-module --disable-mlogc y& Q0 _) f; b9 V3 r
$ make/ }( l1 A- @8 I1 s
编译安装Nginx并添加ModSecurity模块
, F. W4 M# u, Z* ^7 V$ tar xzvf nginx-1.9.2.tar.gz2 [- f0 V* {( o
$ cd nginx-1.9.2. V Q8 }$ _4 ?: k
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/& }" s$ D" _% \5 O3 R3 ?7 w
$ make && make install
4 k$ { i! G: z3 w1 Z8 O: g9 f方法二:编译通过ModSecurity-Nginx Connector加载的动态模块/ V7 H4 g% P/ K5 R
& |+ K- m/ B1 F! i4 j: q- v3 l. g; i! I3 ]
编译LibModSecurity(modsecurity-3.0)
5 \, O+ P: D5 m' p# U/ U$ cd /root
% o; g( ]- }% C, c( X) ?$ git clone https://github.com/SpiderLabs/ModSecurity
: J( S6 w1 n7 b! U# n$ cd ModSecurity; J4 C' B9 M& [! D, p9 g; X
$ git checkout -b v3/master origin/v3/master; K) {+ Q! S) t. K7 d' o( C
$ sh build.sh
( I0 @ s" S" p$ git submodule init
% o1 I$ R6 M5 [7 j8 |& H3 j+ A, S+ l$ git submodule update
3 K' a, A; q! }/ A: S$ ./configure ~: X! k" Z, W" Y2 ]3 g
$ make; E& Q' j2 c4 {5 T7 m! U# R+ O
$ make install8 } A0 q. G0 p: l: q. Y- z4 S
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
! c9 A4 d: q0 w0 I7 D8 W+ `& l9 M2 z" E/ } b
# p; M" E7 }* h9 R! z, }$ ls /usr/local/modsecurity/lib
3 x0 N6 Z9 s& \8 A7 C" ]+ H2 ?libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.04 q- u& f& g! N6 H
编译安装Nginx并添加ModSecurity-Nginx Connector模块% a2 o, J! J. ` z3 D) ?* t
使用ModSecurity-Nginx模块来连接LibModSecurity; r: p( y+ G. N' [ }- q
! E/ |! {5 S' T8 X. W+ o: m/ b
2 o5 b7 Y1 s! O6 U7 ` U% z$ cd /root
+ A- G$ O6 c8 ^" t5 [9 V$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx) G/ w/ f, X1 j: Y0 @
$ tar xzvf nginx-1.9.2.tar.gz
' `; n: b* S2 A* D2 Z# Z, O. E$ cd nginx-1.9.23 f5 _1 T7 r& j9 e
$ ./configure --add-module=/root/modsecurity-nginx M# ?% ^* N6 g% h$ \- d- _
$ make
$ |2 p4 B _; X! G9 n$ p/ M+ o$ make && make install
/ z" p5 E, i& z添加OWASP规则
- T" Q" U( u8 X% x" I9 XModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
) }' ^( A4 u, s$ w$ \
, Z8 k0 g/ b& @: A! U: m( L' y6 s2 I! N2 v9 z Z% g) L
下载OWASP规则并生成配置文件# p' }/ n8 l. B4 \4 |* |
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
# S* k, s5 Y! B( s' a$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf// K8 x8 y( J! s" i
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
N( G1 y) {6 e3 Y$ cp crs-setup.conf.example crs-setup.conf
+ v$ _) g9 _9 y4 ~4 L配置OWASP规则) N, c2 T! X3 K$ f+ g
编辑crs-setup.conf文件6 T/ h0 e* B7 D( h4 W, `
6 ?" H3 i: D6 a; k9 G* g7 f1 c9 j P
; {3 \' O( f* f$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf- j3 ]2 L: }! i$ ^( X
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf9 K5 W8 c5 t) q& C) j: n) W: J
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf* }$ w! t6 s4 |. y
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
9 U1 N* [: I( ~8 a默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。1 i) u/ o" X6 f0 e. Z6 h( a
% n! f# ]+ r& A) a( {8 O
7 q2 i) K+ H$ e1 y
启用ModSecurity模块和CRS规则
% k! U d- X# V复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。+ s/ D7 \9 b4 `* v6 m& n C
5 q' e8 V( D' w, Z% s1 r
' P" W, |' |. m6 R H* u
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。 t$ v A4 s9 [' ~2 N0 s4 K$ g
2 c# k8 B- o6 h5 F8 _6 s3 u& s
- a6 ?1 v2 H# K) {% \ H$ cd /root/modsecurity-2.9.1/
& G; G H2 I& ` g2 \$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
9 N. J' W& d; N- \$ f3 z$ cp unicode.mapping /usr/local/nginx/conf/' E W% z# p+ o- m5 p& m
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。+ i8 i* X Z; k' b# p3 G" y6 T
. B5 U' I1 d$ X. @* c' Q- Z. n ?
+ K, @' O! T5 H# h
$ vim /usr/local/nginx/conf/modsecurity.conf& P4 |1 P9 ~: s( d9 ]7 F
SecRuleEngine On0 o. E) n9 u$ q6 O, ~
ModSecurity中几个常用配置说明:
: Q& h+ D h0 U7 x4 R
- B. O* u( x/ j( u' |9 e: E* L5 a0 p* Z/ }
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。 B& D! C: k7 c5 p% b) `
6 p/ k3 d8 A. b* f- i2 s3 W$ X4 Y3 R- M0 P2 Y
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。. K. ~9 e. e) j# L8 |' r
0 O6 z- J5 D. L6 P' }( n+ ~# W, w
$ b6 ]: X6 b* C0 g& D7 F- M3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
* Z$ h7 K( R# @ r: N' J
' Z+ x/ G. F* y. I R
: `7 W4 J" e/ q& z2 C, z+ Y4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。& ?, s/ }, Q) i# q1 f8 e0 ]
# Q% o& L, x, C0 {1 ?; s) T* B1 X) H! w% O3 q8 Y2 {
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
( X7 |3 X/ B4 u
: {# k# X4 `- d P$ y) @4 c2 E6 s" \$ B) N& c. C6 e
3.x版本CRS, B) c1 W& k4 j0 U: B
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs( \4 m s1 i0 N* K( _4 \+ D" m2 K
# 生成例外排除请求的配置文件9 H# i# X% t; D$ ?$ V; G4 L
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf8 [. x" E; [$ b, ]% X. g1 y0 C- k
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
9 H. |2 q7 T$ ~2 I$ cp rules/*.data /usr/local/nginx/conf
' Y2 R1 q* d, ^1 O5 m5 r为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。 C6 o; ^7 m$ n: O2 x: n# D9 ~: Y) I; p
& q" r) {: j( B. a! X
. O4 Q3 ]% y1 [8 D* N1 T( M8 ~/ h0 b
$ vim /usr/local/nginx/conf/modsec_includes.conf# W4 B0 [8 `- V. E7 ]7 q# w3 }
% m9 \2 ]0 X8 h, f U1 q! ?[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
! |+ ]) X; _) N4 i% |, g3 N; a
3 i1 N' g i9 t7 f5 d: j# ^注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。2 G7 I. l, q- z8 o. L3 `
. W( N1 I/ \# v( L0 i' K) T
4 F5 O1 I7 k a. B6 B配置Nginx支持Modsecurity
: b) X. ^3 {! q启用Modsecurity, U5 ?3 T# ]9 l4 b$ m: R9 D
使用静态模块加载的配置方法
/ J* v; b- K& Y$ ?) @, C. x在需要启用Modsecurity的主机的location下面加入下面两行即可:, E4 o4 }. g8 p" M- e y8 N& }) W
! ~: k) f( V& |2 c6 Q* a2 A$ z5 U9 ?0 v$ E; _" N# m
ModSecurityEnabled on;
, ~6 N3 T2 S. V* K: e6 c$ sModSecurityConfig modsec_includes.conf;
7 M, m- r7 ^# k; z修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。) z. S+ x6 Z8 t- K
# H4 \9 }1 q9 c7 j9 u
+ e: m% v$ B' g7 X6 ?( S: j+ q/ F
$ vim /usr/local/nginx/conf/nginx.conf
" O9 O" [- B! q! h6 k1 ^0 b' S! y4 W7 x& X7 j
- T+ q) S7 a- B; o a9 w g, U" Userver {- d% A {6 ]& C$ i9 c! J" ?2 S
listen 80;
7 p- T6 @0 P1 F2 _9 G" [# R" \ server_name example.com;
, A) \& _8 C) a, k; n" Q& n( U( y
& x% q5 [' r; W
location / {5 s. A7 H6 x+ R& d; n6 {2 p
ModSecurityEnabled on;# S% K+ L7 e% o: d t1 E/ s, g/ a
ModSecurityConfig modsec_includes.conf;
+ K1 N* C* R ?5 r2 S4 I! ?* w root html;
# Y. _2 s: u, d* G p; E index index.html index.htm;
, y5 |4 t! H' ?6 _1 F4 C }
/ x. Z! V; q* ] A, @( }9 h}
) I. l9 S" }1 b( @9 I c使用动态模块加载的配置方法
0 G* Z8 u V2 `% T8 K在需要启用Modsecurity的主机的location下面加入下面两行即可:
- s4 X' Z+ K& S1 F" @2 ~8 G8 N. L, k0 t7 P
4 k* r8 j S1 p& V& H: a
modsecurity on;
7 N( y5 I; I! x3 o0 omodsecurity_rules_file modsec_includes.conf;
3 h5 U- A1 O* K( W c- t修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。+ i5 x5 U& s. j- t/ ?3 U
0 o. [% t, j5 P# o6 }6 M# U
1 B) [% e; R$ o/ J7 u, |: \$ vim /usr/local/nginx/conf/nginx.conf0 G% U' W5 D H
3 B$ c) I+ _1 |5 t
/ T! S/ Y- E. ]- V gserver {: n! z( }1 @( Z1 s! y1 _- `+ ~
listen 80;" H! W9 _ f7 y$ X
server_name localhost mike.hi-linux.com;
( ]2 a- B1 l: v7 p! P2 |+ q access_log /var/log/nginx/yourdomain.log;8 P, m$ O5 @- G, A6 p" Q
) @ I0 K. ^! @1 E% L( n: G
2 M" G+ `" p$ }5 o! p location / {
. j. m2 z0 u0 U& [+ p
! M" d! N# q4 o, B
# }" _9 i+ }4 r modsecurity on;3 e& V1 K" j) o* `, D8 Y
modsecurity_rules_file modsec_includes.conf;: q2 U b% k1 H: q. ?# H8 H& _" e. U9 c; h
root html;" D0 o: `2 r! f3 ^/ j# Z) r6 C' j
index index.html index.htm;
' e; p: P; |7 f+ P, t+ W ^}
! a$ s" ]# e& q- m}9 L! u; @5 B4 [& z& q# t1 l; ~
验证Nginx配置文件
" A# G8 }; D7 C! w, h$ /usr/local/nginx/sbin/nginx -t1 \4 D7 \: b9 N& {' J: m
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
$ I" T1 @( d7 Z- C, _nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
. [& D E( \" ?) ^4 u4 Z- H5 \! z0 Y启动Nginx A( E: O' |8 ]& m0 O8 }
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf* K6 W: A! |, s+ ?5 U8 N+ c
, ~, ?% o$ J* `! k- i- b
测试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能够战胜更多复杂/未知/混淆的攻击模式。
5 k" _+ Q$ a3 h* ^ |