ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。( l$ b V9 X( b7 C# s
d$ f9 L+ W+ y5 C' a
! y( E+ B/ C' j" ^ S7 C* R在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
" T4 v; N; s" l) f6 E
% m& ^- @* {, F4 k+ L8 s1 A% x% Z* \9 ?8 d' H
什么是ModSecurity
: A9 C/ a4 f7 `! m( dModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
$ _1 x+ |( b, p& B9 e
6 Y7 K! n' N! ]* j* x$ W1 U2 A
9 w# B+ U2 Z$ T4 f* l$ a! nModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
% F7 Q o6 ]( s' g+ O. B& F7 c" N) ^" @0 ?7 L0 u
" Z" x0 U# |: d; d; _ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。2 a8 h% A. n. ?3 o( C2 I1 j6 k, |, h3 F
* W7 \0 }# j% d! ^3 `* h
0 ^, ?. L6 I, Q8 ^0 J
官网: https://www.modsecurity.org/
# S6 k g( \ u, b& G/ H* ]! w
( k O3 x) s: W0 R) R) _& I/ z: @: }$ F0 X6 S" s9 o. |7 i
什么是OWASP CRS
# z- o0 d8 ?8 _OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
6 A- X! J( p) Q
" X9 q, Q! T5 k& `2 t* w
4 e/ r4 g, @* V. U* @7 pModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。: _5 _& O) N4 @/ M" `* s
7 l7 O$ ]2 w' j
6 t$ Q2 f# S q8 d5 k% J
HTTP Protection(HTTP防御)* K" b8 ?5 B( I0 m
HTTP协议和本地定义使用的detectsviolations策略。
7 h+ N1 t4 M" `3 C: z
: W) B& V; I- Y
; s: H- h% e% x# kReal-time Blacklist Lookups(实时黑名单查询)
) H$ Q' {1 X" H2 Q) ^9 G' U利用第三方IP名单。
" |5 H6 R; M" z' e/ Q& s9 ]7 N0 S9 W% V: h) u# j+ w( m
& u2 j( j% @9 ^! ?0 M' P8 e5 V) ?
HTTP Denial of Service Protections(HTTP的拒绝服务保护)+ u* E! O- L+ N: O$ |( n9 L
防御HTTP的洪水攻击和HTTP Dos攻击。
: u$ r. D- G* z, V# e+ j) n# L; w: p
# `- H( y5 v- ]2 S. X: g: VCommon Web Attacks Protection(常见的Web攻击防护)1 E6 |- s/ X! S4 A5 z
检测常见的Web应用程序的安全攻击。
$ c! a2 l: B5 d2 e. C( {0 a" x+ i3 \/ x' W2 k5 P
$ m' T+ ~" U) U6 J. o# v p
Automation Detection(自动化检测): }: E: o& s% f ^
检测机器人,爬虫,扫描仪和其他表面恶意活动。7 D; q* X1 S6 @# @ S# c* ^9 o
. n% x" I% [4 ~0 D8 ?/ ~) X' m9 E( s. I( e& H1 G4 d4 W& G
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)7 a5 f7 I0 [- o; F- ~* U8 U
检测通过Web应用程序上传的恶意文件。0 ?! p0 M5 f3 c) D
" M6 g4 r1 G/ E7 P
$ K% L# X3 x* PTracking Sensitive Data(跟踪敏感数据)2 U! P8 K1 a9 Z$ n% o
信用卡通道的使用,并阻止泄漏。2 @" f3 y' u3 \& j8 f
: m9 @0 f8 [$ K9 O
& o Y; w: x$ ^# h% {7 s: |0 H; tTrojan Protection(木马防护)( F3 V/ t" r! E2 ^/ s, P
检测访问木马。1 [5 `8 t) X! z9 J# Z
& ^) G. R0 ~( f- |, a# K4 R- \
% D9 r- x/ f/ e" s
Identification of Application Defects(应用程序缺陷的鉴定)( J9 }' O# O7 g. a; F) ^9 V
检测应用程序的错误配置警报。6 y/ X) R0 f3 _+ ~
L" W2 S0 I& A3 J- r# |0 F1 L# T$ u
2 p3 u' v! [. b$ ]' G) k$ ~8 JError Detection and Hiding(错误检测和隐藏)
( j- L% ^! w- X; W: E! f( O; |# p检测伪装服务器发送错误消息。* d6 }% w7 C4 a+ ]9 J, p* H
; c2 V& |) X9 s% z- k. j; T
( @5 m8 Y7 G; |% r5 b1 J
安装ModSecurity7 ?, t+ }; w( n3 H3 }) }; p$ {# n3 n: B
软件基础环境准备! o6 u# z& v1 B
下载对应软件包
* u! ~- C a: I( f4 ^4 Z8 G( Q$ cd /root
' G/ Y& E# i# G2 m/ r$ S$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'# J% d4 J- [, X2 n$ V( c
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
2 M% R: F5 X5 l安装Nginx和ModSecurity依赖包* \) \- Z5 I6 w
Centos/RHEL) Q* X7 }3 c4 g, D6 R7 ~
5 u% F- Z: k5 l* ^
0 X. ]4 Y& l1 p: N) k W; `" \$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel% s, l! t0 ]( N3 `
Ubuntu/Debian
1 R3 B( {( b( A7 N! H. z
6 |: E. S$ ^6 P$ q. [8 `6 P- g8 W8 ?+ R: n1 R* X. j4 e* @6 E
$ 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+ E8 y6 C6 s3 u& p
编译安装ModSecurity2 H3 B5 ^: q5 P9 ~- t+ T" F* _, N
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
% ]& y D; S1 u5 b% q, X! I4 X( y' s) P y8 B
( X& W1 p( o+ ~4 U" I方法一:编译为Nginx静态模块7 x% g9 _$ y/ i7 U
6 @5 I8 j7 c* C) C- L
( A' r" v7 W5 n; j6 P
编译为独立模块(modsecurity-2.9.1)1 u* p2 q: L6 F7 i3 G0 K4 U" n
$ tar xzvf modsecurity-2.9.1.tar.gz
3 Q/ B0 G% A, S- ?9 \$ cd modsecurity-2.9.1/9 j1 a5 a- s; T9 L
$ ./autogen.sh" @! ^% u$ d6 z# J/ p
$ ./configure --enable-standalone-module --disable-mlogc3 f5 U, `( m8 |# `& Z
$ make
) I" C4 z. u5 a5 c! [" C编译安装Nginx并添加ModSecurity模块* R' F$ `5 p" ~0 I v; {$ b
$ tar xzvf nginx-1.9.2.tar.gz3 l4 ]6 ~# @: R+ G
$ cd nginx-1.9.24 L/ q! H. c8 L6 M
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
% a. I* N) {1 I4 F* F/ M0 J$ make && make install
8 |) ?) R# x5 z i5 U9 L6 v! ~方法二:编译通过ModSecurity-Nginx Connector加载的动态模块$ b% e7 C( q; \, f- y
, A) `& U1 T0 Z& b
; z3 l& m+ R) R5 e5 W9 G3 M6 j编译LibModSecurity(modsecurity-3.0)* {; F, v' P8 Q1 N4 E2 y
$ cd /root
, z: M/ y* }5 |" h; {0 [$ git clone https://github.com/SpiderLabs/ModSecurity
0 N9 [3 t9 W/ r! Y5 s/ j" n. d$ cd ModSecurity
+ I9 ?7 P2 a2 y$ git checkout -b v3/master origin/v3/master; t* ]. }7 W5 D5 \6 q' s: z! |: Q
$ sh build.sh
5 l# |# t/ q7 }! f; n6 \/ h: `$ git submodule init$ J! X0 |9 k; L
$ git submodule update0 b8 N' ^3 Z% g8 `# U
$ ./configure
! i8 a* O' Q5 Y* v" _3 v& \: v H" n$ make
# O4 m1 i2 w/ w2 S. f; \4 _$ make install
2 J) p0 g p- x7 dLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
3 g% g3 q0 S6 o0 ?# V, h+ d
M) G' ?) f, n( l9 C. [5 ~
( H2 _* `$ |& m' v3 `* @" [' t1 u$ ls /usr/local/modsecurity/lib7 A9 ^, h0 z# n
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0* O9 L! F: }- I& z/ X
编译安装Nginx并添加ModSecurity-Nginx Connector模块) |8 n- o" H: y5 z$ t& ^' A1 l! G+ U
使用ModSecurity-Nginx模块来连接LibModSecurity
) D8 j8 o# j* Q+ z+ q; i6 W2 o$ d5 [" `' H% R7 b
9 O0 }2 H; I$ w3 a- [$ cd /root" G2 d2 M2 ~( a e8 P: w) r8 x
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
% N3 N, Q# e% G) J/ C) v$ tar xzvf nginx-1.9.2.tar.gz! o% i9 v# p6 u7 E$ @5 o
$ cd nginx-1.9.2
* \; {$ D+ l% H% B1 r$ ./configure --add-module=/root/modsecurity-nginx9 _4 e8 V) t8 Y( Q
$ make
5 C- t$ o$ K: ^" l$ make && make install- a* |) j7 L) J w1 A* W8 I5 ~
添加OWASP规则3 J7 ]8 A3 e4 F+ A7 f, _ ?
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
/ [5 |3 q& Z2 ~: Z x3 Y. J2 Z# u* ]6 f4 }4 t* C$ }6 v" e8 u
' `! ?$ E' `9 D: H下载OWASP规则并生成配置文件
. b' l" ^- d8 o. n0 \$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git& w q+ z" |' c$ W- d5 ]
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
8 ^4 S, G- _# ? N$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ L: R$ n* x0 f( B# X) e! ~$ cp crs-setup.conf.example crs-setup.conf8 G- X2 {9 M) y: ~
配置OWASP规则
5 s7 J2 S0 f* A9 R- j Q编辑crs-setup.conf文件/ e2 _4 @ E4 J( d* w% M
T; s: [0 e v# t& o0 Q! D: c: B( ^- I/ u; c+ k( P
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
; X. y) D9 f+ M0 o$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
, U, x; T/ M$ b$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
; q1 j# x( s2 R6 S! @8 q: l9 [7 i$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf% D5 a. C% v( Y7 w* F6 b3 p
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
. D2 Y/ _. h% t; A
* b1 X# R$ X' |- K+ {+ w
3 r& l6 c2 F- Z1 d9 w启用ModSecurity模块和CRS规则
2 r' z& z; w3 M( l复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
- O7 F/ Q7 s) b4 b& v
6 f6 D: o9 U' T |0 \, a( n9 L) j# V# S" d
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。8 M; b+ F5 h% g) E
4 f2 e2 ?8 ?5 r" U6 J. F6 Y6 p8 v5 R
$ cd /root/modsecurity-2.9.1/
u9 w/ n* U, Z w' P! }$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
9 y: S: V& q8 |$ v) R$ V7 z9 u$ M: |$ cp unicode.mapping /usr/local/nginx/conf/+ y% p+ h6 P# h$ S6 k6 K9 a
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
7 U! L, D/ [' e/ n% R: B0 I% O' i
5 M' b0 C9 W* j: I
3 |+ O* x g! C, ?/ Y$ vim /usr/local/nginx/conf/modsecurity.conf
4 S8 s) O& }0 E5 {" wSecRuleEngine On7 d3 a' @7 R# i, v
ModSecurity中几个常用配置说明:2 [& \& {6 {/ x$ X& V+ S
) g, {5 U8 u( Y( D/ \/ |' A8 K. j. @
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。$ m0 T) K2 d. l* K$ D
: [# j& K, ` b( R8 O2 E* _; O& h3 C2 }+ z
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
) c5 |. `0 n# f3 w% V
' W$ R& h3 i" V2 ~2 W8 p. s1 @
- H7 m3 c2 p/ ?6 T3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
+ W2 h- j7 O; ]# y1 y2 P3 @9 V9 J, [. A) ~+ F# t2 `# H( v4 S* i
4 c$ |0 Q, J0 m4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
, Q1 t: C: ]6 F; u7 P# B: x7 q' d& h7 V+ c2 N) D* l q
+ L0 G% W. }( [# c, ~
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。' _) W0 u: l5 M! z7 X/ @
4 \! ~- K' V' j: G% o* O5 @
1 u8 x' _& j% Y8 [# k
3.x版本CRS
+ x7 Y$ H- _+ q, D. d3 T$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
, ^7 y3 ?; x7 i" A7 N# v1 G+ \; p# 生成例外排除请求的配置文件6 @$ c- V( W* s( O2 @
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf0 x* L9 t. T9 V v
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf/ A" H; T, o4 `4 o2 q! g4 F
$ cp rules/*.data /usr/local/nginx/conf
0 e+ v. p+ v# {$ ~" b) y5 k为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。5 e2 t/ j: c& E% m5 B: h; y
/ J0 _, P4 N X; E
* ?9 l& f0 T' p$ vim /usr/local/nginx/conf/modsec_includes.conf' A) |2 S+ {0 k/ ~2 E" B
7 B: t0 Z9 {% u( n, x[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 D# J! E6 |5 V7 V, C8 o7 U' U
; {; Y1 @, Y8 N. B& K注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。$ |' H, R" o: M/ m' q
6 B4 H9 h I; p! A' m# V+ v
( |, p+ {% {. A9 M1 _1 K, `$ a* l4 b配置Nginx支持Modsecurity
?* B% P1 t! J9 o. i启用Modsecurity7 S. q. A6 w* j. m. W( }
使用静态模块加载的配置方法1 d- R0 y e. a3 ~$ c3 ^
在需要启用Modsecurity的主机的location下面加入下面两行即可:
# _3 D7 Y) d8 u0 V: ?; ^
# c* g' W1 s7 i5 X/ S r2 s5 v! b: w
ModSecurityEnabled on;
. D* L0 `9 Q0 J2 x4 t# nModSecurityConfig modsec_includes.conf;% w. @ d( f; \+ o
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
! x: l f. e" _5 H2 ]7 N: H: a( r& V$ R4 o: I3 {
: y9 V/ R" e, M* a( K6 @, `
$ vim /usr/local/nginx/conf/nginx.conf& o f! N- O& I& p" H
+ D/ R& Z7 B: W6 Q) h5 v4 [' S
6 e9 ^( p3 p$ y: a$ Y6 w$ p
server {
) n) ^9 l: R9 K' [8 M listen 80;
# S9 }6 m* X, J1 q0 n+ i server_name example.com;0 p1 j- g* t" i8 F$ }2 z
5 }+ T1 Q8 A( q
7 ] u/ W- {- ~. e! A2 I z location / {
# k, S+ j: D( }' n+ Z ModSecurityEnabled on;4 N$ f W, _ N) {& Z* j; y- Z4 Y: z
ModSecurityConfig modsec_includes.conf;
$ }1 O, i j1 ]" I4 T! ~8 d root html; ]& e% c* ?- r% ?1 P: i; g) V
index index.html index.htm;
% o( Z7 R0 x( P" }. L+ n4 \* O }* J$ v9 v) Q6 f' n9 q5 s1 p
}8 f' N8 J) B; q/ I3 j
使用动态模块加载的配置方法
* m% e! p- P% y: x; l+ Z+ H在需要启用Modsecurity的主机的location下面加入下面两行即可:
5 x- m ?% ]; N) o6 H. c+ K7 h; e3 E/ O8 u
6 q; |) O" @9 M; e0 tmodsecurity on;) Q8 a2 M- e0 w' w7 m# t) ?+ V# C
modsecurity_rules_file modsec_includes.conf;
; W& E8 w, @3 P7 T1 m修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
3 }% |$ Y$ J1 V6 N* N) C
+ x3 D. T: p' E% I {: z0 K+ l. Q8 v$ O+ H6 x' L6 }
$ vim /usr/local/nginx/conf/nginx.conf
7 l3 z# F: ~7 O# t& [( C
6 P+ ` T t+ Q( o9 x4 k' Y
# J" B( W0 E! _$ jserver {
* r, q+ p% Q% T! c# Q listen 80;& j6 r$ {/ K& H
server_name localhost mike.hi-linux.com;
8 a4 h2 _; c5 [/ T8 H2 c1 M access_log /var/log/nginx/yourdomain.log;
2 c9 ]( X: N6 C' u i/ E+ Y4 T; N, M2 _. G- J% H: a( W% i
8 p& j+ E. n, c location / {
; L) R9 x5 L: C$ T- i: n1 y3 B9 f
% @" \4 X p3 ]. O; Q, Z' c7 X, P) ]
modsecurity on;
0 X; c1 S4 c: p8 P4 a8 L modsecurity_rules_file modsec_includes.conf;( @$ p# l }9 y) k5 U
root html;5 T" p9 b- Y; V- o
index index.html index.htm; F9 s( p* h' m1 w$ q# \
}- W+ g5 R6 y: d1 w+ a
}( b7 t2 C5 K- y( Y/ F
验证Nginx配置文件) n* M8 p2 z8 ^* o0 J! {' c
$ /usr/local/nginx/sbin/nginx -t
- y6 e2 W3 k* H! o! Q5 y$ rnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok5 W& C- A* b, v0 T- L# A# e
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
4 b3 K$ T* F1 R% z8 ?% E# r/ G启动Nginx
- x5 g6 F/ n4 q' M$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf! v2 r$ w* ~& j, }
3 t9 ^) x4 v8 W m6 p+ t3 B
测试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.logModsecurity主要是规则验证(验证已知漏洞),Nginx下还有另一个功能强大的WAF模块Naxsi。Naxsi最大特点是可以设置学习模式,抓取您的网站产生必要的白名单,以避免误报!Naxsi不依赖于预先定义的签名,Naxsi能够战胜更多复杂/未知/混淆的攻击模式。 - ~5 z0 a/ Y2 z7 G
|