ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。4 F3 Y) Q, \, N
/ x5 f+ f# f$ L* b, g
, B! K/ _5 n2 \& B9 c在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
6 E) w# G5 @2 C% ^4 p3 R& @5 ^; j. A3 c7 ] H4 ~
8 g9 Y* x3 S* b) w+ X- T; o什么是ModSecurity
0 V/ H; r d5 G+ I* F) OModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
* C$ d# }3 o2 {; ]
6 G8 d+ ~: g8 O N
3 W; D# e0 g# V h. Z6 @ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
5 ~# k) x% W8 L: v; ?8 S
y8 F+ ^6 D' I2 K2 _- S$ l! N
. L/ ~3 K0 _2 s" Z# m5 NModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
3 g9 q$ v) k- b8 y- Q- C/ M6 h6 r, B2 h
. {. {2 m0 C, O( `官网: https://www.modsecurity.org/
+ K3 B; o8 s3 `, T6 W: R+ b4 E0 ]' O
/ v/ ~, v9 y+ L0 i- x% b2 V' f
什么是OWASP CRS
, D; D/ C3 m! vOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
6 C. k4 [* W, {5 W+ n! D% T3 ^ w( V' A; a) d
% C0 I+ m* _+ zModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
1 v3 w4 d+ u6 F. U |& H) M
! }2 d1 i' L; A) ~& O
6 W4 E7 X. n3 m) H9 w8 pHTTP Protection(HTTP防御)" [7 v# z5 S+ R& ]$ _2 F
HTTP协议和本地定义使用的detectsviolations策略。
2 `7 [0 j6 ?% [: k% E* U1 D) M i! @6 v
2 Y6 |/ l& w. v3 p, p
$ q& L! r6 |3 v# aReal-time Blacklist Lookups(实时黑名单查询)% N/ x* p/ k8 b6 z5 ^4 L" U
利用第三方IP名单。
5 A/ F& ?. v2 l7 p+ f# \4 M- A& R8 b3 ~3 q( S$ w
& R0 w E1 y& R+ H9 t1 m% \4 Z; Z
HTTP Denial of Service Protections(HTTP的拒绝服务保护)) {; a2 u: r, K3 A5 x2 B
防御HTTP的洪水攻击和HTTP Dos攻击。
5 x: S6 E, Q. N; b
( [! @$ o% }6 t$ [4 A: T
6 t g0 `& d4 ?/ RCommon Web Attacks Protection(常见的Web攻击防护)
1 v+ f3 ?( D$ j8 g# a检测常见的Web应用程序的安全攻击。
+ i( }" D( L, i/ k8 M3 o/ m" j g; `7 ?; p
$ T. X, x. Q- Z- c t4 A
Automation Detection(自动化检测)
2 }" E( J+ }* Y$ t" _5 P2 A9 ~5 a4 M检测机器人,爬虫,扫描仪和其他表面恶意活动。8 f/ m( k# R/ ^
- S" Z/ `4 a. p/ J, _
$ ?1 S/ c" o$ \, X- E: _9 YIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
$ n( b7 u9 h6 T6 V检测通过Web应用程序上传的恶意文件。6 ?) F* h+ B( u3 u( A& r" D. ~( S
A- L4 ]2 t7 d \. |/ [3 I: ]
& N) c* z3 I* w3 s2 z7 O* @Tracking Sensitive Data(跟踪敏感数据)
# Y/ S E3 d" Q4 h信用卡通道的使用,并阻止泄漏。+ M+ @2 O2 B/ j6 Z- J
0 S5 u4 H {+ ?, z/ G) }3 @
8 D( d: X- o% L9 V1 `Trojan Protection(木马防护)
- p% J% m/ N- s- m$ e4 K检测访问木马。* N- y6 b: z/ |' f' S
6 T7 n/ _: Z% l2 [! C' I' D* g1 W% K" c: h) w! D( K0 y
Identification of Application Defects(应用程序缺陷的鉴定)+ U; R+ p$ h6 @! h" t
检测应用程序的错误配置警报。
. m+ S) S7 z/ Y+ H! C5 e6 I8 V2 E9 i6 ?
" \: L D6 ?3 m ^
Error Detection and Hiding(错误检测和隐藏)1 ^2 h1 v; b' S" \% c( B' u
检测伪装服务器发送错误消息。5 ]- v. b4 u6 c( l$ h/ {
- C* b5 r% l6 H' C, l- v' ~
1 n9 m7 F& V! c/ m+ R$ b安装ModSecurity
. Q+ b* j) D$ a5 L软件基础环境准备
. W; V" m4 l- H下载对应软件包8 c& Z9 S0 S7 ^" W* d
$ cd /root
L+ o8 @, t& u! `; @ H* [! j( _' [$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'# V( f' [1 I% \' P, C1 ]* ]. n
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz- y; D% m# E1 O9 [
安装Nginx和ModSecurity依赖包' {" c; R/ y# s7 e) T
Centos/RHEL8 Y5 x: ]7 ^" _( l' ?, Q+ C
' B/ t; ^5 Q+ c- F) N, ?! e
5 J3 r( N- q& a
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel$ o; V+ Y+ E8 F% j
Ubuntu/Debian% r* ]0 G( W( P! U. o+ L4 K
+ X9 H. X' f" m/ |5 E" q* t# y0 j; L' d6 d3 }) i1 n' T/ S
$ 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++-dev2 G9 ?/ W5 h4 X5 Y \; f
编译安装ModSecurity, v- m1 w) i! o8 @4 c. w8 p$ j
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。. ^. c9 ?4 Y+ Y* ?
* [0 L6 m' H: K# ~
/ s+ U r) b$ ^
方法一:编译为Nginx静态模块. H& M! t2 z5 f
: S" I% O4 i6 \! w' f0 S( K9 M. Y
7 I" ]7 N$ ~0 \& F% v
编译为独立模块(modsecurity-2.9.1)& }$ I& L+ Q3 C. P/ T- T7 V
$ tar xzvf modsecurity-2.9.1.tar.gz$ o+ e6 r; m# _2 `$ _: }
$ cd modsecurity-2.9.1/
+ |6 I. K3 g/ o, f" L" ~/ |$ x$ ./autogen.sh
0 b0 b/ y, J3 m# |* L; K$ ./configure --enable-standalone-module --disable-mlogc
; r, h$ K" Y2 \' d$ v" l$ make
6 Z8 j4 h5 Y0 I编译安装Nginx并添加ModSecurity模块
! r6 [) _- ^0 U# L8 d1 C$ tar xzvf nginx-1.9.2.tar.gz
6 u" h# g ~8 f) l- e1 @$ cd nginx-1.9.2+ t, p, a& {4 t
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/5 k: P1 a6 @) {2 I- `
$ make && make install0 Q% ^# z; t. p+ a
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
* N$ d, E+ d/ ^6 u3 V% l
2 @7 ]. n1 B! i) o0 V1 o
# |1 u) o' V+ n编译LibModSecurity(modsecurity-3.0)
7 k1 n1 I$ J( Z$ cd /root
% f' \% f( w6 R) x) I+ ~$ git clone https://github.com/SpiderLabs/ModSecurity. m" I0 |5 R: E2 I* q3 l
$ cd ModSecurity7 i( x; t9 u: ` Y7 M
$ git checkout -b v3/master origin/v3/master
! D/ n4 i4 `3 p$ sh build.sh
3 R( Z V/ r1 @$ C$ git submodule init
/ }! l8 s. i# z4 G$ git submodule update
$ I' @- E# A+ G% d$ ./configure! r6 s( ?1 X2 g5 q8 k: ~0 X7 ~
$ make K2 N# b% X. {5 X0 m8 M
$ make install
7 H1 s% z# S! U7 B$ G3 b0 jLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
$ U2 J& N& {' c1 O. f( R. I+ ]6 B* R- j( D4 s" {# L
# U! H9 g/ ^$ y% {2 j
$ ls /usr/local/modsecurity/lib
4 M( j+ H! a% y( Flibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
$ A9 @6 W; K" }& n; h$ [9 }$ j编译安装Nginx并添加ModSecurity-Nginx Connector模块
1 U* ^) r) `% K0 G使用ModSecurity-Nginx模块来连接LibModSecurity
v7 S) n/ V. e) m: F5 S8 c
# \9 z" |" B1 Z5 @% m
: E5 k+ E d% \( f( O, W5 g; H$ cd /root
+ s# r2 A1 S* |$ p/ Y3 h$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx% W- i+ j' X0 v, [! n
$ tar xzvf nginx-1.9.2.tar.gz
: M6 q7 _2 o) H6 I, K$ cd nginx-1.9.2
+ w2 M* b, C/ Q2 M( h" S7 b$ ./configure --add-module=/root/modsecurity-nginx
' _! Z7 k; U5 s9 q1 z0 E$ make
E1 j/ t% [8 L9 h' w! B1 T$ make && make install; X3 Q# H6 d5 D5 d9 a3 ]' f$ I
添加OWASP规则+ i0 Q6 t0 l- t0 `
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
6 `. A. X) K, R) r s% F* F' N7 p! `1 l5 b2 s
% \4 b+ O; r$ |8 P; @. {
下载OWASP规则并生成配置文件9 ~7 ^ s3 k' W( V
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git# c0 D8 ~' K( o/ ?) a4 I
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
2 o# r8 T4 B( t3 o% e5 e- o$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
/ R: t) F, ?# y& j$ cp crs-setup.conf.example crs-setup.conf, I' J2 I E0 {! t. |7 S! o) `; t
配置OWASP规则" |3 V5 V- F# X( k; T
编辑crs-setup.conf文件4 E& }* L' a% x { _9 `/ T0 h
' [' o$ g# O6 n& a* J
# ]0 U T2 ?5 M9 v$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
+ l" X$ U) ]) x7 p6 \$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf) s" n' k5 @' `1 u6 w
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
3 G! X5 E" Q% M8 q$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
9 f: W& a' Y3 d0 e: q) m* n默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。6 v0 k& x. h. M
2 \, @& _5 h0 q
4 d2 ~# J( R9 a
启用ModSecurity模块和CRS规则7 B5 h( F+ A$ w" |/ q+ i
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ n* W; L+ @% j
4 u/ c- i! g5 q. y4 P& U/ U* R
! w0 E0 }1 y7 cmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
4 Y s3 n2 x- {( Q
b6 e2 k' a0 d L4 p
7 I# ^! |( _$ B* E4 L3 W1 t$ cd /root/modsecurity-2.9.1/
2 X/ Z: Y' E( G5 K& p$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf ( Z" F9 ]4 z0 O
$ cp unicode.mapping /usr/local/nginx/conf/" ?2 T$ ?) R' k% z; j
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。8 f4 O% W1 l# E" Y: B: _+ |% L- A% _
! i6 k" @. n$ N5 r, V
2 ]- T; z# t9 @) V' C/ Z# I( |$ vim /usr/local/nginx/conf/modsecurity.conf5 g& a# M5 i* \' K: ]/ F* _ C/ |
SecRuleEngine On5 } ~6 G8 R4 @; x* V; o& |
ModSecurity中几个常用配置说明:
0 a% P1 R8 g; m; L, c& L: j3 W ]( s5 g! `0 f
6 g) ?! Z3 E- `0 s }' A( ? ]1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
4 v) D y2 \( i7 ~6 ~* s7 M2 P l8 x+ z* I/ |' H8 s
. Y5 d' y6 ~4 K, n2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
; y' i( W$ Y* }2 V& \5 R; h. {7 @, x, {+ |8 o, M; {( ~9 L
+ ^0 l& V% c3 @6 y2 i& U/ k
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
4 ^8 {5 |4 V5 t I
4 k0 X+ z9 p! `+ x z2 ~# O! x% O9 F* N: F: Y
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。: h9 \5 R" r- g& m
( l0 a' w2 F* f8 s! L( d6 o3 {4 R1 ~9 l [
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
7 U! m" L; w. v: d8 P$ E) I9 h# G. x4 n' N& |1 n
8 j. Y0 a4 C: C+ @2 O3.x版本CRS
2 O: Q6 K& W, d3 @- n! f$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ v9 x+ q5 y: v& b. I4 J: e# 生成例外排除请求的配置文件
! x9 M0 G( g) x+ q: z5 N7 j$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
" P7 q* ^- e1 J' z0 {$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf8 Q$ g" }" O+ c3 s* X
$ cp rules/*.data /usr/local/nginx/conf
! N7 c& Z/ ?. n7 T9 W为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。- k- A% u" N e" S2 I. e
9 `6 V6 h" a. i9 f# @
* V% \" l) g6 r, j$ vim /usr/local/nginx/conf/modsec_includes.conf- ?. {0 I' S" x' Y0 s
( H3 `9 \% l o* T! 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
# T) \# A: g1 G+ z
, ?6 w+ j% f$ f6 I& ?" a注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。 i/ z1 Q: B. r/ {" j! N5 L4 C
' s: U: P ~2 N) a0 [) l+ I2 k- K/ d
配置Nginx支持Modsecurity# I4 p1 @/ P+ r9 h6 O
启用Modsecurity9 f: ^ i/ {8 e# j
使用静态模块加载的配置方法3 d' {- M5 W! O0 M: N3 m4 F# R6 b
在需要启用Modsecurity的主机的location下面加入下面两行即可:! a2 U! _) v; N9 s5 N, M* f
8 Q1 W j5 V& V' A% E& Y
+ J5 ~7 L' C* B+ ~8 l& X- h
ModSecurityEnabled on;
3 P6 q+ Z9 O3 e( y) aModSecurityConfig modsec_includes.conf;
+ Q" F: p+ Q2 O6 K0 B2 d3 g$ ]4 _修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。0 f5 S+ d" p& u: q0 m
8 M+ V( D5 a' g% A+ i2 I8 V* k
h/ }# G: }- u4 w9 ^3 U# s# u$ vim /usr/local/nginx/conf/nginx.conf' t$ H R; n% a$ p0 K+ G- k
2 Q ^) Y9 c, n* ^ m
9 M" M2 L9 _, m% f0 n! U0 l, u4 G' h
server {/ y( s$ o6 P) f. U
listen 80;
8 K$ O. C& Y8 t. b server_name example.com;
3 Q9 u/ `( f+ ]8 U& a5 r
- u* @. }; m( Q
) l, Y- ]: h5 k, a6 N location / {8 o. h. v e0 v, n# |( x9 A8 V) S
ModSecurityEnabled on;' D$ F c) F) a$ L1 E& s8 C; \
ModSecurityConfig modsec_includes.conf;: z4 O" x& a& p" o+ X7 q9 e
root html;
" i! | @) p. w! `# W" F# I# Q2 A# A index index.html index.htm;
4 g( t8 F* `% Y+ A) z }
9 u- R7 f4 z: @1 V8 S}. y t3 t/ c6 e0 ?* P
使用动态模块加载的配置方法
" `& c. H5 ]2 a% V2 s在需要启用Modsecurity的主机的location下面加入下面两行即可: W5 }" R. I2 [( N6 s/ R3 F3 i
# h: u4 i4 T2 G- e/ m
6 |5 D3 U& l' a9 j0 L4 Z9 Y5 x
modsecurity on;
1 d1 D ~- V5 k0 P0 Smodsecurity_rules_file modsec_includes.conf;' g( S7 q( A& A. G
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
" w6 v: q1 t! D# z3 L* Y* G4 F) r$ A8 U$ P9 i
+ m" C% R4 ^3 I O2 z, e9 q5 }$ vim /usr/local/nginx/conf/nginx.conf
; V0 v! M3 Y5 R1 O% u [. H) r$ G$ p, m2 E& B! c% N/ [2 J, x& p
( p0 j% j0 r u
server {
6 G- q7 n) c; X! n) g" r' J% U1 T; f- J5 y listen 80;
1 Q# ? i1 f; U! A: x8 L5 n server_name localhost mike.hi-linux.com;+ V3 R, l2 Z6 r1 E; a
access_log /var/log/nginx/yourdomain.log;
/ S$ X: \8 i2 B! ~. o
r" X3 i6 e- {+ b% s5 f1 b" Y7 x6 s9 `' T* J" o `! i2 e
location / {
: D5 m5 g) @1 ~, e$ W! {/ C; m: B
: M1 T& ?+ X9 Y- ` d: v* `1 f- m
$ h6 x5 S6 N. J4 i) E7 n$ o. ~ modsecurity on;7 x) h5 Z9 ~! c \- O
modsecurity_rules_file modsec_includes.conf;
0 J7 i8 w- P* j- Q root html;1 R6 X, |+ D, q# l; w
index index.html index.htm;% Q$ u$ V$ O" {; i2 _( F
}; K2 @: y: H2 v2 D) K3 h
}
3 h$ ? ~: T/ ]- J' X r0 y$ m% V. O/ b验证Nginx配置文件
& b$ y/ q" P1 R! ^# |$ /usr/local/nginx/sbin/nginx -t
9 \: p: {7 I3 i, J- z$ J4 j1 fnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
4 Q" ~) l- m) ?nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful& @# t2 k; p" M9 W+ y8 {. v
启动Nginx
8 _# w6 ?* A9 H" c H8 y$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
S* j- K" P( m- j" e/ Z# n" } 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能够战胜更多复杂/未知/混淆的攻击模式。 . \" c* ]' I3 E: M8 B+ D
|