ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
" ]3 b& t' `' w: q& ^; C
' M5 q4 _& u, F" {& S4 x' w+ `0 B* X( U
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
q @* n8 s. x ~( w# g: f; l3 _) d. d' |$ I
W, c; u, E5 q# a什么是ModSecurity8 Q' D; M* T7 m2 ~2 e; l, L
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
7 Q( r' D; x4 | O: A; [4 K( Z
' P, ^7 V* {; a. V/ z! w, r8 n$ I, @" I7 w
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。( f# h. N' G3 e' N! Q" I
" z# D X! i8 s: ?+ N+ j- A% }) f0 Q+ z1 h
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。1 ]& v* Z; D4 Y/ f' \
i# z3 }8 R8 D3 C2 e
1 Z$ n# ]! q$ r* g8 r官网: https://www.modsecurity.org/
) D/ Q: N0 v) V: j
: W( b0 X: j0 I( L" a* u9 {; ]5 {3 k: ?3 d& O
什么是OWASP CRS
7 a# D7 T) v! H9 E( z; w' `OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
3 ]0 C9 ~5 b; I/ _9 L' c& \- p0 p( [5 M8 G; `2 L% n0 \0 i
$ v+ E- Q4 z6 a5 B5 O5 n
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
@( `$ X1 h0 j: S! l* s7 T+ ]5 Q+ T k1 l/ H; o: R
+ {1 x8 c: u) V# o+ \
HTTP Protection(HTTP防御)
; p$ c+ r1 _, ]8 u5 T: g: ]9 M# UHTTP协议和本地定义使用的detectsviolations策略。5 s3 J) O( O- F" \3 I0 `" d
! M5 w4 [ [5 j
& _" ?! v! S w7 `9 IReal-time Blacklist Lookups(实时黑名单查询)! x- ^- ~3 u1 O# g: ^+ U% x
利用第三方IP名单。
) b- m% l: O3 B7 N$ Q* g
. c {& q5 L* ]3 E
+ _, \5 q! L1 nHTTP Denial of Service Protections(HTTP的拒绝服务保护)* a' q. r1 K7 L- D
防御HTTP的洪水攻击和HTTP Dos攻击。
6 S0 S$ b, B% k3 `, Y& g8 m
% i0 N! q# d$ Z; ?
4 `& `3 A; N$ Q$ C8 {Common Web Attacks Protection(常见的Web攻击防护)
" x9 @5 l! N# j0 A& k4 j检测常见的Web应用程序的安全攻击。' [9 U/ }+ y) [1 t& {
3 P: o1 U5 G" H8 p6 X9 t w h
& ?- D7 Q3 Q- k- k
Automation Detection(自动化检测)' }( E9 y" A; h# ?
检测机器人,爬虫,扫描仪和其他表面恶意活动。, {, q+ y c5 b; w- i% F
$ Z- z5 z1 t6 U' P/ h. I$ E! t: v
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
" K% l# u3 J' }, n3 s$ \, f- C检测通过Web应用程序上传的恶意文件。% m+ G0 r) J& L9 r, \& J. A2 T
% W4 V, `5 G8 A9 l6 v( \
! B, T( x4 z/ @6 {0 e$ Z8 hTracking Sensitive Data(跟踪敏感数据)' }. V# Y6 l p% d, c4 Z" q+ e
信用卡通道的使用,并阻止泄漏。; U9 }6 l3 U1 Y4 S+ D6 T2 d% A
* l" ^: s3 e" X5 H
3 h* x* @3 K4 ~0 W0 w8 J, n" K
Trojan Protection(木马防护)
, y& v; C8 V) W% w检测访问木马。
% j* _: K K% L3 g( ]' r
4 G# } [1 `6 J
4 s3 {1 ?/ L2 w# `8 H7 y8 kIdentification of Application Defects(应用程序缺陷的鉴定)! D* w% l) h( p6 g8 E
检测应用程序的错误配置警报。
% E! A6 \8 k1 u1 U1 o, |) H4 @3 y8 n- F/ J) y, b
: C* e, H k1 b( E- Q1 h0 H5 \6 C. K
Error Detection and Hiding(错误检测和隐藏)
4 Y7 Q: g" H. k1 \% R0 D9 i检测伪装服务器发送错误消息。) N2 V3 ?* m) _3 Y* M
* ^, m x8 Z/ D+ y. F0 e; U
9 f" u+ _0 R a! o: ?4 f; t安装ModSecurity
- o( V3 |6 r$ N软件基础环境准备! {% T3 J( q" F* Q% E9 N5 Z/ b
下载对应软件包
$ ` p& H. Q7 k: d' O- |8 t% B$ cd /root' f! b8 z- C: Z/ C8 y g$ u, |+ T
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
2 u+ E) g" C4 @; }$ S+ W" W5 l$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz' t, R: o0 w& ]* z7 n9 h; T; X
安装Nginx和ModSecurity依赖包, ?* V2 ~' \ I. b
Centos/RHEL
2 E" u) i4 R7 I/ A7 Q
U+ y2 F+ e l8 G- _% `, J8 k( V0 L- B% D1 B$ }8 ]
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel7 ^7 q, `1 [- D, j7 _" T& k
Ubuntu/Debian) |; F, i1 ?+ a- J) a' r
# }, V/ e4 A3 w% O( \$ d) S6 @0 j2 z, |
$ 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
$ r# ?" l3 X2 Q! Q编译安装ModSecurity
' D' @# l0 B3 ?' s! C* b4 {Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
" l. J2 y, Q$ Z3 n4 A2 W) s$ A7 W4 }/ G% j: u/ Y
8 M8 x9 n, R& C) z方法一:编译为Nginx静态模块
5 G% a9 D: j& i) R" w2 z& ]( D: h0 H/ l
" l' z- g2 A7 L) x: X2 c+ \% X. I编译为独立模块(modsecurity-2.9.1)& k- g5 z0 h) {' t. X
$ tar xzvf modsecurity-2.9.1.tar.gz: P' g" o# @$ ]/ y& u
$ cd modsecurity-2.9.1/' D- j7 y# {6 `, ]" O8 j0 u1 I
$ ./autogen.sh
/ s9 ^) S7 `) t9 Y* s$ ./configure --enable-standalone-module --disable-mlogc3 D3 j+ V; u6 f. t0 q
$ make
- l% l0 Z( p8 U' r8 v' m; o编译安装Nginx并添加ModSecurity模块/ n+ ? m5 G5 y% X
$ tar xzvf nginx-1.9.2.tar.gz
! B# x4 o/ f, _- N+ ^$ cd nginx-1.9.2( j( F$ P8 J2 }8 ~$ G) P
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
) \/ p2 x! E5 G& K$ make && make install; Z4 J# ?: j, N
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
$ X( j# c' g, h' p Y- @1 l4 O/ f# Y& r
) F% ^6 v! Z* L0 q" i编译LibModSecurity(modsecurity-3.0)! `) k: `( s7 v7 u
$ cd /root
/ l% M' D/ x0 ]4 C8 S& r0 q$ git clone https://github.com/SpiderLabs/ModSecurity
+ g1 f% l5 W% d( W7 }: W2 p4 L$ cd ModSecurity, |7 ^% N) b, u1 Z& _ V
$ git checkout -b v3/master origin/v3/master
# O! S& o3 p/ o9 c& c$ sh build.sh2 C. D I5 V0 @! Z7 r( M
$ git submodule init% h3 c& i; [$ O& ?$ O3 O+ S
$ git submodule update
9 {6 W2 m2 G+ x/ A0 z' }( `$ ./configure
; @2 q& j# [* e6 L1 U, C$ make
0 ~/ `3 h* f" w$ make install" x/ |6 D1 c" O% C6 t' {
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
4 e0 f7 g5 D. O) z7 r: `& x+ T% j, m% \, }! ?9 W: N
1 h% `5 G1 t u4 {/ S, Q
$ ls /usr/local/modsecurity/lib& Y3 \) d2 ^8 H% S) |
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0+ f O7 N$ L! k( g! G+ c
编译安装Nginx并添加ModSecurity-Nginx Connector模块
6 b0 l* `7 G: e. f2 ?% _# L使用ModSecurity-Nginx模块来连接LibModSecurity8 _8 |. m0 k) [2 O
% \" a+ W- n" E" o2 D
) v' L4 W1 e8 u" m' ]$ cd /root
: X8 a0 Y6 N9 g8 ^$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
! e. @: V r5 x/ }* L$ tar xzvf nginx-1.9.2.tar.gz- @6 {( M+ R' P2 s* d
$ cd nginx-1.9.2* s' r- W6 a# _8 \$ W: k. C
$ ./configure --add-module=/root/modsecurity-nginx
6 a* w1 `' u0 L$ make2 W* ~" p$ U, c8 r4 S+ h
$ make && make install
4 z9 l4 }, K# r% t4 [* q/ ?添加OWASP规则( w. C; m% F; D/ h; |& F5 t) I. f/ e
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。7 Z# B s5 H4 u$ j# w) z& X
+ ^: t; q) M. N( x) S1 u
2 \" s6 _' S {6 Z: E+ t2 V, x下载OWASP规则并生成配置文件
6 e& ] v" D- [1 m% F$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git; h3 p) Q: L! Y: [5 }$ t0 Z
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/4 {* y7 P0 a7 u/ b. w+ ?
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs3 {1 s( x3 z5 V% _' L5 `. a4 h0 J8 Z o
$ cp crs-setup.conf.example crs-setup.conf
( A1 h' u, p1 c' V, {6 H& _配置OWASP规则
) R" B& B. u3 J8 X% C3 e1 i编辑crs-setup.conf文件5 [% |& w9 Q& H+ S
9 U0 d- }3 \# a& h+ R
2 r) p* _/ w$ r6 U
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
2 e. \4 ^7 O9 ]$ n4 b. A+ M$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf9 Y5 `+ P3 C8 [+ t1 W
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf# @/ n/ K9 |# [( ^
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
) E) W/ v$ |2 k0 [默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
" t4 f2 J: B' U5 U/ C# E# |6 P" v5 o% U8 W2 s2 ?% ~
3 z* J. E( C, m启用ModSecurity模块和CRS规则& A. h+ R7 B* p+ P/ x
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
7 C/ V! y# s1 W6 n/ t0 T5 G) {% p
! X1 H3 @. f9 B3 _/ }7 ?* K$ x
z8 C. ?, x/ _modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。- T8 X& l0 I1 t3 ^
) A/ D0 u# c7 J) M" ~0 M* L1 n$ i! y2 P
$ cd /root/modsecurity-2.9.1/
2 l. W0 `" ~( L2 E' g! H5 l$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
2 J4 b2 Y4 z0 O& K+ T$ cp unicode.mapping /usr/local/nginx/conf/+ W* I0 {4 P3 V9 D" K3 a" T& m% R
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。: k- J5 k" B- E+ O* t+ [/ D$ E
. @5 P0 Y( g O9 p# D
3 j# c- X- m: }. _$ vim /usr/local/nginx/conf/modsecurity.conf
; y8 M) N# z" V- q2 E8 ASecRuleEngine On$ v3 V, J" X& f
ModSecurity中几个常用配置说明:
! @8 u/ O1 F9 [
* ?: J5 f1 K( `. q. O' S5 h
$ N8 i$ c) B1 _7 V* x) O! [1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。8 l- E0 w8 t6 O! O+ o- S+ x) w
" f8 t4 x# e% j7 S/ {; o2 Z
" j* u' o3 Q0 Q2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
6 A$ k) @% }7 C
# o/ y/ H' l( ]9 u3 y3 f8 n' N s& o- c1 K( h7 \- G4 G
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
- }0 L- I" x! x3 ]4 ]; E, k2 f: e8 F# f' Q6 s0 [0 O& ?
' B8 t& m& F5 w4 M4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
# e8 p4 a. _2 e& ^9 g6 Q2 X7 ?5 F3 Y! ~# o( G. t8 P0 @% |4 {; Z3 f- m
% q$ z& G, v$ Z) z' F& y6 C
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
3 c/ R7 s: w* a- g& i% U0 G8 u$ d3 S. I d J
+ B# k; G: s! A3.x版本CRS8 P: ^3 o0 u, U, `/ I4 k. V0 N
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ C: ]5 x; X$ }8 s3 n0 ~( G; H# 生成例外排除请求的配置文件
- t! m1 `9 m' d! ]$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf) Z0 G; p# z3 x8 b2 t' d& t
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf! k2 Z) r1 Q3 Q) S# y1 i
$ cp rules/*.data /usr/local/nginx/conf
% ]- m# s( r* E2 `+ x( z0 j7 A为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。4 u4 p/ F/ C D9 D1 U
5 u5 w% P7 X2 Z, o4 ^5 D/ H
& A9 S& X) F2 S$ vim /usr/local/nginx/conf/modsec_includes.conf
' M+ q& a) `8 F7 O5 x! L5 Z* n7 T5 a+ \) `" Q; S! o
[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
4 s2 L. I" \/ m* X8 o( a* L, d6 H' h6 `' r5 r8 P
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。2 i5 d7 q5 m: F; T0 U- k' n: C
8 l( u c- ^* e6 D& e0 R% g5 _
* n6 Z0 z! N" t* v7 q配置Nginx支持Modsecurity: {0 j# m6 i4 E0 V8 {0 Y+ u% Q
启用Modsecurity, `# S+ r9 H: {& b
使用静态模块加载的配置方法; T: v& [/ W' E: c, j
在需要启用Modsecurity的主机的location下面加入下面两行即可:, N3 [/ q: }4 {0 f5 y* u
# m* \: d" M) G5 ]- H+ `2 U% ~3 L$ X" m( C& k7 F; Q( g u' O7 B
ModSecurityEnabled on;
: w: N+ ~+ E2 n3 X r" xModSecurityConfig modsec_includes.conf;
8 ^4 z9 N, G# w修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。% `* m" w8 I0 `0 r/ x+ y
: Y9 X: f O& P- j9 I
9 [4 w* j! a+ K$ E7 M
$ vim /usr/local/nginx/conf/nginx.conf
9 ]! e% b1 d( y0 r, i$ k5 x' y6 k& x. u
" Z5 g; {/ o! n
server {9 e8 ]: R, k% Z. h3 q \
listen 80;
8 G/ k" `# r9 `( ` server_name example.com;
+ Z2 Z e/ M, A+ X
' q2 u2 t% |; G0 W+ l% d
/ h) h9 C2 N% e, i$ P location / {6 N) _* C$ |7 X# A7 B
ModSecurityEnabled on;6 U d8 l9 |( k' |
ModSecurityConfig modsec_includes.conf;
# G$ Z: e; b2 }( \4 @ root html;9 t0 V8 j$ ~ c: x# x
index index.html index.htm;7 t5 p* F2 _& Y& ?& H
}
9 \! h4 I# G# g5 M$ g$ X. ]}
# _5 u! A* y+ @# ]7 k, t& C使用动态模块加载的配置方法/ Z% P- i9 h# m9 o" W9 y; f" N( |: O
在需要启用Modsecurity的主机的location下面加入下面两行即可:, c* X4 V" g, {2 D6 W' C4 W. k
, e: z( I% ^/ Y
3 A# p1 W% r" D) jmodsecurity on;- e; Y# J( U! r. k; h
modsecurity_rules_file modsec_includes.conf;
5 }1 G# e9 E9 n修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
! }( S0 g- |- a) n, E" P I, T& ]) ~% ]/ U$ e0 s. {. O% v. y7 B
- [3 J0 f8 t$ n+ c! W6 ?
$ vim /usr/local/nginx/conf/nginx.conf
R% {- c/ m6 T/ Z4 s3 l6 b3 j! s/ g, X) e% w. u
" w+ p Y# v* ^server {
# D& y! X5 V+ H, N listen 80;# |& w/ s4 J- Z: D* J# h
server_name localhost mike.hi-linux.com;
! K C! j: b- @7 v access_log /var/log/nginx/yourdomain.log;
- C- b+ y k" J
( }! r U7 V# t2 X) t! N2 ]3 Z7 q! F: X3 M, n: Z
location / {1 ?2 f$ Y' s; V: X$ _4 O
+ n* j" C& u* q; _' R# \+ \7 c- a# h2 F2 q [3 P4 q; I
modsecurity on;
& C! r/ t. `, N- ^& t modsecurity_rules_file modsec_includes.conf;
6 d* J* r% V) O% [ root html;! n, c4 I) x) m; \; X! i
index index.html index.htm;
2 G. G7 k3 m/ f$ d9 O. F) A* Y0 L}
2 i# Y3 n" ~* A% Q$ e g9 `}
7 r g2 g* \9 h- c! u8 x验证Nginx配置文件6 e$ M% w! I& P" H2 K5 r5 e8 i4 n
$ /usr/local/nginx/sbin/nginx -t
9 W1 J8 {! M0 m$ p: ?nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok$ X9 _! S, @+ c: g- V9 q( P
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful* Z2 g7 l9 }, [* F$ r
启动Nginx
) _% N" J9 r7 i! s8 Q, k$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf i; l& O3 X! `7 \
% j% m Z- ?/ n/ 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.logModsecurity主要是规则验证(验证已知漏洞),Nginx下还有另一个功能强大的WAF模块Naxsi。Naxsi最大特点是可以设置学习模式,抓取您的网站产生必要的白名单,以避免误报!Naxsi不依赖于预先定义的签名,Naxsi能够战胜更多复杂/未知/混淆的攻击模式。 8 S0 {. U3 n) |, w) i4 s) O8 L/ Z
|