找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10334|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
: f: B( x  N! X2 N" m0 M; R9 k4 P5 k* p/ N2 ]% ^
( ]2 j8 N& p. S6 s8 p" q  N
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
( l( ~5 i0 g* s! J- R5 S, T6 _) y! l6 q9 _

% I+ @4 K9 f, v2 [6 g4 N7 b9 c" V什么是ModSecurity+ ~6 [; z- B, v: y7 W1 k* g4 I
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。; D8 H, D3 T- N$ e5 B

7 S- @* M( G; r( e* {/ O2 o4 t! l2 U6 O' A
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。; C+ w% P3 T/ Q5 B' `
0 W0 e- M. P  \/ N

; U! ~  l; C0 r1 O9 R. h$ k6 aModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
( V) d. q! v; |2 W/ [& w4 J. r  A- q, o

5 a8 ]# x, v, U! m  U& Z! A7 _官网: https://www.modsecurity.org/2 x# M5 z  b* u4 l

  G& e* b* w7 ?. g$ o  d
" H1 j7 T9 T, ~8 M) B什么是OWASP CRS
) F: j$ ?3 @; p/ I1 wOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
& s6 Z) v' C* {+ q8 o/ E
. a! a. c2 T. M! X* @1 r$ S
, V& _, @4 s: Q4 ~7 PModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。+ g3 _4 b6 s" t2 Y: q

# Y& s6 u7 p/ \0 ~6 r5 j
: b% E  |- p) Y/ ^! `, sHTTP Protection(HTTP防御)  Y" f6 R/ `3 g" e% B6 j
HTTP协议和本地定义使用的detectsviolations策略。! h; t' [+ k# k* B

6 Q# ^# x9 `) B$ m; z- O
5 y# ~( d4 b, }  TReal-time Blacklist Lookups(实时黑名单查询); {9 I1 J7 Q* Y5 X% \
利用第三方IP名单。
5 G1 ^! U/ n+ g( Y7 f) I( c/ o0 Y! p" b9 t6 u* V5 L
9 G& ?0 n- _/ H6 P% I7 j
HTTP Denial of Service Protections(HTTP的拒绝服务保护)" D/ h9 L0 ?. E; n2 o# J/ q
防御HTTP的洪水攻击和HTTP Dos攻击。
, W3 B% o7 ]+ m: B1 |+ }; G- O
6 E3 ]% k! w1 c! ^, }  `% Y$ v3 W3 j+ r! ]
Common Web Attacks Protection(常见的Web攻击防护): I; m* x( V/ {; D6 Y
检测常见的Web应用程序的安全攻击。" o+ L1 k4 u8 \
3 L3 G3 x/ n$ Q6 l/ ]7 h

1 a6 ~9 z; v) B5 K% CAutomation Detection(自动化检测): y+ L% v& r- u8 w" L
检测机器人,爬虫,扫描仪和其他表面恶意活动。) P2 d, q# y0 t
4 r# K1 P3 U0 Q7 [. i& a7 ~9 T( X

$ ^4 G+ V" U7 _9 m' f. X" oIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
! Q7 |1 x! z3 f8 i& ?8 v; G检测通过Web应用程序上传的恶意文件。
% n) u+ ]2 l9 N8 K# q9 s9 O3 Y0 A: b4 e: X  s
/ _  b6 n/ i4 H7 C1 R* j8 r8 w
Tracking Sensitive Data(跟踪敏感数据)
- J3 H2 l% u# F8 I( ?信用卡通道的使用,并阻止泄漏。( a& O: l1 t# K7 j/ ]' \5 H
5 s' x/ \! @+ l
: }4 }: U0 ^9 p& g4 v# Z+ [
Trojan Protection(木马防护)
0 \8 E: H5 ]( h  h4 _检测访问木马。
- v0 W8 T% ~% W; u% J3 S
. v% o5 \! l# i) B4 b0 z& I* n5 l4 t% {& S* B
Identification of Application Defects(应用程序缺陷的鉴定)5 j. h" x; i$ ~: T0 s
检测应用程序的错误配置警报。
1 K/ m: U4 d1 r! n1 ?& E3 u: H' i9 x  M4 u2 \6 c
9 l' [1 |! L/ U+ S! F- k
Error Detection and Hiding(错误检测和隐藏). D1 d. T5 D# Y# e) c
检测伪装服务器发送错误消息。
- X+ K9 k  T$ G" h
( }! a6 `* r" j; E) D8 C& X. A
7 w4 j2 y$ ?+ T. f- b2 V* A) ^: s安装ModSecurity
) L9 e8 v) Y6 ^1 e/ Y软件基础环境准备
% X  z1 |5 O# a9 u5 f9 E下载对应软件包
0 ~: u/ T: @+ k. k8 B2 X$ cd /root
0 O8 h+ G( s: \$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'& p. K0 ^/ n- g( Y( Z* a
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz* `% |: g; ~0 L  E9 T% y
安装Nginx和ModSecurity依赖包
9 z6 m& B& B% f' BCentos/RHEL
/ j- W* c' _2 j, r: i$ C2 h# x' E9 M; h7 d
, w6 \% W1 q. t% B5 o' z
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel$ U! S8 C/ ~6 F* Y1 q* D
Ubuntu/Debian- h$ c5 `3 B8 g! N* }1 d

& v# v$ w2 m* N1 {( f: F$ J! h/ G% a4 w* ^$ S  _; u
$ 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
" J: Q4 W* Z2 g1 l3 b) [编译安装ModSecurity
! b# J- l6 u* m* L5 NNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。) u0 {) @( b2 h  Y# ~0 [: U' a

7 g1 k- u/ ^3 R6 }' o0 r, r
( `5 P% }- P3 w+ f3 P' \& z% W$ q方法一:编译为Nginx静态模块* t( _/ `. z5 I

+ e1 M' B8 Q' r) r% z$ ?  s' b, L  w; f1 m3 w% Z: }
编译为独立模块(modsecurity-2.9.1)2 V' k" w" ]+ Q* W6 J+ p5 }
$ tar xzvf modsecurity-2.9.1.tar.gz4 ~+ N  A* M" v, ]- k0 E
$ cd modsecurity-2.9.1// ?0 u# q* T: F; W
$ ./autogen.sh+ a  e9 S: k1 d4 u4 v. C
$ ./configure --enable-standalone-module --disable-mlogc
* P) I( Z. m8 r3 \; x2 g" k$ make/ N2 Y# ]1 v5 U" O3 s+ W8 K
编译安装Nginx并添加ModSecurity模块& r# \( F2 }5 Y; V/ j2 p0 Q( z
$ tar xzvf nginx-1.9.2.tar.gz
2 ~" y* H7 {  @' O) y$ cd nginx-1.9.2& |! C* N$ J/ w% b- }& d
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
' h. d5 F- x( I* i1 z$ make && make install
5 _5 h1 e6 n. B方法二:编译通过ModSecurity-Nginx Connector加载的动态模块2 v) D5 |+ v, W* ]7 V( z

* g( W& k3 U! }% f; @8 l6 n. O! U3 C7 u, c; c3 C: l
编译LibModSecurity(modsecurity-3.0)
, V9 A$ g" L6 l3 s  j$ cd /root
3 v# d+ K: j5 {; a$ S2 G$ git clone https://github.com/SpiderLabs/ModSecurity
0 R/ ^. ]6 h' B4 H! e$ cd ModSecurity4 [0 G$ E7 Z& y6 y1 Z% i7 U- G
$ git checkout -b v3/master origin/v3/master
, c2 T1 S7 z, S6 Z- C# S$ sh build.sh8 E8 l* A1 r; h# ~: ?
$ git submodule init4 j& q  o' K/ d; ?) w  I  }
$ git submodule update2 g! h, J: [, G  y' H7 _
$ ./configure, J+ `- k1 e# z5 v. m
$ make
1 P9 E% V0 f5 _3 @; N3 m/ n$ make install
  q. E3 N, P  u2 S" |/ E8 ?LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。7 O  e  b7 M1 s* w7 R6 w/ U
/ L* a/ H  w7 r" g+ p, S, b) k
( r5 V) n- m1 g  @+ w6 U1 Z
$ ls /usr/local/modsecurity/lib
6 }6 V0 v; u" alibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0" c  I$ V3 C0 w6 L3 r& H
编译安装Nginx并添加ModSecurity-Nginx Connector模块
+ y3 C/ U& J5 G4 b使用ModSecurity-Nginx模块来连接LibModSecurity
& R# h: {+ E4 L# z% s1 \
' D0 m( ~) }; J  X: V7 L
! U, n$ u  M/ q3 r8 {4 Y/ M8 o% b$ cd /root( O. k0 h2 g, u! ]
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx! r) n0 h) F; A" h
$ tar xzvf nginx-1.9.2.tar.gz7 A) h: I* x5 m# R( h5 M
$ cd nginx-1.9.2
7 e6 ?; |1 V0 z$ ./configure --add-module=/root/modsecurity-nginx
7 _: Y  U. i. ^' x$ make0 k) V9 r0 Y! R
$ make && make install' ^! d/ |* [2 N1 n9 n
添加OWASP规则# Q7 i0 i4 _% ^+ E1 X: E
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
  }' j5 `; ~1 B6 b. H$ ?" g# W1 e. J+ p: s2 [
) i1 W1 r/ |& v$ {: `; c* O
下载OWASP规则并生成配置文件
$ h  d  X$ Y8 E0 q7 F* y% r; R$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
$ n$ h1 J# d6 T. v- J% G  B2 Z* C) Y$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/5 ?4 @! D2 X0 |
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs( |" A8 l4 z) h! D. o/ W3 \8 _
$ cp crs-setup.conf.example  crs-setup.conf
& e, \; X* J( f, j' {$ \配置OWASP规则+ m2 a* L. Y! K
编辑crs-setup.conf文件
5 V% R( _6 C* f0 m. f" y8 e$ K6 {9 a1 G: Q7 _4 ^0 `$ U0 ?: \0 }
# U: L: J6 |& X' }9 N7 G3 [
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
9 Z- ?) ~- `8 e( m+ \: M. L$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf# C, t& ~- O* [4 {* J7 E. e
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf5 o  E4 i7 M1 G5 t
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf3 D7 h2 O) G/ h3 ]0 E
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
0 X0 w3 h" m% E( g& {2 T  g6 ?) l% r2 U) y6 j/ B- r

! `" J9 M# y$ t1 [启用ModSecurity模块和CRS规则
: u: i) P5 Y% |7 G- p复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( d% a7 X8 D3 `9 R, Q4 b' ~+ G2 [; J6 q  ?! \. E0 f2 O5 C& R4 K

1 b% {0 z- c, G6 Mmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
+ x1 V6 \$ L- ~; H4 n+ k
6 R3 ]- w7 ^) l( o
& A& x0 w" C' X* A$ cd /root/modsecurity-2.9.1/* n! |* W. _1 g
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
- a+ L  T+ I1 F6 ^# h9 l+ n$ cp unicode.mapping  /usr/local/nginx/conf/5 o- X5 v8 C: H6 H1 ?
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
, P* T$ v( x) k% n' `5 @8 C1 L4 H. P

& @% e* z8 v& H# y# _$ e4 i$ h$ vim /usr/local/nginx/conf/modsecurity.conf4 U# I/ @1 _) a# E1 R+ m3 i
SecRuleEngine On1 q' r8 S8 N8 J' n: r, e( k
ModSecurity中几个常用配置说明:
2 A0 @2 x9 {- U( E8 [8 e* W
. x  `! u8 r$ Q, _# i( p
/ d+ u- b4 l6 |1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。- _. v7 _2 m+ _1 v7 Y

0 G0 s; e0 }0 V" b; ]7 Z' Y0 y) r
# c5 _) E" G: I2 x2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
" A, S1 A: C( @( z- n
& g' y4 d# u' Q$ ]' P) }: G  _# {0 L4 q9 R: j* d
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
& z+ g7 f0 H1 o: K% c+ j3 f# }# J. A# U* p/ d* l# Z1 w

0 o/ @! H9 Y8 n  O/ \4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
' t) h! P4 }$ S/ J4 j& T, {1 P# S8 P% k7 [

- _" U* Y, T- H3 p+ _& v3 x% N! X在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
* k* J1 h, Q0 h4 k: n1 L& h* S) y  K/ _; U8 T# J( Q9 `' {
+ A. @3 I- X& B' N1 O2 V" v4 ?
3.x版本CRS
( e" x7 o6 B" s$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
, G" y! e# T$ x) s0 y# 生成例外排除请求的配置文件# Z  |: K9 B, ^5 o. e: }( `; G
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
: r' y, I0 l$ }$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf* H7 j8 M8 `3 m$ u' R4 n5 {: Q( _
$ cp rules/*.data /usr/local/nginx/conf9 j* E5 e0 Z8 c  v% y. ~, D
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。. W% `6 A7 Q6 v+ v8 g7 J

) b2 m6 U7 x, ]2 @8 `
- b' v; i5 Z& E) ~8 d2 u' |$ vim /usr/local/nginx/conf/modsec_includes.conf
4 {" Q7 m8 k5 B% a" O, ?0 r
: t# @( D. ]: W# h5 z% S1 u2 b/ _
[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
3 Z: }9 V; c2 f' `! Y3 v

4 e% d) v, b( |+ ]: a- E+ O/ A# h注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。3 E; k* v& ]! G4 W9 Q
/ N2 [! [- n: p5 d) p5 V8 R+ ~/ u
! l0 @: V+ \( P
配置Nginx支持Modsecurity5 Y) a6 a/ Y7 P
启用Modsecurity
8 `9 q# p9 ?5 [使用静态模块加载的配置方法
4 v9 L9 ?- k' l/ U5 R+ p4 a+ \. q在需要启用Modsecurity的主机的location下面加入下面两行即可:& a, l4 z' P' H' k% R( y

8 D. S$ h8 ?6 B! T
4 R, V& P! N. m( f8 Q7 ZModSecurityEnabled on;+ T# @/ @$ U9 `
ModSecurityConfig modsec_includes.conf;
9 G' {3 I3 q3 P4 e2 h7 @6 N修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
/ D( S9 {& ^: p; h
5 D$ T7 B& H4 s& c
/ v$ D  z- C- \, m$ vim /usr/local/nginx/conf/nginx.conf
$ h$ \6 {' I! l( I; N
% \" h+ |7 F% b2 z* o% L
3 u* X4 S( n/ I2 ^# S, m" ~server {1 ]$ v3 x- w6 a# ^: p4 n4 v( J
  listen       80;" O( R% f; f1 g
  server_name  example.com;  B( o: U! T- q, A
$ X% z  }: g  l. T0 P6 b1 F
  I( k' v! q8 \7 X5 M
  location / {
( q8 o0 A) p# f' i  k    ModSecurityEnabled on;
: t1 x; a: n6 i' V% X# r) S    ModSecurityConfig modsec_includes.conf;
5 H* z) Z* w  A8 `1 L$ P. B5 k    root   html;! k% {3 ^, r, ?# ^) z$ U
    index  index.html index.htm;. l/ y, a  y5 U, Q( h
  }
& r6 T1 \: d( V$ Q}; T2 v; I; M, o2 ]8 I% M) h
使用动态模块加载的配置方法6 O! q% D8 b/ e6 ^, Z8 u. y4 a
在需要启用Modsecurity的主机的location下面加入下面两行即可:4 f# T4 E! E" z/ @5 O) k' f
, A* a, x+ v; k, M% ^% O
6 v" U2 s+ c" L6 @/ ?
modsecurity on;
. y: ~& t& ?) P, T' o! hmodsecurity_rules_file modsec_includes.conf;
0 F5 d: l4 ^( [0 U( ]2 z# a修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。) j! A( [: z+ q

# H2 o/ _" [% l9 G7 D: n
9 i. ^- X- i' {" q* X$ vim /usr/local/nginx/conf/nginx.conf% ]% i4 w) h0 [) j! o* _0 M! L4 z, G7 e
+ C% x" j; Z  o$ @' b5 n! ?

2 v* Y: W- \. ]3 m' eserver {
, x3 l* |6 s! _6 }, q  listen  80;2 z3 I/ M/ |! k" e1 u. R
  server_name localhost mike.hi-linux.com;3 o" G* W9 e% C
  access_log /var/log/nginx/yourdomain.log;. E6 u! c2 w! Y2 M
/ @  N' N2 U- e) ?; ^7 a: c3 l
- X- Q6 x. o9 Q7 B
  location / {
& ^) `. {4 Y0 `( b8 D: O
* H# d: |7 [9 B1 Z# U
8 M! r5 t# {1 B: _  A: z# D  modsecurity on;8 j+ z, z. Z% N) Q# r4 n
  modsecurity_rules_file modsec_includes.conf;" F+ D4 |3 {- M
  root   html;
- s' Y% H- c8 j" c5 b  index  index.html index.htm;/ j; b( r' E% a6 i
}9 }. z9 a7 d! b+ ~2 D
}
; j  T0 @( I' [7 }$ ]7 s1 D/ g验证Nginx配置文件% h. X3 _" S. h7 n9 A2 J: H! C
$ /usr/local/nginx/sbin/nginx -t
, t1 l: w. c8 ?nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok3 X; j$ o! h$ {2 T
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful4 m" A' m* F( Z/ X$ e: f7 K
启动Nginx7 g( N9 M5 U( f* E& W
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf. R3 e( G' v! W+ z) \
* g+ L! s( V7 B/ O; M3 e

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


, r; A9 V: g- R4 o( Y1 o

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-2-26 16:04 , Processed in 0.417860 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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