|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
, p; q; Y, d/ G& d! s/ V- C u
. ?7 A: ^* @1 i3 p* d/ z' Z A一.准备工作
K1 v4 M2 C7 }) W4 b/ s
' Y/ w7 T% ]3 K2 Y5 \ J+ z系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.08 A% }4 p' W4 Y# c. ?
( T7 @5 ]4 d" F$ ctengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz) m, i! {/ X' y/ {' J4 p
8 U# z% s* v0 M' b0 B6 L& t% s0 A3 Rmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 r1 u. n! R# [2 q9 K7 H( X1 H& E
% n: `0 Y+ W2 h9 {# N$ E$ w. QOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs% H* R0 a9 e0 F! F+ ` B+ i# J! b; D
+ {, E! Y# N4 L. h- n' a
依赖关系:' f+ w9 I8 W: Y" k! F; [" z
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:& d0 b/ c8 ~) U) k+ k
3 g% A# Q# ~% [3 v" myum install zlib zlib-devel openssl openssl-devel pcre pcre-devel- m$ C( F& p7 x" ]5 r6 `
modsecurty依赖的包:pcre httpd-devel libxml2 apr
0 M |, b2 d9 D W- U. J! R( R& D( W: k
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
# o4 t Y$ o7 |0 z# E4 j二.启用standalone模块并编译) \& D8 f. H- ^* k0 k' `; s
. q+ K7 j) t9 L1 w$ o. A
下载modsecurity for nginx 解压,进入解压后目录执行:# i, a$ I# ~" H& S! e0 t9 W* F
; w2 j7 L4 c7 l- y' _5 x
./autogen.sh9 i$ u8 G& I7 h7 M) G7 [* @
./configure --enable-standalone-module --disable-mlogc7 ], i9 N$ F1 Q/ I
make
0 K* A" [# \7 d) w& d三.nginx添加modsecurity模块" G8 n" }+ T3 d- }. D
N! d m2 U8 K/ i
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:. q& M1 ~7 A% H
% W) s( S2 r1 |* F3 Y" f m5 g" I./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
; c* ~# v# S: R8 R4 n+ A# `make && make install
& d( q' p/ H6 Z3 q6 r/ m4 N四.添加规则$ [; d1 {% A0 }' B
7 V5 I1 l. I: L3 C( x5 emodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。& y: D. S7 ~' V" @' ?! g6 O5 t
h, Y+ z" w% F& A; c8 q7 a
1.下载OWASP规则:
, R/ ?1 ~1 Z7 v; ], k/ ?
! g3 g. P( H9 \3 x: [4 ?2 hgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs9 B$ ?+ b- G- W1 b$ b s
1 o/ U$ O7 l' L" v9 Dmv owasp-modsecurity-crs /opt/tengine/conf/
9 v/ a3 Y# ]2 @. V& m
4 ^+ b" u0 {3 g n+ u; A; acd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
: A% u' h7 w+ y2.启用OWASP规则:' J! I% n9 `9 o- }( s6 W
a4 U) {; I1 q" x1 Y$ }
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
' I& D0 f/ u4 N3 C, h! \ x9 @8 e' e! N
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
; ~) R) r) O5 b. O0 s
. g1 _+ \& o: z; F) F% zowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
2 \4 }8 ]6 u5 P
; M2 X) @* |2 e2 M1 E _ s0 Q' @/ oInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf. @# c8 y. m9 Y6 v8 D, z R n0 P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
?) f. r/ c# q: V6 ~' G9 KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf/ G" w& H( z5 y, a
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf- C: I" j8 D" X7 J4 o# x$ m0 Y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
" `! U) X' l4 {# d* n# Y7 M3 Q5 c1 TInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf' E1 g7 S% p* y3 d J3 {
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
9 h1 w6 Q* p3 {& U五.配置nginx
/ ?2 g8 Q+ F( R: x3 o$ Y( m$ J: W
- Y0 u" f8 {8 Y3 T4 }, c. Q在需要启用modsecurity的主机的location下面加入下面两行即可:) H% C* L! p( J- T! D
- [1 q) k1 s- Q7 NModSecurityEnabled on; , m, }- j0 P1 f, {
ModSecurityConfig modsecurity.conf;
( @* E, }7 I6 t) H8 ]3 U下面是两个示例配置,php虚拟主机:
9 C' j! a. L- a3 O1 b/ I/ `/ B2 R
server {
$ ?9 l- i8 ~7 U0 c9 s listen 80;
# |. E H+ ?# \% d. I, J( X9 V server_name 52os.net www.52os.net;
& a, M* ?$ n; m
3 w' m+ w9 r2 s: V) Y location ~ \.php$ {& m4 X. F% U" P5 t0 B$ _
ModSecurityEnabled on;
0 w& F2 R) Y3 j6 j# Y0 l" f" K ModSecurityConfig modsecurity.conf;
: W6 v9 G# X( M! ^
/ d6 A& x0 p) C9 L; x1 [ root /web/wordpress;
% j, w$ U1 r6 ^: P: y) @/ T: X index index.php index.html index.htm;
6 G) h4 _5 a Y6 @# |3 _2 V # y/ s6 p' e( G& o5 a
fastcgi_pass 127.0.0.1:9000;6 [$ @* t6 p, A+ ^! i
fastcgi_index index.php;/ u% W2 g! q& l) K- K7 c
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
6 @& N6 U, d5 f' ]& f% h0 {+ F3 q include fastcgi_params;
/ s5 o+ X! V' F, _' g; d }
2 o8 l& L @4 Q8 j }+ ^" a S% J: k; T0 t
upstream负载均衡:2 {/ ?# l; t! F2 U6 |7 [- D& L
. b1 ?; o( d; H( ^# J4 vupstream 52os.net {7 x: J" S0 h, b7 r7 Q
server 192.168.1.100:8080;
: s) t( Z# B0 h/ x4 H server 192.168.1.101:8080 backup;
& ?' w5 A8 F- t1 b4 B% {}
8 _) I& h4 e1 K
8 F! q" [* C2 |4 Cserver {
4 K- L% u1 A2 n/ u& R; A" i, `( Alisten 80;& k. K' ?2 y W6 ^5 k7 o8 }/ S' d- i
server_name 52os.net www.52os.net;, F* \1 L4 P, w" B( }8 U3 A
$ X2 |$ S! D4 w( K" A5 o2 |3 ~location / {. @ R8 [% k. d: ?( {3 S9 v
ModSecurityEnabled on;
0 ~( a0 V" N3 V- [- K+ W* K ModSecurityConfig modsecurity.conf; * D# a4 g& U4 k* Z6 Y7 ~! a* T
( t1 X# u: C$ T0 e proxy_pass http://online; c$ \" s1 x9 K0 N, y0 @. I
proxy_redirect off;
2 l$ f/ D7 m" g4 s- p( W% \ proxy_set_header Host $host;
9 V5 Z" {9 v( ` proxy_set_header X-Real-IP $remote_addr;9 V& ?+ _2 f( ^7 V& M: F v
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
! A: h, O: k+ r0 q' q8 `) a }0 y. g; Z3 E4 d; R( H! q# A
}
8 D, ]" g6 N3 n2 Y& s8 F六.测试0 T; g0 S% C& q. D+ f
" ?; W( A0 x1 a我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
. K) y" h v! ?+ {- d
$ l. U( k+ t" r- i<?php% C$ }2 j8 Z( g
phpinfo();
6 z1 G9 s9 P; Y: l H?>
8 Z" `2 |. @2 O% X S9 d5 ?" k* I6 v9 `在浏览器中访问:
. h% L. |: r4 H9 D, |& Y0 Z3 x7 [% Q! C7 v. D* b% o7 y5 U6 Q1 z
http://www.52os.net/phpinfo.php?id=1 正常显示。
, \" m4 z1 q# X4 p7 E7 y* Zhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
% ^! S+ U2 D" _2 K: D& s |- H1 uhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
g z( ~- f+ z说明sql注入和xss已经被过滤了6 H4 Z. t& n1 n1 n7 ^
: Z: @( T' P5 i- A6 V, Q0 m: v七、安装过程中排错
0 r6 X U7 ^5 e9 J. _) J* u
5 m4 V- \0 t D; V8 L+ Y1.缺少APXS会报错
, |0 A1 Z6 I! y5 ~$ i
: N5 l' F6 E6 q. x) Vconfigure: looking for Apache module support via DSO through APXS
9 f- I7 k- D9 _7 y& l) `! Dconfigure: error: couldn't find APXS0 ^: u8 x8 S, z" A3 D, \* X( j
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
( A# [9 ^' L* p8 |7 o5 V解决方法:, o: V8 M* z7 G m) Y; J& c+ V
2 U9 \# \% O* v$ O' y7 A9 I; j
yum install httpd-devel
* s( [. [9 j7 m2.没有pcre; V) G5 W& b3 ~ m; E) z" u6 _9 r, E
, h. @* ?' b# |3 Jconfigure: *** pcre library not found.; G9 D4 w, Z# v$ d6 Q, ]; I
configure: error: pcre library is required
/ P0 k' e r! X3 d) O3 d4 m# |解决方法:1 I7 S! ]" S+ o8 `5 D* ^
4 P. g2 n) V+ e* N% }4 u
yum install pcre pcre-devel
& }3 H& z3 K$ V- e8 p3.没有libxml2
& W5 C$ ]) i: A- P; O) _' n% Q* l, y( e6 u
7 X& ?& R( m- G0 x
configure: *** xml library not found.
/ s$ B- D% @6 hconfigure: error: libxml2 is required) h+ r" b c$ t- o( X2 F! h* G$ K1 Q
解决方法:
' Y% q7 C8 M* O7 J8 `2 C
3 Y# O, G! p9 q. H1 r2 }yum install libxml2 libxml2-devel
& l% R- B- ^/ A- d3 U- w4 ]% d4.执行 /opt/tengine/sbin/nginx -m 时有警告
/ E, Z: O- j6 |4 w+ p* y- G* z! {7 a3 v
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
2 o: K6 X% U0 \; B: J5 wnginx: [warn] ModSecurity: Loaded APR do not match with compiled!+ B; |6 A- P5 u- r0 q9 a
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
( T0 I6 e! c1 D2 G9 o' ?* @* L t+ @8 d$ t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.3 ?3 L, r2 O$ w* t$ L$ a
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
: X, I" t U: p2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!6 f; K% E/ k0 R" D2 n# h: g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
: H+ ]; y8 G$ N2 @% }' P: s2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"1 ^1 R c' T& l8 o5 u" {) m2 G+ ^$ e( \
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
J+ D2 B* `( ?% w解决方法,移除低版本的APR (1.3.9)" D0 ~. s9 t3 R# K0 Q& G& O
6 q k% F8 w) Cyum remove apr
7 k @6 w+ ?% c4 ?& t5.Error.log中有: Audit log: Failed to lock global mutex: R) x' S3 ?' n. s! Z
$ ?. R2 X7 X5 S* U8 |
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock E6 z; X7 \, i7 K' o
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
# k, e% G+ x4 f+ m; _% B( g( _解决方法:
7 J; [5 d7 H/ v: y! }; {编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:" @0 A3 z( R+ n; P$ x9 W+ F, |
# Q* V* h* S4 t; S+ z- c6 ]8 QSecAuditLogDirMode 0777
1 x) U. ?& S1 p! K; O7 C" F# ^SecAuditLogFileMode 05509 ^# N1 l( R0 `* z @
SecAuditLogStorageDir /var/log/modsecurity
2 _+ _; m& e d5 aSecAuditLogType Concurrent4 k3 [8 ?* S8 A$ j9 |' r/ i
参考文章:
3 {/ N T, b! C n# Ahttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
8 h$ F+ j( w4 x1 Q, Whttp://drops.wooyun.org/tips/2614 |
|