ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
! i8 T, C4 G9 j3 G A) z
( b3 l3 v% `0 g6 b" V1 o- O: F
8 V' ^9 T: E" X在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。& x1 N& `9 e1 C- K- b3 R
. x- q9 Z" p" Z. n: A* H
( Y) U9 ` _% {, v$ [+ _5 t什么是ModSecurity
4 I0 X6 \. g) L6 g1 y9 YModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。7 H# g' Y! ~2 \- k ]
9 C9 y9 B; n' J; ^
' f2 l0 [: X9 V* {ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。- S" d* T3 u; O/ q
$ t5 x0 H0 J9 x
+ v8 Z5 Z+ l" W* p* y
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
/ z3 O9 h: c3 D) i5 v T7 S# M# w6 Y; p7 U- J' K
. Z! p; e/ }4 k0 R4 r官网: https://www.modsecurity.org/% W9 r0 H! d/ \$ M8 h. p
, }6 S! s( `6 B2 C, {+ p( m
. e ~/ P9 |3 v8 ]
什么是OWASP CRS
S' V$ g$ ?% h: V0 ~0 g( G7 TOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。2 c7 J Q/ J9 Z6 K: W1 W7 o
5 H$ S& K% T9 L7 Z7 F. ~( A* c
, H2 P. E* _+ V+ }6 ]ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。' H4 \ S7 y5 S$ ]5 }
- ^' x! w9 t: Q4 Q8 _! z0 ~: Z: g- _; O9 M2 I
HTTP Protection(HTTP防御)
) l* ?# x* ^- P' c2 \HTTP协议和本地定义使用的detectsviolations策略。
2 h8 y. x! v8 y) F- C g" q# |
; I$ X# B5 ?! J/ V2 O$ XReal-time Blacklist Lookups(实时黑名单查询)
( Q" M. z W5 Y6 X3 w; b利用第三方IP名单。
0 X) l# x$ o, Y0 S. |$ m/ A! c
3 U0 n* ^2 t4 u
/ T& S! z& v/ `$ ^2 }; ]5 o5 lHTTP Denial of Service Protections(HTTP的拒绝服务保护)3 y4 ?* q I8 N
防御HTTP的洪水攻击和HTTP Dos攻击。
0 n% b2 J. }, |( Y* q; b
! u! }- P' R/ B/ Q, X9 L( s
$ z9 I5 C s( @, ~; i- U8 ~Common Web Attacks Protection(常见的Web攻击防护)
' m& I4 P" B$ A, z检测常见的Web应用程序的安全攻击。
+ S: @: \. X& Y* Z1 C
* J/ _: J1 @; {% @% S
9 n! D( d t) \% U9 EAutomation Detection(自动化检测)
6 p+ h: E: l& @& G$ c+ }, j! ~/ \3 ~检测机器人,爬虫,扫描仪和其他表面恶意活动。4 R* F& n$ H! ^+ d
l' B( z1 {3 s7 H" |; D& U
9 I7 z o2 f, ~. [4 CIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
; T$ o5 v- L9 C" H1 w' \检测通过Web应用程序上传的恶意文件。% @; H3 y! G' e4 N- M R
- r. K5 V. i; v) |; @# C: x; F4 y4 W0 V- J: v1 C( B7 n- o
Tracking Sensitive Data(跟踪敏感数据)
5 e! f9 a0 A5 r# @信用卡通道的使用,并阻止泄漏。
3 N" x: Q+ Z' `. O3 {8 N
# p3 ]4 U- `6 }; _6 U& U* @8 W0 m" A( P( W5 U
Trojan Protection(木马防护)3 U. Y: L9 G6 Q2 w+ n
检测访问木马。
! Y1 i0 i6 P8 Z
& u% G+ p4 [9 y( G* Z1 G$ P* L
( e3 d4 m; W# H- n) b0 H+ @Identification of Application Defects(应用程序缺陷的鉴定) O, v1 i4 X# i' Q. S3 i8 ^& Q
检测应用程序的错误配置警报。
) ~1 P4 S1 F9 h2 d9 s; T1 |) {) {( W9 m) m7 C H; ?
6 [" o( i, H( I3 TError Detection and Hiding(错误检测和隐藏). J: A1 C, |5 l& p: [# P
检测伪装服务器发送错误消息。
7 D7 ~! U0 P( e! V, Q' ?& {" N1 L! J! P+ h6 V! U
# ?- l2 O0 @9 l安装ModSecurity
; M6 N2 W3 l% V/ R' w软件基础环境准备$ i: `0 W/ P2 {$ Z: [. x4 V1 x
下载对应软件包: R7 ?& |" D; A) B
$ cd /root
9 O& l& l6 t: N4 |0 X$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'+ ^6 u7 f: j9 K, d
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
# ]9 w5 R$ j) W) Z安装Nginx和ModSecurity依赖包, r# T# G$ E _6 m* V" t
Centos/RHEL
; a! F0 k, b. n8 T) D" L) [. e1 ~; N7 i
5 E, M5 l! F6 [6 Q; h2 P$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
9 R1 w4 G7 ]+ Z! f6 `Ubuntu/Debian5 n! s. o6 j+ S: n e3 V( K+ f
' ^# c- [8 F, m, _, f( Z" ~' \+ C% `+ U5 B' i/ G0 ]
$ 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- Z+ c, h4 I6 i X& z
编译安装ModSecurity' q1 q: `. P1 c' s& T
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。' S$ O6 A* R/ [0 U
& j8 z( @; F4 F3 l2 w
6 ]7 S q) l* G# J6 l方法一:编译为Nginx静态模块
$ `" K8 c+ O2 A" S# T9 v: V' Y; L+ G f- S* W1 e0 T; x O* i
# Q7 p1 W% B* J4 S' A; i7 H编译为独立模块(modsecurity-2.9.1): X$ s' d& x f
$ tar xzvf modsecurity-2.9.1.tar.gz
1 q% h1 I, o/ b( G6 h2 n$ cd modsecurity-2.9.1/
* E z, [. X3 f. ?/ \$ ./autogen.sh0 y/ |4 v1 B t$ K( d3 l$ X
$ ./configure --enable-standalone-module --disable-mlogc& F' S0 U; F3 ~2 }
$ make8 j' \% x7 O# [' Y I K" s& e# A
编译安装Nginx并添加ModSecurity模块% ]. A$ Y* k: W- R* c5 Z# |
$ tar xzvf nginx-1.9.2.tar.gz
9 C: C4 N7 i& ` ~* x- u$ cd nginx-1.9.2
7 z& o( ^: T2 E3 y$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
B& W8 K* L# A$ make && make install
( y+ {+ k6 } R3 |方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
) G& O. x' M( ~! G3 W# ?$ |+ C4 f K, p h. F6 z8 Q" Z
+ L7 J4 U) h2 b# l2 ~编译LibModSecurity(modsecurity-3.0)
+ z8 s. Q' p/ a! m$ cd /root- {* k1 H# ~3 d3 w, B, T* b
$ git clone https://github.com/SpiderLabs/ModSecurity
7 n$ t/ ]$ |1 V3 W5 {0 P& A3 l$ cd ModSecurity3 c, m1 f' w, e1 a
$ git checkout -b v3/master origin/v3/master
5 y* j3 d5 l* t+ e$ sh build.sh. I& A! b, r- t; d/ K' {
$ git submodule init
+ L) G) U, Y* [7 H7 w' H5 L$ git submodule update
2 q" h3 B; [3 K. ? ~; p( H/ F$ ./configure
5 R/ p) f0 ~: s/ C/ T; F g$ make
7 L/ Z) z6 S8 Y4 F$ make install
6 m3 d! s* I+ O& o" \1 I2 n! ALibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。, M/ K2 l2 G4 _" r0 f. \
4 K# \/ y* s( n0 T' U0 l( m
9 h! W. q( C1 C+ J+ I+ [1 j: P$ ls /usr/local/modsecurity/lib
# o: _4 l( e: n% ?: mlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
4 R( r% c# [6 a1 I& _+ {# p( M7 Q编译安装Nginx并添加ModSecurity-Nginx Connector模块; a# [: k7 O& @5 f. i
使用ModSecurity-Nginx模块来连接LibModSecurity+ P4 b$ W* O/ v. E# w
* l9 M+ a* K A0 p
7 h1 U* S3 ~; |$ x+ N$ cd /root& j9 i# H0 B" w, L" z) O" ~ z
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx/ F$ H5 t8 V/ Z6 W: s' _" t
$ tar xzvf nginx-1.9.2.tar.gz: A6 A& F7 K/ m3 ~. G* L
$ cd nginx-1.9.20 H6 b8 N6 F9 H5 J
$ ./configure --add-module=/root/modsecurity-nginx
P! `3 X% _$ H5 M4 M' w$ make
8 v* R N( e. T6 }$ make && make install
/ v- Z" d R1 h! @添加OWASP规则
' B4 g, l i1 WModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
6 a6 D. q, l O; d4 X: z0 g' i2 i( q7 r
" n, f5 @$ E4 o7 w( B- Z1 p
下载OWASP规则并生成配置文件" @9 C+ i2 l( m* t, C
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git: B0 C: y) K0 Y1 M' r: q
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
/ w1 S7 \: I& o M; g$ cd /usr/local/nginx/conf/owasp-modsecurity-crs- I! Z+ {& D% \1 g5 I5 z" t
$ cp crs-setup.conf.example crs-setup.conf! k2 d3 t( J* f
配置OWASP规则$ i& F* r* w G0 g$ t
编辑crs-setup.conf文件( y0 R- G0 |2 Y' \( R
, M4 X" n9 z9 z0 A2 y& I
0 F7 K7 M0 Z, B" A, E( r0 A; M$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf4 h5 H, m/ K7 F( l S8 s
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
2 {) q9 s" Z2 o( e$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
/ F* g2 j& Y% H( ?" Z" I$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
* B* M8 A! _5 L. P% _默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。3 O5 x) A6 A/ w4 O1 b
& P9 ~+ D0 V( k Q
% ?2 k. o' w* h启用ModSecurity模块和CRS规则: G& d5 V4 K4 M7 j4 ~5 |: l
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( @8 s/ n2 z9 o. U2 s/ w0 x. p
5 i8 S/ s3 l+ |. `
& j: H1 o+ c8 `1 Qmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
1 `5 t: |- F% u: j; k9 H
% ?% S% A) {: P7 G/ K/ T# b) d: P0 T3 h; i3 M
$ cd /root/modsecurity-2.9.1/
) m' a1 T1 g# ]$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf ( y+ y4 i. C2 l8 t4 Q/ V
$ cp unicode.mapping /usr/local/nginx/conf/
; I& G* B" p0 i) {2 Q; {将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
; G1 M; N8 @& u9 k+ f a/ K0 p2 S& Z# G* K6 }+ Y" j; G
U0 o: A3 x( d# a) G
$ vim /usr/local/nginx/conf/modsecurity.conf
1 F7 g) J. ^ x$ D3 `+ hSecRuleEngine On& ^1 h+ T6 w B: p
ModSecurity中几个常用配置说明:
0 ?! y; G/ y/ O. r3 }: E6 o
" F/ c+ {) _5 D8 P4 X# g1 p9 E4 O, v/ w/ u- v
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
3 _+ s: Z1 U9 ~, {+ { U; o; Q& S6 Y1 E6 k% h l7 J
, l- g2 {& Q, e9 d( P
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
) o; z( {8 |5 |" g. G* \$ ]& x4 @4 L& p1 ]6 L8 C; r
6 G/ R g. u$ W" A! B
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。" S& |2 X3 i1 W
. ^! k7 I% [3 u5 n: c) S, Z7 A& |4 \% b8 A: c, z6 o
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。4 L; n. C# e' d# n
" o& w7 O% X: c) @+ c
1 S& u1 f) f* o" [在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
' P" L- {4 ~ p( e5 g- b8 Q5 V. m0 y3 |# m* \1 p. m& ]3 B1 D
1 z0 ~8 k; T1 }5 ^3.x版本CRS
% q) [& t4 f8 b( b* m$ cd /usr/local/nginx/conf/owasp-modsecurity-crs8 h2 ]# b; L1 d) `
# 生成例外排除请求的配置文件
& \- Y* X* y9 ]2 G7 H& K7 S; S$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
& \. K0 b+ y( V$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
' Z% g9 \- D& s2 i$ cp rules/*.data /usr/local/nginx/conf& K" [$ i: c. R7 }; C' H# Z" R$ P! [
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
, R% k3 f7 {) _1 @8 N
s. l0 @. Y; u0 S+ x4 a
2 r$ A) \7 k2 I$ _' F* z$ vim /usr/local/nginx/conf/modsec_includes.conf. E0 Q: C& f3 S( [2 F2 s
/ g/ t! ]0 f) a& ]( C x
[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
' n% o" |% D) e" A
9 g f! Q3 o- h注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
% E% h) N; _+ _7 Q" h5 x8 p! F( E+ @" f$ \
' W" L/ z+ C4 X3 N7 K' y7 h. s9 |配置Nginx支持Modsecurity
6 X8 F7 u) ?; j& D, [' j4 [/ |启用Modsecurity
# T; K( L$ f6 o使用静态模块加载的配置方法
9 Y0 I$ h* _1 i. z在需要启用Modsecurity的主机的location下面加入下面两行即可:4 v# g a# i/ m$ N p
! K+ w% u! _; w
2 f5 Y$ Z% E7 ]% UModSecurityEnabled on; I$ a9 R! X! w# K6 M. w
ModSecurityConfig modsec_includes.conf;
3 D/ g# E6 M& X: h# ]( k: q修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
+ y4 e) R4 Q6 v }
1 Z9 }- x8 U9 E
+ M9 X. e0 p+ T. T9 E: p- ~$ vim /usr/local/nginx/conf/nginx.conf
; g& ?; q" \6 B* [* A0 C! p: [4 e0 _- V( x
- D0 v2 y( O! t- userver {2 T. \( m8 i7 A' ^' I2 |
listen 80;
" V! M6 E9 r1 z, \5 n, C( r server_name example.com;$ a! [8 ^# Y5 g$ F- ^- O9 N0 l
+ m9 g T. v( e$ c/ k0 J
6 O E% N: T3 N9 W& _( S) q
location / {3 y5 d7 E+ Q3 l2 W
ModSecurityEnabled on;2 O3 Z( X/ f0 \& B2 ^
ModSecurityConfig modsec_includes.conf;/ E" q6 I- w# |" ^
root html;( w& K* ^7 x6 w5 k9 l+ ]* |
index index.html index.htm;6 ?4 J6 c& A3 m8 F+ g
}. R: x3 {: C* n9 r5 O# M4 N* J
}1 j' r" Z% r: ]8 e; A# J/ x
使用动态模块加载的配置方法4 g0 {* b8 a' l$ p. Q) ?
在需要启用Modsecurity的主机的location下面加入下面两行即可:9 b( W' A- H! ?3 [ m Q |
~" E/ v& J6 [7 C' t
) Z, p8 }0 [3 R8 Vmodsecurity on;' T3 {7 U" t% D) j3 [7 {% E9 _1 o
modsecurity_rules_file modsec_includes.conf;
7 Q* X! ~7 m$ K修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。$ L6 @- T& V9 @
Y) E- Z5 G) w/ x' V: W- n
; @) C% |/ D% U/ S* n5 j7 W$ vim /usr/local/nginx/conf/nginx.conf
3 U% L! P3 h9 y4 p; B, ?2 f/ O D6 d4 e0 g o
; f+ B" b, I: e0 @: V" Iserver {
/ x9 E9 T" _/ T& M; } listen 80;0 D8 X \' v! X) T9 \, }) L
server_name localhost mike.hi-linux.com;* P! |2 O3 b* P x4 p
access_log /var/log/nginx/yourdomain.log;
+ m$ x9 E- e/ {
3 _/ Z6 m: i7 j: L4 c9 \; k/ ]" v( Q5 V$ ^" D2 ~( K0 q/ m
location / {7 [* a) Q7 Y: i9 m2 r
2 J( x @; M6 o# D5 m n7 f6 c/ \; \- a Z9 g, g" c& Z
modsecurity on;
# D# a7 }7 f- l' M) h: r7 n; H! H3 j modsecurity_rules_file modsec_includes.conf;
7 r4 | W8 w$ T/ k$ A0 Y- H; p root html;* \, d8 w% f6 C: |9 c7 x
index index.html index.htm;
9 o* ^" @# g) Q$ T8 s: _4 Q) l}6 j! n# j$ L3 X
}
' c! s1 |* d5 ^# u验证Nginx配置文件. m* K. ~1 l- |, c; \
$ /usr/local/nginx/sbin/nginx -t
9 g+ H2 f; D* R- Ynginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# _$ {2 Y) L. n5 _% G3 ?* jnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
+ z8 I+ ]1 a: r" Z* t启动Nginx5 W$ `8 n: d* w! ^# N
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
1 j3 r! f, ~3 H
( b, w( ^6 {) B* _- }$ w测试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能够战胜更多复杂/未知/混淆的攻击模式。 ; h3 q5 H5 `- r3 @* B
|