|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。- ~( ^; T" @& v- Q- r
9 y. M, Q- S" s6 G3 I
一.准备工作9 d6 \/ e: ~' t. U( @
8 q( o8 d+ J7 h6 J: n K系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
. H4 S* S( V* r( [( b$ n9 n! G
$ P: h7 F1 T z6 d& p0 Qtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz' s4 ~; x$ _' l8 M( F
: m% L t8 n3 A3 T; A3 p) Rmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
. u0 ~4 R; v) F+ R) t; W$ s
1 Q o% k0 Q2 J1 p6 kOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
5 a9 [: S- {% A O& u. |) b% L' S( Z
依赖关系:* j6 Q1 ^: A/ J' |* i3 U
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:( R, A/ H" _5 Y6 w( Y, N
6 g6 s3 B! D% [+ k3 Tyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
* T8 o" R3 m tmodsecurty依赖的包:pcre httpd-devel libxml2 apr* Y/ |+ n# L0 J' _& _# @7 [
; L" `9 h7 {- {% q* { b3 u, l+ c7 ]
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel# ?9 M2 L" K+ m7 K/ u
二.启用standalone模块并编译
Z3 l W; V5 |9 i3 \5 g' s% R6 Z; d' _/ e/ [5 ], z9 F
下载modsecurity for nginx 解压,进入解压后目录执行:. E9 E; t; L+ z2 `4 y1 I1 I
5 Q$ b9 g3 ~( }* \0 D7 V3 W./autogen.sh
; V, ]' E: ~! J./configure --enable-standalone-module --disable-mlogc& k, l" \& C7 R5 g2 Z' i
make ! V' ?& b8 h; i
三.nginx添加modsecurity模块
2 Z% \1 s b* U( [! N" c5 a) O
! W: Q$ X8 t/ N7 D3 c. |在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:! C2 ^! i. p: z# s c
# a* y \ m* G$ q
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
8 S* B- N2 g6 ]; Gmake && make install( x( U# i; h) A8 F0 S& b* i
四.添加规则
, P2 c9 ?. k5 Z/ i/ w- K2 G8 E
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
/ r) o5 R, z0 o! V) T% o% M1 V @2 ~7 K' e7 c5 Z" s
1.下载OWASP规则:
) R0 z' Q2 M3 u" t6 f/ E$ Z- U4 l
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs: F+ z+ |7 A; o" z8 O2 d
& \/ W; P0 C. B- T* j' C2 ~mv owasp-modsecurity-crs /opt/tengine/conf/* H8 ?3 n( v, o: T# f, t
& _" k/ V1 s# i! {7 O- O4 h- _$ h
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf7 q3 ^% x, D6 n! w2 t7 |$ ]
2.启用OWASP规则:/ n7 B0 B% x0 V& a0 k r
# K5 t% D+ u4 Y4 _, k: h复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. B( G& N) q& Y5 D; V! i) p2 Z- U6 F+ J" k( u* M. G' _
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on& i# U& s' e0 y" q/ q9 U* ?
( y _5 z% M7 ~4 A% l. ^8 [$ M% [
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
6 Y- F) b' e! o8 o2 O9 j4 N# v4 V" _
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
/ Z. S, @) Y& G6 r" FInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf& `5 ]0 r% R- k3 A- k: O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf% H4 c& B& a1 W/ K3 S( v* _
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf X) E* k6 g* c! \. r
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf. h: }( I! N6 ?, o/ I
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
6 k7 r" g6 O8 u) HInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
; e5 p( `( G1 E五.配置nginx
; h7 h8 C* q* n0 K; c$ Q# w. |! K
# j5 I6 \9 M2 w9 i- h: ?. |: F在需要启用modsecurity的主机的location下面加入下面两行即可:; ]2 ~3 d' \! e# d7 c5 E& _
4 }8 v; A! V& V: R) i' M; ^8 _" aModSecurityEnabled on;
8 I* |& n3 [+ N2 P" m: `ModSecurityConfig modsecurity.conf;$ ^0 j1 y0 u% @4 j
下面是两个示例配置,php虚拟主机:) F( Q: {* H; d' J. }6 z: R3 A7 n
; D8 _9 u% b5 e U
server {$ k- d Z. _" c1 Y; S
listen 80;" L& C- J* `+ n+ h
server_name 52os.net www.52os.net;
" G; |. k' b) Z2 i . `( y) u' v% v' v
location ~ \.php$ {- S1 v* _0 i6 }2 ^$ V8 q+ i
ModSecurityEnabled on;
r6 n. x+ a9 Z# D: e8 r8 U ModSecurityConfig modsecurity.conf;/ v' H' z: ^6 b- w( X" L) I
+ \& t* ]# @3 {0 j root /web/wordpress;
+ |9 L' o# D4 C- }( k index index.php index.html index.htm;
* K$ H4 G6 x4 I% I7 u ; G! T) p0 P) j6 h+ e2 K# N
fastcgi_pass 127.0.0.1:9000;
% e7 w/ w; K: {0 }, c& T: G @ fastcgi_index index.php;: }, Y% w' d% i* e1 f8 e
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
4 S6 M. H v# \2 l include fastcgi_params;
/ F" i5 H1 ?" g8 c) s }
9 f/ R J* L- m# h, o- U6 e }- `% U9 Z+ ]' q5 B! t
upstream负载均衡:
3 K+ K7 b+ A6 f1 k8 B3 @ r' R" y# v* H# g
upstream 52os.net {5 @1 y7 Q0 N- x8 K
server 192.168.1.100:8080;
. q' \0 [% p; U0 E9 U/ b server 192.168.1.101:8080 backup;
; `8 v1 ]5 q R- | _8 Z}* C# q; K4 }! b5 t
4 V9 R/ `( m( [. e) F& ?, Rserver {- i- A* o- }& x
listen 80;
, S6 Q \5 g& p$ t: N+ hserver_name 52os.net www.52os.net;
! g% t( x D7 Y& A' y% s. C* O& }7 L/ M- |6 G' Z: `1 w. F" F
location / {
5 z& d _6 A0 x( |+ ~ ModSecurityEnabled on; \$ Q3 s' H+ }
ModSecurityConfig modsecurity.conf;
4 g% S8 A% z5 J: [2 x$ `1 i4 l- l
" C1 Z6 N1 D0 J- ? @$ m proxy_pass http://online;* a' \8 h( e9 w
proxy_redirect off;
+ l7 W0 i* ], ^ proxy_set_header Host $host;
6 [0 V% m- k- E+ f- V# i j Y5 _- M( P proxy_set_header X-Real-IP $remote_addr;% Y5 v t: Y7 R# w& e
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;) i0 l, P, c9 p/ ^+ t) r8 s
}; v q4 K# |- n' o R9 P) U
}
( }/ ~2 b$ D2 Q$ T6 `. j六.测试
" x2 t) r! L1 U( I4 y( P& ?5 q8 F4 ]& \! g; D x9 N0 P7 q! G- Y* u
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
9 Z+ v7 y1 w, s3 q
. u2 Y. i5 e9 O5 V1 |0 u2 n h9 n<?php
: I/ o/ ?7 W t Z: a phpinfo();
0 n4 n0 e3 q( v% n?>$ G! L" ~; B$ j- A( S# M4 O
在浏览器中访问:
! P* f1 c' t4 d) m7 b% l" n# g# v- _- o2 l m6 o. e% ]
http://www.52os.net/phpinfo.php?id=1 正常显示。
: t- R" X! j# |) w. N, j' o8 nhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
. e2 M' B8 T: f4 Jhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。% z8 D* m6 c, t3 ?) f
说明sql注入和xss已经被过滤了
$ `' e# Z) {$ l" k9 P; K, u+ p
- M( V! u: F; ~0 I% E七、安装过程中排错5 R) V! a7 R' v' D, P
& a z) s2 y' y& Z# }( r8 k% q
1.缺少APXS会报错
- f. x' k& `% T5 [2 d% w0 f0 m$ P4 U7 x, W+ L6 e9 H. X& ^
configure: looking for Apache module support via DSO through APXS
, C+ K1 S" N6 f Q8 Hconfigure: error: couldn't find APXS/ f3 o" {" @: z1 f: l6 y8 z+ @8 m2 N
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
3 Z1 `# L6 w1 _, e" k% S. Q解决方法:
4 S) W( q* ~( A2 R+ v0 ~" Y) U' n, k! p9 u/ N3 T; I4 k; N O
yum install httpd-devel* B$ D. Z6 S, Q
2.没有pcre p9 o6 w. _1 C4 X
' D6 L9 N I& ^( ~( F- p3 i
configure: *** pcre library not found.
& \4 v& ?' r0 _4 u" {) C# Jconfigure: error: pcre library is required% p6 M' f2 ?# a+ [
解决方法:
l8 u: e6 j. X6 v. M: W" Z% I5 o* j! A- |2 {& n
yum install pcre pcre-devel& i# Q9 C* z4 W1 y9 l; M
3.没有libxml2
8 u& i* f& \3 u( p, d) E
9 V2 v1 P: p' {; J) a, {" @7 U6 V3 u3 l- s0 H
configure: *** xml library not found.
3 {, r+ d- z% Z' w( Z }1 Rconfigure: error: libxml2 is required
. H9 @9 q7 M$ h6 G" A! t' R解决方法:
- P! p# C! k2 R: P$ R& N7 L" H% }& M
. n* q# D L! J" e! C. C/ {9 \yum install libxml2 libxml2-devel
/ K& |1 M7 n+ N c+ ^ x, E/ W" j8 u2 L4.执行 /opt/tengine/sbin/nginx -m 时有警告
+ ?+ N/ d7 ?2 ^5 ?8 }: x, o8 d7 N9 @( S l Q
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
( L/ F1 r, l% Rnginx: [warn] ModSecurity: Loaded APR do not match with compiled!$ Y- y- B- S" ~4 `
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log* O, \, v" l6 L3 t9 U- Z
2 q9 a3 q. ]% w- d- n5 i6 i' b
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.1 v6 d3 l. F1 \3 r9 O6 Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9" D6 ^! _0 }2 z
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!" q) K: A' i7 j8 n x2 Q4 r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
" B1 e- H# x' H: {1 i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"- v0 ]7 c- X, t
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.+ S" R: B- H5 w3 o2 ~
解决方法,移除低版本的APR (1.3.9)
7 z& w( l, T* f/ y; w) [6 ^
& E$ b. V6 F h4 p' h; {. tyum remove apr" w1 G6 g6 V7 Q0 k( B
5.Error.log中有: Audit log: Failed to lock global mutex% a& p( W- ?9 H8 p9 z2 a
0 [+ i% q1 W) C2 _; C2 ]# g$ Z2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ! e3 V7 s H/ e, p$ G$ I i
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
2 l K0 l7 ]! k7 [+ i1 `解决方法:3 M( t5 Y, D ?; A& n& m) i
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:! g3 K A$ t% w8 C1 N) B7 T
! a/ B& \3 k' ~SecAuditLogDirMode 0777
8 g* b- P4 Q' k! M' Y7 k+ f( R5 y' tSecAuditLogFileMode 0550
1 T# ?+ k4 @0 B. W& E7 Z+ VSecAuditLogStorageDir /var/log/modsecurity
; V* Q; l w. s0 BSecAuditLogType Concurrent& _! F0 Q2 b' M, F
参考文章:
* Z5 Y8 S2 d3 w4 u. Q: \8 i4 nhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX0 j9 X/ K$ N/ ]
http://drops.wooyun.org/tips/2614 |
|