|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。$ a6 v) w% w. n: |3 h
' O; \0 U. F( S; M! X- I
一.准备工作! o0 L3 i9 n9 ?$ C% |. |
$ } \5 t9 Q% g* X6 K( k, I
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
! Y6 Y# _0 j* v" y* [
, e' Y$ m: L7 F+ B, D6 r4 Wtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz$ Z* r c; x x+ r
5 ?- T" m8 ?% u- n6 c* N# N) s
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
' s7 ^ T8 A3 F3 s# n) p( ?% X& o4 c1 |8 X; z6 `1 R/ R! ?
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
3 b- q: N0 X' A
+ x% G8 z( v, M2 `; T依赖关系:
- E( W; l1 F, J: Z! @; atengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ T0 ~+ L7 T' T1 n, `- | s8 v9 x6 d: E) m" D% x
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel# l9 J% W; n2 U
modsecurty依赖的包:pcre httpd-devel libxml2 apr
+ k: o+ R4 v4 n9 a+ J* P* w+ N# L" I' n x# P& o s& x9 o; o
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
% D3 _/ V8 p/ j# r( M' x二.启用standalone模块并编译
2 D' d$ Q* Y3 M- a* ~2 L& b
8 a3 k# c' L% x0 ]1 Z, C& b下载modsecurity for nginx 解压,进入解压后目录执行:
0 {' _5 H' p! e( M; f7 B" l0 w/ J5 t+ q3 b1 p- @. U) I, C0 o
./autogen.sh
* V9 p* S1 @# f) H+ ~/ u./configure --enable-standalone-module --disable-mlogc& s% H8 b5 J O
make . i2 @4 b X2 Z* w! d; ~
三.nginx添加modsecurity模块! d4 i: l" R9 Y, q/ A* g$ T
9 Z" g- ?6 ^, n# ]6 `在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:5 Z# I8 F0 K9 ^
: r2 b4 k4 k9 O3 S0 d( {* W' J' x- U
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine$ r) b: d" y$ [
make && make install
3 K' R" x' V# W, j3 |& P% {- H2 t8 a四.添加规则* V- a1 ^+ Q0 O
# c g- n4 s- _( f# P: A
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。" {4 \2 t- b' [" T" }, ]4 l5 e
7 {- Y* B+ W% n9 J" k- l1.下载OWASP规则:
+ E. G- K+ E6 C( F Z+ c
" g, u* P( m$ Z. r7 dgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs3 m& _) `2 k& R6 L. _2 J* ?8 V
8 m( \6 S2 z) L* d$ f' r5 y
mv owasp-modsecurity-crs /opt/tengine/conf/' a# }* H) Y, S( x* O/ U+ J+ v
8 G3 ]& L! e! \3 D0 I3 N( f
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
+ N9 b3 r U& J/ s2.启用OWASP规则:
) I' J: ?- l) R( O1 [0 i
) v x" D5 C+ L0 u' I- \5 V复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
# c# v! F% O4 [
* Y+ H2 @! [9 K: f n/ {( l编辑modsecurity.conf 文件,将SecRuleEngine设置为 on0 }7 ]8 S f9 P7 M
: U) k; X: q+ `' T1 V# o B0 zowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
" V: H$ ^2 M+ ^9 a1 J8 s
3 R/ G+ z: ~' h9 X& JInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
( v. o# \$ C1 P6 u7 |/ N( rInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf# t3 |! k/ I4 V! R
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf- a. X# T6 J/ i/ ~7 Q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf7 A9 ~5 s8 L8 y! v. r7 ^* M
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
$ @3 j' ]; X$ P0 s7 qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
3 d% ]2 s$ `3 c8 H% A" GInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
6 [7 v8 ~. w5 @' P五.配置nginx
+ k2 V6 Y9 L5 |) [ @8 i6 K% L+ B" I; D3 y! G" D! C$ Q* B
在需要启用modsecurity的主机的location下面加入下面两行即可:
2 N7 }( r* }8 i$ n, [% d4 f, b& z2 u/ y+ H
ModSecurityEnabled on;
) M5 k, U' v3 Y9 \+ w- p% W2 K3 l$ W& nModSecurityConfig modsecurity.conf;; r- K) a ]3 X: B& O
下面是两个示例配置,php虚拟主机:
5 O+ `# j& t2 t# t7 `( Z- h4 }9 L4 u
server {
- E* i) Q" ~ [7 j% R% Y% D listen 80;
1 X3 T1 z1 w* E& x( ^1 W ?2 E5 r server_name 52os.net www.52os.net;
% h; o2 W, r- N o 6 U1 P# y* r! |$ W( ?
location ~ \.php$ {/ ~' O) t6 H8 y, p) Z: p
ModSecurityEnabled on; 5 l, G8 C% y9 x( `& q- F; _
ModSecurityConfig modsecurity.conf;
3 Y0 d/ k# A+ V+ _
' @. w+ W. m* E0 D root /web/wordpress;
" a1 _- t, j( G( s! x4 J- x6 I index index.php index.html index.htm;% e; p* {4 @1 N5 p8 L" }9 @
+ R2 P; _( U2 J5 T0 M$ Z
fastcgi_pass 127.0.0.1:9000;0 p: R, B" x, t) C7 y# R5 }
fastcgi_index index.php;% Q6 ?8 o' d5 C2 v5 k2 ?0 p1 M
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;6 F0 Z" A2 v1 g, U+ p% n
include fastcgi_params;2 i0 W' l i5 X8 O J3 v; |. ^
}+ K' q- r7 ?0 j- Q6 @
}
; U8 G( j% f6 F& ~$ }upstream负载均衡:
- W) @2 w# F8 C! U l
6 W' V& B6 i. \$ |5 j3 e" Z) w$ oupstream 52os.net {
5 {4 s) `- D- m server 192.168.1.100:8080;
* J6 n6 ~+ B: b4 V( Y7 h* w' E4 p server 192.168.1.101:8080 backup;
$ A( m: |) }1 G3 @9 K}
( a& l+ _. ~- }% r/ }5 ?% C
2 O* Z5 } [) }server {
6 W* ]; n+ B# M! Y' V8 J; Jlisten 80;
$ P# G- v7 H0 l% I( K. Pserver_name 52os.net www.52os.net;# o, H1 H' X7 ~# P
1 x. p9 U# s- N' v1 X v4 r T; Xlocation / {* }% `4 J# r" Z
ModSecurityEnabled on; ; Y) M+ v7 `3 J8 `3 Q3 `
ModSecurityConfig modsecurity.conf; # ?% I# [2 q4 V) |( |8 i+ D. _ q; Y `
; c2 F1 K0 g6 S2 D
proxy_pass http://online; t3 h( U; k$ N7 R/ B, K! n
proxy_redirect off;
# [2 Q4 R T# {( K proxy_set_header Host $host;) z) N7 U* [# |
proxy_set_header X-Real-IP $remote_addr;0 d3 D- M; Y& T1 K* E7 y- M1 f
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;3 R# W" R* b- @$ z+ Q
}, b5 q: |! V l* {* f+ r
}
4 r$ m# S, h) J: b6 s y8 e" w六.测试
( `% K2 V! i2 m _0 k5 n
" V" z, `9 q: B! t0 W我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
, e# h' e0 [1 Z% l& f3 V6 p8 k; R) L/ w7 T2 e( e. e7 ]
<?php
7 u; R3 ?0 x) K phpinfo(); & ~1 Q! r# u( x* n
?>4 _0 `; E ^0 M6 G# H7 N
在浏览器中访问:5 L* ]7 q$ p! w! \
' U1 m h" e. b4 p" |
http://www.52os.net/phpinfo.php?id=1 正常显示。7 L0 q4 F+ l, P9 G
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。5 P, H% Q3 X4 _8 g
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
, i- N* Z' \. F& V; U* u说明sql注入和xss已经被过滤了
$ q, W2 K, w9 V$ i3 e& a8 [" J" g- i" [* {
七、安装过程中排错
' ~4 x4 z! ~+ h O) o( Z7 T5 a
; z# _8 i4 [- b% k1.缺少APXS会报错6 K& `2 z/ }/ U) X$ S6 o
1 h8 p4 Q5 s4 r1 k4 @! w3 M( d
configure: looking for Apache module support via DSO through APXS
" M+ i1 e3 a3 K$ kconfigure: error: couldn't find APXS
/ C8 B7 n& S% Z5 A) T0 A- R5 xapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
. R7 g1 G( L; C6 `% c; Q. f解决方法:
6 f) \7 x$ E' j5 r) W. p; A* }; ^+ l- i$ T
yum install httpd-devel. u6 q0 C6 B, {
2.没有pcre5 q1 O6 j1 L8 A. B+ U. j
! Z& m2 K3 M; _% t* w2 Qconfigure: *** pcre library not found.: t8 V G3 B, l- K) i
configure: error: pcre library is required& z9 |9 y5 _. X o/ m
解决方法:$ l- w3 t) ]2 \( F
0 v9 ~2 _* R6 k# I ^yum install pcre pcre-devel
, O: h) R# b! ` C3.没有libxml2
3 {' a# M- Z' Q+ P, R/ ~7 [- ]: u1 V. c9 p( m+ I2 g! h
s' V5 h$ K" j3 ?7 L
configure: *** xml library not found.
! ~$ F9 g! d+ k, Y, N9 T# ^configure: error: libxml2 is required
8 p9 P0 ^& ?, `8 X1 x$ i9 z; w解决方法:
0 {( P. d, i3 O; p7 Z- `% ?3 K1 \
1 k9 M3 ]' t4 m5 }: I3 fyum install libxml2 libxml2-devel
8 Q1 i$ d4 {; g5 x6 T7 }. `' [4.执行 /opt/tengine/sbin/nginx -m 时有警告" [" W/ t G1 _6 j$ Z, D
+ k m9 W* K5 J7 T9 p2 U1 RTengine version: Tengine/2.1.0 (nginx/1.6.2)) Y& F$ b% T9 c" A* s
nginx: [warn] ModSecurity: Loaded APR do not match with compiled! ]2 t% l6 T- W7 t; J& o" q4 W$ a$ h
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log. S7 T$ [% v0 I7 R1 W; G/ Y5 k9 C% c
f& p' U! L- ^0 I. e5 Y4 B2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.& f# f+ H s( A- G( k' f/ }7 G1 o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
% \+ `8 r: ? I9 s' Y2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
/ ]6 K/ {: ?* h# g7 q' G: k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"/ [4 W/ B" S) P# r" D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
0 t( Q2 J4 W7 R' _& ?2 Q2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.; Y1 `. C3 n/ O
解决方法,移除低版本的APR (1.3.9)
$ b* s+ }0 p* I# B9 s* D3 ]0 ]* p8 w d- a% L
yum remove apr; D! i6 [+ W7 \. q
5.Error.log中有: Audit log: Failed to lock global mutex
$ D* C% C& Y) x$ w( w# X' {/ d" G# {
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 8 x/ r- H# |* ~5 _( m8 l% k/ V& |
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
( ]* ~4 x. S B+ g解决方法:
& {& Q3 `& R- X& g9 h) k* M$ R编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
. ~: Z0 s3 G* _8 F% G. _* z2 d- Z$ |! o$ f: s4 Z- E
SecAuditLogDirMode 07778 _, R( N. Y |+ P+ g
SecAuditLogFileMode 0550
( w: i R. n+ FSecAuditLogStorageDir /var/log/modsecurity9 C h% H Q0 z3 K" [& o: ?
SecAuditLogType Concurrent' F: J/ \8 \) P. c% U: g
参考文章:; \1 }6 u( o8 k$ x) Y+ t
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX& f% F n" V% k- t0 W" u
http://drops.wooyun.org/tips/2614 |
|