找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10375|回复: 0

利用ModSecurity在Nginx上构建WAF

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

6 F) X7 F+ U: f
9 K# ^! r9 x5 N* @在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
" J( P1 @6 h, `- ]# }% F( A5 R1 y5 Z: O  p
# x* V; k; g( A
什么是ModSecurity
  v4 h4 i8 U3 U8 k9 mModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。/ t% K1 V1 J$ m, H

+ H. i+ g7 ~7 ?# _  _$ g4 e5 A3 ~
% s* k7 f! Q- T4 b, N! ~ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
2 i" b) k! K- ^  P  f9 r7 y7 A7 i* r$ `" _0 E7 y$ `
% F2 i+ c7 b/ ]  X1 `8 F/ m
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
- k1 T, x, u* |& K) {1 y' g! q7 A3 B( T& Q9 Y
0 d! [1 _, H" m; @0 d
官网: https://www.modsecurity.org/$ M" V) T1 [" I8 j! z' v$ f) S. r

: e  ^% e  P, b" a
0 S) y; |! i9 d1 A  }/ q什么是OWASP CRS7 O+ C9 E: Z4 q0 `+ R; b
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。9 h; Q4 L9 B3 M5 _5 H, Y; T; j& c
" T/ a: A8 w8 p) m, \: S6 b

; ~% y; ~& \: u. G5 C6 o4 q* kModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
3 E3 R2 o+ X4 g- L
! t: X/ a3 c  R, v: y
+ e  F/ l: S4 q2 L# DHTTP Protection(HTTP防御)
5 ?0 O" |3 `3 j9 {- E* mHTTP协议和本地定义使用的detectsviolations策略。* r9 F+ N( d: Y' {/ ^6 m  k
  Q' k- _' r# H0 P* V; |  B, Y

& x' p# u& ], J) b) f, \$ L7 qReal-time Blacklist Lookups(实时黑名单查询)5 m2 ]" t1 a8 H' t8 E5 a& F
利用第三方IP名单。
# ?! h4 v& L- O: p! y  c2 l% g7 r
6 N  V& N# s$ H* |# X8 t) n3 M4 s/ d# x: n& Z0 \- F$ X! _
HTTP Denial of Service Protections(HTTP的拒绝服务保护)5 M' ^. V4 k6 D; `$ m
防御HTTP的洪水攻击和HTTP Dos攻击。8 }8 M6 e  y/ A8 O8 ?

0 w# p/ R* h) N" k  W7 A) O+ n; o( A' O
Common Web Attacks Protection(常见的Web攻击防护)
' m9 F  V+ b3 j; E- \  b2 v检测常见的Web应用程序的安全攻击。, {2 n8 W5 B4 u: ~/ k9 V; ]

# S0 @; Q' k$ l% g8 ]
, S# V! y& ^- U! r0 uAutomation Detection(自动化检测)
; _+ N9 N/ a' y  Z4 ?7 D* T检测机器人,爬虫,扫描仪和其他表面恶意活动。: x0 q: c. A. u, F, M6 `5 M; U

, J, T" D) E! G6 e, u& z
" E, E$ g; Y+ a' d- iIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
! |8 l6 w; t2 D& f2 `. V检测通过Web应用程序上传的恶意文件。
! E/ O/ |8 _1 ]9 n( ?. x) N3 v! h4 o5 D+ b7 t

0 G. {! I( A) ^# w. n" O' K$ H9 a' LTracking Sensitive Data(跟踪敏感数据)" E& c; `/ s5 q/ Q+ d  x$ h, _
信用卡通道的使用,并阻止泄漏。
) F5 c. ?& p# v2 H3 u7 N$ [- T6 g) s" w+ N# y7 l

  x- U5 N8 g/ }' k* {# j- D! N; B$ yTrojan Protection(木马防护)
+ w. F& v4 S0 O( ~检测访问木马。
/ l8 P; \; W# u
/ R- {, Z; V) E# G: f" a5 R1 i( ]9 [& W6 l6 D7 C
Identification of Application Defects(应用程序缺陷的鉴定)
' k1 \& L& a, i  m( K检测应用程序的错误配置警报。6 b5 M7 i1 {5 p2 u: {- }, g
: k5 v, s/ e* F0 B5 x+ B; J
0 F* @4 s9 |" n( o  m/ q+ D% ^
Error Detection and Hiding(错误检测和隐藏): i: g$ a/ U' w4 o9 I
检测伪装服务器发送错误消息。
7 Q! Q  ?& n3 k+ r% d% I  Q/ {5 Y: D( k! ^

+ n! n9 D: {9 L4 H) `/ k1 U/ h安装ModSecurity
! K) W1 N' s3 c软件基础环境准备
& C8 V8 G) h: F- ?. ]下载对应软件包# E' @" J) }3 r) O2 R9 Y' n, D0 B* O
$ cd /root: u' B% ^" c8 V% ]
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'- N% a: t& O! N1 A+ [9 b
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
7 _0 s! g0 s4 E' ]$ g/ v安装Nginx和ModSecurity依赖包
2 {; h3 N  Q% n9 r% [1 I% vCentos/RHEL* w$ q" Z7 X5 U& O7 C4 w

7 K+ H* \0 K$ d. m% \: f
$ K7 `) n% g6 Q5 V" L$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel  {4 n8 t, _8 f1 K; [
Ubuntu/Debian
0 {- m: c/ r/ i3 L( T% d6 P8 ?4 P8 a  y6 f: Q

& l' T0 q' T1 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
! f# q# ]8 [2 _编译安装ModSecurity' X% A. U9 ]; Q
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。# [# e( I1 j* u1 ^

. o8 i% [" u, b( g8 C- |! v0 V" v
& f  U/ s7 M1 L; Q+ z方法一:编译为Nginx静态模块
, Q1 n1 y+ z( |. \% X) w$ E
( ~" s+ K/ h7 ?  c4 m( s5 N* K/ V+ e" C1 V- W$ c9 H& o! o
编译为独立模块(modsecurity-2.9.1)
7 [; V- N; o8 C' d( n$ tar xzvf modsecurity-2.9.1.tar.gz
# n0 U$ D) D9 `% n$ cd modsecurity-2.9.1/2 @  m# F3 h# @  D
$ ./autogen.sh) ?9 m% u/ u5 b) N& _  ?9 Y) W
$ ./configure --enable-standalone-module --disable-mlogc' S8 Y2 s2 a8 X# |
$ make! L  }0 g  e# r+ j
编译安装Nginx并添加ModSecurity模块
$ y. \7 T" \7 e& N& F& @$ tar xzvf nginx-1.9.2.tar.gz3 I1 {4 A( x& z8 s+ r2 ?
$ cd nginx-1.9.20 f3 Z# [6 V4 y1 C
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
+ A" B2 g- [( j  D$ make && make install
* M' n) j1 L3 [方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
) l* [- V9 S* s% T2 B& H7 c8 u" g" H, {4 |6 g$ W$ N7 Y7 c

' \% j/ |6 D0 g  C; j编译LibModSecurity(modsecurity-3.0)
- h. J/ H0 M. I) L/ d9 N$ cd /root) f) j! y6 S# A1 c9 e' O7 J5 a1 P
$ git clone https://github.com/SpiderLabs/ModSecurity
- b. x4 q0 m4 [$ k* N1 I8 p$ cd ModSecurity5 x, d, H: I) L
$ git checkout -b v3/master origin/v3/master
& s: T: B- v3 _0 }& j$ H' v# B$ sh build.sh
, D2 m+ [& s% Z) K. F: W$ git submodule init
' e4 K. ^: r$ s, {  `0 x& |$ git submodule update1 C, n$ B8 g, }' P* a: U9 k
$ ./configure
9 x/ w% T) d8 V- G' i$ make
  _- E) q5 ?& }& q) Z8 R$ make install
& K# ?7 O# x+ e9 _7 Z. HLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。: x" a4 |; e* T+ v  k6 I
2 i. }% `7 r" O  ~

$ o6 |* ?& Q& I% ~6 C& l$ ls /usr/local/modsecurity/lib
6 K9 L( R% c$ i8 ^: `libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0% D2 |6 i1 E. j
编译安装Nginx并添加ModSecurity-Nginx Connector模块
8 b2 p+ J& A+ g4 [3 X" _, h" s使用ModSecurity-Nginx模块来连接LibModSecurity
+ j. f  p6 y3 [; ~* W; O3 a, F2 H5 p, u' l
$ m. g# f6 D7 a$ |; a$ m* J/ @, d/ l
$ cd /root
; T( K4 b  u& o: h$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx+ u5 N: E" q( U4 U5 @0 n
$ tar xzvf nginx-1.9.2.tar.gz" m. l5 m5 J$ G* {
$ cd nginx-1.9.27 m6 M/ l' A0 n5 f; C+ Q4 ^
$ ./configure --add-module=/root/modsecurity-nginx
1 Z4 M. O4 e( k' Y$ make, J2 @, O( E7 C2 Z% h" a$ h
$ make && make install
8 ^3 ~: E) H1 p9 W( n添加OWASP规则
  o  i9 m9 a* rModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。. o2 c, I9 X( {" f8 E' f) U

7 H' O* c" |1 s# Z$ A+ B' t, A% e9 i
下载OWASP规则并生成配置文件
8 A& v9 P. {& |! p. K' |$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
4 [# _: [+ w. b* W( ~$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf// V/ @/ h( }& ^( C) Y) \
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
' i; {4 ]0 R/ Q; L+ m+ Y$ cp crs-setup.conf.example  crs-setup.conf
. g! u, e2 A+ {配置OWASP规则
( @' Q) T8 z% ]编辑crs-setup.conf文件
9 B( {$ i+ N# `/ k2 O4 M( p' c$ s# l' p6 N

( ~  W/ j) p' M/ o9 c$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
4 Y1 F$ Z: T9 {1 `- M9 S' M+ e9 K; B$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
2 U. u, F6 A1 N' Q- a4 K$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
' M& {- e/ w# p9 _0 V- R7 }$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
7 Y0 j" s! e2 a2 Z  e- G3 W, [' Z默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。! ]" ~* o6 u$ z; [% Y: a& _
4 Z; n/ r. }% l1 z( N: q
- L+ ?0 f' g4 g: |0 Z
启用ModSecurity模块和CRS规则6 \# L% ]: G. h! D, p  E% Y" q
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' W) D9 L1 o! K3 {

3 M( o6 N0 n8 ]! x9 x% ^3 U  ~8 q3 U6 _+ f3 c
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
: s# t% _: F. @/ A: V( s4 \% g# O# g6 p' e' Z9 ]

. v% [- L6 E) o( o2 E4 x9 e- Y$ cd /root/modsecurity-2.9.1/
8 R( y9 g# }/ I$ N$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
7 B7 o0 E# S( Q. {5 g9 ?$ cp unicode.mapping  /usr/local/nginx/conf/
: F- `  O- T3 `1 O9 x将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。* A1 p) p! c- T
; _7 @2 M3 @. _4 F
0 U0 G" d- Y4 ~0 i  {
$ vim /usr/local/nginx/conf/modsecurity.conf" o- v5 }3 f- c3 Q7 Z: G- d
SecRuleEngine On
. g$ y$ E% ~/ KModSecurity中几个常用配置说明:3 n4 U7 E7 e$ M" A( H" C

. H/ D7 t) G7 Z" \( l5 U6 l0 k
$ t2 v: y3 ]+ G3 @* D/ {" S" X+ U1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。! ?" c7 O% ~6 g1 N
8 q! O* ?1 I, @: {5 h( h( l, H. |/ F
( s1 @, b% K& e$ G0 M, `( u
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
2 B! K( T- S0 H( j4 ~( e  C( K' }

8 Z1 x  j3 @4 v% o; M; o; `" K3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
6 Q* G, w7 Z5 z0 B+ F, ?7 i6 b5 m" ]" l( S5 U/ Z( ?

2 a' f2 L4 s' R% R8 w3 u) E4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。7 I9 d) g1 U- h4 v/ L. Q

& y% r6 ]- R! `& `$ s: w1 A; b% V5 ?9 |/ e; ]7 q% [
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。+ Q% N- Z% N, O9 ~& U; o
4 E' g% P1 K+ \# w# m
4 u) Y) X; G* m5 y0 \& G9 ^
3.x版本CRS, V7 c" j! Z4 J4 x/ y2 C" F1 J3 O
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs  L8 X% f+ b+ _
# 生成例外排除请求的配置文件
: j, ~+ j8 o4 a3 t6 S5 Y. B$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
7 s: W$ P! G5 [* t& H$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
3 v: X" ]% }% P( {$ cp rules/*.data /usr/local/nginx/conf
5 V3 Y  d. m8 o' T! H为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
; [0 t* L' N- ~3 J/ R! {, S4 d. }; ], l6 A3 U
8 w% ~3 p! Z3 n- ^1 A' ?
$ vim /usr/local/nginx/conf/modsec_includes.conf
% r% j5 B; O( T) O2 V
9 K% B( o7 N1 d
[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
$ O  G+ u2 T  j/ b9 I) w1 t0 k

# f7 U5 e% J) ^) L, M注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
4 m. }2 P; B& w! M9 t1 R: F4 _$ N& ~4 U3 J' Q
/ Z" i4 |4 b5 M/ A! e) ]
配置Nginx支持Modsecurity
& B% ^6 [# Z* D1 O( k) v4 V启用Modsecurity) T* q0 r1 F6 C- b9 a
使用静态模块加载的配置方法
& Y- J0 b8 E, x2 j4 {2 N# S* Y6 r2 W在需要启用Modsecurity的主机的location下面加入下面两行即可:+ b9 ~' c- U# V2 ]0 n, A
5 J% A6 i- l. f& ]" |$ ^

+ ^9 @0 z) o  [3 w* S3 ~+ x' T/ iModSecurityEnabled on;- v9 e  d7 d4 O6 S; [
ModSecurityConfig modsec_includes.conf;
' x* E/ d+ s" o% ]  a( Y修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
6 `* ]/ \8 c: o, o& i" p
# B/ V6 U# C5 {: u( d( U! x# V7 I0 _: r$ d1 V* I
$ vim /usr/local/nginx/conf/nginx.conf, R* t0 j( v2 j2 m

+ @: S! {6 T, q& `, O
: |* g8 O3 Y! S+ n" Iserver {4 _0 l9 e4 u( j7 V
  listen       80;& Q# V% a) e, {6 H
  server_name  example.com;
+ _( o. A% L% g: S  G3 B% v0 _" c1 ?3 W
7 I; f0 R9 y1 R" o3 g
  location / {
9 w$ V& s5 ^  f/ r$ W% Z, E% L    ModSecurityEnabled on;
9 Q0 h% R8 C1 ~& L: Q    ModSecurityConfig modsec_includes.conf;* g' `! H; Y7 G! f4 b
    root   html;8 V8 y# s7 X" b. E) Q. i
    index  index.html index.htm;7 @! {" t, P/ K' ?/ b# }3 n
  }8 n8 p+ Z8 z8 {$ F2 x
}
7 T9 `9 }+ d$ R6 C7 I' M: j& A使用动态模块加载的配置方法( d! {* \; {- B$ ?- \
在需要启用Modsecurity的主机的location下面加入下面两行即可:7 b3 D' y- {6 u( E# z4 |! H
  I+ F( d( {0 C: n2 T$ T
3 W% H& ]8 I3 j
modsecurity on;0 p3 N. i2 |% L$ f# x' [
modsecurity_rules_file modsec_includes.conf;0 h) @2 N' _, T# X
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。/ y( Z* \( I2 O7 O/ u
2 B# U7 p8 U. Y' c- n0 M+ v
; R" Z# {4 M% E* q1 h8 v0 T2 o; L
$ vim /usr/local/nginx/conf/nginx.conf- v7 N* N" ~' n. A& ^
9 `/ S% j& G7 H
  L$ t. R$ x2 z. G
server {
, w* p! p& o6 ]2 Y  listen  80;
. p5 N0 J; d6 p# ^  server_name localhost mike.hi-linux.com;
5 N0 M0 H) R! h( C, H4 x  access_log /var/log/nginx/yourdomain.log;; y. d; _7 A! S6 U9 y0 Y/ Z

3 M0 R% ~2 B5 f8 Y, e8 X5 s) U3 }# S
  location / {
  {% p# s" A0 W* W; U5 i6 c# F: M4 g; d3 c/ b; [/ p& r

2 B+ p3 v- O8 U0 _. d3 T1 P! Y  modsecurity on;9 o% f$ \. \+ g6 E. d
  modsecurity_rules_file modsec_includes.conf;
1 d% d9 I6 i  H! t  F  root   html;( e* h6 q, q1 I" @4 _' s3 h
  index  index.html index.htm;# Y) O  l3 _$ y# c: {5 }0 I
}3 \3 D+ }1 s& ^/ \9 L
}
* y9 k1 m4 M% }% i验证Nginx配置文件  W0 o1 f1 S2 r( T9 n, t4 V/ [7 ~7 s
$ /usr/local/nginx/sbin/nginx -t% g* d4 U; l/ N5 M& S
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok9 r8 A  }, J+ _
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful6 U3 N  M( Y& T, K  X5 l6 J  f
启动Nginx; c- ?1 }' f: p3 f1 q& ?3 [% |
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
9 _. T+ a. x  B! X7 H
' q  h4 d8 i4 J" l& i0 r

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

- J  N+ n3 N9 C( g( d9 c

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-3-2 07:08 , Processed in 0.083198 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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