找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10322|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
4 D. F. g4 ^0 t* L0 \# v: C2 a. j. I" v: f+ E0 U' {0 M! l
, ~2 h+ E# `4 [) Y% J3 W" r
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
/ F, s9 Z4 g0 S1 K/ a" p2 s/ l
- c, o, j1 h4 m+ j! ]8 V  J8 {  o3 h+ l" \
什么是ModSecurity% c  y: t* t( {1 r0 m& X- x
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
* t* f; f9 H& ^' i4 m, E
9 g. u' a  }6 L$ C, Q; w$ k- R
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。" u: [- s! C+ {6 O* M+ g5 R
  H5 D1 G/ I- {

0 A* h9 Y* |- u: f- E( m, `0 BModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。+ n- A) I5 D) \$ E

& w* v, O9 a- H: a7 a! `; F, n* D
8 B% z& ?. j7 V官网: https://www.modsecurity.org/, P. d3 I! o' `: o+ B' W5 O+ v

" A, t* L# N6 P5 A/ J% u5 l9 @, t; I! S9 ]. J  K$ T7 A
什么是OWASP CRS
; B7 I" A4 s9 e* Y! j; j0 `OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。$ u% x4 k* L/ j- _. l7 i6 f" I+ M

* Y& \" \: G8 |9 d1 s+ n" L" Y) M* B. f! C4 T
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。1 L& N/ `! q; f; }+ z0 ~

) e. ]5 D. m, I/ ?; J" t/ V
: O; m* E9 [& H* R& {! HHTTP Protection(HTTP防御)) |) G+ j8 u" `& \2 |
HTTP协议和本地定义使用的detectsviolations策略。$ g; x. T8 t- R3 \3 z  h

) s9 @" v- H! l2 w
+ O7 N" I) g* d6 C: hReal-time Blacklist Lookups(实时黑名单查询)
  M0 s& x3 Z( W) L5 d利用第三方IP名单。( ?$ n" {: A( o* h  N7 ~% s

$ V* M4 j' \" S$ q0 K: i
! A% S2 X' H: jHTTP Denial of Service Protections(HTTP的拒绝服务保护)
, b' Z) d5 w  N$ m; x# _7 S' J防御HTTP的洪水攻击和HTTP Dos攻击。$ N' Z8 h/ m7 I* Q8 o! d" `* @
+ P+ ^) N4 d& P: m) D/ v/ X2 h

& @2 a* |1 g% wCommon Web Attacks Protection(常见的Web攻击防护)( J+ Z$ `1 ?# L
检测常见的Web应用程序的安全攻击。
8 U( O  U% J8 I2 v$ a' m3 V) D
/ [4 c6 c: k1 E6 q) H
. q4 I! o, [* x$ BAutomation Detection(自动化检测)
. W, f; l5 Y; w1 V检测机器人,爬虫,扫描仪和其他表面恶意活动。
& X9 n/ P' o: U. C! J7 m9 R6 j1 H3 A
. i/ G, X* Q/ y+ s, x: ?7 T
: Y) b4 ^& k8 p* Q  N1 [8 dIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)9 B% m) C4 U* V
检测通过Web应用程序上传的恶意文件。0 {& y4 J3 R$ t; z
+ ^7 G. d3 e' Y6 W* D

1 {# a9 S) D# @# p1 o0 sTracking Sensitive Data(跟踪敏感数据)4 R$ c6 l" w' i* x
信用卡通道的使用,并阻止泄漏。
2 Q( b% |' F7 h+ p
7 [- p5 K- T! G  i/ O# d0 y/ i  u2 s  k% v1 t2 A* {# G* x  P' l( z
Trojan Protection(木马防护)
) G* s+ u: r' b! ^检测访问木马。3 {! B& ^* U$ R* J: z. l; }9 `

6 L7 e1 e3 C* c( T7 Z1 H7 z+ r! B( J
( X$ R1 L: u4 @/ f5 j: X: B$ tIdentification of Application Defects(应用程序缺陷的鉴定)1 U4 n5 q' D" v
检测应用程序的错误配置警报。
+ _- ], z% z5 [, W1 O# T( t( L1 a$ J" ^% f; l# S
1 m: d8 |. `5 C! j% Z1 L/ U5 q
Error Detection and Hiding(错误检测和隐藏)
& ~& j6 Z# Y* i. _检测伪装服务器发送错误消息。
3 t: O# @; U" U7 n1 ]+ \8 B+ _6 i9 @
, `+ k1 b% O* X7 l  z! g# p4 x
安装ModSecurity
  a# H6 q5 H& h8 X9 ~2 J( o% K! j软件基础环境准备! N2 |+ N0 `6 i8 n( _
下载对应软件包
7 S7 p+ Y# G) x! J1 K$ cd /root
" p$ S2 P+ l$ [, F" C$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'2 h2 v: l% m8 ^+ h8 `
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
! w" |9 k! v. S' x5 j+ w1 A安装Nginx和ModSecurity依赖包
) q; t$ v. U2 K  \) l3 k5 x" RCentos/RHEL) Z8 w  Q2 Q, N% I! @3 Z, ]2 I
. x' K* k  G( _* J' b& o+ |' v

3 j+ l/ {2 Q, J* O0 A; R* s/ {$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel) C( T3 o9 |  i9 t0 G6 N
Ubuntu/Debian
; C' R3 D/ E/ s# e/ V& j9 |! w" k, y0 L

8 h) m' r, A0 d' @" i3 E0 i/ W$ 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 A& w2 T4 e( D" S编译安装ModSecurity
) i' @% y- S# hNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。- k; C4 m7 T9 I4 v
' d3 n7 y, F: z" }4 p) ^
6 r0 X8 i# J# {8 H, L8 u# [  L
方法一:编译为Nginx静态模块
& e% O8 U# w' o% D' o  i) Q% V( b- s- n- {% P( b
% _8 Z/ b9 H. q: p. o6 W
编译为独立模块(modsecurity-2.9.1)3 b) J$ g, Y5 Y+ S1 U' f6 Y
$ tar xzvf modsecurity-2.9.1.tar.gz
# m4 P! W9 S  \. a: q8 R2 B% M$ cd modsecurity-2.9.1/
5 I: K1 `2 a8 v7 k0 @0 V$ ./autogen.sh9 i3 `+ {1 W8 D& V; y1 J
$ ./configure --enable-standalone-module --disable-mlogc
% l; E4 L& v- F. |6 w, {* y$ make
0 `: z* d# ^4 n- }5 J) j0 \6 m7 ?4 O编译安装Nginx并添加ModSecurity模块4 l( b" z+ Y/ D
$ tar xzvf nginx-1.9.2.tar.gz) ~8 K+ l0 c/ \1 z# |/ X9 ]
$ cd nginx-1.9.27 r- v) u" O% u& l
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
( Z+ w) P5 M, w" F) P, m# P$ make && make install; r3 D, P; G+ f! p/ |+ a
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
+ p5 A; F  \( V+ h3 |$ n: Y
" u2 ~/ ^2 n9 Q2 d. |. R0 l7 D5 o
) D! h; X! R& B编译LibModSecurity(modsecurity-3.0)
4 B! i. {5 B; Q2 _$ cd /root
, e" F6 g3 {( c+ o& ?$ git clone https://github.com/SpiderLabs/ModSecurity- Q# r+ N0 D$ d( I0 F: l$ u) k
$ cd ModSecurity
  o: j/ G6 y/ t  m1 d* R$ git checkout -b v3/master origin/v3/master& k( M3 `5 G$ j8 Q3 ^8 p
$ sh build.sh
6 Y+ w; ?4 q$ K$ git submodule init" E$ J$ n1 g3 Z1 h5 y' |+ _
$ git submodule update# g1 C/ |- V7 O" ]
$ ./configure
, ^8 W: G" {9 \) K2 S$ make
; a6 C2 v6 I) Y' X; h  i$ make install4 A8 {, ~. e8 z) X* h/ ?; U
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
; [3 ?, ~3 K& y3 F$ g  V/ X; v. ?5 e! R( y/ K
" `5 @0 _& F$ o. L1 L
$ ls /usr/local/modsecurity/lib
: N" Z- P! F% |8 F. _; }libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0" g$ ?3 u( A) t# U; ~  V2 S
编译安装Nginx并添加ModSecurity-Nginx Connector模块
9 s" q, Z) i6 y( g使用ModSecurity-Nginx模块来连接LibModSecurity
' F% q6 ]( Z! P  {$ Z: Y/ w  Y! f. K0 X3 D/ v# _. K

- k8 L; p; u/ m4 |( F$ cd /root) _/ T5 p0 d6 Q0 R
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
+ K6 Y! V. o* W! r: A6 K; f: e5 b$ tar xzvf nginx-1.9.2.tar.gz
# p, }* L$ [! q# L$ cd nginx-1.9.2. ^, p! S; f1 y9 s/ L
$ ./configure --add-module=/root/modsecurity-nginx! ]" ], k, c6 C4 O
$ make7 w% ?; t2 o7 N' A' m& U
$ make && make install
; {3 n6 D. J. M! ^% y添加OWASP规则5 g5 d  L" O5 d6 Z. B8 b
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
0 M/ C# p6 S6 K; t6 M& E. h) P& H! e; M; i# d: ~9 a

( A( ~% M' y5 z! k! I- E5 P下载OWASP规则并生成配置文件# ]. o- X' m8 C( H0 y' R9 K% x
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git5 O$ b  b+ b6 p+ J- X8 w9 E
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf// p% }* J! t# y5 {
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
8 D' y! f2 U5 h0 m  D- q. k: y) a$ cp crs-setup.conf.example  crs-setup.conf
  y8 u4 i( _" ]  B配置OWASP规则+ |6 E" K8 |; \6 d& s
编辑crs-setup.conf文件
! Q5 A" b# [+ h, k( ~( [1 j. E9 h! J/ s+ `# n
* |0 l2 J& y' ?. J$ i. [
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
; M$ y! t; ?; ]% }6 R' T$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
& o6 n1 S$ F2 V3 O) l( _6 f9 L$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf9 g  ]0 H0 c8 U# M5 P
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
, C8 w5 j$ D6 x0 S% `默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。2 S+ L* c# g: W% E

' d  h, d( N; ^$ Z3 f7 a
5 l! k( m( B9 e# p8 b9 ]5 C启用ModSecurity模块和CRS规则
2 T4 n0 `. O. Z6 K9 U, w复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。0 ~8 ?2 f1 w$ A

) R# R2 d+ |: H6 N4 i8 m% F; ]9 m3 X# b; l+ i2 }' I: a
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。0 J& r* Q* H& Z; {! n' G/ n
8 [8 S; q1 c' x& O' _4 D

4 c' [0 P$ U: b* o( q+ k. z$ cd /root/modsecurity-2.9.1/4 i/ z1 p6 W4 }
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  9 \2 R' W- [- H0 s2 d; E
$ cp unicode.mapping  /usr/local/nginx/conf/6 a# T1 H$ m" s9 o6 @- j+ n( U
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。  G1 r  m5 V0 W: W
4 M" E# t5 Z% H: Q' S/ E/ {. l

" k% C6 P0 s4 a% ?: l# K9 E, k& r$ vim /usr/local/nginx/conf/modsecurity.conf
8 x, {" j* x, J; O2 R, d2 bSecRuleEngine On0 W9 E) Z& }4 i5 P3 ^7 G) b4 l
ModSecurity中几个常用配置说明:
6 R& t/ b* @% Y9 B
- `  q5 |2 o  p2 i9 L# G& d2 `. o0 |+ U4 K9 L) C9 c
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
5 ]' j9 R" `9 A) A$ |* p% t- k, q) n2 E4 ?5 R/ v. M

0 J% S+ p8 @; d' q/ }. [/ }2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
9 j9 ]# ]9 S9 z# b! X0 A
' D4 T" \/ h, d4 u1 g
: [6 q* T' x/ r& `3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。- v" W6 Z/ o( Y

; j$ ?2 ]. P0 c1 E" g
7 z: p3 x: n" w& ~7 m3 s+ _, s1 n4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。' c  P; v: G0 g# @) v- d+ Y& v

( R1 b0 h9 U) w/ _9 l' U6 r) a
4 A# h% m$ u$ ?' [在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。' v% D2 i3 i6 A$ O$ j
0 }# |4 D2 s4 V; T; v
& F. _7 g; R9 `( u
3.x版本CRS: f. {! e& z% Z$ _! m
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
" E4 ^! U- R$ ]5 \' U4 n& b# 生成例外排除请求的配置文件  t! u8 Z! l4 Q) e- `
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
9 z' x' r6 Y  @% t8 e" H$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf6 r& Q# R4 F7 \& N4 y4 Q9 A! p- k
$ cp rules/*.data /usr/local/nginx/conf
+ I" G3 D8 X3 N! _& D, s3 I为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
- H: a* b+ w# [$ E  y% @/ F& i" u9 h& _& Z6 B1 t: z/ F/ ]

6 Y/ A* R7 ^+ v- l4 b1 E( p$ vim /usr/local/nginx/conf/modsec_includes.conf. b  N" ?8 `- B
2 q7 m: C, O- z: N) N2 P
[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

$ b$ C* k( R) G0 C" l* d4 M
6 W# j5 k& \6 |+ c' K注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。8 v! ?# v: i1 w4 u, U% a+ Q2 @, Z

" W# e* I; s! T3 K/ e) `
# o8 N: d2 R$ h3 V* t配置Nginx支持Modsecurity8 p  z+ v+ O& K0 P! P7 q( I& j
启用Modsecurity' {$ S  @' \9 V2 y  \9 s
使用静态模块加载的配置方法! n8 H4 Q3 g7 u
在需要启用Modsecurity的主机的location下面加入下面两行即可:& o" N$ R7 F5 @# Y& s3 Q, {
; L: l: h5 w. u' O# T
- U6 w2 S+ X% _0 Y4 s* l& l/ l
ModSecurityEnabled on;
1 X; @8 D/ s. O; K+ n% g0 N( }" NModSecurityConfig modsec_includes.conf;
0 ?/ M- I; U0 u修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。2 N# k- _, w' G' }3 B; ^: B

8 y# ]5 n2 p/ |/ I
! k0 j3 N; D  H( `2 ]$ vim /usr/local/nginx/conf/nginx.conf
) l1 _3 g7 a1 u5 F3 L+ _+ ?9 U
9 o8 X2 y6 c' Q8 h) L0 x5 `$ w- u- E+ Q7 [. X, t3 p
server {* f. s) }4 y* ^7 t0 h/ j) ]
  listen       80;
& e6 i" X  P' N& ^/ v3 F/ k  server_name  example.com;
( T/ o& z  K! H' N+ Z6 x3 b8 p
7 z  @% T, ]" y7 v6 g* c* {' Y) X6 R6 l3 X7 I
  location / {
8 d" q3 ~# a+ N. [( O7 s  p    ModSecurityEnabled on;) i7 r5 g1 p3 t  ?: K$ S* V* |
    ModSecurityConfig modsec_includes.conf;, T' ?: N7 @& p* n6 w9 U
    root   html;
/ n) \8 b& d5 E; ]8 F4 q* S    index  index.html index.htm;, O4 I- l- E& \2 p
  }
$ a+ e0 b3 J# R5 `0 v7 ?8 v/ Y}. C+ w+ R' W1 r4 ?2 j: O6 Z  c
使用动态模块加载的配置方法  y+ X, R7 p& T( o) G
在需要启用Modsecurity的主机的location下面加入下面两行即可:
/ c9 n+ E* _0 v! f9 T, d: f- C: c) ]; ?0 Q/ x

3 D2 c6 |; G/ e3 E. o" d- |$ Z9 r6 ^modsecurity on;
; `# o3 }1 ]5 `  J) h6 @" Z" k: amodsecurity_rules_file modsec_includes.conf;
) c" K4 s# z6 U, M3 u修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
% ~1 v- O6 R  I5 X% E
$ P( |6 t+ }& g% `" x1 |+ K$ d: t
- @" r, q) j3 Y$ vim /usr/local/nginx/conf/nginx.conf
4 B' @: ]6 X; o% H
: i  a/ f* B  Y: }6 y* ]8 B/ s1 q- B" c' X+ X
server {
6 N6 Y( ]9 i- t+ b; h  listen  80;
3 H! t. q, b) b. I  server_name localhost mike.hi-linux.com;
% w( ]* P3 X' J+ k5 O" @) x. J  access_log /var/log/nginx/yourdomain.log;% {7 T4 C0 g/ m: P
& O7 T! ]' @+ Y+ l
% x, Y2 f- _! P
  location / {
; A$ p! A, G8 B$ j) C
+ ?: {. }9 i- ?" R1 [) B* u8 W+ m
6 T. N5 [2 ~& U  modsecurity on;
1 ^% O5 J7 ?7 b, O: G  modsecurity_rules_file modsec_includes.conf;
( b: ]; g' R& g0 [9 i9 G, {" U  root   html;8 V( z5 B; a  \
  index  index.html index.htm;3 Y( `2 _2 a5 F0 @9 a
}
8 u$ A, G/ U6 @8 u- V  n}/ `6 s( ]7 F  b/ V
验证Nginx配置文件( Q( l5 y* L( p! z$ _0 p& q
$ /usr/local/nginx/sbin/nginx -t9 e6 H- E* w, p2 j" b( @% {
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok* C( Z6 a$ L! a) K0 d  I! c
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
, y% T& j' J+ b/ l% C7 @% y# g" H启动Nginx
. Z$ n+ \/ j" K/ M" ]% ~/ {$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf1 s7 h2 u$ r. P$ w! e3 y" \
9 c$ c( H2 ]" I' i8 h; m0 r

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


6 h1 Z, B' e( w* U2 U2 ?

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-2-24 00:50 , Processed in 0.093975 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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