找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11364|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
+ h4 [4 K) ]: ^
0 T; N0 B4 r9 i% j! v" h" {- p* p& A
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
: d) j1 U" g' s  Y6 v
$ @% X/ }4 V. p- O8 L; P
: p7 A9 z0 U; g: @什么是ModSecurity
; g; z1 l. s6 M5 W. c% d4 qModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
; G6 h5 b! @& w; o0 K( {0 M! [! k* Z! x, D3 m7 Z
8 S$ _' m& T+ p  a" R4 z. A( y0 {
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。; h9 y" R) J6 [' {7 x

1 e2 |3 j; K0 Y% p
; W9 N) d3 c! a. M9 \, RModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。- Q: ^3 {$ J  s
& B( f8 x6 O/ e' S$ [/ c
) R2 m) G8 Z% n% y; ?
官网: https://www.modsecurity.org/
5 S+ c! q4 L* x' r" n' H0 n5 Q0 ?4 q; Z
! C0 c! P' }$ v1 ?2 n
什么是OWASP CRS8 L9 R; \$ h1 p6 ]  L
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。' H& Z' m% f' w. H* B+ [

! b! R; F; n: N3 p+ l: ]6 B. [: D1 |# s/ w; Y8 W( V
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。& w& m' i: U. p! ?/ G5 \

# I7 i6 R: G9 ~; G
% W/ A: l' t% i+ e1 x* l* }- lHTTP Protection(HTTP防御)8 t0 B; k9 C1 t7 Y
HTTP协议和本地定义使用的detectsviolations策略。' N& [0 s2 V) ~- G0 r, y
! C- D$ q9 D- l. T# Q

) \3 q1 G4 z, xReal-time Blacklist Lookups(实时黑名单查询)
' G0 h+ J! W$ a# y8 Z! ^* a( \利用第三方IP名单。
8 C" ]( e4 R9 z' y
/ X) J6 U) U- T2 |+ m* D9 I! ]; x8 {. _4 S7 o. ]8 Q
HTTP Denial of Service Protections(HTTP的拒绝服务保护)" H7 n3 i8 f8 h5 L4 ?% G# s$ {3 A
防御HTTP的洪水攻击和HTTP Dos攻击。
- L7 v: h2 E6 J  k* P6 {
3 u+ q5 h( x& z% r6 W7 Z; a: ]  A0 u! b: z  l
Common Web Attacks Protection(常见的Web攻击防护)
0 l4 m/ ?9 h+ u6 X9 Q检测常见的Web应用程序的安全攻击。
4 ]. m9 J! Y9 ?: Q' l" G: G- w+ V# s+ q$ d
) |  }' J- o: a, A
Automation Detection(自动化检测)
: g! {; V" u- A. @6 a5 I检测机器人,爬虫,扫描仪和其他表面恶意活动。- Q. X% \+ M, X
6 R0 {1 F( N/ t7 J0 d8 b4 V
# K$ e4 b& _% Y6 A- r0 \% z/ M
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)( N; o9 D6 }' E
检测通过Web应用程序上传的恶意文件。: @8 s% z% M1 r
' N1 N0 ~; i& [6 \# U6 f% c
& t4 V; g6 C* G' \5 K! @. l
Tracking Sensitive Data(跟踪敏感数据)
) D* u7 r9 c# W7 q% A0 A信用卡通道的使用,并阻止泄漏。
* j) v% B( L: n1 T* U2 D5 T
9 O/ q" N$ {+ x/ b4 o5 @
1 Y1 L1 w" y" w7 T" lTrojan Protection(木马防护)
) O* k5 Q3 H3 y9 @: B. M7 d检测访问木马。/ w2 B8 F& g# T5 m0 u- U
% Q4 G  E+ E; O6 J% ^3 c  I
9 v3 _4 X) k& f0 b, h
Identification of Application Defects(应用程序缺陷的鉴定); ?3 K" H/ h: z# z0 r; Z2 k1 y
检测应用程序的错误配置警报。
2 K* m/ s0 x& [! P- S
2 j& R, g* F# r1 c: x  u4 |6 y( Q2 r' n1 d( J, t+ ?0 {* ~' E1 o
Error Detection and Hiding(错误检测和隐藏)1 c9 V2 b' @( K$ M
检测伪装服务器发送错误消息。4 f5 d+ d1 A! A1 s0 Y
% T3 C4 K: m! `2 b" ~" R- M

  m# |/ X/ H+ M. y% k安装ModSecurity
' r  D8 {9 b; a" B+ M  @. q% b软件基础环境准备
) ?/ ]8 L1 |0 I下载对应软件包* N1 m. |4 O9 T1 D7 b' ^7 P* ?
$ cd /root# R3 q; t0 C7 V& u5 n
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
. B5 h, G" F1 ~$ |$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
3 X! ~  P2 p, X! M" f9 U+ V安装Nginx和ModSecurity依赖包! q4 G* X6 ?$ }: X. j
Centos/RHEL: d/ [& E& J1 c1 C

- \4 t+ k# |2 K3 i6 Q! O9 @
+ J% t4 \2 `; W) m$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
5 I7 v( P9 o0 F% _/ cUbuntu/Debian
. m1 [& T& g: g3 r- i0 n  Q) ~( q9 @8 a: u. i+ X; t* Y, ~

& g1 m2 h, h$ r( r$ 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
5 A# M& X) b8 Z; U, K9 g编译安装ModSecurity. U2 T1 |8 t7 q: B( I; r" f' ]
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
- k+ U) `* ]) ~9 z( _
# u  F. @( I8 v7 O, M0 S5 T. e+ Y, ~6 d( P% u0 x9 C  m- Y! K: x2 n
方法一:编译为Nginx静态模块+ _* {0 \" H3 J% g6 K

6 Y' d) k  F  g  M% Z9 A# R1 d; R" x) m; T% v: M' Q( K: p' \" E
编译为独立模块(modsecurity-2.9.1)) }3 v/ u0 K: Y7 m3 h  o
$ tar xzvf modsecurity-2.9.1.tar.gz% m; `9 W  k+ a, q6 l
$ cd modsecurity-2.9.1/2 a6 T% {$ b; x& ~1 f$ r/ m; u
$ ./autogen.sh8 a$ e0 I+ x$ C! _3 s
$ ./configure --enable-standalone-module --disable-mlogc1 f) V/ ~, e$ m9 k
$ make9 N) x' l0 h# y( ^2 g, d* V3 L
编译安装Nginx并添加ModSecurity模块/ n. s" ~. S2 O- a5 J1 \
$ tar xzvf nginx-1.9.2.tar.gz, g% L& }4 J3 y
$ cd nginx-1.9.2
( u' j0 u. b; c* l8 U& @9 e4 E! [$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/  H" W$ C( `- ^
$ make && make install( y6 B2 z" ]" L7 V) w
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
% F& S7 r. W, k. E  s/ x! B) `2 R5 ^9 }1 g0 \; ~0 N7 f- c

! E, s9 V- N$ n  Q编译LibModSecurity(modsecurity-3.0): L, i, S. w: G
$ cd /root" X& o" t9 t2 {) N% J. v- J# f3 i
$ git clone https://github.com/SpiderLabs/ModSecurity
0 b# c* `# I' c8 a& A- O* y$ cd ModSecurity, W- F; k8 Q% `8 Y  m$ W
$ git checkout -b v3/master origin/v3/master  x. b: n! W0 v  n: x$ s2 e  `
$ sh build.sh: _, g( z; g/ \$ P1 ?5 n$ }
$ git submodule init) |/ e3 G9 I0 V9 g. j7 X! x! j+ V0 D, L) F
$ git submodule update% {3 F' Y2 S# `! I  A3 T/ h
$ ./configure
" p. C; V' \& u5 L" l" s% d1 H7 E$ make
% }1 V( Q6 ]- V' R1 k$ make install
/ T4 `' [' u: F& Y! x7 uLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。3 G* g- d! K1 d4 r
$ \8 {! f+ Z* X8 e: j1 J
/ Y+ U1 }4 {6 |1 n) y6 C) J+ l
$ ls /usr/local/modsecurity/lib# V8 K' G3 ^; U0 z/ q! U
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0/ Q$ {5 O$ i. Q
编译安装Nginx并添加ModSecurity-Nginx Connector模块
# I6 c. T; U& M" d8 p* k% x* C8 \使用ModSecurity-Nginx模块来连接LibModSecurity& N3 Y( y1 P9 a, e9 P$ A
& t5 i5 x1 G- D, Y; D7 Y

# a3 x% E5 ?% q- c9 k! D0 u5 X2 b) t2 J$ cd /root! ?2 W3 s6 \( T1 d* Y- _0 R
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx' A; `1 J0 I1 b- Q  V5 }. t
$ tar xzvf nginx-1.9.2.tar.gz5 _7 t" J+ a7 |. h
$ cd nginx-1.9.2
; g0 b: m; o0 |1 X, G% `8 r) A1 ~" D3 F$ ./configure --add-module=/root/modsecurity-nginx4 J) _- e, h6 n) p8 k/ p
$ make
/ w* y! t% M- h- c4 a8 B, k$ make && make install2 @/ f8 c# R  j: Z" O  Q
添加OWASP规则
, [2 y. w& i( u8 Y3 n8 Z; ~ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
' H: N+ T/ R. ~& F# O! `% N: d+ P  E
8 N+ u* {4 c. H5 E: d
下载OWASP规则并生成配置文件
: l% |. {) V& R" ^$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
/ }' j( E* j8 Q$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
' e" F! ]/ P  B6 |$ cd /usr/local/nginx/conf/owasp-modsecurity-crs" |$ E" S6 h( |3 B5 Y0 u# F
$ cp crs-setup.conf.example  crs-setup.conf8 c0 }2 @9 I( v, v
配置OWASP规则
1 k' U. |3 _: `; a编辑crs-setup.conf文件3 T! Z; A: l/ N6 B2 ^# s
; ~5 U/ _9 v& B+ h: C

/ o; ?+ ~" ~2 P" R$ _. J$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf# p) ]! b1 ~5 ]* Y! s" P5 m
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf( K3 R1 p  \; T7 Q* A: x; {- q
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf( K: y- j4 C/ L
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
( @+ ?% x/ a1 P  C: H0 P* Z$ {" e: i默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
$ g, m7 `5 t/ f7 C% z/ W1 Y
$ G1 {8 B( p. x: v! [' c
0 G5 j4 p7 o& ]6 H: L' N启用ModSecurity模块和CRS规则
  w* O" |. \8 m! R复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。2 h3 \7 c5 _( b9 y9 b& ]  J5 u
4 ?* L$ O( W  K( v+ G6 h

* o' ?( W4 f+ I$ s1 r' \0 w$ rmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
- q! h% K) D1 L% k9 W/ \
0 F( o) j. y0 p# q! `$ z4 v3 p  a0 i+ E8 L2 x9 {# u( k- E0 s
$ cd /root/modsecurity-2.9.1/
3 w0 d8 X, [. x7 T: e: G- X$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  % o6 a5 x8 t1 p+ W  A, x2 j
$ cp unicode.mapping  /usr/local/nginx/conf/( s4 _2 y# F1 X3 I% X7 i
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
9 n/ [( A  O9 z/ r7 [
8 @1 t, }+ `1 |& c
8 M- }1 @+ \, D, L: {$ vim /usr/local/nginx/conf/modsecurity.conf
: r$ ^2 _) o* |  N% w: VSecRuleEngine On# G) \  o) k8 }* h0 U2 s
ModSecurity中几个常用配置说明:
8 W: ^. m3 j9 T3 p. Z1 f0 v: D& F, B  ?: T

# s2 ?# b6 I) @9 p; \1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。' W. _# O2 @& z0 ?2 y

. f, u4 V4 I- n
* W6 a% a. C7 h0 G2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
5 u: C& A8 F' d# Q$ X/ i1 V2 K. M: ^- F2 L! \& i; G9 J
: H2 U. l- J4 ?- ^# _6 b! {( h2 a
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。' I+ r: ]* o( T8 }' j
$ t5 u8 X: }3 `  T9 u: W
% A! ?3 I6 C$ [2 Z( y* `  }
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
# }$ C/ ^5 @1 R! m0 f. ^$ Y1 t% [3 R1 J

. Y% C" y7 l& a" @3 C在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
/ x3 h+ u5 ]( H% |2 ~% F$ {' s0 j' ], p5 i+ T# w9 q

: A; _1 n% P" ?+ J3.x版本CRS/ N- b1 V7 n2 h1 J7 R4 O( y* |& V! @
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
& F, t% h9 B" C8 q1 X( `# 生成例外排除请求的配置文件
( j+ l5 M; F  N8 U$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
- g6 x0 S0 m2 s+ m8 Z1 a* |7 L$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf3 N8 H3 q0 ?5 V7 q4 |
$ cp rules/*.data /usr/local/nginx/conf
+ A5 W6 \+ Z4 V; B* q为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。2 b8 l$ r5 ?% A- o# N% @4 f
2 I" B) Y$ z9 b# F& w# v

6 i. `9 E8 V, ]7 U4 L: U$ vim /usr/local/nginx/conf/modsec_includes.conf
6 i) {# T+ Y  V0 ]9 l# R2 \- |5 j; _" M/ E
[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

0 K7 `) j# n& S3 J/ e8 F
# t, T" Y' p5 j. l$ p0 e. `注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。; i1 Z7 j3 V' r

2 p  \/ y$ e( n5 w3 J0 k) [: I. j! Q, [7 [  I% c# G  k' ^
配置Nginx支持Modsecurity
8 G5 y6 C, L( \# ^启用Modsecurity
- b0 P9 u( r% U( z使用静态模块加载的配置方法( A: n% \, `) q0 X9 b0 D8 |
在需要启用Modsecurity的主机的location下面加入下面两行即可:. q; _+ q0 W# n
5 R5 o* ~' Z2 ]

- b: u3 g$ U6 A# u* vModSecurityEnabled on;
1 j$ _' \# N1 Y  }ModSecurityConfig modsec_includes.conf;
5 T2 {( ^1 K7 ?$ F; P- T% V/ U修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
3 v0 b/ R( \) `/ w; }( g2 a! q0 L  T4 r+ Z0 d

; t7 P/ k9 t6 c$ vim /usr/local/nginx/conf/nginx.conf
6 u: N4 e6 x* [5 x" n4 G+ `# T8 q/ ^# @9 g1 C6 R

8 ~1 n/ {" z/ {1 \2 m% Rserver {
  z# q6 Z$ B1 R  listen       80;
+ @" K% a6 i: P  server_name  example.com;9 `+ ~, B, Q8 @2 _+ W

& b' }9 N. J: Y: V2 Q% j; p4 d3 @7 s6 z( a0 n- U4 S0 \2 V% q9 k4 S3 t
  location / {3 X, {+ @3 F- P/ J
    ModSecurityEnabled on;/ L9 m* F; e) T5 p# A
    ModSecurityConfig modsec_includes.conf;
0 A! V6 w" J" y. n5 G9 t6 f    root   html;/ p# V/ J- p2 v3 j0 W2 K
    index  index.html index.htm;2 c0 K+ A( c8 ]# J  ~, `3 ~( E  i* h
  }  {( S) b  b1 K6 [+ o
}: x' {, |1 y% i& B, O0 T2 S6 L
使用动态模块加载的配置方法  r- p) j/ n+ E- t5 }0 F
在需要启用Modsecurity的主机的location下面加入下面两行即可:
. q' U& p. ]0 M
7 D$ d  d, R  M! M& {! x" r7 q: k5 T8 h9 S
modsecurity on;' }0 w: d5 d. n
modsecurity_rules_file modsec_includes.conf;4 h# y% b# a# p  ~
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
& G, S! ?4 q& `7 [/ a! W; A
; H8 J  G1 Z% d+ x1 O9 a0 @2 o5 h5 N/ \- C3 T
$ vim /usr/local/nginx/conf/nginx.conf- P0 V8 K  A5 [+ Y1 o6 ^( ?; h. J" t
% D8 U: n/ ^& n1 Q
5 G# v8 R' P. h  |
server {1 V3 d; l1 V, }6 }6 U2 K% m
  listen  80;
5 M% x/ _0 R1 {  server_name localhost mike.hi-linux.com;
7 O6 E, T" T0 P  access_log /var/log/nginx/yourdomain.log;+ U! E- M) s! f  ~* Z

8 I* R$ u# s, Z- a4 r' r: V4 H7 \( c! O3 y6 D
  location / {7 U" I0 R: K1 T- w' k0 K, o
& K! l+ d0 g: j

8 L& e8 a! z8 P3 ~  modsecurity on;
& d& n* ]6 L0 k, ~  modsecurity_rules_file modsec_includes.conf;
3 q; }8 h8 c/ Y( }  root   html;* h. h# X* I4 Z
  index  index.html index.htm;$ _3 l) z2 z9 b  P" t
}! p6 O" L; n, G$ m% _
}2 Q" Z$ W# h# b+ p* u7 P
验证Nginx配置文件$ b  Z0 ^5 A( }! A% f" h
$ /usr/local/nginx/sbin/nginx -t
) V6 r+ ~5 K- P* O/ lnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
% i  G1 p6 W( g1 t/ |nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
* }0 Q6 A' Z8 e9 M启动Nginx# r, }3 O( f2 W- T4 X7 R
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf; [+ M6 i! j& d# T

/ N# {  F' g2 R9 I4 S" y2 [) d) K4 K

测试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能够战胜更多复杂/未知/混淆的攻击模式。

% `; q& W, |+ s2 \, Z6 U, |6 t

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-6-3 11:13 , Processed in 0.076007 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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