找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11165|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
0 `* n* M& C- M# `" ?: L' |! z( R3 ~  [3 {
; B) C, v3 ^4 V: z
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
& T6 Z6 {1 w8 e5 \* P7 T' h2 s6 g) T# n$ @: h3 Z, d0 S
9 A4 R) t+ }3 F1 ~- {
什么是ModSecurity2 ?, v/ I: t+ ]
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。1 t) B: E1 ?! v) }

' _. T. N* Y/ I8 a# n0 [  g8 o
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
4 {" T* f* D  Q5 G1 h4 z* J, Y/ S7 ]2 ?6 O( ?& }

5 `" i6 T8 g" Q; L# x: wModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
' K  [+ ~" B& m% K' k5 ?
$ r' a; K) b; W% }* W
" d( `$ R7 @" u9 O: o1 L0 ^0 e官网: https://www.modsecurity.org/; j0 @" o" i5 u- t. w
8 y: {( u* \# [1 e7 |- I( z

6 h' C( w. M1 |; v什么是OWASP CRS
; |5 s% l; P8 d7 k8 \OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。4 z5 v! f9 M, C4 |, p

- \* H. w. g" X% x" f/ m, b: Y6 k  [! g6 W
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。3 r. f3 Z) L# Q* _; O7 I

, x) A7 V- j  ~, J1 q& X5 A# Q
2 H" r& `0 c. r( t7 PHTTP Protection(HTTP防御)
! A" o* w5 P1 [1 N1 IHTTP协议和本地定义使用的detectsviolations策略。, m; G0 d  Q3 E: i) X
, o" \( {+ l  [2 y7 n3 `# U
7 E8 m( u& j: j
Real-time Blacklist Lookups(实时黑名单查询): Y( W0 A. N  N( ~; o( Z6 W' L, R
利用第三方IP名单。
* D6 g4 @% S4 p3 l9 g  q$ j7 d
; L( M, I0 |' x% V# R7 Y8 |" U6 n1 g
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
$ W7 |: F1 D2 P+ X防御HTTP的洪水攻击和HTTP Dos攻击。
4 }/ k6 l+ t% G4 X! E) V
5 M; F; t: Q$ N4 x& \& A
& S& `1 F, I) n1 {8 BCommon Web Attacks Protection(常见的Web攻击防护)
1 \) c4 _5 v/ b1 Q/ k检测常见的Web应用程序的安全攻击。
$ I! U5 y5 _2 K) f8 N5 _" d1 H9 t) m7 q4 K
0 W5 m1 ~+ `6 k' A6 v
Automation Detection(自动化检测)3 h2 T% i1 n5 Y4 L
检测机器人,爬虫,扫描仪和其他表面恶意活动。% ^5 W& D+ v9 b+ g" R! z

0 H6 w3 [$ d/ u+ g. d; `) o) V1 D% ^) W
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
% ]. X, o. W4 u. x$ r6 k检测通过Web应用程序上传的恶意文件。0 w0 _1 D. ]; \
6 E! ?# f, x& @) Z4 J
) c1 @/ `& @$ {! I
Tracking Sensitive Data(跟踪敏感数据)3 ?3 P4 I) k4 y( K
信用卡通道的使用,并阻止泄漏。1 ]5 Y4 y' T+ h0 K& F4 n
( T  `# F& L# I) x

& N* Q( s) K+ _0 X% U4 q& }3 `# @& XTrojan Protection(木马防护)$ W  `4 c6 k$ Y. r1 n( K$ @
检测访问木马。( l# z0 I6 d) g; O
  Q4 b2 W. @4 d

3 r* X6 ~: {5 X, Y' rIdentification of Application Defects(应用程序缺陷的鉴定)
. e/ W  m2 \( ^0 x- v: j/ G检测应用程序的错误配置警报。9 d4 j6 b; o) T" F/ o  z, G' H: x
3 E. P* Q4 a; O( X% f. G7 Y

  J6 k8 L0 u# Q  E: n; OError Detection and Hiding(错误检测和隐藏)
8 h0 f6 x1 p8 v检测伪装服务器发送错误消息。
) w; v$ H( V& i* I1 m- G) s- I% A0 U" ~3 X" P5 n
8 k- m4 \+ f* {5 a* v
安装ModSecurity; _: l) ?  `1 A8 C
软件基础环境准备1 W& b: X5 {  d! i5 D8 _7 A9 @" L, f
下载对应软件包
& s& V3 x$ m* p$ cd /root' F" m' l0 X# ?' ]  h+ e' B; I
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'6 d8 c! Z$ \' I' B# T. Z5 t1 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
2 R6 j+ z5 w2 M- z安装Nginx和ModSecurity依赖包
$ B, p. c$ \3 a) b+ fCentos/RHEL/ v6 Z6 r* e: c
; j' ?& _2 d4 T+ X. [3 ?3 i

8 C) u; n9 ]8 N+ k5 E5 G9 r$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
' i  X" [1 i2 Z# d7 NUbuntu/Debian$ L2 o6 i' C. s1 u( A: _

" k% }/ |$ W( _* I7 ]
% d) U0 I  o3 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* t$ E* ?# N# \# t. k9 v6 B
编译安装ModSecurity
  T* u* N2 }9 ~Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。0 i* m& O4 B9 T0 J- S/ o% N+ g# x

4 v9 v. n0 v6 l! z0 a; T
* M/ k' R+ e0 h方法一:编译为Nginx静态模块; W0 m$ N8 T; y; y5 A

) `9 ^, U: `) v2 J: [! _$ ~7 T0 i
' y4 O: k' r) f( g3 |. i编译为独立模块(modsecurity-2.9.1)5 S4 m( @7 H# Z3 J% K
$ tar xzvf modsecurity-2.9.1.tar.gz$ c1 n) `; k9 z3 e3 h% _0 {
$ cd modsecurity-2.9.1/- r6 m( W6 u* G5 p) z
$ ./autogen.sh
* m$ a5 y' |/ a$ ./configure --enable-standalone-module --disable-mlogc5 i  ?2 V* {# M6 K& s9 w
$ make7 G: o# F! `- Z: m
编译安装Nginx并添加ModSecurity模块
' C( r7 ~  T9 r7 s$ N+ J$ tar xzvf nginx-1.9.2.tar.gz
6 b$ s( o. _' }) R8 j& R$ cd nginx-1.9.2
4 `/ v& K$ f$ ?' I/ v$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
" K8 Y. Y$ v# }9 s$ make && make install* n* v# m% |/ i/ N9 @) o
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块7 x2 _7 `8 R8 d7 N
' K$ S$ }3 [: A

* f0 Y. k4 v( L8 J$ }5 _编译LibModSecurity(modsecurity-3.0)% @9 z9 X2 Q6 a1 p
$ cd /root
; Z+ [3 I& ^1 V  W* D% _8 g! N2 d8 f+ ]$ git clone https://github.com/SpiderLabs/ModSecurity
1 z4 _  C: Q" I' N8 v3 c8 L5 P$ cd ModSecurity
6 F$ ?8 u7 p2 y  a+ N" B$ git checkout -b v3/master origin/v3/master
, g" t; T6 |: x8 h$ sh build.sh4 A' Y- y! N  }6 ?( z) Q
$ git submodule init% J( F0 D/ x6 B
$ git submodule update
3 y* I- U0 g( y6 t$ ./configure' Z4 G6 I0 C2 {6 V& y
$ make
# `3 E. S- u( m! g) O$ make install; }+ C6 O1 l# I3 O
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。- k/ _; N+ C* g9 O8 G9 h( R
# G9 W: C# {: s6 X) I
1 D: L! T9 r# I" h. I; ?% w! I' q5 r7 M
$ ls /usr/local/modsecurity/lib) c' X( Q  t  z, L/ f! s
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
6 x, p, `6 w$ c7 o% f6 ?编译安装Nginx并添加ModSecurity-Nginx Connector模块. {9 ~2 W$ d$ W  J. k+ r
使用ModSecurity-Nginx模块来连接LibModSecurity! Y+ \  p  B: p4 i; b" L& M

6 F( ^$ e' x% i8 Y9 C# a1 I# a
$ cd /root
) [. c2 z7 A+ q% e1 ?$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx# ]- g: L" I4 H% C& O
$ tar xzvf nginx-1.9.2.tar.gz8 E: i; V; p4 E
$ cd nginx-1.9.2
7 t- s8 g. c$ T" k' `$ ./configure --add-module=/root/modsecurity-nginx
& |: Y; p$ y3 D2 b$ make
7 |" w# h2 C# y( A" y$ make && make install
$ g, h7 P5 i& T4 n3 ]添加OWASP规则, o$ J8 A$ ^8 e
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
0 d. I( p" L. @- l; r7 {9 G, K
# m$ l/ a0 P3 `- h4 K
下载OWASP规则并生成配置文件
# C* E) D" T% v( K$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
2 d# W5 ?# d- q. \+ o% T$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/9 {  A5 G, `$ C9 b9 h: |1 Y" ~
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs* h/ U; o* g3 F' Z" I5 m
$ cp crs-setup.conf.example  crs-setup.conf
2 a! u: ^+ h& O+ x  u" w& d配置OWASP规则9 Y" Q3 ^, w0 p; F. a
编辑crs-setup.conf文件
$ A+ h" F/ c  h6 \' y! W9 H4 V
7 u( ~  X: I( q1 N1 F; s* n. D' b% k0 D) f( I& _
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf3 L/ ~( b$ W+ q+ H
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
7 Z" S4 F$ |8 v* o0 ]$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf4 l9 m# K8 b: _/ ]) n4 S
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
6 P; n$ A2 u" A9 Q: O9 d' i! S默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。' r; c# q1 q, m5 y% N5 m
& b% R) ?' N5 U  v  b+ C
: b$ M+ s0 F' Q7 Q% o7 N
启用ModSecurity模块和CRS规则# n1 J. W+ A6 J/ i* _0 p
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
- c; T7 c( @) [4 Q$ n) [: c  E" N9 ]4 x

. E3 K5 d$ Y+ J9 umodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。8 B( c6 }) P- z
) F3 d5 A: t. Z2 A4 W

: g2 e  {7 Y" Y; X% }$ cd /root/modsecurity-2.9.1/0 F) S1 I) @8 Z9 k3 v
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
5 Y4 c& ~) D8 [$ cp unicode.mapping  /usr/local/nginx/conf/9 v8 O. K$ l1 g0 g( i
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。3 I+ }' |/ ^5 Z* J; ~% L7 Z0 z# b

# c% w# U  T3 }: B, t
8 v; ~; U3 u4 ~' {$ vim /usr/local/nginx/conf/modsecurity.conf; [+ s3 f. W( b! q+ N- T3 V# y/ S' U  O
SecRuleEngine On
( E$ D" Z- V& OModSecurity中几个常用配置说明:$ N' T( s: o: b
) s- d/ O) B! A8 a9 g- [. W
5 D. ]5 Z) g+ |( G/ z! _
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
0 c; N1 d- K- E# z6 x2 A8 A  w4 ~1 N' p7 f! d3 g( {" G
  f) F. x4 Q& Y
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
) e, O: w" f$ O! q: W
$ y  A9 t. y7 \6 R, `
: x+ }, X% S1 L3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。8 A: ?2 d6 Z2 @! D5 v# Q: j9 F( k

! e# I7 K8 y' e) l9 |6 s: R. q
. R5 O( S: \9 j' F4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
! w: q) O/ E& G( i, s3 E
- c6 }( k; |) g. Z% x4 T/ @5 w, z, n8 g; h. K0 W7 d4 M
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。( ]% F% C, Z$ }1 r

( U5 Q4 U; R9 Y: T: @) _
5 d. C2 Y4 |! Z3 V; D' }- E0 ]  a3.x版本CRS
/ @, o9 Q: T4 N; o0 M  |$ cd /usr/local/nginx/conf/owasp-modsecurity-crs6 \% |7 C6 o' X
# 生成例外排除请求的配置文件
- X7 ^$ t& V9 p1 \0 l8 j$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf2 D% Z0 V, t5 \, P( n
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf2 r3 \/ `: h9 ]) J+ T0 y( Z
$ cp rules/*.data /usr/local/nginx/conf
( v2 ^5 s1 d/ n6 X' C% ?- u& O为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。( |6 a6 L/ t1 o  i
0 p% `! D/ l, v& X( e& e% E$ e( {

. h- Z# ?. o6 x. Q$ S) p$ vim /usr/local/nginx/conf/modsec_includes.conf
1 j7 ]) |1 U( }8 H$ C2 g, t
' H) E8 U  S0 V1 c) I
[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

2 ?2 P1 x! c! \: ?; C
" @9 j8 U5 {, R* b% m注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。( _, f) f2 _# b/ S' M9 K9 k

8 }8 q' g' r0 i: x  @2 I
) z5 r+ ~# }/ m1 s. q7 a配置Nginx支持Modsecurity
7 {' o) o+ e5 i, v2 y; C$ }启用Modsecurity) b) I- d) v) w# g6 e3 C0 P
使用静态模块加载的配置方法
6 N; u. a  H0 N+ r2 x在需要启用Modsecurity的主机的location下面加入下面两行即可:
" O; E7 \  E/ P4 c# X% I3 t" W2 k$ }1 e- n6 U* E0 ^6 h

, m, u  [5 k0 Z' H/ }' iModSecurityEnabled on;  w( f5 x7 T% n
ModSecurityConfig modsec_includes.conf;7 _8 I* R. P( S2 I( N& l& ^0 U
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。. s/ }% ]& T3 B, O$ O- S  i
0 ?8 I) Q, \1 T! Y0 v
+ K& n2 k4 l4 o0 u  i/ H9 K
$ vim /usr/local/nginx/conf/nginx.conf
0 J# ]- J" h/ V) @4 k
* g7 P9 N! n3 g3 K, X+ R5 K& \8 L6 A: W7 Y4 B: g1 I. g: y  _+ H
server {
% k4 U" P. x/ `* j# a( U# q  listen       80;
' R; _6 x! A. S+ \/ n9 f  server_name  example.com;% T6 s) p& U% N2 v, c1 y) h2 [
" P6 r$ y( {, P. g
+ c$ w) c. {3 V1 C. e7 E4 g
  location / {
! d# W' a; X' L6 A9 E2 Q/ u. y8 [6 b. w    ModSecurityEnabled on;0 o$ w$ t  O7 _5 m& K/ h' x
    ModSecurityConfig modsec_includes.conf;1 K6 @1 h5 |. I( c6 u
    root   html;
* c- o9 T  X0 C$ Q4 M0 ?" B    index  index.html index.htm;! F# e% s) @$ ^0 L- J/ Y
  }, g  _$ }! \$ Z  E7 b: e
}( r/ o  V7 J6 G5 F
使用动态模块加载的配置方法4 z9 i% }, Y( \/ x5 h( |
在需要启用Modsecurity的主机的location下面加入下面两行即可:
& ?% H( T& r/ c( ?8 R( |) _! B% [# R% H# F( Q( m6 `# G7 }

/ t7 I/ U+ s5 Vmodsecurity on;
% C3 }% p' Z( Rmodsecurity_rules_file modsec_includes.conf;
" Z( X8 |" z- O; i" a- m修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
# [/ c0 C; i, Q- G/ W- g# W4 u- z3 W- k  o! ]
9 w& Q* K6 ^2 D
$ vim /usr/local/nginx/conf/nginx.conf( u: d1 q$ D! V
" |( L# N/ D2 E3 I, S4 J
1 _( q1 ]. I1 B* @% S9 ?% U
server {
# D$ z" A, Y2 }! c  listen  80;  s0 Q* O! ?% p9 r" |: I/ O) y
  server_name localhost mike.hi-linux.com;" s- ~2 O) |% C& Y- g; w
  access_log /var/log/nginx/yourdomain.log;
; `- S# N% _  E; g: z6 t
# @& h& g$ ^7 ~0 g) y  U% [: H. o1 E" W4 k6 B) t1 g% }
  location / {
( {/ t# Q8 ^* A& U6 P4 W7 @, A. W2 g+ D4 R# P+ m/ c9 U

  j9 S* O2 F- S7 D5 Z8 I4 a: o  modsecurity on;( D3 m; r+ ]. K2 q# X+ Q6 i
  modsecurity_rules_file modsec_includes.conf;
& n( j- V# Y+ u% [! D  root   html;
; [; G  W0 S8 D, N/ [  index  index.html index.htm;1 I4 }4 ?( ]+ }7 F- L0 g
}& t; W: _- e+ ~( m# H
}6 \; ^2 D8 P  [8 j# B
验证Nginx配置文件# E2 p1 L, t3 B& b2 X; U
$ /usr/local/nginx/sbin/nginx -t, l7 T5 i8 z  q" c! ~6 Z
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok1 M2 ?3 B, a/ a9 O8 a7 b
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
! E) y5 _2 w4 P启动Nginx4 u2 }  A1 k4 U6 a
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf* C& q2 Z$ Q# P/ P9 ?

$ Q% b0 N7 g& ~' \

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

, v: P# N1 u5 c/ E, I8 e$ F2 ]: r

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-5-13 19:32 , Processed in 0.102547 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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