找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11291|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。+ i2 `4 _* R; `4 F

, b; E  s3 t. c; G6 O4 q: @0 v
5 o% y5 X2 N8 p# j  [在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。" I; m2 Z+ g5 Y8 Z& R) S
8 e  I* w5 |- x! a$ w; u! S' g! p

9 b+ `/ c8 P6 v3 ~. N, _5 N' n, M什么是ModSecurity
* l% R2 h5 e3 A7 o7 eModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。/ }$ F) u6 I) x' ?  l& n

/ t3 ~' `: s, S. j" F- `4 Q4 c( J7 T4 s$ ?2 W6 A
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。& o( _' y7 b. O7 y% [
$ E' j0 \% D2 f! k  t2 q* i

; x8 l. y0 V- ~4 Z) w" AModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。5 d0 w! d, _. C7 Q) A) G

: f2 e* b9 u3 `9 Q6 m2 r
: @8 A, v, J3 z) K& q: H官网: https://www.modsecurity.org/) \% X0 O* g# G! q+ T
/ `' g1 h- \5 o% ~3 C

& q# Q, t! {- c/ }$ I2 v- [什么是OWASP CRS
5 B( G7 R: R' c$ ]$ ZOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
! `. n6 K: [$ O; u) v. J0 m; G& V4 X; I& \  X. c  T
$ r% p1 j# J; ]: p" h
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
8 J3 K3 e. Y. y5 k0 H$ N
) |. s0 z. R  m' f+ x$ d
: D* ?6 c2 U' O# A: @% wHTTP Protection(HTTP防御)
) Y( e( ?5 z3 Y3 k+ pHTTP协议和本地定义使用的detectsviolations策略。
% y) |2 G/ T3 `5 Z4 J9 ~2 i% a# x# X' j) K; j

; S) V7 x  v7 I8 ]# M& CReal-time Blacklist Lookups(实时黑名单查询)
! T. A. ~5 R6 x8 B1 C* Q利用第三方IP名单。
. J& t! `: [4 {% ^  f; }& U2 M( I5 M- @; s' b/ }

5 r" X8 C6 d1 s* h7 {* m! eHTTP Denial of Service Protections(HTTP的拒绝服务保护), w+ ]' Q9 f; y4 l7 `4 L8 p- V
防御HTTP的洪水攻击和HTTP Dos攻击。
2 l) f! @  R) k9 o% x
6 ]) v8 A5 \: ]: U+ z1 D# X* y7 e8 ]0 k, c9 d4 L+ e& s
Common Web Attacks Protection(常见的Web攻击防护)
2 h9 X6 C4 m2 Y1 `检测常见的Web应用程序的安全攻击。- H' q1 ~% \3 n# ^* y% R' O. k# s

6 I( M4 H; x) z( W6 C
% d, s6 G/ j# H& qAutomation Detection(自动化检测)
" A3 i; s4 w5 L$ Y4 R检测机器人,爬虫,扫描仪和其他表面恶意活动。
  x$ |8 p( M0 c; v8 |! {
( g# p5 v- u8 V, F& _4 i0 d& _" F
8 e8 J, a0 ~, l. D# B7 t; H' [Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
# D( J; {! ^0 B: y- s3 `检测通过Web应用程序上传的恶意文件。" O- L/ M6 ~# c
" r  Y9 j& _' B3 Q
# Y) O  d6 C6 v7 w; }% ?/ ?, w; `
Tracking Sensitive Data(跟踪敏感数据)
2 R* A( U2 Q- C( [& S信用卡通道的使用,并阻止泄漏。
+ Z/ Y7 `1 w& E3 \: K
& h8 f: y3 ]+ k1 D% o' {
& M2 l& O! T3 H' R* a6 RTrojan Protection(木马防护)! G+ [0 t; d, E/ Y
检测访问木马。
0 b% U! \: E6 I4 x. H$ `
* @& a) Z  Q7 S- W; ~( T( U- y
7 J' s  X0 k. W5 Y! C9 oIdentification of Application Defects(应用程序缺陷的鉴定)
; i* y6 z4 s; I' E* g  W* I检测应用程序的错误配置警报。
- M6 y7 i7 R! b. f* H
2 Q9 E4 a/ X/ ]0 S
. H) ?, ?- @! m' a, XError Detection and Hiding(错误检测和隐藏)
# ^1 W% J% U3 p* y& S" e检测伪装服务器发送错误消息。: P- b9 f! A- j% X1 S
+ E6 T  t6 E* j
- Q" j5 a. Y* P0 m& D* z5 c
安装ModSecurity
: l$ P0 f$ ~: J$ ?5 Q软件基础环境准备) H+ E: T1 j3 y/ D5 C! m' |0 z: h" f
下载对应软件包8 o( F5 [, {: X9 C% Z/ a7 h0 m8 I
$ cd /root# V( r1 \, b) z
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
( L9 r& F; I& O- |- ]$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz' E, m9 t- H4 ?3 {: R
安装Nginx和ModSecurity依赖包2 s* v$ X, z8 m  A6 B
Centos/RHEL
4 q4 @8 K. c0 R6 }$ p2 G! a" w$ J! h" T7 r; e  t
1 ^: g. K  F" o% Y# A- |. [
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel, j6 Q( S3 ^2 @  p
Ubuntu/Debian
4 O( X: H9 W- E/ a# r  m8 x4 q0 D3 A( j( @

. J8 A4 M) E5 ?& x! A3 o$ L$ 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; x9 b+ A3 m$ Q' w' Q
编译安装ModSecurity
3 R+ s, y! Y$ ~( C3 Z! D# S# ]8 ENginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
+ b, h( L5 \3 p) C% n, I7 s
/ S5 H# ^1 Z; J- c) n+ ~/ v* x) J$ m1 \' w
方法一:编译为Nginx静态模块
, j5 k/ E# A" t5 S: ~* f( D+ r  K. L1 R4 c$ B- O6 {

$ n& e) ^2 r/ ?9 D  N. p( c编译为独立模块(modsecurity-2.9.1)
5 a4 Q: b1 z0 C$ p' g# D6 j' ~$ tar xzvf modsecurity-2.9.1.tar.gz
* d0 i* {# v  ~, r1 t- I4 L$ cd modsecurity-2.9.1/
4 Z+ [; C/ x) @1 R1 C" `9 W$ ./autogen.sh; I3 @& O$ O, U! e9 k) c
$ ./configure --enable-standalone-module --disable-mlogc" i/ z% [' W" _9 k" b
$ make; s4 [. G  [$ g3 ]& Q# g$ N7 n
编译安装Nginx并添加ModSecurity模块0 }/ n: Y. Y/ e4 i. d
$ tar xzvf nginx-1.9.2.tar.gz
, ~: v0 T$ M& p& W! f( j$ cd nginx-1.9.2) s7 ~  s; G8 A5 N1 L8 h
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/+ ]5 A, r$ C) Q
$ make && make install; K7 ?. h, U6 E4 @- s
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
1 y: V! T* F* p% _* v4 Z0 g6 c* v( c7 W9 f9 H

) W8 L; g- [' d+ ~" e+ Z. ~编译LibModSecurity(modsecurity-3.0)- k5 N" }: C$ F! W
$ cd /root
) `6 O. ?3 F  g. e  T$ git clone https://github.com/SpiderLabs/ModSecurity/ ?0 r$ z. Z: x2 s( b# x
$ cd ModSecurity# k/ x! |. E& \  p2 u5 f) q
$ git checkout -b v3/master origin/v3/master
; c$ I5 s: n% W9 s. G$ sh build.sh- y$ U, |$ M7 x* \. G
$ git submodule init
, a: U) v6 H7 h7 j' U$ git submodule update3 l  K" j) a8 P; k. o0 K' m
$ ./configure
' k, P5 x- b% _4 F5 ]8 V$ make6 o' H3 R8 g) o! A2 b/ ~9 h$ R9 G
$ make install" h; h. p& h' Y2 t+ @7 y* ]7 c
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。! {- _7 s* `: r+ K9 w0 Y

$ N) O- h, I6 _) o
, ~4 D, r0 V: y  L2 Q; ?$ ls /usr/local/modsecurity/lib. b, P$ l* J5 z# Y
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0) `* U' `/ R3 X8 \; P3 _# L; |. @$ n
编译安装Nginx并添加ModSecurity-Nginx Connector模块$ j6 N/ v; f# L  D4 Z0 S
使用ModSecurity-Nginx模块来连接LibModSecurity
% V: b: d: Z) j- f4 C* }3 f) J* x+ F- ]) m  X3 ?7 `  L
" ~9 @4 K  Q2 U9 ^! g
$ cd /root5 ^+ E9 A/ {8 o: B& N$ W" q
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx" }3 v' I  T1 \' t  I
$ tar xzvf nginx-1.9.2.tar.gz. C" z0 |3 W1 ~0 ?
$ cd nginx-1.9.2/ O4 W5 }1 s: s5 n: O
$ ./configure --add-module=/root/modsecurity-nginx
! m# V5 b4 `2 S' I$ _$ make
& r3 ?; J9 f6 L) {. B& k$ make && make install& v4 f& g) T& Y% `, y) n9 i6 k
添加OWASP规则
5 O- I+ k) D# t( {7 mModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
3 K8 w$ Q2 m" u+ c: W( \; I2 S' ]8 ?* h- p- A2 p
! ?* o) Q! T1 F) j
下载OWASP规则并生成配置文件3 X% T( q2 W; E( i* w
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git9 y+ f: Q0 _) q! q
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
5 D8 L" p- ~4 N7 s) U$ cd /usr/local/nginx/conf/owasp-modsecurity-crs$ e! c/ U% |! B" i
$ cp crs-setup.conf.example  crs-setup.conf
; G$ g8 `- Z' Z% M6 b' q6 y6 u& T5 I配置OWASP规则
6 f- r) v4 a, A- c$ v6 V$ l编辑crs-setup.conf文件
) k" c6 g4 p" ]5 x1 C
2 t8 m8 W- l" ?4 Y2 n" D
$ [+ O8 l& m+ y$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf+ {& p# f& o7 A4 a; e$ ?1 R
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
. q) V6 R! R  a' 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  Q" Y' {# y) _5 H" X. W& M$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
% e3 o1 [7 A3 f0 R& n8 H: \5 R默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
5 ~8 O1 [2 @8 }4 L9 o+ m: V# d
. M% {, f( U' \) d
$ t0 h5 |% [+ ]4 t( y启用ModSecurity模块和CRS规则$ X5 h; G5 v4 w
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。9 }$ e$ o: o. H- N) X$ S9 x
+ U2 b7 ~) Y) }

. ]8 I4 V8 v8 p/ v  Lmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。. i" g, t* ]1 e/ U
# R, U$ @$ n  e

3 A& W/ L1 e/ v( d$ cd /root/modsecurity-2.9.1/( C% {% ]# c5 K$ m! B# Q& {$ E. }
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  9 u5 m; \+ X) U8 v
$ cp unicode.mapping  /usr/local/nginx/conf/) x, m% u9 g0 i7 r6 i; a2 A
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
5 T; \6 o" R' O* l. a" r8 B& y; A% m3 N$ i( i

8 N% _$ A' ^; {5 P- w$ vim /usr/local/nginx/conf/modsecurity.conf. ^1 D8 B3 U9 A4 Y
SecRuleEngine On* _- A3 ~, ~: ^) ]+ O1 y
ModSecurity中几个常用配置说明:& F7 V! F. n& R9 C3 T' @
5 f8 y$ L$ @- E+ j6 ?

" i) D% U# n1 [  P" {1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
, \: z' K- P5 F5 v) \2 a9 h# }8 I& O; f; U. @; e2 f3 p

8 c- H" _8 b2 a# ^2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。, j0 L- q; a0 o% c4 O+ a3 v
+ \/ p* J: v; v  h

4 z. r, Q) @: w4 j! `; a3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
% F& n- I0 F: Y+ I# u' d: j$ v
/ i; S  i: R7 r+ r* S: I, U. Y& e" p3 Q9 B( M# m) z# N& Y7 R
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。3 b. F( F" G" _$ j+ ?, ~

" p% T& ~5 @( S% u+ C5 h  Q* y5 l& {# U6 T" |
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
, F6 h/ A) H% w4 K# ~  `, ?. G- M. M* T2 D: U; [

. w+ U$ _1 d8 O3 k% `, r3.x版本CRS; h& c$ a% m  S) Z: _* Y. w  w
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
2 d3 a$ @5 _$ R% P- H# 生成例外排除请求的配置文件; e7 ^7 |" b" x# [' U
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
6 e* f4 ?' H1 J$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
/ d7 m6 f* F! J4 q) n3 |* `; N5 l$ cp rules/*.data /usr/local/nginx/conf
) H$ i9 L. S9 e4 W5 M为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。$ H; `: ^$ M& c3 h/ {& e5 ?

7 T1 c. E% N' b8 Z' y1 h9 T" z
4 L! Q9 h* f! y9 u$ vim /usr/local/nginx/conf/modsec_includes.conf# |4 I3 e: d5 A2 a. R" q( Q9 u
, g1 l7 O! m8 g  Y& e( v6 S5 _( J) g0 ?1 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

8 i& B" E1 ?$ K( f- k. A+ f
0 `9 @# Z' |  Y7 ^2 `: J9 m# |0 J! ~/ ]注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
8 q, K9 [) ~7 ^4 T) T; h7 a0 T1 ?3 o$ R3 N, s5 S6 B. b
# V5 K# D4 Z4 |1 [, k0 q
配置Nginx支持Modsecurity' y% P+ G' q3 p' T. y& g3 K
启用Modsecurity1 ]% n* P/ y' \. _, h; Y
使用静态模块加载的配置方法
) l! P, _! Z1 F) n7 o2 k6 M! @在需要启用Modsecurity的主机的location下面加入下面两行即可:
1 K) H  J9 M9 o' {( l8 Q1 |
4 O* m8 U1 ]* H! f: q/ E! z) s7 C
# m& l7 d" r) M- ?9 gModSecurityEnabled on;
3 r2 E5 n  B8 O( f! GModSecurityConfig modsec_includes.conf;
0 s- R1 {. P& E修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。+ o& w6 p! s' |& p% ^0 s
9 x! o2 H  y  a
. D% Z0 R) f0 U" e* j# n
$ vim /usr/local/nginx/conf/nginx.conf' L6 _, j* A( Q8 @; J5 s
5 k, k9 K- ]$ J! r8 V8 O5 G
- t, F1 u6 H1 i+ H7 O
server {
5 r0 Q& s4 b4 r% D  listen       80;
2 q% _% m  q& N# E( Q  server_name  example.com;
+ |* i- u4 v+ V, y) a
& d' _2 |/ S: M5 u# u5 q" W; r7 }, n3 _' S8 o3 n4 j
  location / {! A! r1 s0 R- a; U) g% w" w
    ModSecurityEnabled on;
% E" b' \5 b, C  M; d7 g0 @  Z    ModSecurityConfig modsec_includes.conf;* b5 t) C$ j+ m' ?' x
    root   html;
- Z+ E6 w" t0 X( I9 n    index  index.html index.htm;- _  r5 R& I5 B: m8 \% Q
  }
3 N! H; d! T+ Q7 F( R}! M& e! z' d. Z7 |* {1 s
使用动态模块加载的配置方法* J/ Z7 Z# U8 g" X" J, ]
在需要启用Modsecurity的主机的location下面加入下面两行即可:
3 v) e; a8 x9 x  v1 F
) l" p" Q, f- R; \/ f7 i, q* Z& g9 p& O- N! G* h
modsecurity on;
3 g/ q3 ^. E& g. vmodsecurity_rules_file modsec_includes.conf;
- a( ]' @7 A# C0 S# Y修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。1 S6 a+ e& B" q# r
) o& w, {+ f$ x) Z' O
6 B% `: N( u3 F, Y8 E
$ vim /usr/local/nginx/conf/nginx.conf; r% U9 Q, Y8 p& A2 Z3 c1 @2 M
9 _1 t$ ^5 {6 f# q+ X1 C
# i7 p- z3 s7 q- g
server {! U) c) N0 s( r4 E$ d
  listen  80;) |2 p) M" s' E9 e( w) Z
  server_name localhost mike.hi-linux.com;
; P( u1 j' }" Z. @7 P  access_log /var/log/nginx/yourdomain.log;8 L2 ?: v8 e: a

5 z- F4 E. q' H2 l# M/ O2 p$ H# y: a! [$ L( N- Q) d: @4 Z1 W
  location / {$ q. K4 P' t8 \3 A* r5 C
$ Z% ~  V0 H, [, Z3 @9 E

+ j/ t$ B# Q9 ?% B9 e# P8 R  modsecurity on;! ], \0 |2 S5 Y. U; r9 }! G
  modsecurity_rules_file modsec_includes.conf;
5 ^5 X6 G7 b9 T; l* W  V  root   html;
# i$ B3 m) U& a8 J! w. }8 Z  index  index.html index.htm;0 J+ @. c, a! J7 ~& m* v
}
# |8 E1 K( J9 e5 E9 s}- z: j5 C7 C9 N
验证Nginx配置文件
6 @1 `2 T8 i& _  }0 \! U$ /usr/local/nginx/sbin/nginx -t0 a( N" J9 t6 g8 j7 V4 p
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok/ x  F" L$ n0 d) r% F
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
+ j0 |, s, j" T1 t启动Nginx
0 f, t% D) e5 f8 _- E9 g$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# ?+ u/ }/ N( c* c) B( B, i3 H6 W& G' p) U5 v4 u% p) y* B/ f

测试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.log

Modsecurity主要是规则验证(验证已知漏洞),Nginx下还有另一个功能强大的WAF模块Naxsi。Naxsi最大特点是可以设置学习模式,抓取您的网站产生必要的白名单,以避免误报!Naxsi不依赖于预先定义的签名,Naxsi能够战胜更多复杂/未知/混淆的攻击模式。


/ @( ^2 m- V, K0 S* t: }0 }* P

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|第一站论坛 ( 蜀ICP备06004864号-6 )

GMT+8, 2026-5-25 18:09 , Processed in 0.075675 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表