|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。/ C. s8 g/ I! Z: V" a6 t6 T
# t1 v" h% Y) k1 ^一.准备工作
/ L/ k3 E, t. s: s8 p% s. \: N5 @+ L# t' I. ]. Y4 ~' g
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0$ p* A& m2 W' O
0 x& y" z7 A, `- ztengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
v7 m* K& Q4 c& ?7 T# P8 L; U5 Z$ Y' `* p
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz* P3 |+ ]+ a3 }
7 S `( h: z) }OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
9 X, R; r# }" n# |$ K* e8 G
1 b7 x# B3 l4 N) k2 b依赖关系:0 v$ t' Y, p6 z/ @
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
* u4 J& m& y! p1 O0 U; P L6 \4 O# J
2 T0 L8 H, u N! ~yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
: H6 @( N) s! C0 |+ Smodsecurty依赖的包:pcre httpd-devel libxml2 apr
5 R6 e5 W1 q( D6 S* E' r r. o" T* B6 ]* j/ @# i
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
7 J; ?* W3 X: _% B二.启用standalone模块并编译
4 e: ~0 U; u; ~& [3 b: t
+ }* N, @% W# t& L7 p2 I! b下载modsecurity for nginx 解压,进入解压后目录执行:9 Q2 p# k$ w0 r$ j6 M
4 H; c% L, n$ B4 C- d./autogen.sh
9 O' w7 G, T, d0 h8 O./configure --enable-standalone-module --disable-mlogc
# C+ D* { Y" g' _- ^8 z; [/ nmake
" ^$ \4 J4 G/ q. [0 _# \三.nginx添加modsecurity模块" g, n1 |9 `1 L3 ]4 F7 P
4 y. b& K9 e; K2 `. f在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
' `* x) o. m) s$ A: U- i
1 k6 }+ D$ q. j* A3 C./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
2 V4 K2 h% n. p, }' ymake && make install
i B: l N+ @# C K8 W- E( {. J四.添加规则2 a, P& C8 K3 S" p3 a& y. B
( @( Q( v! S- }4 d3 {# W1 P
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。8 j1 K T8 d$ {$ K2 S
4 H- x8 N1 l# |) B+ k; ^$ x1.下载OWASP规则:9 Z9 r. N' v" Q# C+ J& }; M
5 n( C( C- r4 j& Rgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs* U' \5 c, e0 j [! @; y
- A7 w* h% k5 k7 W; fmv owasp-modsecurity-crs /opt/tengine/conf/. r* b' w0 J+ e! N2 o# ]
) C# q, M& G$ t) `7 Gcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 F. t# J+ o% N/ |2 j" U: s( d
2.启用OWASP规则:
; W N1 k7 x, N8 i, I3 c. ?0 o
3 V8 _! q; b- I7 o2 m复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。, ]/ z' ^0 r' Q7 {4 y
" B e) b. x! D' H* p$ n, H0 k: u
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
7 I- V8 K0 c$ S+ f( W5 y
1 D" g5 E0 T4 X5 g# v3 }* Zowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。! w& l' s/ m2 G# P6 L
8 B) l3 Y1 Z6 U+ ~: k
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf. C0 }7 p1 ^4 T+ E" O7 v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf/ ?3 k, V3 q. S, T* N+ e" S6 B
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
5 p% D1 T4 x, L- |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
# j1 U. w" ?7 j4 t3 c7 a; @Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf! D! }% i3 L# M7 @' @2 G
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf1 Y6 s/ I" J8 l5 s
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
" f( c O, ~" N+ _( g五.配置nginx8 e+ [( T. i; ^1 y
B, ~3 G1 k; ^' W% f在需要启用modsecurity的主机的location下面加入下面两行即可:
4 h4 b7 @! K J% v
: f3 V! {! j- s" [, R! EModSecurityEnabled on; 9 \8 O# W0 r4 r+ s- Q
ModSecurityConfig modsecurity.conf;
2 l1 k4 P5 {5 A, p下面是两个示例配置,php虚拟主机:% @; a" E# w4 R \. S) S7 w9 g
4 @, \1 V( m$ T" A& h% t
server {
?1 c& N% M2 f1 B listen 80;
- A; I5 f; l+ r server_name 52os.net www.52os.net;
+ I3 u, F9 U$ k( m. C! M1 a' l
% a0 `' q% E. k5 X* Z, R location ~ \.php$ {
$ c. {) `2 N* C" a2 Q ModSecurityEnabled on;
0 z, d# L0 }6 n; k) q% I% v; t! i ModSecurityConfig modsecurity.conf;
& k# C9 `- Y" c6 O' z0 X: `9 k! a0 T# c5 f# r5 M
root /web/wordpress;
7 E" s8 X# m& g# n) L index index.php index.html index.htm;
" D! Q. |; V- _1 Q + b# ]9 n3 W5 I" o$ H
fastcgi_pass 127.0.0.1:9000;
& r0 g* s3 T) x/ H- y5 q+ }. O fastcgi_index index.php;9 r% U( D# W* {! o
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;& E* |+ v3 n# L4 X; }% i7 C. G
include fastcgi_params;
$ r9 H, A; x2 ^% e2 F, u }& K& j0 D$ |0 a; \! N0 P
}
, ^1 p, e8 Y; e- T) i& Fupstream负载均衡:# D' c5 N, @0 U$ M- P6 F* B
# M. D4 k5 o$ s6 M
upstream 52os.net {
4 B* f* l# c( }$ K* |# z4 n server 192.168.1.100:8080;
" K% {7 w, {5 k- A+ x( z6 _; M' h' h% O# v server 192.168.1.101:8080 backup;+ \) k7 r8 \, H9 g' X
}
% r# ^, U+ k( j' E2 T! F
) z+ ^9 w) E5 Z, `1 @9 L/ `server {+ o) i6 t9 K9 U' Y" n- k$ h* C# g
listen 80;4 Q& o& C! K# J5 L. f) R+ X+ }
server_name 52os.net www.52os.net;: @1 V/ t y5 ~
* h5 p) p, m+ v9 p. L2 M/ I
location / { _2 ?- R0 i$ `% d0 t9 y) v: G9 V
ModSecurityEnabled on; 0 s( T; Q+ j; O8 N. h! j- u$ k, o# m! X
ModSecurityConfig modsecurity.conf; 4 q3 r( d0 s4 Q7 P" z0 F) T# T5 \
0 _- @9 ~% Q# [# d7 l$ T, _
proxy_pass http://online;# b- r- f& _; X { D7 Z
proxy_redirect off;) V9 f' z8 g% I! W7 u
proxy_set_header Host $host;% o L( w& R) @ z, H# s
proxy_set_header X-Real-IP $remote_addr;
' t" G6 f8 Y" b Z0 g proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
4 S. X5 x$ p. i- G) ^ }
5 |- W( b7 B5 F3 H/ `+ B}
% Y2 w2 K2 m( G6 O! q六.测试 J5 W( ]: W; [# B
! d. j+ N# Q3 P, I我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:( _5 U' [ |, {# i6 k. K; X
/ `) u9 ~5 F. E<?php
$ ^2 U& `+ K# h" U9 \# i phpinfo(); 0 `4 Q# V8 u8 X. @
?>6 O, ?' f E, H4 h+ N" z0 {# i% d# o
在浏览器中访问:
8 Y8 _, U) i6 P N2 R& L$ q4 Q: p- n$ [* u5 X9 K
http://www.52os.net/phpinfo.php?id=1 正常显示。
3 t. C6 A+ T( N6 fhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
! Y0 ^7 ~8 ^5 \7 Xhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
1 y" r) Q* \: R& B. b- x说明sql注入和xss已经被过滤了
4 t+ F% ?# W- M; N
( r9 j4 k3 \# ]. C* {/ G+ W9 L七、安装过程中排错
7 A% m1 z# k& r' d2 a
7 n( |+ ~5 q) Y7 W1.缺少APXS会报错% y! J3 Z1 r2 K3 z, }
; d5 f9 i7 ^& c& ]6 b
configure: looking for Apache module support via DSO through APXS
/ l: `/ z0 c: d/ Yconfigure: error: couldn't find APXS) S2 p* f9 ^! N% f$ L N* _4 p
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
9 P) G; u4 z5 w X4 ] \解决方法:8 \2 Z, \8 T) w/ T U) k
# O; S/ o) f/ f6 |+ s6 U, G
yum install httpd-devel, G; M' ^5 E6 `6 ?! r
2.没有pcre* z) ^* A5 a/ S7 I7 H8 @, E
, \' q. i8 h1 B- l# r+ O
configure: *** pcre library not found.
0 r0 k; {4 `+ Q( F/ Sconfigure: error: pcre library is required0 i: Z8 R, {* _7 \$ K( Z
解决方法:
7 A6 X W* d' H4 L }6 M
/ _5 o& e: L1 A% o! Vyum install pcre pcre-devel
; x$ `6 k" f& ?. o7 |# S" {% u; ~; L& H3.没有libxml26 a1 v j! y( O8 C" Y
: a5 e3 ~" t& u2 o
# l: E6 C. Y* q
configure: *** xml library not found.
& q% K3 @: ?& b; V" T; Sconfigure: error: libxml2 is required
4 W; o5 ]2 d) m/ A- y解决方法:
+ V9 n1 y, ]& q! L4 a
" a+ N7 `& ~. Z9 O5 o, v+ t2 e, fyum install libxml2 libxml2-devel
6 `- H4 G* f5 p5 |8 }, i4.执行 /opt/tengine/sbin/nginx -m 时有警告
. ?: I% C# x# i- i4 ^: X4 f7 _& t
9 J! @1 q# B$ y1 Y4 ~# ^Tengine version: Tengine/2.1.0 (nginx/1.6.2)
) R: t* m- v5 H$ d+ snginx: [warn] ModSecurity: Loaded APR do not match with compiled!
$ e0 h @# n/ L x5 M原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log* t% K, M: v; J" i8 V( q4 ?; ?; ^! y
5 H' p+ k+ U7 Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.7 k7 T `- H' z8 l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"' @$ n+ T) }' l, G0 E
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!+ i6 N8 y8 U, ?. C7 l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"# }4 U& R, ]' I7 O. }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
5 u, N# Q& i/ |; ]) {2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
g) I) `( B! f s: g% m. P; `解决方法,移除低版本的APR (1.3.9)
4 @! ? P3 i; ?$ J: u5 i- g" e
6 T! m5 U* g. I$ |& @6 A1 Xyum remove apr
! r) F \2 }) j) D* u) S# f( X5.Error.log中有: Audit log: Failed to lock global mutex
7 B7 J% a: F5 b" b+ s& B) k
* i1 ~& W& ~& m0 H& T' `, h, K2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock , D5 D- ~1 o* q6 K# B4 t/ ?
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]& R1 P5 x2 Q9 h1 V5 C( D
解决方法:' T& _; u6 g& U1 @* N% f- G% V! {
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:6 y6 t+ }9 Z& n4 T, {: [( }" h0 ~2 p
+ l' { I, A) A3 H" f
SecAuditLogDirMode 0777
- _7 K1 T& F. P+ e0 \, ]SecAuditLogFileMode 05505 E, h" V9 f, r% x! R
SecAuditLogStorageDir /var/log/modsecurity
# q/ F) M$ ?" p, ^( t2 G" q. @SecAuditLogType Concurrent
7 c' F( E- h$ o( P" r! D参考文章:
) z" I$ U5 T8 B5 o! S& @4 z! Dhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX; Q( C: T8 D1 B" V: f* S3 A* g
http://drops.wooyun.org/tips/2614 |
|