ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。; I% S! C) g. v B" p7 G
: j) D W6 s* w
) j4 u4 f8 |. H+ Q+ v在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
% W" I$ M8 G/ N# _& ` y3 a4 H0 o, W6 w3 i# }* T
- _3 B9 S* o% r: z' C8 o) g9 ^
什么是ModSecurity
6 g8 E0 L3 |0 m! Q! zModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
: a- b/ b- F* e$ k6 I! {3 c1 C9 F& q, I: s( C- d+ Y% \
A& `6 ?; \/ {) l: rModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。- i" u4 |# {8 A( L
& L& l9 \! q* y1 g* v
) L9 `$ c7 P$ J' UModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。# R! i5 u' q8 ~- P2 }4 b* s
7 s- P8 O7 @ }" ?# x# S. b* l, c) o" x/ M' x
官网: https://www.modsecurity.org/
( G, q+ ~1 |: Y, V g! T
& \; r2 b' ^: g/ q+ ~: D- f5 P4 b8 R8 U( b2 S3 y9 M
什么是OWASP CRS
Y; s& } A( [- V$ W6 {OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
) `9 k4 i8 b, z) t
$ h( u- v* `0 R7 O( ?. h: Q1 Z0 s6 [. n: L% @7 s
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。) C0 \; S/ |) I. j& Y6 K
% U$ k6 M' N# M) X1 y+ j+ ?7 y- X8 V0 t8 \1 Q8 W" E [
HTTP Protection(HTTP防御)
5 X k4 K& `" P8 i/ M- E2 k6 CHTTP协议和本地定义使用的detectsviolations策略。0 j& y5 F5 c5 t- r( _
+ Y: _$ D, N4 [' x5 i/ ~* W6 {4 F
Real-time Blacklist Lookups(实时黑名单查询)
7 c0 Y% e, A0 M9 X- O1 ^* Z利用第三方IP名单。, A3 N8 X) ?$ e& X5 J
0 {- F" p: e- t Z$ q/ g% [; G, x% g0 E \: q
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
' W. P) e& r1 l$ | r9 h3 A. Z防御HTTP的洪水攻击和HTTP Dos攻击。+ w* W5 u1 }: z! f
5 e7 R* o8 `8 S% b! m& k C/ y% H7 s O
Common Web Attacks Protection(常见的Web攻击防护)2 q; k/ D; b5 _
检测常见的Web应用程序的安全攻击。
% l8 e1 h% k9 t. a0 M" W
4 r2 }1 w: _# Q0 I0 ]) u1 J" J7 c8 _# |5 G7 n( ^# N/ ^
Automation Detection(自动化检测)$ A5 \8 W7 Y# Q$ I5 D, _
检测机器人,爬虫,扫描仪和其他表面恶意活动。
% a6 s( d% F# [ u. }
7 X: t2 D1 F* o5 S3 x# j0 {6 w5 T4 T/ G3 B
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
) s( t. q& K4 Y w) K% g检测通过Web应用程序上传的恶意文件。
0 A e' M- v4 e. O1 c- E/ ~5 `0 }+ w- S; B
. m7 P' w% z1 k- O3 {3 h6 r; P
Tracking Sensitive Data(跟踪敏感数据)) I) E+ j( D' O' c, L, }; h
信用卡通道的使用,并阻止泄漏。8 g- t4 B. ^! _2 O. n" _
' h" J; I0 \& v/ c& o/ e# G- \# T& c/ K- i+ h% M
Trojan Protection(木马防护)
1 e3 s: \) e6 f" \- g检测访问木马。: w0 B/ D* I5 x; X# |
% e% J7 s. \. Q# D7 [, _0 C
+ _" L8 X, Y6 n7 r
Identification of Application Defects(应用程序缺陷的鉴定)& K) G- O0 `* p8 A7 Q
检测应用程序的错误配置警报。& w/ P% b* g* Z; o, T
9 r# S$ `) S6 Z) L* V+ v
$ ^5 Q& S" `! \7 U! g
Error Detection and Hiding(错误检测和隐藏)) q! ^1 R4 B1 a) b8 i* \7 ^" i% S
检测伪装服务器发送错误消息。# f6 a) W% E- L: b, V# |3 I4 d6 ]
I: i1 g' Z8 i0 P0 \, p
3 L [) w0 Q7 ?0 [: ^4 p2 f安装ModSecurity
# p7 j. B" h8 z4 r e软件基础环境准备0 u+ Z+ B- I2 f" Y; M9 v
下载对应软件包
) H. \( V I( p: b$ cd /root5 S* P }3 ~2 g% B) \3 f* R
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
0 J z3 ~. y2 b( b) |+ A$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz0 T( E4 D" O6 @7 N
安装Nginx和ModSecurity依赖包
; q/ _& a1 D, z3 ZCentos/RHEL
" r3 d* M; M7 ?5 o
3 g% I/ w5 D1 H" W
4 m, P% L- s- j# i' E$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
. r* ~' l. [- | \9 ?0 K' x8 mUbuntu/Debian
; T- O" M2 U4 h5 v9 g# A) T U3 W, h0 E5 h( ?+ R* g
{( } J; u+ J& u$ 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
6 n. J4 _5 l1 |6 b& Q' ]* _/ A编译安装ModSecurity
# Q6 S2 ~$ t3 y- y+ PNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
/ Z: Z" g* B+ y; b8 ~% {4 E) E
/ {. t: x$ f/ K% g N) e& ~1 y0 `* G: x, F# M
方法一:编译为Nginx静态模块
# P) _* i* v' }! u3 B4 @
& Q y2 }# {5 d$ i" ^! h. V( {9 }% F9 P& F& d h7 u- Q* U6 B
编译为独立模块(modsecurity-2.9.1)+ |) U9 w6 k1 \, Z" d
$ tar xzvf modsecurity-2.9.1.tar.gz5 E8 j- g3 Y* A6 O
$ cd modsecurity-2.9.1/
: V( s0 U3 B9 I/ d z5 y2 z$ ./autogen.sh
9 C! k4 ]! a7 v) {; V- B" H7 M$ ./configure --enable-standalone-module --disable-mlogc
" W( q- n" z- F- i, t1 k$ make
2 G% @$ P/ T; n) G" Y2 D编译安装Nginx并添加ModSecurity模块 q) Y0 j! H" ?8 d1 i
$ tar xzvf nginx-1.9.2.tar.gz
. e! E& b2 U" O% V* J4 Y7 U6 u$ cd nginx-1.9.2
. j" V8 w5 t8 m' o7 P6 i- J! F$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/- Y; h$ |' X; x
$ make && make install, a1 G+ m9 x0 ^4 H: y' B
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块; g' S' [0 _* Z: e3 K
$ F3 r" S4 H h# X _+ g
5 {; k: K( r# d编译LibModSecurity(modsecurity-3.0)
1 j V) [: G7 o" ~+ t$ cd /root
) d- M8 B T+ C4 ]. G$ [$ git clone https://github.com/SpiderLabs/ModSecurity
- j+ Z6 ^9 b' Q8 ^- G; D$ cd ModSecurity: I8 s; H7 h* E, V$ q3 X
$ git checkout -b v3/master origin/v3/master9 s/ t; v1 W# @
$ sh build.sh
/ }" |. w9 Y* W0 O9 v* l: H$ git submodule init
o* e0 |5 A$ S8 q. H% @- o$ git submodule update* `' }6 U7 H, E8 H, i! r2 I
$ ./configure
7 f' U2 \9 P I7 H: X; M/ g$ make
+ Z2 c! Y2 l7 F! b6 \$ make install- y0 z+ f& W# W5 ~7 \8 F* S
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。/ L1 g' G& V% ~) u
( F+ _+ G# T! L9 J0 S
& d- O' \3 @6 r ~4 {- t* H$ ls /usr/local/modsecurity/lib
& S [5 C% w# m4 r: |4 ]( y" W8 c+ qlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0- I) z) i( b+ n; p9 t% N
编译安装Nginx并添加ModSecurity-Nginx Connector模块5 l8 {* s8 f6 z' |
使用ModSecurity-Nginx模块来连接LibModSecurity5 C) D, J: H8 \8 E
. v9 a: N% G' h, L) ?0 X# f! z
2 ^( ^7 b5 ]+ ?; l! I$ cd /root3 ]0 W, y% ?* g! C2 T$ A/ v
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
- |7 `$ v( J, ]: Y) X- b" A5 k( C$ tar xzvf nginx-1.9.2.tar.gz
2 `4 [+ E& N! g$ cd nginx-1.9.2' \, d$ p$ H/ x4 ?: v( t
$ ./configure --add-module=/root/modsecurity-nginx1 B* b; Z' S' m' K$ _
$ make
* N4 O% J7 W/ U# D( j: `$ make && make install
" Q7 I0 {( [8 x- i添加OWASP规则5 E# @2 n) V2 P
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。3 N: S. z F: [+ P( `
6 y$ J/ {" I) S& U! E, ?8 H
) U [: R9 [: ^2 x
下载OWASP规则并生成配置文件
' ]& R& |* m+ k& r2 u' R# ~6 |$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git ]; n+ @1 Z9 i% O4 ~6 H9 N
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
$ p$ x0 [5 d% R* \" F) \$ cd /usr/local/nginx/conf/owasp-modsecurity-crs4 t# F$ b/ W& }; k- q$ t! W
$ cp crs-setup.conf.example crs-setup.conf
4 P5 y% I( _* F& c# c配置OWASP规则
) Q. N. n& \4 J4 o+ |编辑crs-setup.conf文件
* }3 T! {' H& b& O8 C# `' w1 \; g3 ]( L! V! E
$ ? V }' P" `0 e+ Z$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
. ^ Z* K2 X, ]' `$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf/ t3 `# O3 j$ r$ c; T8 O8 T
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
* w5 }& C7 F, F9 F. C; M$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf* u! O K8 N; e' X1 E" J
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。6 u, @: M6 g0 h% F C0 R# M
% w8 X# I8 f0 l6 a& Z1 W5 N
# C% L" W$ Q" @% S2 `* e7 j' c
启用ModSecurity模块和CRS规则
$ R- o1 G4 O) V" u; E5 w" I8 m复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 L$ \7 K4 K' X6 g
$ b4 V' W6 D. N/ v, G9 b2 @4 W2 g+ o
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
4 I' ]# \! A) o+ i0 Q/ Y1 T% C; v1 c6 V7 J, G
- v8 S' ~) d E; m v( ^6 e$ cd /root/modsecurity-2.9.1/+ j" X2 G- ]# X3 [7 H
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
' W8 k$ ]9 A$ X5 r$ N/ }$ cp unicode.mapping /usr/local/nginx/conf// w9 V7 H' ^( E9 D
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。0 _3 f7 c" i/ O9 O, C4 o' E3 _
4 f, {% W. i. `/ L/ W1 ?2 b) U$ `6 ]& Z) w5 Y2 j: r* t8 u
$ vim /usr/local/nginx/conf/modsecurity.conf
* Q6 {5 ?: M W5 w0 c" t/ _SecRuleEngine On z7 p* J6 Q8 q, A0 R% z
ModSecurity中几个常用配置说明:
* k+ p8 k* x! ^; e& q/ K# r; Q- Q- P; Q, e( i( o
+ g+ f9 `2 A7 F1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
& l% F3 h @# Y
- u p4 g' B0 J! z/ ?3 c
; C- a" a8 l) r3 c2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
& R& d8 ~" n* b& s0 ]/ V Y! [( Y/ }% F) s. e7 Y
( {5 U, ?7 A7 C4 w" Y$ Z5 C4 `
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
: D! q1 f0 D4 m) @% V0 E( c
: t+ Q8 B( Q! N& @( z/ C a' d; v( G
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
0 N, O3 f) F6 @! x6 A3 J/ t
, F, f, v2 }+ ^/ B
1 M6 Z7 {8 u" W' Q在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。/ B8 @8 @6 I; k( S' N
: \* U s3 _2 W- E
]' Q$ G) t+ c6 d( g" Y3.x版本CRS$ p P1 B1 ^7 O$ q8 ?9 P
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
! U' ]& J* P9 x/ e+ y; l# 生成例外排除请求的配置文件
- G X- T1 Y: T, S6 L- h8 r$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf& D: N+ ]4 m2 O+ k
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
$ @# H! J1 g7 O% a/ e+ {2 ?* \* V- a2 d$ cp rules/*.data /usr/local/nginx/conf3 b7 d0 N( o5 z4 K% Z
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。; m8 I5 N9 ^3 }% E! U
A9 C6 U, n' k7 q! k
/ I6 K7 I8 ]4 ]7 C9 C$ vim /usr/local/nginx/conf/modsec_includes.conf
' c! ^$ ]! S. `) ^5 d% v, d- V9 l4 q0 S0 D
[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
: e" J& Y0 i' p9 X- E1 Y" ?1 [0 l t+ k3 g9 L+ d/ ?8 L1 s
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
$ u" q. N. y5 p7 g6 e8 T. N
( Z0 B h0 s& C+ s# j. B
- ^% g+ T, ?8 p/ a8 d' V配置Nginx支持Modsecurity
! S" P1 [3 i- O* o, V1 I& H X启用Modsecurity" b( e( ~# `: i4 @' ?, K5 |% {! n
使用静态模块加载的配置方法! `$ t3 ~( z7 T1 `$ Z
在需要启用Modsecurity的主机的location下面加入下面两行即可:
. C, w. @. D- l2 _- k
" e- w3 |, B+ }+ X1 c$ Q
" @7 {5 u" A! oModSecurityEnabled on;9 S9 C9 v5 j$ V, I0 {6 n7 l
ModSecurityConfig modsec_includes.conf;
+ }8 Q9 G$ @$ g9 C; K修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。4 s/ R" H& S" C, N+ Q) ^8 P
) M' R4 W5 b, |' f! s- m/ j
6 R0 N8 A0 v t8 O$ vim /usr/local/nginx/conf/nginx.conf% [8 P. A# _1 T9 X. r
# g0 k$ F H9 C
, e% M$ v8 z! S; L userver { F& {' |4 ]* n
listen 80;% d' y$ H# e9 g( x' O3 U
server_name example.com; i2 M# |! t4 [! N
, }, [9 E& Q. n) l5 d
( J. u3 O' J6 {& p, q& |& O0 E location / {
7 T8 Z; ~) d9 a: R ModSecurityEnabled on;0 |! p* p: o4 F2 T( Q
ModSecurityConfig modsec_includes.conf;
) e. A( r& X$ y0 [, T& \0 c root html;
! z3 H/ o2 u% u3 V/ v& d6 L index index.html index.htm;
2 x' J5 ^# X; i. z }1 G( n/ e! l3 r6 N4 ~& F" v
}# p/ T- V; u( @- k1 J: ]0 T
使用动态模块加载的配置方法' u g8 Y f! L1 G
在需要启用Modsecurity的主机的location下面加入下面两行即可:
$ v% ~" N: f: ?# A, E& T
3 @+ S X' I8 ^2 l( G
7 `+ G* w. A1 c7 n$ vmodsecurity on;1 n: t4 g, \1 w
modsecurity_rules_file modsec_includes.conf;
, \% F* b9 c9 m0 b修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。9 j* Y5 M2 U/ c/ q" ?; _
) l/ P4 z5 C7 h9 L; Y
9 X6 ?1 o/ T: V$ vim /usr/local/nginx/conf/nginx.conf
_, Z- \, }9 N& Y0 ~+ U3 L; \ Y% q4 ~: f: d7 r0 y) w0 [* |
' v9 @1 i2 Y% T& P7 m+ `server {
/ l8 ]0 e( u' ?" { listen 80;. B/ v! S& w. G N/ { W
server_name localhost mike.hi-linux.com;
6 U6 V4 c, _- d( t$ S" s" i access_log /var/log/nginx/yourdomain.log;! E; X0 K% I' j' {9 A6 I7 }
2 o( @; w. L* i# i/ P+ U, h
2 q: K/ U, @/ V# K* ]9 M
location / {& J% s: R* C/ Y$ ?7 q" N
5 N5 Y" j# t( I
8 Z* j; s% M4 E/ [) p# G1 t
modsecurity on;- w: g8 _" s7 |# x( K% D3 t6 i* O
modsecurity_rules_file modsec_includes.conf;
- B% v' U! I5 P; ?* U6 @( n R root html;
* S* Z( @. J* K index index.html index.htm;
7 G4 D, E# ~5 a# U2 m% m}$ ]5 Y% i2 C* c7 v* z
}
( H: L+ n, I; T5 R验证Nginx配置文件; O5 a; k5 l: X4 M' A3 j
$ /usr/local/nginx/sbin/nginx -t8 s5 P- N" ?, X; v
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok A# x* m3 p1 U7 X' ^# @
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
6 |" f" m) D0 ]5 Z; I启动Nginx
! s% i& u$ ^3 v# R% P r$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
x3 v: \; Y$ L& @$ _
6 L. m3 h' d" p$ D5 h/ 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能够战胜更多复杂/未知/混淆的攻击模式。
3 i: W9 C' C2 {) N' ^- \ |