找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10227|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。+ c5 C. @& s% _( ~1 A
# x- ~! ~8 x4 M) ]
- q% U+ S0 I, n( k
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。0 Q, h; u! e3 y0 R( c
. s  R/ ]" X6 |( |  b
) x+ v2 G1 b, O
什么是ModSecurity
" ]+ C1 c( Y6 w3 hModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。; D4 N- Y+ s3 P0 f, M

& Y% d5 n+ f2 Q% y' t% ~/ v6 f* ^2 R
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。4 [: s( }  d- \, `

: _8 A4 _# Q$ G5 Z$ X
' t4 C( u' N3 G. BModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
  M3 U$ e7 J5 u* E! r& k1 d, o, X6 T) \! s$ W

9 L2 D, W4 _3 d官网: https://www.modsecurity.org/
2 D8 A) {) G3 m: n  W9 n; I8 U0 q! P& q  m/ l9 p, w6 o# H

2 T* t7 Z3 T* ?& Q什么是OWASP CRS
( r/ C; u& }  pOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。! h3 s3 t5 V6 V' d1 o' _! \5 t% }

! a! u+ C9 ]) w' q2 ^: [2 f2 R+ e. s* T7 J) U/ e! U
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
* K2 c3 N) W, S+ n$ ^" f% c: }$ }: G
) l6 |6 e( L; _+ Y
HTTP Protection(HTTP防御)* M) ~$ L8 p6 a5 i4 w
HTTP协议和本地定义使用的detectsviolations策略。4 M6 D4 u" E7 H/ a. X

2 ^- d; C, R, K+ Q1 o8 s, ~' c* b4 J- y4 e2 H
Real-time Blacklist Lookups(实时黑名单查询)
+ h8 |: F! b, a$ ^利用第三方IP名单。& Z& W, a$ {. \2 }* @7 ~+ o
4 M2 G' L" t# w

, J3 }! m$ X. T7 P; bHTTP Denial of Service Protections(HTTP的拒绝服务保护)& h- F6 j6 w# M, V
防御HTTP的洪水攻击和HTTP Dos攻击。
# n; y, }. H. b4 p
2 n' q7 Y. _! {. ]7 b$ R# I/ r/ _3 Y/ D$ d+ ?8 n
Common Web Attacks Protection(常见的Web攻击防护)* d8 h9 z+ [5 o" T
检测常见的Web应用程序的安全攻击。" Y/ q2 e" c/ a) e' K* S
% S" H9 ^! r. K! ]' w
% @1 U8 m0 V# S% ]( W# ?2 K
Automation Detection(自动化检测)
: I4 {& Z! I# W8 ^: F5 r/ s8 w检测机器人,爬虫,扫描仪和其他表面恶意活动。5 }1 u! C; q# q  s# M

: ]3 K1 @4 p5 S3 b) Z' Y& D# T: _- n% j5 C, W
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
% N! B! n! m) D/ z' w% b& o检测通过Web应用程序上传的恶意文件。. c* Q2 H. D3 M( M

5 G2 z, H$ D. f/ H; s1 ~+ o3 j" O$ ^0 F8 X
Tracking Sensitive Data(跟踪敏感数据)
/ n1 d  P3 `- n7 W信用卡通道的使用,并阻止泄漏。: P" z# [3 K1 f& B) n! A+ i' M

6 i4 ^& j( q0 P' ]' r$ g6 Y2 c" V$ m3 x
Trojan Protection(木马防护)1 ^7 ?- |6 E% ?+ H& l% C
检测访问木马。- B1 T% R  }6 J/ m4 e
' k  n3 M$ Q* _- B# o1 x; |! h
1 `! i; K, \& F: m* K
Identification of Application Defects(应用程序缺陷的鉴定)8 Y( `% A* o4 i& ^( o( r  k
检测应用程序的错误配置警报。
/ l; z/ q- u# j! S3 K' h* t1 f3 v" {4 \

" Z9 c$ l5 z" l, c! k& NError Detection and Hiding(错误检测和隐藏)
( J5 d- m! i' T$ A检测伪装服务器发送错误消息。" A- n) r1 A/ I' S3 K
' m& q; r/ r( B! j5 {( c  y

4 g' F# H( v; b2 y1 y$ n安装ModSecurity( M8 _3 d& \, n' k: }& r
软件基础环境准备
: M: x$ T8 X1 y- V. [! w9 b- q下载对应软件包0 Y0 p4 }5 _' X, j
$ cd /root
4 S' u1 @  a1 `7 {6 L$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
, Y1 D) Q( K) M" A$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz9 G$ X5 _8 l; u
安装Nginx和ModSecurity依赖包+ Y3 j  a0 |7 F" N1 a+ q0 R* _
Centos/RHEL8 F) G& C1 _6 N/ c& W2 k/ B, T

# I, N$ K% a! z; K1 a& u5 m
: p: a9 K8 y1 Z, Z$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
& k8 {: H5 h7 @: X1 g, f' i3 i: UUbuntu/Debian' \9 U& m( t( I( g6 J( b/ D& Y

8 F0 n5 [0 \) {! @3 I, h; ?  r7 B" ?
$ 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
( P" Z. @2 G' B( I. i; ~编译安装ModSecurity
' `. I  J, c& r. mNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
* W! H" R7 f: y; B4 Y6 o: S$ y  U; ?2 @8 z1 ?
2 V& l% w; m3 z5 ~
方法一:编译为Nginx静态模块
% S6 p4 G3 {! u8 }, @
. z# e6 _8 y& W0 k& K) |6 r5 c0 _0 Y$ c$ c- |- C
编译为独立模块(modsecurity-2.9.1)
# w% k. k' O9 i5 Q$ tar xzvf modsecurity-2.9.1.tar.gz% x% F; E; N7 a3 ^& Y
$ cd modsecurity-2.9.1/% M* E6 S8 l. h% O4 l& A1 @; j. {
$ ./autogen.sh: V. N5 t/ d- Y& ]2 a" i+ V
$ ./configure --enable-standalone-module --disable-mlogc* d" H% r" s- T" B) i% f1 {" C3 ?
$ make( v% K" Q- @4 V+ [5 E' [4 o
编译安装Nginx并添加ModSecurity模块
2 j. e# I. M( @- G- e$ tar xzvf nginx-1.9.2.tar.gz
7 k( r" @" W8 l1 r2 M$ cd nginx-1.9.2
& @7 Z( j% h( e4 E# g& W$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
$ p9 g+ }2 j8 q1 [9 ^+ q2 ^$ make && make install
' ], q% y: G! J方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
1 t. w% B. H' ?; X' U, f) K: u
. w- d2 `- Z" q9 t  ~; z$ ^) s! y* ]4 ~' C% k3 t7 t4 @
编译LibModSecurity(modsecurity-3.0)
% H% L- G5 p/ k2 Z% a3 D9 B' I$ cd /root; u( x0 G  @/ J
$ git clone https://github.com/SpiderLabs/ModSecurity8 `% U$ P8 z  c1 x) f9 L! q0 C6 ]
$ cd ModSecurity
9 O1 x5 ], P; e$ git checkout -b v3/master origin/v3/master  l+ V3 ]$ P9 |+ a( o" P' T
$ sh build.sh0 {& r) e$ r* B
$ git submodule init* X/ ~$ E( s( J( w( `& m  L+ M6 [
$ git submodule update
. g& e7 V+ a6 C0 s$ ./configure8 X$ D# z+ K2 A' o
$ make% L: x1 G5 i6 a1 |
$ make install
, K: \: K* e0 w% P+ n( F: bLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。. l% R3 C7 F; ~1 b$ h) A
$ P; K% I/ P* c1 [; l
$ ?% b) ^: ~( r' m8 Q, _+ ~' a
$ ls /usr/local/modsecurity/lib
, T+ V5 J, A/ W( t# klibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0" `. t6 M( _. N) Q
编译安装Nginx并添加ModSecurity-Nginx Connector模块
/ @! ~4 x" \2 c5 v; _3 U, ]' h2 s使用ModSecurity-Nginx模块来连接LibModSecurity2 h' ^& H7 U9 x* H/ p

) Q, }" X4 Y6 r$ h4 L, l" b8 I; ?0 \. D3 T+ Y) e/ K2 ^) H7 T5 p, i
$ cd /root* _( m0 Z( W) ^- G
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
* W/ h# D) X8 S$ tar xzvf nginx-1.9.2.tar.gz
0 Y! w, L9 M# J4 z* p$ cd nginx-1.9.2, e8 A4 s! E4 w8 s0 y! P; m1 ^
$ ./configure --add-module=/root/modsecurity-nginx- E4 V- t, R& A8 E2 `- x3 f! n
$ make% }- n- L/ k, E) m
$ make && make install
2 X# ]0 m* D7 {# Q! |添加OWASP规则
  {0 B0 p2 `+ AModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。4 |4 f6 o& \1 ?
0 M2 G8 K  R+ x( z5 d, d

$ D- u3 r1 O8 l, H& T0 V下载OWASP规则并生成配置文件
+ \# m& r% }6 ~- u$ X$ H$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
5 l+ g8 T  K7 ^, W$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/6 Q8 o/ M. q) \5 x1 w4 m5 g% ]3 t& x9 i
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
2 e# ]' Z) w' ~$ ~$ z" \$ cp crs-setup.conf.example  crs-setup.conf+ j; d6 f8 T- L- I% z2 Y
配置OWASP规则/ x5 W: E7 n" z1 B5 ]* E1 ~
编辑crs-setup.conf文件9 E6 j9 L$ F' |6 L5 d
- a0 f; k) O) }9 C- k' t& g2 R# x
: Z7 G' Q5 f9 S: a6 a
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
6 p) j7 O8 B' f- t4 e$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf4 w2 N% k' x5 X4 o4 j4 N6 T% U
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
3 G5 t. e. A8 f8 j/ I, q4 p, E$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf2 j1 \8 Z* o! t( D) Z- j
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
3 d1 W. H$ `6 H6 w. d. ?! r3 K
) C% \4 j: r: v  z# ~
$ o& _' ^" s% u* q* u  t启用ModSecurity模块和CRS规则5 w8 B4 y% D, h
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ u7 ?1 B) N2 j- i# b

! t3 S0 R- F" }: P
6 L# w- o1 f+ n2 N- cmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。/ z  |' T- M1 V( p) `5 A, T( I
1 O* M$ R) m& ~; S& F9 c
1 K% K( m, D! u9 r4 N/ j. E
$ cd /root/modsecurity-2.9.1/
2 q' K) e- k7 i' `4 ^8 \$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  ' P6 W; h! x  M5 t! }# F/ S
$ cp unicode.mapping  /usr/local/nginx/conf/
3 e, \- M5 `3 d4 l- T9 c将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。' Y1 R) I6 Q; q; d. {8 T

3 t+ V! x+ C" s4 z. D
. E3 ^1 }. ]' `& S$ vim /usr/local/nginx/conf/modsecurity.conf
4 g; ~2 p$ |# f6 j$ rSecRuleEngine On3 ~+ I. n, I, m9 s: P+ i' \  l! H
ModSecurity中几个常用配置说明:! c# _% w. ]4 l' m' |4 Y) p

' ^8 i9 L9 c8 X: q' u
. Q$ X2 x; u9 C1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。- h, h0 S8 p/ U% H: S
1 z7 p' ^9 J2 d2 v' t% U4 T! \
, Z% O5 u# P* t+ S  _0 d. \; u
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。: T! V" q1 m: z
( r4 n( P0 ?1 S3 f& |6 C% q

2 v, d/ G& U$ V" c# s. x6 N3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
- Q( {; y+ V4 e2 \) j$ r3 l- Y' \: M$ ^- X
  L+ D6 `& C1 u& E
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。6 ?4 _$ ?8 q2 v

  R2 l9 J. H! y3 H/ v
* |8 A% c, ]& u. c. x  p在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。  Z/ ]; v4 Z: F, u- C* r

1 O2 r. `+ Z1 U. Z) S
+ {' E+ n6 O9 y3.x版本CRS, m+ @! I. {9 ^$ S1 ]
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
/ X* c) b  A3 I( }/ N# 生成例外排除请求的配置文件2 l' d4 Y6 t# V+ j3 Z  g
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
/ |6 n( M, `# Y) X& I2 C3 ~$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf  Y" \9 |$ o1 M6 r
$ cp rules/*.data /usr/local/nginx/conf
: `+ b! [8 y9 G0 ]% Y- l9 m; Q为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
  y. I, d( f' t8 t
+ I& `$ N  H1 G9 x' S8 t7 G( L6 z* A& s: A  E+ j$ s& |
$ vim /usr/local/nginx/conf/modsec_includes.conf! t$ }% f! p9 a7 \
" p, U6 S# i7 S3 }) L
[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

5 T+ [5 i5 g, {9 }4 K  \! b8 I
: X4 h! l3 H8 x注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。' ]2 g! \8 g& W7 ~- F

$ g: V2 ?% b) W& `- {* o9 ~0 |1 I; V/ V
3 s+ B, c$ _( x/ u3 N: [配置Nginx支持Modsecurity- Q$ ~# d; k0 J- V- w
启用Modsecurity
2 f5 m% Z* C# y' W2 C3 @/ X8 B使用静态模块加载的配置方法
# n' s9 P% m5 V1 p  e在需要启用Modsecurity的主机的location下面加入下面两行即可:
1 Y4 _) h5 ?& v9 I+ ^# t3 O& g
) u! H% j. y) D2 E3 K6 u
0 k# P  ^7 o. Z* t! D5 VModSecurityEnabled on;
+ w7 [9 ~5 m  M# n2 p5 _; SModSecurityConfig modsec_includes.conf;
& t) N; O: D3 y* G" _; k+ F8 `修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。& J# _3 V- ?' _

' o& r1 f5 N- ?5 ]) ^; [. e6 Q) @8 V0 o
$ vim /usr/local/nginx/conf/nginx.conf
2 |' t5 B& v8 i8 _) X; Q+ V9 G
8 E- o$ g4 [% b: ^1 m
( `" U0 S. b8 C: O' X* A% fserver {8 s, {1 z6 O4 g- Z
  listen       80;* U# d" B0 E. O) J( u# f* L
  server_name  example.com;
4 b3 u0 `2 J8 n( H6 K( I% j
" [9 [2 S2 q/ w+ h) F- ~4 F: ]7 d$ j8 O6 O) z) f% Q- ~- J  Z
  location / {
- G0 H; u( Q( D0 y, @    ModSecurityEnabled on;. s' m& f" j4 C- y
    ModSecurityConfig modsec_includes.conf;
4 ]0 A  ?, w& q! J: V# U" ~    root   html;
4 h& `* Z; \2 b" P" _7 g% G5 `    index  index.html index.htm;
4 M$ p9 D7 {) `5 g7 I  }
  O( t) d; w7 x. e' f9 k}9 \- K& S. o9 R- R6 @9 N# C
使用动态模块加载的配置方法
& \* L* |8 a; R. ?在需要启用Modsecurity的主机的location下面加入下面两行即可:
# ]  `' q# I# z) S  Y7 A/ Y+ b7 J0 ?' o! [2 T
+ g& x/ P% `: `
modsecurity on;5 A& }9 z1 f0 u( F6 Q
modsecurity_rules_file modsec_includes.conf;
7 D( O7 E' S! C0 |& t: h% N修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。9 K; F/ [, J8 i
7 i- V" J7 _" H: x- m. F
9 s0 w5 J+ U- g, y1 V
$ vim /usr/local/nginx/conf/nginx.conf+ N# d6 D+ ]# E. x
0 f# l6 `/ c* y

( S- m, t/ b/ D" B4 userver {
, I7 L( Y! d: r! P  listen  80;+ t. n+ d5 {1 H# R
  server_name localhost mike.hi-linux.com;
: Y( A0 s$ d. w3 d& i4 X* M: `  access_log /var/log/nginx/yourdomain.log;
: C1 T# `- ~/ S& g
1 n* b/ I& l) L1 o
, g+ g+ a/ d; @+ j1 e% D& g) f7 Z  location / {' P- x8 ]* j9 u5 p" c; V
" v3 T$ _1 _: p! d% L& i) {

4 F4 S$ G* x' z  modsecurity on;
$ \' }: }6 V5 A) x( n  x& I  modsecurity_rules_file modsec_includes.conf;
" ~5 ]+ D3 W. L  root   html;
3 ?& i% j9 g# a) \9 _4 q  index  index.html index.htm;
/ f  p" k7 U# B}) n6 L' k% \/ s7 ^
}- m  P' X* q/ m& t
验证Nginx配置文件4 A" F! ]/ z! E2 L  b6 E7 r" \
$ /usr/local/nginx/sbin/nginx -t4 s8 W/ I* k7 x5 I
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok/ b' z  u0 D3 ]
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# U( n) z# _; ?0 {* p9 c2 R启动Nginx* |$ A% O+ e4 d; V
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
5 m0 s; S5 @/ V  o7 \
0 n& k8 U1 \4 W2 P( R

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


: F6 D* N$ t* R( g& k- J

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-2-14 07:18 , Processed in 0.070699 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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