ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
' m- N* u! C; d: T; g3 I
$ v/ \ [, s6 |$ j* q. ?4 W' z% U
4 y0 j' Q" a/ Z2 C i: `在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
, r G, V3 p- _+ V9 m. y: }* s% A# b& X- V: u# \0 @8 p
% B- }+ U1 c P! V7 C( L/ J什么是ModSecurity8 a4 n+ d: z9 N4 K3 }2 O0 Z6 I1 o! t
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
- a7 I4 g6 D! ~
! V/ q$ x- `$ @1 R$ ~) Q$ C$ n5 @$ a4 z2 b( a+ \$ D' ^" K% I3 x+ J0 ]
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
6 M; D3 v0 k6 E7 a. e5 H, k
/ f" Q+ R- k# i, N+ W! j$ \/ F" j6 R! h! n
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。8 I* D) }+ u& p& R% U
9 x% _9 u/ J& u) t9 B1 C* D2 ^" h
/ c r; k, @5 ^/ ^( \! J官网: https://www.modsecurity.org/
, ~. j4 r( H: |- i2 d" `7 k6 E/ b, F4 z: v( Y
4 _) j+ \/ T( d" ]/ _# }& C- y什么是OWASP CRS
. o$ @2 K1 ~3 l6 d3 v8 R9 k$ TOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
3 {5 `9 a* Y" r$ q8 b" [0 |$ d( b. a, o
1 ^7 T Z' @) w
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
9 {1 R; ^% k7 k* W1 f3 [; P) N3 W: _8 |' B7 ~
/ S4 U; u; n; s( @! D5 k4 T& V
HTTP Protection(HTTP防御)
$ ^3 {6 H: F8 F# t2 i" mHTTP协议和本地定义使用的detectsviolations策略。
/ T; t: b! j$ M9 b0 q M' c) Y" l, {, W/ \4 r
( D( _" O) ?9 S# O. M+ w
Real-time Blacklist Lookups(实时黑名单查询)
+ n5 Z. f( B. z2 F) k0 m利用第三方IP名单。
& k1 } c3 _" H/ A& c8 c
+ C* u. Q1 D0 D: g* o2 i& w: k; T7 e1 y
HTTP Denial of Service Protections(HTTP的拒绝服务保护)2 s1 l+ D/ x" R% d
防御HTTP的洪水攻击和HTTP Dos攻击。
9 P, P7 D/ l# O$ K9 H6 ^/ X, m7 Y+ u# B" R
! H+ n& e0 k: p' c: SCommon Web Attacks Protection(常见的Web攻击防护)3 V+ O& A# f& b) k4 f6 n
检测常见的Web应用程序的安全攻击。- w4 _% E: f" Y$ D* f- A$ o; ?% b
- \$ h! [8 j7 v# [$ C
! ]$ l& S4 M7 g, H [Automation Detection(自动化检测)6 u: Q, s3 Y8 H% }, ~
检测机器人,爬虫,扫描仪和其他表面恶意活动。
' Y% R0 }8 U/ N( k" u6 n2 p7 l
L+ v. o F. M r0 h# \1 d7 @; }% H( i
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
7 ~' d }5 \1 i4 s7 r- e3 z( j检测通过Web应用程序上传的恶意文件。! m9 g. j7 D* s" R4 ^
- R5 t* B. A. p/ A( t: ]- ] P" A1 o2 u/ G. _4 s
Tracking Sensitive Data(跟踪敏感数据)/ g, T" z. O' D8 |- w, t
信用卡通道的使用,并阻止泄漏。9 ~: \& }' `5 Q6 l9 ]
7 X. i) J4 J$ d2 D4 d) o
* i) a* S* |0 F; k, STrojan Protection(木马防护)
. W6 H# Z- j$ y) f4 C1 {检测访问木马。) f+ O. B) [7 n# x9 k4 D. p
' m; Z! _; @/ v2 [' v: L
+ I& b: J2 k& ~+ O3 z( T
Identification of Application Defects(应用程序缺陷的鉴定)) o# h0 M1 f' b7 S2 s
检测应用程序的错误配置警报。
( ]1 s, m8 v% c2 `
& c9 W+ ]. l" z! k) r
& ~' P" x) Q( s9 c- R7 l! vError Detection and Hiding(错误检测和隐藏)/ K2 F3 H( V: T9 Z1 ^% x# }4 ]1 }( @
检测伪装服务器发送错误消息。
# O2 O" q% _' t' h7 @6 Z; r/ D3 v. }, j. Z# m$ @' ]( s
8 D* S% x* |" M* M- P4 _0 s' w
安装ModSecurity2 _! ^1 O: D4 q( A; L; W; d* [
软件基础环境准备 T- Q" D+ J" I2 t
下载对应软件包4 m6 b7 U7 b4 x# y+ V
$ cd /root
+ R# J' s6 o9 S8 Z1 `/ k/ p$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
# A2 `: D$ B# @) g3 o$ b- N$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz9 n: }$ t: t' c1 ?3 Y5 m; j
安装Nginx和ModSecurity依赖包5 Z" z9 | o2 G" b, e; w
Centos/RHEL: Y" ^" P; l& G1 x; P( x
( x w" R% ]# k+ w; q
) U# @$ U' g7 F/ y Q
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel8 U3 x# c9 m7 U$ S) i7 Q
Ubuntu/Debian+ l, @: c+ \" ]' p
. q6 u; x0 T* e: H; ~* a1 t8 f! x; u! ?0 Q- {
$ 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
- u5 F# M2 x& D8 w编译安装ModSecurity+ a4 }; F' U" K
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
9 I7 {2 N; c. s$ L) p4 w
% c W' r7 z4 U' ^! O% L1 o+ {1 S; A7 V; m3 H
方法一:编译为Nginx静态模块
7 l0 {# C7 Y; o5 L3 a6 u' n* ~
( h5 W/ J$ M7 W$ S/ R3 q: f: C
7 T; H5 v: y% m: b编译为独立模块(modsecurity-2.9.1)
( O) P( i$ e4 W$ tar xzvf modsecurity-2.9.1.tar.gz
5 q1 m: M3 d" s: B& h0 T3 T$ cd modsecurity-2.9.1/
, e1 i1 s0 q) B6 p ~7 @/ i$ ./autogen.sh; D6 n+ q7 L5 K" K l0 j
$ ./configure --enable-standalone-module --disable-mlogc
# Q% X8 @( w' [ }8 A9 J$ make
* |+ }; n- k' c3 P9 I编译安装Nginx并添加ModSecurity模块
/ r+ u; H$ _6 i$ ~( o: n' B7 h$ tar xzvf nginx-1.9.2.tar.gz2 e* h9 Z' _, Y# X! |0 X
$ cd nginx-1.9.2$ V8 C* z- k& W: f
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
+ ?% _: e; F1 t2 {% s+ I7 l" i$ make && make install
; ]/ c$ |6 R; w6 v# L- j+ h2 O方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
! B# P1 Y; v% ?' P/ a; D6 c# a5 R4 ~7 j
5 K v% b6 D5 k- g3 N
编译LibModSecurity(modsecurity-3.0)
) F4 d9 p. v$ M2 y7 N* H1 N$ cd /root
8 Z! H! D8 f, W7 Q; d% w5 N$ git clone https://github.com/SpiderLabs/ModSecurity
- V0 f% R/ M; q& a% j$ cd ModSecurity
* M8 }+ v9 y: {4 H7 M' e' |$ git checkout -b v3/master origin/v3/master
9 w9 N$ k; ]: t7 M, R$ sh build.sh
/ Z: R1 ?% \' O' I7 u$ [5 T$ git submodule init
2 X0 F" R! t2 O$ git submodule update) [3 m9 J# C! y+ X6 B4 F5 G
$ ./configure
4 e' z; p0 P, H* B) H$ make
- g: p% J6 T8 m6 d! M) H$ make install/ u) a ^5 H+ ?
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。2 b& O* c. B' i9 ]/ i0 ?% C
) u+ M& P* h- W* e0 p/ E0 c
+ s! X& ?4 A" c7 S6 k
$ ls /usr/local/modsecurity/lib
( I) Z i( m% W& I- D( f% Plibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0) b, B! W6 x+ D+ z( `9 I* `
编译安装Nginx并添加ModSecurity-Nginx Connector模块
: B9 u! ]9 G, y. ?! k0 g使用ModSecurity-Nginx模块来连接LibModSecurity
; K6 K2 h) C5 U% n
# G" D' i0 ?$ y. ?7 f
! M) ]7 s$ A) Q0 L3 K8 _0 l7 K# @$ cd /root
& [/ {* n0 o( G. B$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx, o2 @) `: E/ c' t
$ tar xzvf nginx-1.9.2.tar.gz
6 E- T' g) O' i4 z6 R2 S, d8 g$ cd nginx-1.9.2
9 K% s$ o6 l+ k; t7 p$ ./configure --add-module=/root/modsecurity-nginx* X+ s' |" [+ n: u
$ make1 d' ^( y0 R! c) f9 y
$ make && make install1 U( r0 ]% w& Z* K6 ^2 A& t! ^" h
添加OWASP规则$ I6 Z) k! [( y M* X" R* {$ X7 z
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。3 m& L' A- s8 @7 n
4 R+ i; I$ Q7 ], q m9 ^+ J+ I
下载OWASP规则并生成配置文件
3 X! V& k+ m0 B& t$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
0 j% G/ ]* G6 Z @2 H R$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
: c+ @ ]1 y* R1 M! g# U+ r( F$ cd /usr/local/nginx/conf/owasp-modsecurity-crs: l5 {4 l) d/ f. n
$ cp crs-setup.conf.example crs-setup.conf1 J8 J9 g( B! D* C
配置OWASP规则" Q# L/ {+ w K! Z" D0 Y
编辑crs-setup.conf文件
+ q, N: A; e8 e) e4 I5 a0 K# u7 q
& r7 [& x ^: N- {
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf' x' ^& r0 j4 ~: P7 J/ X% O. o
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf- u! f9 d# X; W( I
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf! n6 b" e4 [% b* P8 _* X9 K
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
, {; A9 p4 C- o0 U默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
`% x( ~. @& a9 H+ H2 f) t9 V; t7 V% t4 q
: ^# B) x5 @* C3 L
启用ModSecurity模块和CRS规则
" k/ _2 U" P3 B" D复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
~; X* f2 b4 t6 U2 @- Q, B: Q0 @* k$ P* V" G
: A! R6 p7 @+ v7 ] O
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。$ x, ^3 m$ N3 N7 u3 M
$ z; P3 W# Q; b* W& ~! S
. @: k& \. t0 j, H2 Z- i$ h$ cd /root/modsecurity-2.9.1/
) o$ U. w# k0 p# S3 _, G: X$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf " |( w5 ~0 e0 i) |" v8 p o
$ cp unicode.mapping /usr/local/nginx/conf// J( ?, f' \& a4 s0 e4 Z
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。- p* i8 _. r l% L
9 l6 H& Z- {' o y2 Z: p; `) p+ b: M7 B' ?: ]
$ vim /usr/local/nginx/conf/modsecurity.conf) b& ?8 }3 `* ]# Y3 e# y& V
SecRuleEngine On
8 {7 l6 L4 W% RModSecurity中几个常用配置说明:8 A) I5 g0 m8 \: D( y
) H8 k. P( K) T/ r/ S! U
]# c+ ` w- G0 t3 F. M1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
3 g( i) z2 `! A1 i( c" U
2 x+ r) H3 u3 I/ o4 z* `) |
% R8 S! D0 F/ n0 y, d; L2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。( t: h7 R; _4 z4 O9 ~
/ B- T5 }9 R" o7 G S: z( p
( w) u' G n# ?3 V& a! V3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
3 r/ z q( `5 g; g2 Q2 M
$ A: Z/ h7 x( g) n% `- G( e' M' P6 n( {5 \+ v1 {& f: Y) S r
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
: {+ O$ a9 ~2 X
7 e" M& _" g) U1 L4 I* W
) |3 L' I% ?0 S9 w+ k0 U( l! C- c a在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
& p' M; P8 v; Y- j: F# m2 T l8 ?/ J6 U8 P
1 U; b0 O7 N9 \- E! D7 A. C; j
3.x版本CRS7 O B# f* W8 k) l8 l
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs5 |9 M# R% D6 n! Z) \9 a/ ^) U. G6 `
# 生成例外排除请求的配置文件, F$ x) P) e6 U, [
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf$ ?' ^3 T4 s, y* E6 w2 X: ~" t
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf+ \- Q2 p) _9 [' D' j
$ cp rules/*.data /usr/local/nginx/conf* ?: t: F: J5 {, t! C
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
' G5 V) b$ S" q3 n0 I9 `
% _( n8 ^% v3 P0 h4 x d6 R& s4 E% k
$ vim /usr/local/nginx/conf/modsec_includes.conf
. B; X5 J1 `( O3 Y# z5 h# z. Q$ q; x1 q0 \- ?
[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 ; i8 a9 [. |" I6 N6 ]: x
! |+ g& G& q8 F" L# J1 i7 H
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。$ h$ \- w6 h" h, E' N. {! K9 p
/ ]9 `& d ]: u! D( g5 T- r
G1 x" {8 z0 I9 T$ ^配置Nginx支持Modsecurity( A& }0 ?9 d& Y m& C
启用Modsecurity
* ~! X3 g y# Q使用静态模块加载的配置方法. \/ ~- Y) B. t. s" c
在需要启用Modsecurity的主机的location下面加入下面两行即可:! X- J9 c, `; C
# j1 [! W# T( d" w/ k7 O; N! r2 }9 i1 g1 B2 ]
ModSecurityEnabled on;
, Y1 p |* m5 c( W* rModSecurityConfig modsec_includes.conf;
! M( y' Y$ n4 A, t+ z修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。! `3 M2 i1 h0 ?/ }7 _8 q, o3 @
) B g) X# ]0 D1 f
1 r/ u" H1 I4 K- Q$ vim /usr/local/nginx/conf/nginx.conf
% O! @0 Z( m6 f+ l3 a. @ ^& `8 s! u( ^: H, L
& ?* ^5 q4 H8 [) ?
server {
( ~% P D+ k5 j. K9 r, Q8 q! b& P, H0 x listen 80;$ I* E7 z0 |0 d5 Y( Y* Y
server_name example.com;
, F$ @' F9 p$ m1 ?+ Z0 u
! O! z7 Q$ ]( h% R0 x
5 C: }. R3 r( f/ x. s location / {' s' C+ b; V- i) v
ModSecurityEnabled on;7 X+ b" k q% r3 ?! l0 A; b% H, E& z
ModSecurityConfig modsec_includes.conf;* X7 F+ K& q4 S2 k" i& J
root html;( ^' [7 G, X$ ~, ^/ l9 y8 j& r
index index.html index.htm;0 `5 u/ `& y5 G3 G5 s
}% Z2 H' a7 ~: `; @+ L" {/ [* c
}1 {1 B6 Y+ r: c8 v# H i; Y- \
使用动态模块加载的配置方法$ `0 ^" ]! g% x: K
在需要启用Modsecurity的主机的location下面加入下面两行即可:
- _. U7 M7 w; w- m t' F' L% T3 H( B2 l6 A& M( b- c
$ b7 p" c, ^* S: g8 t
modsecurity on;
+ R3 o* _% D; \% ?$ z$ Vmodsecurity_rules_file modsec_includes.conf;2 E Y W8 ]9 v0 v
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。$ X: t$ d2 l% l& q6 ^
8 k4 e' R: Y: |4 S
! A% A& @' D: r$ ?6 R( k& s4 a$ vim /usr/local/nginx/conf/nginx.conf
' \$ L( Z* P% x" t9 y, K0 a
9 g' _# Z! D6 h& P5 G5 x. G
; X; _! c& }. Zserver {
+ [5 c) [9 c# I( K1 S listen 80;+ k3 Z" p$ i1 y
server_name localhost mike.hi-linux.com;
7 x! T" n+ J. d1 y/ c2 J* h access_log /var/log/nginx/yourdomain.log;, ^. Z9 P2 j( ?& m
/ {: I; e4 [) F6 {5 P+ q6 ~
( V1 ]+ k( M* O6 E, G {+ C location / {/ { _' P$ n% D3 s4 K; Q4 V) }% d
$ E! k+ a+ K; ~; k/ z
% L4 f" N. K2 x. |# }& D modsecurity on;
& l6 {. X1 x& [$ V/ ~ modsecurity_rules_file modsec_includes.conf;
' i6 |9 D8 e0 u* o+ f8 W root html;
9 i9 f2 _5 J9 w" z0 ^7 F1 j4 Z index index.html index.htm;
! B& Q, C y8 `+ X/ W0 k$ ? P- S}
: @6 \9 k9 t: i- `/ H}
% y" V( Q- K7 V6 X4 G验证Nginx配置文件3 j. q8 Q2 Q# q& |
$ /usr/local/nginx/sbin/nginx -t
# |+ J; x' T' ^" g9 I* [/ Z* \nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok7 w; T. F; ^' _. ^
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful; R+ z8 r, W2 e7 c; B5 n; D
启动Nginx
* v& o0 C! ?' {$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf7 ?8 j" l& ~+ I+ p
- u5 w3 _6 g6 e, J
测试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能够战胜更多复杂/未知/混淆的攻击模式。
; L. g6 W2 p" x. x* N |