|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。! W t9 h9 a- @ s) }9 f/ J
! n ?1 m$ L% X3 E7 a6 G' U一.准备工作; v! O+ h: _0 ^) I
+ v' f2 A4 L* s系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0. r: a) {( V; g# R+ }
+ i: z% a9 X' c
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% d6 b/ Y! W. U- a, d
) g6 h& S) D( g, R. P* Omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz' a0 {! a5 U# L% T1 o
& l% k- h0 q0 l s* FOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs' F( Q) b1 g" N; V2 z* h4 m% F
2 A9 n1 P0 v* E0 y2 ?- S' u依赖关系:. n( k0 f/ p) \; H0 r; n
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
7 W T* U' G# b3 a' S4 y6 O( S) o1 j6 d( |
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel- ]! U& K) @* a8 O0 U/ ^
modsecurty依赖的包:pcre httpd-devel libxml2 apr
7 }$ L# f3 ]& `, i. |- t; p
7 s6 `2 t! R! X. I, c D+ Lyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel7 \* j+ m q: r: R+ c5 o
二.启用standalone模块并编译5 G- c% g# Q3 Z! h7 }' z, K+ l( \
: \1 Z; g) Z1 X* }7 |7 o- X下载modsecurity for nginx 解压,进入解压后目录执行:0 S1 S( \6 J: h% i0 e( ~1 _% X. Q
: x9 L. @: c" L( R1 [/ N' w2 b) r: f4 R& H./autogen.sh
; c" ^ S9 l6 g./configure --enable-standalone-module --disable-mlogc
/ k1 M( W) M, R0 Hmake
3 k4 L: m. g6 K7 D3 S, U9 o' d三.nginx添加modsecurity模块
- \: [ ^ Q0 s, w8 b3 _% T6 m
: a. o; B4 @7 |: L, k$ b) N! _, |在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:5 I( u4 |- u/ v2 _
% W5 [( T- q$ p7 i8 M2 l
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
' C7 \3 H+ R; qmake && make install
n' ~3 O( {! { N9 X9 m四.添加规则
) Z% y y% v% v( R2 y2 y4 F
- Z! x D3 J: s9 \0 @7 x. vmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
2 m; N, }4 ^( L5 ]" ^8 z
$ |, ?; A, K E. |1.下载OWASP规则:8 ?, K v( r+ z l3 B0 L6 h
9 e/ c6 o8 W! T2 y Z* y+ Igit clone https://github.com/SpiderLabs/owasp-modsecurity-crs0 W, a- N u" y+ @
: ^3 ] V- B; M
mv owasp-modsecurity-crs /opt/tengine/conf/
; \7 N) J/ u! r
) W% r2 V6 @. b6 E" mcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
9 J+ S7 `. h! J7 P4 S( v2.启用OWASP规则:
" j0 w& l9 w% v$ n4 j
4 ^/ ]* S0 y4 Y复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 j9 n% X5 Y+ x! a' _ P
7 V6 P2 x1 v6 D, @% t U
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 N5 R0 Y7 d$ u9 h+ Z' ~3 Y
* ^2 Z: }4 t2 n
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 r5 _; d6 A9 c0 r. A
& l) B5 m( I! d
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
% k# `) T) e) c7 _4 H. y2 b: |' e, SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf& @. R1 n- U, B6 Y2 q- v+ X9 I' Q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf/ B- T7 Q# G6 f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
6 k6 o8 u/ w5 T5 u; _/ ?Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf2 [+ M Q0 J* z& r( T6 f
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf7 g6 b ], J) a
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
' C& k* e6 ], U2 B t五.配置nginx
, h0 p5 f; A1 D. p5 w/ c& v4 R9 c$ w, u4 _& t
在需要启用modsecurity的主机的location下面加入下面两行即可:3 L% k: h) V- }: @+ v9 M
$ F% l" j1 N/ F* yModSecurityEnabled on; : x% S. b( j/ ]$ ]& S! v5 R3 `
ModSecurityConfig modsecurity.conf;
6 T. D- S6 Y; X( B下面是两个示例配置,php虚拟主机:
) B+ h `# r# A2 c- t% i1 e+ L/ ?6 [9 o
server {7 Q) n# o$ c% F6 D' A
listen 80;( p2 A7 d# o' q5 D
server_name 52os.net www.52os.net;& d8 U6 z! V& b8 g
# D5 n5 A/ {- S8 B location ~ \.php$ {
6 ^( B7 ^4 d' h0 Z ModSecurityEnabled on;
8 E5 O2 r5 \+ Y# C) {6 G ModSecurityConfig modsecurity.conf; U0 B# W+ B! I
2 ^0 m: v) K' l) D. y0 A. X root /web/wordpress;4 [7 U6 G/ ?' }( y0 U
index index.php index.html index.htm;
* e- g4 T) l# R9 [9 Q1 A6 Y
3 { d8 `7 S$ |: @ fastcgi_pass 127.0.0.1:9000;
: B) Q5 b' i. E& u& [; L' n fastcgi_index index.php;
' v, a8 A3 o! I8 _8 H# L2 ^ fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
% h9 o8 n" M4 \7 e5 s include fastcgi_params;
; J5 t7 ]! V+ P' T6 ~ }
9 u0 Z+ Z4 G$ |3 ]4 p5 K }
2 d4 h: L2 \1 {0 A; _' {( ?upstream负载均衡:
1 l e1 r& D; `( u5 _8 i1 w+ j; e
upstream 52os.net {; u0 q, o4 y7 f3 z+ m
server 192.168.1.100:8080;+ T* R' b0 f) {. q. c
server 192.168.1.101:8080 backup;1 t+ g! \9 b0 E/ s1 O$ t% C- H$ X3 q
}5 D. l2 g) @1 n6 }2 ^" r
( I; y7 N& `1 ?6 @* kserver {; q; b. F4 R6 j3 \6 g
listen 80;
$ T( e0 q T$ [: Xserver_name 52os.net www.52os.net;
/ I2 l0 X ^, ~+ G# K9 F% u' j9 z0 D7 E
location / { _6 k5 @5 |( d4 Z7 X0 z: t/ x
ModSecurityEnabled on; % ]# O: ^) a/ {+ s T* F
ModSecurityConfig modsecurity.conf;
9 A7 F+ i( {" c! r4 G4 q* O( c! K$ ~4 q
proxy_pass http://online;! p& q& T' u" E1 R5 V, l
proxy_redirect off;" Q$ P! q! j" B2 l( m
proxy_set_header Host $host;- G M+ Q, p" P0 S P8 V7 k1 t" A2 p
proxy_set_header X-Real-IP $remote_addr;
" `; {: u2 p, ?* J a _# d proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;; ? h5 v3 l- @/ u7 o& R) \; ? O
}
# ~' ]3 @/ S0 |2 ^1 C1 v4 m" |3 t}
5 N3 U& Z# k* V9 U4 P' @% ~4 }9 q六.测试
; I) b7 E' |' v$ ?2 y
& |& a X' \% ?: K9 l7 [% z我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:8 o/ j L! V1 \5 z; m$ w5 x
2 \/ }; x" P; p5 j$ Z8 Q<?php
/ u# h( j: A* C! g) C I5 n' s phpinfo(); , G6 X) F) N; x" M- X+ @: {
?>
. k- v( D; x) |; j6 S( }5 F在浏览器中访问:- Z! y, X6 Q; h& O: j6 r
: g a5 w7 B9 j+ R4 d P6 M+ ^8 G9 n
http://www.52os.net/phpinfo.php?id=1 正常显示。
( b% F% k- U9 ~7 x$ S$ g. S" ]( Bhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
. r4 Z4 L W! a9 vhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
% K8 Y' }, f1 r0 _; M& g说明sql注入和xss已经被过滤了
; r) N+ _$ ?5 P8 T2 G2 L' n; C+ R! _: U3 D4 ~
七、安装过程中排错
: i3 f; S. j( B. y
$ a0 I' A) `& D$ a) n+ D$ [1.缺少APXS会报错$ ^3 H6 b) P2 B
d1 H. s$ q( U8 b2 F+ f
configure: looking for Apache module support via DSO through APXS
6 k5 B: l% Y$ ^. L5 s" ? Aconfigure: error: couldn't find APXS% B$ T3 A. {& O9 p7 ]
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。( L6 C" z. a. e. A( a& y
解决方法:; _1 [1 J& q5 i) W/ a/ w% ^
* S. z0 X$ H" E1 d& t2 k
yum install httpd-devel
6 S6 B2 c1 B) V. R, e2.没有pcre
2 t/ W3 Q& B3 ]& r) t( I; M* E
2 x ~+ @# `: H0 t# Zconfigure: *** pcre library not found.8 I" p @3 u/ P# ?& C' |5 H. Z& W
configure: error: pcre library is required
- e1 `9 [0 o) d; E2 ?解决方法:
% |4 e) S# S( g B8 @, h/ m
( ^3 T' a+ }; ^, Gyum install pcre pcre-devel1 E+ q# m. k" N8 e$ b3 M
3.没有libxml2& d7 r" e& X: X2 y' t/ F( F% g
+ @; \& f0 _1 A8 Z/ M0 D2 |, q( e% @
8 u! _; Q3 |- Q& U. n0 xconfigure: *** xml library not found.
3 d. v! Q4 Y1 E/ t# {% W3 _configure: error: libxml2 is required9 Q/ j5 \# S3 w x& ^
解决方法:$ D& X, W' |/ h- L+ n2 s5 Y6 p5 S" H
' p4 `+ l- }& W/ v+ kyum install libxml2 libxml2-devel
) J/ K, J# [% R! K6 E4.执行 /opt/tengine/sbin/nginx -m 时有警告
! [) P6 [- m/ H4 @% h
4 B' s- Q$ I5 q7 j, KTengine version: Tengine/2.1.0 (nginx/1.6.2): k- c5 d( ?2 K& d
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!/ |, {' @3 T! R3 X2 k
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ T. L2 u9 J' K! c/ t
7 v ]6 B- ]* [5 R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.- ^9 ^* i9 ?8 S% l0 V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
# M# y- G/ v3 a2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!" O, U7 h3 r: @; n1 J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 X7 F: H# Q; C4 _9 I* A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
. G- y* X1 Y! i( g, V8 d. _% \2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
4 {$ ?+ y' x4 o+ a1 M解决方法,移除低版本的APR (1.3.9)
R6 Q# o2 H/ |0 {% ]$ k# I" u' O: C3 ]2 x2 B/ ]
yum remove apr
b& }4 @: Z4 ~2 c5.Error.log中有: Audit log: Failed to lock global mutex
0 ]! Y2 u A, p% Y$ B( S3 q" _4 G0 i1 T5 ~) ~- ?
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ! c/ Z" {, t. b7 D& H
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
3 ^, ^" s+ l% e) o9 ^9 F解决方法:
" r& k+ c# v0 ]编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:: c" c' E4 c( N
2 d' d# D8 T5 mSecAuditLogDirMode 0777' H0 _/ G6 U4 l ^& B2 k
SecAuditLogFileMode 0550
/ ^$ v% U T8 q0 D) F$ K! s3 u1 dSecAuditLogStorageDir /var/log/modsecurity8 D, m! `. ^" l$ _0 X W, O) X
SecAuditLogType Concurrent
( u& o& z: G2 Y0 P8 k u3 p4 a参考文章:/ \) a9 T: k! W, M+ h t
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
5 `* ]& I) ^) R8 K' L, phttp://drops.wooyun.org/tips/2614 |
|