找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10136|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。! T# t6 N, V  |  v6 D
% d) Y1 g9 a- c# d- N8 R+ k* |
" U% |1 {) q' B
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。1 h/ I' H' T+ [7 t" I; z
0 e: g  O) z/ M

6 E% H! k0 B1 ]; C什么是ModSecurity6 r3 B! c  n0 X$ w
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
- F1 X7 y" D4 |7 I) l8 n) ~6 l  r2 [5 s& j

& I6 P* {* H* J: ]ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
; y  f  u9 D1 k% e; q5 V, x) F/ O+ Q) I
0 h$ m+ w/ ^- H: r0 y5 f
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。4 v- Q- |6 J- {4 L- Z
4 D% r' C5 x; e1 ]( a( ~. H: N
  R2 d$ D3 n" L. s$ y5 o0 U
官网: https://www.modsecurity.org/
, R0 n5 d6 ?3 u% a! {8 o* K, r0 D. ?- z

. I  g- [/ ?& V8 G4 h: ]6 T3 ]什么是OWASP CRS8 p  j6 ~0 ?3 g1 ]& `, |2 ~" T
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
. G2 T5 z9 S( r: k7 E$ B: U
( M: |, o0 }# C- Z$ h* D/ a( H
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
8 V: i+ T5 r7 v
  {, L# F, z& q8 u$ K8 }
* _+ }' l. @2 j* kHTTP Protection(HTTP防御)
# Q  V8 z' e* R- s8 FHTTP协议和本地定义使用的detectsviolations策略。
( N: W& R3 r$ {/ q- V8 W4 W4 j5 C) C

6 P6 u5 t/ u3 y( D: I4 mReal-time Blacklist Lookups(实时黑名单查询)
* W0 P7 {$ m1 f0 L/ F利用第三方IP名单。
4 }* N8 s: e7 w  t7 A: G
+ B% C3 w. F+ C( A3 p! a
* V  q" W$ k% R7 d2 L  |HTTP Denial of Service Protections(HTTP的拒绝服务保护). Y6 u. p4 ?* A& s  q5 A
防御HTTP的洪水攻击和HTTP Dos攻击。/ N+ ^3 ^& a4 P/ R. P7 }% ]
6 T+ A9 V! ?) H2 L9 I
4 Z6 K  w$ N0 c/ R/ w! o0 j
Common Web Attacks Protection(常见的Web攻击防护)
" s( l$ n/ N8 j7 g* t检测常见的Web应用程序的安全攻击。" s! y0 J& v1 k# `

' T4 l+ H5 r- }' d; @# A& y! [' S9 ]6 C; x' w" m
Automation Detection(自动化检测); T5 M1 X$ b* ~
检测机器人,爬虫,扫描仪和其他表面恶意活动。
7 e! L3 X! w4 l" z) y. Q& q1 R: Q. |& v/ s( D" B' e
! Y% Q# i- Z- b+ x" j
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
. _+ P# S9 P+ {1 p6 w# d检测通过Web应用程序上传的恶意文件。
, _$ u. v% E) t3 ^5 k8 b) ~  a' Q2 r1 z. P4 l/ j

; Q+ u# m0 P0 F1 c! |Tracking Sensitive Data(跟踪敏感数据)
$ n: P- H* w6 ]信用卡通道的使用,并阻止泄漏。
5 `- g3 t7 b/ C# j+ v2 [7 y3 I
# o0 Y% O/ l& m% l. n* P$ d6 Q8 @. f3 J4 F4 L
Trojan Protection(木马防护)
/ m. S$ Y3 F# _! W: V' ~: u检测访问木马。9 L  x4 T- X1 B6 R3 v

. Y$ C/ `4 c+ R0 H7 j( ?# K
: H$ f; z6 a$ e2 SIdentification of Application Defects(应用程序缺陷的鉴定)  D0 }# C# \/ K. z
检测应用程序的错误配置警报。
5 ~  l( [: l& o
  w. k9 y; S: n5 t
5 E& F& Z3 Q' o& [+ iError Detection and Hiding(错误检测和隐藏)7 E, B5 k1 j% f" P; ^# ?
检测伪装服务器发送错误消息。
, r" ]6 n! T* I8 c
' `7 y$ A1 G" _1 E7 b
/ e1 A9 r. s8 _1 T2 n5 c安装ModSecurity: q' [3 U5 i. V7 H. n) y3 r
软件基础环境准备
4 g4 v) ^+ ~  R3 M5 H! }5 Y, K下载对应软件包
7 p1 i5 \4 }3 k8 p' T6 b3 Q  x$ cd /root( ~4 c& V" S& K5 A; a$ Y$ B4 G
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
& u  l  b$ O; j3 X; |) J2 ~$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
* _1 a0 k' X; d; L# B8 C( B安装Nginx和ModSecurity依赖包; F- p+ ]# H" {. D! j6 k
Centos/RHEL  f4 l% k; ^9 J+ o8 a, J9 Y8 L
/ l0 z) n' I& _' i1 {

4 c5 e; {8 Q8 Y$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel7 C0 q3 L- y3 V& p* ^) \0 g
Ubuntu/Debian
  L$ p% s7 c% H7 w9 c/ q, g  D( n! P3 h

$ K) q, `; i# Y' b+ k6 F9 J$ 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
9 C! ^' k$ d7 T( J8 u6 w" y编译安装ModSecurity
' C+ r1 T5 q' nNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
8 L$ q; u* N+ _7 T  @
/ i2 w) R: Y$ q) D* W0 [6 T" D2 Z+ Z& W, g
方法一:编译为Nginx静态模块+ P! y' c- F6 [- }; j. b+ z6 }

! j# K6 ~( W, {2 G0 l5 O- Y
) A) T7 D: i; K编译为独立模块(modsecurity-2.9.1), Q" T- n% }: C2 {
$ tar xzvf modsecurity-2.9.1.tar.gz
" T4 |) n1 _* \1 ^% a( J$ cd modsecurity-2.9.1/
+ Y& |- e% i- J1 p$ J4 j7 Q+ i2 V$ ./autogen.sh
1 U. A# O2 ]0 f" ~3 c% S$ ./configure --enable-standalone-module --disable-mlogc' h& O+ [/ N; W9 j" L2 I
$ make5 y0 k9 i/ ~  _
编译安装Nginx并添加ModSecurity模块( g' v6 e9 K$ b; n
$ tar xzvf nginx-1.9.2.tar.gz
, I  v) R! i" j& N( l; M$ cd nginx-1.9.2
6 \# A9 Z2 L; Q$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
# X, {6 r( P7 ^5 G2 f, o$ make && make install
' c9 O: l& L- a, ~, |6 |方法二:编译通过ModSecurity-Nginx Connector加载的动态模块# Z8 n6 J+ s* G, D0 T

, c' Z+ U% H7 \2 w  I9 }* t2 T
, U7 o) h( j8 B0 W( Q% u5 Q  ?$ w编译LibModSecurity(modsecurity-3.0)
3 M! L3 y0 {( K8 K4 K: e$ cd /root( q1 `' |2 j5 q) n( I4 L
$ git clone https://github.com/SpiderLabs/ModSecurity
5 G: [; v2 j9 L6 v$ cd ModSecurity
" m0 q5 U2 G( x6 S7 j' E& u$ git checkout -b v3/master origin/v3/master
* N! E3 y, m! M6 Q2 e6 r$ sh build.sh
3 p4 m1 S" \' a  Y* m( F% c$ git submodule init) O5 _$ ^( y' m* E
$ git submodule update
0 [5 A2 M) J9 W$ ./configure$ \" \1 Q7 n! W8 Y
$ make
+ c9 g" X3 b1 W8 i$ make install/ I6 X3 q+ M# ~" y9 g
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。! Q" p6 D9 G( _5 n

  Q; f, U6 _3 i1 L" C1 c
9 r+ ~, G3 z/ [$ ls /usr/local/modsecurity/lib% N* ]# k* k5 X- v3 v; T; `% ]0 P
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
  @* b6 u2 E4 O6 a$ A0 C编译安装Nginx并添加ModSecurity-Nginx Connector模块
' f2 T  S: C7 @& E& m( _$ }使用ModSecurity-Nginx模块来连接LibModSecurity
3 X( w" k4 d, _0 L) @& v) z# e' C# W2 g' q

: e1 u2 y9 \/ l1 F$ cd /root
6 z) n  d: [" A$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
! ?3 S' z2 a8 _  `$ r* R  k$ tar xzvf nginx-1.9.2.tar.gz
3 L* S& i1 I3 F4 L% ~$ cd nginx-1.9.2
" i! y( H1 {# \( k- |$ M$ ./configure --add-module=/root/modsecurity-nginx
5 v; N9 D0 a( Y$ m& u6 w+ ~3 z' M0 g$ make% {  B% @' s* q
$ make && make install' [9 |8 }5 a: K" r  h  w/ A( X9 S
添加OWASP规则
  x! D" n4 y2 {4 }2 M1 HModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。$ r6 b) N7 A# B; c
3 A' j* f3 q# V6 j' |

$ z! ^5 }( H! D: i+ W0 H* l下载OWASP规则并生成配置文件
# e) _, k  R1 t6 a) E- i% n7 q. i$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git  X  p7 R! i: r! ^% X
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf// a6 M& Y8 F4 M! N# Y" B5 g2 V
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs  k4 g  [; a3 b: I2 W, b( Z- I
$ cp crs-setup.conf.example  crs-setup.conf
1 @' d; l; A1 t1 K) O1 W# a配置OWASP规则
- z5 c* j) D9 s3 L. q& v编辑crs-setup.conf文件
$ T1 O- h/ c" a) J
* `2 B* O4 |$ N# |  z' ~1 X
1 }+ t4 [+ H, V+ p( D$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
& J8 p2 p3 _# n$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
, \9 {# X% Q" j$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
. A- i$ D$ d% v! }  u# S4 U& m$ Q$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf/ _3 n, s6 m6 k/ t3 b
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
) N" j3 C8 z; `) \9 D- M, L, P: o+ \! O* ]. K' d
& i" z- ]  t$ J
启用ModSecurity模块和CRS规则
/ C# m0 T# f9 ?1 }$ c- @复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; S6 t, P3 c% s0 X  A, E
7 H7 R- @2 M# A5 r, z

& b4 h$ ~' Z& b+ z! X: Smodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
2 w. |5 ]" e& y! s7 `" ~. |
3 u: f2 n- g( ^: q
% p4 `& Y' R" @7 I& o# {5 t$ cd /root/modsecurity-2.9.1/' F4 y$ P, n0 ~5 f
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
. j  o  D+ x' a/ {/ N5 e) m2 m$ cp unicode.mapping  /usr/local/nginx/conf/
! S& J, K. X# y! H; C将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
$ H1 S( K; g% w2 e, i4 W, c: h; G; c2 r( y' i
+ o, h' Y: f* Q6 d8 j
$ vim /usr/local/nginx/conf/modsecurity.conf) x' E. _- R" x
SecRuleEngine On
0 G+ m8 a1 V* }8 ]: X1 i, hModSecurity中几个常用配置说明:
3 p7 P1 x  R8 r  d4 m2 r" y0 `6 @" h" G3 P- f' B6 `

8 ^' ~( O  C+ Z. b2 }) b1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
9 q7 F, a8 Q  }2 g0 F9 z$ Y* }, v1 P1 E" l5 Y+ E( e7 b& R
  i# _( F" r$ ^& a7 Z2 S# l
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
, ~8 K) [3 s% o$ R6 Y: ^/ K+ O2 @+ N' I1 @
5 E+ p! G" x2 v  o/ {
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
8 F5 ?5 Y; ^& l+ \0 Z2 i3 {/ k" H% x6 w

. h4 Y4 T  H4 l( O* C8 V4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。5 r' \) Q% {1 w# m' T+ [

( \1 J: d% i* B% ?- B0 z# {+ ~5 N
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。0 \# i* D- N6 a1 w' p- C1 F6 d" k
0 o1 \1 \4 n/ ~& I1 [
3 I1 V! ?/ y; o4 D
3.x版本CRS
" l2 c+ m" B* }) J7 U# w$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
9 D# L' M6 K- X5 m* s" O# 生成例外排除请求的配置文件2 L8 H% _% [' m/ O
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
, X' H' K+ _& d% P4 q7 p+ q4 H$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf- m: e8 E9 I0 O3 f
$ cp rules/*.data /usr/local/nginx/conf5 v& w! K& s! F7 H& J
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
5 w0 _) z% k! @) P2 _# ]4 q8 J% C$ w$ o. s* k6 ?

1 Y  s3 R5 a. R9 r$ X( y$ vim /usr/local/nginx/conf/modsec_includes.conf
3 S- v: M& I( u+ f9 M3 Y; G! t1 w5 h3 C% Z! F- M
[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
+ |% L6 A. ^' U

3 j8 u! z% Z: a& n- a- t$ B注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
$ G' E- L5 c/ a  }
6 t; z9 W2 _9 l4 [  U. a' C; Q9 P: `  c& S
配置Nginx支持Modsecurity
; U, n$ M# o, ?9 i2 p启用Modsecurity. I/ [% G& e( z, x* m$ e
使用静态模块加载的配置方法9 a/ _- ~+ ?9 r; L
在需要启用Modsecurity的主机的location下面加入下面两行即可:- r) c$ b8 b, X. X

( c8 T+ ?& N+ O; k+ f
; \3 w* S. X1 J) s/ m4 Q  W: gModSecurityEnabled on;
( x2 [. l* R; n$ x3 r4 nModSecurityConfig modsec_includes.conf;
0 a: L/ M) \  O# `修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
9 }$ g. f# M+ I
5 O2 z8 W  c0 Q- {/ ^5 J# W: L
+ ^" l% J1 c3 y9 q8 o' A$ vim /usr/local/nginx/conf/nginx.conf
+ V% B# ?' M- |8 J( ?
+ B& s7 L0 T, L4 {2 P' F6 m6 b* }2 T# q
server {3 S6 L7 ?4 @) X% \: i
  listen       80;
) ]( ~1 {, ~! u' Q  server_name  example.com;
+ s+ @7 h0 Z8 M2 s5 w
" H/ L& U' S, \8 _0 F
5 F0 n) |: ~0 ?; _# d& U  location / {
' ]2 H/ ?2 W; W- u- `8 S& L    ModSecurityEnabled on;
0 v2 q7 \/ h* R+ p. S9 T, }    ModSecurityConfig modsec_includes.conf;
+ T1 B0 t. k. w$ Z/ w    root   html;. Q0 A6 J5 {+ `0 g" B
    index  index.html index.htm;
  i  ]& n* I% U; M7 M* s) e  }) x) f/ Y5 O, ^; A! p
}
* o. N0 u) K0 v6 C# d1 k+ Y9 w使用动态模块加载的配置方法, G% }- \# w, T( \5 F& `# b+ L9 p3 h
在需要启用Modsecurity的主机的location下面加入下面两行即可:: }9 P5 t' H. w' R% {/ R5 w

/ U" Y/ q5 f4 t; b" g) L
5 a  D& f- \  ?modsecurity on;, j! d/ M; J' r! g% {! L0 ^- T
modsecurity_rules_file modsec_includes.conf;: A/ E" S, B9 j3 C; h+ |- F0 v! |
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。5 U$ H: D% c' u/ @

! c4 d% S  |$ X3 I2 P5 E) y! a7 q" v, B% u+ \! m0 A
$ vim /usr/local/nginx/conf/nginx.conf
; D& O9 V% U- t8 Y  }( x
8 t) F* T2 _4 o( k& |9 N6 s: j' P$ |# ?5 b# |0 X5 `
server {
6 D4 S( `1 V9 e* ?$ m  listen  80;0 i6 m8 V2 U% r8 E( C' b1 c* E; `
  server_name localhost mike.hi-linux.com;1 R% z, x, P% w3 W3 L% P. q* y
  access_log /var/log/nginx/yourdomain.log;
/ n6 S  @3 x  F0 p* j' z, ^* G: I. o
* l, Q; \, p! l6 m8 G4 @8 M$ g0 O+ F" @7 h- K& z: p
  location / {- r& E  a& a+ P4 l2 f# e* ]8 e5 Q

; {! O$ j6 h3 g- t0 V& `
. {$ e! }# F3 N" M- \  R) K  modsecurity on;
: _- |$ ^( X! C- {$ J- L  modsecurity_rules_file modsec_includes.conf;# l- x, Q/ ?/ t$ F+ Q* V
  root   html;
" n. Y! j( @. T, ?1 y& j  index  index.html index.htm;1 `8 i: E# G1 s* d/ L
}3 W2 a( H1 D$ E0 ]; r
}: d7 ?: H& ^* ^" P# v
验证Nginx配置文件- m' Y2 v% B; L4 {
$ /usr/local/nginx/sbin/nginx -t! i7 U5 W. }7 K$ r- G' C$ K
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok: `" Q" U# i& G8 j4 W; I8 g
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
! a+ d7 J; ^# @3 o: K启动Nginx1 V$ {# Z5 g/ b1 B- e
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
( W, A3 I$ b. v0 d) x
# A- d4 x- I  N+ \

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

* ^9 m: b4 o9 F+ S8 l5 m$ N  F

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-2-6 22:58 , Processed in 0.078338 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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