|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
8 r- d5 Q2 }$ G! U: ?
) l: {! Y. L. F, T一.准备工作5 G0 P' q3 C7 v% \7 M
. N' C: C1 U: Z7 ]6 Y9 L系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
- N$ g/ k7 A# z' I& C) d+ \4 A! D. C5 r* E+ R+ n
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
t; d- s9 F' _' R' O! G& ?- I
4 }' E4 F: p! l- E+ M. kmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz, ?' {/ [5 A- R7 \) @: y: k3 y" K ~$ a
6 u+ }. h. M, {: w9 g
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs, p4 `3 m; E$ B8 c& u- G
4 B3 s* i# R* Y1 Y/ c+ e
依赖关系:8 s* A* x% ^6 ~% p% _! q
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
3 l( O$ p% A5 m+ g6 h
6 T- R* S" A: n* ~/ Z* y& c, ]yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
% t2 v0 M! }5 l% f- u. Ymodsecurty依赖的包:pcre httpd-devel libxml2 apr
5 M9 W& Y" J& v Q& P7 T* }
% ~) ~7 C+ r+ r- F; syum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel" I: h5 \8 N% z6 X& B) F7 T# X
二.启用standalone模块并编译
4 N: c" u h- I* a/ w9 F& u1 o3 U' ?) x, Y% l( u# o
下载modsecurity for nginx 解压,进入解压后目录执行:
' u5 I, \; |: C- h$ ]! t) C- i+ A" O- ~ Y }2 G: V. \
./autogen.sh
- S" g) d: z& `% L./configure --enable-standalone-module --disable-mlogc6 _2 W: j8 X( Z+ ?, ?6 b
make * b4 ?) t# ^3 T: o6 ?2 M
三.nginx添加modsecurity模块 N7 ^% ?8 x4 \# z* n
3 l, W' S+ W! o3 ~9 c* O. v, V4 v在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
7 j- T. l9 N# b1 _# ^! S
* _; _+ y1 E4 S7 G4 y) O3 \# L./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine" Q2 p: k$ O* c- _ Q
make && make install8 N8 E/ o) v& [* B
四.添加规则
1 m% H: x( v# [5 i' T/ t# y/ q/ u) \! R. b0 u
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
/ E$ x5 o/ R0 N8 o u( b2 [" d* I; P3 M3 o; d" V* V9 C; e/ h
1.下载OWASP规则:( x6 ~ h9 s6 K5 _
T8 t, t- U) ~+ A2 `$ R. t
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
5 a2 B7 ~/ {6 c1 b- ~4 e7 G5 H9 z( N3 u2 d, H4 p o
mv owasp-modsecurity-crs /opt/tengine/conf/
( ^0 v" X( z, s; \: h' t" @* J1 B# g0 z1 U! ]$ ~
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf" u: m) `. a! h
2.启用OWASP规则:
( y2 Z9 g4 h2 n0 z% n+ J7 S$ R# j% p
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. ^2 C- X+ \" D- }
$ c9 q9 T+ J. u$ F编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
" l" d) M# _& G$ [& L- v! B: D
5 O" f1 {+ M1 h, Wowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' e# A v1 \" ?& @) O( u \* a
9 i( Q0 o5 ?' I) V1 RInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
! O. U/ q- B$ G+ aInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
! z0 c! D0 v8 I2 ?8 I; h/ eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
' g- D! V* \" B' ]Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf4 h6 d0 |# a9 F4 M* ^
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf$ f- R" Q; c& r. J; w( ]$ V) i) u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
1 ?: U8 k5 E4 }Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf: w# ]6 j- v' l# g/ I T" k( N8 }+ G
五.配置nginx
/ q8 b$ g, B/ R! y+ T
0 A% T( Q: b9 z; G( a. t在需要启用modsecurity的主机的location下面加入下面两行即可:9 U+ _- j0 [+ G
' \2 D; [: r' z1 t- z9 s+ ^$ [( O
ModSecurityEnabled on;
$ ], }: P. v; j! ^% ZModSecurityConfig modsecurity.conf;
0 S+ M6 T& N. U! v9 D# U) _下面是两个示例配置,php虚拟主机:. r. Q9 q& t% d/ Q2 l7 V1 ^
- u) E$ K. U$ x# G% jserver {) \0 B/ R) k$ T5 |" |% p
listen 80;5 G1 d# r7 J: P5 x# O
server_name 52os.net www.52os.net;$ ^- h9 o9 t, b- B( v8 c
( P- ?/ ?- S# y3 o' W' w& o location ~ \.php$ {0 {8 N( ?, ~8 ]% f, m* B9 f5 J
ModSecurityEnabled on;
, s, G* f' g9 q2 e$ [# U8 ~. u A ModSecurityConfig modsecurity.conf;& U: N U4 w6 b8 @
& |" p* W+ V6 v6 Y0 s& c1 { root /web/wordpress;8 I8 B3 S, X( e: N1 {
index index.php index.html index.htm;2 u) ~. J8 t0 ?1 O
) E6 Y7 B! I& R, ~% e' V5 T" C
fastcgi_pass 127.0.0.1:9000;9 o4 i: R [2 {% G$ j
fastcgi_index index.php;1 L6 u& ]" i" I9 @
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
8 f, ?/ b' i) ^; u4 l4 l include fastcgi_params;
7 O. P4 n7 ?; S5 H4 e, ^ }- D: X G3 o4 V! g+ W( W K
}
& g9 G( p2 ^+ m% pupstream负载均衡:# F, }0 i; Y: S- g
8 [2 N4 X- W; Y, g5 g$ R# ?upstream 52os.net {
9 t5 L( O/ ]" j8 T# f server 192.168.1.100:8080;
4 f/ p- Q& R- i$ E3 N server 192.168.1.101:8080 backup;
$ f4 d* L ^5 B4 A}
8 Y2 k. k( b# v1 C4 c' D2 C& S4 x3 G4 _
server {: h$ H; s# I' r5 J5 i* c! u" t1 }/ N
listen 80;
) t, f2 Q* q m( d7 wserver_name 52os.net www.52os.net;( K4 w) W3 v8 r; L: ]" S8 ^5 |& v7 K) E
d# z2 ~& R' Y- Z: V
location / {
7 P, k& p' v% p5 i" @ ModSecurityEnabled on; & a1 ?: P# }$ F K5 K
ModSecurityConfig modsecurity.conf;
0 F. K& y4 e. h- \
4 @4 G! X! t0 ]: d& Y proxy_pass http://online;
, a: b. z, t" q) r- ?1 Q+ s& F proxy_redirect off;/ i2 K E# H0 { n9 q0 s- Q4 ^ _, ?1 D
proxy_set_header Host $host;/ j! ]& a; i" ~$ m( v4 @
proxy_set_header X-Real-IP $remote_addr;
& h U6 X& j- O& a& u+ ?; n- y proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;/ d3 U: k9 f/ C; ^1 [% _# i
}
6 N4 q6 v5 N4 N& j- M5 P}7 _0 P' R- M0 v; n! u ~2 u0 p. y
六.测试
& v5 Z% D) |& g& ^0 {$ Z
! ]2 x. M3 u Q! q我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:1 a- A F* s) ~, p- ~" H: U( D
( F9 M+ D1 j+ w8 v- w0 Y<?php2 ^6 J& x9 ?4 [( C; @4 B# X% L
phpinfo();
1 e" h) C3 V3 H+ J?>
* I+ x% h, C6 U X: x" E2 \在浏览器中访问:
' k- N+ t" \$ f/ @* R" W$ T- e0 G8 y7 ^* Y) ^% }5 Y2 b
http://www.52os.net/phpinfo.php?id=1 正常显示。4 q% S- A5 C2 F
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
5 f7 l( W7 z# {' \& ]4 c$ lhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
" Q% h% T. E9 [$ B3 d* i( G- F* P说明sql注入和xss已经被过滤了
- k: C; e$ C4 S$ h F, w% `$ f+ B3 j! K8 `# R/ J# F- n
七、安装过程中排错8 u R4 h+ J% u9 T
) }& ]5 p( i5 @6 i' d2 T) G
1.缺少APXS会报错' a/ Z# j- p( X* g
7 ]6 D" F! B8 V; P2 @0 Y% L" X
configure: looking for Apache module support via DSO through APXS7 ?# n- C3 J- ]8 j% F k( T8 s; K
configure: error: couldn't find APXS
$ y7 x3 g! Q& U" ^7 dapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
, s0 e9 K0 R9 |$ i解决方法:
+ m/ e4 H- d$ c+ `
; A( u4 V0 @' Iyum install httpd-devel
7 e) ` _! s3 a& o" o9 n: i. `) l2.没有pcre4 h! I) Z! [* M. R* L
( u& z6 E6 ]) N+ U5 ? q3 rconfigure: *** pcre library not found.' S$ k$ z1 `* y( T" A% h0 i
configure: error: pcre library is required8 }- d6 M8 M9 O% c% I( Z! U4 s
解决方法:$ c5 J; [2 E$ z# J' R1 H
- M" w, @- D- l! C, lyum install pcre pcre-devel/ E, k6 V; M8 ~* k' O
3.没有libxml26 \! a/ d+ z" [( k- U4 W7 C
1 G" T$ Q" Y2 B$ ^5 e3 J
1 S' H: a. l. R* ~
configure: *** xml library not found.& s* w" R2 x! h# z
configure: error: libxml2 is required
, J5 K# ]7 Y' a% @( [解决方法:
' t$ a* F% F' [" {. k0 ?* ~+ [1 s; I# _& f# t
yum install libxml2 libxml2-devel
3 J. v- j9 D B6 }9 ]7 t& a+ y4.执行 /opt/tengine/sbin/nginx -m 时有警告
# c4 k6 [6 g3 U* h1 W' b, j$ Y
9 f4 M2 t7 w8 B6 \% O- qTengine version: Tengine/2.1.0 (nginx/1.6.2)( \& B/ H, [9 m( Y, H# a
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!: Z( e, i, l& {6 T$ h
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log# d4 C! ^0 Z: W" f. a
# z1 d( p, b! [) b$ U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.. m4 s# \6 }" x9 i. n- ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"9 Z# P& e( W- e0 |# }2 p
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% j2 f2 F; ~) e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
" z/ e/ ?+ _- `/ Z' i8 ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
: A9 u9 m8 D Y$ l2 | m$ F/ u% h6 ~# F2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
, r. u' [& s# u1 k& S4 v解决方法,移除低版本的APR (1.3.9)/ j6 ~# v2 x9 V) ?+ h: N
; t: ]3 z, ?3 t4 S6 E2 B
yum remove apr4 U; r' i& T( G9 a
5.Error.log中有: Audit log: Failed to lock global mutex
3 x$ Z$ P' @, o1 H# w6 z; j& x+ R# e6 D# Y A
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock + W1 J$ [0 r% i6 A! ?- @
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]; C/ }2 }. \( ]0 h p0 }4 p( G
解决方法:
- n$ a j2 O) d* h0 V4 J: j编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
/ G' ]8 D0 C; { T7 D q( u9 A' W* F" R6 M4 C: K
SecAuditLogDirMode 0777
/ o3 `) l5 k/ C7 `$ H% i+ B# dSecAuditLogFileMode 0550
1 X4 t: a7 G, Q; z+ i" P- x, wSecAuditLogStorageDir /var/log/modsecurity
/ o9 N" Z( H' z( zSecAuditLogType Concurrent
4 L% t$ R8 |! m4 r7 B: f1 f参考文章:
, a4 {( j; F2 j7 ` I+ D0 ghttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" o! g- G1 U7 p1 {9 k7 [; C, s8 r3 W
http://drops.wooyun.org/tips/2614 |
|