ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
: e9 c! m" T9 R# n( m8 Z0 t' o4 X5 _1 K/ y. E A
. [8 f0 Q) U# N4 h2 S
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
2 o9 B& P ~6 D3 j8 S# i2 W& U% `5 X/ G6 u
1 \5 f- V+ w0 P/ O9 p9 u$ g" l什么是ModSecurity% W' l% ~8 K! o+ b. k
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
) D9 J. D2 N+ L* S" \# N( |/ T
0 {/ s" L3 s# n4 J5 h
/ h2 S& n6 \, ~+ q p% Y6 ~7 rModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。8 @; c" ?$ \1 J7 x( ?) Z
, ~* W/ |3 S! y3 L F/ N7 Z7 g, u2 w
0 @+ G8 f* L* B1 J+ p9 H* mModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
% Z4 S8 T# n6 {: Q+ j5 f$ g
5 T9 R, _. l0 b n" L! F O8 B$ w
官网: https://www.modsecurity.org/
5 O+ V% {7 j7 V0 \" f
c4 e! V" o' R% }' p8 o% G! v, b: {! J
什么是OWASP CRS
8 g% W# g: e) t- ?1 JOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。# \1 |9 G; w0 A# T3 n
7 u+ p, w. T8 c/ {+ M& f
. F' |9 v3 Z# ], T. _ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。 `$ C9 t h* @% y2 h$ K5 N( T0 ]
5 `% ?7 {" B3 f% |* p& l( \
3 C- l* a- n3 f, x! N
HTTP Protection(HTTP防御)
& V7 f% Q1 }; n' mHTTP协议和本地定义使用的detectsviolations策略。
: s! R. v7 W) t/ E( G) `6 J5 k- Q
& G0 q0 s- `* `; i& |! |8 `) {, k- l3 i+ D/ p) b
Real-time Blacklist Lookups(实时黑名单查询)' @' Z/ {5 s* S
利用第三方IP名单。
' [: t3 o1 u9 [' F+ x+ E5 \; i
2 c; p6 `( ?6 [; p2 h
1 h5 y: d4 X8 a; Y% B3 Q% tHTTP Denial of Service Protections(HTTP的拒绝服务保护)
W4 J+ @; ~1 T" z# g防御HTTP的洪水攻击和HTTP Dos攻击。. u2 r" E) i2 M) e2 D* v
( @. y! F7 h& J& z
6 U s* ?- V4 r" KCommon Web Attacks Protection(常见的Web攻击防护)& `& E, V( o0 p8 ~# V; G t* j6 |5 V
检测常见的Web应用程序的安全攻击。
0 e, ~$ j! ^" Z( s, y G/ h) E$ U) z4 z' p0 g. ?
% z7 K( k" W4 w7 D2 AAutomation Detection(自动化检测)
# J5 f1 f2 z0 I7 q检测机器人,爬虫,扫描仪和其他表面恶意活动。3 w, _5 e, H. ^
. E, z/ \0 k1 i S+ p0 X6 y* k% u; [
* R0 b4 Y( |- i, \2 i& ]5 TIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
( u% V1 g, s$ t! K* E7 @检测通过Web应用程序上传的恶意文件。
" }4 ?) _( q1 t, e* j6 n( {3 h4 f8 |+ b2 U5 K
9 h2 v6 A. M$ v& S% sTracking Sensitive Data(跟踪敏感数据)& q$ i1 V' i9 _+ y9 f) R
信用卡通道的使用,并阻止泄漏。
( c4 Q- {1 Y8 L
% m$ l* C1 Q5 @# _2 Z4 E
7 ~+ `# I) T, c5 K/ ZTrojan Protection(木马防护)
2 d/ P/ G+ t" I检测访问木马。
$ o4 h }/ A6 `0 J2 `" u
8 X0 Y9 m g# d) b4 D/ t L! i" E3 u) {' N, s& t) h4 A
Identification of Application Defects(应用程序缺陷的鉴定)* K4 s- Y: T/ Y9 u2 I
检测应用程序的错误配置警报。
( N' O9 D% f7 ~* W2 _; \
6 ~. m: n6 ~. X5 W3 q/ e; r
( x, n) T% r0 dError Detection and Hiding(错误检测和隐藏)1 _1 c8 X0 I4 o- y0 u+ f# w& ]# D
检测伪装服务器发送错误消息。
3 C+ [3 Q1 w& S2 H: H! }& M9 O) e5 r( r* Q9 s% j! @, ]0 \
! e. d' p2 U) T& I; k8 R
安装ModSecurity) G& U7 \$ j4 _- [9 s' f7 ]
软件基础环境准备
) Z0 S, s4 Q) x下载对应软件包
* E |5 h! B2 ?+ p& P7 t$ cd /root3 s5 M; O4 ]" ?3 R' v
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'' T7 `) i0 x& E, `. ?
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz8 J5 l! s8 \0 l. h @: c
安装Nginx和ModSecurity依赖包/ `$ n* a p, I. n6 Q
Centos/RHEL2 R! G2 x% \ Q% C0 Q, B
( ]4 ^1 r% `: p. l3 F( N; b
, t) w7 p- p* h/ R$ ?9 S$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
5 r: K. W f0 e5 zUbuntu/Debian0 l/ N5 q/ B4 Q' J
j4 S1 ]+ N% b
- t) _ ?0 ?2 j% q' w% e" `$ 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$ ]/ W- w' o7 T/ d( G8 |# C
编译安装ModSecurity7 |7 x3 S" Q" ]6 q$ f7 v
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。( d7 F) G8 J9 i" l: c& T0 D
0 H: J1 j% v7 s# ^; ~% e
9 b$ T! g( d, A) A" m( Q9 [; B6 V$ H方法一:编译为Nginx静态模块
8 f2 F- z! ~* U
9 B- |( i, ^: P
* z/ N6 q2 v4 ^7 v编译为独立模块(modsecurity-2.9.1)
8 |$ V7 H D! U' F$ tar xzvf modsecurity-2.9.1.tar.gz
4 F* \( V, r, p9 L& u$ cd modsecurity-2.9.1/
( e' `3 u/ N) N7 l @+ H8 N$ ./autogen.sh$ U0 _2 y7 a& G" ]5 J
$ ./configure --enable-standalone-module --disable-mlogc
' A$ z0 \ p8 R5 \4 E5 J6 n$ make
2 [5 b% o9 T; J2 P2 v编译安装Nginx并添加ModSecurity模块, ]1 @0 _) a) w( g
$ tar xzvf nginx-1.9.2.tar.gz
; U! z$ _* K5 G/ O* X$ cd nginx-1.9.2: d9 \6 |6 ?; i
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
& t* S0 X, _, E, n! M$ make && make install
% | [- I4 {6 B+ A7 @$ R方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
& _6 d# ?: P/ `# ~3 k5 f/ p( I9 {) U) @
" Z8 d0 L# \* {1 f编译LibModSecurity(modsecurity-3.0)
y$ h, h6 u* @+ N- J$ cd /root
: u3 U# L& i* q2 c$ git clone https://github.com/SpiderLabs/ModSecurity! T$ k9 V* z/ x
$ cd ModSecurity3 w/ z7 ]* d3 g3 ~( D
$ git checkout -b v3/master origin/v3/master
* W4 W# |4 z/ n$ sh build.sh
1 d4 S: J ]/ L2 ^% a% |+ A, A4 {$ git submodule init! X$ w; H0 X( B# j$ D
$ git submodule update
' W! W( D+ A7 ^+ f; x1 s; E$ ./configure
) O- y8 l3 p1 \+ I$ make
# B8 o4 t1 U% W7 g' b$ make install
u( Z* M- s l! `2 w iLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。6 s1 L! _* L- V( K+ z8 d# G3 P
1 {0 J; n0 t: a" i; L. W2 A
1 j: D* H; @0 _4 g4 h# J7 w
$ ls /usr/local/modsecurity/lib
7 J- V" K! `* p% olibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.07 `+ ?* A; {& d" q& v* x5 L
编译安装Nginx并添加ModSecurity-Nginx Connector模块5 ?# M. F# j3 C0 D0 j
使用ModSecurity-Nginx模块来连接LibModSecurity
+ U% b! q3 |; a+ x3 D1 A) r; |5 z$ a6 U% q+ {
5 @ U/ k" e% p/ e; _- g
$ cd /root
, h% @1 J: I+ h6 G# Y# w) P) G) e$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx0 V2 j+ U" l# l3 O2 x- _
$ tar xzvf nginx-1.9.2.tar.gz5 o& _# ]: W- G4 B4 V
$ cd nginx-1.9.2
$ l- u R8 ]. f5 {2 x" z+ k$ ./configure --add-module=/root/modsecurity-nginx, q% \6 o) q$ N) p
$ make
$ E. P" ?6 g _$ i. q$ make && make install
# k) W/ w4 r( g* `添加OWASP规则5 u1 K# M, b) e, \+ b$ p8 g
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
1 y9 c3 F+ |2 `& c, ]
4 X& s6 Q9 N2 U, n
( u3 t2 D$ @5 M: m9 [' O下载OWASP规则并生成配置文件
* g; o; Q0 r1 \7 @" A0 E. M& N# e" w$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git5 ]0 G/ ?, Q! j4 P" y4 f
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
- F; w: x' W+ i- V$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
% d' a% b/ ?! S" f" T: c. p7 C$ cp crs-setup.conf.example crs-setup.conf3 ^& P9 b+ @ A* J1 X
配置OWASP规则
, n4 i' e( Z7 M: r) x; @+ R8 M编辑crs-setup.conf文件# {- I- ?' s7 A" k. D0 k
4 Y# ^& k& [& g8 g7 g8 t
6 Q7 Y) m. O& V- e g7 f+ }- e$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
1 n F( E3 g3 |0 ~8 z) G9 E5 }2 ?$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf/ o+ p g, A9 `4 w+ {% U- K
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf* e, I& `) ~& }& V0 P+ Q
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf: m7 _2 p# S5 B9 Z+ Z2 v, ]
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。/ d6 V' E* l4 f+ ]) a
4 P# A9 o! i9 H0 i! O1 \1 K! M8 _" ?! @5 _
启用ModSecurity模块和CRS规则
0 R" q$ Y% f/ U# q( L+ h% j复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。* s% W7 g( q/ l* D
" K7 y) z/ }9 p, P+ D( n
8 `2 a5 s2 B: `+ I+ K+ _modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。- g$ H' r. A2 D4 `9 g/ `, i
' S ?7 c2 ^0 Z4 R! [
1 d8 n5 S" u) \" Q) _$ cd /root/modsecurity-2.9.1/; d; T7 A) x# x: g
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf + W3 [- v7 U' u( J6 X$ g
$ cp unicode.mapping /usr/local/nginx/conf/
7 ?, w) R4 H8 K! A, J将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
" H& Z3 ~3 F5 U+ f" ?
0 n' R, m- d( z8 h7 E
/ |% [8 d. g. }* u$ vim /usr/local/nginx/conf/modsecurity.conf
! Q7 C+ }3 j7 W( n* p! P: S3 J* q. h4 nSecRuleEngine On: o* P* a7 `3 ~9 t# v& X8 T/ n
ModSecurity中几个常用配置说明:$ y4 ^+ L- s% v5 z
+ m$ Q8 ?& W' v! q' N7 h2 \
/ \# [9 g) L$ m' ~1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
& B, z% n! @" p! v5 @' @
5 q; t1 Y' W0 B6 p5 s1 ~+ e% U1 E7 ]
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。8 x5 X8 @$ G, e n! X# o
! y/ p o0 g) T% O2 B
3 |+ @ Q1 m5 x6 o ^: k0 d. F6 v3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。% P/ r9 r. V8 m9 G9 H3 k* K
, a2 p% q' k( C4 G; i" W6 I) f+ W& ]( {( `) W5 T+ L# k
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。7 |0 c: p5 J# q
- F7 E( D& [8 K3 {. c3 x, T0 x& b% G- d5 u, T. \% Z& K
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
' p: E% t2 Z* C/ }3 S# ^; w6 G
8 b( Z. v# ^1 m& z! ^
^% [" P4 _, o! g! Z: V* v# M3.x版本CRS
" j. V; g: K. w+ G3 v$ t$ cd /usr/local/nginx/conf/owasp-modsecurity-crs( L: q. a: X; U
# 生成例外排除请求的配置文件0 I9 y/ w7 c n! X" {! z0 a& g. p
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf2 X3 j" [5 H3 f, E5 T' ~7 Z/ S p
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
. d5 |5 f% m& l1 ^2 X6 ]$ cp rules/*.data /usr/local/nginx/conf
( i O) A' ~! K+ I为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
& S4 y$ o4 G; U, S J' O
4 v$ ?' \3 w X4 i/ c& l _' i
% N' g1 E& e* Q) F/ p$ vim /usr/local/nginx/conf/modsec_includes.conf6 k, Z7 t3 _2 P2 |+ S- H
+ v( w8 M, l; H& Y
[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 5 U: A8 u0 o6 r6 S7 `+ J
+ R, d" |0 `% R注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
# ^& x1 |- n6 ^9 t3 R- @3 _" u: q0 W$ H- r+ q% K* }- @+ q
; x; ~% j) \' ^配置Nginx支持Modsecurity0 n$ {- h- r6 s
启用Modsecurity
: l: A5 t# ~# }2 G5 J: ^: [使用静态模块加载的配置方法
" }% ~. u0 y- t& Y& l7 |在需要启用Modsecurity的主机的location下面加入下面两行即可:
8 N; [ }. `! N6 _
8 w, k% b! _8 I0 M+ E8 {% M: E |0 k1 U1 u# I9 g9 p3 L
ModSecurityEnabled on;( I; l! T! K, U8 B
ModSecurityConfig modsec_includes.conf;- g# I; c+ b" a( H- F# N% C
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。$ J; P4 A. }$ X9 }
$ {* {' j5 f8 D' K# e+ P
8 |8 x3 ?& J# Y/ u$ vim /usr/local/nginx/conf/nginx.conf
' N. s, ^! o8 r$ L: u
% q# O0 S5 X& n/ [% |% a8 S
& b0 p1 t& `' d* ~6 m1 Xserver {
% y* w" B" L3 d( s! p! {+ s3 F! S listen 80;; E4 z: }, ^ m8 [
server_name example.com;6 v4 J' @3 A2 F2 `' t3 y
* I7 K! H! P0 t7 Y$ G/ a4 ^4 {- e$ m9 I* m' i
location / {- P7 z o! [- B3 Z7 {2 O
ModSecurityEnabled on;9 L- G. |9 v: z# F6 ^" G+ t6 X& W
ModSecurityConfig modsec_includes.conf;
- ]7 X, j F8 U) x9 W, K- U root html;
3 u6 }7 B' }) }3 A, y index index.html index.htm; Z" M& @2 k3 M7 V
}9 [. J5 Q* ]/ J, T/ s0 D
}1 K. g4 i3 e+ U8 }+ _$ N0 Q
使用动态模块加载的配置方法. W% B) J) _7 x y
在需要启用Modsecurity的主机的location下面加入下面两行即可:
9 W6 i6 H' [2 d0 X- z2 V; ]/ s9 Q* t4 J& c. u3 H
' `# R' |/ m$ G/ f
modsecurity on;
! H a3 q6 W' x5 o' nmodsecurity_rules_file modsec_includes.conf;+ J4 _# }/ K/ o, a
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
/ n* Q. r/ r9 S% v1 [& M! [" ^) d" H2 R" B" C
8 P& ^0 [* n/ b4 {* t4 k7 t$ vim /usr/local/nginx/conf/nginx.conf
+ H g) A$ n8 ?/ u- G; {2 O" P9 n
' Y0 r; [, ~$ J- ]) [& M% c* ]8 d
" A; d& O" ~3 bserver {
) C5 J' V8 ]+ h1 J: T listen 80;
/ |, h# f+ _ s) z3 D$ n server_name localhost mike.hi-linux.com;* b8 K0 ^* z9 Q2 T1 r+ g5 n
access_log /var/log/nginx/yourdomain.log;
$ |0 I& Y" {+ F0 |9 F9 [* b$ l7 P) ?( g* X
; U5 m5 v8 ~2 h9 @ location / {1 ~; Z2 ]2 l7 x
1 ?' N8 m/ U+ u" l! E) F! ^4 W5 v
% P A' x& v$ Y! n. i% W
modsecurity on;
% E' N; H! @$ Q8 i7 [4 ] modsecurity_rules_file modsec_includes.conf;% d9 b% c1 `( b
root html;
" p7 X6 W: s0 E, R: p+ k index index.html index.htm;
" K4 E. y4 O9 J2 o+ c}$ q1 I3 ^$ G# I7 P
}
" E" [. m+ v0 ]8 _验证Nginx配置文件4 i$ x1 ~* t5 k: Q
$ /usr/local/nginx/sbin/nginx -t
5 P5 |& U) e- Ynginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
f. s1 H' |& V- T% A* M* C1 V$ r2 Tnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful; ?8 x# g- M# w" a# w: S
启动Nginx q* H9 j4 {3 t: e
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf8 Z" i* R+ Y: f {% ~8 E
3 J+ ]- b( z: i' H* f! Z
测试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能够战胜更多复杂/未知/混淆的攻击模式。
' j- V2 }1 _, `+ \ |