找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10138|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。: t# X# X# v5 |! U1 n0 A* t
4 ?6 X  Y+ @, ?- f  D) d* A* H

5 H" K0 ~; K2 A/ e  @0 p" h* {在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
8 T/ j% s& D& u6 q# C8 o
% L7 b& k. e% K! U* J: H- m  b; x, v" p1 Y$ o0 ]
什么是ModSecurity0 g5 j% c/ y+ [6 k! `
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。( }1 f3 c4 B- T
/ w& x( L6 _/ i% o
3 F" h4 G# r- C8 V$ h6 {
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。2 |/ M* u2 S+ G3 b8 t
4 }& @7 T% }/ G5 e+ n

. a3 S' X. }" r& U5 `' OModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
% u% m9 J+ a; K) \0 O' W) _- K7 X* m) j0 E& E# J' I: k+ Y$ |6 F1 h# X
6 [+ J# Z, l. ~" |* `, M
官网: https://www.modsecurity.org/# H, K9 ~+ o/ {8 n- c" u

9 \  f. h1 N( U. i0 C1 t- R- n- b: P
什么是OWASP CRS1 w, p2 C" n* Z& x2 N. k
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
8 G% q4 O9 E  s: K* R: P* l
4 w; S- n* U, B- E" V- H  }6 f. V& o
) I( b6 v0 a$ j2 g* c+ V# FModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。/ t+ b+ v* Q, Y7 G* k
7 }1 S5 r" S; @: c( @

! \4 H9 \2 A$ S1 ]- RHTTP Protection(HTTP防御)  U2 D  J- a, `' S
HTTP协议和本地定义使用的detectsviolations策略。
7 |, b% t4 C* E6 j. Q- V' H  _1 Y4 W5 \
& {5 X! a& d7 E7 f& K# r
Real-time Blacklist Lookups(实时黑名单查询)0 A) L7 ~. H5 e
利用第三方IP名单。
; \. p3 z8 N  b! f- D( ?' l
- K/ V6 L# w: J6 j* M6 P7 s6 I7 L7 l; I8 ^+ [5 q
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
' ]" p- D1 b6 J防御HTTP的洪水攻击和HTTP Dos攻击。5 ]" B; k% E& t% m9 h
" ~- D# e1 m4 l$ U
1 v: {$ J5 B  ?0 |- `4 V2 N
Common Web Attacks Protection(常见的Web攻击防护)
3 a2 H+ b/ {7 h: j& @$ _: ]检测常见的Web应用程序的安全攻击。0 Y2 z" W2 F' H
5 Q9 `( R& C( b# d7 H0 N  Z

4 `1 g% G; @- L, ^) C. rAutomation Detection(自动化检测)9 M& n5 E1 l. c2 z0 X& C% S
检测机器人,爬虫,扫描仪和其他表面恶意活动。: s1 b( A6 ^7 W8 s' p9 H

. C# L9 r4 \- G$ L" u7 \4 n2 `1 i5 s$ Q! o% q
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
% i  e! f/ \, ^检测通过Web应用程序上传的恶意文件。" ?: H% N; Y& w

$ O+ h; _* ^! r# E1 ^
- k: E$ T+ Y4 b( ^Tracking Sensitive Data(跟踪敏感数据); H5 Z: r+ r( R) K' Z6 g2 `
信用卡通道的使用,并阻止泄漏。
0 k, a+ O  I. ^+ L
3 V* r+ X( i& Y1 g% R* a! \/ S- |6 K0 X7 Q% V" w
Trojan Protection(木马防护)
2 F3 ?  Q6 Y! S9 h) [检测访问木马。6 N' T& Y# E" t( d3 m) Y' e

4 s! y7 H& _$ T6 m- Y1 i& K6 e, h! T" c" g
Identification of Application Defects(应用程序缺陷的鉴定)
3 a" e, `8 @, U3 |# F* U5 V0 p检测应用程序的错误配置警报。3 @4 S' N( J) I5 h) \/ M( r3 n: R: k
  Z" n# a/ E2 E1 M# C5 q8 T) y
+ n: W' _' E8 b
Error Detection and Hiding(错误检测和隐藏)
6 N+ T. {# @+ A6 ?; T; I检测伪装服务器发送错误消息。  _' ^/ N! ]0 L5 z3 |
4 F, s# _* N9 u0 j% ^
; [/ |3 l# T( X+ E" ]# `
安装ModSecurity+ d/ ]- W* J; K' ]
软件基础环境准备3 y. P8 a' K$ t: L% x/ ^) A
下载对应软件包  a( _! P1 p5 V: z- g# h# e
$ cd /root
4 F! Q  W  _# I, W( l$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
& n# E' O( r) y7 N6 i- L$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
0 S. p( B7 b/ N3 V* E9 G; l$ s+ y安装Nginx和ModSecurity依赖包
7 R/ ~3 Y3 h! R+ ^* tCentos/RHEL9 `# ^4 N* o9 Y+ D' m

5 v8 C, _7 l9 Q& E: q0 [% ~0 s* Z' M) M' w! ?/ Y% t6 E
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel' e8 O+ l* K! t% k! ?5 F' }
Ubuntu/Debian6 f( U7 N, `/ ]1 b! u# \/ v0 n

. r4 d4 n9 M. n7 J, \
& N! g% W4 t, L, b% ~$ 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
: t7 y8 ~: j3 u编译安装ModSecurity
$ k" W# g/ p: N; D! @% n# e8 GNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。8 O2 G5 z5 }) e; c5 T% t9 E* P
6 V$ x! a4 J% k( q9 c0 r, p7 U/ S$ u6 V; y

5 H: X1 j; G" U* {" b方法一:编译为Nginx静态模块# m. A; E- y9 f2 D' B! y/ m% ^
1 I! D4 b! _; W  }! T

& L4 S- c& M4 K. t8 M& g7 O编译为独立模块(modsecurity-2.9.1)4 {/ f6 ?. z, Q# _, y# A, _! V
$ tar xzvf modsecurity-2.9.1.tar.gz
, U* \+ X& t: x. Q( O* ?$ w0 Z$ cd modsecurity-2.9.1/
5 A* p9 W# R# t6 h: `* I# ^$ ./autogen.sh
/ }4 o" [  t2 L2 u0 V0 b$ ./configure --enable-standalone-module --disable-mlogc5 i# F$ g# I7 T& W: ~
$ make- `. l# N' W$ \* ?  M
编译安装Nginx并添加ModSecurity模块) e- l  T& _$ ^1 p: }
$ tar xzvf nginx-1.9.2.tar.gz
8 Z' P! s' B0 f, M3 v5 x0 V: G$ cd nginx-1.9.2
; q2 }; ~0 o3 L; r" X7 @* t3 o$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/- h3 m. m: X5 w& K
$ make && make install
, I$ Q; a, T/ A' q' w) u方法二:编译通过ModSecurity-Nginx Connector加载的动态模块& m# ^1 s  E9 w8 N
, d% x% c7 {) ]5 x: q
) a5 J' i9 B: o' ?; `
编译LibModSecurity(modsecurity-3.0)
3 u0 P8 Y7 @! @1 f" Y9 n6 C7 O$ cd /root
( \$ X. `8 q% N# I% C6 ~' l9 Y$ git clone https://github.com/SpiderLabs/ModSecurity7 Y+ B' P5 \. Q  C: F) P
$ cd ModSecurity8 F' i/ I9 p5 g+ Q- [6 z
$ git checkout -b v3/master origin/v3/master+ J) `  \. j  M2 A; P0 o; |
$ sh build.sh8 ?$ f0 P4 s$ M+ ?0 @
$ git submodule init
7 ^/ _7 @; _' `$ git submodule update
) l1 a8 R5 _+ r7 A9 i* a4 M) G+ q$ ./configure) T/ @4 K2 X% p; i
$ make
0 x+ ~0 H$ s5 v* V, U* Z! m$ make install
0 Q% v. W, [& {1 e2 S8 l' q: A, OLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。0 b; r0 v. `8 c2 u( w. u1 p# i

$ j# q/ k( V6 I
! ]- G- u/ }# \8 L7 N$ ls /usr/local/modsecurity/lib
& N8 y# S. p  o% Qlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
' d9 k$ t, [: s0 p7 v编译安装Nginx并添加ModSecurity-Nginx Connector模块7 Q( W/ y' B2 Y- o: D3 |) T) Z
使用ModSecurity-Nginx模块来连接LibModSecurity
8 R9 |% r  _3 h( s& C3 F: u3 U( J" m# U: l9 Z5 h& y. M# K, ~- {

0 O; p& p$ a1 i' _' n/ D9 Y$ cd /root: q1 o4 w7 c+ Y" N, `+ }; h
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx3 ?( d' Q; @$ V
$ tar xzvf nginx-1.9.2.tar.gz; n' `8 i$ m8 J7 o6 k, J' W
$ cd nginx-1.9.2
. M/ [! j, u; u, {3 w% z$ ./configure --add-module=/root/modsecurity-nginx8 y9 H9 g( H0 [4 o
$ make
. M0 j& m4 @& s0 ]9 r3 F: N/ u' ?$ make && make install4 s: ?) `* o$ |1 f! G9 g
添加OWASP规则
' {+ m* y% f1 Y3 y- ^! \# V4 G# O6 gModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
8 C1 O0 C, S6 K* L4 a" J/ ]( U* q  i- T: z! L3 X. x

3 _) n' R9 }1 ~7 v下载OWASP规则并生成配置文件% s9 K$ m1 l( E+ W3 C& D
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
: S4 P- c  c5 H; y& H6 u" h$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/$ a3 E' V, Q7 e: A' P- S
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
1 k( ^& [  ^% K3 Z# ]$ cp crs-setup.conf.example  crs-setup.conf
& e3 t, Y( e: @8 E配置OWASP规则
( N2 ?5 s) U( l, ^, A+ C( C* D- u编辑crs-setup.conf文件
. J2 ~: H6 N/ }3 T; B: z# Q% V0 X

6 \0 L  v4 c1 m$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
8 Y! U' q2 u7 A8 Q$ U$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
. J7 Z: A3 g4 m( M+ k$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
. x& Q. I0 Y6 F% ^$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
# m/ [/ Q$ E' b3 r" B! q& `默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。  Q3 Z4 D# i2 |( K$ d  W5 W* i
( T$ x) k1 D( t/ m4 o1 {7 s

$ [/ C8 O. t1 S1 Q! t启用ModSecurity模块和CRS规则
8 ~7 g. U, |6 O  r  v复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. T5 G: v/ q9 U1 q$ ^/ |2 |# ~  p
7 \1 Z8 @1 I, h! \7 T0 n
/ T) F3 m  W* d! Y, m. P- \modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。9 Z; }+ J7 w, E
  I! D, q7 C' K- i3 _- O7 @& G/ P4 m
1 y8 p5 t5 \, a
$ cd /root/modsecurity-2.9.1/6 B, V$ L9 O, {$ W
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  7 g7 b, N7 Q  }$ G
$ cp unicode.mapping  /usr/local/nginx/conf/
$ {2 L5 a( M( I+ H" p) t1 w& |& `将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
/ Q$ O5 i& j5 @. a3 ^4 k
1 |* r1 z1 _7 O( Q
& K8 W: k, P0 Q; t$ vim /usr/local/nginx/conf/modsecurity.conf6 ?6 N7 r$ t: J, p+ Q' ~
SecRuleEngine On" |) @: m( D; o/ x
ModSecurity中几个常用配置说明:' L$ ^1 L# U7 X8 |9 l9 l

4 ~# o2 U2 {3 [0 Q+ R' N/ T, ]1 Z0 X
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。8 L+ s/ o8 Y: ?8 {8 `7 x

8 a& z" S3 I0 C2 [
( v4 ~5 N; L  q# ?2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。5 W- {8 ?6 h7 R2 A: c! h! x
: f1 k+ `4 `* B! Q$ d) A/ E

( {# K( W4 m$ N3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。# U7 r7 A* v3 ?( S9 F/ j& g* r; m

5 C$ C0 ~! l" e6 ^
% b: @) V; C# _4 ^. _4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
! o' g8 q# I8 E
5 v  V4 [: l5 q" ]
& o$ a! ~$ f! i5 Z1 U' @- Q$ R/ A在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。& d( d! P% O9 t4 H3 s( f# c

5 C7 p' D, C& [' I: p: \/ Q
" d" P* [6 F* i5 @+ `1 ?( ~3.x版本CRS! P+ C( Y7 J) M
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
& o. U  e; h) i- g/ x# 生成例外排除请求的配置文件
" @7 \2 N6 _* f2 S7 Y! e% h$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
' ?: w) v2 K" n4 h8 B4 U- q$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
; j8 e+ l3 e$ n; x1 M, o& p& ~$ cp rules/*.data /usr/local/nginx/conf# {5 t# _/ g) L$ T3 e+ O
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。. E/ H5 p' z# ]" [
4 N" [- n$ V  k; s# z

" @7 ~+ S, E: c/ T" \% ]$ vim /usr/local/nginx/conf/modsec_includes.conf6 T9 s0 g! a; f: m

5 q5 E+ C6 j+ w3 S: p9 A5 q( O
[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
( @, K8 C( V( c& H9 E' }

7 y5 b$ H% N1 x5 w3 o注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。1 r, A1 b2 o* |) v5 x7 I" k
) S; s% z* V1 @- J% @; L
! A* T' C0 G9 b! H3 A; t
配置Nginx支持Modsecurity
# n3 @/ S! K% Z启用Modsecurity: \$ m. Y, |# g' X6 \% d
使用静态模块加载的配置方法
2 u8 n% c: [7 _- x- I在需要启用Modsecurity的主机的location下面加入下面两行即可:
* R; y- p- V. K1 Y* Z1 x& U
& A5 i# `, E' p0 M9 @
8 ]1 K; W# G4 J2 HModSecurityEnabled on;
/ z( z3 S( u& |$ X0 G% C, K  PModSecurityConfig modsec_includes.conf;- `! e9 f5 D( k( o: L
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。( Y, @& p, X+ n" Y3 b! ^" k; O

* x* l1 P% Y* \, O- y; J6 M7 W# f4 O$ }. k3 y, y' V; a0 K) b
$ vim /usr/local/nginx/conf/nginx.conf4 Z! [: u6 v, D* p) I5 E; i/ k* q

+ w; t4 n+ j. `5 A9 @; Y
1 _; A; n- t' v, X1 hserver {
7 Y- ?& g% b/ Y  U& W1 t2 _3 D  listen       80;4 \0 f, A+ f/ ?: a) f$ u- P/ K8 F8 S
  server_name  example.com;. d5 A( j8 {4 d
" e9 e( ?5 e- F" c) D# N* x
, e, {( D4 ~: {( v
  location / {
% Y! K) N# A6 v    ModSecurityEnabled on;
9 [2 F" t# ~! ^2 }' Q( J    ModSecurityConfig modsec_includes.conf;
1 t3 S  \6 n: R, c( [/ {( X    root   html;4 ^% s3 F" z! U, f& l( r
    index  index.html index.htm;) p  `4 F+ m3 E8 i- M5 H3 l
  }, d! {- \4 A( ]" o$ c" {7 |8 S* \2 q
}8 r, ?  a* `( ~; Q: ~/ _4 I
使用动态模块加载的配置方法
6 a. s' `" s& K/ f- z/ D, k- t在需要启用Modsecurity的主机的location下面加入下面两行即可:
  O) M6 J8 P9 ?* e  ^: H  a' O
6 W. h1 [) o, M+ W7 e% T0 C3 P; I6 l" u" {- S
modsecurity on;
- v6 V9 _* O, O; gmodsecurity_rules_file modsec_includes.conf;( |4 F) W- V4 f# f5 L
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。1 {" f9 z) R6 V/ F6 B
' v* z3 G! ]- x9 P7 g6 }$ k+ J

# a3 Y7 O1 y* V; T7 [$ vim /usr/local/nginx/conf/nginx.conf
2 P! Z* u. G+ |( \9 Z# ?# }" d: C! h2 F" w  q" I! |5 @4 u

2 ^6 ~$ `2 C5 U+ Z5 x! N# Tserver {
6 |7 v1 q. {# H6 J  listen  80;
1 U8 r& w# f3 A1 ^7 W" V1 L  server_name localhost mike.hi-linux.com;
8 {# D( V3 g. d6 p4 H  access_log /var/log/nginx/yourdomain.log;1 P6 h' M9 D+ b

# R/ j$ i/ E2 K
& e5 T9 W: X4 j5 g8 P  location / {
, p7 Z$ f# y4 E) ]3 o+ V4 l7 j, f- _/ q" U

5 j8 c4 X9 n* @; \! ~  modsecurity on;5 p5 _& [4 d) c$ m, ~: A
  modsecurity_rules_file modsec_includes.conf;1 w6 l7 e" q4 d3 f+ `1 [' n/ @
  root   html;
) e# f( c% a/ g+ C2 Q2 I  index  index.html index.htm;
# j; t' f" }  P! K2 K}
5 H0 }1 `5 _3 p# Y; r+ e* n" M}
8 T" o9 ]0 Z9 {验证Nginx配置文件- x; y8 l, t; F# G
$ /usr/local/nginx/sbin/nginx -t6 g4 K: e% }" m) z5 Z5 ]2 P+ C
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok# K8 `5 T; n5 E) b, ?/ g  Y# S: t
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
- Q4 Y3 P1 B3 \0 ~" b启动Nginx
: R: o; V& x) B. e( R  h$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf# \6 D: t' I8 A+ S) x
2 u" h/ r9 D4 y2 z

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


. [8 R+ v! q: R

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-2-7 02:24 , Processed in 0.087193 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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