找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12376|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。# }! h( t5 K3 L" U) {. D
+ d& H4 u: Z- u/ t; o) Q* {
3 L5 _* E, J% ~0 H; Y! ?- j5 b
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
+ }8 S. \* v) _% x0 r8 {- }; d$ T2 d( o
7 P' W3 f4 A( A: j
什么是ModSecurity
. E9 l: |; @! X' R+ GModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。/ D# d3 P0 w; ?/ @

$ H. B% B. k8 H4 N6 t( S4 j* X
; \8 y- s) G# K0 _' A% i5 H, jModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
  e  m8 _! |5 d2 N& c/ _' K2 U/ D, F( d' q( e
; x+ D  b" q# h
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。4 N* Y6 {5 L8 T) j1 p" ?: {

% i* \' C# g% @0 g0 d
9 }* T1 h6 r0 m! Z' H5 I$ t0 m官网: https://www.modsecurity.org/
' Y  Y- R( W0 w8 l5 o3 c, _" V8 T! z

1 J7 {: e$ [4 ^什么是OWASP CRS
3 O( C5 L: H+ u. D. |2 yOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。+ f5 {9 @) D0 S0 w/ L- B6 @

6 c0 E9 ~% W' ?/ {) K3 Y% }
( |, r2 Z# D/ ]( a2 E" p! ~$ B6 UModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
+ `; m; K3 T, K3 C
  Q+ ~$ S1 _; Z- m$ T2 C& J; A' w5 M% U1 \! t; m- x# K( C
HTTP Protection(HTTP防御)/ P1 e1 k0 p" l
HTTP协议和本地定义使用的detectsviolations策略。) p& X, ^6 ]7 Y( e2 Q6 T5 c8 V

8 K+ l4 F. m, w0 ?0 Q- S8 [" D/ x, m# L8 d9 g! ]6 H
Real-time Blacklist Lookups(实时黑名单查询)
9 M/ r; h5 a( T3 L. L利用第三方IP名单。
! V$ {! c; H6 {8 |. J
9 S4 ^7 ^& z8 i& I3 H% u( N  }- w" Q/ Q$ a& |
HTTP Denial of Service Protections(HTTP的拒绝服务保护)* I! F+ {/ u3 u! g
防御HTTP的洪水攻击和HTTP Dos攻击。( m, [! Y3 k, T8 b6 R
  d. O8 l( ?- l! [# \
; D/ v1 ]" [. b; }* V: V# t& J
Common Web Attacks Protection(常见的Web攻击防护)
2 Y0 _. k1 p, R. _/ y( i检测常见的Web应用程序的安全攻击。% D& w0 j6 a4 D. t: Z
' r1 ~' b5 h; d- x" Q

8 n" p; J- G& W3 A/ C8 t4 D9 x* q6 EAutomation Detection(自动化检测)
& X5 L6 J0 g6 E  d& Y4 R( t( J检测机器人,爬虫,扫描仪和其他表面恶意活动。8 r( W; z, t9 A: m: u0 S
' }7 U; A! I( ^. W. R. s4 |

" ^( b8 r3 f/ uIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)& }# l7 C8 @( }( Q  ^
检测通过Web应用程序上传的恶意文件。2 k" p$ q+ E: T

$ A% p; J0 ~: _* g9 }2 {) K
% }+ ^, t! d8 O/ dTracking Sensitive Data(跟踪敏感数据): X( N$ e/ D$ b7 ?
信用卡通道的使用,并阻止泄漏。
# u/ Y6 \: b4 D9 \/ U
9 g/ N3 H9 O9 _  w$ j
( m) o$ a3 e7 P/ X, ?  HTrojan Protection(木马防护). C$ m! a+ E& C
检测访问木马。
7 `) E0 l/ P2 U+ d' M: W4 d
& @2 S) a3 B: ^+ n0 v( U9 ^) U
& V, R/ d; W3 D& W) {Identification of Application Defects(应用程序缺陷的鉴定)" }& f) b+ j( R
检测应用程序的错误配置警报。" |% R* ~" U) }1 x* ]8 G3 w
3 D0 v: K3 H6 q0 C6 G& K+ B' m
3 V6 X8 Y( Y: y% o
Error Detection and Hiding(错误检测和隐藏)
/ ~" P: c' k7 i/ D/ f5 W9 x检测伪装服务器发送错误消息。
; D3 }, O! i( y% i7 F/ u; y, V8 `' }# Q; f
/ ^  b$ x( ~  a( F! F8 l1 |9 ?' i( U+ [! J- S
安装ModSecurity( U; H4 z  _, y; ]+ x* A% ~9 Q
软件基础环境准备
( ]% D* y1 U9 d/ o0 W6 Y) @下载对应软件包- q' U8 h1 X) \0 d/ b0 o
$ cd /root7 B% {6 u; p) W! e
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
; g4 q9 ^9 A9 c8 k& R$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz. u6 ^1 \; [9 a! x
安装Nginx和ModSecurity依赖包
! M7 j7 {, \: p! V  d  zCentos/RHEL- q) m9 k! @3 r4 n
' y; c2 @( r3 Y: F9 P: J0 V  V

) e& N3 U- K: J( l3 j& s$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel3 U( T. s3 D7 E
Ubuntu/Debian- l8 L3 b2 ]( m9 Z: o* P9 [$ s$ E

: F0 z1 c5 ^; J. G& M5 w# D# c2 Z$ q9 S' B% G) |. v: D7 W, E" P
$ 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 P0 W7 p' Y! N5 I, F; m
编译安装ModSecurity& x0 J7 H2 [( V/ k
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。5 t4 j. e' j8 c5 ]/ I! v

' [7 @2 A. v# B6 L8 o/ a3 @/ e. a
方法一:编译为Nginx静态模块
6 ]; |# H; z8 V8 n) f
5 i# q3 ], f" m3 g- G" @' Q
& i3 ^( q; C. y/ v; B+ S8 a编译为独立模块(modsecurity-2.9.1)
6 j* v) ?* j; @# R4 B6 L  ]* l$ tar xzvf modsecurity-2.9.1.tar.gz
7 o$ ^( }/ h5 c$ t8 [$ cd modsecurity-2.9.1/
+ L6 x' h' U. |$ ./autogen.sh
) O2 L; v/ r3 k1 y$ ./configure --enable-standalone-module --disable-mlogc7 j/ b6 i- b3 d0 }; z& S2 ~5 O
$ make
) p4 T: j" |" O编译安装Nginx并添加ModSecurity模块
: m2 f- h, \- ], r' H$ tar xzvf nginx-1.9.2.tar.gz8 |5 D* l% {6 q% `" e
$ cd nginx-1.9.24 `$ H7 i% t7 q) A7 C
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/2 O  c" v: A! E+ r9 _/ I4 E* p" S
$ make && make install9 O$ A( \! b% f' g. A0 o
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
1 a; h9 q/ i: l5 E. A9 E
/ j5 J# _- n' h  s/ s/ C0 O+ N- f7 j% }- {) X
编译LibModSecurity(modsecurity-3.0)
3 W& ?; ]8 C# k: q$ cd /root
( e; C) k) }$ U& p9 f: m$ git clone https://github.com/SpiderLabs/ModSecurity
# R. m0 W3 F! `0 W; U# T6 t$ cd ModSecurity# ]! s7 i7 K2 F7 i- n1 G
$ git checkout -b v3/master origin/v3/master) s+ m% R5 o6 r/ a
$ sh build.sh1 D, c1 f/ \) D5 |+ Z3 ?
$ git submodule init
, {, @6 _0 E9 m, U$ git submodule update
0 p: c7 e: d9 R3 \$ ./configure
8 }( C) t" p8 G$ make
, y$ N7 b  P+ M" p6 F- {8 E$ make install
7 ]: E- Q+ R  C2 N* q$ F$ iLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
1 t- Q8 y: \# x) n! t: t) A+ o& L1 l" b) x8 N
! H( E( |9 O& t- D- l0 L. E9 v+ w
$ ls /usr/local/modsecurity/lib$ b. c$ L3 {, Y" o
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
. t2 O) W" {- A- O' L& P编译安装Nginx并添加ModSecurity-Nginx Connector模块! ]4 v) a$ z  }  h2 `+ U+ |8 F6 V: S1 Q2 Y
使用ModSecurity-Nginx模块来连接LibModSecurity& \# i8 \3 w8 \

7 _1 ?+ F# b* Q, r4 M. g3 D" U0 u3 O' H
$ cd /root) m6 B! y) b! f( Q5 M
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
8 V* u, k. p0 X$ tar xzvf nginx-1.9.2.tar.gz+ d& v" ~: E* G! i; u9 o0 D
$ cd nginx-1.9.2$ [: l% c8 \) P3 t1 \' l' h
$ ./configure --add-module=/root/modsecurity-nginx$ [$ x1 z, x7 q0 B( i: y7 y/ ^
$ make/ @: k& ?/ ^+ ?0 O
$ make && make install: f- f2 w" w& K7 G9 x( A
添加OWASP规则
/ [8 i5 D' P5 k; M( z4 @0 hModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。, i1 t6 J8 {, a; \' y
- n/ n1 `9 H& ]" z( @; H$ l! V
) [% ]- ~! V: A3 e
下载OWASP规则并生成配置文件, j! Y+ h/ H( k/ e$ p# T
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
  E" {! C1 o1 F, l' U; K+ C$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
1 ^) a( A- ^) K+ @5 U3 t$ cd /usr/local/nginx/conf/owasp-modsecurity-crs" {$ D4 [8 g* W1 d
$ cp crs-setup.conf.example  crs-setup.conf
0 k+ l( [( [9 B, R; H  L3 h. G配置OWASP规则- R: v' r0 b: A: p; o/ L& z
编辑crs-setup.conf文件6 }# W# [* y- `( R" R# V7 I$ S

/ z# s: |& X) o' w4 u& Y& G
8 F1 w) A, w3 u6 J/ T7 X$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf- w/ h9 j( K6 p1 x2 {9 O. V
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf1 g) Y% `; u. c0 s6 g1 B8 c
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf) x( w( E1 A# A
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
) b8 ~/ c1 T$ y0 A7 {$ I默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。* w: M7 K% b- t1 ~6 G/ p

4 X, a3 ?- K! j7 K* T
& Q# f$ y: C. n/ k" B! B7 K启用ModSecurity模块和CRS规则
1 p; \  F3 r( b) G复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
1 m& G: r  X& l5 L9 t9 m& J: L' g% W8 Q
& o) p% y- ]3 h: K. d/ f* u
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
- q6 `3 z9 r( u9 O% `+ E
* Q$ J- r4 G, Q2 ?4 P% r( `
: ?( `+ g$ g1 D) y( [$ cd /root/modsecurity-2.9.1/0 x+ T9 Z0 k& {8 Z+ F% |. h  k
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
9 i- t$ K- v' @5 J+ B$ cp unicode.mapping  /usr/local/nginx/conf/
. S+ f# a4 p/ s: w% k* [; {- H* d) D! a将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
& ]& _$ [& W" e( P" W& x- V1 p, U% J

- A8 Z) @, w+ B$ vim /usr/local/nginx/conf/modsecurity.conf
7 h! O  W5 H& `& T; ]* `SecRuleEngine On3 O$ z' U) _/ L- Q( j0 n
ModSecurity中几个常用配置说明:
$ l: K7 v) H* ?" {3 Z( @( N4 N' c$ }" }0 q6 _
+ k- @. ]/ t) U2 ^
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。' H* t' Y& D6 N8 x' ]

2 H/ b, M! r3 f! u
" h2 N8 R" F" O& M/ b3 `# V2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。& V/ I& o  x# h" k0 c8 a

+ N  Y: c8 C$ B' V8 @
) |' E! _3 G+ w: T/ E! b: ~" n3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
0 |8 w  ~1 m4 p6 f  Y
" r6 M# k2 n# X4 E* {# U
5 ^* `" s# h& h- K" N  H4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
4 n- f" |/ b/ T5 T7 t% J! d$ J
# V& }  o1 {/ @' ?& i! z8 U( e
) ^! V  @! T% ]5 G; j' [+ R. [, X在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。0 V/ f/ _, h; H

# |6 q' |- g+ S+ x# I
' X. T/ w# ^5 |9 S1 N6 B( \3 d3.x版本CRS) L4 C0 P+ u' P# |! I8 P
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
& G4 \3 u/ ?6 Z4 ~$ A* W# 生成例外排除请求的配置文件4 U6 y" R* g, F* D, T
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf' `; @/ @; n9 a# l; M: k- s  D$ ]5 Z
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf. L0 f# ]6 @) c6 R; r) X
$ cp rules/*.data /usr/local/nginx/conf+ ~. {! w! A% g5 ]
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。# B9 |5 s; w) F' E0 e* U2 m
2 `  m8 W6 E5 j
* b" e, t' ]7 g, Y8 j9 m. O9 a
$ vim /usr/local/nginx/conf/modsec_includes.conf
; X5 @, J$ ^0 e- V
1 {7 v5 L/ q; n  h2 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
1 Y+ Q4 ?) Y* \  ?! I& e3 j& ~2 J
$ D- l/ `' i7 F0 m2 a
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。' |9 g9 C7 @1 u& k- c/ N& n; e
) `" {2 F! W9 d8 E/ b/ a, }7 ~
" j: l, S( v! Y: W/ ~
配置Nginx支持Modsecurity
8 Y1 g% s2 B! h6 u启用Modsecurity/ M3 v% r" g; |
使用静态模块加载的配置方法
& \, J* C& u3 C在需要启用Modsecurity的主机的location下面加入下面两行即可:2 ]5 O; d) P) ~% [+ S

' b& T% f. D& R9 h' A
2 H& d/ K* ~1 xModSecurityEnabled on;/ Q3 O1 G" C( ~% {% U' L
ModSecurityConfig modsec_includes.conf;
1 z# K* T% H, b  N) M$ C$ p  g修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
' V& [6 Z/ @4 i( X( Q0 G
2 a* Q9 Z! `% {( W- `
  O9 ?& `( P. h" \. G/ E$ vim /usr/local/nginx/conf/nginx.conf
4 Z6 }; U& s1 Z3 u- R
, L1 q8 B/ d( y  ~
2 p$ H! E8 r4 c# t" mserver {
! _1 m! g7 ?9 u% V9 @  listen       80;
9 R# `+ }# o1 m' r  server_name  example.com;' t. o; [3 ]2 D8 j0 P1 J: J) i
% i0 {" X8 `! M& J, U

% ^3 c* v9 x0 n3 w* X! n  location / {. i6 J" r" O; P; Q% J7 q0 k9 H
    ModSecurityEnabled on;
" F! ^. m$ c7 C( j- i6 x    ModSecurityConfig modsec_includes.conf;# T- q. b* f$ Q2 U
    root   html;
! ^/ N1 P1 G6 ?% E    index  index.html index.htm;. ?, k" H/ v7 }6 _% \# i
  }
8 f+ i, v/ [  l}, C# @, j" {4 _+ |7 K3 v
使用动态模块加载的配置方法' P' Z# q& h! @
在需要启用Modsecurity的主机的location下面加入下面两行即可:
% z& E2 q; `0 J1 Y6 W
3 E4 y9 z% V9 E: ?
( ^. W2 R# P, [7 Q5 A* Xmodsecurity on;/ f0 f: K  ~0 E2 [5 |  B9 F
modsecurity_rules_file modsec_includes.conf;' q2 A) A! M# V+ q7 U# c
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
2 c4 N. F+ i4 E# @1 h. M1 _. X; @

9 {5 E; Z( z; C3 H6 J( N" {* \$ vim /usr/local/nginx/conf/nginx.conf
+ g% ^8 w' W2 X: Z* w' G6 F2 |7 z6 y$ d* a  M. D$ \+ _" H& D

" }6 Q6 ]: l- G& y' Oserver {$ d( Y$ M% U' O# J0 p! d
  listen  80;
* }4 g: z! y2 i, {/ c  server_name localhost mike.hi-linux.com;
8 ?& r4 W9 S% [& l  access_log /var/log/nginx/yourdomain.log;0 n  N- b7 z5 e: W2 W

7 S3 Q& h) f9 c6 I; |- P+ J5 C3 j0 A# R- V+ B- N+ r6 w; O
  location / {3 e3 L3 a! }2 v: _- x

8 h. H* O5 e. c- b8 q9 C' Q9 ~* |& F  X! J/ G# n
  modsecurity on;
/ J, e) ^1 v5 Q4 j1 ]: g  modsecurity_rules_file modsec_includes.conf;
7 L! f' T+ Z' a  root   html;
. V9 E, Z, J/ \' y  index  index.html index.htm;9 t+ H# p2 E) w0 x9 g
}
  N6 ]5 S% T# c& _9 S}4 |, Y  e, }! r
验证Nginx配置文件
# D3 Q4 m$ i8 l9 Z2 ]& B7 O; y$ /usr/local/nginx/sbin/nginx -t; S' r' g0 h# L: k4 ^
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
, B9 {2 W2 \- i1 \; U& U) @nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful* W9 P8 M! _6 g3 k* G$ ]7 J$ o
启动Nginx2 y! h1 M7 t$ s1 L, `6 f0 z& V
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
: {. P$ u$ p$ Y, S4 V2 B& X, R5 W  [% l9 D" A7 Y( b% d

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


1 F. ~  o6 k4 W& y# [  w

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-9-16 12:08 , Processed in 0.061556 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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