ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。6 k( J( i& F/ J7 h
3 }+ U& W! y/ r' L6 i
: h& x- n1 n. M在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。 B. D$ ?, B3 V- Y; S/ M7 d3 P
& x8 {5 T% ^/ w5 I/ r K- z5 X; w
2 a' S6 A6 _+ t7 u/ w什么是ModSecurity4 H3 c3 {! ]* ^* T% Z
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
& w) a( I1 A9 [ r+ v" b o* _" S( i0 @
: ^! T; S5 J) t$ N
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。9 W+ n! J4 [3 b; m f5 S/ E. @
8 x6 P# E1 @! _( p- V/ `9 y% U& \. Q( g3 Q. K' a
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
# \/ Q: p% J4 n1 O9 R1 I! Y0 [
8 x) M' w' Z0 {! s6 A* }0 i0 i {' `6 |, T& b/ l0 t
官网: https://www.modsecurity.org/
y& H; K! j+ u6 ^* W$ c% l
" G' C: E3 V4 z9 k
% `* K5 t; O, g什么是OWASP CRS. k) Y n4 z" L9 q" i/ }$ e1 }
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
3 W( A, V" }& D( z7 o% F, O9 V: P& h3 ^ u6 X
S+ {0 ]3 A2 A5 M* F
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。1 u5 L' M* V, H/ M9 V1 W
8 m: `0 e0 h' Q O* `0 O! j
# f: q2 _- X2 Q" m1 q8 F7 P. e- D% }- fHTTP Protection(HTTP防御)
, J1 |" F I! {/ YHTTP协议和本地定义使用的detectsviolations策略。
; Y K( @& w2 k l* h; o, s4 W( p9 s' V+ ~5 [1 }* L+ ^; S
! C w- \* _& }
Real-time Blacklist Lookups(实时黑名单查询)1 U, @4 ?7 P5 y3 F# r
利用第三方IP名单。
3 {. V' Z! J* Z! V3 Z/ L1 K! D
4 L$ f. J7 k% ]3 I7 L" m9 `/ r% @) N) F
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
. x2 o+ V1 S5 W) u防御HTTP的洪水攻击和HTTP Dos攻击。
9 Y; @) d# }. |
4 Z, r! z8 y% w) X! z) z' i! J% Y8 V; c
Common Web Attacks Protection(常见的Web攻击防护)
% k" z+ J* ?2 `. ]( t: d8 X9 |& C检测常见的Web应用程序的安全攻击。4 J; Y8 m& N1 j
% D6 e7 l) m" W
3 H y% v7 L8 M% q1 k* l3 hAutomation Detection(自动化检测)' R9 {* d+ H( V# c
检测机器人,爬虫,扫描仪和其他表面恶意活动。3 G8 b& s9 \) _' j& T5 s( X$ T3 _: @
4 [$ v/ I$ m! v8 z& Z8 \
! J$ H! _. {3 k; E6 s+ w( y2 @Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
* S5 O% }" Z- T6 L! D) \" A( ^' t检测通过Web应用程序上传的恶意文件。
$ X; s' _* _# [0 P0 p+ k% u7 Y4 ` A2 m* F; K
( F! V* z6 j9 D0 s* |- dTracking Sensitive Data(跟踪敏感数据)
4 ~- n F J. @- b3 k& M) B( {信用卡通道的使用,并阻止泄漏。- l, C1 E' P* [1 F
, g6 w! E* v4 u4 p" w
K- R% U4 c* {( P& y" uTrojan Protection(木马防护)
% r1 W2 V' Q G6 I t) L: g1 x检测访问木马。% I; x: P; x) w- Q* |! [' O9 s' b
1 A3 _/ ^6 V. g Q- C$ y! X: A
% E) j1 w( p# T) N* U, fIdentification of Application Defects(应用程序缺陷的鉴定)
: m9 p/ v& t6 p* ]: N& K4 i检测应用程序的错误配置警报。' C) o' T9 T6 C% }
9 z5 K0 S6 B9 }( E
# c7 \( Y. j4 K6 @) `5 }. aError Detection and Hiding(错误检测和隐藏)
7 V, V. _0 R- K7 u0 n/ |9 @! N+ w1 S检测伪装服务器发送错误消息。, T3 Q/ P0 M: t5 X. z- u3 f
" a* C" O, x2 I% y9 _; [
* b) C E6 O& f$ Y& f$ u6 d安装ModSecurity' a6 ]0 S+ x) b5 D
软件基础环境准备( n7 _8 _8 J* o
下载对应软件包
1 |8 |5 `- L0 d+ }+ r9 M* D+ g) C$ cd /root
; _8 f: K: Q Q8 s8 |; ?$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'/ A6 X0 t0 Q- {0 A5 d
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz3 ~$ p% e' s3 O+ x) s7 h$ ~9 ~
安装Nginx和ModSecurity依赖包
3 j3 C6 S0 ~' r* UCentos/RHEL0 z- G2 \5 C3 O" a9 k
( q- N9 @& Q, s5 f4 p2 m% ], U2 F% t! Y% h( b% u( X
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
9 ^3 Z+ H3 W0 Z+ S' L& x& FUbuntu/Debian
/ j$ G/ v$ b% T4 F0 b& w1 R. ]: X/ G3 k5 C, V: f# B0 B
. p6 A, m, i5 ^ C
$ 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& n4 [9 d9 \: N0 F3 C4 L
编译安装ModSecurity i, E- ~3 E8 G' {) x9 P
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。% p- ^. N" P l- Z3 B& a/ ]
' Z) M8 K0 V. ~: x9 c* _0 w) J7 Z% f8 U: ^$ k
方法一:编译为Nginx静态模块
v8 q4 \8 C0 B+ X1 `( a& Q
( e% ^# ]0 K h. J/ f/ E6 h1 F, X
编译为独立模块(modsecurity-2.9.1)
) \ S/ P% h- Y: q4 w$ tar xzvf modsecurity-2.9.1.tar.gz/ R, v1 a5 V7 L& o7 C
$ cd modsecurity-2.9.1/! F1 C/ u: m- x0 ^( O- ?
$ ./autogen.sh; {# [& s6 {$ j2 z5 H
$ ./configure --enable-standalone-module --disable-mlogc+ H# b- V* w3 G
$ make
# z* K# h n7 K' a) `( ` n6 N+ P编译安装Nginx并添加ModSecurity模块+ N$ M- z+ h7 H% \7 {% {# ?5 O
$ tar xzvf nginx-1.9.2.tar.gz
F" B$ F. i+ O: P4 ]( ^8 u3 x+ a$ cd nginx-1.9.2
! v. I u0 h3 Z$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/$ b. R0 m* Z+ k, J
$ make && make install# `. Y3 ]- I5 I0 C! q; k5 t
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块: m) X! c6 j, M* a$ ?
# J3 x, M! W5 t# q& p
5 Y# a' Y! k9 b" e编译LibModSecurity(modsecurity-3.0)
4 y8 [' Y1 y: a2 v$ cd /root
+ P2 Y% L B1 [' J& @$ git clone https://github.com/SpiderLabs/ModSecurity
3 H7 a6 g2 A+ _; d8 c$ cd ModSecurity
- A. y4 r. H$ R8 f$ git checkout -b v3/master origin/v3/master
& R8 Y/ R7 r/ T& F M$ sh build.sh
/ l! U2 q+ h' t$ git submodule init6 [; @8 q4 j5 E) w2 \3 C
$ git submodule update
3 ^8 X& g* D/ o& E0 |9 P; M- X4 Z$ ./configure
: m. p+ f D3 \7 y, D, l3 |/ i$ make
X' K- n7 t! K- z& @7 l5 Q$ make install
) D* _: v$ E+ P" q! p$ ?: rLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。" M4 j! X c$ e2 `" E2 [5 ~9 _
3 H( @* y/ Y: b: U
) M. A5 Y! b4 C4 z0 d( [
$ ls /usr/local/modsecurity/lib$ y/ i$ c; x: ~
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
, {8 n# g% x% x2 {& `编译安装Nginx并添加ModSecurity-Nginx Connector模块
6 P# M, s; f1 t! q- B- {使用ModSecurity-Nginx模块来连接LibModSecurity# R( v, ?9 l* [& u
1 G6 ?' s% L: z
: j9 T- ], ?/ g6 Z$ cd /root2 }" U0 Q; ^6 K/ h5 k
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
# A( S& I% Q8 e$ tar xzvf nginx-1.9.2.tar.gz
( _2 [8 C& l+ N: X( \1 j$ cd nginx-1.9.2
9 w% g6 C9 O5 I( B. p+ Q7 g$ ./configure --add-module=/root/modsecurity-nginx$ J# Q# f1 w& |# ~
$ make% z7 H: W3 x! N0 W. e' D
$ make && make install
6 F) A# u) v; {6 Q& m添加OWASP规则
8 P- f4 b+ N+ Z+ s5 G& `ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。9 v! a' N' t" O. ^; v: c
7 n' I- `; Y4 Q" L8 ~* W
0 |; k% F% h# R4 |下载OWASP规则并生成配置文件
4 o% S6 o1 `8 @! M$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git0 Y; W: [1 z8 t3 I6 E* N6 T3 M ]0 o
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/' M. @$ n' Q( l$ N
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
, R8 @* S( g# x$ cp crs-setup.conf.example crs-setup.conf( ?! B' f. @& L! p" F9 i$ y
配置OWASP规则1 w2 M3 q9 g) ~
编辑crs-setup.conf文件
6 u; I, X, b9 m! w
+ V4 K1 v3 V/ S. F
- y9 ]/ S; {) j3 m# F& j$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
" |6 Z1 C# K6 K3 ^ g5 T+ D$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
( `( T' k" q6 p, {0 {0 r$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf5 A: K- _& y4 F4 \$ j( p( S
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
4 \, A7 q( i& G/ e默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
0 I3 J L9 f# |9 d3 ` {) j" v
/ o/ i2 z0 H! ^% a
1 d) R. B3 a7 b$ Y% h启用ModSecurity模块和CRS规则
' K0 P+ }* u! u: P$ L2 c复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ R# c X- c& B7 w3 B/ V* H8 O2 |, E) q
) ]- f' F; u+ l: \modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。% M7 T; F) h$ w8 f1 e, ~
$ x2 M6 q9 E+ [4 O$ v* V: y O
& `6 q; {" Y; T$ H0 J. G$ cd /root/modsecurity-2.9.1/- T6 s( b! r8 [
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf : ^3 p t4 J1 g" i
$ cp unicode.mapping /usr/local/nginx/conf/
8 C& d$ L3 ]6 F; W8 S0 h- O5 X将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
0 j' c$ h) L) |: C+ ~& b
& [/ k( S7 P# A, H
. C3 k2 t, F. a! F$ vim /usr/local/nginx/conf/modsecurity.conf
# }8 r0 ?3 k f% K9 Y0 r+ W6 m9 RSecRuleEngine On
! Q- `9 T K! y4 pModSecurity中几个常用配置说明:) v" T, C4 c* W+ i3 [% W4 j
$ x7 U! L: @$ D( [- W6 E& _9 G5 c W! `& }0 R) z2 v
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。! w) t# o6 t8 Y. ^) W6 A, }1 n7 t# o
' z$ w/ @1 b7 O; H( ~9 J4 ?# K
/ X3 X/ v K" b& J2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
5 ~. L4 X L& n, N* L
* {$ Z/ j6 ~% `- z
8 w( Q8 D+ Y4 Y3 k: I3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
' V3 p. `' q! c! p6 Q
# o1 K! ?$ S( y- r7 n. W8 Y7 W6 {& X) ?
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。! K g0 p6 K7 M3 V/ h
" F6 b, q- ]( H0 D$ a" J9 W
! }$ l, `; A# s在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。: s! H4 B2 U7 l: ^# f
9 }! ^) i8 k9 m% o) p: H6 `% }) C. M t/ @" l8 {+ S/ B; F" b1 ^
3.x版本CRS; A' V* B y) z0 v
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
) i( Q* I) w' I# 生成例外排除请求的配置文件* J) [7 k' g/ c# J
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf6 \/ `) k5 v$ r/ c( U, Y; T" C. S
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
# \. ]& B1 z( }! h6 ]$ c) n, G$ cp rules/*.data /usr/local/nginx/conf4 d4 t M) j$ `8 p
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
6 z) _. }$ `9 O: ?# J& Z$ f9 f: N' f$ B
7 U% o( {& j2 c& Y: A0 O2 | L$ vim /usr/local/nginx/conf/modsec_includes.conf/ e& m$ w4 ~8 G- c
* T" `. e$ b4 |7 D) {6 _( e[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
0 b6 D- F, e; v! t E9 T7 Z: q; v7 i3 \
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。, q4 i# K: k6 i) @* W- x; v. N
$ _! D5 ]7 f: N9 ?6 R: W
: N9 l( ~) R( Y9 H) U9 s6 R Q. l配置Nginx支持Modsecurity
$ B) r) _- Y. |0 u启用Modsecurity
) h4 ]- G! X9 h7 `# c D, S使用静态模块加载的配置方法! D& _! d* u1 N: [1 _! _
在需要启用Modsecurity的主机的location下面加入下面两行即可:3 h# S! u' r* P/ }8 Q- P0 A) e
; ]% Q) `3 h' w; \7 b% x
6 C) j, q; U' G v. H1 z' \9 JModSecurityEnabled on;
% p7 x& }' t1 h# Y( Z- `5 C. rModSecurityConfig modsec_includes.conf;" A4 ^* {) f. I0 g+ \. X
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
/ S3 x: |* o" w* y( J6 ?4 T$ u, R. _/ M5 G/ C: `2 o
( r* N5 Q; p( _* G* @+ e# n$ vim /usr/local/nginx/conf/nginx.conf3 I: j' j Q( q0 X
1 S; s) a$ \, ^+ q& e( {8 e* J6 \" r- r; O& F/ t
server {
/ T0 Z8 N3 q, p# d* V( T listen 80;/ ~+ \, S! a( [9 a
server_name example.com;4 l0 K; ?! V8 f9 O& t
* O4 `* O* f1 {: W/ }1 C( f
) t6 @7 s/ U/ Y9 @
location / {
: S' d* ]) c. @2 B: z! w ModSecurityEnabled on;
! n! N% R( e. d+ W ModSecurityConfig modsec_includes.conf;5 y: \' x" P( R
root html;
, J: ]- Y1 r1 K4 E9 n$ s( N index index.html index.htm;
( N. P& T& W( W, d' L$ r9 }7 T* P }
$ ]1 S6 w3 O4 C- ?, l/ h! S}
* |. M, Z$ I; o# u3 Q使用动态模块加载的配置方法2 s, I* m+ J9 k+ O$ V9 m
在需要启用Modsecurity的主机的location下面加入下面两行即可:
9 `# H9 C+ X7 C# [7 j! m2 \: B( U0 f9 ^! u( ?9 @' W
$ F7 C% c0 l: {4 {modsecurity on;2 V2 k. A$ J+ {9 x$ r7 h
modsecurity_rules_file modsec_includes.conf;
9 I1 s- [5 V! o修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。8 a7 B; k4 F0 O* C+ n X4 s! C
# Z! K& Q. a2 U t0 {) a4 A; Y5 F- F9 g' L( e; }+ o
$ vim /usr/local/nginx/conf/nginx.conf
. F; Q8 ?; `- j! ?6 T4 A! ]: `" d
8 c' S& V( X7 ~9 |1 nserver {# |( U3 `, {2 q; y, X* ~
listen 80;# y/ R0 D, O7 ]
server_name localhost mike.hi-linux.com;' v+ _8 E: ]: K4 l/ | L* o5 q
access_log /var/log/nginx/yourdomain.log;
! }7 _9 }& w& K! u; a' L
' f. y9 Z. X o! u2 W" r, z) X) {9 L
( i! A: Z. K+ v, y* ]7 \ location / {
, f# j( h4 Y1 t& j0 I8 ~8 U" I+ _# C
" z$ i. K, i5 _) o, L, g$ o* r modsecurity on;9 G$ v$ b' {# u$ j
modsecurity_rules_file modsec_includes.conf;7 G/ B3 Q$ Z+ }8 L
root html;
. r/ v# I- `! P3 C% a index index.html index.htm;/ R3 @2 i3 j. T1 f. e5 e3 ~
}; h. p8 g# G5 d. H) ]
}1 a! p) w- h, L' E% A
验证Nginx配置文件4 c V$ }* L9 u0 U' v4 M! W( d& m$ f
$ /usr/local/nginx/sbin/nginx -t* k) N: ?8 v8 {( r6 \
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok0 w& ?: A4 `3 [ ]8 h
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
\. ~( A) {0 [9 G启动Nginx
% Z4 [( L+ A1 Y9 ]/ D7 y$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
% C' x4 ~# ^. h3 v2 H! {4 h# V! Z6 L9 p* q! A4 F. t
测试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能够战胜更多复杂/未知/混淆的攻击模式。
H, }$ m. X2 k3 \4 g |