找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9853|回复: 0

利用ModSecurity在Nginx上构建WAF

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

# [5 y* Q4 u; ?6 k/ T0 J& A/ s$ E9 c7 W4 b' f8 P
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。2 x: ~$ q* |* _1 W' U
/ C. b* J; E1 n/ e$ G$ m3 }/ [
) T5 ?8 g! k. I9 z) i3 g) u) p
什么是ModSecurity& V( [& y5 O6 b
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。1 v0 `4 o5 r/ Z. Y0 r( z
, P  Q! a4 u# I( ]4 I

: I+ m' k" |6 g5 ?! }( ]ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。0 X; k3 M9 ]; T7 Q
$ q/ U- H( _5 O; [4 ]% V5 N
- k% |- l  J  {3 B0 Z8 l
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
3 I4 E( P5 l) c6 i: d8 r$ ~6 ~6 ?1 `- t6 ?  Z: q+ N
; ~8 V; o9 Z5 M0 @# S# e: Z5 o  ?- v
官网: https://www.modsecurity.org/
" t; `; ?8 s; S) \) M6 ^8 N6 ^- L

9 [/ k9 D* a, A' u/ Q: W6 z) |2 e什么是OWASP CRS
1 a7 f- n- M; _3 H' VOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。  u: y1 M2 O, ~+ O+ C$ x5 T( y; K: r
- \1 v; L% {( G% V# s7 {

/ B' S2 T! q. pModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
8 Y& H+ U0 q( {' H) n. [
/ S: e+ t) @4 i0 a% |3 ~) t" Q. }; Q" ~+ T, m# j* z
HTTP Protection(HTTP防御)& M8 \' p. t; o  B, }
HTTP协议和本地定义使用的detectsviolations策略。
& G4 O3 S) c! l  @) O7 k, O8 _$ {2 o# f" m0 u5 E4 y. p
% S9 X5 D! y6 S+ T: Y; h0 L2 G1 Z
Real-time Blacklist Lookups(实时黑名单查询)
6 D( ?0 |: g  |( R7 L2 t+ C利用第三方IP名单。
% m, C5 D( X7 \8 m( _
0 L" l9 _- w. i7 n" o: S7 F& H; `; O5 d* k+ ]
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
7 j0 a% D. M8 ^7 l$ f9 E8 D防御HTTP的洪水攻击和HTTP Dos攻击。5 D' p- g$ u! C; y/ V  U
  x) Q* ~* w: A7 Z- f! p0 ?2 V
2 x6 ?' r  O3 h8 P
Common Web Attacks Protection(常见的Web攻击防护)
1 B$ _# c' e/ b7 U, y# ]$ {检测常见的Web应用程序的安全攻击。, R/ e3 k% o- o% T2 A; H, k

1 X9 }! f0 Y1 x# G  M. l$ |
7 N% {+ l% u; p0 iAutomation Detection(自动化检测)
  t: n4 C7 s' N1 t& H检测机器人,爬虫,扫描仪和其他表面恶意活动。
" J9 X+ ~: f* V) ^9 i3 x
9 w( j) N8 {  L& n
9 V  C, O# q2 x- ~4 Z6 sIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)+ d) Y1 s# \$ c/ T
检测通过Web应用程序上传的恶意文件。
. [& K* g$ L2 Z8 t
4 w: n. t. v! x
" p& m& U3 G" _! z. f8 W" T/ QTracking Sensitive Data(跟踪敏感数据)6 v( c2 b: J. p9 d6 C/ Z+ A# B
信用卡通道的使用,并阻止泄漏。
6 H3 ]& ~, \% B' L% {( a
: `- M) u" c' D' Y9 Z6 y  [# _* H: M% A4 W# s* W
Trojan Protection(木马防护)* p; S# z" p/ S0 A+ y
检测访问木马。' O5 m  `4 G' M; G
: `. o% k# [- H3 u5 U
& J5 U+ o$ H5 C; h  [3 X7 t
Identification of Application Defects(应用程序缺陷的鉴定): W4 K) |$ S. V5 j! p! R
检测应用程序的错误配置警报。& c" l8 u7 V. b' z4 b

! m; i  U" A1 Z5 i" z
/ y, K8 i3 t; j! X5 oError Detection and Hiding(错误检测和隐藏)
3 o% W# A$ X; q7 i检测伪装服务器发送错误消息。& v: f; ^& d- Z
. x$ }7 x4 h& X6 L& m' N
# @5 J4 y& }1 o+ ]4 V; l; P
安装ModSecurity
6 H5 I; D, Z9 y. b: C软件基础环境准备8 Y! S5 T  c, e, G' L" H" C
下载对应软件包# `1 E; ^# M7 T5 I
$ cd /root
; a# c4 l; C% r! y! `! n$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
  H1 |$ F4 l; d3 U- ]7 y2 g$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz8 J$ b# h, c; o2 Q, `
安装Nginx和ModSecurity依赖包
$ \' ?* _* X( i( iCentos/RHEL
! q5 X( P+ l4 _( {- U
* G0 Q4 U; a5 L/ A4 |, k  J) X
. m- i: l/ s1 T" E* G$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel0 F$ i9 S! `7 M5 T! w
Ubuntu/Debian
$ i3 M' u% F$ o+ ]* w1 _
  v; R# o$ m: ^# S' ?2 R! z
% ]: U+ f- ?8 b3 d" q$ 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
( Z/ t" r. |/ t, p编译安装ModSecurity4 N" X  l! q3 W5 c& i1 J
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。( q/ r: F: A- n% |& E0 T9 R
: Y& V- o% p7 |8 t3 s

. {: [  p2 R9 y方法一:编译为Nginx静态模块
' }9 `2 @# i) s: Z2 |; T8 l/ {
) m$ c4 e9 X8 m  |8 k
% `( Y+ y4 c8 a' d/ H+ k编译为独立模块(modsecurity-2.9.1)
6 E, y9 {  f6 s1 ^2 p( o+ J$ tar xzvf modsecurity-2.9.1.tar.gz$ Q9 [! }8 n: r
$ cd modsecurity-2.9.1/
2 R2 M  f/ l9 ]: M; V7 Z: R6 @0 V2 R$ ./autogen.sh) r, z6 s1 G  b# q
$ ./configure --enable-standalone-module --disable-mlogc
! R8 [$ c$ |6 p3 d% I$ make! F, M+ `* E" I: _
编译安装Nginx并添加ModSecurity模块! ^/ m5 ~. g- G$ L9 @
$ tar xzvf nginx-1.9.2.tar.gz1 J; m( I4 Q) V
$ cd nginx-1.9.2
" j1 I7 t( k8 K; z$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/* G( |5 }3 V6 V+ }+ q
$ make && make install
2 R& n7 i) y  C* V; P! I方法二:编译通过ModSecurity-Nginx Connector加载的动态模块# ~0 V1 V- ~( }( U5 B- O& U. H
0 S* I+ S" w3 r, x( w

2 M6 x( \# S% k2 y编译LibModSecurity(modsecurity-3.0)
9 t; ~& q; l; b7 J% j3 p$ cd /root
  H/ J8 Q! G3 D6 H2 ~$ git clone https://github.com/SpiderLabs/ModSecurity' \# N7 |/ {* k: ^- t! n3 @
$ cd ModSecurity( L3 n: e; B; `# |0 P- r, S
$ git checkout -b v3/master origin/v3/master5 _6 e6 d6 G, |8 s' M
$ sh build.sh( J5 L% b6 t. d$ h" d
$ git submodule init# N0 ^9 E+ [; c# X9 T
$ git submodule update! W/ W4 {3 y6 W- G4 u
$ ./configure; ~& R7 l# ]5 w3 d
$ make
# t& Q4 \" X/ K- n$ make install
0 m4 g9 g) P+ f: s' qLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。: ~+ d% Y7 h9 u6 i9 F4 A
3 n9 I0 F( O1 \( w

, w4 d& ?4 I- U+ V1 Z2 y$ ls /usr/local/modsecurity/lib; ?1 I, e. q) u4 s- E1 C6 y/ ?
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
& l: N5 V9 S$ j2 U1 w8 _编译安装Nginx并添加ModSecurity-Nginx Connector模块
# w! f2 Y* l1 g& M. D$ B' w使用ModSecurity-Nginx模块来连接LibModSecurity3 Z5 C' C+ h3 \- J/ H
/ E9 n+ x0 c7 [+ N
/ h& {1 I6 P5 |; N7 Z! [/ M% L
$ cd /root! K, d: j9 N7 x: K
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
. N- ~3 R6 Q) T$ tar xzvf nginx-1.9.2.tar.gz
$ z3 Z: ^$ `0 [- ^/ `% Y$ cd nginx-1.9.2" T6 u, v3 v# R" H9 V/ N0 @) J
$ ./configure --add-module=/root/modsecurity-nginx
5 X; l) L" A7 O* |! S9 t# [$ make
  g- H* W( @3 F7 j/ B# F( T$ make && make install
% ^( P0 C, ^9 u( ]+ z添加OWASP规则9 s! y2 _' V0 R% X& f
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。/ Z4 Q1 w/ M# [4 a" t
0 h9 g. Z& ~  }( a* \

" Z/ E3 n4 S* F4 [" J# H下载OWASP规则并生成配置文件/ J2 j1 \+ u% c+ X
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
+ {6 q6 H  W5 z! v+ h: P$ W$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
+ t+ ?1 t, i9 q2 T  f+ B$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
' M& n  j  Q+ z% r' w* ?$ cp crs-setup.conf.example  crs-setup.conf2 w) \% ]0 R+ e* Y/ c
配置OWASP规则9 [% t( F* Q; L& G  V% _( g
编辑crs-setup.conf文件
" M  m# ?* ]" A. K6 S
; `. V! {& ^  ]- w
, |! H/ V8 a) x) q) C! y' x$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
) Z" o, p( @  Y; d$ c. f. @$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf3 y, r& c8 {* C1 ]; m2 e* t! A
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
3 R, U# F$ z  s7 g3 |/ H  D! l0 J  R+ ~3 {$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf: Y& W' F- J) Y
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。# M2 g5 U! M1 ?* H5 M" I; l0 L6 e

% D8 v0 |6 Y1 a7 F  h* ]* d' O
0 H7 b/ M- n8 N# I6 ^& s启用ModSecurity模块和CRS规则
; J$ E0 d6 F2 {+ t2 w) y7 D4 b复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
0 [; L' x4 _4 @2 w' A) x3 m- u8 h$ w1 v! W4 R

$ O. n0 m# ?: A' Q3 \* R8 j7 }modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
; ]! ^* Y: i+ Q+ S4 K: M' V: y5 q

+ q& Y. [- o& I! z. H4 S4 `$ cd /root/modsecurity-2.9.1/
( R8 \& H) z) e$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
+ S! E' A6 {4 Y) b$ cp unicode.mapping  /usr/local/nginx/conf/
( }# N: Y8 G# x3 k5 j将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
4 N+ b4 ~0 w  l* c; N3 _9 ~7 M8 h

" [5 y7 ?1 h2 R$ vim /usr/local/nginx/conf/modsecurity.conf) u1 @/ k, ^8 U
SecRuleEngine On
6 C, r1 U/ ~3 u& _2 x$ qModSecurity中几个常用配置说明:
. S. B5 I" H$ L3 ^5 x5 W" l+ W. T) A# O7 |. R- x3 ~3 G

, {, k* P, P# _5 b6 l' ^# I2 [1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。1 l" E+ g  u4 n& ^2 G* ]
: }/ {2 ]; v% y$ H* F# s5 _
! q, r( }" o% j# v8 U
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。/ X" c, n' ^% X9 G6 N
2 T3 q  w) M. H# D  u$ Z/ V

" ~, t3 o- U, ]3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
9 ?2 e" j% a. C' I# u" j6 ]6 A: G+ J# G6 c' F* U+ U" x" ~
! M* m+ `8 |+ Y( C
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。( ^7 X* _7 R2 [8 R! ~

% e; [6 `- b# D+ \- u8 N9 k! j5 ~. P  T2 b. V/ `
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
# ~( g3 {7 h- t1 m; p
: [8 h5 e) o+ s- `( E! m5 I
, G. D: s4 t7 x4 S' g1 B* c3.x版本CRS. Y1 o' n! |. x4 h/ |5 k$ y% C- R  I
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
9 o. d$ `4 [; [  ~# 生成例外排除请求的配置文件
  y% t( L. n& \' @  s$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
% n1 o; c. s( p6 m  r5 r$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf* I% J# o+ T. K3 _5 d: b* V
$ cp rules/*.data /usr/local/nginx/conf
' Q# ]8 C8 v: K. W& j7 d为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。$ r$ L/ L; ]/ J( b( f: h0 }

+ G9 [  x' f5 D6 P! V
+ k8 o5 B8 Y# O4 D$ vim /usr/local/nginx/conf/modsec_includes.conf
8 B# s1 r5 f: |& F! s; E7 U  C; m. I. @$ o5 ]5 `4 q
[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
6 R0 Y$ h& y( o  L# Z4 o
- q+ m: i4 z% h( h8 z
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
  ~* j! @1 c- H- a; [, ~4 o6 h$ s

3 o# t0 J) d* W- c9 N配置Nginx支持Modsecurity+ [' X, _3 F- @% r+ a" d' Y# q/ j, c
启用Modsecurity
6 b8 i$ }, U7 p! {% ]$ N$ S# n5 F使用静态模块加载的配置方法
/ Q$ h; L, V/ {# X/ O* T2 B9 e& o/ Q在需要启用Modsecurity的主机的location下面加入下面两行即可:9 j1 Q# G6 a) ]! V; x$ V5 I
6 B# M! w; P! _: a4 w8 B) Q8 ]

+ F- x( M& \6 NModSecurityEnabled on;
/ E) s1 [# C0 x5 J8 dModSecurityConfig modsec_includes.conf;% I0 ^& s% R5 d- f
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。6 {* {0 t, M. |% Q

8 E" h/ x" g3 C/ E4 r4 f
* e  r# S, l0 ]$ vim /usr/local/nginx/conf/nginx.conf7 [8 }; x$ ?2 ?% \' t" U* l

( W8 ^/ S3 Q/ H" Q$ I* U. i! \  ~( Y0 _2 y8 D( v+ g
server {
7 A9 c" W: o7 J  listen       80;
% J- w0 u5 P; R3 k$ ]  server_name  example.com;
/ ?9 c: U; ^: t8 ?" Z# _( @8 J+ T+ a
8 o+ i& W5 l1 K9 Y, ^# _4 O
  location / {* M9 p. \( m3 C% Q5 ]% A, q
    ModSecurityEnabled on;, {9 k: X  g" n) X8 B
    ModSecurityConfig modsec_includes.conf;
* I5 g% v" X2 N$ o' x6 U* g    root   html;
& x2 i0 |. p/ {. l( ?1 y* P    index  index.html index.htm;* n! L" \7 E' l% K; T3 h
  }
* K! X% s6 _( X8 n; h}$ h# _+ V% T: W0 V3 z
使用动态模块加载的配置方法9 M  V& a$ l; y% E2 a
在需要启用Modsecurity的主机的location下面加入下面两行即可:
/ f4 ~- i. e* [; H! }7 e: z2 Y7 z- ?, x' ^6 H& Z
" @( {5 O, D& b4 g" k
modsecurity on;
! k9 U, a# X$ j" @# ?. `modsecurity_rules_file modsec_includes.conf;# w1 q. w* C' J8 `
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。9 O) u. v# f& |+ t

; j! p6 v  e* I" G2 d# J. ~0 F7 W) x; ^. ]6 v- x" g' r$ f
$ vim /usr/local/nginx/conf/nginx.conf
' F2 g6 Z+ o2 t9 W! D" B: E( ], b/ M3 |6 `  I+ a4 {* `
* V) q  y2 M. Q9 M& S' ]6 S
server {
) t- r1 r/ o8 [* v; p3 J  listen  80;
. p. o- m" O& I; R& u* u; U  server_name localhost mike.hi-linux.com;
/ b4 ?3 G3 D6 e1 R* D4 X  access_log /var/log/nginx/yourdomain.log;1 u! b7 ?  u+ X2 M0 K) i

7 `; k5 y9 q) Z; B7 t2 V
" b% [; S, a/ n# ]3 J/ v/ P- }) Z  location / {; G9 _: S" l* Y

  X) r: @7 h9 g9 f
+ Z" W4 H( x4 [2 G  modsecurity on;
% r! w. C0 L/ ^4 r  modsecurity_rules_file modsec_includes.conf;
2 K6 g( @8 L( h2 a2 {- W  root   html;, J; B0 l. {6 |( t0 [
  index  index.html index.htm;& ?$ }9 ^' D; G) |* `" y
}
6 S8 J" v; @" k' n. \" u: l! ^}
9 t3 P' x5 P- s$ e0 }$ [验证Nginx配置文件
  G% B( C; X' s" I7 F* ^$ /usr/local/nginx/sbin/nginx -t. H( s4 s8 V, f! N
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
4 |' }1 B' c5 r5 h, pnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
4 h" Q" }% F- F( |. s启动Nginx$ _5 Z, c9 n/ g9 n' h, J
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf1 C) ~  t/ W. ~' W

% b6 ?9 |( b  X! 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能够战胜更多复杂/未知/混淆的攻击模式。

7 [) U! D9 O& s$ N# d

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-1-2 11:38 , Processed in 0.050606 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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