|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。# ^0 f: d$ S, T7 Y
/ X/ I' A" e+ \. m; k) u一.准备工作
0 r8 [( S) m& d! _5 j
& F6 B0 [/ G- [ ? q' A系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0% K4 ]4 c3 E5 k3 C$ K; J7 h. K4 T3 M
' I* B* i& D# ]9 r
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz& Q- l0 I$ k$ u1 H0 {) e$ ]! H _
: _& I6 E4 B) Q8 k9 s" M Kmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz* {& {% K2 @/ l' R5 B
+ h% r8 L- v) O# h' l# I% r% r
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
( H3 X6 e, ~4 n S
- S, m% m1 g- |2 j依赖关系:3 h7 B) `- v ]& g! p
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
* Z2 F4 O( U1 M& q1 F' R0 O4 X' o( S* q2 l4 x- g
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
' a+ C3 i) X/ J; J6 O, J4 T zmodsecurty依赖的包:pcre httpd-devel libxml2 apr
4 ^& U$ f6 D& v1 f3 |' O4 S5 p1 W
. w ]7 ?1 s. i y2 Cyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
9 d3 y2 \/ h/ X0 @- x% v" C二.启用standalone模块并编译. w" Y; `* v3 u# ^' g3 i
: y9 U( T5 z Q. h下载modsecurity for nginx 解压,进入解压后目录执行:
* X4 e1 R# H- P& e( M6 M
" x% a7 e' N- V$ W4 c9 V! f./autogen.sh- F, s; }( t6 O* I1 V# d* D
./configure --enable-standalone-module --disable-mlogc4 x8 N9 K# }1 ]) q
make
' z/ k* w0 W1 r0 T; @三.nginx添加modsecurity模块
$ y6 _! N4 k+ T/ x+ P/ z5 V( Q$ `/ s
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
1 ]7 d2 v4 O/ T. \6 S$ i" Q/ y9 s4 k7 a' o% C6 }3 v2 D# f
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine! x/ Q# |+ n' W P8 Z
make && make install
/ Z3 d! _: I$ c, {" L8 ?$ h四.添加规则
/ z, y& \8 j, C' \# r; e; X+ J8 N" m3 v1 s5 K1 n6 k+ v
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
+ R5 M( {, n$ V8 n' E! g! _: n7 a8 m# l# t* j7 l, n
1.下载OWASP规则:* v2 \8 Z6 m- C
# b2 t$ h: C! T7 sgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
2 v- c. z3 [& L2 T' b) e7 Z; j- M1 S, d2 S4 ?
mv owasp-modsecurity-crs /opt/tengine/conf/- f4 S$ F* m: s# P5 j7 D
$ b$ t$ `% T7 v( Rcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
9 _' z/ F% C/ v6 n2.启用OWASP规则:
1 z s) y+ j0 c4 l% x: p4 z' `, s8 ^+ s' g
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* J& t' ?% P" L, |8 N' _7 b, u0 i
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( N/ s, B8 i8 A' i ~4 ^
/ y: W7 f5 L1 {; u( bowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
$ a4 W; E- M# {5 ]5 V0 M. l5 @/ a, ^6 E$ X
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
; N4 s# l" s, {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf- e7 z! H# _: [* H6 \# n
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 b. y& H+ h( E7 f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
3 p r5 f$ f+ ]Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf" Y4 ^# y) o+ d. r' t/ i
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
3 [. X: O' V4 t* p. CInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf m& _6 f+ r+ a# @- }( L# C" L: m
五.配置nginx. k* l q/ g6 Q+ m; b, a# `( r/ C
. R& C5 j1 R5 x# X
在需要启用modsecurity的主机的location下面加入下面两行即可:; p G% X8 ^9 K9 a& B
" R2 ?4 c3 n# T& ^ModSecurityEnabled on;
2 `8 W9 e) H5 i) WModSecurityConfig modsecurity.conf;
* f4 i _+ }+ l下面是两个示例配置,php虚拟主机:
4 _) H4 h7 F2 J3 _; G. K; ~, ?2 V6 g Q) o& u) }9 _1 e1 f6 i( I. r
server {
1 {5 j+ Q6 M7 ~' p8 g- I8 u, O; @ listen 80;
$ i3 B' ]3 j* R% Z& y: Q6 n server_name 52os.net www.52os.net;
7 t! E; @$ ]5 u 9 |; G4 w) @" Y1 v& D' a
location ~ \.php$ {
1 j3 |7 g6 d1 b- w4 J6 H D8 \ ModSecurityEnabled on;
5 T8 t6 s5 j& I( z0 p4 D ModSecurityConfig modsecurity.conf;
. S: F- A+ c0 [3 ]. y7 `% t/ l& ?# C8 V. E! v* d# d+ |; T
root /web/wordpress;
( u6 p ?; }* _' N4 T index index.php index.html index.htm;8 F, \! q0 x& i6 w, f6 s0 s- s
- v& E; t" C% T( S8 ~
fastcgi_pass 127.0.0.1:9000;
- a a* A0 W$ |: ] fastcgi_index index.php;
' S# ?' c/ Q8 ~9 h( { fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;8 M$ l* z- i/ [+ ?- v5 d
include fastcgi_params; x; u7 {% C1 m5 `' Q( i) k5 k6 A* V
}
% s% z. T# l# z! k. L }+ u2 m3 G. Y1 O7 l
upstream负载均衡:
. G; M S/ h1 j1 M2 v
: Z1 \- n1 }3 @5 h5 f4 n' rupstream 52os.net {& p/ p5 D: \7 h6 X& B; i$ g
server 192.168.1.100:8080;
/ |. e0 _4 h* O ~- b6 ~, g# L/ T L$ g server 192.168.1.101:8080 backup;. B0 l3 o# b g3 A- G# K
}
' Z) W5 a) I' q0 A* y
: \! ^ m8 s% [# y- }. f* S9 Xserver {
& f, ]0 z3 `$ B' U9 k: L! f8 K1 Blisten 80;* y) A# o6 ^2 u/ ` q& o" X: J5 e% Y
server_name 52os.net www.52os.net;6 {; ?/ m5 k* U3 j8 |; @: ]
$ k& H/ l6 Y7 T1 Z2 glocation / {3 r9 i% B+ U7 P' }' S2 f
ModSecurityEnabled on; 3 B& l0 i/ @, L- Z! h s+ D
ModSecurityConfig modsecurity.conf;
/ w8 p/ y7 ^, P* m
) C$ C0 @# z) C9 L' F) @8 ^ proxy_pass http://online;# A5 ]2 v- P# b% |* k6 ~& S
proxy_redirect off;" }2 v1 L8 @" [5 ]9 K, v6 `
proxy_set_header Host $host;1 D2 D, r) u$ p4 w" C
proxy_set_header X-Real-IP $remote_addr;+ j; Z2 o; ^' D
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
4 R7 {/ t: ^! { }5 k# ~# _: Z' K( H2 F- Z
}
; K5 @3 |6 l. M: z) L' n六.测试7 P. f) Z, T+ m1 h0 r$ U: k
: V- t; ~- ]8 I我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
# S9 E% N8 n3 v3 u
7 w f Y% m; z9 I# h<?php4 \, l; j+ L) z
phpinfo(); + w/ ^% Y- n/ L4 T' X/ n
?>
; ^$ a( l; M& X/ Q在浏览器中访问:+ f% y+ `4 j7 l) n: N
6 ?, t! L( b4 X, dhttp://www.52os.net/phpinfo.php?id=1 正常显示。
+ m, A) S2 q+ r; }& ]http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
/ I( u+ r0 e0 k( P0 @& f5 F rhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
: Q" y2 D+ j) Y% _说明sql注入和xss已经被过滤了9 f$ b6 e# f% O( z6 Q& S6 l
. l7 t( P" j! g" a$ J七、安装过程中排错
' h: t2 X0 G7 ]: t7 ]# g7 `$ z7 J* `
1.缺少APXS会报错2 u5 V- Y$ J4 j3 {. |& ~7 i
! T k o& I, e% n5 A
configure: looking for Apache module support via DSO through APXS
4 g1 o7 l: E9 oconfigure: error: couldn't find APXS
& Z4 @8 r: G6 f3 Q# Bapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
" g/ n- Q2 y( I8 S* z) v5 b解决方法:/ g: s+ q& q$ S- q- I5 X0 V; c
% z% Z1 [8 x. W5 e( J$ }# d) Syum install httpd-devel) u3 @4 c, x" N. {3 a5 O3 o
2.没有pcre
/ u, s# M5 ]) _# k
% ?$ l/ i) F, o* D# a! ]4 dconfigure: *** pcre library not found.0 m& l- d" c s' E
configure: error: pcre library is required
6 y. Y3 t# L. l$ P解决方法:
! S2 u- b+ _$ v" S
' Y' D2 s4 K- z' vyum install pcre pcre-devel
M0 w% R+ k/ m7 c) C3.没有libxml2$ V) _+ y4 }: x( I( ^# Q, J! y
1 d5 X) Y$ C2 |
( x3 j/ I# s) r0 K
configure: *** xml library not found.
' f6 ^. x9 s. V Y! Jconfigure: error: libxml2 is required
* f: W2 w/ r( k5 h8 f解决方法:6 p$ D- C: W/ X6 X. y
, V3 {# l. q9 d% K) C3 _
yum install libxml2 libxml2-devel
' E- I. f+ w- F; M4.执行 /opt/tengine/sbin/nginx -m 时有警告" w6 R# F! S+ {( H8 k1 T4 V# d
" P% v2 O$ s# \2 w+ \+ _Tengine version: Tengine/2.1.0 (nginx/1.6.2)! \( [$ z5 Q. O ?$ ~" N
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
5 R7 E+ \; }3 Y- B原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log, U) ]9 U0 j; i" F1 G
) v" x; | V; x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
" y! K; S9 |6 H$ P* f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"/ s' L5 q6 Z/ V8 L# k4 Y/ S
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!7 w: d+ A$ L4 Y: y* z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
0 p) `4 D0 q4 R3 `- [4 m0 c4 d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
1 P& j8 I4 D& H% r9 _ B( x2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
5 E2 M+ z3 P, l$ {- u8 w! u解决方法,移除低版本的APR (1.3.9)+ t) [1 K; ]. R! @- z5 S O3 N1 \
" }4 p; o* B. ?, b [9 jyum remove apr
" x% `0 m- q+ d' g5.Error.log中有: Audit log: Failed to lock global mutex4 x8 Y: b( M8 [7 T, V. W
+ \& z( h' ]) @8 l7 V' [8 F
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock . U0 L9 z4 @) W+ [1 ~
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]8 Y, v! P) a8 d9 Q2 q% x; M# p
解决方法:1 C5 u- A3 z: O; G
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:, }: k. X- O; P/ }
! y7 U& [/ _! h+ E- Q, G- J
SecAuditLogDirMode 0777
- p; U4 z- k( q: R& y" f$ K/ }SecAuditLogFileMode 0550! Y- Y; K: q" d
SecAuditLogStorageDir /var/log/modsecurity% _/ h/ e# e0 c6 A
SecAuditLogType Concurrent
6 l3 _6 }, }: ]( b参考文章:
% r, {0 P. |8 a4 H$ y5 f5 Thttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
* j2 @% ]2 ?7 @) [- V- Rhttp://drops.wooyun.org/tips/2614 |
|