ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。0 T$ R% c2 S0 p. g0 k/ e7 R. x$ L
" j3 V# `# ~# v8 F) Q- l! r" X
$ [$ K c& D! a9 m* u在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。* t) T8 I3 g1 V$ L. B
1 f% l' }+ C$ x3 M6 G
$ [ P2 f/ O2 `4 s( r3 s n什么是ModSecurity9 R7 M; d6 q4 d8 i1 [% E, h0 v
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
& H; X1 A j r( }- X% P, Q5 o: n8 D6 G
" ]% t- [6 v8 K- ]ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。8 ^( V( @+ O- p* S9 k, r
& C3 ~$ ~2 k& K0 y! c
6 t# x, a: k6 c0 y+ W$ J# u; ?ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。" z2 U$ b& x, s$ z0 p- x5 L7 U
3 q6 ^1 P* N p% ~! z3 e+ S6 b9 [' f" u
官网: https://www.modsecurity.org/" N/ g0 Z* Y, G" s: S8 X
% Y) A( ^! d/ k3 h y6 D$ d
3 F0 [. e \& c
什么是OWASP CRS
# A$ R% I& }6 ~. J: c5 U- COWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。1 D+ ?6 C2 j) }: e" O$ w# }
% h( Q, b# P3 v( [' _
' v- [! ?$ \2 n6 }2 I B1 H& ZModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
/ m- N5 N) P3 l2 a. c( ~9 V, \3 g# H0 T& K2 q1 D. e* k/ m
: |7 [: q9 x8 {, ^HTTP Protection(HTTP防御)! B. T4 Y0 {" L# f
HTTP协议和本地定义使用的detectsviolations策略。. s" \- Z% J( ^ ~' J" h
0 I2 D. i8 z: h3 K4 P
5 K6 n: O/ q& D- \1 y/ u* D
Real-time Blacklist Lookups(实时黑名单查询)
& o* L' b' S# H" g利用第三方IP名单。8 d/ L+ Y% R: p6 o! {6 {
, ~4 t6 j/ ~: K& U% d' v' J9 Z' l
2 a( }- x$ f2 S5 e. R7 w7 V4 tHTTP Denial of Service Protections(HTTP的拒绝服务保护)
3 k9 Z. E# o5 E8 `防御HTTP的洪水攻击和HTTP Dos攻击。9 V: a w6 X- ]) y6 B
) H S) O/ H7 J n0 Q
- b# S% l' k: x! J! JCommon Web Attacks Protection(常见的Web攻击防护)- S6 h1 Y9 Z% j/ r9 N, u9 f8 e
检测常见的Web应用程序的安全攻击。
1 w v- l* |- W, g8 w5 x, T5 u' f3 \' N
+ u) C2 c7 n/ l G* l
Automation Detection(自动化检测)& I [* }* ^' N3 t6 |! g5 `
检测机器人,爬虫,扫描仪和其他表面恶意活动。/ q8 ?& w4 L' B% M- h C
( q% q6 r0 N. C) ~! U- ]8 I
% A# \/ k# S2 z/ {" j. UIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
) f# p: j0 j( ]+ P$ P) m. W: E检测通过Web应用程序上传的恶意文件。
5 K& ~' q7 C8 m6 \% o
, y8 D/ s$ q. f% o% u! [0 c' D; E$ H6 v" _& T
Tracking Sensitive Data(跟踪敏感数据)1 n0 d" `5 p7 B: Q
信用卡通道的使用,并阻止泄漏。
9 j5 L: B: M) `# B
6 R4 |! `8 ~/ S! `( E9 J) i' y6 s( `/ _8 v4 V
Trojan Protection(木马防护) b4 ^- ^" ]' ]- g/ L
检测访问木马。( N6 {9 U7 O- {! y s+ _' u4 R7 G
- F( G- H# I( S3 J* T" j% |
$ ~* y( t' b; v& a$ v7 T% h) w/ u
Identification of Application Defects(应用程序缺陷的鉴定)
& X3 o. {. @# O" N检测应用程序的错误配置警报。
9 L d7 ~$ b+ b' T- C% D( y( X+ K# T* O6 o
1 i7 [% S4 ]1 D" B+ ?Error Detection and Hiding(错误检测和隐藏)9 y* b3 k$ L' e1 a. Q+ H+ I, M
检测伪装服务器发送错误消息。% O9 h& I7 |$ c6 v4 p. w
4 l5 m7 T9 g/ T# F: L, M
' |! _) K& g& u$ u2 k安装ModSecurity7 z; n) S0 r3 }! T% _
软件基础环境准备
( m( w' u h* u& d+ Y# x下载对应软件包- j/ c2 x: ~3 B- F$ ]* r! e
$ cd /root
6 L( Y2 A( ^! `/ v0 {, L; u& P+ b$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'9 r3 w1 X% y8 O$ T; A0 G' E
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz3 ]& K* g% T) z$ D) _: y+ y2 D
安装Nginx和ModSecurity依赖包& d6 ? X1 w7 O
Centos/RHEL' p5 }& y) V, y# s( p# z
; U9 O% k1 k- _/ p, `
% \$ h7 i9 R3 m( Y% v
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
- k& k, Q: i+ o6 aUbuntu/Debian
* k! ~3 F) W8 ^, `8 {/ p
! c* Y9 X& C+ I
7 H+ t3 g, h+ r5 M( W& I% P6 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
) Z7 d/ _/ \" ?编译安装ModSecurity! r2 P' ^# g0 h
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
, | Q, C8 G; l! w; h
5 \: g8 b) F. C& _! M0 Y; ~1 f- h( ?: V. d: \# {3 g) ~, W C! I
方法一:编译为Nginx静态模块
, I# Q; _' x4 C. g1 o0 a" J$ \1 m1 W' R3 K" O
! l' [) i% c7 n% l8 ~1 Q
编译为独立模块(modsecurity-2.9.1)
( p# n- b4 d% J9 N: A, I Z$ tar xzvf modsecurity-2.9.1.tar.gz
. v2 q8 `3 Q% H9 v8 `0 W' u' y; i$ cd modsecurity-2.9.1/
( O0 n# h) k: ?8 i- [3 S3 H$ ./autogen.sh
$ x7 j6 ^7 V( h# L& s$ ./configure --enable-standalone-module --disable-mlogc1 F" K& q4 m; N! q- j( u
$ make* P6 |' z$ l& O; P+ [5 a+ r
编译安装Nginx并添加ModSecurity模块1 p# }' y' P" o* [
$ tar xzvf nginx-1.9.2.tar.gz: }; [5 k! m/ C" O6 D( w
$ cd nginx-1.9.2. X" X+ E% G4 ~6 R
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/, i: P; U3 x0 \8 m3 L2 l5 U Q
$ make && make install5 y, b7 ?) C8 J4 @! |6 j
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块8 [, N# p2 O! ?8 q" X# r- p
; y7 F& r0 B4 P% R ], W
7 s+ S0 ^' |; u) |
编译LibModSecurity(modsecurity-3.0). ]5 x7 j) c8 t N! _8 M
$ cd /root v9 O l. u. B0 x
$ git clone https://github.com/SpiderLabs/ModSecurity
$ g2 K# H# Y' Y, K K% N$ cd ModSecurity4 o# V. t% l, k' }6 x: a0 _3 d
$ git checkout -b v3/master origin/v3/master
) d) h3 ?% `& D# B* e$ sh build.sh/ b! i- w$ C1 P9 C" k3 a
$ git submodule init6 m1 k' |+ J3 C0 i1 y. J
$ git submodule update
8 G7 n! Q% ^/ o& b% n1 M$ ./configure z% W; ]# I' }% o
$ make
# t. t/ Y5 F5 J$ ]/ s6 ]2 B7 D+ D$ make install1 {2 ^0 l% G$ Z5 Z8 s! Z' m2 m3 u
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。3 f4 n( ]1 b7 [8 Y
( A% T6 w. y0 d( t- r( ~ j( ^/ j
$ ls /usr/local/modsecurity/lib
, i) L5 c9 b; S7 B# slibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.07 N% N" C6 g, d8 _: G+ X1 F0 T6 Z5 E
编译安装Nginx并添加ModSecurity-Nginx Connector模块$ k5 t" | [/ }3 z
使用ModSecurity-Nginx模块来连接LibModSecurity
$ S$ Z% ]( D. b, o0 J3 j( m+ T. v3 X- \& H9 i
" f1 q2 ?% [9 a$ cd /root, B% N9 j- ^+ j n
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
" g; n" D( Y1 U" I2 a% U3 J4 w$ tar xzvf nginx-1.9.2.tar.gz7 q, @/ E: t3 x& y# v5 Z y3 P# n
$ cd nginx-1.9.2
8 ^5 u3 s2 C7 w+ p- `$ y; @/ D# a$ ./configure --add-module=/root/modsecurity-nginx, T4 D+ _0 `5 e( ?' {. \) E3 c
$ make& K1 X t j5 A$ ^4 @4 a/ L& @
$ make && make install0 e4 W/ K8 P9 X: g0 b- G; }
添加OWASP规则1 m2 F3 H a, ?6 k3 s0 h3 s/ ]% ^
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。2 u" x5 G: }/ Q$ a V; Q6 o2 T
4 @5 D F. ]0 ^6 P
1 r8 @/ @$ P; j. J% R
下载OWASP规则并生成配置文件
' g& F0 x1 C# Z$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
* {9 @ ^8 \& Y6 W3 q$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/3 v. U, i; v1 t" l
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
/ Y, @- d) r& E: H9 s+ W$ cp crs-setup.conf.example crs-setup.conf: T6 i9 l0 J1 t- K3 V* P- s/ n' G+ s
配置OWASP规则( J) M7 y6 z% G2 k
编辑crs-setup.conf文件
* U7 m2 e2 ]1 ^4 h0 @
6 {% t( `" U: Q! j. X5 X: ^3 W, o% n4 K/ F5 M# e2 Q
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
0 A% E0 {1 z! M+ Z6 h8 L$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
* P1 r: x9 f7 s$ ]$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
, p) i0 Y5 z4 n) m( r q$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
; O, \( Q7 A( A默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
$ R9 _9 v- ]( S0 n: C
7 u/ R# D) ]2 m) A
3 R5 F7 d0 X' L7 t% X0 ^启用ModSecurity模块和CRS规则
) C5 i" R, R, F复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ s+ h+ [( T/ c/ h5 u& E4 o: X/ Y+ L) A
' E1 W0 N& l8 ~/ o6 F+ m) R( K
2 A' o/ V5 Y+ \6 _' N U1 J$ Y& L
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
& S* T( ]/ y, e0 _9 [( H$ d% I+ ?: X7 ^2 c; @' |
: y+ r4 i- X/ R0 J; [$ cd /root/modsecurity-2.9.1/
3 e+ w5 y4 ]* T. Y$ d3 [: B- H$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
0 ^7 j+ ?% x8 c8 }- X$ cp unicode.mapping /usr/local/nginx/conf/) A0 e) \3 G5 R: Q, E0 N/ A
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
( m" f. _6 t M- Z0 k+ b4 z( M y0 n/ h% E$ r$ t: T( U" _
' ^+ v9 x ?# e1 E9 L, }8 C2 |
$ vim /usr/local/nginx/conf/modsecurity.conf
+ r* \$ Z$ l" w/ F g! Y% }; `3 v oSecRuleEngine On
9 V* ~+ b4 F2 G" q) ?1 T" F) QModSecurity中几个常用配置说明:
4 E/ }1 ?+ O( r0 F/ k9 x& q i) T9 T# B+ m
# P% Y% Y ^! X: M
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。% t: L( `7 h/ K- @
- K- \( z# r9 I$ I
: x* p- s& O% T+ w# u2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。2 X# J- Q- a, {4 ~6 |& H! k& \5 T
( y9 W+ V) B( v8 e
# q4 F2 ^3 ~. P) C0 N' e2 X
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。5 x3 j$ O8 z+ H: [# x% f; B
4 N8 v1 O; h& l/ j0 @/ s" M, H5 h U9 j$ \
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
* h' m; z6 x& u3 C0 e3 e+ ?$ f
: F# ~; ]1 O; a* U6 k4 i) z0 x* B- e# `! G S- B; |3 V; r
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。# I# r# T- u+ N Z" ]
1 T3 B# G6 }; [; ?1 B( C' {; h7 }# N5 B# i
3.x版本CRS# N* b6 J$ H1 V5 i8 l3 Q
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
% h2 O; \0 Z5 N: ~0 U8 f7 o# 生成例外排除请求的配置文件8 l1 r" J" E3 X/ C+ U0 ?
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf: c- R Y' g3 X7 F# `1 f
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf+ D0 S& l8 B/ P3 y6 }
$ cp rules/*.data /usr/local/nginx/conf
# h3 i+ o) k( }为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。+ p6 T% P, \ e% P- u9 r9 S
1 O5 L8 M9 Q- F; C
9 a( H" ]0 g# b D5 g! {$ vim /usr/local/nginx/conf/modsec_includes.conf
/ Z5 [$ r. ^9 _5 t& k+ X$ ?; P1 ~% Y; T2 J+ N+ @6 U* F0 \' U& S7 _
[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 $ r7 D5 S j- D$ e# G
% g( s8 k4 F; p X5 H0 V1 ]. o& r注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
- _( c$ q' e- a4 T& l: {
6 z0 a+ |0 f( T0 k, }+ y1 J9 M
5 i1 z$ R7 e- d1 ]配置Nginx支持Modsecurity
5 c4 X9 t0 ^2 b7 N; t启用Modsecurity$ s4 [! Y& u2 H" t8 a( W% ?$ R
使用静态模块加载的配置方法
. P, g( ~: G G% _) ]在需要启用Modsecurity的主机的location下面加入下面两行即可:
' v. W3 ^# j# }0 s" O5 s: e: L9 q2 d! X* I
' J6 a+ {4 c, @8 f* a1 H4 e$ z( m# ]) GModSecurityEnabled on;3 t3 `7 }/ }+ G& j3 o2 X8 `' r
ModSecurityConfig modsec_includes.conf;: o& B( E! i6 w- `% h
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。3 a# u0 o7 M8 S, A/ t: v. D
6 q5 K, y: ]" o' c6 T( U! V7 A) V H5 B" j7 D
$ vim /usr/local/nginx/conf/nginx.conf# l0 e4 K0 m. l5 i9 V
5 a* T2 ?+ @* S, q
% { a( T" E: m& D' jserver {% c# G7 b* f n5 ^- Y# m
listen 80;8 J% M8 Y+ C. d' V& J
server_name example.com;
# R5 \$ O. G3 k8 A2 Q7 }, Z# W% t/ Q: j8 G
" L% q" x+ ]5 T7 ^& r& g
location / {4 S% F& r/ m! ^
ModSecurityEnabled on;
* N9 @5 {8 h/ l/ f c" |( ~ ModSecurityConfig modsec_includes.conf;
* A7 Q7 q r0 A7 @2 t root html;
- F& g# j2 A) n) a6 K, ~# U1 D0 x index index.html index.htm;
# i9 M7 |7 Q% L) B% F }
3 l5 X8 s9 M) S5 v}
# ^9 Q, V2 c0 T0 J/ P: C使用动态模块加载的配置方法$ e* H$ h' O k
在需要启用Modsecurity的主机的location下面加入下面两行即可:
1 C4 }- \* @3 ^" g
0 {" x+ u6 k5 F: V6 y& B, Y4 R/ ?0 y' G! V* D# Y
modsecurity on;
; O. k" i$ v8 ?modsecurity_rules_file modsec_includes.conf;
$ Q( Y Q9 T; X7 a- c/ z修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。3 i: U6 n' X- i0 a% @) z @ t
/ g5 S" E5 s) o; Z4 D
0 K1 }) F4 _' F# E$ vim /usr/local/nginx/conf/nginx.conf
* W7 L' R. B, \# H# _0 p: a$ m, V v6 |* ?2 f( Y8 z( o& o
; q K/ B0 E/ T9 C* x' kserver {# D% m: t" z5 u; ?3 C$ A( r* K8 O
listen 80;4 y3 \, ]- f+ ^8 I
server_name localhost mike.hi-linux.com;
1 @, j& i) i5 ^ access_log /var/log/nginx/yourdomain.log;
" p: G; g# Q7 Y/ l2 |
& f' ]2 R/ ~ Z! ^
$ b1 b4 \# Z9 b& G location / {
/ f: T1 R; E/ I: u; g! @$ ^+ X9 P! Y4 Z: z( z
: L' Z5 g1 d8 a9 {
modsecurity on;
& k+ p: H2 `5 ^( `# M5 W, U7 H modsecurity_rules_file modsec_includes.conf;
& j$ r8 [, k8 I+ J2 j/ |+ A root html;
( g" W, q* }3 a' U: v9 }9 R index index.html index.htm;
# x; v3 u6 E5 E}
' R7 U+ E, K9 x7 X}! H e( A! }+ `- L
验证Nginx配置文件
' u# {* D4 Z0 Q7 `$ /usr/local/nginx/sbin/nginx -t
5 D: D" a# @$ u; s# |nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok4 u# F' _" k8 Y8 _3 g+ }6 X
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful3 s5 Y7 ~. p0 s+ R
启动Nginx
$ K/ }$ c2 ~9 q# `/ I7 c% Q$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf5 Z" N# e3 w5 q8 ]0 K0 S
: r8 R( Q; n6 e' K+ v0 v0 T: d/ ?测试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能够战胜更多复杂/未知/混淆的攻击模式。
" W- S% O1 V7 ]; Y+ S$ I: g |