ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
0 B9 j, A( e. D. r/ W4 X2 X! v& h* V& }9 U. c
: k6 W6 |. F5 S b3 G; j" @$ L
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
9 r# n' N$ K* ?; e- d# h( m
) s9 T# {- X" m1 i7 l( `# f9 k h* k$ y. p
什么是ModSecurity
7 J9 A/ z/ d8 h3 @ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。) s* D: Y1 _6 L6 T4 l6 _/ V5 D9 V4 N% |
6 ?+ x' t0 x `$ G/ p
7 y. @% E+ q5 M% R
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
5 z, C+ [/ G0 h8 J3 c% X. a$ {( p# R
- b8 ` N! I$ z5 |) x; O* Y. E9 G
. g* Q- Y ?- g+ b: }! aModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。. \5 F4 V, A, ~+ S! w {7 e9 e
+ e1 N3 x) a# f& O% J7 }* t9 ^
6 w$ S1 u7 O: t9 A+ T. O* r3 G% Z官网: https://www.modsecurity.org/4 t0 |/ i( U8 t; X) x7 m
/ k2 M) J/ y3 u8 N# V; M% W9 n3 E+ G* Z' ^4 [
什么是OWASP CRS
2 C. a/ k. I5 B) U- S: {OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。& l$ a: M K; `& }% e; v
) K: q. d" p; Z7 v; X
H4 }$ U$ y; _3 X6 V; K0 Y2 G, jModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
6 g q( M# Y: u$ t4 q! L& ^
$ p' a- ]$ w( ?) n& `5 w# Y# o
! _$ F4 R* r# f7 Q: P6 V) YHTTP Protection(HTTP防御)
. k" E9 k$ T7 P8 P @7 Z- `+ tHTTP协议和本地定义使用的detectsviolations策略。' m) k7 d- ?8 \# G6 K8 s8 S7 Q
% `% N9 W- f2 Z0 E, c/ {
- G# H: k& ?1 v- W7 b& ]- z
Real-time Blacklist Lookups(实时黑名单查询)
# G3 D3 `) _( P: n5 H8 a利用第三方IP名单。8 U( T& N$ T6 V8 i ]8 b6 }
7 T- n& i1 z- l* S; M; i6 ` }" A' j% G! `
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
6 g6 L" q% A9 b. X防御HTTP的洪水攻击和HTTP Dos攻击。/ q' B7 R$ G1 Y: y8 j6 A
( |6 Q9 J% A3 F+ D
1 K$ V/ Y6 h, d6 V. S% HCommon Web Attacks Protection(常见的Web攻击防护)/ ]9 [- ?, B" k3 ?
检测常见的Web应用程序的安全攻击。 C0 U/ |: m; v$ ~3 a
P' d( ?3 z) L) {4 W
! L: d& i& J F7 p4 P# u3 a5 |Automation Detection(自动化检测)' F6 h7 P" f% G% Q
检测机器人,爬虫,扫描仪和其他表面恶意活动。1 T; {. v$ G% t" e/ l
x. F2 b3 u: g( ~
9 z2 B+ u4 ^9 B% xIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)* F7 t6 d$ J8 i& N8 m2 R
检测通过Web应用程序上传的恶意文件。1 h- s, E4 v5 _1 K$ \
" I; B* }% F6 G0 G& T, ]' L) e% ^! R* E2 w$ y
Tracking Sensitive Data(跟踪敏感数据)) g' l+ l/ h* v% l4 t) n2 e
信用卡通道的使用,并阻止泄漏。8 N) r' B0 ~! h) k/ ~
! u' Z0 V1 f$ q* K, G1 I7 E) M$ `4 a6 W, f! m
Trojan Protection(木马防护)
3 F( i3 J% T( n检测访问木马。
w% k+ J1 p, r9 w3 t# t5 t1 R+ J6 y; W/ T, L
5 {4 w% m4 k- @% `2 LIdentification of Application Defects(应用程序缺陷的鉴定)2 y* ?9 H" k/ M& d& |
检测应用程序的错误配置警报。- i7 ]7 }) ?3 M4 b9 N3 d6 @5 l. b
' w9 r" U8 e) w2 r: |, q# _0 J6 G6 F8 H; E# y; [
Error Detection and Hiding(错误检测和隐藏)
1 s$ Q( n0 B& N检测伪装服务器发送错误消息。+ ~5 b# ^+ h p1 l5 w& }
2 y: @; G7 \$ r5 ]" G1 ]- B
+ b9 X8 `$ S% L" e0 V ]% J$ _# ?
安装ModSecurity3 M5 Y5 R$ C$ F5 `8 F
软件基础环境准备* p6 B4 v2 O0 n4 ?- S
下载对应软件包
1 w; S5 a" ], F. K9 v% m% u. ]6 E- N$ cd /root
* e9 \3 d+ ^/ k+ a$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'9 }5 ?+ r# z( x# ?/ 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
* u1 ^) K1 p5 W2 T: H5 ^3 G B4 [1 p安装Nginx和ModSecurity依赖包
& S( o0 D6 N* J0 B& B! v yCentos/RHEL
8 i2 J* U7 q( ~) {
3 O. i) H2 Q: B# o# I: E2 j% t" W" Y4 D/ @+ j! I
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel/ w9 x3 n3 w3 F+ a& _6 E
Ubuntu/Debian
8 U2 i- n& R' w- Z% U4 G. I5 M' Z# J5 x) {! u7 I
4 @! S" S- J7 C1 P+ d$ g: g- Y/ q
$ 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++-dev8 W! Y) P, r/ N
编译安装ModSecurity( D% X0 L. E/ {; w; s
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。. F1 F6 B; \0 o* D: D& j& X
- ~% }# n$ _1 L6 M) M! T) t
4 H/ Z- b7 G$ V) u
方法一:编译为Nginx静态模块
* Y/ Q# I* Q9 S2 e$ m$ `& U5 G
" a2 ]) ?+ ^. Y% S$ k! `( R; F, A2 Y7 X% N7 t9 K
编译为独立模块(modsecurity-2.9.1)/ U9 l1 } ]6 E* W# n
$ tar xzvf modsecurity-2.9.1.tar.gz% G- b- s! f4 M: c
$ cd modsecurity-2.9.1// _3 C7 u, |0 K+ z
$ ./autogen.sh- j/ }8 G/ U2 i( i
$ ./configure --enable-standalone-module --disable-mlogc
* X$ U& ~9 e: c$ make( |& `/ T3 r3 X
编译安装Nginx并添加ModSecurity模块6 I6 h, {7 a1 e8 T8 |) d
$ tar xzvf nginx-1.9.2.tar.gz, R7 D2 D! e6 q/ a s1 ?
$ cd nginx-1.9.2
C* \2 d; d$ _" v* b0 o: B' ^$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/# ]& t) h- h7 L: T" u- g: y, I
$ make && make install
, }/ ^+ U* }9 r; g K: K方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
: o& f& H, g' t! F2 }. P! ?% u' q, x9 a" ^
" {& |* ^- m6 t6 A( c; P5 D4 @编译LibModSecurity(modsecurity-3.0)# e$ t \6 D8 p( D/ l, [
$ cd /root
P1 G0 E0 A% S0 `" _7 H$ git clone https://github.com/SpiderLabs/ModSecurity% l" d% w1 J% K1 \5 d
$ cd ModSecurity
/ ?4 n. K$ q: H5 d! M9 g: L3 k$ git checkout -b v3/master origin/v3/master
& W1 v. F# z. ^- Y$ sh build.sh
) o* |+ K9 r2 _4 X; Z+ c$ git submodule init
& h- n9 j& s- f9 q5 T/ N: z7 O$ git submodule update$ g# w. X' @" w7 K1 C& ?1 t
$ ./configure9 ~$ V5 x, s9 P9 F3 O
$ make
7 Q* D4 _$ c, g2 z; z$ make install" g' I* C0 B- P! R# C! N
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
8 D: w7 R' g- q+ r
* S6 `0 i" S( u) U+ `& ]" A$ A# N7 T9 [4 }: r
$ ls /usr/local/modsecurity/lib
; G( U" m+ k5 z6 r1 }libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
1 t% d% q+ b& s" { c& B编译安装Nginx并添加ModSecurity-Nginx Connector模块1 C" e/ j; G) P# A. N; z
使用ModSecurity-Nginx模块来连接LibModSecurity
" B! b7 g# O0 N! R
' f1 _0 k7 x" G5 W( B8 a: U" `$ c' T- i& `* a& S0 R2 r) T
$ cd /root
/ j; V6 V' K1 L; b; q w2 f) O$ F$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
$ p; X5 Q2 ^5 \$ Y& ?% {9 ]* d$ tar xzvf nginx-1.9.2.tar.gz) f: F8 d1 M1 u9 n2 i
$ cd nginx-1.9.2
6 a; m. \+ f# ?& p% ?$ ./configure --add-module=/root/modsecurity-nginx' g7 s% m' g' M1 c% u
$ make" {3 r$ g# I/ n7 a
$ make && make install( j3 d4 v+ C" m+ S: m3 N' p2 w
添加OWASP规则
' e) I. P- y6 o& KModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
7 X+ z+ L7 U* ?; a" |- k+ B8 g0 P% f
* _2 N. v" B4 O$ [, k( U, a下载OWASP规则并生成配置文件$ t- ?0 F2 o% Y5 z! k
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git2 s+ h* w% N R3 r6 v0 r7 F! V
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
: J' K" f7 b: I, d, P$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
; T: t" _9 u8 v7 ]. g$ cp crs-setup.conf.example crs-setup.conf$ F5 {8 s# A. X6 s& ^0 M: ?
配置OWASP规则
& N$ l" J* p \3 V编辑crs-setup.conf文件
5 w# G8 u) u( K% I5 n; ~; l
% i/ ^5 u$ [% }6 p# Y- O5 }
3 m9 {( o; k: O, K0 j/ U3 q$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf4 K, x3 w* E6 I# c
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf* f7 ~" e9 r- O$ y
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
9 F r7 F% Z5 V3 Z$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf9 r6 l( ?3 \- d
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。. _7 a+ }. Z: f
0 \/ q6 v, ~% O/ P: ^
6 Z! y& Z- x+ m$ X1 C$ S/ m& v
启用ModSecurity模块和CRS规则2 M( o$ U5 V4 n( j2 q7 ?. D) r' K. S
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ y6 b# y9 u# X; k4 }; i5 g
; i+ c5 M h7 N6 f& w9 t" `
$ x. Q. m6 ^& w9 |4 T# a) ^- l
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
* O$ c" j/ H* B) A
* ?5 b, k: K) F! ]
" H" O( _* r, w$ A% K7 k' ]; x$ cd /root/modsecurity-2.9.1/ {; [$ s; O; G
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf : E0 _& y6 P% S
$ cp unicode.mapping /usr/local/nginx/conf/
: Q5 r" D! p6 [! Y; [8 N; u将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。0 [! K/ m/ F/ e. R) U5 R
9 p7 q$ x+ T- A3 u& ^- C+ O
0 }$ L J# V1 R) H3 t: \$ vim /usr/local/nginx/conf/modsecurity.conf: t, @: R2 o8 U
SecRuleEngine On! N) f1 L& l& [0 A9 D& p: D% ?5 ~
ModSecurity中几个常用配置说明:
. y$ T( {% m, r+ r
- H+ s( A4 q! p. w. k: i+ E9 c- G5 U( ~4 t
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
# y1 q2 O+ F/ i# |" T: }8 p; z6 r. v/ i) t% C$ S+ u( D
5 @5 y: r- N) p
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
% M% R. F( d7 ]% t$ l4 B! f$ I$ [7 T! \* {1 i# K
) h y! h t( O. J9 X3 O4 x3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
5 Q) d# Y# T: n. Z/ Q1 w! Q0 B: N3 E( S/ J/ R6 o5 L
2 S$ M0 ~2 q9 G7 l
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。5 U( q& t7 e) ~1 [0 j0 L1 \
* g y8 V. I: [$ Q* A+ }: j a1 C: @1 K
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。, E; P( K( o& z, S
+ Z( A3 ]1 m" N N& ]/ j
8 E" d! k9 j+ @. C3.x版本CRS0 e3 ?: g( x) H0 P6 F. _1 h
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
# ` [. }7 K0 I% j; M* m- G# 生成例外排除请求的配置文件0 k. ~4 H! f0 }8 N, S9 {- z y
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf; {/ Y" f6 P; E; D2 C7 S& v5 I2 F2 I
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf9 `' o$ {5 V( P0 x+ k5 ^8 V# Q
$ cp rules/*.data /usr/local/nginx/conf. G. Y6 k0 K# L, U H' b' c: g
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。7 g! z- Y% E0 n1 s2 g( D, Q8 q
6 V/ [0 u. r L/ ~8 `9 T; {
4 V) [; b% L; A( v0 T$ vim /usr/local/nginx/conf/modsec_includes.conf
! R. |+ s- x# e d# Q# r' L$ L( e- k; h8 J% K. M- t) A0 M, [
[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
; U: D% G# J) b1 ~* h' e/ u2 O6 L) N/ ?3 ^8 ]; r/ Q* n
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。/ j( s: z$ {8 r( n8 u
5 G9 E# F0 M9 E( d7 J7 |
0 @# g3 K9 `3 N& a, X( i% h配置Nginx支持Modsecurity
7 `' Z Z. V- W' h启用Modsecurity& A: T* t) z" n$ c; u
使用静态模块加载的配置方法
5 j. `! z/ z$ \0 D J, _3 R9 L在需要启用Modsecurity的主机的location下面加入下面两行即可:
! ]1 y% m5 f' I( ^) D
2 z% X" | d' F/ q7 P$ ?. N' }: s" T* W5 y
ModSecurityEnabled on;
9 ^( I5 f% A5 FModSecurityConfig modsec_includes.conf;1 E6 }& v- J8 w8 _2 F! [
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
, w$ P8 ?- J; ^8 M+ \+ q) B: z+ F. H- c
$ c4 v5 A1 [0 }$ vim /usr/local/nginx/conf/nginx.conf. D5 C1 W3 j. W+ K* F
5 j5 b" c# E* E
$ S& u1 m" B2 I5 q3 S; x
server {% F- }9 V7 W. @
listen 80;
0 d! \5 O8 J) U6 y: A) p server_name example.com;! m! O( ^% D) u; E' r) i4 O
% ]; g) e. [" o7 a9 |* \6 p' {' F
1 m/ C$ c, t; E0 C location / {2 a5 D3 K( m! h" g& @, w9 e
ModSecurityEnabled on;
0 |* E% v7 g, Z3 o* p+ ^$ y ModSecurityConfig modsec_includes.conf;
* T- o% y$ O! T root html;
! m6 \& H* W) ^2 b% u index index.html index.htm;
- x4 e: }* x4 S/ x }0 O5 A9 \- z; M" x# P+ j+ z( O
}
7 V3 e9 m {' J8 e) }" T使用动态模块加载的配置方法2 q5 I! J6 ~3 `1 \* I. q9 p
在需要启用Modsecurity的主机的location下面加入下面两行即可:
: `6 _4 k; X. Q6 P) X
' Q3 x- G$ | b$ W) d+ l5 Z s/ [
6 A% G& s5 I3 F* X% Pmodsecurity on;
" s4 `( y2 v! S( K) I' z2 \modsecurity_rules_file modsec_includes.conf;. ~& p5 g `3 m% I
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。2 I" J1 T4 M* q
! W! I5 B' d t, C8 t3 `) z' W3 E+ b
$ vim /usr/local/nginx/conf/nginx.conf
9 M; U, K% T4 S& M* h
, A/ a; [- p5 C* V. o
$ [& K1 s V6 ^. @) \) [3 Vserver {% ?! ^2 `0 b" Z. F
listen 80;4 i7 d, d0 o/ ~
server_name localhost mike.hi-linux.com;. h1 D6 r- S2 D( D( l
access_log /var/log/nginx/yourdomain.log;
, V' h' [* g3 P$ b3 r" S$ r, R S4 z' [ r2 a8 u% R3 Y
$ H0 ^: V, ?4 m+ ]# H# c e location / {& ?& P; s1 x$ y# U% i/ s( ^
; ?' h2 ~4 ^0 P0 t
: m2 q7 \( z- l o: D. }$ L# I modsecurity on;' A3 H4 V& W( ~! l) S- x0 {
modsecurity_rules_file modsec_includes.conf;; b3 P& J h0 `: K1 r
root html;; L: j( i+ V; y: W7 w
index index.html index.htm;
' I8 b; `6 F* _5 Y" E. N3 {2 b}" Y* p; r* S a+ d' o
}
& D( r& j$ _0 i H验证Nginx配置文件) I1 L' D; l7 U1 \( q
$ /usr/local/nginx/sbin/nginx -t
# }0 c2 Q, g+ q, M. pnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
7 ~9 p9 }* ^/ H2 A6 A! L9 d k8 X6 snginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
+ \, K# K. n) R, S$ w5 ]启动Nginx
. G* ?' v E) n4 y$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf9 D/ e9 D) P. j! S8 m1 J& K
# _1 e6 Y3 u, l4 a. n' ?3 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能够战胜更多复杂/未知/混淆的攻击模式。
8 E" I( E8 i) d- P. q, n E( Z |