找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9861|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。3 b% G$ e, F1 I( b7 ~4 F# @
  L, `# ?% m: M8 A$ e: [. p4 @) w4 e9 d* T

7 N2 V- ^8 L) G# ?在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。* G- m" q, E6 _. A5 Y

4 y; |2 i0 s8 R$ B) j) u. ?( l) w
/ ^! V' i3 V+ C/ V$ I, ?6 I什么是ModSecurity
5 q; x& A0 ^: h6 pModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。7 ~+ X/ p( o. w1 u9 J6 e7 \# ^% x

4 ^! E0 p! }' g0 \+ T3 X. o( I
3 B4 S; ~+ @1 |) @ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。$ H% g! ?! Z: _, e# n3 d0 I

; e0 L3 n" z5 ^, q6 m7 B9 w
0 r. H3 [: ~4 L8 cModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。" i. R7 C( V. Q; ~+ b. x) ]& S

* D5 u% ]: ~5 h' U) J' o8 Q( O8 @
; x; F) p, n7 G6 F2 R! f1 \官网: https://www.modsecurity.org/6 ]' q/ C5 F8 A
( F4 p3 @: @, L# y3 e8 p  h

/ o4 l! J% C8 o$ e/ U+ E' ]什么是OWASP CRS# c  H4 h' e5 e
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
# N4 n9 E9 W) ]. n' j& U' y; G  V9 q
* Z$ T  O# A* D( ~8 w1 a' f
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
+ A9 p4 E4 x2 d0 t3 j  Q0 g% |  v$ w2 t) f/ u' f9 u9 X9 T& [- [
3 X# A( i! [$ k& O- i
HTTP Protection(HTTP防御)
3 s2 f3 y7 d2 d4 E0 z5 t1 JHTTP协议和本地定义使用的detectsviolations策略。: F( D# L4 k$ V! j/ s' B" v4 z
& ^( I; |% K  `) o7 d  t
: G( k) c; L' ^9 z) Y% D# s. d
Real-time Blacklist Lookups(实时黑名单查询)! |1 u2 s8 X/ y# h" t' P
利用第三方IP名单。6 r' |3 O0 Z: E; O) ^% O& W. n
" ~" E( P0 }1 G
6 O; V) a( O& B# y7 H9 \  m
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
3 i# c& l5 C; T3 S防御HTTP的洪水攻击和HTTP Dos攻击。9 N1 x* K. A* M- z2 ^0 J
# v* v. h# h- E) @8 z& o1 b: I
! [7 e0 n9 u& t
Common Web Attacks Protection(常见的Web攻击防护)) h) L" h% U& @; o& l0 U$ \
检测常见的Web应用程序的安全攻击。! T6 z9 M+ W7 K8 f. q& h

( m$ Y9 N7 |( g5 S6 _
4 F# W9 \5 T3 ]$ I/ T9 O, wAutomation Detection(自动化检测)
& {5 M7 k" m+ p3 F" n检测机器人,爬虫,扫描仪和其他表面恶意活动。
4 Y- M: Y2 [" ]! }/ n% _7 y
5 J( z, X7 d, J9 b1 E) `/ |: T$ u2 h7 H* U
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)  C. O4 a' v- Y2 N
检测通过Web应用程序上传的恶意文件。
, h& d* |. u! _9 I9 g
! p4 F( m3 d0 l; ?# z
3 G3 h$ i# p6 LTracking Sensitive Data(跟踪敏感数据)/ w( H4 X/ N6 z* F
信用卡通道的使用,并阻止泄漏。
% T# ~& N; |3 F7 U7 f6 z, D
9 Z8 k) Q4 e) M& Z- S" A  _: F/ W
4 N0 I& Q' L+ Q. \6 R, u3 D- j% \Trojan Protection(木马防护)
& i% N/ T8 c0 r7 h4 c检测访问木马。9 h. H2 ]5 ]+ u2 d0 `3 b
  m+ Z! P% Q$ X6 L; _# `

+ H$ A1 s5 n& z4 wIdentification of Application Defects(应用程序缺陷的鉴定)
- h" o' u# p7 A! V' Y  _# g8 z检测应用程序的错误配置警报。
! ]& V/ R. U& r* B$ e" O4 s, @) N2 {. o
7 y2 o+ X/ z. ^) E' _( I% @
Error Detection and Hiding(错误检测和隐藏)% L! B& i3 a/ v0 }( Y5 b
检测伪装服务器发送错误消息。
, D9 W2 S5 ?! L7 q% a1 L$ T
2 A' S4 p. I- E0 u1 n( ?- \
* ?9 a* ^4 {3 N8 r安装ModSecurity0 ~4 X( F0 t$ y3 G
软件基础环境准备' c1 R4 w6 D4 L
下载对应软件包
, M8 D) x' s# `" `: G$ cd /root
7 N* f. b* s6 v' ?" l$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'/ G+ N# R8 z- d3 F; l5 e, t
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz; m" G6 h) W* w. H" A2 l8 V
安装Nginx和ModSecurity依赖包
5 u6 R" I5 u3 @# Z9 \Centos/RHEL. Q% g. f$ r7 g  x/ l- h1 N! _8 L
% C( M4 z5 ?- f
- v$ w3 }' |/ I6 V
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel; z; ?# t9 x$ l; A5 t7 w
Ubuntu/Debian
' A. f7 r5 M. `3 |! Q
: Q0 x5 z4 `: p3 G: `. |' ?) k! |( L" R. Z
$ 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
' `4 ]1 n" y3 H& p5 _+ W$ Q' F: b0 _编译安装ModSecurity
7 G9 L; t5 R" y4 w1 \9 ^5 BNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
) h2 W! |7 v' J* {' u, u% L! ~1 s7 V8 A5 N: P: g! l$ _! h4 I3 g
) Q8 r) k* t" |3 e
方法一:编译为Nginx静态模块
1 Y# `$ H" Z" O% p- u$ t/ {" b7 d4 S! C! ?1 q+ Z& d

8 r  l- f% O. O, l8 n5 A& l5 y编译为独立模块(modsecurity-2.9.1)
( M# U5 Z+ c1 z4 E+ F& M2 q$ tar xzvf modsecurity-2.9.1.tar.gz& w& b& V1 [* _7 a
$ cd modsecurity-2.9.1/( @) J* n3 D) S2 p0 Q5 V
$ ./autogen.sh
, e0 D) W: n. c3 b5 |$ ./configure --enable-standalone-module --disable-mlogc
! `+ V1 y$ e1 b4 X4 E: X* S8 g$ make* o$ m9 d* e2 [! m. }! ~/ E
编译安装Nginx并添加ModSecurity模块' v: q- I8 D* e& R
$ tar xzvf nginx-1.9.2.tar.gz
8 R# o: h) i3 {1 L( O7 J$ cd nginx-1.9.2$ u& A4 u* \/ \: t1 {
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/8 m/ O5 k; S8 t" T8 p7 n
$ make && make install6 i( R+ S$ W# ]+ b# l. ^8 m' m
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
0 {' r3 W* Q) l0 ^/ |8 D& i% @4 z2 m. l. y# c

* B3 K' d/ _+ N( q2 j5 H' o: d( E6 u编译LibModSecurity(modsecurity-3.0)
) {' W! u4 s* k$ cd /root" R6 p- }. z' [7 m8 M6 r4 L8 d0 i
$ git clone https://github.com/SpiderLabs/ModSecurity0 P  u1 P/ n0 F1 H
$ cd ModSecurity
6 k. ~1 Q; v! Z* O4 o, T. F$ git checkout -b v3/master origin/v3/master
% f% i; D. x) E5 v. @: e- Z( y$ sh build.sh
: y6 m! L3 Q* o' h/ I$ git submodule init
* k; L8 S- @( a" l, B9 ^$ git submodule update
: \# P5 Y. H  O+ J4 M$ t$ ./configure7 w! |+ g9 P1 j! y9 `/ B1 _
$ make
. x5 a( K: H2 ]2 J3 J1 O6 |. E+ R. z$ make install) ~; H4 ~# p$ \4 G  y; c0 a
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
/ r- \0 Y. ^& |$ z% s! B8 n0 j
% U6 |% J' z9 `0 ^; F
* m: `: s- ^/ I2 B( n$ ls /usr/local/modsecurity/lib' F1 _3 s9 j( }1 y& J
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.04 f$ W4 I0 A2 d+ q
编译安装Nginx并添加ModSecurity-Nginx Connector模块
7 ]7 z1 e/ V2 r使用ModSecurity-Nginx模块来连接LibModSecurity
. a# }; U2 D! ]/ {
9 D4 G9 P% H0 A6 v  i4 |- X: E  n7 d: C$ i* s% p5 Q( p2 G( d3 N
$ cd /root3 O9 K# ?4 W3 c& W- U2 H. l+ q5 j. |
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
/ O( x! M' T% c* c5 l5 J$ r$ tar xzvf nginx-1.9.2.tar.gz. O9 O/ _2 G1 g9 K% N
$ cd nginx-1.9.2
- |4 V' _$ R) ?# w1 t2 Y4 G$ ./configure --add-module=/root/modsecurity-nginx
) V! t( M4 n2 [$ o3 a- `6 k+ m; u1 s6 d$ make
; ^) E! }0 E- ^, j8 K$ make && make install
8 p* H" x# N4 \$ K! M& f6 r5 D添加OWASP规则
9 a2 @6 W6 O7 A7 U& @ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。' p! H3 i. v; {6 d. F* {  H

3 |) q( O. w& e! p; g. P+ D& [7 l" O
下载OWASP规则并生成配置文件
7 y: }3 ~7 }7 Z$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
. h+ Q; p! f% s$ G: U6 e# S$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
: N  {7 B, K: ^$ cd /usr/local/nginx/conf/owasp-modsecurity-crs1 W/ d, ]# A7 v. Y7 n5 q/ A/ c
$ cp crs-setup.conf.example  crs-setup.conf% a; }: u( o5 Y; O  t  S8 k; a8 H
配置OWASP规则" y9 h% r' {0 W& I8 |* V
编辑crs-setup.conf文件7 t  D* T& \4 I& h/ x( c

" l2 D0 i( G* i( ^6 g, f8 O, ?5 f9 L  }6 u) D
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
% A/ t2 Y/ g) V* d$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
8 s8 U  O- c2 {, j$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf; E0 s4 M' C. `0 b
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf, \! v  L: N- A! V0 `
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。. a9 f) ^" ]3 l. f5 {( r

- p! L8 Q5 p5 A. ^6 I9 ?8 m* a' N' l8 b5 s
启用ModSecurity模块和CRS规则
; v3 o% h' G& ?# Y; z- k复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& k# ^% \$ M5 S2 }" t
3 Y0 @9 \$ J5 N. j
, ^+ ^/ p! {' \; P! J
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。8 |* T: d  B/ [6 e7 N+ D

3 ^/ H+ U. g6 ], B" `
  A* U4 K4 S2 Z, X% S& r) s$ cd /root/modsecurity-2.9.1/
8 \6 |& W3 M3 I9 ?) {; I$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
; X1 o1 u2 a! @7 x, s3 \$ cp unicode.mapping  /usr/local/nginx/conf/& E+ K3 [4 W1 b) e4 a( r- k# z
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。( e5 H, I9 |! x
8 _4 \& X6 c  ~( w. g/ A# [2 ^2 a

* r' Q% x* T# q/ X$ vim /usr/local/nginx/conf/modsecurity.conf; k8 C7 K5 N. T* R8 j
SecRuleEngine On! q  X( I2 f! I6 K( p5 {7 V
ModSecurity中几个常用配置说明:0 N, p; \% {1 A0 z! W

6 v% r6 P" A1 d' ~- S- Z, W; M
8 A4 e3 f8 q# [2 ]2 S; @# |1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
" _" B( c7 c- R; H4 E# k  O4 E- C3 A4 |. A2 n! \' e+ H7 @9 U

, p  H. v) o, |% z) @2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
0 V% [* Y0 b9 d3 a2 z. `+ N; t* |, R0 F& U+ N

! K  c* q& S, a# I0 p3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
3 j0 q) S2 w5 h1 t+ i/ O; ^# a) b. h* K/ C9 H/ k. m. ^
) f) e/ R- w5 r0 A: n
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
* z9 b: t( N+ E& M/ u* q6 {8 J. n

# N$ {. `8 m: t4 {+ |3 [5 {在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
" K2 d7 s) U# l% R) V) v+ k3 X, }
/ _9 u/ E% e6 e( q9 R
0 i9 a- {: A; b# l: t- F" R6 l9 @3.x版本CRS' W& X+ c5 U! F! H: J8 [
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs' `; z( P6 r8 {" n
# 生成例外排除请求的配置文件
7 M9 r" L& U, |( L: i$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
7 M9 s! k; t3 y  A! f$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
) s. U# H7 U0 j. \5 f' }$ cp rules/*.data /usr/local/nginx/conf, h' b+ x9 j0 F
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。6 t6 ]# r9 }$ {  r' H: L2 a  D  n

  f& W) b' k* Y  G: n( K3 P* A! W9 I3 L7 o
$ vim /usr/local/nginx/conf/modsec_includes.conf, U5 c' t. W% Y  B
/ r8 H1 }( C" \- c
[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
/ I. v' V5 Y  f

) m4 n; s5 h+ W5 \; @" M2 w注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
& ^1 u: z: V, i+ E2 k  ]
1 A/ H& S5 j; R5 b7 [4 ]; X4 ?' N' p' ~6 [) I
配置Nginx支持Modsecurity! O+ \5 h+ ]# ]
启用Modsecurity/ T0 e8 c% p/ K4 D# L- L
使用静态模块加载的配置方法* r( @% G! V$ J
在需要启用Modsecurity的主机的location下面加入下面两行即可:
" Z: \- W0 T- L, @% b$ a( [. F
2 ]4 f& R) K# G' X3 \+ e# @
+ a; G  G! K3 g, ]: x# vModSecurityEnabled on;, y8 M0 s" {) l3 m
ModSecurityConfig modsec_includes.conf;& `# Z3 Y: t4 e7 [9 k. b
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。8 l+ [0 T( M; @8 `0 x- u
: B, p: b; `7 [0 l1 }
  j  E3 v5 V. B- x4 F
$ vim /usr/local/nginx/conf/nginx.conf7 @5 w; g3 e0 \" j( _  {( V, @2 ^
4 z! Q; }/ p. Y, M2 a

$ n8 S5 C0 m) S+ A4 n. oserver {
$ O% q. j' U9 I8 U( u; W9 ]  listen       80;( O' V% c' W# L4 E$ t8 \3 \" z
  server_name  example.com;
0 R+ U9 X" h2 a, P7 R/ `2 Y; n# `! D7 ]6 ^9 i
0 `" y, p2 r( u5 ?0 s
  location / {
0 S% B4 c% k% i9 ^5 l    ModSecurityEnabled on;: a' x3 q: }/ F8 a5 e, b
    ModSecurityConfig modsec_includes.conf;9 X) v4 E3 q: h) B2 d1 y. R, g* D
    root   html;( C- _" H$ X. N/ S
    index  index.html index.htm;
5 E% ?+ d0 ^2 z4 A' ]  }8 ]9 |1 \8 P+ W. [( @8 R2 C$ y
}
  L9 D, _! \' R% ?使用动态模块加载的配置方法
- y# Z# J2 d1 u5 {: T$ G* @5 T; h" _在需要启用Modsecurity的主机的location下面加入下面两行即可:; D; t% n# \9 O
+ Q) Z% S, U0 d4 `6 O: T$ u4 l* v( V

# p- f( A2 E; `& [( Z2 ~, m, g# jmodsecurity on;* A/ E' g( {! H
modsecurity_rules_file modsec_includes.conf;5 M6 S# W) v$ O4 g1 F
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。  k( q7 M0 {) V* q* d; z
% a- i) S  c$ d9 q* `
( d- ^# M) j$ `) y3 R* p9 K! N
$ vim /usr/local/nginx/conf/nginx.conf2 K, A6 n1 x5 b% w8 P
8 W7 S' m4 S; c5 b" u+ M- _+ |% p( F
7 u7 k( C' O7 [: s: {) A
server {
, n1 m( Q3 C" U( @# G$ Z/ _" F  listen  80;
; L5 [9 q# I% y% v, j  server_name localhost mike.hi-linux.com;
$ R( h7 c' U' y, h  access_log /var/log/nginx/yourdomain.log;3 }2 ?% d" w) g* G/ c4 t/ j
! D0 F) N/ c3 `9 ~. a# b

% O, Y6 ~" `" y5 Y2 R5 \  location / {, H' p) o7 {2 a& ~5 Z9 }; i0 C. U
6 T. r( N% i2 y1 [

6 d3 v+ o; U. l, w' G  modsecurity on;0 {7 \1 Q1 g; p% g- ~
  modsecurity_rules_file modsec_includes.conf;% p8 {7 W0 i, x7 p* o
  root   html;
, s1 X& D/ z4 }% o- E5 A, m; g  index  index.html index.htm;
8 N' f" N' O& ^}
5 b  u: w2 d$ }2 t}7 n3 o! j6 B/ o8 v5 |9 @
验证Nginx配置文件1 I4 I1 J# C" K- f: `9 Z! M
$ /usr/local/nginx/sbin/nginx -t) l. v& C# D& ?4 C$ i. I
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok/ f( e" o8 K# Z5 [2 @
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful* t+ K- \6 j4 s0 S$ T6 o
启动Nginx" |$ H$ @% _& \( _7 K! H/ D3 A+ i
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf* |* L( X5 ]  }5 C" k
8 A' O6 w8 {0 ~, z% a

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


* h+ @& ?! m  G" a' {( q

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-1-3 02:10 , Processed in 0.087018 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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