|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。8 [, |! C) x; D9 q3 B/ F" W" [, z5 e
1 l' }+ l" j# c# Y8 y) a7 n一.准备工作" X! k0 r) X9 A, Y& L' o* D
( j9 Z* J2 Q2 v
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0* M# b2 R- L4 G/ Q/ J2 _
c" x9 s4 k7 @+ m! B1 w
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
. E2 y: }9 L6 n, D- Y" v( b$ t2 w* \) h& J m0 J, Y; u. B T
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
0 t* m7 W) U* y% I0 {
, h3 K4 m( y+ O3 UOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs! |% z7 F3 B) { u
$ L& h8 r9 S. \# p3 M4 J依赖关系:3 f& B( r- N' Y( E4 a/ D1 f' R' ]
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:$ F N4 b# _4 \' [$ V
/ k: Z' G. }$ |* F& w6 nyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
$ W6 a. b& L. Ymodsecurty依赖的包:pcre httpd-devel libxml2 apr3 t8 W) v3 L, b, ^
; m' U. K p9 vyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel7 H" L8 s0 v1 z: a% H$ }2 @3 P" N
二.启用standalone模块并编译
0 H8 {+ G1 ?4 d$ [, ~) c: _5 _" c4 t; |1 m7 `- g* j* S
下载modsecurity for nginx 解压,进入解压后目录执行:0 V7 N# y; g8 e \ S/ H E: P
* a3 Y1 ^+ Z* p7 M( Y$ `5 h/ P, v./autogen.sh t& C2 S8 n2 C# O6 j2 l
./configure --enable-standalone-module --disable-mlogc: b4 x9 g" g7 t e) { ?$ g0 r, P4 f
make
$ r; R: \. @0 Y5 B7 L2 N2 G三.nginx添加modsecurity模块
) t' E. U* T. t7 n5 ^4 y0 q
0 J/ _- s6 Z: q1 c在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:. F3 c+ O5 Q7 E. I
) E% v2 _* |7 f0 g( f# w' }
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
' c, v/ G: `9 {' R9 ?* Q5 Omake && make install
6 D8 ]( _5 a% R2 {# b! c四.添加规则& A8 o+ G. B) F! {
$ b( o0 ~ P. d& N
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。, j) Q( R* A# \( a
! o: N2 _9 F: C: x5 n, E
1.下载OWASP规则:
" d' v4 h1 P, k# T8 f, I0 {' C6 Z& |0 j& [& z- H* b
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
6 g" M" C* s: K0 y o0 H
& x" H- [3 t8 K7 d% {$ Gmv owasp-modsecurity-crs /opt/tengine/conf/
. v# Q" o; y e! x: M; I( R6 n" D- C+ T; K5 ~
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
+ C& S& ]9 c# J2.启用OWASP规则:
) ? X# t, S- t% \) {9 u; E) j9 G% y$ w$ S$ t" |" H/ I- q
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. u" E: P) e# Z. `- L
( {: a6 m2 Z! I8 K: t编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 ?) p$ L+ G* g5 p }
% r6 X1 [' D5 C- Fowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, c- z/ H6 s! E, r- g3 P% Z0 T
, v( ~/ M1 }5 K4 {4 M' o( P) {! h
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf, f& \: v# C% c) | O* D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
s, b9 }, a) z4 v. w6 | J0 IInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 L8 y( k+ D' ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
5 \/ E, F: ` N5 D; a! v+ dInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf9 F3 o$ f/ p5 t" N2 i5 ?2 y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
! o. D7 c7 R: a5 iInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
* x3 W8 v- n O' h! z T五.配置nginx
# j" O7 T; X& U- q) i' S5 H7 z5 b8 Y, J# |. @* m/ Y1 N
在需要启用modsecurity的主机的location下面加入下面两行即可:% K1 P5 [2 ^( l" X/ m7 e" S9 k
0 k4 b2 \- v' Z- _* XModSecurityEnabled on; * S7 X9 k5 ^6 ?. d6 E( d
ModSecurityConfig modsecurity.conf;
* @' x E9 X* u% j下面是两个示例配置,php虚拟主机:
7 P7 m, |- y, |5 G. o
9 R7 j3 C8 l4 G$ s4 f8 _server {' {" E4 @5 o& A: h: } t- x
listen 80;: K& \3 d: R; z+ }
server_name 52os.net www.52os.net;
; S- y& Q- N l2 X- C7 \; ?
2 G. v( A& B' [. N8 {/ { location ~ \.php$ {& M% F! A7 Y- ^1 @, d4 b" b$ p3 T
ModSecurityEnabled on;
$ b* ]9 y" Q% h# p1 K ModSecurityConfig modsecurity.conf;
: |% P4 _ u& }- ] E K# t9 i% Y: P
root /web/wordpress;$ ~/ o- X' {. Q& j
index index.php index.html index.htm;$ [3 {- g; Z7 M$ a
, G% c( M! r% s& \) R w fastcgi_pass 127.0.0.1:9000;
) f1 ]) _) k4 u1 S8 z) r' o fastcgi_index index.php;
. j3 r. D, U" V fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
- l& `* j, F) A3 Q) f& l! _ include fastcgi_params;) h# R( R# B+ s) d" Z7 N
}2 H1 P$ }5 a/ y! A5 G
}
% C5 ? }: x2 p5 p; s* b) uupstream负载均衡:
; U4 a& d% S. y z1 q$ N
& v9 U4 T. p2 Q9 k! Nupstream 52os.net {
5 u6 y6 m" t+ q8 h' J( K& E: } server 192.168.1.100:8080;. n2 O2 h& g* r5 [& d
server 192.168.1.101:8080 backup;* K" j$ C8 S, x8 x% o: l
}8 r0 {5 e6 q! W: {$ j; u+ ^, _
% C7 v6 |# L8 Q0 ^6 k! Vserver {
. k U; s) f& N7 a6 glisten 80;. ~# ]6 ^, i. `' }0 d# i, V, {
server_name 52os.net www.52os.net;
9 M, T9 H6 S/ w" Q/ E! j
- k) F( d, C& i: ^, Plocation / {: t9 v! c; A$ v* [4 j5 \* e
ModSecurityEnabled on;
, A5 J) h7 a4 J7 i a ModSecurityConfig modsecurity.conf;
0 j4 s! D1 R6 s3 c* B4 }5 n3 x. m1 u
6 U$ `1 W9 X8 B& G proxy_pass http://online;4 q, d* e E6 n X H/ K) L5 w
proxy_redirect off;6 J5 b3 |; u6 W+ H9 R
proxy_set_header Host $host;
9 o: K- O1 ^& Y- a" n% k proxy_set_header X-Real-IP $remote_addr;( ] B3 Z: B1 P# d0 d# Y0 y
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;5 V9 n N6 r/ ?) A7 C; ?$ f7 Q
}/ Y; P+ l! B9 B/ @/ u; Q& z7 Z6 D
}
% m4 g' t/ X5 K* w7 P六.测试6 M; l/ L3 B4 k9 Y3 S: r/ H
5 o& u* v/ I* x, `1 U
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:! a9 U4 e! [- ?6 |2 \
5 h; P" f7 A( _6 J A0 X<?php
, [# |$ h) y7 F8 G" E* x phpinfo(); & `% h4 G4 K' C7 Z! ?8 c
?>
4 Z" D/ m5 u" V5 J5 k; u6 C在浏览器中访问:- x1 }* a* C2 X- |& f$ W8 F+ |* _
. U, n O! L% K% V" I \, e
http://www.52os.net/phpinfo.php?id=1 正常显示。 Y( v! n8 i1 @) z% W* m
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。* n1 c' q# h F( W
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
: s- o X3 @" A, V说明sql注入和xss已经被过滤了
' p3 n9 s( R. g4 `1 }7 u' M/ C& n3 f
七、安装过程中排错, [% a3 Y [) G% E4 C/ o4 E, n
% `; p' j; H& h. m8 A4 M
1.缺少APXS会报错
5 j- E6 }% l7 Q! X& n4 o! x' U7 x6 W4 b* {8 S Z
configure: looking for Apache module support via DSO through APXS
. Y5 @! L" I$ O$ Vconfigure: error: couldn't find APXS
3 _2 O! w3 ]; Uapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。6 L& h {( Q* T9 K9 [. [( ?
解决方法:! I+ `' ^4 [- e1 Y
6 `4 t9 b# p: d$ W! Dyum install httpd-devel2 b6 ]0 N' X- A8 J' B1 Q
2.没有pcre
' Y% s9 C+ l- _. ^' w
; _% }; W2 a$ G. F3 Wconfigure: *** pcre library not found.
! d" J0 Q! M' k% u) Z/ jconfigure: error: pcre library is required
8 E8 ~* u: u7 U/ j8 L; f# w解决方法:) S+ [$ E# {( K/ U$ v' L
_: b- `8 L. _; w3 j. J7 t
yum install pcre pcre-devel
& b0 F+ l. U! M9 ^0 ^3.没有libxml2
! G. {' W4 |6 a) l; E
% Z/ Q, g( p$ L0 l- Y2 S+ [! |: x8 \ a
configure: *** xml library not found.- c( s& A! h8 a7 {# K
configure: error: libxml2 is required
6 |) _9 J* Y* F% D5 L9 P; E# ]* w" F解决方法:
3 \7 O9 l0 m( ~/ @9 ?1 f" |( b5 N0 p# s! R; h) _
yum install libxml2 libxml2-devel, N: @" c6 b. n" H( p
4.执行 /opt/tengine/sbin/nginx -m 时有警告
% E e! M" x/ `. D( Z9 ^1 \5 \- o
. L( \4 Q; p" o3 [" }+ s3 ATengine version: Tengine/2.1.0 (nginx/1.6.2)
" l( ^' ~1 m0 A3 Q' nnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
t" S6 T9 P5 R% m* D/ S原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log* b$ }' e5 ^$ _! h; o. z" t" f
, F: K$ h1 D( A+ j6 r0 m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.' g: a) I' a! `( E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
# v+ e. D2 N, S2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
1 p* g! d( Y0 s9 G2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05". R; D9 W$ q9 W- O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
6 V7 z8 |" }$ }$ M/ v1 }2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
; c! n/ I) p- v/ Y3 p7 f5 D解决方法,移除低版本的APR (1.3.9)2 o1 o: X, q. m7 q
4 S% C5 v3 c# S+ k7 n @' L6 z# ~1 v6 Pyum remove apr
, @% J: {4 C. c7 }& W! O5.Error.log中有: Audit log: Failed to lock global mutex9 x! f4 F/ f5 q }% U
: s! a! J- f3 L- Q/ f4 |
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock - X" C3 s7 p4 l# g& @
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]" X" K T B, V* s% i9 e
解决方法:
* r1 X* Z; V* k编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:, y$ E8 n7 Q% g; _2 t0 o7 m
5 p& Q6 L$ `* g# ^* |9 o3 k" p9 J& g
SecAuditLogDirMode 0777& [4 [7 r' {1 v$ S9 B' r& N
SecAuditLogFileMode 0550; A9 W" x1 v. l; o5 y8 X
SecAuditLogStorageDir /var/log/modsecurity: F: c$ i/ n5 j5 g) d0 D. X
SecAuditLogType Concurrent
. u; a; O! A8 O; G m参考文章:! ?, @. b: s3 _$ W/ x
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX. _# T/ P! _) E
http://drops.wooyun.org/tips/2614 |
|