找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11889|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。. {. K, N2 L# c$ D+ s

7 ~( x0 S: `. c
6 B( ]4 c) Y$ |3 _" I在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
& ~2 k# Z! \2 K- ^: }0 D7 w4 ]6 p4 e% o: p
" H" ~4 G  q& s$ c5 G- p  H
什么是ModSecurity
/ e8 v- ?- x  I) z5 CModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。$ K* q, q3 ?4 L7 s+ J; p0 o

* G7 v: `& \5 R, M2 c5 \/ d; j" n2 H- M8 u" m8 ]
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。7 `/ k6 E+ N/ Q6 D$ B- ?& H  @
+ s0 F  K; y. }7 R- b3 F$ I
8 ~# ^( f$ P* T
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。8 L3 Q  j3 M' r- h- [
# `. W+ E0 O/ L8 t8 ^

4 C5 ^3 L- b* H官网: https://www.modsecurity.org/) X' z. j: Q" t, A  o

: o3 ^. q" ^) K  I0 I" l3 |. q5 D# s: c" K
什么是OWASP CRS
( H$ ?2 d9 D# V- T6 R3 pOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。& W1 j% e7 d# A8 a  X* ~! s

7 b5 @' R+ ]& Q* V% ?( a4 `. T: R& {( T2 ]
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。$ t2 M2 u* h+ t: i& V8 r( k

1 q# I5 ^9 p+ ~; y  q. P/ ?6 F( N9 J7 y; O! N& v5 N/ I
HTTP Protection(HTTP防御)! o" ?$ n) T8 x0 b5 c0 }9 ]4 ]" C
HTTP协议和本地定义使用的detectsviolations策略。
& y" g3 r% A6 W& T) G6 r, \
: ]( X9 @* w9 {: f6 ]* D+ q, r1 k" w  l; F% d! Z
Real-time Blacklist Lookups(实时黑名单查询)# h  T4 V! b/ C3 a; y9 B1 {! N; P3 N4 W
利用第三方IP名单。% t+ m& \, g4 \7 k+ P& y. f

0 l, H) }6 U+ e1 l
2 `6 Y2 `* R+ g$ m" _HTTP Denial of Service Protections(HTTP的拒绝服务保护)( c  B; w* L$ c0 K% U( K8 {. R, j
防御HTTP的洪水攻击和HTTP Dos攻击。
5 J) s$ {& Q5 J4 \0 I6 q# D0 r# y; {

0 q" v: p1 T: |. i$ vCommon Web Attacks Protection(常见的Web攻击防护)  |5 V  q" G. o# z
检测常见的Web应用程序的安全攻击。* N& M  ]9 s- b" p0 G  `

+ i/ f7 Y; k8 E6 o5 R( {% W/ f% v: [/ {
Automation Detection(自动化检测)  G8 g' U: j7 M9 @3 c, I3 U
检测机器人,爬虫,扫描仪和其他表面恶意活动。
8 d/ o5 I, B) ^+ }$ [3 T7 H, {6 ]* c( h1 `3 R  S8 X
% Q9 [! T4 W8 f1 p3 V
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)( E# {0 v) _7 W
检测通过Web应用程序上传的恶意文件。
+ O8 L* l9 S( h2 m+ Q4 X' k* _( T
! F9 Y& M/ ?5 N( p/ K% t4 Y2 r* Q8 S4 v) G' w
Tracking Sensitive Data(跟踪敏感数据)5 ^8 o. w/ n5 d: s7 X0 Q$ O8 F" ~
信用卡通道的使用,并阻止泄漏。
) F3 Z0 y" {( K. j0 g
2 Y9 D7 r# s) K, p7 `
3 X: A, L" ?; G8 r% f3 \( lTrojan Protection(木马防护)6 R1 b+ e0 `* t
检测访问木马。( |) t# I9 j+ [
. f' h* a1 R. g9 L0 L; Q

; N' J' K% c: wIdentification of Application Defects(应用程序缺陷的鉴定)
2 N. v- c+ m0 @; p5 A# b) w6 P检测应用程序的错误配置警报。8 q/ @* e0 d7 c+ r: M* @
' R/ H6 v) D- A: f; V. m
) {/ C% |! y' y% G5 [( Q- a) [
Error Detection and Hiding(错误检测和隐藏), i1 i6 Q& V: V% i2 F/ @
检测伪装服务器发送错误消息。
! K& z  W* V4 i4 Z) Y
7 q! X0 @3 a' Y0 c! J4 q2 G7 b+ M! r9 V  E3 f0 i# T
安装ModSecurity
: K3 G% U1 D5 v" r7 a软件基础环境准备
5 z2 J& w0 y1 |- p  p' ?  _) v下载对应软件包+ r+ N* t0 P2 \. G0 z$ I
$ cd /root2 R3 `8 G) ~) z# J5 s4 a* i
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'% r: l; B3 k- u7 L) E
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz/ [$ @9 j( }( X
安装Nginx和ModSecurity依赖包
( W. i) r5 l4 }& X# z! p/ CCentos/RHEL, G: f3 O* r7 \- r0 E+ M! P
7 t/ l% c2 P! H+ a& @4 U: t( B
' c; r# F* q# l( \: _6 g0 f
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
" g' `% d9 @; Q: L) L# }# [Ubuntu/Debian& G  |" E( V" S% Z" ^7 \  C' s

) c9 ~9 x. g0 A8 x8 |) M5 C3 g/ A
$ 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
/ I+ j; X0 y9 o2 [编译安装ModSecurity: U' V6 j0 Z  p% E. n0 S7 E
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。( m# i) H; A* P

7 ~& j' J& Z3 F4 _# p; Q% b* F! x9 x
方法一:编译为Nginx静态模块# R2 \* T9 J! E8 ^" P% }+ c

% h( I6 e2 b; X* H4 w" }
" K: V9 _: M4 p% @0 q3 e编译为独立模块(modsecurity-2.9.1)8 E. w& @- S3 A) t, ^+ i4 O# ?
$ tar xzvf modsecurity-2.9.1.tar.gz
- C2 z, x6 Y; K" t  T. ?/ m' _$ cd modsecurity-2.9.1/# q* {1 h* P, E2 O, W' R
$ ./autogen.sh
1 Z. Q% V/ M6 _/ y5 J$ ./configure --enable-standalone-module --disable-mlogc* f. g3 J0 h* a% j
$ make6 @) w# z, e( z/ ?8 K! h6 L0 K
编译安装Nginx并添加ModSecurity模块
1 }0 T) C: I1 o- Z3 u$ tar xzvf nginx-1.9.2.tar.gz
( H- J" p! r% J$ cd nginx-1.9.2
* |! v! U0 T; H5 n  V2 f- N$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity// K7 `4 {/ E- C4 H
$ make && make install
3 K; ~# \+ R2 e. I' O* {方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
9 v/ |+ |; r1 @5 |. r3 D/ m& D4 o# D8 M0 K5 X- w+ x/ |

# p+ j: D' c+ }: C8 N+ t编译LibModSecurity(modsecurity-3.0)
# K% ~9 X/ U3 j0 N& r$ cd /root
7 J5 y7 S4 {7 C3 M$ git clone https://github.com/SpiderLabs/ModSecurity7 b2 \1 x% K) Q' n; i
$ cd ModSecurity
2 `, k" {: G7 {/ n9 O! i, S$ git checkout -b v3/master origin/v3/master0 g4 k( x$ u: \% ~& h7 @- c
$ sh build.sh2 z, d$ F/ N) @) q
$ git submodule init, F$ G0 G  F+ o6 M6 j" E1 @6 W8 l1 L
$ git submodule update
; p! t6 V/ `0 ~' }; w9 g$ ./configure
6 B" T5 z% ]7 L/ @5 t$ make4 q3 a& y5 J* c. t
$ make install
2 @, P2 j8 f5 ^  NLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。2 |4 s8 m; v+ {- Q$ ^5 k* `: e7 ?2 `

2 ~3 I7 V/ r) G+ A$ t9 f: s) ~: U, {' |1 h
$ ls /usr/local/modsecurity/lib
; x7 _, y$ B* x7 slibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
0 [8 M( L5 P7 R# z编译安装Nginx并添加ModSecurity-Nginx Connector模块; J/ u; \3 G7 b! B* X' w
使用ModSecurity-Nginx模块来连接LibModSecurity# l! O2 m: c- M* t- n

# _& }4 o. t7 b% G3 }: b% ^: v+ {5 d7 F/ V
$ cd /root
0 b" s  S9 v' i. f* E$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx/ m% Z9 E3 ]: D1 z+ h/ h% Y
$ tar xzvf nginx-1.9.2.tar.gz
$ a" m' H( X+ s6 {( B$ cd nginx-1.9.2
- @: `. P: c: A9 v9 S/ \$ ./configure --add-module=/root/modsecurity-nginx
1 @. p& J* s: k$ }6 |% q$ make
( ?9 C9 L0 _3 t+ I' z- w" s  z$ make && make install
5 l4 o2 @8 \8 o添加OWASP规则
: E5 }/ e0 w3 @, @; K- A) Z( VModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。/ J! s+ S+ [, \2 b+ w
4 k4 a# b! z  K
2 P4 n9 _( W8 k# Q. X& r
下载OWASP规则并生成配置文件
- [" b. |6 o. i6 M$ \( O$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
" {. E4 J7 U9 B$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf// {8 W' C! b( U; p7 \" }8 H
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
1 ~  c# j' s  u% E  r  m7 p6 P$ cp crs-setup.conf.example  crs-setup.conf
# M3 w9 t0 J! V# S. }" x: c% }配置OWASP规则
! {( |" W4 U: s% t6 s编辑crs-setup.conf文件
& r( E& W: D% D+ n  }" C8 z+ G( g
: o9 n! _: n* n) K' ]4 v0 L4 X: G/ G* r$ G" ~
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf, u" z) o; d6 a/ D
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
. N" E- ^3 q4 D4 Q4 c- o  n! N% d$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf$ {: F& q4 t7 s  J6 y3 R
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
. y1 X( v4 S$ A) j, @默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。* v) j( P8 Q; _

' w" Y, v6 X- X$ h4 U( F# w2 x4 z5 H0 e' r9 }7 E
启用ModSecurity模块和CRS规则7 b' a1 r! P& c# ?& A
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。9 \+ P1 C6 g3 t, {( O- T

' @' p/ b2 h% Q( N% d# {- y9 i' `
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。- U% x+ o$ l8 h- n6 Y! q

! d  U- I9 X2 n, T( u6 U; N/ F" r
' E3 O: C8 ?4 U( Z$ cd /root/modsecurity-2.9.1/
' H0 ?; X# j- ^! g$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
3 F/ b" Y& @! ]3 G. h3 k$ cp unicode.mapping  /usr/local/nginx/conf/! F$ S& m1 E& m/ g1 Q' i9 U
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。  W' W/ x6 Z; ]1 `3 F3 a

, j+ |9 C0 g+ P* K) L, Y
% a8 p2 S% i# W$ vim /usr/local/nginx/conf/modsecurity.conf0 A+ i! _2 k6 e: S+ C
SecRuleEngine On
, ^6 K9 ?- q2 z$ _' u  c9 H# h: CModSecurity中几个常用配置说明:4 r: ~0 u5 R" Z- b
1 H+ q; I& b! h0 r
7 z& d. c$ r3 v* a
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。# Q4 U# h5 W; @

$ f% z9 G9 a& x3 {8 f, m
; O- P3 V2 z. h1 J2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
# b5 e2 P9 F* q4 M4 B- o; X0 Z# D* p

. D( o' ?$ K' `% B; l; _) }  e3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。* z5 q, |( ~! c
$ w( C, \3 a4 V# }5 u( R9 u

' D' T4 Z4 `0 w: E# d' G4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
/ U+ X. T' ], Z6 ]! }# `& |9 ^
/ N; Z8 }$ t* U% j7 ]! S: D, l" W
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。- f  A6 B9 _- t: p0 x8 X" Q
  ~; j& b+ x& m7 B
3 F2 X6 d7 Z- ~% ]' ^0 [( M# F& b0 D
3.x版本CRS) c. H2 t/ k* m' K3 G
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs% E- V1 R4 r: R* p9 h2 y6 L1 {
# 生成例外排除请求的配置文件" ~/ [. x8 B. M; q2 B7 r2 x& d
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
: \2 Z" o, \: r7 J: z  y  ?$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf, |' C" P* o$ X9 V1 ]
$ cp rules/*.data /usr/local/nginx/conf. t# I: J& E( O, W5 X
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
% U6 W6 n9 s( Q. p4 S/ j( U6 \6 q9 |8 b# s, e  }( s4 C9 P' Y

7 G7 W( ^/ i0 \' d4 u- W$ vim /usr/local/nginx/conf/modsec_includes.conf
% [2 e7 }5 U3 C+ j8 p  ?6 W
0 U+ ]1 x8 Y- s( y' 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

: o1 t) ~4 d, ?% D
( s* V$ `9 e7 M9 B; v  F注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。2 M$ k/ s5 x5 s: N

0 K+ Z5 X# M0 R( S6 Q7 e$ t9 K  X' A& p+ q$ J" O
配置Nginx支持Modsecurity  ^9 X  d; z  y/ v3 N
启用Modsecurity
: d* _0 g# ?% l8 O- V0 y/ n使用静态模块加载的配置方法% ^& f! ^' w* b: H# }1 }$ y( C
在需要启用Modsecurity的主机的location下面加入下面两行即可:% @0 u# O3 }6 ?0 d; b

) R9 r* A% e( v" e4 j' m9 l5 i! u! R: H- A! z$ |, w* q" g7 @% i
ModSecurityEnabled on;
# }; l$ G* A! iModSecurityConfig modsec_includes.conf;9 g4 A& h& @( ]# U+ k/ X
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
# l0 f! x( s6 c# \0 w1 t3 G; r' m2 ~" R
' a# W/ Y* J1 Z& m) H
$ vim /usr/local/nginx/conf/nginx.conf& p0 u5 E+ T" t  @1 d; Q

# B+ S% H3 k( ~' o
7 R0 J* m! v8 q6 `) O+ u' ~6 Vserver {" u% C8 ^& j0 z4 B* ^+ Z. m
  listen       80;, i9 P- ]: k; i- V" F- l8 j
  server_name  example.com;
1 K1 J7 K9 {0 I5 a$ u" N0 k' Z, s& A5 H5 g% _$ ]& F6 O
1 H9 ~$ c3 z4 L, k
  location / {
1 H8 w2 ~6 O2 K8 E6 c! l    ModSecurityEnabled on;  K$ Y: C7 H0 C, [/ I5 U/ z
    ModSecurityConfig modsec_includes.conf;
3 s/ i0 U- {2 C5 ?0 x& A    root   html;
) Y( @5 _; D/ i' C    index  index.html index.htm;
/ y) U; z, [1 B* c- h  }0 z  Q) a$ |! |$ R, a9 y, m! ?
}
# I4 }0 i& L6 N使用动态模块加载的配置方法" O/ m3 A" c% z2 l' K
在需要启用Modsecurity的主机的location下面加入下面两行即可:
2 M. S+ ^* n2 r) \' n4 e6 M
6 H7 v) t4 z4 [/ H
9 Z) p" f! K- R( h* l5 r1 A2 {# r2 Tmodsecurity on;
- Z3 @! s0 W9 `# I( I. e# \% J  Z4 mmodsecurity_rules_file modsec_includes.conf;+ |8 Q% |: g( ^6 `& b$ A7 {' w
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
% e! `2 Y) D. u/ K3 j
- C2 \5 B6 w4 C# Y
5 y! S& Y* ]# q! j( X; o0 H$ vim /usr/local/nginx/conf/nginx.conf, G. l) q: n% G6 N( B, e: d1 g8 W
1 V* o' J$ q. G: x/ `/ B
) A3 T3 C3 g4 V% Z
server {
" M- P" u; Z( ^0 v" K* [  listen  80;# a1 {* n0 [5 m! c0 E0 [+ \) Y. o
  server_name localhost mike.hi-linux.com;/ f+ ^8 {3 h" W$ @7 A4 E
  access_log /var/log/nginx/yourdomain.log;; V1 z; ]9 Z$ z1 X" I

1 Y' U8 N9 u2 L' c4 ~
5 q8 V" a' `& y* }# U8 y/ e/ k  location / {; T" U) E) w+ E- f/ K  S; F

! V; c6 t' ^# \  w0 w8 Y+ l0 S1 S! m8 x" }
  modsecurity on;
# A  s3 q6 ^  i3 A+ I8 t  modsecurity_rules_file modsec_includes.conf;3 f  A5 _% r! j
  root   html;
+ F% s6 F& R; k5 U2 {* K  index  index.html index.htm;
9 S# c3 h7 X& y* h8 r9 u9 ]/ b}  P' l; E  u3 G! J) r0 ?7 J7 b: `
}& @3 k+ o9 X/ c9 l3 ]
验证Nginx配置文件" \2 t% w2 g  d$ B/ T7 U  [+ l
$ /usr/local/nginx/sbin/nginx -t( X" g" u: z6 @4 c) S
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
6 k# `+ l4 r7 C' L  Unginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful* w% G/ ], u" B$ Z9 P" }6 C7 _
启动Nginx5 c4 e; u" Z4 s6 B, e
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
4 y* A3 I3 v/ |( \- |" ^1 Q4 Y9 Z. a
) M$ k& u# J& R9 H

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


9 \+ g# i# e! w  B

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-7-25 17:04 , Processed in 0.080113 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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