找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12883|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
5 f% P4 N& W$ Q: w) ?- u6 V4 S* ?* A/ j& {( |; h) J

, [& }8 t* z' v" d在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。: N: f$ B5 z" l' |1 |7 Y

  Z. q0 E$ t3 a- p7 }$ C6 W4 c- m' v5 h. h& O& v
什么是ModSecurity7 y& x, d# j7 e1 @
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
2 ]$ h4 M, [& B- O$ ~  e( l) }0 ?" H" J) K

! I0 \/ D5 K0 v' s9 L) sModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。; R, ~% @7 L4 }0 c

, p9 m0 f8 e8 C* ]2 t; V5 v+ Z% f* f+ n8 x# W
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
, `4 D; L7 P* Y3 L# ?" F' r: X
1 H* s( G( ]  [7 r, v9 s6 x  V' L9 C" O2 A+ W
官网: https://www.modsecurity.org/
9 q3 q+ ~0 }7 I- r0 R
3 l' L3 L. i1 l5 f7 P5 v/ ]
, }' D, Q6 }/ l6 h) @什么是OWASP CRS
: W. Q  g. H% p$ b. \1 Q* \OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
1 l2 j/ z; x# i6 |( m1 K; G2 _$ O5 ]" A/ Z- e8 t% {- |8 O7 `
6 t- ]9 C' h, n: o
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。  L4 Z, P5 ^" C4 M! u
  m% L* m3 \5 w& }3 Z

. o$ }) M5 v0 Z0 k: PHTTP Protection(HTTP防御)0 Y9 e4 l3 J/ Q  X8 w
HTTP协议和本地定义使用的detectsviolations策略。) u& r! A, B" e1 x$ S; x7 d

1 H& B: ?: P0 l) Q- r  ~( \# b3 J% U" p
Real-time Blacklist Lookups(实时黑名单查询)
; Z8 h3 k- y" q2 G, a利用第三方IP名单。
5 R- C, I) z( u3 t- }( I2 a- K+ I4 b

! u( L3 A7 [8 HHTTP Denial of Service Protections(HTTP的拒绝服务保护)' q5 _8 S& t# G0 Q5 j* x* e4 a2 f
防御HTTP的洪水攻击和HTTP Dos攻击。- e3 e2 _5 X: Q/ r
' M5 q. B! _9 k& l4 U% L8 H) k

0 M0 }# o, C- w5 t# s& l! oCommon Web Attacks Protection(常见的Web攻击防护). x7 ?8 Z! e5 P" F" p+ @; b: o
检测常见的Web应用程序的安全攻击。' ~+ _8 N* Q# {+ G) R4 P
/ i/ h' i0 e: v, Z+ m
6 q$ ~9 K$ H* Q/ }, T
Automation Detection(自动化检测)! [8 X8 |% l( z( E
检测机器人,爬虫,扫描仪和其他表面恶意活动。
  X5 [& @* j" P  X" z1 _" d# Z' }8 N. U7 \& L
$ ~% i+ ~  c( Q  `
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
8 q0 [" I9 J: s; ~+ I% P2 x检测通过Web应用程序上传的恶意文件。
, _0 j6 n$ l$ g$ E
' B" M$ j  h8 A" Z. v, U% P5 ~/ s
4 n4 B) b! w" ?! d7 k! e6 LTracking Sensitive Data(跟踪敏感数据)8 C* v6 A1 K- b. P7 F; I0 A
信用卡通道的使用,并阻止泄漏。8 @  B2 I4 T1 p; w1 W: }
' L$ I- \2 H2 o
% O8 s6 y* W$ {/ i9 T$ R* |
Trojan Protection(木马防护)
6 `' q# P& P9 R6 `) T, I9 Q2 m检测访问木马。
- z2 n3 \" _4 f2 k6 Z$ \  D3 r) K4 z- [9 h* R& K0 c0 V
8 V" s" y5 N( @& s
Identification of Application Defects(应用程序缺陷的鉴定)! o1 d' I* d4 Q
检测应用程序的错误配置警报。
5 y; a# E( k4 }  d& F) G- n3 x& r5 Q& b

5 o; R6 v" p% KError Detection and Hiding(错误检测和隐藏)
( Q+ q/ V$ d  L. m) _4 c/ q+ ~检测伪装服务器发送错误消息。, Z9 O6 i6 Y& h! A" e$ q

7 ^8 N3 _8 M) ]+ t& V5 f3 h" V9 R
* L, b) O% H. V安装ModSecurity# Y: y& f, G( U7 n5 \
软件基础环境准备
3 L# l, i( G3 t& @下载对应软件包
/ J# j5 I) W, I7 V+ D; `$ cd /root) T% H# v/ \0 i+ q" B' l, M
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
1 H; S. v0 b+ S0 r* `$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz3 `* d0 O, r- b* {
安装Nginx和ModSecurity依赖包: z" F  K" M; V+ ]! z/ X
Centos/RHEL9 X/ Q7 _" H3 q5 [$ z' ^

& C. B) n+ X8 |$ m
5 J5 r1 C, _1 w3 ?( N: i$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
2 F  S* ^! Q* F+ {6 LUbuntu/Debian! K  |" E- K5 ^5 w0 ?0 V% L- z

# ]7 C/ E: E; l% I4 i# ]3 t- H
+ x6 M: c7 u& a) |$ 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
- w: `4 P4 X% ]编译安装ModSecurity
- ^8 l  e) f$ e; e+ E* \  QNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
  j" c% T3 h0 P3 A2 u# `" ?% P- |* m5 ~

; O0 w# D- f& P: N, Y方法一:编译为Nginx静态模块. j9 R& W) e: r  Q. ?- t& ]3 ]

4 M' z3 A+ \+ c, _3 Y( _" a) t& u: E) Y" H. M* ]
编译为独立模块(modsecurity-2.9.1)
7 E: h1 c! l8 e% o$ tar xzvf modsecurity-2.9.1.tar.gz
: E# Z5 H! J, F$ cd modsecurity-2.9.1/) E) H, y. v- [' Z' f6 a" ~5 B
$ ./autogen.sh2 U: J; y) w2 I1 E  h0 G" K
$ ./configure --enable-standalone-module --disable-mlogc
% R1 p" ?8 p/ X0 N3 y/ \$ make# x% v9 |5 q; v0 B
编译安装Nginx并添加ModSecurity模块
, F. y. ?$ [! o- }8 B+ Q$ tar xzvf nginx-1.9.2.tar.gz
6 p; P6 I& d, J* C, J6 [$ cd nginx-1.9.29 x" A6 Q8 ^# w1 ?: k1 }
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/  ?% s! H% v+ K7 n9 O
$ make && make install
% Y2 H& D/ H" k2 H8 u2 M方法二:编译通过ModSecurity-Nginx Connector加载的动态模块8 x' G" o# d, m3 r2 E7 [

1 e. r1 H& A( X" I. i
3 ]4 a" ?0 G$ G, @8 q, K$ l编译LibModSecurity(modsecurity-3.0)
% X# A  Y0 a6 \; ^7 f$ cd /root8 `# w% E5 n( P
$ git clone https://github.com/SpiderLabs/ModSecurity9 \) M2 _# W) }! u0 J3 Q- q
$ cd ModSecurity  l2 X% C* }* T
$ git checkout -b v3/master origin/v3/master7 h$ h2 o0 F$ ~0 f/ D, D
$ sh build.sh
6 M+ i2 L9 Q8 l4 ^! g, O" S/ N7 ?$ git submodule init7 [+ j! u. F% L$ e
$ git submodule update
8 J5 B# y+ K9 {) }8 R' @$ ./configure& [$ N* P) f. b' b! i! Q& o
$ make
# S0 V$ P, E* B' Y1 _* x, V! @2 ]: W* I$ make install
4 A8 _: D, q0 b& H3 T2 vLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。% _/ a% P6 P9 b9 A. d& b& z# a
/ j# T$ _8 N7 Y& S! n$ y

# i# U5 x. |) r" ^0 S1 o7 }$ ls /usr/local/modsecurity/lib
% x3 X4 h! n5 W$ Llibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
/ o' P3 d0 e% Y" N. J( b编译安装Nginx并添加ModSecurity-Nginx Connector模块  @7 `) ^# O( b2 O9 }
使用ModSecurity-Nginx模块来连接LibModSecurity8 b" o* O4 [) M$ k: ~! O

0 @# [  ^( U, J, W' F
1 q5 Q% L7 I1 j" J2 Q, r8 {$ cd /root/ S) Q1 |" `( a
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
# p: k- c; g) U' C3 P$ e  u! n/ `' x$ tar xzvf nginx-1.9.2.tar.gz
1 @( A) g( C- S7 X3 b4 D0 |4 @$ A$ cd nginx-1.9.2
7 i$ z* b* n* I* D- C" v! H$ ./configure --add-module=/root/modsecurity-nginx; |; G1 L) y% @$ u4 f
$ make
6 E$ i0 M+ ~2 c! e1 D$ M$ make && make install! y; r1 f" r. i8 t( @; X& M( p
添加OWASP规则# I/ u( J9 ]4 b
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
3 C3 ]* P: G+ Z1 [
, e( \8 A( ~! l( a' y/ T7 {) H% @
下载OWASP规则并生成配置文件
. p7 [, V3 |; j! |6 u/ v4 z* o$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
0 K  `' H$ P- ^& w5 l; ]$ l$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
( B9 u5 M6 G$ z  L' A$ cd /usr/local/nginx/conf/owasp-modsecurity-crs) S0 u; X' X* O3 F6 W
$ cp crs-setup.conf.example  crs-setup.conf
1 N1 Z- n* M0 e4 x$ a6 I" ^配置OWASP规则
/ E, S( I6 w- |- c# B/ U# p% y3 S# S编辑crs-setup.conf文件& e# p$ O! R6 v( I- d) g

3 X3 r+ q# R! W7 ]+ {+ |; M2 D2 \
7 {+ {' b( X" j. c: R# e; t. ?2 f$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf% V- n) R& B, F7 K0 K
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
" C& h0 Z) C9 n! p$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
: B, t+ E( J+ [- A  C6 R2 a$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf1 c. V; P, L+ f( _* g
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
3 |: D2 l" Y; v) ~0 u
5 n  Q0 [" E+ a- O6 i9 D" A
5 Y9 h) S4 \! o4 V- g  [1 p启用ModSecurity模块和CRS规则5 `+ K- W5 L' k3 W9 Y2 |
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
4 g0 G5 V4 v! \! A: h" c: c" z9 X- w, O2 U) }1 _
1 A' l2 O+ N% q' d; c( g0 U
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。/ s# ~4 v1 S) T% l
! N0 D9 t1 e+ T

8 ?( `* p& _8 U6 i$ cd /root/modsecurity-2.9.1/
" X5 G% P2 g7 L+ l$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
$ N4 J/ N7 U5 o$ cp unicode.mapping  /usr/local/nginx/conf/6 y2 U' _9 U1 a6 _
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。# H  i* R0 S! j# P
" N" d+ Z$ h4 W- x! F

0 n: t6 r6 \  D& G7 g5 \$ vim /usr/local/nginx/conf/modsecurity.conf! U! i1 s$ j6 ?- }, S
SecRuleEngine On
, e9 u# Q/ ~! FModSecurity中几个常用配置说明:
8 i' j. \* p' B- R$ S$ l
# g+ f5 Z* ]7 Q( n0 [5 L# C+ d
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
  R" H. N2 f7 b; ], ^7 I/ S' q: S4 k- Q0 _$ e3 X
) x: i  q0 \1 x8 ]; f( C7 M: h/ p4 h& Q
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
4 I# p  N- [" Q1 H- [6 i6 B( j/ _8 x: ?1 z7 w( P# X! ~5 D
# s" I5 f( I' g2 w9 u. c" o6 G
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。: o8 ?! h( c+ h4 D- ]5 ]0 y

/ D* w4 f" K& K. T) S; q! d5 K" P$ a
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
+ l" {* D% @' n9 l' G( ?2 h' Y3 p4 a
! M" b. h; C" U2 |% O4 f
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。0 m( N; t1 D9 M( X) _! W  ?6 g
: E  z+ k- p5 _' c6 K
2 s: m- J. M2 m) c' l
3.x版本CRS
# s4 g/ z" }' E4 i. P  J2 g' v$ cd /usr/local/nginx/conf/owasp-modsecurity-crs6 Y. c8 N7 T& H7 Z
# 生成例外排除请求的配置文件
, M# j/ [+ m+ [- h0 J5 C% m: }$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
3 d9 l4 C( j9 f; n5 R$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
) v0 D( B2 K& f3 h! }$ cp rules/*.data /usr/local/nginx/conf: [% \# B7 P& p, {) x
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
' J" z) ?+ S( w$ R0 V# \1 w$ f7 b! J3 l; m% ^5 A: i, w

9 E! o$ @# o- Z' l/ S4 r$ vim /usr/local/nginx/conf/modsec_includes.conf0 N; W, |& E' n
9 M1 F, C$ Z4 Q$ p# |8 R
[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 I3 e$ h9 E' p
2 d4 C2 ^/ E, [, [8 ?# q2 l注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。. X( e) V1 H2 ]! g! q) d

* T. [2 o& q% v) l$ A9 E
/ e! U5 J+ m; \9 t配置Nginx支持Modsecurity
% m  I6 ]  {" s2 o8 Q( t5 n( k启用Modsecurity
& J- S+ F9 C5 s5 s5 [* e$ n使用静态模块加载的配置方法
- m, @9 F0 `/ J/ ]+ H" _在需要启用Modsecurity的主机的location下面加入下面两行即可:4 s. I+ W: S! f7 S

( d+ P& L+ m" a% \
: x* M# J8 @7 }4 B/ _ModSecurityEnabled on;$ m4 q" }1 r3 l) M2 f
ModSecurityConfig modsec_includes.conf;" u1 i* Y/ k: r! e
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
2 H  J  Y3 {7 X7 t0 S9 j4 C" s- d
- r5 c( L* Y) D- J5 f1 a. Z% o
# m2 B2 z* W! L  |" N9 h% K6 T$ vim /usr/local/nginx/conf/nginx.conf# P' t) i* N4 c4 j
4 s: O' L7 m" t7 M4 K
9 n5 b- `+ J, x# H, s% c
server {% p- l* E6 I$ [% S+ E  M, h- T% n
  listen       80;
0 f9 n9 x) l' @& e2 N9 r0 X  server_name  example.com;
0 X- h) @3 H+ x5 N: A6 x8 m- o. @. b2 }! v! b

2 I3 |- N, @; T! |# K3 C4 v2 f1 j  location / {
6 v% K, I8 T9 h3 W( m3 a$ U2 _    ModSecurityEnabled on;
# a# ~3 X% u! b4 ~" s; o$ Y    ModSecurityConfig modsec_includes.conf;
" a- ?1 e- [" O2 ?; H6 j! H    root   html;! F& A! z7 P8 ~5 g! ^" z# b9 u" ^
    index  index.html index.htm;
, d. l- F! W- H- k5 }  }! k7 @  L: ]" V: i
}2 w" ?( ]& y1 r. U6 M. s" z
使用动态模块加载的配置方法
: i! L' v2 S' }5 H. B1 Y1 ^在需要启用Modsecurity的主机的location下面加入下面两行即可:
  B- ]1 l( B/ M- ~/ v( v% K' P
; X6 I. u1 ], g# A7 ]6 \6 o
+ S7 }3 D: u# V: P! Pmodsecurity on;5 O- Z( i" E6 k$ x' j8 G9 r
modsecurity_rules_file modsec_includes.conf;
. Z. N4 e" T! T7 N0 e: ?& I, a. u修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。5 T$ G8 J. \; d6 _

. T- Y; ~* T5 |! w. k
# m; X- x! z) I8 Y+ U$ vim /usr/local/nginx/conf/nginx.conf6 d5 b) B1 s" H/ }, ?$ S% h- H

% f/ F! M7 ~3 ]! d* k$ O  a; T2 |7 U
server {
5 W+ X, D  u+ ]7 ^: Z! B2 Q! T  listen  80;  W4 j3 o& F$ \% x( l2 n4 q
  server_name localhost mike.hi-linux.com;7 R2 X0 G+ ^. X9 _
  access_log /var/log/nginx/yourdomain.log;
( q# ?5 W% n9 d3 q% j  e( d5 E4 g( f+ D# @( u" t

; ]5 M; _& P# F) a) u3 F4 @  location / {
' n# O8 Y: y5 I' q8 c5 R
" h/ E! F1 ~! o0 ~
# S, A% l: U/ s! y+ e# q' X3 e- H  modsecurity on;
! l' y% y7 Z1 Y. @) \& S' {' w& `  modsecurity_rules_file modsec_includes.conf;, V; o3 ^9 ^  {
  root   html;  P4 T+ B9 I7 d7 L
  index  index.html index.htm;2 |$ m; Z2 ]0 m- K
}
' m! b; _+ B) m# V2 p0 w" c% {0 ^% j1 d}
9 w) D4 l  o' b验证Nginx配置文件7 \  J0 [: f  O4 o2 x/ C! D; X
$ /usr/local/nginx/sbin/nginx -t5 a) ^" q. I; A& L
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok! U- [& C# f9 ?+ S5 S4 b0 Q/ `
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
$ F# N( Q# d$ ~4 C' _启动Nginx6 w' f: j( o( N5 P0 b& D+ E
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
* z* h0 V* f9 m9 W4 C6 {7 c. e- J2 n7 t$ k

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


# Y1 o# M5 V# {

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-11-27 06:38 , Processed in 0.082678 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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