ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。! b0 X0 ^3 C0 E0 C! U8 Z
! p% o& P2 ^8 \ P) u |8 O
' x; _' J$ o- k q8 }$ L在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。$ h9 S* ~6 q6 |: g
* Z0 S- j, |9 h: R- Y& ~; ?+ f. j: P1 h c: A; i7 b. U
什么是ModSecurity
, n7 e" @* B% @7 g: WModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
8 W) j4 Y+ x. C4 ?6 s; m* u6 g& S$ }# A; w3 J* K( p
$ {% I' I8 i! f" O
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。8 K5 H2 z5 e# U# y) ?+ p
/ w. J+ J/ l6 {5 r
' _- `" p( k2 jModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
8 |# S9 W3 }" H0 w
6 X4 x5 \8 K% v* e3 C9 A8 d9 \8 B( E: H% ^2 |+ x! {
官网: https://www.modsecurity.org/
& o0 ~2 ~- C( { U+ ?- Z# V3 |2 r& ~5 N( i
1 q8 b1 U& U1 H什么是OWASP CRS2 q$ E$ G5 b K" @1 q
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。& E8 }. V+ S3 M& X* B
- b7 f& }! N6 }. x
& H4 t: D; i; [ h% W) H" j6 E
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
( U/ M" [* Y1 u8 e: H
4 B. m0 d1 S' u" ~( v# C# t4 y. _! C% w/ X# W8 y+ H/ b/ O4 u" c& H) u
HTTP Protection(HTTP防御)6 q; ` @; b0 b+ m: }+ Y. ~
HTTP协议和本地定义使用的detectsviolations策略。
$ @4 C, P+ ]8 r! p4 N5 b# | {. i/ m2 R
" a$ i# h6 R# H8 N; k# C4 C5 vReal-time Blacklist Lookups(实时黑名单查询)+ m8 @) }) ?/ _2 b; @4 n! B
利用第三方IP名单。* D6 W( b# t$ Y3 t" k- E
# W9 {/ u& d4 P2 i: [; j
0 W4 A0 B- U/ w
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
8 O( c: t* f8 R防御HTTP的洪水攻击和HTTP Dos攻击。9 A- S' d$ G6 |; B1 k0 U1 Z3 I5 }
# `, G- J3 [5 y7 X; H! e6 O2 g7 x/ m0 ]: f+ y* a" o- Z6 h, |
Common Web Attacks Protection(常见的Web攻击防护)8 l7 R' i1 h: \2 h* ~& i% Q: C
检测常见的Web应用程序的安全攻击。
0 n' x: T1 N A% L
6 G0 Z3 h8 }* x2 }% m: R' H- Y* l% }& U. M7 h
Automation Detection(自动化检测)6 r( X8 a+ i- S- d- B3 f# r
检测机器人,爬虫,扫描仪和其他表面恶意活动。
1 \" W' |$ Z- l, K" d N4 }8 [
/ U% ^% k) l- q& e' J4 I _
- j+ ]+ l. G$ ~" `6 G3 W% w# tIntegration with AV Scanning for File Uploads(文件上传防病毒扫描); ]; Y& R: N" l! q3 k# Y
检测通过Web应用程序上传的恶意文件。2 g9 Z- o) P9 q/ @& X" [4 I
- k- v% r, O" ~" G& b
/ M$ i9 d, M& LTracking Sensitive Data(跟踪敏感数据)
* s! ^! u7 g6 S0 A2 G" Y信用卡通道的使用,并阻止泄漏。
, y3 s: m9 Z& }, `. g
& ` j. l% o# z/ W N; I! |. c5 K. _4 D7 Z' S- {- x
Trojan Protection(木马防护)
" k! ]; {( P9 |检测访问木马。1 {5 j1 }& ^# V( Y
5 M, G) Z# a. v
% |3 [. h% G. ?2 G; P6 YIdentification of Application Defects(应用程序缺陷的鉴定)8 W# I7 q! M- x- n% g9 P
检测应用程序的错误配置警报。4 S0 ~% v1 o. G [, ?
- d, G5 O+ ]! _+ P6 m* E7 M* ~& z, k" C$ c4 b0 N
Error Detection and Hiding(错误检测和隐藏)
- S) C. m6 M5 A0 o检测伪装服务器发送错误消息。9 I2 q3 X4 t! V- _5 Z- }
9 r) {, ]) [* E, K: W4 l
+ H( ?9 m- [; x" p% V安装ModSecurity9 J# d+ g* b7 z. S% \1 e
软件基础环境准备7 g, Y, u, E4 Q
下载对应软件包
5 t2 q/ w% H+ p6 H0 w$ T, t$ cd /root
" a; ^ V+ w, h+ ]$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'# m! R4 Z3 a# `( U. @, l/ B
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz9 b9 O/ M' Q" r2 \) b( I
安装Nginx和ModSecurity依赖包
7 `* }, ?+ v ~% M& hCentos/RHEL$ e, o+ ~8 o$ g5 G+ O
2 C( r2 ?5 D8 H7 }" _3 y- _9 t0 ]1 n1 x- ~
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel% z; C Z$ B8 z4 ?7 P
Ubuntu/Debian
& @1 V; w6 W K! N4 F" \' s2 p0 i9 t% i, L
6 m+ ^) \( l, \* k( ~4 a$ 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++-dev8 g% Y( ?4 [9 S" A }
编译安装ModSecurity6 U4 }2 X6 y2 w' j3 c1 W, [: B
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。) r1 q s# a8 P, R, D
& D% f7 R( y" W( y
& H& D5 b; r7 r3 h1 W" e g$ a' ?
方法一:编译为Nginx静态模块
! ]/ Y' C- D" _! u, o7 N# h4 Y! j5 _0 B! D* {
$ S; V9 Q$ z) U% I9 R* Z编译为独立模块(modsecurity-2.9.1) P) O: z% U% ?. x9 ] r9 ~
$ tar xzvf modsecurity-2.9.1.tar.gz$ r* I6 |" M4 |* k* Q
$ cd modsecurity-2.9.1/. M% f! U3 G5 @% {
$ ./autogen.sh
4 a; \7 G8 g1 _: i) _$ T% E$ ./configure --enable-standalone-module --disable-mlogc
* G/ I9 e8 o3 e) @7 b$ make
" Q$ [5 {( y3 `编译安装Nginx并添加ModSecurity模块
* _+ f7 {- G: m+ e; X* b& q$ tar xzvf nginx-1.9.2.tar.gz
3 X& e% d# Q2 ?, B$ cd nginx-1.9.2& S+ O, [' G! [- x5 l3 a- X6 ^% s
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/# \! i! j% l, t
$ make && make install
% J9 U4 r# i! P' T* G+ k4 z% c方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
! D6 @9 `) {: [8 p4 ~5 s3 [: K# W, a9 P
) g. s; e% x- p; `编译LibModSecurity(modsecurity-3.0)$ r9 ]- }/ ^1 m' ^8 w, D
$ cd /root
( o4 ~$ F( x7 P; ^$ git clone https://github.com/SpiderLabs/ModSecurity
0 N( f! ], @( w- q/ G* `6 B$ cd ModSecurity M; b+ `8 q- v# j% H# |
$ git checkout -b v3/master origin/v3/master
- ^2 i3 r) y: K- {8 ?- ?$ sh build.sh
" E1 A$ n' S& m. X! y& N+ ^0 L) o$ git submodule init
1 ~& Q" f9 w' t! q0 L# j* k5 Y' |$ git submodule update
5 V" _8 N% Q, E6 |. z0 L$ s$ ./configure
/ Q% N3 L* r3 `0 A5 k/ t* O$ make
( a2 U& i# C% v8 y0 r$ make install
# h$ d2 o; ]$ \6 u$ o: M# S: NLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。$ b& ^3 S0 ?/ k/ u! U% b
& A* f/ Q/ O' k# U; i
; u. [, h% |8 P+ |( s0 K4 m, \$ ls /usr/local/modsecurity/lib
# U8 f$ T# D( ?$ L% ylibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0$ j- u4 O0 u- q& I
编译安装Nginx并添加ModSecurity-Nginx Connector模块
7 a: ^$ q) r0 \$ H, R. J使用ModSecurity-Nginx模块来连接LibModSecurity3 G0 s/ X+ z2 P z4 u
% }/ U$ g1 x4 o, i- I9 J/ O/ [
% b+ L8 w6 q, }$ c$ H, h$ cd /root1 X% O: z5 j ?
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
/ L: }# o' \9 C( A! q$ tar xzvf nginx-1.9.2.tar.gz
- W: H8 A% O1 S5 z, P1 |$ cd nginx-1.9.2' _, ]8 p. g4 l5 S1 i! q
$ ./configure --add-module=/root/modsecurity-nginx; I( U$ T9 o5 H) v( W0 R: s
$ make
( m( O. s. u# X1 v7 \; t$ make && make install" E H# s6 G5 T' V& H2 M
添加OWASP规则( ~1 f; v/ l. q: l, X( A9 z
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。, @6 O9 P2 g1 l9 i
& S) x! H! v! b# n! y
) j$ p& ]; L' k3 |* f- M& Y3 G下载OWASP规则并生成配置文件' G, d) E& @/ |2 a+ v
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
0 V$ q7 ], A9 p$ j' \' i d$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
( R) T7 @8 N9 c$ cd /usr/local/nginx/conf/owasp-modsecurity-crs8 h) h& ]) p4 Z3 s* M! R$ f
$ cp crs-setup.conf.example crs-setup.conf
7 l5 t o/ `- @配置OWASP规则
) }, v5 \" W; A- z) W- }编辑crs-setup.conf文件7 ^$ W7 }& e# c% }* E
. M6 U4 q* b% o) B8 g: J$ Z
; z/ v) X/ V3 }0 n8 F. ~& D
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
. R0 _5 ]+ d" k* Y% k$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
2 ?2 O2 d" F `5 V( P/ `$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
9 a# T9 U, G" z0 L2 _" b$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf1 g% O+ b+ S4 H
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。, a0 q0 a b7 ?, u$ [ w! Q
( `& B/ l7 _3 x& c, R
& s* h. i4 ?; V启用ModSecurity模块和CRS规则
+ R* K1 I0 y7 X2 E3 ^/ R复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。# R1 P5 D5 O( u
; B* K0 q. G0 g
4 I4 v* z4 F( v+ ~9 cmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
$ b9 Y( W2 D2 }7 n" j) `$ h! {- {: B" X
. p( `; _- l5 O" `
$ cd /root/modsecurity-2.9.1/$ f$ p6 W! _8 z- ~
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf ) J+ @0 K/ d! P; ]/ m E. d- N
$ cp unicode.mapping /usr/local/nginx/conf/* ]5 p' Z$ F" _& Z; B" j6 ]0 _) m# m
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。- y" e/ N: s; C+ U. ^& \. g
A& a l3 U* d! }- N# x" ^
* A( ^, v) `7 d1 n$ vim /usr/local/nginx/conf/modsecurity.conf
% L9 H; j. n& k6 V# q* c% eSecRuleEngine On. T9 c6 N" R) d x
ModSecurity中几个常用配置说明:
! N4 m5 X% r) c4 r* Z3 X( n) I9 o [ j6 U& ?3 W4 D
2 h' w! K2 m2 D1 l! q8 o9 M% m
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
: E; k, s2 R2 p0 M1 l0 o$ Q
7 h( Q4 ?& Z9 r! c
T* \5 t! e+ T/ U$ X5 o8 a* J2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。* {9 s( q `9 }$ A# G* u n8 j
4 ~ l! j9 q+ c0 G" S7 W
' d9 v* i$ d9 }
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。' m4 t1 k( L J/ @; Y% `/ h* ]
+ B: R% V- j, f7 Q1 B
0 Z4 D/ e& B( D8 v6 I6 q4 q7 R
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。6 d, i( K8 x& p& ~+ x0 t
! |' W7 |4 Y7 Y, j+ n1 f1 S8 z* W& p5 J) c, Q
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。( I* V, \, C2 V9 n" I
1 a2 o! n% N2 J1 H0 R. ~
- h4 a1 F* m) z- x
3.x版本CRS
2 p& k+ u7 ^/ w0 ^$ cd /usr/local/nginx/conf/owasp-modsecurity-crs3 F& {0 j% X* d- C% @
# 生成例外排除请求的配置文件" ]& J7 C1 b) ^! V8 r+ P, U
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf" C0 |# e5 Z% ^* J Y% F6 r
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf I; x6 i3 e# ~& G# G6 |7 q0 p
$ cp rules/*.data /usr/local/nginx/conf x1 v0 |; x* Z3 U. X: e
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
( {$ n- d4 y) u
; g; O( W+ ~( ^% x6 J( y) q# z6 ~( s, _7 z/ }6 F1 L, m+ \
$ vim /usr/local/nginx/conf/modsec_includes.conf
" F* b) l! f; e+ b
0 Q& [* |3 J8 c) \[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 y ^* r- T3 |( n' s$ g8 A
# E8 |3 q5 E# O/ U2 \# D# {注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。0 L1 m8 _: [: }8 f/ t
4 Q. I& g( \2 L3 X! a6 J
% r) L* g+ a" O9 C4 V3 X5 r配置Nginx支持Modsecurity5 Q# u7 R" z/ O& v
启用Modsecurity, l1 I( B' W! V3 f7 x
使用静态模块加载的配置方法4 N% ` M! v; O: D" K5 f% R
在需要启用Modsecurity的主机的location下面加入下面两行即可:
9 u# j, L& ^4 h& `% I& ~ I1 A: g- K+ x) Q* ?7 Q/ ^. F0 Y
: L! Y2 W: E. D( W) V
ModSecurityEnabled on;) b' [' p E+ b5 c; j& p5 m
ModSecurityConfig modsec_includes.conf;
% W- o9 O0 D7 H$ d修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
/ f# `% c/ Z* [2 S5 C: j& a
1 [) K# m0 a- q9 M* @
/ f4 u6 R1 L' |% |8 G1 o) | s$ vim /usr/local/nginx/conf/nginx.conf
/ p9 i( `$ [! E6 k
! R" F8 `) i; z8 |: C- q6 h r' X2 k) f" u2 t2 [& s
server {
; ?% |6 Z% G, O7 F listen 80;
, ]& K* f5 ^- I) T server_name example.com;
$ h" `5 y7 g8 u
1 Y5 \1 F$ t0 W4 g4 e, O% Q0 w. S+ @+ C4 v) p! d
location / {
7 ]1 l/ n3 s9 v: w' H ModSecurityEnabled on;: d9 E3 B$ J1 L6 q
ModSecurityConfig modsec_includes.conf;$ B' b$ J" q$ r) o' J
root html;
4 i U/ x# T; `5 O5 N* y$ E index index.html index.htm;. Y- D$ T3 C# c* w
}
2 Y; W7 q7 d( q}" i$ v0 h& s* X# z4 v1 \
使用动态模块加载的配置方法
/ \: l, E+ n" Z在需要启用Modsecurity的主机的location下面加入下面两行即可:5 r4 x4 d& @/ h
. h; |8 O" t5 ]" u& \# K3 r7 y* e
, {* S/ i2 H/ d" e+ W7 F: N3 Pmodsecurity on;
0 x6 M8 D9 \# Z6 o5 k) X& T" nmodsecurity_rules_file modsec_includes.conf;
3 O: o- [* Q- `4 }修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。( b1 I3 Y6 I6 h
1 A, D" \; B. C' f$ e3 u; O5 S
5 ~+ E0 d6 c# Z1 v3 z. ~, M$ vim /usr/local/nginx/conf/nginx.conf" ^- ~* _; X2 j! T" {
( q, X. R6 z4 _; s% X
, I9 p; C/ a* L3 h/ \; ` B$ Z
server {
0 _- j7 k) w& [: Q% C1 W listen 80;
8 i( a4 M! b- ~* `: c server_name localhost mike.hi-linux.com;' ^) U4 d3 T+ s& E
access_log /var/log/nginx/yourdomain.log;, n. w( @4 ?2 w( c' k
9 f; L* z4 T, Z8 O% P5 {; n; F
7 [ P1 _/ x1 m2 s1 g: m% |1 z9 M9 g location / {
+ k2 B5 q8 F. ?% \3 ~& ]( D7 [% |1 O9 q$ n3 a u
& G5 w* I/ M' Y" _$ q
modsecurity on;
' i) {* @2 x/ E5 e/ c. b8 U& u( B modsecurity_rules_file modsec_includes.conf;
6 ~& Y1 ] m/ U8 J" h( u root html;0 q4 G, ~9 I( L8 w, v9 p' v I. {) }8 z
index index.html index.htm;6 x/ k. x$ ?; G. m' ]
}
* {' U: [7 m% f7 Y! ^/ N}
# G2 R# W3 H8 R4 {0 M验证Nginx配置文件" a! C6 @2 b( s, j, Z& Y# g. \
$ /usr/local/nginx/sbin/nginx -t
$ c: o$ ~$ p. S* Y0 m7 B+ j# K2 vnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
$ q1 p/ }: f/ R8 q, d9 X( Ynginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
, p% m; F9 E( S {# }( v$ |% s启动Nginx
7 V) e2 E. O- z+ M5 z' ^$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
% z' p A$ i$ C% F$ w1 n- ~ \' L, 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能够战胜更多复杂/未知/混淆的攻击模式。 4 n, z: r5 _ u6 m6 M3 ^: E$ M$ |
|