找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9890|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
3 B! \% S' P! v" l3 a' j& l' {
: A4 H, ^; J) J
/ y4 |$ J- h; p/ L5 f$ h* u# h在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
* Y' V' I( I5 A# p  D! B8 ?. s  x! s  ?& F
2 u. I" J! x, b. ^  H
什么是ModSecurity
: A. k6 q# \; A" H( O7 e& F9 Q/ eModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
& ?0 ]; w) Q0 p" m! W. i$ D7 _  u2 H1 \
5 m5 L$ s3 S# r* S9 e9 w
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。) h( Y9 ?3 Z, m- S

* v6 }( z/ e; B: y
, K( L2 H- w1 y5 xModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
/ T' P2 j. _* W# M3 B1 @' x, D

" N  e) g& G2 E0 g6 L官网: https://www.modsecurity.org/
' E3 d6 f% P  j! Q  ]
2 Y+ q8 x( S. z
% X: n. s  O; L; x什么是OWASP CRS
7 V4 h: r; G& Z3 e- `7 {9 XOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。1 B& k+ c. n: e6 u- Y
* X) I4 n+ n0 p( Q2 n  ~

# w; U+ ?* s. j* W4 cModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。$ J* m. i+ N5 L+ n7 ^  o% G3 l/ _

8 j! D) ?) H' a$ q  ^3 x4 V' v+ c) c$ g
HTTP Protection(HTTP防御)
$ }1 G8 t* J8 N$ c) jHTTP协议和本地定义使用的detectsviolations策略。4 x  `3 k  ~; G" i7 h+ Y( g
8 }$ r! A0 h+ f$ h0 R$ ?
6 a( B0 k- W, D1 b
Real-time Blacklist Lookups(实时黑名单查询)
/ [0 G# s8 b: z' ?: A$ n7 y利用第三方IP名单。/ k& o  X0 [9 P( A! ]3 S9 @' F+ u
" I' d9 ^- [+ o
" U- C/ ?4 S2 M6 C- {/ b4 F
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
  ?- x' v1 S+ N防御HTTP的洪水攻击和HTTP Dos攻击。
5 r3 b/ H  f( ^4 V! B( ^, Z, p0 x: k7 K: M' _

) F' e# x1 o0 L- ^Common Web Attacks Protection(常见的Web攻击防护)
% G# |" k! P: Z& p9 z( y8 H  X) s检测常见的Web应用程序的安全攻击。/ Y# D" ]! O+ Y! z
1 I% T, V. P+ D" Z

3 C  y! g9 n8 W8 C% kAutomation Detection(自动化检测): `; h; y3 p& k
检测机器人,爬虫,扫描仪和其他表面恶意活动。
6 X% V! [7 k2 f; M6 f* P* E& J; \. l# u% _/ N; x- L, W/ `4 _
! k& L/ L3 u9 B. c$ e) c: Z
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
0 u: d9 X* L0 u检测通过Web应用程序上传的恶意文件。
" l' R% l4 h) d! x4 O/ d; }  X! Q8 J3 h% O2 v" @

; I# }" @3 q# U" wTracking Sensitive Data(跟踪敏感数据)
2 [  I6 n: w' z: o3 U4 W信用卡通道的使用,并阻止泄漏。
! D- Y9 @5 u: m) h
0 v. T2 o* ^# y, I7 i( K* @: v- K2 B' z& f, Q9 G5 f5 Y
Trojan Protection(木马防护)
! e7 ?, A9 B8 ]/ S( }( t2 T3 b2 b检测访问木马。$ |; R* V4 B3 m8 `6 K6 V) ?- f# v; T

( m- H5 n" f$ j0 }* M. z
  d' _" f9 D( S8 KIdentification of Application Defects(应用程序缺陷的鉴定)1 N5 _: l. O' S7 N
检测应用程序的错误配置警报。
* |' Y- S3 J# C5 u0 |; G- i! f- u6 o1 F# N3 Q

4 Q; x# u/ L' g+ a6 LError Detection and Hiding(错误检测和隐藏)
7 t* m/ k' D1 }2 H& x0 R, U检测伪装服务器发送错误消息。
2 X% Z& o" V) [9 v" f. S% T/ W6 S0 x0 J9 y" l1 f

; {9 f9 m) \- D* u# l! p安装ModSecurity" W6 G# z2 `& Y& X  D, ~' D
软件基础环境准备- i# |5 k  a) a7 T$ x/ A7 T- `  @
下载对应软件包! U. g6 ?& W, t. x6 z( t
$ cd /root8 Q1 J: `) ~! h0 f  J
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'& P5 E, R- O6 l$ S8 J
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
4 ?$ u, P+ C4 F" a" C  s) ^安装Nginx和ModSecurity依赖包0 d  _: H3 v* N
Centos/RHEL) p* z  l6 R) ^
& k  u( M3 m( [: J4 s

1 z$ e+ k3 E, ?! i" n" W$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel* N) u; H! x4 @* O' C( T0 [
Ubuntu/Debian
! r/ W, k& z4 c/ G1 w3 G; I8 b2 j( h, R: G* y3 }
: U( s- R5 Y& z$ m1 O
$ 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++-dev9 G; l0 y4 n7 }4 R& z. ^
编译安装ModSecurity& z5 N9 M" m7 _1 u) z) A1 Q/ y
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。% l* z+ `; Q# B$ ~+ C
# m# S' c, l% J# \0 l
/ [/ j! \  X* X7 G5 }6 P, g' N/ F
方法一:编译为Nginx静态模块, ]8 r. Q- T' L! X/ ]) G* w
2 W0 |0 H: i6 e( ~
" W- d  B: N% p0 L& ~& e
编译为独立模块(modsecurity-2.9.1)* J# p. I! }& ^# q2 w- S7 y% ?: q
$ tar xzvf modsecurity-2.9.1.tar.gz8 [7 T2 S6 W3 ^* D4 Z8 w  J
$ cd modsecurity-2.9.1/
. _0 ~& V/ H" o- P5 ^8 Q$ ./autogen.sh
! |8 b( w" D+ }, B$ k* V# s$ ./configure --enable-standalone-module --disable-mlogc
- w& M& A. A6 d6 I" h8 h$ make* U3 U3 g# F7 R4 @& U
编译安装Nginx并添加ModSecurity模块
8 W# m- }: Q$ f& A" v9 K. y$ tar xzvf nginx-1.9.2.tar.gz
+ U& F; X) Q: o, M- |9 V3 k$ cd nginx-1.9.23 }3 v+ m( g7 {1 C
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
' \% l3 P& E$ }: W$ t* x$ make && make install2 [$ v; o  ?; e
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块* `) P* I4 v3 ~6 t! I
" D3 X- G- C- ^# ?

" v7 Z* i  V/ n# q7 l编译LibModSecurity(modsecurity-3.0)+ ]# a; g5 h' ^( L
$ cd /root
7 l. O' z0 X- S2 Z$ git clone https://github.com/SpiderLabs/ModSecurity
5 P2 d. j7 T1 G2 e5 _, Q8 I$ cd ModSecurity
2 @& G, s5 B+ a/ V% m# w9 W$ git checkout -b v3/master origin/v3/master
1 U4 P+ b7 t. N4 A5 F$ sh build.sh1 X. T9 d5 T) ?" ?9 U
$ git submodule init. n/ a9 m9 |  A( P" a+ Q0 j) E2 Z5 v
$ git submodule update$ O& `% K6 ~9 S- g. `
$ ./configure
8 w! w3 ^& x$ e6 A1 t3 V& e' O$ make# l% o6 B/ |4 \2 _
$ make install0 W4 E- m) z' M  n9 d, j+ e
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。+ N- c9 t% V2 _* v; {
( i: e* A4 R/ x0 C
" t8 C# V& ~! n9 i: `! B9 |
$ ls /usr/local/modsecurity/lib0 c$ P/ X( ~: \8 z& f) c0 Y
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
1 s  s9 x% [; P6 a* B. O编译安装Nginx并添加ModSecurity-Nginx Connector模块
, T& \  r. ]; s( e% l& ]使用ModSecurity-Nginx模块来连接LibModSecurity
: H1 ]( A7 t# m" `1 Z/ ]0 R5 v" t1 O' Z' D& U4 g' z- H* p# M

* C: \" f3 ^# K: |$ cd /root
1 {, ^# ^/ q! J( k$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx& X4 ~# a6 Z; T  m% L, A
$ tar xzvf nginx-1.9.2.tar.gz+ K& a' Z6 M9 e- x
$ cd nginx-1.9.2
2 r+ B9 @+ Y7 x* U9 z8 h$ ./configure --add-module=/root/modsecurity-nginx
  H" _$ `3 m+ h4 L  k" o1 [$ make5 W6 `4 _& [" D
$ make && make install
; z5 x& _9 k3 p/ T+ y添加OWASP规则. X. a2 K# X( @2 h7 k: m. v5 \9 `
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。& o4 o5 f7 @' t2 L5 T, M

4 Z" U# q- ?% |$ N$ |! l4 X& o! L& X# Z) ]3 |; f
下载OWASP规则并生成配置文件
7 X  M) x$ V( {3 Z$ g* I$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
( k/ d7 k! c' b8 z* q8 `4 i  O. q$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
0 _! w# h( q( x( ?1 W! Q$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
1 S' G, |! S: A8 s+ q; ^/ T$ cp crs-setup.conf.example  crs-setup.conf
2 u/ y5 c) X; C5 s/ y# h配置OWASP规则- O  d. h0 j0 Y- m; @
编辑crs-setup.conf文件, ^$ V# I, g" f2 ?1 ~( |

& B$ a/ h' D2 u" P+ l8 r- {* M: c) L
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
# y: L3 s$ ]. C) n" f* h$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf- L$ n- D3 o/ N- p4 J! r
$ 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* z2 I1 C' t
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf# ^2 w8 i% |+ x, N! m
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
2 m; g8 A0 T( g3 c# i6 m
% P$ b# B/ }7 r$ P
' r% f# I2 ~1 X启用ModSecurity模块和CRS规则4 z2 d6 \( [! r7 `9 B! t3 j& R9 Q
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
! I  d2 x5 l, @+ b2 B0 {) Q; @/ b( l) [; x+ R

! U! E5 W' m9 g+ Y3 Bmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。! c$ G& I* i- k8 U) }+ t" u2 c
/ U4 B* J; U% r# ?! }* _7 [

% y' R- v$ {# ?) y# X9 ~: `* {$ cd /root/modsecurity-2.9.1/, U* t. p6 e: D- ~5 H6 A- e0 {! Y' ^$ s6 i
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
# K4 l& w4 Q+ z# ~, H* e5 |6 n$ cp unicode.mapping  /usr/local/nginx/conf/$ }7 T9 ]- R5 Q0 B! T: ]
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。7 x$ c' a( x/ ]5 m+ O, N
/ z# I9 P/ c) J  U$ k( s/ M
+ |4 L! t5 X% _, ?; h- z# E
$ vim /usr/local/nginx/conf/modsecurity.conf% M) N* q0 E/ T+ B: }
SecRuleEngine On! F1 h0 C. N* f% C' v5 t
ModSecurity中几个常用配置说明:6 n  V2 g, d* e) y( {

$ ?. n, r0 f, {% m% l
+ \2 x2 b$ O' [5 T4 Z. t0 v1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
; `, C2 ^4 i; N2 w3 ?, K$ z
$ K( {1 I  N  Y* Y  H, j
2 L# ^' s0 O* }' S2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。  ?+ A7 a5 Q" N9 N! S- L' Z
1 _1 Z: D" \6 Z" X! a

. R# _' J9 V( K; c$ ]3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
6 h3 ]9 w8 C- u3 s% L1 u) e. x0 l, _+ ~% F
! g5 Z- J$ @, Y' V' T( w: k6 n1 f
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。' v2 F% I" a. G% V& i
* c8 W: ?3 U* S

8 I4 y2 M( n. b' E1 a4 |3 s* l6 X在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。$ D- z6 e) j. y6 K) H1 l, J

: H3 D5 H5 P3 f9 k; H" R/ ^5 V
# c8 [" t: U9 r, ?( R  a# \- q3.x版本CRS
3 w4 C) y2 J6 C# p: B5 |$ cd /usr/local/nginx/conf/owasp-modsecurity-crs* T# e; O1 Q0 \' y
# 生成例外排除请求的配置文件
1 k  y* Z* ^% l& B$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf. O! ]7 D9 [+ P/ i. V( _* J! s! p
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf* i) ~2 ?6 g9 y+ e, J  T0 B+ `
$ cp rules/*.data /usr/local/nginx/conf& z6 V' v9 ^4 _5 B2 a' p3 v
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
8 n3 g1 x4 }% M5 m) G6 |- n7 x: J- w0 b. B( @

+ a$ X0 p$ v8 F1 j9 H! o4 a  Q4 x$ vim /usr/local/nginx/conf/modsec_includes.conf5 q* \8 R: ^6 J7 G( [6 S. D! k

& ^" T8 F+ r: e
[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
0 ?% P4 s# s0 b1 `$ e6 H9 n. W, Z: w
" Y# N( I( ]4 L
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。+ E4 X5 G7 G  ?1 b5 [) Q2 \. ?3 t
; a' {0 z- ^4 t% `5 J. T4 y( F; e
$ A: e' K/ r! H' r# `7 V
配置Nginx支持Modsecurity4 F+ m  {$ F+ _. m" ?; d
启用Modsecurity2 X! M% Q1 ]8 u. @
使用静态模块加载的配置方法: G; I. b, N6 `+ r- l# o0 L
在需要启用Modsecurity的主机的location下面加入下面两行即可:
  x- [0 _9 J( T& V( _# A+ ?, Z) `: W( s$ E2 r

* `8 d& i: [, cModSecurityEnabled on;2 ]& k0 ]) O( F. T- w* i- W
ModSecurityConfig modsec_includes.conf;
3 j8 r" S% j2 K- w/ R* @修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。" c/ i. ]6 G: W7 u$ ?% H
1 q) x8 r) r, S0 s. I" I0 X6 c/ t
9 C" u& C4 F/ c- a( @" [. ^! q- H- {
$ vim /usr/local/nginx/conf/nginx.conf' l6 l9 f0 j6 d% e& i
! a+ Y/ u: F6 N# C( R

( A- p7 K5 |) @1 r4 b4 ~% @server {% ^$ m1 H0 k* y8 E$ E: E* G
  listen       80;% L* v# Q% p- q$ u
  server_name  example.com;
) A  T% }. b- f' t! n
) B7 m: L/ }( [. P) K! C, v+ Y+ v6 A
  location / {
5 `8 u9 Y9 s5 ]+ T& f    ModSecurityEnabled on;( y2 \/ j8 b- x3 J9 a  w
    ModSecurityConfig modsec_includes.conf;
2 t2 G% ]) r0 v% b+ k    root   html;$ }: ^2 q+ V+ M4 L+ U. w
    index  index.html index.htm;
/ a1 Z5 Z" P& H# G8 S& m, g  }4 b9 h& O2 G% U& ]* N/ l1 [
}& u; P  e0 R" a- y/ o0 V
使用动态模块加载的配置方法
4 }$ P5 r) E& ~# e, C在需要启用Modsecurity的主机的location下面加入下面两行即可:
9 K. v4 I+ Z: K" V3 L8 O1 f8 p
: K4 l& y. E1 i! Z# l0 m7 _
" z1 M. ~2 U6 d/ [3 |modsecurity on;1 n! L) ]0 c+ d! F' L/ }
modsecurity_rules_file modsec_includes.conf;4 D# u5 d0 Z& f8 _
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
# m! R* R* g# A  V+ \
. r3 @. ?6 x" q' w' X
$ K+ C0 X0 _1 K  }- y! s& a$ vim /usr/local/nginx/conf/nginx.conf: U) j: n4 f1 ~4 o: b( {/ q4 {

0 X, f6 O9 l0 X6 [% d7 d
2 d  N( v0 Y; |" U: g4 H) ^server {) e! J* X5 `6 e3 _& U4 v
  listen  80;; K3 N  }$ E& }; n
  server_name localhost mike.hi-linux.com;
) \) i& `2 ?0 t8 ?  access_log /var/log/nginx/yourdomain.log;
4 l% \2 }. |7 |+ g( G
/ _' H- j* m5 Q" j* @
# d$ g" t5 f) @  |( w0 ~' K- ~- t  location / {! w) X+ Z% ^: Y$ i: l! c0 P
/ B4 \5 R3 |7 a% d
  Z2 v8 i! y9 K9 G0 M" r+ @: t1 S4 D
  modsecurity on;/ x; N. r6 N, f; G. _% P
  modsecurity_rules_file modsec_includes.conf;+ T) l& A( K  A# V  E" Y7 T
  root   html;
# ?; R( W) b8 {# j5 \, w  index  index.html index.htm;
% _8 |. V+ D+ X8 s* }8 z5 [) o}
' S, q: u2 d' m9 y7 Q( t! M}
3 x3 ~4 _+ m# k9 s* Z4 K验证Nginx配置文件
  N, f2 B2 A. V$ R4 f" f# w$ /usr/local/nginx/sbin/nginx -t# J) T* q: N, Z" z5 e5 q; o! `( d/ ]
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok% y' A2 e) l5 d3 L- M
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
9 H/ z# _2 o% J7 R: S) s9 m启动Nginx
. s& t! A+ p- s, ~. E5 \+ H$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf7 k0 ]  D( E3 }  Y7 j! N7 g- W
2 h8 Y* i- \* t8 P, 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能够战胜更多复杂/未知/混淆的攻击模式。


+ o: g1 K* Q. _% r4 I% {

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-1-8 04:47 , Processed in 0.060971 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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