找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10863|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。1 Y) ~$ x/ d/ O  A  ?* L. P' f

4 V$ B2 W" z1 w5 I7 A/ P6 R
, z$ P0 V- ^/ B. z' z' s& f: o4 M在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
4 R8 p8 D# N) R' _! z: x& {
( K9 B0 S- ]9 R. E1 r8 R$ x: ]9 o
: R  `# N" s9 M: }: P5 M- y什么是ModSecurity7 t) Y$ ], j* J% Z+ d2 M
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。* s' L. D6 J7 \/ F% h) @: z
: \9 g. Y# J) T7 t0 o/ X

. E' W( ]( W0 y& _6 BModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
4 i7 ]/ x3 t1 y7 |( I3 d- }; w/ k+ T' T7 T. F

! C; z! u3 r2 k5 ]2 ^" X" T/ LModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。( `- @# `/ r2 \) n. `

# n% P* z+ z! E
6 c/ D! P3 E3 f+ U: t官网: https://www.modsecurity.org/  H0 k- D1 X7 l  {# b6 V% h

/ w4 |1 s' E% M" S5 B$ k8 n5 j. V, j1 ^& T
什么是OWASP CRS3 o0 p  T& S) S6 p& P. C
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。$ _* I4 k4 c4 c! r: D5 g5 ~# C- L
& n. s) ^9 C& T- F

! p: H% P" [' A# u! q! G- xModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
4 s$ {) N6 j4 {! q1 F! ?" H/ t' Y0 v, {& Z* J' A; u
/ j( p( B% Q/ M$ j. j" A; r$ D
HTTP Protection(HTTP防御)
* `! j/ Y# Y) E1 J) s0 x; K, sHTTP协议和本地定义使用的detectsviolations策略。3 j7 a7 w5 |- k# u0 @# N  v1 N# E

6 F4 f- ]; \- u( q, \
, n  l3 U7 Z* x$ E$ wReal-time Blacklist Lookups(实时黑名单查询)
6 b4 L3 D; K# L# k' L利用第三方IP名单。$ x5 {( L! o0 I6 z' Q

3 r7 D# D: k2 }' t. D
+ n0 U4 E* d5 LHTTP Denial of Service Protections(HTTP的拒绝服务保护)
/ C/ v4 F4 U) ?& u7 _0 p2 F' R防御HTTP的洪水攻击和HTTP Dos攻击。/ Q* H+ f8 B) M8 A2 ?' T# {, t8 i! }

$ m! T1 b0 F; F' s1 b& w( r6 x( H2 q/ \
Common Web Attacks Protection(常见的Web攻击防护)# W: Q; s8 U! T! Q- g
检测常见的Web应用程序的安全攻击。
3 w$ w5 m* R9 i$ a6 {2 X6 ]; F: J  T. u$ S0 I

3 V, S) |  e8 o3 u; E! {0 ]( ]Automation Detection(自动化检测)1 d8 b" A* ~- U- R) k6 N
检测机器人,爬虫,扫描仪和其他表面恶意活动。
0 i' W: S0 t" D) ^( L% [; m1 O6 B
$ B6 s8 t5 o1 |7 `' T2 E4 z5 k8 l$ |& v. K; O- O3 u$ E4 M
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
7 x1 S2 X% w$ T/ N$ U检测通过Web应用程序上传的恶意文件。
5 q9 E2 C* F5 t! t) ^/ o! z3 ]! {. w: W4 f1 t
) F! [5 ?0 w8 k' r- r- J1 W6 H
Tracking Sensitive Data(跟踪敏感数据)
: \% @) u- l/ t8 z+ _0 W2 M信用卡通道的使用,并阻止泄漏。- Q4 U1 @8 s/ [6 k4 N  k
5 z7 ^+ c5 D; ~" L
- C) F3 p( P% u# ]# W8 [, s
Trojan Protection(木马防护)$ e1 y' P" f# w: z1 N) |$ j5 U) R( p
检测访问木马。
$ V5 h5 X4 C- m- A- ]6 y2 T; Y+ z* w' m" _2 c: q
9 m% O- g( j" D* ]
Identification of Application Defects(应用程序缺陷的鉴定)4 N- B- K3 t  w8 k2 f) Z
检测应用程序的错误配置警报。
! ?' P( h# K2 H4 r9 u5 T. K5 P, v. m# |0 m% D2 I, h- }
6 `% o" _+ [; {% N* Y
Error Detection and Hiding(错误检测和隐藏)) k' f8 |4 P; c+ d# I
检测伪装服务器发送错误消息。
6 r4 Y8 U% V+ i$ `* _! N4 a3 b0 L- ?5 z8 i4 X+ ]
) x, f" n7 K" ~3 o) O- r
安装ModSecurity' w3 F* f3 S7 q0 v- D
软件基础环境准备
3 e7 _* k6 x# _  p' o下载对应软件包
% d8 t( j7 N  v% R0 }7 W- H& C$ cd /root1 `$ U& u; l- z# @
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
) z* P4 G0 x) ~) ]) l' O$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
) ^! V9 I* j6 L+ p( [( l6 s安装Nginx和ModSecurity依赖包) o* x6 u( R+ I5 o/ j- n- a8 l
Centos/RHEL- _( r5 s0 f7 v+ o/ |  V) M
) D9 n' M; G" y% V% X3 X

% k% K' o) y8 a6 z2 ~' H$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
1 Y. s2 B/ [/ U7 M5 o! B0 fUbuntu/Debian3 X9 X6 ?4 k2 V% L

/ [5 L. K# |5 w/ D! u' `" i- ^& v7 `/ `) }2 I1 i
$ 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
7 Q  @& N. i! {1 ]编译安装ModSecurity' M/ }- _  c: ^) Z0 v
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
7 D" E% u) i0 B
+ d6 }1 a$ O/ \6 o" t8 E' s- i
1 R  m' A6 G, z* P( b方法一:编译为Nginx静态模块$ H" x; t4 r/ x# ^
2 p. d6 X( Q3 z: I! R

2 Z; u1 y5 E" R编译为独立模块(modsecurity-2.9.1)
* ~) |, U+ @' _1 D3 Y4 `7 s$ tar xzvf modsecurity-2.9.1.tar.gz
: Q' c; v7 U2 u; N: C$ cd modsecurity-2.9.1// q8 P, Q* t- W2 w# W6 P# v+ `
$ ./autogen.sh
$ J! p0 ~/ y4 m3 O" j2 S5 z# D3 g$ ./configure --enable-standalone-module --disable-mlogc
  A6 z' v/ c. }; ^1 Q$ make2 j2 {7 n/ J! g
编译安装Nginx并添加ModSecurity模块
) g$ e  x5 ]  U( ]! D  n$ tar xzvf nginx-1.9.2.tar.gz
4 d* t6 w0 D& F$ cd nginx-1.9.2
$ I; T9 X. J- g, y5 W$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/% [" |. ^: [  ?$ N: i7 `9 c/ M9 r
$ make && make install7 y9 Z( G% P; ~- m' R3 i
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
. K- \: z+ ?" H& R  R6 }
9 @+ U1 t% I3 j2 }6 e) l) k( Z' e2 K2 ~# E7 t- T( p% v
编译LibModSecurity(modsecurity-3.0)5 f. s0 G0 h+ b; C
$ cd /root
% g, b3 @( S3 S3 b$ git clone https://github.com/SpiderLabs/ModSecurity. n& W0 U; k' Z
$ cd ModSecurity
) m$ C( [' g2 Z9 D$ g" a. f2 e$ a$ git checkout -b v3/master origin/v3/master+ ^1 \' L5 j$ h1 v* i4 c; e
$ sh build.sh
' M, g  j2 [( Y6 c$ u$ git submodule init* }( z* u% [; z0 `
$ git submodule update
) _  F% H, U: H% F* t5 n. }$ ./configure
$ [1 `9 T8 b7 Z  n& r2 [, Y$ make3 l, o0 _. ?9 e6 ]
$ make install/ q; [( i: Z& [  R% q
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。1 t; L8 W+ P7 w3 l7 C

5 M. d6 p) R* }9 _0 u  w3 ?" o: O% X$ R, d# t1 ~4 S
$ ls /usr/local/modsecurity/lib2 S* v2 w1 p5 m8 e
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
& Z( [$ Z1 B& c$ P% Z) {& |" h编译安装Nginx并添加ModSecurity-Nginx Connector模块* Y( m" G. H7 G# o- _% ^8 _
使用ModSecurity-Nginx模块来连接LibModSecurity
- f. L! Y/ S  }9 |. i, S* G
% N# T7 {. W5 I) N2 `0 q0 B5 p6 i3 V* m6 r
$ cd /root& B5 E4 a' X; P8 t1 H, T% e
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx: C- o* g/ v, d1 w4 b; L
$ tar xzvf nginx-1.9.2.tar.gz" o5 O6 S" h# g" u  I% a
$ cd nginx-1.9.2. [& g2 [4 J( r$ Z/ u! F' z; w* x
$ ./configure --add-module=/root/modsecurity-nginx1 Q+ w4 m$ q. v  _
$ make0 Q- n' H; r  a: @1 f2 o, S0 d- _* m
$ make && make install
9 ~) A8 d0 Y2 |  |- z: Z9 ~* a添加OWASP规则
6 _1 W. c8 F  u; ^' b$ D6 _- ZModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
' U2 Q) `$ v! \7 e$ i, s; ^
# j! Z4 a/ ]! @! a/ }5 S, g, D$ v) ]
6 v, O& w* k$ O/ e/ j6 }. w  H下载OWASP规则并生成配置文件0 Y* t# D& v: C1 }) p
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
; `* w# _4 Q6 @. `. M  Y7 n$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/$ j4 I  [9 |% i
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs8 D3 X% ?  B* `# |8 w
$ cp crs-setup.conf.example  crs-setup.conf/ O0 L6 J$ E: s  e5 t
配置OWASP规则
' z4 p9 K0 }3 `1 M; e% R5 I编辑crs-setup.conf文件5 e  U  ^8 |8 I. l6 U. G3 Q2 S
6 v: g" @8 F' _: j6 I, ^% s
5 `7 y9 e5 Y; X6 H7 ^
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
2 n  a' p" d8 A$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf& e' l  c. Z; Y- O* C
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
2 X  {7 z2 Q2 q% T& X, q: l; P$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
. ?( U2 m% L" U- z默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
. w, ]( n$ m" V; I  x7 B' c& W
+ ?# v! q) K+ E- @+ P
! ], k  r) G; }$ @启用ModSecurity模块和CRS规则
# Z) z  x7 r# j# e5 Z) ^% W; U+ y复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。9 u1 v7 l+ ^6 ?. r# c
3 {. r4 h) ?* z, Z

& D% e% K" ~# I* h+ L9 ^( Gmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
$ e6 [* v' m$ \6 G. f2 ?/ `& w8 V
# |4 X$ U# M* W1 j4 P1 f* ]
% K0 s: @& f& B, y3 ]$ cd /root/modsecurity-2.9.1/7 T  p3 Q0 F1 \( G
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  / q  [+ @( F4 l: j6 ~6 _5 V
$ cp unicode.mapping  /usr/local/nginx/conf/: u+ k" q- R- g5 z1 l7 S$ E# r
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
3 U- C6 k  ]0 [0 v# {8 t  a" m0 `% Z2 ]" _/ J3 V* \# T

1 x/ _: R$ G& K7 l6 t! y$ vim /usr/local/nginx/conf/modsecurity.conf
* M7 i# u9 k  J5 p- X5 `; h" b+ KSecRuleEngine On" ]% ]' F( H9 z3 v1 n
ModSecurity中几个常用配置说明:
* r5 _" G. V6 x! G4 y9 u% M" e  t# P/ K1 W* }- S% x- |

  S# A7 i6 m2 A' G$ o1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。( {3 g8 w# u8 f: O) O
0 F  X3 t5 R% x0 c. `$ H% b* \% \
0 v& @5 x- e! {: c! y# s
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。  T% _0 C# |1 i

& Q+ X7 Q% m% g) \3 k
8 w$ A" M! O- i7 u* U3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。# @, r- k: e# S9 t5 z  P

/ S! k4 ?' w  z9 E. u& ~: k* F" R4 b/ q
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。  }( T$ ~4 _( Z  s
# y* k* D' L; v/ A0 S+ I

' |3 E" J' g. `6 J在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。  |( j; }$ z. ]; T% X3 v' z

  u* p7 [) ~  R- T, P5 R
+ ~! W0 Z  b. o, \( A3 D3.x版本CRS
( j) L+ M8 Q) c# r$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
: ?; m. {6 }4 I# r: L# 生成例外排除请求的配置文件2 h  ?  I" C) i/ W5 _% A8 z$ \
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
! X- x; i- k1 z; u3 \$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf# c- M4 W( X, U" r1 ^* s
$ cp rules/*.data /usr/local/nginx/conf
9 f4 l+ E4 E) r# }为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
/ K' [* X. t3 e* L1 g* H* X! u8 |' u

0 t( p- M* V2 R; Q7 {* W; L$ vim /usr/local/nginx/conf/modsec_includes.conf
' X% }6 U6 A" [) f( T7 f0 p. D& ]( ~) A
[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

# U3 g$ d9 c$ `+ I5 v3 l! T9 _# R- j6 K# m9 \
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。3 H& g5 S2 j6 ?( }8 {+ k

1 W$ T/ K% L/ Q. D
( s+ Q) Q% C6 O( v) T配置Nginx支持Modsecurity# U( u/ M2 D. B1 F) W/ z" E4 |$ ^
启用Modsecurity" Q( A# b/ r  r7 ]- @' u
使用静态模块加载的配置方法
1 N0 C" r& z* F3 O在需要启用Modsecurity的主机的location下面加入下面两行即可:
7 L) k; K8 I( g; [" O% {* S; g7 R2 q! W' l4 |
( B: r8 t) w5 f+ P
ModSecurityEnabled on;
8 _6 v; B# z8 x5 {" zModSecurityConfig modsec_includes.conf;# o- N+ {- G2 W. [! `7 r
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。* m8 H$ i& S- \% M" n+ s8 c9 v) s6 t
- l* H. y( I$ w

" |) @( P: L5 c: n7 ~3 J$ vim /usr/local/nginx/conf/nginx.conf: u5 t1 g3 u; ~5 K

6 ~8 J4 D- a9 b, K- X, I4 N% K$ m, a
server {0 J6 s" s; i1 e' f0 U$ R% j1 s
  listen       80;6 |/ r4 o; k1 c# N; I( p7 d+ t
  server_name  example.com;
' a) W/ P0 o5 `' N" x
7 g4 Z! H, d- [; x* r% J: v+ |7 s# n6 O5 m) ]$ E
  location / {5 Z9 x; V9 K& K
    ModSecurityEnabled on;
2 Z# ]7 l$ x) r2 _6 X* q; N2 q    ModSecurityConfig modsec_includes.conf;8 z" y- \% N' H
    root   html;
; P2 e+ i0 |1 j" w. R7 ?0 f    index  index.html index.htm;
( D7 b: G$ M3 O7 G3 q3 D6 f  }
2 g9 Z+ D; w# `8 {}
/ e; m9 l5 K! n, U使用动态模块加载的配置方法* O% j, L; f; H+ j5 w+ C
在需要启用Modsecurity的主机的location下面加入下面两行即可:
4 m$ x  C5 h) y' X. d* \! E( H4 `' |& O4 e* l

$ l2 k& Q9 b- O2 g$ ~  s/ j9 X+ Ymodsecurity on;6 U" ~5 G& b' _+ A0 @  z( O
modsecurity_rules_file modsec_includes.conf;
- v6 G# Z  w# d  [8 |' K( s修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。7 d: N( r* {5 ~8 I8 x0 t' O! i
- Y+ k' c) \/ O- @2 n( q! z2 D

. z$ T' N2 u+ T9 I5 ^- e0 d$ vim /usr/local/nginx/conf/nginx.conf9 h0 `; ~8 e$ a* \. g
  n% p# N- k9 B- O  i2 A

5 N/ e4 x  t' pserver {
1 T  y' n2 h6 ?; x2 d% k  listen  80;- q9 |( T9 X5 ?3 T! j. e! f2 A
  server_name localhost mike.hi-linux.com;
  i2 i, L1 ]4 L' t/ ]: A2 N- Z  access_log /var/log/nginx/yourdomain.log;
7 }' c. e9 H( B& ~9 [
& x) @9 q, P3 _! D! E( \% o* |( v/ Z' o0 S3 O9 n; U* C
  location / {
" K& X5 ^2 `2 W/ z; b$ u6 p! _1 c8 N3 }+ z# _

6 f% D7 X+ x: D  modsecurity on;
, J+ D4 @( l1 T) e7 e  modsecurity_rules_file modsec_includes.conf;
/ M) v& K0 ?  ^  root   html;
; h' L( S" t( G: a! x  index  index.html index.htm;. e7 ^1 @) H) `
}
4 b. U) b1 z" l4 X" t}
) H8 A9 J& m: k. }' N( r3 c验证Nginx配置文件0 d% h* \- D; U5 Z
$ /usr/local/nginx/sbin/nginx -t
1 E" u5 j( j  w: b) m1 anginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# L- b& s5 i7 k" {, fnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
4 z& `+ [: i. |" o启动Nginx
2 E6 K5 u3 @& l) Y" }$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf, B4 m! R& y$ L# l8 F7 [
0 z& p/ G/ r8 c$ d$ M! u

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


5 v( ^; t4 w* [6 ^- y3 h9 N# w

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-4-17 05:30 , Processed in 0.077261 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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