|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
* S4 K9 b2 c* u0 d3 C0 M
3 Q- H- t0 W& |; l一.准备工作
9 `; \* u3 y' _0 I7 s! z. x7 f! n1 j2 M
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.04 R$ F: H8 j6 B2 U
5 V8 j5 O j3 Y2 |) H' b2 N5 Z2 |" }5 w( b
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
4 L/ {/ l) N% G6 q- b; w" G; L8 J, j/ O8 C" \$ |6 ~! s; Z
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
5 Y7 t) L7 B# j* Z1 ?
5 Z N k7 ~% }7 x& ROWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs- f$ a# q1 n9 Z& M7 X% \4 K# E
" a" n: b0 N( h. C9 g4 B依赖关系: _, B! u& B" W8 \& O) s4 j
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
. r( r; Y6 L) M$ q4 k& O1 w
1 d( ]8 `; _3 ]% ^8 q8 a( ~/ pyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
8 G, E' \9 }# x; E* w- \modsecurty依赖的包:pcre httpd-devel libxml2 apr
* R% M1 {! p( H0 K7 l9 K
1 J8 I" P I* U6 X$ K1 Q% q. ryum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
% w3 H# Y3 i3 T7 V! m: o二.启用standalone模块并编译5 d8 D! l( r! e3 [" ^5 `8 `: }% _
( O, ~+ m7 U/ v7 w! g) _+ }下载modsecurity for nginx 解压,进入解压后目录执行:/ R% ~# U6 o( ]( N0 W) N k9 j
' `! _1 J5 R0 }& q./autogen.sh
' m. O$ B+ U* \0 t/ x7 a6 k; Q, m./configure --enable-standalone-module --disable-mlogc7 T D1 n# ?! [
make * @, E/ {0 D4 t+ o( V; `
三.nginx添加modsecurity模块 b1 J4 v- h6 U. h
) @, ]: O$ M2 p! x- X
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
4 a2 h1 q, @4 r; q
3 D6 w5 V2 U$ `; G./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
$ K F' T, z$ M3 H. E7 W `make && make install
! i. b8 N4 ?, P4 q2 |四.添加规则
/ \) R- g+ Y! g I8 r; Q+ r' m6 ? ~0 X( W) z. y& B2 s# I
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。7 z. V5 X6 _* {7 ^
! ?' v4 Y- g3 K! s6 t8 v* i0 [1.下载OWASP规则:
+ r7 u$ y$ {' R |9 w* @: y0 s" w T: a3 Z, f. [$ N+ e( o7 i& Z& a
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs. L* l5 G" p, B5 }% l/ |% r
6 J$ z5 p% ? g& s+ D
mv owasp-modsecurity-crs /opt/tengine/conf/3 e7 V8 C: E' w$ O4 l; e
' n! r3 K2 m% t( C* Qcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf. [) p9 K' l# d; [
2.启用OWASP规则:
" N& I* q2 G! {: A5 W
4 y ?, }, t, D5 E8 t, p+ u复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。# R/ i8 H7 y- B3 P& s0 i
, F6 D4 X. L# \/ m+ G( V; k$ Y9 t
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
) i6 a* a% t/ o% x7 L- S3 Z4 q+ L$ Z! `# Z6 }; z- J' c7 f5 X4 i
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 c; U# c; |" S* n" R
& y4 G% `2 [( S8 U+ S! R# i, M3 fInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
/ U* ?# T2 Y1 u' d) c. SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf' v9 w q& f$ K, n2 W$ Z7 f/ r. r
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
: f y) f4 h3 p( u, v/ I( _Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
2 _4 s% P3 y( _Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! C( L1 S: ~7 R% B! j7 P$ WInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
, w9 a% C. I& k2 ] m0 ~) _Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf( L0 Z* j% w+ \9 O
五.配置nginx
; J2 W8 \7 e6 ]6 @* G3 y% D
* d5 U8 z+ A8 S在需要启用modsecurity的主机的location下面加入下面两行即可:
# \3 C; o) ~5 w
+ d* \; r; d( pModSecurityEnabled on; ; h. K c) L, f0 ]
ModSecurityConfig modsecurity.conf;2 R: T W2 c. a. \
下面是两个示例配置,php虚拟主机:9 K1 z O1 }9 p3 I& ?+ |8 ]
( M a, g/ c" |
server {
6 \' K S8 P8 e! t7 _ listen 80;
7 H6 F! @# n+ d& M' s server_name 52os.net www.52os.net;% S F y/ h! A$ K. i
0 q& U K1 h. L& N3 | location ~ \.php$ {
5 o" R! n9 D T6 @! G/ e) O6 Y ModSecurityEnabled on; + Y& Y* |0 C2 R" z Y L
ModSecurityConfig modsecurity.conf;$ P0 ~" d" i9 ]# T3 f3 s
1 i1 J1 G* v# u7 `" q" e' }
root /web/wordpress;1 V9 U9 B4 ~: P: i d
index index.php index.html index.htm;6 \+ \3 t2 i( j5 H6 {* n& g/ D" \8 v
5 o3 d1 A% K3 n7 b0 F, d( u1 ? fastcgi_pass 127.0.0.1:9000;
5 V% U/ v8 n" s% h fastcgi_index index.php;
6 i: P `( Y g3 c' z fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;5 }; q- G7 a8 ?; T& T' N
include fastcgi_params;1 }( U$ n L3 r. n4 ^
}2 I4 Y( N7 v/ V; I, J* R0 F) C+ j
}
. Q5 \% a, T+ \" k# Eupstream负载均衡:) L/ r% v: G# _7 ^) o: ^
1 z8 d Y6 O' e; d4 k: uupstream 52os.net {
: D4 X8 c! _# }6 ^* K9 Q server 192.168.1.100:8080;9 k: d9 [9 x3 M* N) g- ]" ~
server 192.168.1.101:8080 backup;% ~0 m$ y2 ]5 u6 p+ ^
}
6 |* s6 d) R, V+ C( b* \" ]- r0 z3 r; S
server {
: M8 [" C' t& Alisten 80;6 Y8 i* _9 g+ e) ?, [
server_name 52os.net www.52os.net;
' B+ Q4 \2 o. N5 i5 `% N: T% X- v* b ~' c2 t9 \7 ~1 w
location / {
" ^6 f; H: Q/ W( |! K9 L" g+ | ModSecurityEnabled on;
* g* W+ ]. x% p ModSecurityConfig modsecurity.conf;
) i+ |: H6 f3 z- }
" l& p2 Y" y+ g7 E; {4 {$ H5 Y3 K7 b proxy_pass http://online;
+ m& O2 } E4 n0 W7 Q proxy_redirect off;: }5 K6 E! J6 F( u5 x2 [: ~
proxy_set_header Host $host;
+ _# e2 a7 p6 ^' { proxy_set_header X-Real-IP $remote_addr;
! ~- f0 w& a; g! h, W" p; c proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
/ i- w/ h- w" x }
! D: M0 e! ], H& `% Z' n}& B/ l/ \8 W d0 U
六.测试
. [% u# P. q, x( P6 v, [7 {0 q. V3 M* ~
4 f6 I' H! r' S5 z我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:/ h* G5 X. V, h# i9 p
3 i/ K8 c8 ?; @$ I* u' V<?php
, W7 d8 ~5 z N& v; p) | phpinfo(); . W' D( U; R% C- E
?>7 `8 g3 v6 }: [0 k2 \; o
在浏览器中访问:
3 _0 f& l% N3 ]$ `' S+ s. W7 g/ A- D& d4 J4 D+ i3 J
http://www.52os.net/phpinfo.php?id=1 正常显示。
: T- T# k* E. Q7 ^! t# k F2 ~9 Ohttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
# w! W% P* z* y/ v& {http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
$ [; q8 g# O4 X X3 w) b说明sql注入和xss已经被过滤了$ R& p3 x2 D' \3 \3 q
4 ~2 f8 U. R/ m, R
七、安装过程中排错
8 `2 @+ d6 W& p; y0 g' Y2 ?+ d3 L& D! U5 ~$ c' V2 C( E7 i/ W
1.缺少APXS会报错3 `4 J+ u* E' C! y
5 O) n( R/ i2 {2 K0 L. ^
configure: looking for Apache module support via DSO through APXS
" @2 d2 i% E [" ~5 s5 h0 Hconfigure: error: couldn't find APXS1 z- }3 W8 Y" f$ N
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
$ S! ^! O" t) }; e; \解决方法:
3 K; z4 F g1 V: ~
$ H9 D' E( C7 H2 s) {yum install httpd-devel
, a Z: ?$ O& c8 n2.没有pcre
u2 J; u2 r x5 t& d L
9 |( v. R5 X+ O, iconfigure: *** pcre library not found.
9 |2 a6 g; w6 y g% ~6 tconfigure: error: pcre library is required
! P( v( W$ w6 f5 t解决方法:
& v, L. P( @+ J! H u+ T+ ?
0 M( P9 |' I" h6 x( \yum install pcre pcre-devel
+ x! |. P. s9 z2 C3.没有libxml2
; O! X. m1 C( k. \3 M( o, |$ a Z% y) \7 _! o0 J( t
3 I7 ^5 `: q' B. Z6 e8 bconfigure: *** xml library not found.3 X. g% |' M5 h. t+ f9 i* J
configure: error: libxml2 is required
2 I* c5 V. i. G$ O" U* l解决方法:1 n, C' I4 O/ w7 i4 {
+ `- U- I& r4 h2 S+ _( c0 z" y$ A% k
yum install libxml2 libxml2-devel
$ `. \; T2 B- f0 Y0 |- Y6 w4.执行 /opt/tengine/sbin/nginx -m 时有警告0 S d5 A' _' d p
( |% q8 `* c+ S4 Q+ m/ E
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
, l/ U- R* J1 r" w' Nnginx: [warn] ModSecurity: Loaded APR do not match with compiled!3 M' M1 w) k' b9 i" @* o+ e. v1 @
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log, r- d4 g. g* G& h. d0 I" A/ o
$ K6 H: p1 m2 u6 T* u( r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
" R3 e* T4 M7 t" d2 y3 C) ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
/ m0 C; c6 @+ W2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
5 p3 ?$ W; j! e# n. u9 \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
* Q* ]" b" t, Z6 m( q4 B. r! J. {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
' _! U# E( G [$ x+ B2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 |2 _- F5 j8 j4 H. h( W. L
解决方法,移除低版本的APR (1.3.9)* B+ N3 s3 Q! s: W
. T5 ?1 n4 A/ I" S. v% N* R
yum remove apr% b3 L% N( u! k/ L
5.Error.log中有: Audit log: Failed to lock global mutex
7 M8 _9 t, \. v" c& o1 y) I3 Q, I: V7 |
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
! I3 H( ^" n" g1 I; k; P8 Jglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
- x' p' S8 ~2 y6 X解决方法:
$ N1 u2 I# N3 V3 N/ }编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:" |3 T; f. T" X+ x! q' D6 ?% A
" `. b* l7 N! T: c. m L. m1 TSecAuditLogDirMode 0777
& H, K) ^! z: [SecAuditLogFileMode 05501 ]7 q5 _9 s% ]' F6 K
SecAuditLogStorageDir /var/log/modsecurity
* i' W, Y" N7 K9 I& vSecAuditLogType Concurrent) ~: G4 r, H# K. Y
参考文章:- j4 E' C( d2 w) \4 O/ u0 v( b) |
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
8 v* J# {1 X3 y7 Y' p1 fhttp://drops.wooyun.org/tips/2614 |
|