|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: T6 h. n4 N" m1 B2 \+ A# M) T. J) X
一.准备工作
! f4 Q& [. W2 N" ^$ `; z3 T4 I' R# F0 ?) I/ s2 S/ S; M$ G1 F
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0& m3 S- y; I2 J9 L, r- M% F9 x
: e! ^, m$ X" x; O5 c ~
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz. R2 D+ E6 e. T6 S9 ]
, l# b/ t5 y' g* x2 hmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz: \$ y3 q1 G; R" A6 \9 T
3 A2 c/ s0 E1 I" Q3 ^9 ~- Q
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
8 B1 F5 C* Q, Z" x) |
+ J" l! Y: a+ i- Z9 n! s依赖关系:1 K1 w( X2 |9 A ^7 b
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:: B1 r- l+ x* `
* A. @! ?) C9 q9 X, s
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
* z5 W5 Q) F) Nmodsecurty依赖的包:pcre httpd-devel libxml2 apr3 ]. h) ~% U' ]) A) ]5 ]. W
( i3 U. T; x6 a! N: Q8 ~
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
. q3 W/ T; w+ P+ G" o/ k! H% W二.启用standalone模块并编译
, E$ z" e/ f" K1 Z5 ?! w" e- u/ V# Q [# P
下载modsecurity for nginx 解压,进入解压后目录执行:
& \8 |' j" \* Y" V& \. ^
/ W. G9 }4 M( G2 `( \./autogen.sh: P3 w% X2 U( r: L" I4 Y7 y
./configure --enable-standalone-module --disable-mlogc
0 s/ K+ M$ y3 r" v+ s. F* W+ D/ xmake
2 ~0 U+ g, s- @& ^" y+ E. |三.nginx添加modsecurity模块+ g/ o! D# e. V2 t4 g
, E6 G/ Q5 o* _在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:! G, `1 m1 } k' H' {. c
) Q1 \. F+ C, h./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
+ E. x- O) i' v2 r6 d q4 q( xmake && make install1 c: C, }: D+ l/ d
四.添加规则7 B7 k1 o8 m t0 G1 @% ?$ `
. p1 r3 ]$ f6 R' pmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
- U2 C* \) J" V9 X* I) ^" p, _+ E# ]/ X4 A3 a8 T
1.下载OWASP规则:
7 q5 d- e9 T7 j
K: a. D' |. c# m Ggit clone https://github.com/SpiderLabs/owasp-modsecurity-crs" E" W) h* p0 {5 ?; f0 f- c
" O' M4 I+ t! Z6 Rmv owasp-modsecurity-crs /opt/tengine/conf/
$ k( Y+ L1 i% Q+ k. [* o+ L, j1 R9 y0 p2 W7 V; S0 X1 E; M
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
$ }* }6 b" Y. C. x6 Y, [: V- v4 d2.启用OWASP规则:0 q/ r) U: C6 }
- l/ |: t T, D2 T# \复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& A) W, [& [/ r6 M2 ~' p5 w
6 k4 S; ~! z8 j2 r7 @
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
) i( n. F* G7 ]2 j5 e. _/ z* }: l
- W' Z! j: E2 f9 X; x7 F uowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
. c5 P0 O) B w/ u3 u2 _! P( V7 E+ u. u2 e
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf5 \! m' P$ r8 {9 r7 d+ w3 i8 v0 ?: D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
2 f9 F1 Y# W7 G7 _; _' [- xInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
i5 u( x3 B% v" t" PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
& X% E+ H* w3 z3 j! ?. s# h. QInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf! v* B1 r$ r$ o) E) T+ _
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; h% z0 Z4 W8 c
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 `4 a' [' {8 y F) R, _五.配置nginx
8 R2 ^0 o8 d- v5 B
# ?, ]2 d8 p' L8 }6 C+ a' y% }/ ~" E在需要启用modsecurity的主机的location下面加入下面两行即可:
' L: \! w& v T, R2 y
# R# B1 p& M* q5 |. p4 u% {ModSecurityEnabled on;
. H+ l/ K4 q$ p" t4 aModSecurityConfig modsecurity.conf;. V4 \7 x+ B$ m0 C, x ]
下面是两个示例配置,php虚拟主机:
# X7 s; W# h- ` j
' S2 P+ M% f+ h0 v; Cserver {
- g( w- z/ Z3 e1 |2 N+ }/ ^ listen 80; L5 Z' r2 G! O- s
server_name 52os.net www.52os.net;0 E( S6 X3 Z- B4 o
# S4 H0 o, E# c6 G' @ location ~ \.php$ {
0 v P5 Q% `& ^' `* X) A ModSecurityEnabled on; B# Z, H* S3 P! B4 f0 x+ X
ModSecurityConfig modsecurity.conf;
$ n) N9 p; t5 W, y6 j- {8 C7 O8 ]1 F" h
root /web/wordpress;
* }% M i5 ~- i& \* P. }, \0 @2 a index index.php index.html index.htm;7 p' r4 c! M% A. K" ^# B" h1 ]& c
: N9 u) q6 j0 @* l+ H fastcgi_pass 127.0.0.1:9000;1 F m& A' C2 C1 f$ m
fastcgi_index index.php;
5 \: X+ h, u9 w* l( f% _ fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
$ H7 x" L8 j3 }) @" Z include fastcgi_params;3 L2 ]) Q* v& C; j8 n( [" T
}3 ^3 P( U6 ]* |, G! o' W) p
}
; E/ O5 Y. l0 Z" S7 p4 oupstream负载均衡:7 J' l" _; P; C+ x9 `$ B2 `
+ \: `% ]& S& u m+ C
upstream 52os.net {) r7 o- E" ?; _, e
server 192.168.1.100:8080;
7 X' z2 ?" d+ Y, d; z3 |. C server 192.168.1.101:8080 backup;' p; Z8 Z ?' G6 U
}+ p: W) d( @: b2 u: i; i* e
1 P- s2 U0 _7 f( S
server {0 d. h6 B, a" f/ ~3 y: N& {( T" [ `
listen 80;0 M# k: q- X2 Q) p
server_name 52os.net www.52os.net;
n; F6 @9 V$ W) e7 @: f! @- D8 c
/ X1 X7 @4 m U0 Plocation / {
1 r: Z& t/ M* _ ModSecurityEnabled on; . Y( d) x* t0 p( l# e
ModSecurityConfig modsecurity.conf;
" W4 }2 E) Q8 V, H. e) D* J+ f0 Z9 M
proxy_pass http://online;
2 k1 R3 u5 q* } proxy_redirect off;$ @) }/ O: w# Z
proxy_set_header Host $host;, H# P% W9 e" m6 |. y) s
proxy_set_header X-Real-IP $remote_addr;
) i. }2 S6 z6 M0 L: {( v9 ^* k6 u- o proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
9 s, w- f. x6 M( O5 A2 j1 f1 h }
% @; w) g% o% N6 {) X& e) P}/ U4 r! ]( C u
六.测试
8 h4 R. ^# F! Q+ ~- q9 N" V& Y6 A! |/ E) E
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:" O5 t, @1 W/ A' j- X
: n+ b. Y! O4 H
<?php' V5 ?7 N3 D: E/ M
phpinfo();
9 ?9 @3 f0 Y5 i9 |! V9 Q2 U?>+ t) e/ `5 N7 u {) z
在浏览器中访问:
- h0 ]+ |1 u1 g+ z
! e$ k0 k+ [, M2 j- O/ J: Bhttp://www.52os.net/phpinfo.php?id=1 正常显示。3 e8 E& }: {! Y( J3 a; q
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。 F4 i5 e. S. `- r0 U8 x) n
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。, J4 n1 y5 p4 C D7 {
说明sql注入和xss已经被过滤了4 n: H$ L! [* A& N9 Y
8 Q6 m8 A" K/ g) J( A七、安装过程中排错. X3 ?% [8 Y+ z$ J8 B7 b
0 i- y3 z- q; g. A/ c8 q1.缺少APXS会报错
' P& _" ~4 A& Q2 ]
% G8 F) v/ m A" dconfigure: looking for Apache module support via DSO through APXS
3 g# |0 }8 e, ?( ~6 P& B, \" zconfigure: error: couldn't find APXS( e% b' ]) ~8 [
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。9 r; d% d" v k" W9 w' B
解决方法:
8 R/ N E0 m, A% g& m4 Z: S
/ ~: _ _1 k4 n, U' ]6 _9 _yum install httpd-devel
) G& ~4 l% t9 K2.没有pcre3 `# \5 W. m/ G8 I/ {, ]+ n
0 C' c+ T# m) i' ?' O! J' M) [8 ?configure: *** pcre library not found.
) I, b: B9 N; X1 B' w. v5 Econfigure: error: pcre library is required
1 K- O& a; u! s! m3 I s解决方法:: ?4 J7 ]) F) @. v& `
! |. n# M3 |' Nyum install pcre pcre-devel
" L$ L ?& S4 N! C' k3.没有libxml2
1 _" K9 q1 j$ E/ I" |
4 k: J1 }" [* W# u3 O# _ _/ L2 ^, w! B [! G& Z5 v
configure: *** xml library not found.
7 @7 @) [0 A' w3 w" ]configure: error: libxml2 is required
7 [: M8 E9 E2 b) L. n$ G* U解决方法:/ @3 J1 N( }! h' P: Q# W) x
+ ?2 g$ n% i) g, E7 u4 }4 k, Lyum install libxml2 libxml2-devel
8 A. l- I# c+ R' d8 `4.执行 /opt/tengine/sbin/nginx -m 时有警告; A* p; `2 {* ^+ u4 Q9 X' a* S
* W( q! l1 T9 E
Tengine version: Tengine/2.1.0 (nginx/1.6.2)3 A8 M5 ?6 _3 b. e4 ]9 R2 t. \ y
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!% Q+ w- K& \" {' I0 Q
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
6 c& C9 P) m( x% H" \
+ l$ N- I, B5 L; w; g6 i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.' a1 U% s# I' m3 p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9". V; b" X- Q7 O
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!3 ?! D7 s0 P; D' c5 `5 ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
/ v4 y) d1 y3 ]8 O: K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
9 s5 B1 c* Z u0 P* _" v2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.; l! D( }" e( U* _* ^9 I* V
解决方法,移除低版本的APR (1.3.9)* a( R' r: R* j* i% x
, e) k% q$ h5 T8 ?2 ] {yum remove apr- H* I/ w+ B( w) K! Y4 s; E: T
5.Error.log中有: Audit log: Failed to lock global mutex
T3 t7 t& I8 r1 G5 H* u
Q, N' V) m0 ?8 s- _( q& A. @8 ?% z. M+ }2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 2 Z" n1 J1 A( R" {6 a
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
8 l: G. [+ C% P! {$ l0 Y解决方法:
- X E9 s. E9 G编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
) h" D3 I$ F( e& t6 Q; g0 s8 J0 {& `$ q& x) H6 R& o
SecAuditLogDirMode 0777- L# U1 q* }! q9 ^/ W5 |: Y e# h
SecAuditLogFileMode 0550
& E6 T! b0 K; @" n: uSecAuditLogStorageDir /var/log/modsecurity
; V& c& j/ e' I! m$ N% o9 O2 aSecAuditLogType Concurrent* w! j8 z( t [. y+ i9 _ V8 Q
参考文章:
; i' u4 {: |6 v! A+ d; ]& ihttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX0 t' r& C2 ~. o4 s" x& ~8 \0 D( K8 J- m
http://drops.wooyun.org/tips/2614 |
|