找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12153|回复: 0

利用ModSecurity在Nginx上构建WAF

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

3 s! B% ~3 |( c/ F在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
% r  j: t; S$ U4 u0 U1 X
2 M3 y2 \, p; J7 B" a& Q# N" |4 F/ c9 k! A$ v( n& @
什么是ModSecurity, K. J8 ^+ d9 n8 U4 }+ M2 v
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。5 o9 o* a' a7 G$ V, L

2 E" m7 B+ `1 q% P8 l0 P! T5 s8 n! U* ^2 r9 q) b) I' W
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。# A: J8 h& E8 `& B

; O) ]" m! F3 l" q3 f* d: E' M' t( S
( [2 W( b; F* l- [" aModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
- N3 g- `" L) r/ B& @# }
1 ]+ @( U% E$ x# N  S$ X. Q% a! j. O; r4 q
官网: https://www.modsecurity.org/
& ]# i1 b% \* i( h2 ?) `$ f% a( _8 R% b9 ]  a
. ^( J$ t/ I* m  k% o9 S
什么是OWASP CRS
, l0 T2 z- x( O/ }OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
- v! e! i& R, s& u; z3 S* c: a
# H/ d/ s6 l$ |" ?( E+ G4 d
8 Y; Z5 d" c2 N) E' hModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
, V6 ]1 K) V/ i# Q6 M( a
# _7 \# _+ h" R, |
0 z; ]; i& U! \( v( C5 c) G1 v4 b& j0 r/ z+ vHTTP Protection(HTTP防御)6 D& T: v; r- n$ D) g/ r0 j6 V
HTTP协议和本地定义使用的detectsviolations策略。
9 u: S3 a* \8 `
% V, ^0 F! H2 D4 g- T
& h$ R+ d6 Z; d: P8 i; U% bReal-time Blacklist Lookups(实时黑名单查询). J) Z) g% v$ J. a6 r7 [. j
利用第三方IP名单。
' f% G7 s: @- g, Q. r6 v
( a" c" J# B) G- L) C
  F" Y( D, `( q* XHTTP Denial of Service Protections(HTTP的拒绝服务保护)+ N, X8 n3 o2 z1 M
防御HTTP的洪水攻击和HTTP Dos攻击。
- r4 v$ H% w$ `8 Y, r. h
3 k2 W; p3 v  e; U" e/ {8 }. L7 E7 r# `* y
Common Web Attacks Protection(常见的Web攻击防护): E$ \9 U( K9 w" b6 w
检测常见的Web应用程序的安全攻击。
8 v/ ]- U, q: f9 r/ V
+ y; {4 _. M! o1 f4 |* E7 m* K& w
! t3 _8 a; {# S2 k1 P1 ]Automation Detection(自动化检测)
' b* {( ]) s% g8 L检测机器人,爬虫,扫描仪和其他表面恶意活动。% Y, O& p, }- l' w6 Z6 D$ f, f

4 h- f& x" e; s; l1 {. b' i! Q* O  n" _3 V$ {& @
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)- k( D6 `- V( x" v+ f& J
检测通过Web应用程序上传的恶意文件。
* A/ J, B! `! [: n! \' l
- l) {( g: ]( @7 K1 {8 ^9 G! p9 v8 j% B( [% C4 a( [
Tracking Sensitive Data(跟踪敏感数据)
2 g$ Q. U4 b. X2 e% t/ z信用卡通道的使用,并阻止泄漏。
8 L  ?6 {1 ?% u7 F2 M) c! H  {
, W+ X& h* W( N. r( F! T- Y8 m
( ]6 I5 d+ K6 Y* W  Q& ^Trojan Protection(木马防护)+ ~$ a$ b& T% u7 |; w: L* i( g
检测访问木马。& w4 R& u& d6 p5 \% l4 P  W
+ y4 O0 q8 I$ C9 u+ j. i% S0 P
% N1 ?* T- U  m
Identification of Application Defects(应用程序缺陷的鉴定)
9 r7 z. l% B2 L5 g. H检测应用程序的错误配置警报。( @: ?2 U  f2 a9 F* O

9 _+ a( {( s! j. H( ~  {$ D3 K' r: i7 {* d( J
Error Detection and Hiding(错误检测和隐藏)
. V) h4 N: Z* l+ W检测伪装服务器发送错误消息。
) a9 f% P2 d. F! S& N
* u8 y; J" `! e5 D  o) R
/ Z( ?$ y. q3 C' @- n) `5 M安装ModSecurity4 ^& l4 f7 b5 ]8 q9 T( P/ u
软件基础环境准备
. ?' S# d. @( C3 A9 H7 U下载对应软件包4 r( I+ i& l" Q, n7 I0 E
$ cd /root
% H5 O6 W! j0 @$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'; R( o% w- @) @* N
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
# q- E& G$ J! [6 D' d8 B安装Nginx和ModSecurity依赖包7 [0 F; [% z8 J  l* E
Centos/RHEL: E6 b, R7 k3 N; v' x

8 i& E+ P# _. X( \7 ~: l' H  u
7 O% y8 @4 w1 l" Z9 @1 s$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
5 g- V, F6 M; _4 Z/ s& \/ W. ]. NUbuntu/Debian
, @& @% c+ ]- |
9 Q8 V& v9 P6 ?) o2 }) ]% Q
6 B: h2 d1 s: {. Z, ^+ u/ k% O& s* K$ 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
! h. {: i# Z' v4 U编译安装ModSecurity
3 K( [& i9 n* \* w& F3 iNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
1 F- Z+ a( r! k0 _' q& D
: Y* E6 r9 E1 y4 p7 i" L
6 S: M( P# T0 N1 f. B* c方法一:编译为Nginx静态模块5 L  h6 V4 r$ K) k

, r3 k, d  t0 B8 i
2 z2 M1 I5 w9 h6 z5 k9 w编译为独立模块(modsecurity-2.9.1)
3 b9 ^. k, t7 m$ tar xzvf modsecurity-2.9.1.tar.gz
" p- D4 x% }" S$ cd modsecurity-2.9.1/: i# ^$ @  S9 d4 g
$ ./autogen.sh
  M& B4 ~* \9 B' d4 ]$ ./configure --enable-standalone-module --disable-mlogc( t4 Z$ p- T/ @6 O) l7 O
$ make  l0 s  h/ |9 L- ]
编译安装Nginx并添加ModSecurity模块3 B- I) y, _$ L, J8 w5 j; [
$ tar xzvf nginx-1.9.2.tar.gz
  K. w- x, Z8 V3 e9 p6 M$ cd nginx-1.9.2
: g: R* T8 c7 A) N+ N2 p2 R. q$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
$ g) K; Q- w1 J1 Y3 ?0 C) ?$ make && make install
+ M  W) \# m$ A& m) z方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
  M+ q( r& Q# i
$ ?1 d! g( P# \  ]; R1 ~8 D9 K- C5 D4 y& |; C( q# Q. ^8 O
编译LibModSecurity(modsecurity-3.0)
4 U( J7 c  w; v& |2 X$ cd /root! r, G" p+ \$ Y) g  m' o  ~
$ git clone https://github.com/SpiderLabs/ModSecurity
3 ~( a4 S, }  u5 o  Y$ cd ModSecurity
1 W5 E8 P! ~+ ]( S1 M8 g* [$ git checkout -b v3/master origin/v3/master( z1 t5 r% j! W
$ sh build.sh$ X' k+ V1 i8 }% {2 C
$ git submodule init
! @1 N! |9 }) E. a  c; G6 B( O3 W1 n) R$ git submodule update8 ~7 u4 X/ r$ O# F3 y% j
$ ./configure" W3 {5 X: C6 o* j
$ make6 c, Q# C. a' {# s" U
$ make install, E9 h& W) }% X9 K8 u7 i4 Y% A
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
4 t) m- Z% @, r' k0 K; V+ s/ r/ ^! A- x% M  |: Y4 _2 d1 R" O4 H2 S

- G1 o$ O' B' G$ b1 Q7 V$ ls /usr/local/modsecurity/lib
3 W* p+ s( ^' T5 olibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.03 y! ]; h  `5 h8 _* `, e6 ]
编译安装Nginx并添加ModSecurity-Nginx Connector模块6 E  @8 Y( l4 D; M7 y
使用ModSecurity-Nginx模块来连接LibModSecurity1 p3 C3 R( L; U0 B  }  _# c

) T! p1 H$ e, I$ O/ e, j9 E9 T! s& \$ f" N" A) ?/ x/ p$ }
$ cd /root; J! q5 l3 Z) }2 d% M& e9 N+ l% J
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
4 u" e+ l% [; K8 t- i5 ~# x3 _# Z$ tar xzvf nginx-1.9.2.tar.gz" n( y8 A' C" E6 ?, ~% F
$ cd nginx-1.9.2
4 W) w9 P7 B* X1 I+ O- q: W# H$ d$ ./configure --add-module=/root/modsecurity-nginx
, H8 ]' Z0 f9 z5 U' ^2 `+ u* _$ make
% a3 B- M1 Z5 t! ~' ~7 R$ make && make install- _2 u2 a/ C3 }% P8 Z
添加OWASP规则
) G% |( [- `: i+ a4 `ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
* S9 l5 z* l; i
- ?$ j/ W4 [3 U; Q' W2 a8 p
5 J: m" O- d+ ~下载OWASP规则并生成配置文件
& Y# y$ X1 m" M% [4 O7 m+ A$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
! c, U9 Y1 m( }- l$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf// m! X  e: w2 G
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
' _7 y# B. s; y9 V$ cp crs-setup.conf.example  crs-setup.conf
- L$ k8 h# o5 N1 L( g2 H! u" }0 l配置OWASP规则
7 F: F0 V2 T3 h+ C1 O- Z+ [编辑crs-setup.conf文件
+ O- L) @6 ~# l8 ~4 `3 w% t$ o  [$ h0 ~! y3 F
/ w  b# H% O* E$ B) u
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
1 _4 v) s% |8 n$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
7 t" ~) b2 W3 D; v, e. ^$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf' S. d( ~8 i  ?0 K0 c: ]# x$ j
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
5 q* x; P, W4 c$ p- Y( T7 z默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。7 u! U1 Q7 `, h* r& w3 t# Q

: q& T( H- O9 p  f
, I( f: {. I5 {( Q启用ModSecurity模块和CRS规则5 J7 h: G1 m  V3 ]  S
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。2 `- w2 c  H1 Q. @* t9 {
/ v; ]) \# m0 b9 l

6 ]; ?8 ]( Q0 Q- lmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。! \: n. r2 S. N" s
2 e/ A) B" j8 X! D3 d  j4 M, m

. E% X5 }8 J/ T& E$ cd /root/modsecurity-2.9.1/
, a; y- y. G1 q/ f: f$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
% O. f% n3 q1 [$ cp unicode.mapping  /usr/local/nginx/conf/; S& X  S5 T5 z' R$ Z* J
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。' M2 v9 B* `7 x: |

: N7 N( Q. u$ H* e# X3 l
, [" e. m/ R5 Y* B+ ?$ vim /usr/local/nginx/conf/modsecurity.conf' `/ s; q: D/ c' m: Z0 R
SecRuleEngine On
" t4 L( o! f; W6 [: x+ K( B- A! |6 N  _ModSecurity中几个常用配置说明:
! K" u+ c  K- e; X0 |0 ~# K( t* A  \7 x) s, M  V7 d

# D- L, |5 c0 C. o3 p" F/ K; e. ^1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
( f$ u/ @7 D6 c4 ^( ]: b6 N& j& m# w# ?# g

: }: }3 H7 i1 {: c% t8 m2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
, Q2 l: N5 i- u* R' a; l
$ Z% V9 d8 `! G4 b# e8 s5 u2 [% W  s
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。! {$ u1 N' [/ n, i* M

* ~8 w9 z' z& |, u8 A
2 |  I2 f, v6 e# a0 J9 g4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
1 W8 B: B! `. I) A' W
* e( O  U4 f& _+ p0 V2 h4 J/ h3 |2 J. _! J& a, E3 n# e
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。1 g& Z% Q$ o1 z2 m9 K5 {* ^

; h( s/ R$ ]1 T* K) \- S. `
2 g3 q$ g8 r" t& w3 @$ R3.x版本CRS
) b, i; J$ ?- j: R$ \$ |$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
5 v, ~/ N1 m+ U9 u! z9 |# 生成例外排除请求的配置文件. [0 Z! H5 S; w; [9 H6 J7 ], p
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf+ u) ]3 q$ }7 R! ~
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
& u2 _6 l2 ~  i1 [; M" w" _: t. K5 R$ cp rules/*.data /usr/local/nginx/conf
5 ~9 A; U% f  B' w! L% o; v为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。! S0 t9 d, c9 q: B
+ V6 w' o( L6 Q2 Q" J
' V+ E" N1 l7 Z+ t
$ vim /usr/local/nginx/conf/modsec_includes.conf
2 B4 V1 I$ }) C) }) s+ m' z# M& P! S. e. p( H
[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

6 J* o' B  j' [1 \8 f) K
) [0 q1 p& \/ h" D注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
. w, S2 K: o  _/ _5 O2 M' f, n  [" y5 n/ s7 o. z3 O  w3 O
6 N# J8 Z6 C) D4 V, `2 y/ H
配置Nginx支持Modsecurity: U! S+ m" @* P7 R- U# ]% c
启用Modsecurity
; @7 F+ M# A" S2 M! y$ @使用静态模块加载的配置方法+ ?$ Z; y; X' v
在需要启用Modsecurity的主机的location下面加入下面两行即可:
8 D3 X3 M0 k, w2 Y. Y/ H: a5 u& m9 |, _
, S0 o) i6 d( f4 A
ModSecurityEnabled on;/ e+ Y. ?# }3 Z* |7 K
ModSecurityConfig modsec_includes.conf;5 L& \( x' t, N5 ]
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。3 O& \& H8 t0 z5 G8 S; W" \7 |

& V" Q7 Z% p; W. u
! J% Z) I7 k( ^& ~2 i9 b! z$ vim /usr/local/nginx/conf/nginx.conf+ D4 e: s' N( X1 X4 n5 _

$ ]$ s" U1 G3 a: f7 y6 W0 n1 q$ W; \2 h/ |5 E( C0 \7 P2 O
server {
. k! i1 D7 ~" y9 ?  listen       80;6 n+ k8 J, Z9 Z5 @! T# h5 @
  server_name  example.com;5 I' Q, g0 R5 B! d, G9 N  H
# P4 @, l+ U, n* v# T% m

6 `! S7 ]/ H' M% b9 E" i  location / {+ G. f9 v, k/ m! C
    ModSecurityEnabled on;
# ^2 w9 J- @% _    ModSecurityConfig modsec_includes.conf;0 a: D: w9 j# {/ Y
    root   html;5 Z3 s$ y* [% O3 }0 g7 O+ v: N
    index  index.html index.htm;7 V7 r$ v! K2 @
  }
& a# X% R; R0 ]}
; O5 y/ A7 |- `2 J8 M+ W2 A使用动态模块加载的配置方法
$ I$ v6 n0 P0 W/ x* ^1 K在需要启用Modsecurity的主机的location下面加入下面两行即可:
6 Y- [" t1 b) g8 |% r  X1 {3 b! T* y. Z

$ ~: l' s- J7 J3 X7 z$ V; T, D* B" Umodsecurity on;
+ t+ G6 f9 ?  N) z# Kmodsecurity_rules_file modsec_includes.conf;
1 E! h. w1 b! [+ q$ _2 r修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。1 _; d* V3 K) D: a

! M0 k3 z" R  R& q6 M; a
2 x$ B4 m6 q0 i4 g7 e$ vim /usr/local/nginx/conf/nginx.conf
1 q! t4 T" ~; p) y& o/ b9 X
8 D; ?* `2 b: C$ I. C6 x$ q& V. o, q0 B5 j. u/ J) ~9 E
server {9 e0 N2 B: T0 w* M2 N
  listen  80;
; b9 M8 K5 X+ K  server_name localhost mike.hi-linux.com;/ s& X& N5 Q, s& E. u6 m
  access_log /var/log/nginx/yourdomain.log;
( [9 P8 X* l- z+ w* y4 ]0 W, b& w+ M  ^( |5 N& v/ _5 B
; a" h0 \; ?- I9 T0 T! z' h
  location / {
1 S* l: B& \) o9 `
2 p5 o& C- ]5 p
8 ~; K( T6 R8 S; o  n  modsecurity on;$ g5 Q! U8 I% ?' m- B
  modsecurity_rules_file modsec_includes.conf;
8 Z, M$ l) G+ c! I9 v  root   html;: O9 n* p% r3 _+ ?- |5 Q# c) N9 L* }/ ~
  index  index.html index.htm;
: O' x  ~" B( L0 R. {}
' C7 E0 j% D1 d; k1 S}! u; Z, c/ {8 d9 v
验证Nginx配置文件8 s- l5 a) ^+ p
$ /usr/local/nginx/sbin/nginx -t
" @1 G' e; w0 bnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok) F% z, n. S2 f7 F5 l5 v
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful# A4 }6 j7 j( b0 ^$ W, T% u3 c  u
启动Nginx
: ]& C, R3 }$ t+ B+ W$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
7 I$ f0 A$ G- R% s
+ i$ j' E5 c" r# t) x

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

$ x% e, J1 N0 B6 t% @; T/ v

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-8-22 15:43 , Processed in 0.085788 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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