找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11254|回复: 0

利用ModSecurity在Nginx上构建WAF

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

( @' M5 a7 E5 a8 G: L- X# _" C6 \1 [# c6 ?$ _  A
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
6 @' f/ L$ W8 k. P& ~% `
9 `9 P, U- V' ^: n6 C+ Z" Q
3 F) M4 K2 S) e5 f4 k9 b8 n+ u什么是ModSecurity) R2 q  C- F, z# r  Z- ^+ U7 R
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
% T7 ~( Z, m: B7 z+ O3 M3 {. {2 G3 F8 s' n% D! V# u
* f+ R4 h' O3 [9 D& {4 x
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。4 @+ _3 W9 r5 r  Y) E2 s7 W

3 }# X& U; |6 H: j/ F1 v& r4 v. I5 h; Z' ?; G
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
* `: o# I* l1 m$ {6 E$ V! K' w: t7 V  b1 h: ]5 Y

- c% P$ _3 l/ J& R官网: https://www.modsecurity.org/
4 `& N+ w+ T7 H7 p, O7 m2 [; |# m7 g2 d" r

- o5 S' I! h& |! t什么是OWASP CRS# M6 n+ H$ w- B4 c9 u0 Z' r
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
8 K' O4 c% J+ u5 S0 s; ~( C1 O
; s, k- [0 s. S4 d% @# Y9 r. V/ D( v9 i' u" ~
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
0 A9 F- K- u0 \( H! C0 M- `5 ]& D3 E7 X, d/ K  J6 t, g
2 N+ d" T% R: F& n* A
HTTP Protection(HTTP防御)* m. D0 G! b! N3 U# f0 W
HTTP协议和本地定义使用的detectsviolations策略。6 _1 W! F8 D8 m  b: X" `

7 l9 o$ _2 n& O* R  A: t4 I' X5 Y
/ G4 x3 I1 W6 e$ k% `Real-time Blacklist Lookups(实时黑名单查询)! `/ _6 @. Q1 f6 b
利用第三方IP名单。' h; |3 T# U, K3 ~/ m4 u7 ~3 _
6 C, J8 @9 G" v5 w

% A2 ?# |. x3 lHTTP Denial of Service Protections(HTTP的拒绝服务保护)
' a  H) ?" _& j! [3 o; [防御HTTP的洪水攻击和HTTP Dos攻击。
% i& R: {% z! G' S0 G) P3 C
$ \0 ~7 l' a1 P2 K5 s* P  s( \( P+ U+ i
Common Web Attacks Protection(常见的Web攻击防护)
/ h, H* g: t7 h6 X% e" j检测常见的Web应用程序的安全攻击。
9 O* S; ^7 G2 [- }/ D1 Z* H! G7 ?/ {; l
4 N7 R5 r& H: r: b2 |9 j+ B; t3 Y* c
Automation Detection(自动化检测)
5 R' F! }8 Q+ x9 |: `检测机器人,爬虫,扫描仪和其他表面恶意活动。4 a* i' G- n" P/ v9 F) Q8 n

$ C3 c: M  R1 Y
3 a5 W2 ^0 @. R/ @! gIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)* G: x  q* }, P5 h( }
检测通过Web应用程序上传的恶意文件。
1 O, a# }6 y" \6 B! z( \' F
) |9 P; x, P9 b* ?& _0 M5 l" J; u' g% F7 t4 x; z( Z3 d5 r2 P2 L' M
Tracking Sensitive Data(跟踪敏感数据)
% y1 j8 H# M+ ?/ k5 W) l信用卡通道的使用,并阻止泄漏。; h( u7 y( B, m* x  q
3 S! p, T; b7 j/ X6 X, s6 T

0 h" ?# E! \/ {+ ^1 Q4 \Trojan Protection(木马防护)
: U% r- w% ?. o8 K- e检测访问木马。
4 h! e" |% x7 L. x* ~/ u
$ k7 x& }% c8 J6 f" s6 p) m- D; \! X7 E: M# M
Identification of Application Defects(应用程序缺陷的鉴定)/ p  L( l* N$ M! X# O; V; E
检测应用程序的错误配置警报。0 F9 T% Z& O! V+ I
! t  P& _) y9 O
1 B, z* o4 {( q9 X
Error Detection and Hiding(错误检测和隐藏)2 A7 J9 d% P$ o; m: z8 F' {) x
检测伪装服务器发送错误消息。; F  c8 n0 l# ?- T
; E: A0 D$ E: s* `# C& x
& k" T+ G8 j* u
安装ModSecurity
. b% O/ }( b  _! t) A4 Y/ r软件基础环境准备
2 k( a$ e+ Q2 ]; k下载对应软件包
. T. }+ V: A2 C$ cd /root
5 F9 [: l' m* {6 ^) z5 H$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
. b; G% T% k  n! m& u$ 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 N" Z% E7 x* |& W/ p安装Nginx和ModSecurity依赖包2 L! f) \' {0 A8 n/ B
Centos/RHEL
' c% N% E1 g+ R8 v/ \6 X- I6 U& G# g% C$ D; Y; q3 |3 Q

3 C! i/ u( G* w$ r% [+ S, r$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
/ ^0 M( w. i, g/ D2 G. iUbuntu/Debian3 t: f1 K4 p1 X" h) `* G/ J
+ y2 R8 K2 N' c: k" t/ p

3 b% Y9 g1 }& r3 @$ 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
! y7 [9 _+ ?: y; u. `* W8 b编译安装ModSecurity! B0 h" @1 v7 p; S: J, L3 S$ F
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。: T/ ]# ?& U, \3 s

7 J$ J# @# d& ^6 v3 c
, K; a5 p0 I& W' e8 q方法一:编译为Nginx静态模块2 g0 i2 r, @6 K4 K+ _

, T+ W- U/ H" H. N" _
& x  R$ o" i7 b( \( H编译为独立模块(modsecurity-2.9.1)+ |4 K' [  t; f0 r. ]( y' K) S
$ tar xzvf modsecurity-2.9.1.tar.gz
5 D2 `& w; |; x$ cd modsecurity-2.9.1/- K6 E5 S1 f( C% M
$ ./autogen.sh8 W/ [( r; z: Y- ]; q7 q" w
$ ./configure --enable-standalone-module --disable-mlogc! k5 m* B' e- C; F- G
$ make# s) U  ~4 H! W, o6 Z
编译安装Nginx并添加ModSecurity模块) U6 Y$ @# D0 a3 s7 q. k
$ tar xzvf nginx-1.9.2.tar.gz" v2 B' }) Z4 m  Z; S, e
$ cd nginx-1.9.2
  q- h' R: J  t- i$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/) I" Z2 g6 H* V& \  x  O
$ make && make install0 x  S/ K% N1 j* o+ W; g( Z
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
9 v# ?  ^0 h+ A0 D) K3 L& c* n! m" @. R+ Z  t' s3 K

$ M3 N+ g8 @! v/ w+ D* ?编译LibModSecurity(modsecurity-3.0)
* C8 u+ v+ }) N9 P0 t7 `  s8 H, W$ cd /root
, \# U, [: C3 S: A! i- p  }0 W$ git clone https://github.com/SpiderLabs/ModSecurity
$ Q$ `' f! n# {% w; y5 h; c$ cd ModSecurity) h) x0 `; V  s9 O: q2 J8 S
$ git checkout -b v3/master origin/v3/master! `9 V; o8 Q/ [5 S% V/ |
$ sh build.sh: X6 A8 K& H! L% i4 v9 G3 E8 g* }" V; l
$ git submodule init
, c2 V: I9 G/ i$ git submodule update
. @  C0 u5 J0 S7 E$ o! m$ ./configure
7 y+ \9 B( r  N: j- r* u$ make+ e$ E  `- A, i/ B9 ^2 S. V4 Q
$ make install
9 p4 f' u; j# l1 `& R- s6 q8 E( }. K" GLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
! K3 r: N# X) a6 W
; d+ o7 |: L* a3 K7 y! v& e
5 F$ b0 `0 {" G4 O$ ls /usr/local/modsecurity/lib
+ U3 p7 T6 t5 e" ^libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.09 O0 i( p: V1 _
编译安装Nginx并添加ModSecurity-Nginx Connector模块! u5 V! d7 o* p9 ^$ S$ s! `
使用ModSecurity-Nginx模块来连接LibModSecurity
) n7 a0 r& x: D5 ]9 @5 r+ _: s7 p/ \4 ?
7 ?2 [6 R/ S+ a2 j
$ cd /root
* Y6 u) @; K( p' _# j$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
! x1 n, Y; x8 r  }, m$ tar xzvf nginx-1.9.2.tar.gz( |5 @* x( }' g  Q' Y- e+ ?' c
$ cd nginx-1.9.2( Z& \/ O3 Z! s
$ ./configure --add-module=/root/modsecurity-nginx
% s' g( A. c* w4 B3 D$ make, `6 V; b$ a+ F. @/ G
$ make && make install
3 w# h7 \6 c1 _# {& \添加OWASP规则
, Z. S3 L4 l9 Z. h$ }* E- H5 A% EModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
8 h* S3 u2 t0 s. L' T, ]& |
  g+ J/ V2 |) o+ W
/ _" g1 q8 k. q/ q; Z- y! M' M+ ^下载OWASP规则并生成配置文件
# K6 S7 u2 a3 E- {; Y$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git$ ]1 Z& x7 N- o8 w+ E/ x: U
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
, `( j' J& C" H4 K/ p" B  ~9 V/ D$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ x  [0 ~/ L( p: ?- H3 ^+ ~$ cp crs-setup.conf.example  crs-setup.conf& X' |! A; w/ H
配置OWASP规则
! ], ]8 E8 c& m% k编辑crs-setup.conf文件, @5 d( l  t5 w1 D! }
2 s' l+ E5 I7 d) ?, q+ `

; `* V4 V3 _7 V( P$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf8 x9 h- G& r0 {0 r
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf5 _' N$ T( y6 I& k1 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
, `/ e* x; Z! `$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
! C& J2 N0 B* y默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
6 l' y- b8 u3 v. Q! D; o5 \
1 s1 w: v5 r' H4 c8 I0 f  p7 p8 W
% j; g4 |4 y4 |& n" m启用ModSecurity模块和CRS规则
3 `0 ]- A4 c2 V! m+ z/ L复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。0 H" z! B# ]  M# V0 V" v% x& a4 i
+ O" H9 \9 E! i( q; X  L

* z- L7 v' X9 n% M0 H, j& xmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。7 w$ e6 C0 h1 J! B% B+ h

+ |/ |, ]/ i$ H# I; \1 |5 w, J6 |% I' d# U5 ~
$ cd /root/modsecurity-2.9.1/
! q9 @" u. C6 D, Z7 _$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
+ b, J$ E% p3 y) F$ cp unicode.mapping  /usr/local/nginx/conf/
: E" Y' L' v& p1 S: M' ?$ U将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。  Q' r4 x4 ?% j* U) \8 R
: d- ~) D, t' n% g6 o: g4 H
$ ~8 m4 L$ ~- e
$ vim /usr/local/nginx/conf/modsecurity.conf
2 d, s  H* t# O, m, ASecRuleEngine On
- ^1 Y0 o0 d! M0 ~% iModSecurity中几个常用配置说明:
. I0 i' x& o+ |9 J* j9 `- _9 t7 D* T
2 M  W9 x: ^7 v$ v3 R8 C6 C( b  h" S; I6 e
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
4 u: e2 i* h$ ^& N# D2 l5 z/ d  V3 O# k: t! A9 v
  i3 o; p# s8 X6 k8 |
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。; B  z* y* |3 ^3 w0 g1 T
2 Y8 s1 `% V  U" v/ j

- z2 y, x0 h* p: N9 D/ Q3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
7 g: C0 `, y+ o
0 X# T$ S1 J3 r# J& T7 N5 Q' M! U
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
2 ]# Y+ \  z* a$ P" @- h+ A) m4 y: d& U

5 c; _; I) \. A4 {& J$ K2 B在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
# L1 b1 S( B# V% H  `4 O+ u
1 B! f. r4 |4 V2 U% g7 K5 x' D  o% |/ n: u6 D8 @- q: K# k
3.x版本CRS9 \3 r! K, ?1 p% y
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs! B# K$ U5 {" z3 \3 \8 k) o
# 生成例外排除请求的配置文件/ v& O- V/ o9 C0 h
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf3 ]* q  a0 F& I( a
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
% E% N! Y1 r/ N5 L3 ]( y7 I; r$ cp rules/*.data /usr/local/nginx/conf) H5 ~- V, a" B3 w8 A- }/ _
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。6 w7 A9 z- |$ T0 V7 g& F4 d

/ r% T( o" g9 N) c9 z2 X6 a) \2 }% P! \
$ vim /usr/local/nginx/conf/modsec_includes.conf$ s. ~3 ]4 R; ]! r( `
4 o# \& Q2 x6 D- H6 S
[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
8 ]3 p! J* `5 F0 M! u
+ m4 i9 S" B% ~, C7 @" u
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
1 I- [7 D9 v4 Y0 Z. V- ]& _) H) ]" s7 J

2 v2 u7 d" s! b* u* h8 A配置Nginx支持Modsecurity
6 d" z, d' m5 x  s7 i启用Modsecurity4 u, ?  \0 d/ {3 P* p
使用静态模块加载的配置方法7 p) o' b/ m) y' \7 i/ x
在需要启用Modsecurity的主机的location下面加入下面两行即可:
( q$ e8 K- \6 y% @1 R5 N; C: {9 i) \& ]

0 s, _) ^- U1 d8 x  mModSecurityEnabled on;$ [. `  {4 P6 y
ModSecurityConfig modsec_includes.conf;
6 o# B% ~: j" q: w7 y6 l修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
- I5 z9 T' C9 J: r8 ^, t* d  j4 u6 \5 x8 J( Z+ v0 ^- m- e
% \" _" n0 {' X% _) x1 ~) ]
$ vim /usr/local/nginx/conf/nginx.conf* P* _/ F3 N; H5 u; o; ^  r+ k

  K' Y, e$ Y0 R  m  a/ N
1 H" X3 q+ w: y! Y6 B4 Iserver {9 d3 p+ r6 Q* P; b5 S+ [! A1 P
  listen       80;& B( u$ g& L( Y; R! l# S" _6 l+ d) B
  server_name  example.com;* R0 f" F- A0 l* m

7 p9 V5 s1 A+ R7 m8 E. f
- e5 G; S7 n! n! ^  p5 _' `; o0 b  location / {' P, b% c. U+ E& ^. L
    ModSecurityEnabled on;5 t# j! a7 R- R  k7 r6 m
    ModSecurityConfig modsec_includes.conf;* P$ X! b6 `/ W
    root   html;
& K- O0 ^% p/ I) l    index  index.html index.htm;7 u" k* C- u1 _: \( w2 R+ N1 V3 I# e& c
  }) ~1 [8 j: \1 e9 i# |" }
}) L1 b4 b, J/ b. I3 Q
使用动态模块加载的配置方法
0 F/ U: [5 F0 N" f' U3 C在需要启用Modsecurity的主机的location下面加入下面两行即可:
2 {# C2 t2 J$ E8 ~" Q; l) r/ `, M2 N5 F% I: O2 Y  C
5 b9 P# o( S1 b, [  v- C2 U* t6 \
modsecurity on;
8 I& v" a/ [8 x3 G. Vmodsecurity_rules_file modsec_includes.conf;$ _4 d% i2 h9 W' g; h* G/ `9 v
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。6 j# l" n# K( q( H
- U; S9 s2 u4 S( M/ G4 i4 E

$ t" b- @: w' o8 Y$ vim /usr/local/nginx/conf/nginx.conf
2 u3 e- c: }  E! j4 R" R- j% l& q2 U
# I5 q. q  L& Q/ z! W2 r9 D6 G. ^8 t+ o9 V
server {: u1 ~% a& _6 H5 d6 S$ S
  listen  80;
9 P! s/ o2 H" B. S: u  server_name localhost mike.hi-linux.com;
' m6 s4 Y9 h$ j" @  access_log /var/log/nginx/yourdomain.log;( z& v5 \. H7 n# I" {8 F) O

* j; Q8 t- j7 X1 p3 a! T1 S6 G' {+ w- J/ R1 Z
  location / {; I* @4 W$ n8 ?% n* P4 y8 g

, u9 [. L  t& t) u
0 j' N$ }, {% C2 ^  modsecurity on;
+ w( o5 k' a' {3 J$ H2 g  modsecurity_rules_file modsec_includes.conf;
4 x( c+ I0 y3 m; h6 N  root   html;
" Z. S+ v) {, D" t9 p  index  index.html index.htm;
% ?2 W% v* m0 A* J}
5 e  ]% z& }- B3 Q}4 \0 P" g, c9 \/ m7 m+ O+ a) J
验证Nginx配置文件. J* D) F  O# d- M( B) r. C2 o  [
$ /usr/local/nginx/sbin/nginx -t
% M8 c& k* b  w2 v4 V* Tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
/ u) u( S# U. [$ g$ I# _* l$ unginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
2 M1 l0 G& E7 L1 @启动Nginx
- A3 v- K; n  ^% W0 {; S$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# [1 q' j* ]& m" R7 Y+ I$ h8 J2 V. Z3 g: w0 u& ^0 t1 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能够战胜更多复杂/未知/混淆的攻击模式。

, L2 b9 U5 ~1 A" B6 v& h# a

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-5-22 17:27 , Processed in 0.098714 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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