找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11149|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
5 v$ a! d$ q. g, E' i) S  `
6 C6 n! D% D' z+ H+ n
! `1 ]- q9 R) B8 L! U2 I在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
) T5 ~* a/ x3 [  X/ m4 f0 Z: n& F2 b! B9 j- d* S& N3 g& ?  V

# U' ^8 i+ r; }6 c9 L什么是ModSecurity$ R& K3 j+ N7 f  O0 [$ H  B$ G
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
) S% H; e! T! r' O; X6 B
/ X. l2 Q7 _+ M& H' N; N0 U4 }  ~2 A, `3 q
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。) |9 _& T* h- d9 g0 q6 Z6 h9 l8 ]

, P% u! M4 c3 I# I( D
( e: |6 u% r" i5 {9 nModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。) }1 h, P3 Z1 w2 i+ S: N3 ^( b

- ^$ O( ?! x2 r3 k1 k; {& @. z) O- b$ q3 e  k
官网: https://www.modsecurity.org/0 o3 T8 b  W% R* c7 @4 [2 F

7 i) U' a& Z# _" C4 r3 N2 R9 B4 ^. v4 k- {" C. I
什么是OWASP CRS3 K; l& I; R; ~% m  A) c
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
5 [# h4 N0 Q* L" }! s$ s/ {- m) v2 q  E# }
! Q. z: Z2 o8 K, y' J: T# O
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。9 S0 A( Q: F: E1 ^! R% G
( ~% Q1 P; O* t- h  J6 h0 [: G6 ^  Y
& Z4 Q) F- \9 q' [) d
HTTP Protection(HTTP防御)$ E+ I" Z0 b1 G- e7 d
HTTP协议和本地定义使用的detectsviolations策略。- X* f7 q5 g. F7 U

5 N5 v. t" T! y( B. O5 x
% [: u) x) _( E5 W/ L2 l5 pReal-time Blacklist Lookups(实时黑名单查询)5 v; N* `. q4 ?# q0 N
利用第三方IP名单。
2 W1 q+ x. d) O3 |
# @: T5 u3 i6 V  b9 Z; A4 V/ w% `# U1 z
HTTP Denial of Service Protections(HTTP的拒绝服务保护)% t9 b- I! ~1 i3 v* G2 M3 q/ P" y8 R
防御HTTP的洪水攻击和HTTP Dos攻击。
6 J$ V& k; V" e- s# [$ |
+ ?; A3 w/ O/ y1 X3 L" i# c* |4 G8 n( K* k$ i/ ?9 S# ]/ B
Common Web Attacks Protection(常见的Web攻击防护)1 d8 o% F/ @8 @8 _$ N! H
检测常见的Web应用程序的安全攻击。9 I& {0 I( E! |2 [" I

+ W* v2 z1 U1 b5 O% D# i) @. ]( i2 _" X, R7 }0 L: H/ f
Automation Detection(自动化检测)
3 ~5 V4 ~3 n/ d) q$ j检测机器人,爬虫,扫描仪和其他表面恶意活动。
& D1 H# a* }2 l  z9 ?0 C; l9 v
1 e! M0 d, d* u; W- ?
: G5 F) e: d2 o! P2 m" oIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
( J, ?' }9 B2 |* d检测通过Web应用程序上传的恶意文件。
/ J7 |+ m4 g# Q" I' T, r% t0 F- L

8 q- S) S& U  A7 g  ?# O' _' g4 T; lTracking Sensitive Data(跟踪敏感数据)
) u4 {4 d& n/ z" f) Z( _信用卡通道的使用,并阻止泄漏。
0 C3 `% Z9 l' R
5 V: Q. J" W2 K* \$ {1 k; [7 o1 `& g. E" J& s: A1 Y' n
Trojan Protection(木马防护): a# L* X5 \. J8 P& q( P7 x7 B1 U
检测访问木马。7 ]6 f9 P8 P3 P

5 S" N1 w; C0 U
0 M( U4 w1 d3 l, c# F1 n- LIdentification of Application Defects(应用程序缺陷的鉴定)
+ h2 E- `5 ^5 a  ~7 g* x检测应用程序的错误配置警报。$ w8 d+ n# n: c  A1 E) z. h
5 \0 M7 J4 R4 q. x

2 t' N8 ~  p% E/ ?Error Detection and Hiding(错误检测和隐藏)8 u) E! N# j5 F. Z' k
检测伪装服务器发送错误消息。  O4 ]2 Y4 E+ T& W* i3 @

& J1 P0 E' M* _. a+ w3 `: c- D9 x, s$ }& {) O: ~
安装ModSecurity
) d( g# x0 W- t3 u" e软件基础环境准备5 j, K. G7 [8 U- L& M
下载对应软件包9 i5 P. X4 y  D# K  a
$ cd /root+ N. @' q9 {- _# T% D# s3 K# n
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
/ S% E0 M3 i% S5 \1 p$ 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& P2 _$ P: l3 D
安装Nginx和ModSecurity依赖包
" ~8 Q  s3 j) F* h) ?% jCentos/RHEL
# S/ v1 ]; x. E. G( C
% J/ \% A, k. P" w; f
, N4 I+ v* {' K0 {+ j! N0 T* }& H$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
, n0 l* I( U# q; T0 ~" o- I4 QUbuntu/Debian
0 q0 J1 v& y; l' R7 _3 R
$ w. s1 F& }% ?2 }2 K/ B& |9 m  a. F
/ Y$ w& I0 f' H7 o7 v. `$ 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  x) z7 ]6 _# h3 q* ^
编译安装ModSecurity
" E6 h  C3 M( c1 i# u% M- ?9 zNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
! I: M* q, O- h8 W8 D0 A. K$ B+ I2 s& h3 W% p

8 c3 \$ A/ M) i" B方法一:编译为Nginx静态模块2 v1 `5 G* G( D, `

0 o9 d% E: x6 M- c. F
- a! p/ p4 w. A% b编译为独立模块(modsecurity-2.9.1); T$ l) i* F# N9 j& H. ^
$ tar xzvf modsecurity-2.9.1.tar.gz# C+ X+ \& e# o! w* p0 o& I
$ cd modsecurity-2.9.1/
# ~* D! a8 p4 h, }: L* ?$ ./autogen.sh* j2 m& J. z/ D. ~0 F0 [6 @
$ ./configure --enable-standalone-module --disable-mlogc* s5 b% B/ W1 p5 }
$ make9 c5 x$ ^8 d: C
编译安装Nginx并添加ModSecurity模块8 \. j' i( m" C# j2 z
$ tar xzvf nginx-1.9.2.tar.gz
1 [( N7 _: r( }1 b. G1 A; |1 w$ cd nginx-1.9.2" @9 N. Z5 u. ^+ {4 J& P2 c6 t
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
* r0 l1 K# \- Y( r/ E  v% K7 x. B$ make && make install# n( Q3 l3 V7 n0 h, J2 y
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
: u9 U. v& T* e& E# c9 {# h# }9 K" t1 `; a8 @( V" x

/ z$ c  L, H4 o8 d9 W  x4 A编译LibModSecurity(modsecurity-3.0)
& s1 i6 |6 M; `$ ^! y* u$ cd /root
. g9 b& b+ a* H$ git clone https://github.com/SpiderLabs/ModSecurity
. ]; U- o; T; q- G7 A/ C$ cd ModSecurity
" I4 V4 P9 t$ o! [' K' q$ git checkout -b v3/master origin/v3/master+ U. A' d- i5 |
$ sh build.sh
/ O7 D9 n$ Q5 O# v% [, H6 d$ git submodule init
; R! ~6 l& s: _( m, M. ?4 {, V$ git submodule update
0 U  K* G, _2 ]! M2 @% c$ ./configure
8 I8 d5 c5 C2 ^$ J0 ~- n4 C$ make/ a4 ~) X& {$ Z/ v1 }
$ make install
3 |7 F: I9 @; H: G4 h  T0 TLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
0 a# A7 S" t( u- O9 Y9 a; O9 q8 p6 B, o1 x
8 E* v, t4 n& j7 y
$ ls /usr/local/modsecurity/lib; r8 P: s  R2 f5 R" e
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0# u5 F& ]" G. j; D# c: v
编译安装Nginx并添加ModSecurity-Nginx Connector模块
0 N- j5 G; S6 V: Z1 g使用ModSecurity-Nginx模块来连接LibModSecurity5 P: ?- x- H" G9 T
) x  _. l& `4 r+ h
6 d3 K# E. I$ Y
$ cd /root
: n, g' Q: n4 R8 ?) {" |$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx' i8 l& d( O% h7 S/ Y0 a2 d2 f
$ tar xzvf nginx-1.9.2.tar.gz, ]0 z3 ~8 |* R5 ^! B
$ cd nginx-1.9.2
0 b1 @0 T3 S0 b8 T- ?. j/ S- D4 A$ ./configure --add-module=/root/modsecurity-nginx- M4 [( g% _/ \$ A" @3 H
$ make
8 n9 {' @: R0 g' f" Q1 n0 k! e$ k/ M7 x$ make && make install
2 {4 U4 \. [/ O- q4 d添加OWASP规则) x1 O& F4 h+ m' U( `7 T
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。' c$ n; @% J) W; _, n& Z4 h& w

9 k2 O3 d+ ?3 n( g  F# q6 {+ }: R" |8 J! E+ d, l. i" S
下载OWASP规则并生成配置文件
  L" s* J9 Q% n; m- `# P) i: h$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git) q7 s0 O: k& x" [; G
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
' R  Z! d1 K/ u# H, k$ cd /usr/local/nginx/conf/owasp-modsecurity-crs4 H' Q7 d4 V- D2 g. s
$ cp crs-setup.conf.example  crs-setup.conf! I0 g/ O1 a& }) T- F) _
配置OWASP规则
8 M  V) h3 l* R编辑crs-setup.conf文件
8 x& s0 ]( D6 x9 Y3 d4 m; o% e5 N7 k1 q

% B" G4 t& B6 h  \* ]$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf; w3 M9 M, F3 @! _$ w& f* ~
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
/ z1 E6 C+ b/ E7 ]$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
1 {& K3 ?# I% i; r, P4 r0 [' d$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
, Q9 I9 w) ~8 b  k, i默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
# ~& m4 d7 M, |- E% D9 S
+ z% K. G/ }( S7 _! l5 V' s7 U+ @+ |8 Q/ f+ P& ^, t' E+ J
启用ModSecurity模块和CRS规则
2 d" d; \. |( N  U6 S8 k) B9 S! b复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
4 G' Z0 x4 ?6 z! W* \' `! k5 m$ {# A( j: l

9 K3 v+ @+ P" g9 M) E" I' I9 ymodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。- K5 f, e" D' @0 R+ G
( E: m4 ]7 D) D4 ~& d' T+ \

) q# I$ i4 O! |+ r8 g, m$ cd /root/modsecurity-2.9.1/
6 s5 O: i0 f1 T, s; v$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  6 y0 q1 c1 n. e. J1 o5 q4 o
$ cp unicode.mapping  /usr/local/nginx/conf/: m  Q# [  p0 E" F1 {* L% c1 ?
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。: b+ c  |6 o1 h0 ]0 k9 j0 Y3 v+ Y

4 O: s. a  J, @
' |4 d% V, j# j! s1 _$ vim /usr/local/nginx/conf/modsecurity.conf( W$ S+ I$ [# v% V9 V
SecRuleEngine On3 y$ `) O4 ^3 V& {4 b4 _! @
ModSecurity中几个常用配置说明:7 M/ y- A9 S+ m+ ]3 A$ j$ f
0 ^7 T8 I0 |3 G; H9 N, D6 z$ Y

7 S$ ~7 b/ L  G3 Z5 Z1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
6 X# E% Y. T5 ^) X3 K
: D2 }7 j& C0 C; L4 k6 _0 S0 W2 j: Q0 C# j" e; M. p
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
, g' V3 k" N5 u/ _& [
, Q: M2 h& v3 i' f7 o; h- z/ k/ o) ~% [
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。5 k- N; ^' t+ y) r2 J/ b8 X. Q1 {" W
2 }  p- `$ z9 _: f

$ `$ `% p2 f$ t+ K4 u$ V9 o: z4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。3 t& M) [- v. t8 k" G) x

" y& b' J* e. J$ j' s; C+ }0 B5 }8 g1 L- E5 j" t
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
6 j' s, u, y3 P4 F) D# u0 H3 }) s$ x* R0 t1 [
& n# W  x5 ]+ s4 n" X
3.x版本CRS
* o9 X  O6 y) G! `: c$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
8 \5 p/ J( s9 i* B1 m$ t6 I# 生成例外排除请求的配置文件7 d; ]; D# [) M2 g  m
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
: p% w9 O2 e4 P$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf% n# T2 e9 E5 g2 d
$ cp rules/*.data /usr/local/nginx/conf
, K8 t8 y, v+ g$ ?* f为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。7 `6 ]. I# x6 i/ @; l1 V% }! Y
1 o# G" q' n% I7 ], ]! U8 C

+ b  q, E* l" o; y2 l- I$ vim /usr/local/nginx/conf/modsec_includes.conf
$ ~6 }$ T4 t+ o. N4 |# H) `! U" }
0 g) f8 Z& [, N1 ~" D/ X
[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

& v* S5 Z) \. ~+ K4 p) _/ `4 L' Q: d" Q* w3 Y3 y: n" A
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。. }# V* w- Q5 G. j6 W4 N

* D: L" Z% m( J: x1 s
* M- L0 Y- N8 q  y配置Nginx支持Modsecurity
9 ^! {6 j  \7 c2 S启用Modsecurity
' [; _+ f% r0 O3 q. C2 ^  {0 a使用静态模块加载的配置方法: _, C( A! {8 \  ~$ O# d/ q; k
在需要启用Modsecurity的主机的location下面加入下面两行即可:
# i  Y  n. t% \) n% k& o4 ^  a7 \' U4 S( }. M
2 ^6 g, N$ Y* X1 {0 @: u; a+ B) T
ModSecurityEnabled on;
+ R- S# P' K2 Y* w1 XModSecurityConfig modsec_includes.conf;
" i' J+ Z, _" N$ W! b7 U修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
8 ]4 o: W+ H, u7 q* o' A1 h+ J% ?. L* D8 M  A7 z

% L- K4 @+ S! }4 B# J, E% N, r$ vim /usr/local/nginx/conf/nginx.conf
& G" W8 @% g5 n) t# n  p& K( c0 m! F- G% X, D# b$ @

- C" s% n9 {# C, h  y" e4 A3 e: `8 hserver {
, T" \" ~; z1 J3 d+ {2 E% c+ ?  listen       80;- T0 T& @2 e) n, P# u5 z
  server_name  example.com;. k  Z: K: {4 d2 O. ]

/ y7 _1 n) P; b1 I  t. A* ?/ g/ p! ]% J3 `- t
  location / {( T* m  `9 z& A) C3 @
    ModSecurityEnabled on;6 i/ [5 W8 B4 l2 R
    ModSecurityConfig modsec_includes.conf;
6 U, c4 K, O6 \' i    root   html;" E# s7 z( g& p- V3 |4 @
    index  index.html index.htm;. k6 f3 U% E# m2 U" \5 n
  }
- v6 T# g$ G# {6 n: {& W- y0 J}% E2 d' L7 Z# G, z
使用动态模块加载的配置方法% f. i; j0 q1 Q
在需要启用Modsecurity的主机的location下面加入下面两行即可:
6 D2 b2 A6 `! _8 X
3 g0 D; }& v7 s  c) C# |; u  b! f8 l. M
modsecurity on;0 ?! ~, _' H5 J0 x. Z  T
modsecurity_rules_file modsec_includes.conf;
9 y5 n1 ^0 {! j5 Q修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
' A, B/ ]# @4 k/ X
$ M9 a" V1 _( f& M- B- Y+ X& A) z* b1 b5 L" d3 N  F* j. B* j
$ vim /usr/local/nginx/conf/nginx.conf2 ]; f0 i8 B; I' y5 [: Q

9 W5 I% y8 [! C8 r$ U7 z& G* w# {* r7 e" d+ {; W; J# q
server {& r2 R2 h  U8 \6 G
  listen  80;
3 c1 W2 ^& ^; P2 R* E  server_name localhost mike.hi-linux.com;
2 e+ R* q! N) E+ N) h& d  access_log /var/log/nginx/yourdomain.log;
" N3 }! F* K" Y3 H& g
: s% J; n- F5 c% y3 M1 h- v
, n0 w& B( |2 N& W- Z  location / {
  L( J; V9 o9 N/ F2 `  {1 P/ P6 D& U3 `1 w  C* w; n7 v; i
3 T  A/ V2 M. a) S5 _4 Q# b
  modsecurity on;
5 i; P# O" J8 v  modsecurity_rules_file modsec_includes.conf;1 p4 |$ x* |$ p, w# z3 i
  root   html;; w& y- g* M2 B3 p6 y
  index  index.html index.htm;
1 e. }7 _+ D! L+ s. ~% a. `$ z}8 p# N+ _% ^" B: E/ s4 y; z
}
: j( C  E$ @- r验证Nginx配置文件
  s- G2 @. z) K1 C$ /usr/local/nginx/sbin/nginx -t
& l' O$ R: d6 {9 _nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok. ]0 L: D8 s8 I- W% R
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful0 _1 b' i! q2 b1 ?3 o5 U
启动Nginx
( Q& [; W; |4 ^& ~$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  Z% f* v  j/ ]) o' ?

+ g% [! ?" D% S4 U' u0 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能够战胜更多复杂/未知/混淆的攻击模式。


* P9 y# F( ^+ A

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-5-12 06:53 , Processed in 0.087095 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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