|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。 c. S8 R8 U4 n. W: m
# t1 P" ]7 b9 k
一.准备工作
' \0 O8 M; l( J$ Q. Q
! n# a& }, F7 m3 [系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
) n E! x: ~, `' S7 H4 u; H$ ?. d1 p- E* C4 c7 k* W' Y9 z
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz4 r! [ P! c6 e7 Q8 J/ @
5 A* i) W0 {/ T: x l
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
1 O: v# F2 H. n1 {* _. |+ X' Y0 ?1 c+ ]; P7 {
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs$ W/ V# @; A% a: L, r/ U
7 V. g2 O Y( t2 [
依赖关系:5 A2 j, R# h* u" R8 o. N* d: |
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:$ F1 t# i; r6 l9 N0 A1 q6 X) W
, z2 U3 f l, N0 Q
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel9 f7 d. s* |# ^( d
modsecurty依赖的包:pcre httpd-devel libxml2 apr4 T0 W. F% {, x
# o' U! [# e3 x2 M! v
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel) x5 s6 F+ |- ]. L- f, U4 e# f
二.启用standalone模块并编译! g' f/ C' o: r3 ?4 E3 t
5 k2 P) ^. ?% N/ P下载modsecurity for nginx 解压,进入解压后目录执行:: e* _5 q0 P% d" W
; s T% q G# q7 _./autogen.sh% `: H' ]4 W; r0 g6 L
./configure --enable-standalone-module --disable-mlogc& J3 D. P$ _" n8 S# d
make - s# c' ^5 I. m# y& B9 r
三.nginx添加modsecurity模块5 l9 T! M5 q' s5 ?6 {
$ n1 l# J) |' Z% }, n/ v) }
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
7 D! @: j* z, q- I# {0 D. g$ y1 v+ ]2 ^! t
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
8 f2 z$ v2 ~% e8 u2 U5 h$ R7 P# Fmake && make install
8 \# M4 d1 r6 u* W四.添加规则
4 c1 Z7 A; z) D# s" Q* ]7 H5 B* e/ ]( l0 w) B9 G+ M0 P! P2 d
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。 d& ?4 V0 ]; ]1 g
6 x* ?- Y* V7 S! R) u7 C2 C: S1.下载OWASP规则:% `) E( {6 A, l/ |
6 A2 ~+ M) W; `( ?5 i
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
* T4 G0 S7 e( H' O: y: p
0 ]$ I% {% ]- Y' ]3 l, Rmv owasp-modsecurity-crs /opt/tengine/conf// s/ s6 }1 ]" v8 n9 `- ?1 ~
1 z, Z) }, {. e, P3 L3 C( jcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
4 z, V3 i2 g% d* S6 R( l T2.启用OWASP规则:
, C' P$ [/ C* w0 V' z7 R+ p( A9 {8 W9 ?& H
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 q2 B) |1 ~/ T; \ n( X. L
, B- ~ u. L, _8 n编辑modsecurity.conf 文件,将SecRuleEngine设置为 on' h9 U. ^2 t' z2 u* V
* S9 t0 i. m9 i" v8 ]owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
1 T/ |; {% a- G* x
6 b1 \2 K4 o6 v/ pInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf* h- c S% ?9 t$ F0 w. K9 Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf* N* z* K- Y9 E7 t( w( E s! s! \* H
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf! o- J& ?% i$ f. }
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
. E1 C7 z0 \. aInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
3 A& c3 Z* b( HInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf" Z% E$ J' w/ d! a+ m2 l- b
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf" X+ c9 C. C" Q. L; c( z" S
五.配置nginx: T, C% }2 I. ]) V1 L
+ @% Q- q$ D( I+ I. z在需要启用modsecurity的主机的location下面加入下面两行即可:* W* _) g7 K! `; |! G
- G( ]5 v9 b- m7 Z; Z! K7 F! CModSecurityEnabled on; , J( R) L, `0 A9 b# L0 z9 B
ModSecurityConfig modsecurity.conf;0 E( Y; K+ q$ |5 W/ ~; a
下面是两个示例配置,php虚拟主机:
4 T& A0 r# V( ?* s/ z1 k; e+ y3 r' P4 k- t! E
server {
' O: J2 c, i1 F. s listen 80;9 D- V# ?8 K2 X
server_name 52os.net www.52os.net;
6 }. F8 |; { ^ k* l/ d, q
% N; _. ~# P7 _; u @/ R9 t. B location ~ \.php$ {
9 R* c1 X# @0 m; n; O1 Q3 c/ } ModSecurityEnabled on;
# b' {8 E: x) Q9 O% \/ P ModSecurityConfig modsecurity.conf;: P6 v2 u: P% @( {3 ~$ M% |3 R
8 K! g+ O) d8 h P( k root /web/wordpress;
/ |3 {. U4 b# P& f index index.php index.html index.htm;* T4 W" Y( \4 I" g, y
/ a2 _* B/ {2 |+ p/ Q m5 u
fastcgi_pass 127.0.0.1:9000;
9 B. d, |) b. w. \6 M# c! ^8 ] fastcgi_index index.php;
: n% j Y8 h) r k. @0 _. o( } fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;+ ^ b. p: i5 i
include fastcgi_params;
& ]4 q' Q* r ?; C, I& b- C" I% q }
" g- C3 T3 R: o( ^ _' t }
6 h/ y& U+ z. G& y! {* Vupstream负载均衡:
, P U! P4 X$ }4 |4 I; p- }1 U/ e; } C' \6 ~
upstream 52os.net { T3 x5 a8 s, a0 ^0 S$ j( H. t
server 192.168.1.100:8080;' Q, [) }, h' f# q2 b( ]) }# Y9 f
server 192.168.1.101:8080 backup;) v" T: z! y2 n5 v
}5 @1 T% [" l( r4 s5 P- n
$ H# u( V/ N* D2 j1 l4 }
server { i- ~3 r& ^$ C2 ~' p$ W
listen 80;% d. v+ h# j$ r+ O( |
server_name 52os.net www.52os.net;+ M1 l$ \; Q D6 b, ^3 ^. N
- d. {5 y0 M! Mlocation / {* E; q, B( w9 `6 Z8 T- m; D
ModSecurityEnabled on; 9 r$ W4 ?, }$ O# G: P# y, K
ModSecurityConfig modsecurity.conf;
: a- P5 |# G8 L, ^$ B
/ F; U) z0 @/ k proxy_pass http://online;2 Q" p* c- x& F) C4 Z0 R3 U
proxy_redirect off;
9 }! a+ C9 k+ x% \# ?$ |, x" m$ F+ T proxy_set_header Host $host;
/ ^& d9 V* y/ s3 ]& D( g3 h( A proxy_set_header X-Real-IP $remote_addr;9 Y D6 s, B. ^+ |% f( O
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
8 }4 M" P! o) T- Q }
Q+ z$ P; P- ~4 L}
2 J) U& N; b- j. j六.测试$ A$ d! O+ w7 m. d7 Q1 ~
- Q* J+ a( d9 E# h4 P/ z我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:4 K6 G1 Z& s5 [" Q% A U
$ e- q1 _3 _/ f3 N
<?php4 s6 z3 Z# B( q# Z
phpinfo();
( k& A3 G( p2 n& V?>
" c4 o6 C# Z y/ U在浏览器中访问:
, ]1 y# |3 O, S8 e. H- S1 r& W- M8 E. `5 n9 r4 j
http://www.52os.net/phpinfo.php?id=1 正常显示。
* D! b4 b* z& U) Rhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。3 f0 o. l5 [! x
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。3 f1 e, b. y! D6 M& W. B8 {5 ~
说明sql注入和xss已经被过滤了3 F4 D7 m9 I! h% ]2 t/ R) _
1 N+ h/ f# }, s1 C) a
七、安装过程中排错
1 R( ^* s" g0 D+ N* m+ S; k% u& C7 u# O
1.缺少APXS会报错
% x2 n/ _ i; [0 @; x y
6 Z+ k2 d) ?; ^( H# D* ~' ~configure: looking for Apache module support via DSO through APXS
2 o8 P: g5 u9 j6 K& v2 Yconfigure: error: couldn't find APXS
8 X3 J( T3 \9 g8 n% Lapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。/ v; p$ W, t/ ]6 F) \
解决方法:( G1 Z$ k4 s+ ]! N- b- W; U
+ ^. a& u j) A: z# s/ [yum install httpd-devel
* E- |" [7 o1 H6 Y) l2.没有pcre
# u' \ Y* A0 y8 X/ Z6 E
! H5 V( o$ t/ y. ~5 f% xconfigure: *** pcre library not found.# [ N# p+ z. {& a" n# g# l
configure: error: pcre library is required
- I z3 j# X; f- m8 }5 j解决方法:
' m1 N9 u1 P" }- A. x$ \
* ~, v- ? G2 M/ N W! L: m- N1 z syum install pcre pcre-devel
) g' }9 H. H I/ z8 I3.没有libxml2
1 T: g! I- e" C5 S; }+ N* B2 N/ {$ w% _- o
# I9 \2 q3 L3 p6 w# p; k+ `configure: *** xml library not found.9 a. b. X0 Q5 o# E
configure: error: libxml2 is required/ i9 z8 ^- }+ X( Z1 ?
解决方法:% N* N6 f3 `/ ` m6 L" A
; b! ]' P& E) j/ E
yum install libxml2 libxml2-devel4 ?5 O; ^& J- {" [4 G$ a' J
4.执行 /opt/tengine/sbin/nginx -m 时有警告
+ P& @' B$ x, D& [: o0 A
7 e! H( W$ n) R# }; u5 ?Tengine version: Tengine/2.1.0 (nginx/1.6.2)
$ g( N U( l1 a9 [# ^/ N9 s, T* u+ K8 Mnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
# U5 }% b' F' \( Z3 |原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log% }( F$ U( y! y9 l
2 `# `, ]/ z! J+ B$ f: }7 {- T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.5 k# d) G. Q8 g3 o+ n8 O7 X
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
5 `' d( H5 t' B8 O% M! N- U. t2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
# S9 Q5 A3 V' M0 C8 V [5 Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
/ X3 ~2 a/ P \9 {- ?$ Y% v1 a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"9 C# O2 r# r$ v0 k
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.! [! B6 q: Z# b+ V3 k
解决方法,移除低版本的APR (1.3.9)" ~. s' x; N& P" M' [! y6 G
! s" b c) z( H6 w& `# Nyum remove apr' J. d) ] }1 k
5.Error.log中有: Audit log: Failed to lock global mutex
0 s6 L( g2 P( W, `/ ]/ X9 `/ U0 @
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 9 S0 K% H4 }( q0 Q
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ }2 V! f' |$ w1 G" N; s3 T解决方法:5 ?& V7 w% |8 c
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:/ l; ], w: T4 [8 a
. r# ~( f J+ _, Z: j9 ^+ nSecAuditLogDirMode 0777. n9 l7 S( E0 q. a ?
SecAuditLogFileMode 0550
$ }$ N: ?, `, v8 @2 u9 c4 H3 ]SecAuditLogStorageDir /var/log/modsecurity6 f6 q+ W0 S+ `& u
SecAuditLogType Concurrent# u, r4 C( p( F5 L0 F/ o& S6 X
参考文章:5 v7 A8 p" b5 S) J
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
# H6 g* V& M) Dhttp://drops.wooyun.org/tips/2614 |
|