|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
* B/ t; d" T. X( I1 l6 c
0 I# a$ _0 n. c9 I; L一.准备工作
4 G. w+ |" G+ d$ h( C. I7 X$ A! j) V4 s( |# }% A* p6 O
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
3 L4 u) A- x i/ w
/ Q6 f" {9 i) btengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz: Z- g" @6 R3 p* s0 A( q
+ I* E" |5 s& P7 Z1 z$ q" ^modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz6 K( j, }0 b" S8 P0 h
$ H+ d2 L# B- {9 B8 v# X$ TOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs3 i: Z7 r% ?1 ^+ @$ ]4 z
3 J2 @" j& j/ \/ c
依赖关系:( _. `$ d- v$ N: T
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
) Z1 y7 x8 t3 y# I' x4 I# @
- \( x7 W# p0 V: \1 {. U5 Ryum install zlib zlib-devel openssl openssl-devel pcre pcre-devel" r' t. u* V: f3 v
modsecurty依赖的包:pcre httpd-devel libxml2 apr
k: E c0 C! g& D6 C# _7 o: b4 }2 `8 G! n) `: c
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
# n; b3 R/ s! U% Z& p二.启用standalone模块并编译( F! n* s9 o+ o S5 F
* {; s7 ?& T- {( r. f
下载modsecurity for nginx 解压,进入解压后目录执行:
* p3 p# |8 r3 \& @ |6 N( O4 }
% N" ?6 X0 j" Q" g. B0 a, }./autogen.sh
9 w: `. Q7 `# F% Z6 t./configure --enable-standalone-module --disable-mlogc
5 ^6 `+ S3 S+ nmake
0 q- U! A* R: \4 T( c& y三.nginx添加modsecurity模块
* |7 o: j. E0 f6 w. X+ G( ^" n% n4 W7 ?7 j6 s6 O
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
$ R$ h0 H+ e ]* f2 y* m9 i" N- P5 I8 n1 E
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine- y$ B, j- g- Z/ \* p
make && make install
9 {. u1 n* j* r2 ?四.添加规则) \+ B0 T$ r& n2 R6 I6 j
$ m* A3 u. P9 d* C5 O. ]modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。( q8 R1 }8 ?$ q4 L) H7 E, D' ?& P- r
7 F! j9 {. z: V( }6 _, M" D
1.下载OWASP规则:$ W4 \/ x1 G5 E5 g4 E) p+ `' I
5 ^& ^( R% ?5 w3 |# u
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
6 U& V5 \& k, p9 i5 w0 G; E. D) V
. n6 A: I; _! ^' ?mv owasp-modsecurity-crs /opt/tengine/conf/2 e/ V" | H$ s* \( [2 u; o" o, d
9 ~8 _9 x2 ~8 r4 X, H
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf0 S2 h5 B: R* j5 b
2.启用OWASP规则:
% E* h, s6 ^4 a- r$ O' j8 I D- q* b$ H: m& ?& |
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。) o7 T7 d" a' x% \2 }3 i P/ ]
5 j$ W+ _# n5 U" }9 u2 s% w/ D
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# U0 J6 \, \4 d9 w% |5 J3 I6 o F
6 V0 ]6 L% ~ P8 ], iowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
6 \8 q6 B' i1 x( L$ k
6 f2 x" s% ]! p. A$ `8 nInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf6 K; M% S( ^8 ~' W3 s
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
3 A2 p- S7 p2 I) X8 CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
' n) s. f0 ^+ ~+ s. H. `* f( BInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf4 c: U, V+ ^# y- p8 t
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf6 E E: N+ ^, K% }; o
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf% j" Q4 G$ x, c3 _' l
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
) D* E6 J P4 R6 y/ p五.配置nginx
) d; a; K; P: O ]' r5 [5 n P5 W: F! w$ U
在需要启用modsecurity的主机的location下面加入下面两行即可:! T) q. n q' g+ s k( a' S# G
8 P! E0 V: Y, i' Z a5 X' ]
ModSecurityEnabled on; 3 |! f5 D/ Y9 @$ s% ~9 ?, p& @
ModSecurityConfig modsecurity.conf;
, c) I! J# D. P8 W# Q/ b; h下面是两个示例配置,php虚拟主机:
8 V, N. q* |7 D8 a3 p, T1 W& T4 V7 f/ Q) ~- ~
server {3 ^8 w! n& Y' X$ \" `
listen 80;3 p9 E3 ?4 N8 _' G
server_name 52os.net www.52os.net;+ s% @8 _- x$ m0 T
4 y5 U$ m6 H7 d- B+ G location ~ \.php$ {- @. @. p: t& ]
ModSecurityEnabled on; / f! X% `, r; ]
ModSecurityConfig modsecurity.conf;
% L$ H" E/ J$ p' F/ t/ k8 @8 M% M Q, o' c$ _
root /web/wordpress;
: e% Q# t. O5 j( ?3 Y( h' f7 B index index.php index.html index.htm;1 V# q0 s/ \5 e1 z0 l/ R
! u/ j( ?; V& a5 V9 f1 J) l
fastcgi_pass 127.0.0.1:9000;
" R+ v( H1 T" d3 T' r fastcgi_index index.php;, H: x2 W& E8 h
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;! w; z. Y' l8 u4 d( H2 k
include fastcgi_params;
8 p# ~3 l6 ` Q. E }( d: e1 m. v$ p8 k/ _
}
1 @* q. r7 C( [" e/ ?upstream负载均衡:& z3 V7 ~3 X( P) G4 ?2 @4 z
' \* @- [" R4 a) w5 O: I9 ~ v
upstream 52os.net {
: C0 s* i/ _* D8 U1 `# H server 192.168.1.100:8080;$ z3 G8 t& ?7 k0 u2 G5 E$ j. f
server 192.168.1.101:8080 backup;
2 d: P4 G0 b' Y$ J% m0 H: q}
7 q& n/ v: R, m- U7 k
3 b8 r6 g S0 k Cserver {8 u0 O, i% K: u1 a% {( @
listen 80;2 v& r9 n& w. ~, K) U
server_name 52os.net www.52os.net;& d6 s* P g( h' R" f
5 N6 V8 U0 I9 N% Ulocation / { Q" r$ |0 ~5 C" D$ d9 e
ModSecurityEnabled on; ' s; @- z) Y& j/ ]$ ^
ModSecurityConfig modsecurity.conf; 5 ~& ?8 c: s) G* h8 W# f
0 s. T2 W l: `5 m
proxy_pass http://online;: G2 K* m* K7 t9 Y z
proxy_redirect off;
* t& Q: b) d$ N+ R# p5 |! G" } proxy_set_header Host $host;- u0 ?( f& F# ^5 a( u0 B" ^+ y( T
proxy_set_header X-Real-IP $remote_addr;. p: I; g" a2 J4 L0 i6 E6 ], f
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' K& u; L# m9 |, ]4 ?* U3 T
}5 F) d- Q) Y, \9 s0 }
}
& j) l7 f6 M: I0 Y6 R六.测试5 x G' ~+ t. X# R
4 a+ N! ?8 Y/ c& ^* S我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:. E4 b. f' c: D; E+ s8 c& v
1 c8 a8 X, p: r! G: i* t+ M. ]; F<?php
$ a: r; i0 c. U phpinfo();
, M2 {, ~' \) k! w7 s?>5 ?4 X' z0 I6 G6 Q0 ?
在浏览器中访问:# }) }* Z. R) g: l
' _- J, c# m2 Z. m- j
http://www.52os.net/phpinfo.php?id=1 正常显示。& e" Q V0 s1 F% R. i2 N2 L, x; z
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。! V0 a7 v/ T! s6 C2 l
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
/ i" @3 R5 j2 F说明sql注入和xss已经被过滤了# I* X# j7 I8 ]
6 r, c7 }- g0 M5 m; a; D3 z
七、安装过程中排错
: c- ]) a8 a n9 j& B0 r; E) {) V# w. h
1.缺少APXS会报错
8 R3 k( K0 h/ d' b# d8 B
0 |5 w4 P3 H2 p* B8 K. Rconfigure: looking for Apache module support via DSO through APXS
# |" p% @ W3 M. lconfigure: error: couldn't find APXS9 W. m, b9 l3 V; V1 u
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
$ |5 @& m8 F1 R9 \ U/ \解决方法:
* w# T, ]2 G# S2 D; ]- b; z, b
$ f% U: z8 @1 Z5 }* Hyum install httpd-devel/ U! I0 O. ?3 w- e
2.没有pcre
$ |7 j* f5 i8 Q3 H$ V( z+ T9 W7 W# Z" t% ~+ I
configure: *** pcre library not found.
* w" n# P; |! w- o5 hconfigure: error: pcre library is required' y# a8 @; q9 i7 t& e" e
解决方法:( Z6 |& A- Q: c4 o
6 F: t- p% X4 m6 g, B( t2 X
yum install pcre pcre-devel# K4 B. \. X9 Z2 M8 o: {9 ]
3.没有libxml24 ~3 |# r$ S: b
8 F9 j0 ]8 n+ ^: b4 k
: w0 q) g; o1 v- v( Fconfigure: *** xml library not found.
6 P+ h( y- M; z9 D# s% I5 U6 ~configure: error: libxml2 is required# a. x e5 t% |+ G! R0 }+ Q3 y$ P
解决方法:
3 v+ U* a' I% _" A; [1 u7 v. r( h, E0 E' U3 v! M' F( K% _
yum install libxml2 libxml2-devel( R& l- X7 f4 d) b' u# t$ `! h
4.执行 /opt/tengine/sbin/nginx -m 时有警告% G- v+ e) X& C3 X6 |3 v; E
! x6 S8 D' u& j$ r: Y7 TTengine version: Tengine/2.1.0 (nginx/1.6.2)+ d3 h3 v9 B3 R
nginx: [warn] ModSecurity: Loaded APR do not match with compiled! w- U% ~1 S- R* Z
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
& E( v7 R) k$ u, p0 n( }; l; W1 Y% i: j% z; p" U3 k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
5 g1 o: G0 m8 P) G7 ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"1 c ]) Q n5 F; B) B( w
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!( n1 I5 t! n9 Q9 d8 e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"5 r6 Q, j* o% y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"& k) ]$ o9 H, g( b4 ]% q- q
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.) T4 |# E& Q; w2 G$ ^8 u: p7 Q
解决方法,移除低版本的APR (1.3.9)
6 Q6 B- L! s) g4 I% y6 I- _9 z U g% o$ o
yum remove apr& r* ~- V7 `" F C' x1 O
5.Error.log中有: Audit log: Failed to lock global mutex
" h1 q7 o$ R, o0 S% Z; g2 w) S/ {- T
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
x/ n4 s' j! A; P) Q' ^) p' g! Nglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]7 V- p! G' U- W- u
解决方法:
! h4 e) q0 K4 Z/ e% D1 l& K编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 c: Z/ F" f$ [( ^
" D9 }$ q6 i: R6 U" \$ |+ |
SecAuditLogDirMode 0777
' a, s' d q$ p7 d. dSecAuditLogFileMode 05506 z! w; G/ N2 j" ~
SecAuditLogStorageDir /var/log/modsecurity) S+ t% C+ p# h3 U
SecAuditLogType Concurrent! P( Q2 L! T, \+ b' S
参考文章:: z5 z+ a% v! X5 G3 A5 Y/ Q8 y
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX) C5 D7 O1 L1 R$ m7 g5 q
http://drops.wooyun.org/tips/2614 |
|