ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
: O% `0 }+ A8 U9 z/ u/ q- ?7 B) O" w5 z# ]& q
" p, \9 p/ I9 ^5 r. {( b
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
! H, W7 W1 s0 t0 c1 x4 @: ]( T) u) g: m' o
$ E5 e0 G1 K8 }4 Z/ U
什么是ModSecurity3 I( H: E" n3 ~+ i8 u8 H
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
2 b1 K& I+ u6 M# {% f3 Q% V7 J1 E
. x" O8 Z9 P7 @! M* b
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
1 g1 W4 m }3 J" P2 S( O/ g$ P4 g8 V- |5 D+ f) ~, y! s
9 _- f3 r" A( E: e$ XModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
d8 T" G8 K6 j) N, C7 i- E, R) V8 o
) @* ^% D9 g, f% L
官网: https://www.modsecurity.org/) W5 u0 c6 n) y, j3 H6 j
% O a/ x$ b9 n( C3 O9 e, b- ?6 E3 G$ s$ D. @: z
什么是OWASP CRS
! K: b/ ~; {' A: b" b/ l3 ~OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。* ?) Z" B: I3 v0 |) [, F# @
5 |5 V. d: X- ~% }: L/ V( p3 b# J) p9 v( A
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
* g$ M% ^7 L$ Z8 b0 W9 k" s; } R9 F! B: e$ R+ ~$ c- z1 V
1 v8 J/ j( Q6 ^3 Y: n+ |
HTTP Protection(HTTP防御)
3 ?/ ]3 L. p9 N. ~! WHTTP协议和本地定义使用的detectsviolations策略。7 }7 U# {0 ?. P7 H a$ G
. T7 T; c ^( d, ~" a9 ^0 Y
8 F0 H/ ^ q* l* J! @( \Real-time Blacklist Lookups(实时黑名单查询)7 ?0 x, H; ~6 M' B" G' ^
利用第三方IP名单。, `0 O% q0 S3 B& U0 x
l0 I0 Y( j! ?' y8 i9 Y7 g# _& H4 ~# [( k; T. q6 l2 n; U% H1 G
HTTP Denial of Service Protections(HTTP的拒绝服务保护); S2 I2 ^+ y8 ~( `0 ~
防御HTTP的洪水攻击和HTTP Dos攻击。
+ w, L6 |" T* c8 x* r; O4 j" J' o! ~3 H: o! j. @( d
$ g% q* [1 ~1 V7 }* y) E
Common Web Attacks Protection(常见的Web攻击防护)
$ j7 A/ T2 v* j5 f4 n8 G检测常见的Web应用程序的安全攻击。+ M% j8 \2 h$ T9 A" U# G* f9 \
, o U' M3 u( |* ^8 q1 q( K
4 u: J( W/ `. m7 T1 R% q7 RAutomation Detection(自动化检测)
# h, W8 i' q- n5 C; K! x检测机器人,爬虫,扫描仪和其他表面恶意活动。8 G; }* z2 E! x% J
7 x X7 }$ o7 J( x7 {3 A: \- N/ g# ~0 N6 c$ Q
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)) c$ u$ P3 p6 \8 ~6 s7 e' i" v
检测通过Web应用程序上传的恶意文件。
6 ]6 _# H& L1 q8 q4 l' K3 B
# f$ A* K8 ~' e# [
: K$ O3 j' J+ ^7 STracking Sensitive Data(跟踪敏感数据). e' V4 W( M- v
信用卡通道的使用,并阻止泄漏。
0 K! v$ C, n& S
0 x; y- B$ Y9 V' k0 {2 }) z! ~5 P/ \8 ?4 w' j
Trojan Protection(木马防护)
% B; k: }, B8 R1 B# c4 E Q检测访问木马。2 A; p! }, v- B9 s1 c7 e
) Z" M/ Q! n3 m" X1 n
4 G" w/ u1 R# {: AIdentification of Application Defects(应用程序缺陷的鉴定)
/ r$ b% O- q5 P ^$ ?- x4 l6 |- F& C检测应用程序的错误配置警报。
9 ~9 s* \, h' V: N4 t3 l" Z! [" \7 X5 R: x
4 O4 |* X0 N0 S, IError Detection and Hiding(错误检测和隐藏)
5 J6 m' X4 i: Y, A) c% J$ J检测伪装服务器发送错误消息。
3 S4 y& ~$ u/ m8 b
; T/ J5 d/ `( ]! i. a7 F7 M) R% F) W3 m% c! J, z
安装ModSecurity
5 k$ c0 q# k+ i6 X- p软件基础环境准备
: s, {' Y) c2 Q! W9 }下载对应软件包
4 T) H7 Y& I: w" M0 y$ cd /root! c6 C! t4 v" A
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'7 X2 v' R7 Q1 j* R9 E2 S8 ^& w
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
8 g1 O& y( c# q安装Nginx和ModSecurity依赖包: A1 l/ b6 E8 Y- K. n) G0 H- v
Centos/RHEL
' Q& G+ d8 p3 G) v! [+ F3 A5 w8 ~/ E+ ]( B \" o
6 W& J9 [" ?/ h& @( F$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel4 R3 Z! z1 n6 {( E
Ubuntu/Debian: v9 V/ n" L# V6 N: O- u K; L
4 K' I) r0 u: v6 ^0 u1 l
, h, b) m" _/ O) ~$ 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
: O; o w# f [2 n+ E' s& F* @( r编译安装ModSecurity
X9 r& K/ G* ?Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
7 P8 Z% c, r+ }
/ U+ P8 `8 I# w5 t, G( \0 {
+ _8 M2 u' P2 i0 c方法一:编译为Nginx静态模块2 s6 `2 m4 ~) d0 R) W# H1 o
4 Y% Z- j( }' i0 ~1 n: l
- t& X8 b' [9 @! j编译为独立模块(modsecurity-2.9.1)9 B n& j# H4 \0 z, Y g
$ tar xzvf modsecurity-2.9.1.tar.gz
' z9 k5 C, d5 A2 p& \$ cd modsecurity-2.9.1/
$ W7 r: V- f) H$ ./autogen.sh
# V1 c& t" F" Q+ }4 X$ ./configure --enable-standalone-module --disable-mlogc+ u8 }2 S z1 [# c- G
$ make6 e7 ], `' M. Y. B! A9 U
编译安装Nginx并添加ModSecurity模块
( k2 u: V2 B1 N* n9 f. Y8 X6 T3 Y$ tar xzvf nginx-1.9.2.tar.gz, [3 N" \% E v& R3 H
$ cd nginx-1.9.20 T& }: w1 Z% U" |" R0 f
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/, `) I" @) K- g/ {7 M
$ make && make install% S X) K' U% n
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
. p, e2 V: p! P' w! `# B3 {
; i$ ]* D- O& ]- V& x: k3 N+ a
; H0 [6 E5 p( i9 l编译LibModSecurity(modsecurity-3.0)# [7 N; w) L: F4 G9 h' x) J8 H: m; {/ a
$ cd /root
( L1 l7 `7 e0 X3 V# _$ git clone https://github.com/SpiderLabs/ModSecurity
2 D: d- O2 E7 T( ]$ cd ModSecurity; c: D6 m; y" Q/ S" U- m
$ git checkout -b v3/master origin/v3/master9 I' N$ U& Q N" b9 }+ z
$ sh build.sh
' {/ r7 }! `- \- v# g# Z5 \$ git submodule init" T$ C; J1 Q5 d
$ git submodule update# }) |) R/ G2 J- S
$ ./configure* M6 H( ]( R: D) F5 a
$ make
, z: r, @- p3 b$ make install+ o& I$ e- l( A" L
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。8 l3 k0 j7 G+ J. f
- x! f7 i2 W* o5 X/ l r+ I( H4 T. v$ J
$ ls /usr/local/modsecurity/lib8 ~0 q: W! K& i2 J
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
- O( i" B/ j& }/ x! {2 G, J; o0 Z编译安装Nginx并添加ModSecurity-Nginx Connector模块
$ x. d+ T2 y- Y* F y使用ModSecurity-Nginx模块来连接LibModSecurity* N1 A# c& L9 g9 W: d
! U* h# A6 G) p( J) N# u/ H0 X
. P( ^0 [" c2 G7 E$ cd /root( H( F0 f! |3 l. l
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
6 O5 F6 j; I& u) i% O$ tar xzvf nginx-1.9.2.tar.gz% h. y& O" w0 c7 Y& g5 O
$ cd nginx-1.9.2
8 M t' h1 f% j G" X* ?: G$ ./configure --add-module=/root/modsecurity-nginx7 q. a9 ~) g- z0 R" W% `4 U
$ make V! ?8 k) q- R: A9 ~. [/ R
$ make && make install
8 K! Y9 y8 f) \0 `% }& |添加OWASP规则5 E3 _ q: |8 i- M. v
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。4 |+ k& J r& e9 A* g7 D
8 \1 ?3 }* u) |5 |+ b
9 n8 Y6 x* F; p4 c下载OWASP规则并生成配置文件) d: V X- F+ a' b3 s: K6 H7 v8 s
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git% v2 n U" F. ]: z0 L9 Y& L' W
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
+ n Q7 _" s8 N6 `! k! e$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
2 X* e, R4 j- Q B& a5 C/ n1 x5 i$ cp crs-setup.conf.example crs-setup.conf4 d- q% z5 @3 S& B
配置OWASP规则
& _& ?1 z# P! v& S- @编辑crs-setup.conf文件/ N3 c: ~: e/ X2 v8 P4 _
' [; L) p8 q0 o, V1 y( H/ H
* @# F9 ^, Z1 f' ?
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf' W3 e$ `- [/ a0 z
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf$ B3 ^ C" E& R0 @
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf2 x) J& h! p) e$ \6 Z
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
: l4 _' T1 p, M$ U6 K! l% }默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。4 S V Z) n1 c' C: n
' N( L3 R! g1 k, C: u6 K- H& r
* g2 u$ }3 _# v' M) P; O; K/ w- H. h启用ModSecurity模块和CRS规则2 x; E7 w# i1 n; a7 Z
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ }0 `9 J5 f% Q. f5 W: c3 r
* p* R) W! Z) @" y
8 v6 Z4 T0 ^5 l) b) g. dmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
$ f: r2 y( k' R3 D$ o3 j7 X5 m8 E2 a7 @9 z, t7 }
( `9 [4 H1 f' s; \) a9 \, m
$ cd /root/modsecurity-2.9.1/
% q, A$ H- y. z7 [% f$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
5 m; [3 a' G6 }, C* [$ cp unicode.mapping /usr/local/nginx/conf/
2 n; R2 \ i7 x q将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。- ?6 O( c* u% O) Y
7 Z/ p7 |5 H& [$ i
( L2 O0 q5 O# l6 I$ vim /usr/local/nginx/conf/modsecurity.conf8 ]( N8 E* f) A- ~1 h `! ^5 J
SecRuleEngine On
' T* T9 \* I5 E# ~ModSecurity中几个常用配置说明:
# `: W1 `8 \2 ^$ L2 Q+ K
- C( z# e- P/ z0 [
^2 A/ B+ |- B# }+ W1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。+ t# p: z: H3 P
. J! g' K$ |% U s7 Y+ [3 g! o
+ r9 P6 ?# E3 B8 \0 G2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。( g# N9 l/ [' V0 {7 U# q1 F( ~
. j7 P' X% h% a6 n0 S
9 M, i/ ]% Y! r2 ~* C3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。 M J& n' r% T
: Q5 n; Z+ `- q! u& B+ u# { H* L9 b# M
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
( V2 C# i) o$ w6 t( k- f+ g" q0 }: n& g% G' ?6 D. G8 _3 n- }0 c- {, J0 {8 D
- C" [) _% H0 `8 t* X5 X在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
- ]; Z) z5 v' U, c, ]. ]( t! i8 q. I' z" P
- S* j; L/ u( u0 O: ]. ]: W
3.x版本CRS
3 r& [ g: t% Y$ k% u3 R$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
# P4 |: [# Q/ }# d) Y# 生成例外排除请求的配置文件: c0 k1 |+ w7 `6 {5 _
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf5 |* `- ~6 d$ n, y% A# s6 `7 `
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
- F- p: P) y& y- x$ cp rules/*.data /usr/local/nginx/conf
; C- K6 x6 h |; u+ ~" I: q, X为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。4 D& V2 j) @# r, o/ a! {0 d1 ^
9 T7 W2 l' _3 h/ q, v6 j# l% s; r! c
$ vim /usr/local/nginx/conf/modsec_includes.conf$ ?2 F. d: [3 j- r0 Q
8 {+ y. [/ H3 h, \[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 + }2 W( V1 _8 J- }/ A
) L9 x( X7 M" p- R2 ~
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。! C# g2 m7 H+ c' u
7 r/ A. L+ B( s9 J+ b1 d# w
! J0 Y6 D" S0 |$ t
配置Nginx支持Modsecurity' R& U- z6 L1 X( o
启用Modsecurity
6 B6 l9 g7 f; B/ z* C! x使用静态模块加载的配置方法8 u! u2 s9 K" A: o0 _/ [* d
在需要启用Modsecurity的主机的location下面加入下面两行即可:' O! x5 B1 O. m' R
6 l6 y+ w: e. `- _
! v6 G# b1 F& ?6 WModSecurityEnabled on;
" f& B6 J. c9 n) ~8 M, MModSecurityConfig modsec_includes.conf;
7 |: V5 t8 F2 `' }; A: J+ `* v1 h修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
6 A9 J6 r& c8 s8 A& b, ?- W, _! e" R6 Y$ [
" @( X- {1 a. v8 f' l$ vim /usr/local/nginx/conf/nginx.conf
- f$ |/ B$ |9 C- c N9 X+ z. ^: M, ]; I# C0 `4 j
1 @! P2 _* {+ g" R. E
server {
; G5 V( s1 Z. b6 N4 y listen 80;" @- r2 `2 v0 j2 `! M5 H. R
server_name example.com;
7 q5 H8 V7 I0 @# N: Y2 O
9 w8 T: U) P4 ~5 M% m/ F7 r4 G% g& y1 Y _' ?# }: V
location / {
) L$ T9 L: P4 v ModSecurityEnabled on;
( p3 S. L- W/ O" }; L7 C3 W ModSecurityConfig modsec_includes.conf;
: t. k" ] M- s9 { root html;
6 w9 y! |- s; Z" A index index.html index.htm;$ k' g- M# y5 p
}2 O+ @- J- k+ S1 d
}
3 Y7 Z$ R, |& f3 K使用动态模块加载的配置方法
. w4 q1 y/ `' M. z+ X在需要启用Modsecurity的主机的location下面加入下面两行即可:
$ n1 [: {% I _2 q6 t/ X
5 C# b$ H( @$ \6 J! U8 b+ [4 ^8 d& @, h! ?5 g6 @2 z+ T& j2 R* H
modsecurity on;" h) f: ]% [. X0 x' H
modsecurity_rules_file modsec_includes.conf;$ B H, N8 P4 f# Z. o
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
/ h" G, R* _8 M6 N$ z
, Q+ A3 _$ R+ b0 o1 ]% V% ]- @/ p. f" Z& G+ g1 p. Z
$ vim /usr/local/nginx/conf/nginx.conf1 ~# ~7 h k7 L# O6 y# E9 g; C2 C
- X$ [/ l8 [5 Q/ s
* w6 c2 _' o7 i6 b1 H4 Lserver {
" s/ D4 V' I- u4 Y listen 80;
0 \7 m( g! h* F% y3 s( X" w# h0 V server_name localhost mike.hi-linux.com;
' ~) A- W4 \0 p& f' { f1 T9 B access_log /var/log/nginx/yourdomain.log;
0 P8 h% B: b3 M
7 ]) g+ w \% X% @( g$ }1 W: x
2 |- n. [, C5 a4 M location / {. R5 O" P4 ?/ n+ |' i
) s7 O9 @! k; @7 \# U! C3 n! t9 a6 x$ H, g, ?
modsecurity on;9 i" V6 o6 E) `8 A+ h4 u
modsecurity_rules_file modsec_includes.conf;
2 j/ v% Z- \ w root html;
9 T% V; ?* a! x% _8 ]- | index index.html index.htm;
; C2 T3 o8 ?, a, E6 I4 M) I' ~6 l}
0 k- I2 j) R i) I, Z4 c% t1 E}
% f8 i8 l. b6 G p验证Nginx配置文件
1 @* y: \3 P2 H$ /usr/local/nginx/sbin/nginx -t/ z, a* ~; F* ?' R% }
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok$ }5 p) I; y1 h' g8 _0 A
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful* A4 R/ ~* f% |5 l
启动Nginx' U+ P C2 ]% P' M6 T
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf7 A1 X: t5 c9 M6 I
2 R3 v4 ~+ ~+ E; N' 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能够战胜更多复杂/未知/混淆的攻击模式。 3 c% F4 t) V4 Z O
|