找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12036|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
) _  Q9 P0 e) b. K6 _3 F$ }) Q1 F/ h  U" O

) O, f8 ]4 u8 c% V在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
6 @/ F: B7 W& ~0 l( @9 Z, R- H* p5 t$ C; c6 n! i6 u% ?

" D! G2 Y/ |7 V: f% {2 k什么是ModSecurity
2 e& a+ ^5 f) v4 A& sModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
' A- o, ?8 R) H& ?" M# A
& _0 A+ u7 f. k. i; \( f6 V, ~( d' x/ B
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。% F+ s: ]9 E1 d6 F) G
& D) n1 r: W  e5 S
6 f$ A5 u  d: Z
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
6 w- m5 U4 R( y5 P8 d6 H$ R1 _- `3 u2 m. z6 J3 h8 g& L

3 w: A& J$ m$ z, i官网: https://www.modsecurity.org/5 o8 p8 Y7 L7 y/ L; D+ F
9 v: ]% l3 ^2 n

# P' q# a0 u- P/ t0 V+ y: ?. P什么是OWASP CRS
; `" [" U( G, J  Y, h" A  JOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
- k) q, U3 t5 i, n6 }& F' K5 Y* Z3 O$ S/ D/ ?& r4 Y5 E8 l0 g; w* \

- m. u" u0 O) g8 u( e$ }  S8 RModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
" @  x  O" m; W) A1 k) _; P& B. f( X# Y" c; ~1 u$ o3 r

' J  Z- m7 @& THTTP Protection(HTTP防御)0 O. b- A$ E  o' \  ^9 Q
HTTP协议和本地定义使用的detectsviolations策略。
9 F, z% v. ^# z
1 K% g" `* P! @
: Y  f+ X. M- g4 \, z/ O( A$ oReal-time Blacklist Lookups(实时黑名单查询)3 i, E9 N5 r3 W% d) H/ `/ b
利用第三方IP名单。# \9 A+ M" G+ Q/ }' d

' v( u, J5 ]; t) ~
; R& f! [) ]) v  i5 eHTTP Denial of Service Protections(HTTP的拒绝服务保护). S$ n: y6 d: T; ~2 M* ?
防御HTTP的洪水攻击和HTTP Dos攻击。
: y& k$ [( O- H! s8 [
6 a5 C% I5 ^  M0 x
% y2 j; r3 K6 J* q: p4 ~Common Web Attacks Protection(常见的Web攻击防护)
! d: ^4 N/ B. d2 p检测常见的Web应用程序的安全攻击。/ W# n8 ^! A3 `7 C

! |3 Z2 c5 e3 [  e3 p6 L2 B) z
& I1 U; S; |9 N% v& yAutomation Detection(自动化检测)
7 l& G' v+ V2 W& d2 E检测机器人,爬虫,扫描仪和其他表面恶意活动。
% T& T; Z* d& h" c* p& b
  r- J+ ?; T* X1 V7 w
( D& |, c9 s2 qIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)0 B! K  k+ P! q) I$ f' n, W/ w
检测通过Web应用程序上传的恶意文件。
, u0 a+ ]  r( W
0 E+ G2 W7 e# h( T4 o6 T2 U4 Q& ~+ q! R9 ]$ `* ?2 p
Tracking Sensitive Data(跟踪敏感数据)
( ?+ y' d! k2 e' y, ^信用卡通道的使用,并阻止泄漏。3 P- _4 S1 x$ x$ ?( L5 r
+ i/ s5 F$ x  V& R! q
& {( ~% E, [$ x+ E# {
Trojan Protection(木马防护)
% F1 w3 Y2 |  o检测访问木马。4 V( W  i  T' J& A

0 `% L1 V2 A: {" y( c9 {1 x- B
8 ]+ U) [% K0 v2 u6 Q- SIdentification of Application Defects(应用程序缺陷的鉴定)7 P+ ~  R, f% S
检测应用程序的错误配置警报。
, `, O: r/ M4 _% E, j, a+ M0 g( ?& m, |- h  M) l2 o
  j: ^3 m$ A$ m: G
Error Detection and Hiding(错误检测和隐藏), t. m1 q5 u: `
检测伪装服务器发送错误消息。
: m( E7 q2 J! M0 m7 L
6 q1 \$ H; i( O" B, h4 j2 r6 M% t( G% a6 a
安装ModSecurity! p$ o3 _* _. w% G! E. L. d9 X; p
软件基础环境准备
0 p6 P0 U0 ]8 l8 w' B9 d下载对应软件包
. F. o( S/ w# w' J# V. V0 K$ cd /root( d$ R$ F# o- h6 }
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
* O9 ^6 a  G, ]9 w$ e/ ]$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz8 M4 P! K! L! a' z% \8 D7 \$ K, n
安装Nginx和ModSecurity依赖包! Z: A% ^0 O! l0 W" E6 j- ?
Centos/RHEL0 ?) F" I" W8 d' m  C: I! V
- x! e' \4 T5 H# Z+ F' l

5 p/ A% V; a; d3 t6 q5 ?- I$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
# ]3 K3 d! }( E2 v1 S* uUbuntu/Debian* q4 W4 U2 ^4 \; A% I8 H* [: }. F
3 Q+ v# X8 ]& Q
# |* s% L2 D: D" m7 {2 ~
$ 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++-dev2 R& T/ N0 X6 q' u
编译安装ModSecurity
# p  [. @  G% V# b% o$ |# z0 oNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。* w7 s2 P# v) Q0 t' D+ B$ r
/ u' i- j  c! ?' h' I. V) L

4 t5 l2 f. S" n9 g9 V方法一:编译为Nginx静态模块5 S1 z  S  C" h' C

1 t, T2 C( w+ S5 n9 [- |+ v/ l$ _& G4 O' ?
编译为独立模块(modsecurity-2.9.1)
3 A9 Y. z+ I$ f4 v! B9 s+ u$ tar xzvf modsecurity-2.9.1.tar.gz; P) k9 B3 c6 C) S/ J. F/ A( ]( v
$ cd modsecurity-2.9.1/4 X' Q4 W' F8 t+ v
$ ./autogen.sh- m9 K" J- ?- U' X# E: Y3 O* ]( U
$ ./configure --enable-standalone-module --disable-mlogc5 q' t. S3 d& D2 i$ C2 T) b6 Q* D
$ make
0 z3 q% Z- p0 O/ O7 a( h% V编译安装Nginx并添加ModSecurity模块
6 a( I7 V* Z8 g' P9 Y$ tar xzvf nginx-1.9.2.tar.gz
: w/ O8 O: L* Z% j$ cd nginx-1.9.2
1 p1 p, @2 y  }$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/. m) @0 ^. w; _; t/ o) r. J
$ make && make install
. \" p" z! h7 F8 q) {方法二:编译通过ModSecurity-Nginx Connector加载的动态模块  `1 i9 p$ j% R/ K: X" _- ~
" M# \7 k, J  T  i
: M7 v$ J4 C5 {' ^! T8 _
编译LibModSecurity(modsecurity-3.0)
+ W- q8 G9 Z9 u0 S( k! A$ cd /root+ W, U- H8 Z5 F; y, P+ H6 m" Z# T
$ git clone https://github.com/SpiderLabs/ModSecurity
% V6 c) R( y& G% Y8 I( b  {5 w$ cd ModSecurity) ?1 m/ g) Y$ U/ H+ u; n' G0 [
$ git checkout -b v3/master origin/v3/master
; u8 e. l; X6 X' F$ sh build.sh
( J( ^( h) t5 V" n3 \$ git submodule init
3 h8 _0 l5 H, C; o3 X$ git submodule update) O, Y& o; D; ?. B5 T& l2 h7 D
$ ./configure" U* a, s6 \8 P, `' d( M4 W
$ make/ E/ Z7 ]; x4 O  m
$ make install2 T: A7 b3 X0 u( f% `
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
8 `4 P1 Y. R" v" B* u! W: x$ c# u* |+ T! i) n/ p  g5 F) f
7 f( j" k7 Q, a) F
$ ls /usr/local/modsecurity/lib: K0 H7 x/ S' O! g% u  h
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0! o: |/ R% f; X' R" y* G' E
编译安装Nginx并添加ModSecurity-Nginx Connector模块
7 I- k3 A) j* j2 h使用ModSecurity-Nginx模块来连接LibModSecurity0 Q' p& O0 w- z& J! T) ]% ^- w

$ ^" i3 h) b% n; m1 b4 l* R2 l# B& {1 O
$ cd /root( M( g6 ~' R1 Q5 s+ a: `) o( c# w7 n
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx# c+ X  ]. U6 k) d' ?
$ tar xzvf nginx-1.9.2.tar.gz0 j- ~. w2 ]& T' o
$ cd nginx-1.9.2% h. d$ M0 \; i
$ ./configure --add-module=/root/modsecurity-nginx
0 ^, r& P0 ?; G! B- d/ G$ make/ V: V/ J: @- r; i& y, u( N
$ make && make install7 M) ^3 U3 T% r# Y! m8 _
添加OWASP规则8 }6 x: g8 x$ ^5 U
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
6 d" z. x) }2 j" A& H7 u9 D
5 W4 u# `4 K+ q5 M- X4 H' U# a" n+ N% c
下载OWASP规则并生成配置文件* u8 v/ ?) z& y3 h1 {0 a/ @5 x( \0 u
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git8 k3 q/ ?9 e6 m) T; I# a
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
4 Z& o( a& R/ {, O1 K$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
4 J7 k& B: ^& N$ cp crs-setup.conf.example  crs-setup.conf
3 Q. o% z. @$ u配置OWASP规则
1 J7 v2 E4 `. E; h) W2 C7 \3 [编辑crs-setup.conf文件" a2 {. E9 e3 ?( X. `6 u" e% I

' o& \( M3 c' U; g5 h9 m. G" b# w9 b3 F
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
) G. @$ I7 b/ w& a' u( Q* z) ^$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
9 z  F% n$ u' ]% f3 K$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf; w' K5 E9 }. s1 o
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf, ]! o$ D2 d, O" J) S
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
# R- H. L  s, y- ]- J
% `: W; L( H5 J  l  t
' N. q4 N2 x; o4 c% O启用ModSecurity模块和CRS规则
0 f" W4 H3 `, o7 _5 H* P  ?9 f复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
1 K2 u, E( q* b2 B2 k) i
/ s: e: Z: r& F3 m2 v8 Y) i$ p( m, h4 p4 g- c, s$ e, p; V" j
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
$ Y. J% i" y" k0 B8 O1 ]0 _+ s; k& l' }/ J2 L! B  @; D

5 J! ~" r) X, F0 f: V5 ^$ cd /root/modsecurity-2.9.1/
3 {1 t6 G' `% w7 _9 R; L/ j) n" H$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  0 N4 e! r& I2 W0 p4 i0 w7 C
$ cp unicode.mapping  /usr/local/nginx/conf/
. B# N. ^" O# @. l- ]7 p& Q" C将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
6 ]& G3 K( Z! _1 p3 b  z/ c& q1 q

( ]$ g/ Q& j& L+ ]$ vim /usr/local/nginx/conf/modsecurity.conf' B, O* a+ D  F
SecRuleEngine On( h) i) U  G, a2 ^/ M4 U
ModSecurity中几个常用配置说明:  V0 `: o! e: `6 `6 b
( B& Y7 j' E, t% l# [8 o
' N4 |* n; W+ D2 A3 R8 w' t
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
  E3 B  w& r- t' H4 O
; h5 y% v, ^- d+ ]! ?& t$ C. u% R5 m1 m0 Z0 y$ @
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。5 _7 G  y; M. ?

3 t7 q* O9 M' z; X9 ]7 a( ^9 X* r& |/ V! \7 p: R
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
4 P) H) L3 j8 D8 y! S  }' }" B) T7 O) y: |& \

+ z! H. [- E$ j- S+ I1 b, M+ ?1 A4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。5 Q7 Y' h# L) ]7 e' q7 H5 U) Y( ?
8 V6 k; A2 ^8 @0 L' d/ E/ D
2 m% R: @/ o: G# V
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。9 g1 ]) U0 C7 Y; K& I, x
  i' D& [& a1 M

% v9 n# f2 `0 l3 u, v7 G+ l  Z3.x版本CRS1 c) |7 ]3 y5 m+ C8 ]3 a
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs0 A; Y% S- b# k; O* o4 a' W2 ?' A: ?
# 生成例外排除请求的配置文件0 {* o# v5 B( m+ C& |) M
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
/ C+ t5 u% d& Z# \8 ^$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf  ?. \' B$ V; {# K$ q
$ cp rules/*.data /usr/local/nginx/conf6 C$ {+ n0 [! v5 C# p
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。+ @: G2 @9 V0 A5 Q+ d, N
! R1 j! o; H7 _6 K
1 G- ?' A3 Y. p
$ vim /usr/local/nginx/conf/modsec_includes.conf8 d. B  \. b9 a% @3 |: G' _

8 L4 j9 m; _% A7 o6 [
[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

) D. W- F8 T" h) c( x  C& E2 S5 ?; U' _( w, d
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
$ X; [% G- F& x4 r  _
2 v5 W7 u" a- q
% T5 {- n8 |  s+ ~% p, u配置Nginx支持Modsecurity, z3 J6 v+ A3 M% h. a
启用Modsecurity
+ _$ Y; P6 B( v3 |使用静态模块加载的配置方法
8 _, G, ?' w6 {1 L! w  X/ o在需要启用Modsecurity的主机的location下面加入下面两行即可:3 L" H* @; ~" `, E& b
4 G5 k( P5 v% U7 S

3 K5 V- i( M0 `! x( W! oModSecurityEnabled on;5 J2 `8 w* w1 J
ModSecurityConfig modsec_includes.conf;
4 ~# @8 \' M) S) [$ W  L修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
! ^: `7 X% T! ?- }$ P$ ^1 l! {+ X2 w4 r1 y) O6 M; d
* x2 r( ]  z3 W0 R
$ vim /usr/local/nginx/conf/nginx.conf
0 ]3 K) X, K% X& _% y6 e
* W" A1 g" q9 [3 y( M
' B- j3 R5 r" D4 S. j3 i$ Q9 w) userver {# F0 }/ X; M6 f6 t1 |
  listen       80;
# j" `- q. ?- p3 F$ p4 e: ^3 d  server_name  example.com;
5 R; S2 M, Q3 S
% }2 R% U5 q+ e% [3 {, _7 w! M* @  n. v( D$ r, F9 K
  location / {
* I1 k. ^9 z/ G; ?0 G" E; c: J    ModSecurityEnabled on;
' ~2 ?' n, i' Q4 @' z  s5 r+ L    ModSecurityConfig modsec_includes.conf;, L5 \6 R7 u9 ?# j& `. Q
    root   html;1 ?' n) J) O4 E, l' H
    index  index.html index.htm;
" A% C! s; D2 P7 I& c  }! W& I5 X! Y9 c( z5 `# @
}
1 k# s; v. c4 C- G* `使用动态模块加载的配置方法2 ?3 v* e- @! k9 a, d7 T
在需要启用Modsecurity的主机的location下面加入下面两行即可:0 H/ U( n/ N8 t' G3 Z( F* y' X: Y

7 U; `& Z7 n7 r( }5 I, z7 g+ D& v* h. D% z# C; M
modsecurity on;. A/ Y' f6 x3 g4 y1 J, ^0 @4 U: m* U. h
modsecurity_rules_file modsec_includes.conf;
0 L& l( Z7 L" f- U' w, i0 z$ J修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。0 P, w# M6 t5 D( f

5 Q6 X1 W, \2 G# u0 p2 @: ^
, U9 X" Y! F" b$ vim /usr/local/nginx/conf/nginx.conf& s7 I; _: X6 b

: q4 k+ h" k! y7 U
0 v" ]! e' t3 i( r) Vserver {: k- L4 K5 v% {( p/ q
  listen  80;
$ W) g7 p* N" H: A" f  server_name localhost mike.hi-linux.com;
( u; }. x2 k1 P1 Q' m' ~8 U/ Q  access_log /var/log/nginx/yourdomain.log;
, m- @7 Y! H) v( C6 Y. q7 P, y# g% P* q5 |
  Y$ b0 a% s9 z
  location / {4 H: i0 h) |1 \) m) {
% T; r# h! q- E4 y( p

4 K: e: V* R" P# S  modsecurity on;
3 D8 e1 H- o. X  modsecurity_rules_file modsec_includes.conf;
7 }7 H+ I- Q9 x  root   html;9 u" l3 t, t% b8 w  g; ]
  index  index.html index.htm;0 @- }; ^2 o2 M
}( ?: P/ M: E( s& R( V+ d
}
" d8 |) F5 Q$ a& O/ h4 {! U) \  l7 X验证Nginx配置文件# I" x6 l% w0 G( S" N
$ /usr/local/nginx/sbin/nginx -t$ k5 k. o2 M/ E6 b0 z
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok7 {. C% o* N+ \4 f5 |1 q
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
6 J+ G& q5 R8 ^0 ~3 g" U7 c, b启动Nginx
9 A  w% n3 D) M# W3 S1 y8 O$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf3 `& @% D1 ]2 }$ d

! B5 S) v4 E1 R/ @0 W" ]# i

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


/ S( M9 y" h3 ?# u

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-8-11 03:34 , Processed in 0.083708 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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