|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
" Q# M" H; h9 B$ ]9 {. O( r/ {* P. l, m! f, t' A
一.准备工作
, Z4 w2 T' C7 i3 r) s+ Z1 l3 {+ g) Y" L) a4 m2 ]
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0- U3 y6 I6 y/ Z; P' b: Z3 m: z, f6 i
# c$ K& o. x! o' z4 {* Q2 d% V) {
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
2 N' F1 o1 q/ W+ U1 H% c. f
- B3 s7 d7 u7 b) ]$ X* q0 F+ umodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
" R2 A# }8 P U; K" @: j" s8 r
: E& y! Q/ {& i$ B/ ROWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
/ V+ w T2 r2 O7 D
& f3 H% l! h6 f0 C0 A依赖关系:% l$ T1 C9 K9 J( a8 j
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:5 O0 G, D# D1 w2 s5 D
, C7 K6 u- ^: g" c5 A& [# Fyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel$ g& d% V: w9 I) S6 ?$ y/ ^
modsecurty依赖的包:pcre httpd-devel libxml2 apr
6 N9 n; W% r; q; Y. L( _
* M0 s7 D- ]$ C( M- Zyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel' r, U/ D2 O+ z6 U( k$ h0 s7 f
二.启用standalone模块并编译
`3 u" e5 d1 F0 t- [/ e9 w3 {* y: P+ M$ P
$ r7 m! Q# W- ]6 x( D7 z5 g9 s7 ]6 y下载modsecurity for nginx 解压,进入解压后目录执行:
: `5 Y) \1 n9 w, u5 p' t h \
./autogen.sh
) L9 t! {4 g) S! K3 T./configure --enable-standalone-module --disable-mlogc
, T5 Q( Z! P. M- Mmake
1 M& ~, ^+ `9 O# m4 K三.nginx添加modsecurity模块
. V+ @- v% b% F/ ~' P! q$ s
- K; e8 N% E, `5 d在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
* I3 k: o9 m. r7 T) }: m6 i. d; A2 Y6 }5 p4 c
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
+ _$ P3 D0 M5 F3 A7 l, nmake && make install% i$ V) v. Y* V6 |& v! u- w
四.添加规则$ a( ~( b& y; E1 x t- x1 K
" @- u! F: f5 ^2 N" E2 L
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。" ?9 q% K2 p* Y. z
& {+ n) {& x7 n0 G! { ^1.下载OWASP规则:( @# j. n% g( @; F0 [
- U/ [2 [8 w/ L0 R0 G$ T; f. qgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
2 T# v ~- A' a8 i6 _; A, c7 Z/ [- c S \" f: t) M) V5 g
mv owasp-modsecurity-crs /opt/tengine/conf/* W# C9 m. j( _
/ f+ V" N. X' D
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
# I. h7 U6 G' u. N% G& j$ \2.启用OWASP规则:4 Z+ X$ r$ Q. U" N& o |6 Y. Y
! |. ^' O% D" r& C6 p6 c! I% O
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
! ^% H$ u- c, m& L+ m* Z2 ~" V2 ]6 K6 r/ c0 a* q
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
& C, l" e0 b) c- H
: _( _( U( G, B+ X( C! ~- Z& dowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 q4 F: W( B7 N% _; W8 B4 W3 F3 C1 n/ y5 |: e- h ~' ^2 ^
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf8 b2 _ W& i3 t6 S
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf, F3 @1 S$ y5 a. k* L! \7 s' c; p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
( D6 Z V& f0 M6 ~6 G3 V7 vInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
" m ]% d! h& v# H( ^* VInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf/ y2 ]% V0 K/ _9 F9 e# ~
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
2 w x9 w7 O* d# |Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
* P8 i9 b$ {. v% \ o. j5 L- q; b' D. H五.配置nginx4 \0 P9 o- h% `- k- I( E' [& B
5 m6 @. y5 S+ c S) _
在需要启用modsecurity的主机的location下面加入下面两行即可:
- N$ d K& P. U) M/ M, r' R9 k) T- Z8 z0 g2 y$ ]
ModSecurityEnabled on; 7 c+ I0 ?" j( r5 ~" y3 ` R
ModSecurityConfig modsecurity.conf;9 S2 T, v2 ~; S: A* K" i
下面是两个示例配置,php虚拟主机:0 n+ m/ e9 H2 B* ^# H4 x
# v9 T+ |; F! U3 p0 |" sserver {, S2 b' R: N7 y8 q) r2 H
listen 80;
3 E9 f7 [0 p1 X3 h8 I server_name 52os.net www.52os.net;' q& M' e% x7 g& ?- x9 ?! o- [
! p% s5 [; [4 I2 H' F5 O! {
location ~ \.php$ {( h1 M) |/ c, ^( H( G7 Y* U
ModSecurityEnabled on;
1 Q5 C2 a6 [7 L( B v ModSecurityConfig modsecurity.conf;$ z3 S) m2 T) E( A
% r/ B; f( v: z5 [+ ?( K root /web/wordpress;3 k4 I$ |3 H6 T% r& [
index index.php index.html index.htm;
5 H# A! k2 I! C. H7 d$ `4 `$ I & T* t6 ^% b8 X ~5 m7 d" N1 S
fastcgi_pass 127.0.0.1:9000;, e4 s& t1 M1 P7 R9 ?
fastcgi_index index.php;! x! b' }. ? r/ q7 y
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;% ]( [" O B% v# T
include fastcgi_params;' O* g) F1 K7 c: D& n
}
: O5 R, ?9 J1 }/ b. W' S+ L }5 M) h' _1 j8 ~
upstream负载均衡:0 W$ e( P7 r }( H& t* b) Z. n
2 v" ]; r" ^4 d+ {upstream 52os.net {
7 G2 z; Y9 M- D server 192.168.1.100:8080; c6 @2 L" B6 h% G0 y+ x
server 192.168.1.101:8080 backup;8 y; {: W* L! Q2 @
}% A/ c: [( Q6 N8 J6 x
" g3 C# b1 H+ I( y0 _5 Q' }server {
9 ~8 Z9 [) o5 n |, y$ Q8 [listen 80;
+ }$ o+ J0 v7 i$ F/ |/ Kserver_name 52os.net www.52os.net;8 S0 U' v, A5 d) q+ X* i0 g) s' w# ^
% i9 J! ? `" x8 `( j1 B! g
location / {
7 u. ?' l3 O3 r ModSecurityEnabled on;
- H; r/ g/ q/ F- N- c ModSecurityConfig modsecurity.conf; & }+ ^: P8 l5 O, h* ]
/ u, V& x, a5 O8 u+ o# G
proxy_pass http://online;- e: _8 K+ b9 [& X; R' A
proxy_redirect off;5 O) s+ Q0 g3 Y+ J) H. ?" ]1 s3 y& r
proxy_set_header Host $host;
, w! N0 c6 Z4 R4 l proxy_set_header X-Real-IP $remote_addr;
G+ L- t) E% Q9 O& T proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
: v- e% i. H+ V" H1 J' n2 A( W( S }( o4 x2 c1 w2 c; W5 F
}
9 Z. ]: F7 g+ z' U1 U2 N4 R6 Y六.测试$ Y6 d' z2 s, r$ \% t4 T
$ z3 }+ X0 ~5 i我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:) f) n2 V, E6 ]) p
5 `3 {5 ?$ v9 [5 S) l& G$ f
<?php+ C9 D4 f3 I: K3 K& S
phpinfo();
' {6 [$ y+ p' E' a?>
# h+ b, a1 K/ n在浏览器中访问:
9 U$ T) D1 f( ? v" V# F
1 x7 e4 U1 v4 a7 Z6 X4 p5 v0 Ohttp://www.52os.net/phpinfo.php?id=1 正常显示。8 ?! e! C& V: S- L5 M+ w) Z6 w
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
9 Q! E/ J$ s& A1 U0 whttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
& P% V$ u b2 L0 l) V/ w说明sql注入和xss已经被过滤了* S' P% p# M9 W1 j, \
% [+ t* x; ~. i/ Y& o) `
七、安装过程中排错
0 S- I" D# W2 Y; J# O+ u
- L0 k% M- U0 S1.缺少APXS会报错! J3 L- o2 o9 V# B$ k% \
7 ~+ ?6 G; R( o' [+ I& ~
configure: looking for Apache module support via DSO through APXS P' M% v1 z, R: @% ?& a
configure: error: couldn't find APXS
. W9 c. Y' z7 L+ @ k' z capxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。" e2 |# C% x: r8 r$ ]' J8 E
解决方法:
& h; m; H( |) q7 T6 T; @7 @& F$ J% I
yum install httpd-devel( |: I5 h/ ^. t0 t) z8 i
2.没有pcre3 {/ g2 o5 e; A
% X! F' g: E+ |2 i, y: x* y8 C
configure: *** pcre library not found.
3 n& M9 e0 y6 G8 O6 |) Qconfigure: error: pcre library is required# W% w- M0 [0 N2 Y& z: q4 m% S
解决方法:
- W& M- m4 i( ^$ V! K# ?; Q+ Y- S3 R% p1 d( O
yum install pcre pcre-devel' E3 O1 Q4 \5 b2 d
3.没有libxml2
" ^$ O+ Y+ v% v; X
" _' ^ Z1 H. S! k4 |
# z' K$ o$ |4 y& i8 e9 L# ?. cconfigure: *** xml library not found.8 m" R3 }$ ?9 h. T7 F& ]
configure: error: libxml2 is required
6 A, b1 y# j2 W, H* C7 w解决方法:
* T) }$ O! o3 s2 j: U
# Q4 k* \' V6 h" gyum install libxml2 libxml2-devel
! }+ v% v% }6 b4 ]/ z8 D% M% R# x) U4.执行 /opt/tengine/sbin/nginx -m 时有警告
2 r$ D: Y3 W9 ]5 b
; I2 S: F7 j6 S" u1 J4 r- p3 @Tengine version: Tengine/2.1.0 (nginx/1.6.2)+ p, D% y% P2 t3 c2 i
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
9 f- a1 Z0 t. L* G原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log/ O* w$ I5 A) k+ }4 t; ]
3 v7 Z" E% ^5 O- B1 C
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.+ [( o7 T' E0 S; ?# B, h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"8 v& C- }7 ]+ {' k9 Y( H5 v! A
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!9 u" f. w9 p: q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
: L6 t9 E8 _# V ]: T4 [/ y( C, U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
- @ }* {/ G7 a: K# g9 t! S2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
6 v1 c! \1 f" g( z( N解决方法,移除低版本的APR (1.3.9)) [' m- S! m0 ^% d
" b$ j) D) z9 R$ e9 w/ V3 ~+ hyum remove apr! }# b% T, z: f: w9 ]- o
5.Error.log中有: Audit log: Failed to lock global mutex
$ z% |% V/ {3 o$ w2 `' A/ f
: v. V4 h8 l- Q* N8 Y* h( i2 D2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
" b! h6 |+ H: aglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
7 S! b4 z1 \* {. W+ G解决方法:* R: g. v+ S) k5 k8 c
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
# R; m; z2 g3 J. E h9 }; a) h6 u) I# O6 M/ S8 T* F
SecAuditLogDirMode 0777. D# i% F4 K- A/ `7 r: Z- K
SecAuditLogFileMode 0550
5 z2 C. e, D2 S& s/ w% lSecAuditLogStorageDir /var/log/modsecurity
. I5 F$ p4 q4 ~" M) [' NSecAuditLogType Concurrent1 J6 C1 g7 z I& a5 J h/ {
参考文章:
* x) t) V8 A& L7 Shttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX; G$ M+ q9 ~* q. U8 W, T: ~
http://drops.wooyun.org/tips/2614 |
|