|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
9 o3 r& |6 P/ s+ T: T( @* D+ h
0 i! A* K, r: R- h5 t一.准备工作
3 l" w, |, S0 r& E$ n3 N$ K
1 j5 ], F$ J5 E0 j+ w& k* z系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
' j( { S7 J. B9 ^/ y5 I+ E7 Z! P5 s4 c. a: h
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
9 y8 b/ E. X* T9 z0 X, {, V
6 c. ^8 z) ^$ J$ A& j imodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
- K* M" t# m& Q' C3 p& P7 E
4 ?" Q) @2 R+ _7 g& G9 NOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs. U6 }/ {9 i' h( g5 u
- Y% G" h1 w) y& R) f依赖关系:
; u1 Y5 _+ m0 F* B* Htengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:3 E' q- [8 }- ]1 N2 u( {1 B
" ^) e/ i6 C/ I; C/ K- {yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
+ B% i/ e1 c1 m: t9 u4 ]modsecurty依赖的包:pcre httpd-devel libxml2 apr& C9 R3 A, N/ D2 Y
7 Y7 Y, d" z Z: E
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel ^9 m* q3 ?# w) `, t+ h% G
二.启用standalone模块并编译
s, l- i/ l0 V+ L K
5 k$ U5 b) ]& f下载modsecurity for nginx 解压,进入解压后目录执行:! q U- ]7 e: Z8 {! f+ @; F5 `* W
1 l- v( [) {6 N- {./autogen.sh
2 ?$ \7 e+ b8 C W5 |( m- W./configure --enable-standalone-module --disable-mlogc
- j0 E5 f3 V1 c6 omake `% h3 w/ \9 f9 N3 u
三.nginx添加modsecurity模块
9 C7 \* R" _. T2 w
' Z/ K# G4 h- m) ~在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:# x+ D' V4 Q9 n6 M4 t
% ?% g2 ~+ ]( E& w4 E7 \ X8 b d1 a./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
# b, i. [& S; h2 G# |make && make install# P1 x4 p5 c t' ^
四.添加规则4 `$ w/ ]- R& r$ @. Y( H& h1 ~
8 ^# K, Q: b; x p2 |
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。6 d$ \2 R) d7 D" J5 L c% V' c
, B& h9 {. ?. S# J2 x3 _; F1.下载OWASP规则:
+ x- G) U) x" `* h: r! x3 m) K9 r
7 i, i @# V8 w; ]2 T$ w" Tgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
7 J* m g6 S) V2 z# }% \# N8 X7 L% g7 c) r; e+ J/ }& z7 ^
mv owasp-modsecurity-crs /opt/tengine/conf/+ l6 ?/ _& \6 b8 a7 }' Q8 c; y" f
% i, L: X0 p# L' q+ Ucd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
6 M+ J3 ?9 C! ~2 y2.启用OWASP规则:8 `7 y7 J6 |; m7 ^! P! Q
# j# c! h: K6 M$ z, W6 p9 d8 v复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。 h) B& J: t7 l
1 @. U( g2 Q+ A编辑modsecurity.conf 文件,将SecRuleEngine设置为 on t m6 [0 Y: s7 y& ~' L* |
4 a9 n/ H9 A/ N) y* Y g- a" W
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' W, c7 @2 g8 p8 R9 `' e; p. w, D8 \8 _/ z
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf; x5 _7 `; I, Q( D" C$ Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
N" H/ w$ Z) pInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf& C0 @$ Q9 j7 l; ]! D7 ]8 b5 m0 p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf/ E1 d. }: u% n3 M4 m
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
. d% _. M" {8 l! g. s3 K, w) zInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf7 X/ w4 J* ~/ s* m: E
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
4 e; z9 n. G8 ^, t) K' w. ?( G) L五.配置nginx
( r# | a2 h/ B9 _
5 @6 c+ Q' l5 K) u9 b W1 y: i( w/ j在需要启用modsecurity的主机的location下面加入下面两行即可:( W' F( D2 v# n9 r6 h7 h% F. T+ z% U/ o
+ g% ]0 N9 ^" O0 x
ModSecurityEnabled on; / _4 P1 n. p# P% F3 `/ L
ModSecurityConfig modsecurity.conf;. i$ l+ f q" q- t
下面是两个示例配置,php虚拟主机:8 _1 p# W4 x' m; G" l2 n. C# l4 V
! s) j0 M( W1 K( c8 v- gserver {
: }% i0 D* g, q: l1 h6 M8 n. _ ~5 { listen 80;
8 ]. G" a2 p/ h, T server_name 52os.net www.52os.net;3 L; H& F- A9 [' V( n% k3 ?$ J$ b: D/ n
& O1 K% m9 q( @4 F' H; p location ~ \.php$ {
- Y8 t3 C# _- x: v9 ` ModSecurityEnabled on;
9 {8 ?, p( x) f# X ModSecurityConfig modsecurity.conf;
, F0 R( a: ~" f. \3 M
. g* J( { `* U- n8 M root /web/wordpress;2 i$ ~ b! V) ~- e9 N
index index.php index.html index.htm;6 z& L- T7 P% O( o- k
! v0 O2 Z' m& u- i0 e fastcgi_pass 127.0.0.1:9000;2 @+ a) Y3 \) ?+ t- N9 ]; C
fastcgi_index index.php;
( Z& ^" P4 c' c/ ~ fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
7 }7 J/ B9 l$ x$ p& W; z include fastcgi_params;% @3 L* E3 f/ B& O* K' o4 q3 }/ ]
}" e+ O `0 L5 w( u. B' U' ?8 ^( E1 E
}
4 P4 ]% Y' U3 ^( g$ w$ aupstream负载均衡:
7 w: C" s$ w0 i5 j, E
+ s% o7 f: t. }, R6 vupstream 52os.net {
7 P% {2 J$ S( m; B3 K: b9 C server 192.168.1.100:8080;2 D# [3 J: D/ n
server 192.168.1.101:8080 backup;* r; M8 ~' p: Z$ @% z( l* x
}" @( l9 L6 P& h. c$ s! F
* m- z5 ?0 B6 E8 @7 a! C; w iserver {
* E. \- f# ?( x8 M; @" {listen 80;
9 K4 v6 O" l0 c; p. nserver_name 52os.net www.52os.net;
* e+ e7 C* m+ H" |6 s: [; Y2 I l1 F6 L' F2 f( ^$ I
location / {) c, v$ {0 U& _) M9 ?, Q3 A) e/ R
ModSecurityEnabled on;
! W9 o; u% V2 x# K' _' e ModSecurityConfig modsecurity.conf;
1 \ x' Z9 ^, {5 B" \3 T5 ~$ F( j" ^, c) O1 |
proxy_pass http://online;* ~7 m$ v3 n& X4 @* v
proxy_redirect off;
9 Y b- J6 t- e proxy_set_header Host $host;2 c+ ]1 ]/ `( j; R! i
proxy_set_header X-Real-IP $remote_addr;, }5 U5 k3 G( u) L% }
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;+ N3 @/ n b8 a& t8 V/ j
}
2 @/ m+ E$ `! [5 R& }6 a/ H' e}
; y: d: \( s' [% s" i& B5 L7 r六.测试2 N& }! M( _. p- B5 I) \
% y2 [& H7 F- I
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为: w/ h, Z: J8 V1 |: U! [/ }# H/ b
7 _+ b V; H0 U6 g. r
<?php/ B$ p& j/ |2 M- T/ U; z- d& h
phpinfo();
: O5 A8 d7 _1 K. a/ M8 x?>
h* }" D6 p: G8 C5 m- O# ~5 M在浏览器中访问:
, _( E, |. V: W1 D5 ` _1 w
5 Y/ h! Y1 [/ d9 }7 Khttp://www.52os.net/phpinfo.php?id=1 正常显示。5 k5 H" `# Z9 [7 Y1 d
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
0 X' V& |; H: F7 J+ |http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。: c+ i2 q" K% w
说明sql注入和xss已经被过滤了9 @, c) C) [2 n! u. \ M
, {" b. X, ]- k& n# q2 b7 U3 ^
七、安装过程中排错& L4 P0 C. s- o8 ]8 W. B
. u; E2 v4 R9 N* l G3 P' l
1.缺少APXS会报错
( p+ I5 t( B+ s- _/ C/ Q" t
) P8 j8 W; `# o7 aconfigure: looking for Apache module support via DSO through APXS
6 x2 @" q6 ] e: M g# \configure: error: couldn't find APXS& h+ d" t5 ~/ `3 W& ^
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。* w% @& F+ U. t' M1 n* }( d
解决方法:
) x- w& E) W, g4 r+ _. x# Z7 i: _, r% t
yum install httpd-devel; _/ d L2 b, x9 v3 C1 b' L5 C5 B# A
2.没有pcre
( O3 I5 e+ {. T6 z+ Q, v7 Z2 o! D* ^
configure: *** pcre library not found.
2 p; B* N- T3 e gconfigure: error: pcre library is required
3 Q1 j, L o2 s: C& M' U解决方法:( L2 J9 l# |/ Q M
, g0 @! C: V4 V/ w6 q3 Ryum install pcre pcre-devel( o, i( X% S H1 M3 J* a
3.没有libxml2# g9 R: j | u; u# X, z/ ]! O. e
. x/ r' ]5 L3 A! O
, F3 r, M! v f1 uconfigure: *** xml library not found.
1 o+ Y, O @$ v/ @/ {configure: error: libxml2 is required
+ {# g4 n( r5 E2 \& \解决方法:
1 e/ T! L; A! {) ?) ^( F& u7 Y6 ~+ A6 `# c/ L
yum install libxml2 libxml2-devel4 O, p7 V5 G1 ^8 m( E
4.执行 /opt/tengine/sbin/nginx -m 时有警告& W: I; P2 J5 H7 i' O
% h# ~/ o% N1 E/ YTengine version: Tengine/2.1.0 (nginx/1.6.2)0 _) P, Z8 d9 I6 m1 w; x9 q
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!% z" F" S- S! B, `
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log! W5 T" ^8 s6 Q2 N/ B
( G) n! {8 t9 C2 t" Q( L
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.# `3 Q5 i# _; D' E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"6 U+ y1 K8 z9 d8 j
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled! N/ t( z! n: z& Y, T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"; e* D) J* Q; B4 q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
/ C" h! @, {- A" M6 L2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
7 U/ \3 X$ R5 v {2 J0 E! |$ m解决方法,移除低版本的APR (1.3.9)
( P$ s& K5 x$ `9 E+ c! F# ]
$ |* e; N5 `! k; F t( |1 P0 t; Cyum remove apr
; u: x1 {& l4 R/ l1 S5.Error.log中有: Audit log: Failed to lock global mutex$ d( `2 q- [4 x, c
7 y# n8 D* y% B# n8 o! {
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
/ j1 H( q0 U5 y& Cglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]; U( M0 A4 Z; ^
解决方法:9 B7 c; ` L8 |' c
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
4 a {* H. ~& e
( A2 G u t+ N& DSecAuditLogDirMode 0777
4 J* |1 ]; d f; l" k; H; ?SecAuditLogFileMode 0550
; r7 Z0 a( q8 ~" ~# j) e( ESecAuditLogStorageDir /var/log/modsecurity
3 Q6 N. S$ |' A* [SecAuditLogType Concurrent
8 z1 k2 a$ s( J8 }参考文章:& ]; f0 O$ _9 Z; ~
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
) C! O% K1 V4 D2 t! Ghttp://drops.wooyun.org/tips/2614 |
|