找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10881|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。" ~0 t9 J6 I- [6 }1 c- `# p2 I

. X) i, I  h6 \- C& @, v) e. J9 Z
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
& K" [# c+ z& W4 P: K  C+ c$ o+ H$ l) `0 z/ z' o$ e1 C
2 e. H1 |3 ?. |9 C' D0 R/ R# I
什么是ModSecurity+ `, [8 R( S0 q' U8 U8 s! X5 B. y
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。. `! E4 X' ]2 y" r; g2 x1 ~9 I9 h

. s* D' Z, F5 C/ Q
8 V9 c7 s, r  pModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
' V! |  }- Z1 d" r6 r
( e) l* e, D" ^" h
& }# ]- }6 r" V5 e1 F. QModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。9 D6 _0 Y8 R7 e2 o$ C: W0 d

: k1 Z! l$ p2 W1 g' {  C3 M' Y6 w; \+ j0 P8 d
官网: https://www.modsecurity.org/6 a  m" Z1 X0 c) s/ r! ^
# d2 b6 a/ E0 y4 ]" l" R

& \; g* J. y( j( N( G什么是OWASP CRS4 G  O- e" W3 `+ q/ ^5 `
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
8 M( J$ g$ d8 C5 w# K; a
. |6 X; M  p! q( T0 h" Q6 R( t' n; z# S
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。% M- O6 Q4 D0 E2 k. E

1 g9 b( F" f; l$ e2 S% v# Z/ p& N8 G& Y
HTTP Protection(HTTP防御)
1 u+ {, D6 {7 U. C$ BHTTP协议和本地定义使用的detectsviolations策略。" C. \: ?) ]3 t( R. d( e/ D1 H
3 B( h, }9 S: L$ ^7 K
7 ~: R* q2 b0 q
Real-time Blacklist Lookups(实时黑名单查询)
, r2 |' Z9 d, x* s. I9 b, g6 B& H利用第三方IP名单。
3 v. Q  R8 v* |; T& y- M* p* e: ?" ?4 g1 Q
: {& B5 r% M! q5 H9 m% W
HTTP Denial of Service Protections(HTTP的拒绝服务保护)6 v) F9 J' {% b( Y7 Z
防御HTTP的洪水攻击和HTTP Dos攻击。& m3 s9 e( E  `% x2 J0 J+ i0 H

& Q" S! }; z' \6 K, {% ?1 V4 V. A% _4 C
Common Web Attacks Protection(常见的Web攻击防护)
/ T8 Y4 f8 E) {检测常见的Web应用程序的安全攻击。
5 [5 P! |, k/ q: f! ?# M
) R* r, i1 |& h' n' i7 j! {5 T( i$ V, g! Q
Automation Detection(自动化检测)
; `+ o7 E9 A9 p$ B+ Z- w' O( n  Q检测机器人,爬虫,扫描仪和其他表面恶意活动。
0 _* ]( e% q# ?' z% c2 X+ k% S2 C: O: w- S
6 f( P4 q* Q# Y  f* }$ _, j
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)0 Y+ W+ P6 Q- O5 Y: v6 q1 _
检测通过Web应用程序上传的恶意文件。1 u+ N  x: H6 c3 ~4 P. k
! k9 h8 Z- A, U2 h1 Z7 f
& I, E9 Q3 N1 `# |
Tracking Sensitive Data(跟踪敏感数据)
3 z0 t9 C+ t# p8 I) i! ^信用卡通道的使用,并阻止泄漏。8 @7 v/ I7 F4 ?, Z* k  B
) C  N1 v! D, ]4 t- `; a7 t; f- C. ]
' ]8 Y/ R. e6 K- a
Trojan Protection(木马防护)
: z  J) v. x) o, m3 S检测访问木马。
, p- K) S  J; v6 Y) F' B, ~6 A) j; m2 y1 I% ~  V$ l
) Z& `$ m% v* k) o, z8 ^
Identification of Application Defects(应用程序缺陷的鉴定)$ I: M# j  F; o* M2 `4 ?6 o5 q+ w1 ^
检测应用程序的错误配置警报。" g4 @9 M2 g$ z( x

. J1 J7 A! Y& q4 y( P, b( S! O* p! \1 }  d# P5 [
Error Detection and Hiding(错误检测和隐藏)5 j. w0 S3 d3 D
检测伪装服务器发送错误消息。
9 x1 E6 T2 t6 V- X; q! N4 Z: a  i5 [
! h! ?& Q5 m6 t* G0 U" C/ {% ~
安装ModSecurity/ D6 q" j  w0 ?
软件基础环境准备" m$ D9 L7 \6 x5 U9 t8 I
下载对应软件包
3 q, F" _- V$ r9 I' ~$ cd /root$ X7 _+ V9 Q: G8 `
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'  ~- I9 l) t8 [  A6 e+ Z
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz9 ?! Z) c0 n3 n- Q* j+ D+ }9 p9 V
安装Nginx和ModSecurity依赖包
- g) @" o, f: Y% }Centos/RHEL
3 ^9 I. b) x1 m; k0 E2 e4 @2 Z& n3 q! z
* Q0 h* L9 l0 U* Y6 Z" Y  k' ~) y3 K
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel& z; L: n, M( H2 v' S* \& A
Ubuntu/Debian
: [  V2 W+ R- v' {+ w
, P4 ]. v9 E& W/ o3 t
4 V& e7 Y4 u! D: M; |$ 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
% g. ?, S+ F2 z1 }9 \( G3 Q" E4 A编译安装ModSecurity
7 `' R0 O. ~; G4 l7 F: ENginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。; D" ]+ j% b) q- A* ~4 H  U8 q

- N2 R1 r- a! @
" U+ @. P5 U8 }; @; \方法一:编译为Nginx静态模块7 R& ^/ Y, ^; x. o) o  f7 A
9 D; \: W$ i7 M8 [: H4 C3 ~
& p  x; }! Y5 p; ^' j" V+ m
编译为独立模块(modsecurity-2.9.1)
( @% ?3 k1 U! n& }; h$ tar xzvf modsecurity-2.9.1.tar.gz
9 e1 p, e6 s: p) u$ cd modsecurity-2.9.1/) [1 m( N8 o. C7 ~: [& A+ o
$ ./autogen.sh
% `! s/ n9 L# U$ ./configure --enable-standalone-module --disable-mlogc
6 U4 |( |5 }; W0 C/ c; _# L+ P$ make
, V% u- p8 v1 [, i; i- w编译安装Nginx并添加ModSecurity模块' o7 a8 o- ?# H6 a$ t: w0 `
$ tar xzvf nginx-1.9.2.tar.gz  P2 n, I) e4 d8 p3 l9 H7 ?
$ cd nginx-1.9.2- @, {! M2 D1 v2 v( {% ^2 T3 z
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/9 G, h1 n2 W- @6 ^% Y# `  K+ M
$ make && make install
+ S% B# v2 d' o1 W+ @: j方法二:编译通过ModSecurity-Nginx Connector加载的动态模块8 \* g4 M8 @5 h

7 m  X- p/ s- y  q' g1 Q; r$ B6 O8 I/ ]+ P% l9 ^7 S/ ?
编译LibModSecurity(modsecurity-3.0)
" w. i! P* J$ T: s$ cd /root" G1 k0 N! m# @1 R
$ git clone https://github.com/SpiderLabs/ModSecurity; p9 c: y1 h  d( {  _- \% R- \
$ cd ModSecurity
% k, M- S7 I. P# p6 C( `$ git checkout -b v3/master origin/v3/master, W8 G1 Q" o9 V4 H: O- |. U
$ sh build.sh, [: m7 @% ^  F4 o
$ git submodule init
6 g; j% z. `/ D, ]* p$ git submodule update7 I+ U& S( S6 J
$ ./configure
) ~0 A, @9 l( @! k$ make
# n: f% z; Y- L) C$ make install. l( B: Y! D; m6 I/ h: d8 {
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。- e8 P9 t; {" ?$ W: v; S. _  Z3 g8 I
. {/ e' j, m' A
/ I  r, Q  |# M! V0 B7 F2 {/ ?4 T
$ ls /usr/local/modsecurity/lib
3 [: U4 u9 N* F* O+ Ilibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
0 }- O, C$ c  O0 h/ L0 H; c编译安装Nginx并添加ModSecurity-Nginx Connector模块
2 L" s6 v. B' I, v" q3 n使用ModSecurity-Nginx模块来连接LibModSecurity; x8 e+ N) [9 Q9 g6 }

: L: ^) [% i7 L7 d; u4 D0 b8 K3 @) Y$ j8 ~1 J
$ cd /root
9 |8 |5 h( f0 d4 d  J  P0 C7 p$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx( J9 w3 U6 c7 r6 t4 `- C6 o. }
$ tar xzvf nginx-1.9.2.tar.gz
0 P3 U; _3 P+ F$ cd nginx-1.9.2
$ e- y  B* d6 l' K* `$ A1 |0 E$ ./configure --add-module=/root/modsecurity-nginx
; t. S! ?" s1 N0 L+ Q$ make1 K3 i3 l- j* ~, K; J$ ]' z3 P
$ make && make install
, C5 j' o) t; b. D) _$ M添加OWASP规则/ n8 @. `' u& {  x  u
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
. K4 k1 f$ C7 U, L0 F4 o
3 @7 B6 x6 j0 O1 p/ r; @  q) N" K
8 d' Z2 t$ |3 D$ Y6 X5 j$ |9 B下载OWASP规则并生成配置文件$ y; c  k" b9 y6 r, n: N
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git) R+ x- q1 S. ^: j
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/- V7 |. w  n$ h0 ]  ~8 i, @3 u
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs5 H% n  _7 h8 ?! G) U
$ cp crs-setup.conf.example  crs-setup.conf
" y( ?7 x( b# Y: {: i6 j& F8 ^% v6 @3 @配置OWASP规则7 d6 R% f5 O) I+ }0 {8 ]
编辑crs-setup.conf文件* e5 @: ?* C. c

# }" g# S9 d, ~# i* |' Q4 y
  R5 T  E& ?; `7 ?1 J$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf4 I  T2 n( E9 D, D5 P0 G* L; a; ?
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf" ?7 K  p: s  [
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf0 @4 Z1 i/ m. z' s' M
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
- N" ]# q& O5 Y1 k默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。2 [' U! m+ u+ N! |
! D: O* X. F& u' s# M3 R4 L

# J9 }, j5 m. j$ W: L, z0 }. r4 G启用ModSecurity模块和CRS规则9 s% X8 w' q6 k, K
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。6 g7 @. A+ W: B
, a2 R$ B  L: W2 j
$ z( u5 V( u! e2 H0 X
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。) Z% `) v# \. Y" G/ M) E; v
' S( u4 J/ y, g, o

, m( m6 Z$ e& c6 M$ cd /root/modsecurity-2.9.1/
, z) I9 P/ ~  j4 j( r$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  * q8 ?, y0 _9 `0 u$ f
$ cp unicode.mapping  /usr/local/nginx/conf/4 {+ T& z7 Z! V' x- x
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。# T" r4 ]( V0 d/ m+ @
( F8 I. ~/ f2 t+ P( f

$ W; L, }- U/ \/ R$ vim /usr/local/nginx/conf/modsecurity.conf) j* Y4 P/ q; ^. m' Q" E4 Y1 R
SecRuleEngine On
* M) ?5 e4 g8 q  LModSecurity中几个常用配置说明:
9 N( ?! H2 R, q! s7 g% r- [8 i# V3 j" }, ?

* n8 c4 D) L1 p3 }0 H, H9 }1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
/ Z" B# \& W4 |/ P" n' x1 t. B6 h6 ^; Y# S
/ I' v' R1 P/ d! N( D' Y
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。" R+ V1 B0 E& ]' p# x2 c; B# B

' g+ W- `( |9 n5 j
9 \# E# ]  W  N% q- Y3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
! |6 D! u' q+ g8 T  u: u  T1 ~" J
4 Z! I+ Y; c5 M
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。; d7 t, q2 h9 D: h( @6 u

% @7 ~  W* N7 s# F
% ]/ Y" i7 o6 j3 X% R  c' U在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
; q" H- j. L, a5 g. p4 H# Y: R  C' R0 J

0 W2 K2 `' q; {7 M3.x版本CRS; {: _) S: L  W
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs, ~% k; {) J  K6 u- g
# 生成例外排除请求的配置文件% s+ x5 }- r1 S$ D: J) a6 ~# g( O1 g
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
* h# ?# v; T$ \- ?$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
6 _7 G0 \0 N; M1 ~6 [$ cp rules/*.data /usr/local/nginx/conf
; E& k/ Y" V- t- h8 F# A3 S为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。. j( L: E- o/ c8 M# J
" Z2 n6 W* j+ m  T  t- K

3 A# n1 w0 @, s! x& M$ vim /usr/local/nginx/conf/modsec_includes.conf* u9 a5 Z- @. E( m1 d

+ @# S6 \* |" g  u/ ~% l; |* M% S/ A
[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

7 t! Y7 M" k" o  K3 }
# A0 s+ P" c/ S3 F5 k+ f注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。1 u# W0 K; D4 {. t

. Q5 B1 ~7 r  [8 B! B0 J5 }0 J$ p6 k6 ^" H
配置Nginx支持Modsecurity
, `; A" @9 }0 Y+ Z2 c4 F+ j: W启用Modsecurity2 f7 ~2 X9 Q' Y' H$ a* n
使用静态模块加载的配置方法
  X  D* I- ~* Q" X+ R* I) |& w在需要启用Modsecurity的主机的location下面加入下面两行即可:5 ~+ M/ x$ w" N4 {+ i
) J8 L) C  K8 E2 G- m4 x( D# O
/ S6 Q4 z+ X: r2 ~# ^
ModSecurityEnabled on;
9 W3 N! {: u4 U* M- nModSecurityConfig modsec_includes.conf;
. y" S% M9 P. v1 v$ G9 e修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。7 @+ @6 E6 D: v6 s: ]
3 i+ N. g7 S0 Z
% ?) }' u3 e+ O+ x" t# a
$ vim /usr/local/nginx/conf/nginx.conf
- y% U- ]4 p. ]' y) j
5 n8 A# i+ \1 d& T% F2 l1 T2 h; b
$ P$ }' D6 s( V% M  ^" lserver {/ ]. a) E% D- [' s& ^6 @
  listen       80;2 O/ X! M9 O4 n  A- k
  server_name  example.com;# g2 a. ^2 r3 o/ O" }, S
$ h* u6 O- F8 p. k

  S; t2 v% M3 a2 A$ X: i- x  location / {
$ n7 N3 M% L! r    ModSecurityEnabled on;
* p6 w: y. O, f    ModSecurityConfig modsec_includes.conf;7 o3 x: e- O) {
    root   html;, Z  E5 H' C# m. v+ y# d6 Z
    index  index.html index.htm;
/ ~6 A4 ~/ R: v3 w  N  }
) ~* U% T! {# a5 I- [; P}
# K; s9 k' i; G. @2 `) Z1 k使用动态模块加载的配置方法' ?" \- Y& R0 |% A
在需要启用Modsecurity的主机的location下面加入下面两行即可:+ N5 u$ o6 f# Y4 H+ F" A5 L% O

2 n  `+ K5 D- `0 y8 }7 `3 j  |- |: K- r8 p
modsecurity on;0 B0 u- Y3 C5 H3 \
modsecurity_rules_file modsec_includes.conf;. d: d5 i+ J) ]. e0 T& g2 \
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。) G$ K! B) ^) K0 |7 h7 g) f

, O( }. W: X5 C& v) w- m* u
2 [3 H3 H  o1 }' n* K$ vim /usr/local/nginx/conf/nginx.conf& O$ Q2 v( L" p

& \" ~1 F( Q$ |3 g7 N8 _7 S' H" U: x# A
server {
' U2 M& W( J4 [! z$ @  listen  80;+ @9 r! ~' I" a# h5 H. V' @* a
  server_name localhost mike.hi-linux.com;
8 B' X5 s( Q4 I& b* n$ `7 M  access_log /var/log/nginx/yourdomain.log;8 k" g0 A3 F" L% t3 n$ N5 x  G, I/ s8 y( f  w

9 v( r2 x# W- @' E- X! ^( l1 Y! X( R9 U/ b5 W
  location / {' }0 D: E) ?. B% o
0 b) S. l  y1 i6 s+ {, i
# t0 j. ?5 p2 @6 O. U
  modsecurity on;
0 L% s, p! i! ^1 m  modsecurity_rules_file modsec_includes.conf;( e1 R  [$ B9 Y6 Q/ Z
  root   html;
! s  Z+ i! w0 o4 {1 h: B  index  index.html index.htm;  |( _4 S, |- T* C
}- r/ W* p; b' J5 k
}
1 Z* w% Q: P7 Q/ e9 F验证Nginx配置文件
, w3 W  Z# x+ d! l$ /usr/local/nginx/sbin/nginx -t
! T1 h0 ]: ?  @: r& N5 X# Z5 jnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# ^; A+ d/ N+ r! Znginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
1 g2 q% E) r4 {' ^4 L* M启动Nginx7 n! c2 s8 j# x
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf' B2 k" Z+ O- C' _1 ]

6 V: A2 G3 _: G$ `2 r8 _

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


6 }! U7 z4 Y: p+ j

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-4-19 11:10 , Processed in 0.039085 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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