ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
6 V$ F. ?2 |1 X' a- s+ i/ U7 p! ?4 r( ^
$ N* i a' a$ q# j5 f6 c9 P# g
/ g/ \8 O( M1 }' L1 I1 U/ ?在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
; s! u( X+ v0 F% m
, ~6 n$ y( ? T0 E# x
; \5 c0 M- F, Q, E9 z1 ?什么是ModSecurity( m8 @4 P: y$ @) g5 j) u) e
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。4 J. l1 k. _0 i$ d
) r& _6 j0 F5 H
9 |# Q3 s8 F: }5 a( y0 ~# J' i+ QModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。: ~ z) U7 }# F! @; W* n! z
. X9 { I) V2 `( H$ y
* v( J9 C$ y- r1 J; G2 V& aModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。: Q+ c3 G" A: Q* {$ E
0 l2 W" ~. M2 s, P0 W/ N1 n! m6 u; \. N K) |
官网: https://www.modsecurity.org/! ~$ |. G+ \7 V; U6 m
4 ^8 c/ h2 _. t! }0 A
# n; M5 _5 m' `6 v7 o8 ~
什么是OWASP CRS# Q4 S- K1 U' ~
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
0 o- L, c* n+ r( @
+ `' S" O4 b8 h' p5 U6 R5 y6 _+ M4 I8 {# a# h% f
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。 N) X1 g; e; U7 X2 r4 @% x' _
1 j9 g" M) c- X* i! u# K7 g. `
# y+ I0 B8 {5 I' }HTTP Protection(HTTP防御)0 ?; ]# k+ a5 M8 b
HTTP协议和本地定义使用的detectsviolations策略。9 E5 M. Q6 _& {
" w& l( K( i4 T3 \
7 `5 a1 G! ~4 t5 L* RReal-time Blacklist Lookups(实时黑名单查询): e2 l( | U- a8 v. S& E5 V
利用第三方IP名单。( w. G# \0 ~; R! s* _
0 S1 g& E, t& l; {) Z* Q Y5 G# z' E3 {" v% x
HTTP Denial of Service Protections(HTTP的拒绝服务保护)1 j X) p1 U9 F- U
防御HTTP的洪水攻击和HTTP Dos攻击。
6 {0 T+ \* R9 d0 B9 B: P i
2 W6 z. ~ s9 u7 t' W( c; t' D# o& m, ^9 r4 E
Common Web Attacks Protection(常见的Web攻击防护)
/ e' B. U. V) F" I9 [检测常见的Web应用程序的安全攻击。, ^" ^* s; H- r1 X) b3 z
9 c# J- @. H* o, `
& k$ U: @" f& G7 h8 YAutomation Detection(自动化检测)& G1 Q) y$ C; @$ p4 z/ P. h
检测机器人,爬虫,扫描仪和其他表面恶意活动。: J. e# [6 [! z# Z+ w+ Z$ ~" Q
- o) {1 v3 K5 F+ [5 P
8 C( j: o% e! x3 |& ~4 J, c5 c. |Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
# r+ H/ x& [6 m( {检测通过Web应用程序上传的恶意文件。
7 m' {3 v7 y5 B/ h( n( P/ n+ |# z$ W- ~
9 K8 G. c" M8 F% |
Tracking Sensitive Data(跟踪敏感数据)$ i, P" x1 @# |2 b- B
信用卡通道的使用,并阻止泄漏。8 k$ ]- @. J# ]) @7 _6 T/ ?
! e6 \, j. L; L" Z3 G3 q
- o+ u6 p" b& I" E6 bTrojan Protection(木马防护)
1 p7 @* { f( {1 |8 \6 N& v检测访问木马。 i, a3 P K( f
; Z5 w5 Y+ u4 q( G" w1 j1 c0 O
6 T$ L8 q6 W: v% N% i$ m# W0 t- OIdentification of Application Defects(应用程序缺陷的鉴定)
; a# H. J4 U# k+ r检测应用程序的错误配置警报。% ~' \# _3 T* ~+ r2 h" ^4 H% {+ L/ Z
0 L# ^3 e, |* q7 m/ o9 g- H; K3 I: h8 B3 b: @. C9 O- I) e
Error Detection and Hiding(错误检测和隐藏)' Q5 d$ O# _0 s
检测伪装服务器发送错误消息。) N6 {0 W7 B7 f
( O; Z# s# w6 M* B- d! k$ H( s# `" m
) R+ G- O: H+ E. o" K0 b安装ModSecurity* i+ ]2 L6 L( v4 _ R1 V
软件基础环境准备
) ?4 r( U t" F) o/ \' V下载对应软件包. H( d6 p5 N- T
$ cd /root D% i5 b' d2 X2 _1 q
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
# \" y. `5 L' `( a$ C0 w9 T; z$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
, i) Y+ }6 P2 V' O1 V安装Nginx和ModSecurity依赖包
3 u( k# X# ?5 l; N LCentos/RHEL
2 b9 Z& M9 ~2 H5 c& N5 v! [
% R* |1 w. ^; ?# M3 `# y2 \. o" j' u7 d5 s" P
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel2 y! F* C6 |; |3 b) j6 `
Ubuntu/Debian8 l8 {% T% W* M% ?& X, b
3 ?2 }* I1 G3 G6 G/ d, u6 y( e |7 ~- u" A0 q/ R3 d/ m0 q/ h# F
$ 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
. ?- @: _: ~6 w, R编译安装ModSecurity
& ^. x9 J' m& V8 u2 bNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
! o+ l4 J1 G( X( H4 e9 T9 j# S& W) m8 p/ [
: i5 r& c$ E( t# C
方法一:编译为Nginx静态模块
' N7 d$ s0 ?& k2 v$ f" s
8 |. X; X, D& f' o: `' q+ \8 L/ R5 W/ V9 Y4 N# \1 g
编译为独立模块(modsecurity-2.9.1)9 s3 A& W6 r) D$ K# L9 n
$ tar xzvf modsecurity-2.9.1.tar.gz
' L z; \5 S c$ cd modsecurity-2.9.1/" a% M* @" G% i! T( c
$ ./autogen.sh3 s; o8 U' w2 t( `" w, Z
$ ./configure --enable-standalone-module --disable-mlogc! ^7 M+ b$ Q* O/ ?* K" z% l! r
$ make
/ t- V8 e) B' e, h1 m! G! P7 m) a编译安装Nginx并添加ModSecurity模块
# F, f4 ?+ q* I& ^$ tar xzvf nginx-1.9.2.tar.gz9 c) W, {, p) n6 S0 K( c* ?( |/ j
$ cd nginx-1.9.20 u6 x8 N5 M5 X+ O& `3 @0 m4 F" n$ M
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/* m7 r' Y3 Q* y2 Q% J5 k
$ make && make install
k& y5 s7 J+ U方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
: S- p n3 _: y7 c0 N
% c) [! F. q7 L7 D% h; k- j
2 I% ^8 J9 I: h) G编译LibModSecurity(modsecurity-3.0)
9 k, a% T; ?. N- D# f2 I$ cd /root. d5 w V* ^. O W/ C
$ git clone https://github.com/SpiderLabs/ModSecurity
9 l6 A! e1 `' j/ t$ cd ModSecurity
: y0 ?+ W7 Y! y1 y7 S( h$ git checkout -b v3/master origin/v3/master4 ~5 b" Y/ A" T3 o: W: j4 r s: Q
$ sh build.sh5 F& M, \" G7 M+ W9 H+ T9 o9 V
$ git submodule init
/ Q1 r! G& w9 w" o( p) ?- ]$ git submodule update
3 X3 z- W) b% o9 n5 s* t$ ./configure
( j2 V; b3 Y3 H% F7 U$ ?$ make/ A0 k9 c8 r& g1 ^0 R0 P6 e
$ make install' o0 m; T1 Y. S$ ^7 u
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。. K9 ~: @6 _: e
. H* i( i( G& ~2 y# p# [
9 S/ V/ z4 B# j. J/ _8 ^1 x9 Y$ ls /usr/local/modsecurity/lib
) d' K# l, @" I: ylibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0- F' S! J5 p+ A: L" H
编译安装Nginx并添加ModSecurity-Nginx Connector模块
8 D L+ [/ {# K9 L0 V5 L使用ModSecurity-Nginx模块来连接LibModSecurity- G! j2 Y4 G/ C9 Y
4 }8 ^1 @* Y0 v: P+ V# r& g. T
' B( \" K. h7 G5 B) X) i$ cd /root
/ E j+ v) e: Q! D+ t* O$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
P/ @8 {. K: G( T: B$ tar xzvf nginx-1.9.2.tar.gz- T8 L* X6 d \; Z' ~5 H6 W7 ?8 `# w% P
$ cd nginx-1.9.25 N: \4 t6 V1 u+ W/ i
$ ./configure --add-module=/root/modsecurity-nginx1 t" G/ S2 Z5 `# v0 F
$ make
& J9 K4 F9 O6 A% {4 r8 K0 U$ make && make install
+ B: d# a1 P9 w* g& q; |1 R. K2 i添加OWASP规则* m; t0 P, b) {) m- q' W" P+ ^
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
& t! Q, f0 _! ~
9 Q/ `7 u! c* u4 B
8 e- [. U$ Q" p下载OWASP规则并生成配置文件
/ [- m7 j; j5 ]" _/ \' x% P" w$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
9 F! F# [; ^* x- R% @$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
3 ^) d2 i: e- B/ j, I( o6 L$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
* R2 R6 n: c3 @& f: i9 V% n* L$ cp crs-setup.conf.example crs-setup.conf
/ j2 s" Y' T$ o* M4 ]. W2 d配置OWASP规则
8 Q9 ~6 n8 j# t+ P# N+ Y0 F编辑crs-setup.conf文件$ A& ~$ Z! U A8 H8 k0 h6 C7 k
! ~7 B& W: O4 _, ^ w9 J
* m; D$ e& m6 g, J, ~9 ?
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf7 c, j @* s. K4 z: S& Y
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
( Y* c9 j* Z* ?$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
! p5 @( \. y+ m% `% T$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
4 E! }- i+ ]; I6 D2 I8 j# {默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。# j# i4 O/ O7 y3 D9 R. F A7 J2 l
) H6 w' n' X8 j3 v/ T2 ~
+ {' G0 k; ^8 ~启用ModSecurity模块和CRS规则6 R. C4 N) l9 N/ u7 O+ `
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。7 n0 {) O1 b3 ?
7 E. f- l% Z0 U! c
, ?7 ^. m0 U% u, o N5 O- C. ~) Wmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
! E- f$ N% [( [6 H. G1 t$ G, u" n5 ?! Y8 i7 H8 y" r: w
5 O9 X; s _. l3 @: d! n$ cd /root/modsecurity-2.9.1/$ P0 A7 {" v7 K- g/ {0 Y& b
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
! ~+ N8 D3 k3 L2 y& V$ s* O3 V w$ cp unicode.mapping /usr/local/nginx/conf/
# `6 x* O3 w* c4 W- ?/ p将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
% w T* ^# M( J' Y
, P5 r% Y5 e( i2 Z% T
v3 i& g# F4 ^3 v( x$ vim /usr/local/nginx/conf/modsecurity.conf
( z! N( b# x6 F) qSecRuleEngine On
! E5 `( g$ \+ T0 r3 Y/ {ModSecurity中几个常用配置说明:, E- a7 T' B2 ?; H% O) m+ o0 H
: x: {! _! ~( t k2 ?3 V Q- u; @) i M
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
$ A, {6 @2 N: G, M5 N' \8 N- y' N& \% I! W
' A( s, F3 j) Y$ _2 V" e, O+ G) ^
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。9 \3 h; e. B, c6 n; V1 w6 g" H2 E
3 u' E4 g I2 W9 J
; D+ O; T/ C: q. v5 ~3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
# U( C2 \/ C" q) T: h/ m2 r# ^; W0 o J: W. v0 c7 C3 W! l
, A7 q8 k. W7 ^; k, u
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。* _% _9 I. Z. i; e
2 q2 j( i/ M9 Z% Y/ P( y, x+ ]6 \& T
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。6 _; E3 @# x) m' j+ ~
1 M( Q8 e0 g( q3 b/ H
( _- ~0 X* C$ T7 y8 ^" Y3.x版本CRS
, d4 L6 @- k. d8 {2 y1 g/ n: W$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
* n4 a' _# S3 ?, X" v, }/ b; P# 生成例外排除请求的配置文件
- I Q Q8 q: d$ G! x4 E/ O2 q$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
3 X4 r4 K. I$ E8 \- Z. }% E0 C$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf4 @* M% s& m& ]. j& @9 ]& g
$ cp rules/*.data /usr/local/nginx/conf. k8 r4 }1 d2 G* w. }8 {
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
4 C p" U+ {' V( c8 P, Y. e. n" ^9 E3 a+ \0 J5 b
6 @% c9 ]( E1 r( K( g
$ vim /usr/local/nginx/conf/modsec_includes.conf1 b2 A5 j n. P/ [0 q: N; M
4 C6 \( {4 J( @5 T4 U3 z[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
2 T" A7 Q# w; `8 g! i* c" B7 r. O, k6 _& B; y+ i
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。1 T4 B9 L: n7 J
f2 M5 K% P0 F+ Q
" q1 A; R" }, n# X% U I
配置Nginx支持Modsecurity8 t# { \* `0 L1 M
启用Modsecurity* Z# m' ^0 C6 r8 e4 e
使用静态模块加载的配置方法
% Z9 ^' E3 h9 s% V! a. _在需要启用Modsecurity的主机的location下面加入下面两行即可:; f; z. c9 K! T- h: ~0 x
% c+ H0 |2 W$ v# h8 x" u0 R% R \& e
' s" i( C! Z$ Z/ WModSecurityEnabled on;$ I$ `( P2 `5 L6 m4 s3 @ `; I8 Q" j; |
ModSecurityConfig modsec_includes.conf;4 w, J+ Q/ o7 ^3 h
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
* p% P" ~9 H3 {* y/ k& o% P7 u% _/ i7 Y
, S7 d. h$ _& |% O$ vim /usr/local/nginx/conf/nginx.conf
, E; y. \0 g, ^, J5 |5 q; e
. D, D& b- ^7 p5 a, F, x3 N1 G8 h+ ~
server {7 w. |5 W4 I2 h
listen 80;/ V. ?" ?1 M0 I* B! b
server_name example.com;
+ A7 z1 C$ p: h, k) _" \
- t- o- ]; g# l2 ~; e8 q* @' X% U- g7 z9 S* b
location / {
5 N$ n1 S# x5 d) G ModSecurityEnabled on;
/ @' n) z* Y) g. W ModSecurityConfig modsec_includes.conf;
5 S& i9 e/ \8 P7 r) L# o root html;
' @. A% _: @) Y index index.html index.htm;. w& R5 i% n' i! v& |
}* W) F1 H% p. a5 o
}: v% U1 G! i# x8 r: L
使用动态模块加载的配置方法
% ^4 _5 K: G# U5 F在需要启用Modsecurity的主机的location下面加入下面两行即可:$ Y, c6 P% ?1 c$ P
- G+ Z$ f0 J6 ` i8 q5 a9 s& c# W& {: {, V+ T7 n& }" F
modsecurity on;( y# C0 P( x9 D! L! x% v: P
modsecurity_rules_file modsec_includes.conf;
/ J2 K, |6 m% D7 T: S! Y( a( Y修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。% f* q, x3 r z/ K7 K" f# r/ X
2 U9 g2 L8 Z8 @# R+ b1 ^& C; s( z8 w' F6 X
$ vim /usr/local/nginx/conf/nginx.conf
- J1 [5 K. Y4 {- C2 w# A: i) d) C5 o& j, S3 k6 t: p
$ J# ]9 {/ P1 {! y& | Iserver {. c/ C& L( N" h6 U: I6 e0 R
listen 80;% D7 ?4 l* e0 u" v! z
server_name localhost mike.hi-linux.com;
7 N* a* d. S e9 t' J access_log /var/log/nginx/yourdomain.log;
) E% F, ?9 `7 B1 a0 g" A0 S; U Z' t R |) p3 y# D+ j
( Q. A- _$ E2 p2 v location / {: }, Q6 d+ D+ K7 m& d4 t$ c0 A) L
# a% |8 B7 `4 I+ K" x7 ?2 ] Z2 ]
7 H( r% `8 @: a( [9 d1 H modsecurity on;
7 y! `. x5 T; j5 i8 U9 h& P modsecurity_rules_file modsec_includes.conf;: @9 G: d9 P/ }" U1 r1 f3 E% y
root html;
; a) @* a7 }! K1 ~ index index.html index.htm;, Z0 _- p% e. W6 I \
}
" G/ m4 V; E1 X# T$ M f; r}
4 g) C" e! w$ e, ~6 Q/ L验证Nginx配置文件
0 G4 _4 J) @$ n% \1 a/ E3 R" }$ /usr/local/nginx/sbin/nginx -t
: V; K& M- Z. `0 Q6 knginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok0 \, o& J& |& l) P) c7 J
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful0 e9 M y) @+ J- c9 ?; k. u2 j4 Y
启动Nginx2 C1 k3 ]# u2 V
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
6 K$ O) B& r2 u1 Q4 ]2 R
/ p/ e" l) s8 Z9 y7 A% l1 j( S测试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能够战胜更多复杂/未知/混淆的攻击模式。 * ]9 i, [0 k: L1 M; O' _" e
|