找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10949|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
2 U$ j# ^/ y" W' u
* Z! _7 h9 ?5 p" b; Y
1 U* S5 A+ y8 H9 O( F; M在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。8 w3 S5 K# [" _2 U) T8 \# F
3 `9 i  R. _2 k% {7 ?6 _
& z- L5 ]1 y9 `3 Y$ o" d
什么是ModSecurity
9 ]. f5 C+ F, }ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。/ a. s1 H1 K" h! M( O2 i/ k& c" ~' }
1 b$ \5 d3 J( K4 d- }

; a) @5 W+ @* n! y; TModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。' L; E- N9 M% H8 h3 U

5 @: ~) P& d3 S, a" y% C8 D4 S+ i+ t& D
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。' _5 s& s/ Q. W- k! ~: n

/ @5 k# u. p' ]: c% t: q5 Y$ D$ t! Y) i! t% v( s
官网: https://www.modsecurity.org/8 V. G  ^' y/ j) }9 d3 p4 s

3 ?, {2 i7 a% Q8 j7 ^' e/ f9 b0 ~- H7 h0 L. v
什么是OWASP CRS& x8 ]: E1 F2 s8 D$ I
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
, t; _) n. D/ X. _( T
1 ]7 Q+ e- `  K1 r
& r; G' E9 |6 A4 |" i+ K7 VModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。7 f& p" ~. {, `8 P7 \3 j; J, F

5 M9 t) y+ q- V1 A" v" ~3 w  r  G  ~. L0 q; i
HTTP Protection(HTTP防御)6 J4 N$ s' R1 Z3 V& G8 \
HTTP协议和本地定义使用的detectsviolations策略。
/ V  \- r) I" ^% g% d8 Z- r/ d- x( J& X! B7 C. D

1 d0 @( H1 I9 r- AReal-time Blacklist Lookups(实时黑名单查询)9 P% `( t4 w) U, N
利用第三方IP名单。
9 n' N- [" l: P# Z! k* x( F+ i9 N

( [( y7 a2 f0 T- c8 ^HTTP Denial of Service Protections(HTTP的拒绝服务保护); W! V2 L: e+ t6 q
防御HTTP的洪水攻击和HTTP Dos攻击。; {7 A+ M5 N% B  U; s

/ c/ Q0 U* I2 X) F6 k7 Z5 J5 Q+ e; ?1 j1 W) K7 K. h! K% H
Common Web Attacks Protection(常见的Web攻击防护)
! C' G/ k7 Y9 y: i7 U4 x$ ~检测常见的Web应用程序的安全攻击。
) B; i6 J; i, z5 b5 g1 {# l! V: {- h- p% `
7 W8 S% W6 j- s( B! E+ \
Automation Detection(自动化检测)
8 @8 `1 Z1 ^: o) y4 F检测机器人,爬虫,扫描仪和其他表面恶意活动。0 @8 j* |5 G- p2 b5 T

9 V# _* h% f& ^+ p. O, G1 ?  a6 u1 T/ O' t( S1 O
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)+ m( a* D  O- G' J. i* Z/ j9 f5 l
检测通过Web应用程序上传的恶意文件。4 z' S9 Y% t3 F
8 ?7 L+ A$ u. W1 B6 n  a
7 r0 a7 O4 v- X1 D0 q+ ?& s$ X4 ~
Tracking Sensitive Data(跟踪敏感数据)
8 J% [' o$ f. i* ]% o3 K5 f4 y" W信用卡通道的使用,并阻止泄漏。8 I3 g3 b) \. i  _- e1 M. s7 l

4 ^  P# Q/ I, z. d- R) z9 H! ^( \+ S" w& R6 {
Trojan Protection(木马防护)
2 W0 i4 \+ v& b6 Y: c: B检测访问木马。7 f- I+ F. e/ J2 Q8 G# s7 Z

5 [) i9 x5 N. E) @$ P) F" W: B0 M% `6 f# t7 H& I" m2 [. T% Q5 q
Identification of Application Defects(应用程序缺陷的鉴定): h: D) d8 \9 I% I5 \  w: s
检测应用程序的错误配置警报。. \3 [/ t2 q5 d$ d/ d( J' K
/ X& d) o7 k# |# Y) G* ]
5 j- T. a# v5 Y' z5 e
Error Detection and Hiding(错误检测和隐藏). W5 {5 f/ m. }( P
检测伪装服务器发送错误消息。! C7 y* s$ G0 H

' c  K5 |2 x) w8 N( }
! S/ l& M0 R* w' d# K安装ModSecurity
! @1 C( e6 ]  E6 ?9 o. d% @; e5 I软件基础环境准备/ A7 \( z8 R, i$ P
下载对应软件包4 ~$ j1 `/ C1 o$ d1 t6 r( o
$ cd /root, Y& i5 A- U) p" o- l
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
) T$ e) ?9 ]" [0 @, Q$ s2 J$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
4 O" O$ D  L; O4 j7 ^安装Nginx和ModSecurity依赖包: E. T$ c, T% I4 ]$ g/ c  F
Centos/RHEL
6 R: I% j  a# Z# m8 S' V, {" I+ O% W" o$ Z" J
0 q3 E/ g" b. ^9 h5 O
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel8 N7 K5 O2 D" f" f5 @
Ubuntu/Debian
* l  X9 O! Z1 Z
. f6 A( c. P/ ~: c8 N. {$ P. t! O# |4 _1 w! w: s
$ 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++-dev4 O; ]1 T2 W% |: W8 [
编译安装ModSecurity
, Z$ [+ o1 j  U, Z4 nNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。4 C% R' x+ w% }2 c- T+ ]
; C: ?! E: q- d+ W0 Z

* S. q- r/ [5 y  P+ i" ?  f方法一:编译为Nginx静态模块) F' m" q1 J) u! c4 `

! B0 r* [, v3 o# t! x3 w8 @' _0 P5 i
$ h* o$ K* m8 \! u编译为独立模块(modsecurity-2.9.1)0 O$ U+ j% w8 m9 H" c# E
$ tar xzvf modsecurity-2.9.1.tar.gz, Y) m# t# W5 R5 I9 g2 |
$ cd modsecurity-2.9.1/2 r7 [2 f7 k/ S# K+ c  v# x, q9 W4 [
$ ./autogen.sh
* a9 D" R2 a; X& ]/ ~4 S% c$ ./configure --enable-standalone-module --disable-mlogc
* n7 D; k. E4 F- Z& ^2 T% T$ make- [/ {1 O2 ?$ \, J. u7 L3 K- e$ W
编译安装Nginx并添加ModSecurity模块
! `* L4 L# E1 n$ K; @0 s4 h% A$ tar xzvf nginx-1.9.2.tar.gz
3 s3 w$ _  t& |" |" a+ |/ I$ cd nginx-1.9.2
, m  r/ x5 r: R$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
/ b8 P! H) `' u! O1 u$ make && make install
7 N, v; H9 E& r! q) f( h方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
" L. V2 M$ A0 c# ?/ x; C& S4 ^  d
3 ]/ Z8 E  H2 c! Z
编译LibModSecurity(modsecurity-3.0)7 Z5 t# L9 n7 T1 }5 T3 x
$ cd /root
" q. T. I% i% g$ git clone https://github.com/SpiderLabs/ModSecurity- s5 x2 l" z% p# O! R9 O
$ cd ModSecurity
$ _" l- E% Y, l+ s' s; U$ git checkout -b v3/master origin/v3/master! m3 \& V8 H- s
$ sh build.sh- s* Z! @6 N. Z" i3 T8 d* `
$ git submodule init4 K* ?; X" J  h6 r; x& g5 t
$ git submodule update' L, T- `$ F, }; Y6 y, H. x( k
$ ./configure, d, p' C2 x4 d2 v3 J  b5 f
$ make
* S' i9 B+ n, I+ H: K, }/ {* _$ make install
& m6 e& E8 d9 G( U# i: R2 DLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。& g+ a; R; g# [7 i5 B& ]& N

  K* k0 e" @4 G
% h0 \' ^: C8 I% r: |$ ls /usr/local/modsecurity/lib
, v# n: h' X; rlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
' s# H' A2 H& W, G4 O0 }0 a3 U编译安装Nginx并添加ModSecurity-Nginx Connector模块
* u8 m* o3 ^* ?7 e9 {; L" M使用ModSecurity-Nginx模块来连接LibModSecurity/ D4 F/ D" m( k6 U

  [& N; A: B7 s. n9 U% O6 m3 {" B" e* p6 V; z# y7 c
$ cd /root
* M% v. l1 z  _( l4 j0 r& ~$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
  z5 k/ Q% L! F; a0 V* v" Q5 j$ tar xzvf nginx-1.9.2.tar.gz
2 H% I' m; W) X7 I+ i1 y" N$ cd nginx-1.9.2
( [8 t# l$ U; P9 j! x, J) T$ ./configure --add-module=/root/modsecurity-nginx8 ^  K( z: L* y9 ~* j
$ make
: A9 G4 v, `$ r" Y; J8 T$ J$ make && make install- J% ^# {; G) X7 }0 ?8 `
添加OWASP规则
. V1 o) Z8 w2 v7 x. AModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。: k5 V) Z; }) O9 [# q
4 w, ?3 q& t3 I
8 J) [6 K8 G0 @/ E6 T3 P, q! k! Q
下载OWASP规则并生成配置文件/ ^4 H* n0 G4 l' `
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
& @$ D' Z5 E# f* t  Y) B$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/0 {$ W' Z4 {7 K8 f
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
0 s% b7 A  m3 ^: B/ U2 Q1 b$ cp crs-setup.conf.example  crs-setup.conf
( L  |% V$ b; _4 I配置OWASP规则
+ ~* B  Y" d/ f/ \& j; A编辑crs-setup.conf文件
) q1 |  X, _/ @5 O+ @8 Z4 q) b: W% m- I: j7 G8 d: w9 d- q2 e2 s: m

% j! @" C8 S' S) T$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
* i& [3 u, V$ j, u: A! @0 z$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf1 p; V1 |( v* u- f7 M& D
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
* Y& `: U2 B* e: f: I$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
' U! U8 U% s$ C6 O! Q- Y1 e0 H: C: H默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
# t8 f( L( B3 S( D5 J
% G9 F" f( h) |0 y! `
% d/ r5 o1 m) j6 T启用ModSecurity模块和CRS规则
8 L( z: s/ m3 ]复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& u, f$ ]- R1 C0 J3 ?5 v2 I8 x. f

+ f. Y' Y/ v$ B5 X* Z+ d9 F6 Zmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
3 `1 \5 y3 T$ o) u! k  b/ X$ ?- _5 b3 n* Q9 E

; E8 l& c: M  g: F0 p$ cd /root/modsecurity-2.9.1/
7 g7 I" Q0 w' i4 y) H! D$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  : V2 V, ?! [+ ^) `
$ cp unicode.mapping  /usr/local/nginx/conf/0 ~, A" k$ D( U, P, N& q; \
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。- w1 ?8 X( D9 P6 I, A

  z1 F0 B) \" U' B. {# c: C. W) Y4 a) j6 f6 }" k2 h$ R  W
$ vim /usr/local/nginx/conf/modsecurity.conf
& w  d% C2 r9 ISecRuleEngine On, ]3 x8 P0 k/ P- w
ModSecurity中几个常用配置说明:
; @+ W# C3 t, N* F0 I) a0 _' \& @. F. c2 _$ |5 F7 a
* `5 o) q$ L  d7 L4 Y
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。9 w$ D- d  o6 [" b9 o
0 w5 B) n' M+ g2 g# D/ ?( X! A; d

; m4 y7 S+ D1 ^$ }0 P2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。" n# T! L; f+ n0 G8 E' i; m1 t
. |: l8 C$ T7 j  D

, f% h# |" R  f& o# G4 r3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。! I+ J" V, O: O/ M" K! F3 g

7 V1 ]: X; [+ u7 m3 h& S  ~/ I, w' G/ I- ~0 H  T: j
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。/ n" m$ m' @5 U! h0 z6 z

8 H5 N; E) D! i
, k, Y4 a& W# {  h在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。/ ^  p, E1 p; S* Q0 P+ ~$ C5 Z6 r

1 p" S; K% a  E* W6 c- K7 p3 v+ s5 u$ ?$ z
3.x版本CRS
1 b: X0 Q! k. F! q5 t$ n$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
4 Q3 o/ {3 H  F7 S7 Z& `+ P) M0 h# 生成例外排除请求的配置文件* ^+ F( S" N# M4 [
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
9 Y& [7 @& \5 }- |5 h! I) L$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
, H1 c7 I( G) F5 J" I: _$ cp rules/*.data /usr/local/nginx/conf7 w6 f# K3 D; P4 x7 D
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
. d% G8 S# `: b; Y' @- ]% \
" `/ z0 d+ H8 B4 l- F. S
* a# T' a' l9 F) F3 Q; s7 w$ vim /usr/local/nginx/conf/modsec_includes.conf
% A1 K& a8 [8 R% L4 [$ c/ h# K  a, _# o9 W, ]
[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

( V9 U% J2 p& s5 H
) o8 @6 {0 ^6 ~: q2 j& C+ j8 x注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
: b& V: o0 {+ k8 ]% g2 E) ?% k6 b1 d& d/ l# L" D

1 b0 k. M2 ^" X3 c9 n  E3 _. Q, ]配置Nginx支持Modsecurity3 i- b- w6 N& u+ ]! I" Y
启用Modsecurity  s0 }- j( H" B/ J3 b- _6 c
使用静态模块加载的配置方法
" M0 B0 a) A0 E7 ^+ e在需要启用Modsecurity的主机的location下面加入下面两行即可:% Z* |% ^. b/ q" e, z" i

3 {2 \: I" i3 Q. ?# Z+ M+ h4 P2 P5 h) W" z, ^* N( X: J
ModSecurityEnabled on;
8 p' z& H! s" |1 pModSecurityConfig modsec_includes.conf;2 S; l# i9 ^! D/ n" l: o
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。7 p- J! `9 P- v$ S  b" T
, r0 I- n9 V. z0 h

( l! s+ x9 h- }. `$ vim /usr/local/nginx/conf/nginx.conf
, ]6 H, x  C' p2 V5 F7 q
5 ~4 B9 w* n9 R- |# D) V* @, D2 o) a8 t# s
server {1 c7 L7 |$ R1 p- h. P
  listen       80;
3 v" F* B9 ?& L: _" q  server_name  example.com;
/ w3 `, e1 o6 Z+ [! Q8 z
+ m! z# F/ @: I& d  p# y1 {2 \, G: V$ J- N% p
  location / {6 Q/ {0 y9 R9 e% t
    ModSecurityEnabled on;9 W) i( U  @& Z3 m
    ModSecurityConfig modsec_includes.conf;. k8 l: h$ K# t; O9 D5 I8 C
    root   html;" N: z: E' Z5 J, Z* L" C. N
    index  index.html index.htm;
* x& ]6 d/ f! b* q7 T# O7 W  }
* L# C7 K9 y4 E& {3 B8 C# f5 _* \+ u$ }}& a, ~  U; ^/ I* `1 A6 M) U
使用动态模块加载的配置方法/ Z0 p4 i  A2 Z9 E& X
在需要启用Modsecurity的主机的location下面加入下面两行即可:/ L7 N4 G3 E! ]

+ Z1 _- g, w) J! J0 \6 c2 ?3 g
5 p# T" A% H: p  Y  ^" P: ?modsecurity on;
5 W% z9 `+ C. S9 j" gmodsecurity_rules_file modsec_includes.conf;
3 |. E' \1 X3 |; J% W4 k: B修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。: _1 G* }5 J. S( `/ ^; S

* n% N/ F# J% Y
8 f8 K' Q# e: s8 z/ r$ vim /usr/local/nginx/conf/nginx.conf
/ y" C; N$ Z2 s( n6 r  I" Z' O0 W/ s: [* w$ ]
( X2 `. R) ]6 R( [9 d1 A% F6 i6 C) F, Y
server {) [6 L, Z% c1 L' v1 w
  listen  80;
5 }( W; u9 A6 D/ q! l  server_name localhost mike.hi-linux.com;
6 x* s) F& b# }5 O" S9 X( y  access_log /var/log/nginx/yourdomain.log;$ |. j0 _6 p# @$ D# b: h' M

/ j' ?* t" ^5 Y: N' c) f, e! b6 W' E! h
  location / {# U( j5 F) H) _, K  @- J& j

0 n' w# U: g* R+ W
+ M7 X- J- j2 U% L+ Z. M  modsecurity on;
8 w/ L1 O# f: M0 A& x  T6 `  modsecurity_rules_file modsec_includes.conf;6 }1 Q  h, n" J$ |
  root   html;: p4 K4 m& c* e+ d7 d5 J
  index  index.html index.htm;
% r9 C; }! J$ ]}0 N- R: B  |) ]8 u6 e) k8 S( s6 A
}7 c) q+ O0 O" ?8 S
验证Nginx配置文件
# N$ n8 a, `: V4 Q1 ~$ /usr/local/nginx/sbin/nginx -t
$ `0 J6 @1 u# r  j0 [nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok3 N. p4 Z2 |9 C* B8 h5 \
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
1 v. J8 U3 }) N/ T9 B6 s% C& Z! A2 q; s启动Nginx- P+ ]) L$ G& x
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
( m1 c9 [, l  w% ?4 z
& _0 D2 w, H! F9 ]

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


3 O9 c) v; ]2 s9 `5 G- V; M0 [

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-4-26 22:28 , Processed in 0.073276 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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