|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
9 M/ ]3 B0 q, y g6 r
, T" U' l- ?! M4 s( J一.准备工作
8 Q) m' @; d1 S" p2 y9 i% ~! c' ?. y) L, g( F" C
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0- L; b. Q, j# r2 `
* P9 c' k J- [% U* [8 T
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
# S( w# B0 ~( t3 B6 F
, o4 S+ }' [9 E0 m& Fmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz( @2 w8 J: X$ ]1 ^& ]: q ]" u
) D8 O( X+ j4 S
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ ]+ a/ |( y( G5 g, l; T* D" j' g3 D
依赖关系:
; J) E% N" u) Mtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:3 f; l0 S4 S. U0 r9 D" ]* C) f
1 q: \+ D4 i8 r6 k6 ?6 q* Tyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
# {( ^" E3 i7 F% h' bmodsecurty依赖的包:pcre httpd-devel libxml2 apr6 a$ x3 i' a3 o. I7 g% B
- e J& _% i: F, X/ v6 l0 o1 \
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel( l# W s9 w8 Q3 ~8 Y$ j" v7 O
二.启用standalone模块并编译
& u9 v" ~9 O7 f. h! m% ^. a8 R
# y8 k I+ S# l. j* s下载modsecurity for nginx 解压,进入解压后目录执行:
. o8 y+ v5 ?5 _% w7 w/ U9 @9 ?4 w& S% m! W+ _. X
./autogen.sh
; L% G2 u0 e3 e5 h* {./configure --enable-standalone-module --disable-mlogc
. o( E/ d, o' r6 G0 ~make " Y: s9 l% s5 U/ ~/ C
三.nginx添加modsecurity模块
6 @7 l4 r0 a" X2 _1 ]' v
0 H8 t4 o* x6 R' x5 l7 r! f( J, _在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:" Z! G) ^% }/ G; g1 }; |* f6 \
9 T# E( V! M2 y. }! k0 Q0 b
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine6 x, V h* O( [% X9 t9 G
make && make install# m; U8 z4 D. i6 Y9 ]$ N
四.添加规则# ]! t6 u# S" ]
: b+ ` t: T8 B' F2 A
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。 ` b+ c( q& h5 N7 |- P; v
|, Z/ G1 y: a' N
1.下载OWASP规则:
! K2 W6 [! Y" Y, G6 X
# X8 A6 `3 U% W( N, B! zgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs( `) ^6 @$ y% Z/ d# J/ a3 M, U
7 s. m9 e6 y7 U* v' z6 e* Nmv owasp-modsecurity-crs /opt/tengine/conf/1 |* a, k7 r' E/ p$ ~* o9 Q+ ]1 t
# {: k B) ]: |5 T7 J" {$ zcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf: ~0 q' B1 M6 h- O
2.启用OWASP规则:' z2 J9 ?" I' l/ d# E
0 |! Z) e: e$ o, J6 X L复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。" T- @9 d1 X. {: G* z8 d% H
1 d! g8 i" A; n# L6 n
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
% @. Q2 J; a. l. _# M' ]2 J0 z7 B1 `2 D6 O% L1 l! c: Z: O- k
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
7 w3 W" w0 G8 q, m
1 @% h6 }7 e& i; t8 oInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
! v# ?1 w. ]( Q# B1 \7 V/ `2 d- FInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
7 M! C- X1 Q2 ^9 KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf0 S" d& L. B f- l2 ?& m5 i# }
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
) B. d/ H9 {5 Q7 ~Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
& b4 j$ @ `8 mInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf9 x1 q7 w# S/ u2 Y# q U0 t- Z
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 d3 W" p. F# t$ J) |( Q; S! a五.配置nginx
- d5 d/ s+ b" y% K& C
+ \# w8 V) z$ |2 B: j. H% G' m! f+ Z% F在需要启用modsecurity的主机的location下面加入下面两行即可:6 Q( u5 d7 Z2 l, T
* |" ^& C5 o) N
ModSecurityEnabled on; ) H: p& s! @% A+ q& M- R
ModSecurityConfig modsecurity.conf;
% { ?0 `$ L: H# Q+ H* \下面是两个示例配置,php虚拟主机:2 ^& _+ v7 h5 k
5 A& p+ @, ~. n5 n) I. u" p; i* wserver {
$ u6 b# F( @& [- V2 e listen 80;
! _! O; r9 F* h+ q" D) P server_name 52os.net www.52os.net;3 h8 c, o1 M, s
3 M3 k, C# `. r7 n location ~ \.php$ {
, M5 E" `. S( @8 u4 x ModSecurityEnabled on;
- T# U* n+ k2 ~. f; {/ a" E( F ModSecurityConfig modsecurity.conf;2 ^- f* q$ o6 V# y0 Z3 Q
) Y, z" ]$ {9 Y root /web/wordpress;
$ Z/ U D' B0 C' u4 K9 i* e index index.php index.html index.htm;
0 h$ M# I# o% n @ 3 F0 r( K9 f. w; P. N- ?
fastcgi_pass 127.0.0.1:9000;
: A4 `# B4 y/ `* ]# C" m fastcgi_index index.php;
* \1 b+ K% Z. d fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;8 n/ l: T; L* W8 X' S
include fastcgi_params;$ [5 I# ?7 W' [
}
6 H5 t5 S: F! g) X; z& U }
2 L+ e3 |8 g5 g& D+ `upstream负载均衡:+ k' W; ]5 R. e/ v3 \. F
# t4 N: S' c- d" Bupstream 52os.net {
3 ^2 a& S9 E' j7 y' r server 192.168.1.100:8080;; m( Q' [* R- M3 Z/ }8 T. ?
server 192.168.1.101:8080 backup;
6 w8 o4 V$ V: a}
8 ? a6 \1 [* h
6 A1 | W5 K) h) cserver {7 a0 T y$ ?+ y- C0 f9 c
listen 80;
" L( l8 ?" E' F+ l( h5 tserver_name 52os.net www.52os.net;' h5 y, o4 g5 b* A
# o, K) \3 G* S3 z V
location / {
& x0 e. }4 E! V ModSecurityEnabled on;
2 I: G/ W1 F: C P/ h- }3 | ModSecurityConfig modsecurity.conf; C1 A5 {( ]2 _
- [8 e' @& K9 \6 h proxy_pass http://online;
" M/ D( M0 t3 k" z8 _ proxy_redirect off;8 a) F6 \. U- R
proxy_set_header Host $host;2 l/ L- C) \# z8 W P
proxy_set_header X-Real-IP $remote_addr;1 }# V; L( S; p
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;& c: w* {; H% E+ T! m
}
3 {9 X! u+ ^# U$ {9 \6 }# X" c}
! U1 H: Z6 G2 E2 N. ?六.测试
4 }! q& _7 s, e
7 P$ G$ c8 Z/ C2 I我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
/ x7 ~- L6 U' O5 ?
$ t+ n/ K& r# \% W<?php7 G. f" O' a( M' h* \
phpinfo(); * e* n: i! S! Z" V n6 l6 V
?>& e1 }' ^5 J# z$ {* ^( ], E4 {
在浏览器中访问:1 K& M% ?1 q0 \: Y, m0 s: \
# c6 t/ p0 e2 o, I mhttp://www.52os.net/phpinfo.php?id=1 正常显示。 m4 W2 w! C8 j Q+ d! u* `
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
3 Y. a$ K/ U) A( k, ~/ r: U$ Q4 Uhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。, Z4 S0 {% Y! N/ G, a. H4 ^2 E
说明sql注入和xss已经被过滤了6 g1 }5 x- n8 N6 R) Z0 P
& U# ] V# P# q$ {七、安装过程中排错
* o% \9 E( K$ Y+ Q1 ?! G/ c5 g! e! X
1.缺少APXS会报错
2 t0 n% M' o1 U( H4 B" O5 ~5 }/ V: x7 N4 k. f: r/ g
configure: looking for Apache module support via DSO through APXS
) f+ _$ d+ n5 v" gconfigure: error: couldn't find APXS
# T. |) ~, D3 f9 S+ n$ _( napxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。2 L+ U# g1 b2 C2 }2 t4 d/ |
解决方法:
" w' i* E4 G7 z0 { u) R# \
& m% s, \& o( @ O9 W K# uyum install httpd-devel
5 [- t, w. x: t$ m2.没有pcre | C6 c/ c! z# P
5 I% d# U# t" D, I. r5 ^configure: *** pcre library not found.- k4 Z5 d1 X4 T2 O
configure: error: pcre library is required1 x7 }5 y1 H( O; z/ A
解决方法:
: B& C( R) L5 ^/ J/ M) ~
. L' d* Q1 S3 s6 _+ T6 o3 P9 yyum install pcre pcre-devel
- w2 b/ e+ J! s5 T/ _2 W8 t% r3.没有libxml25 ^" B' E' j3 R% J: V
& K0 W1 F. ]3 Y% Q( ^; |. Z3 r6 y$ C+ W j! D. t6 q
configure: *** xml library not found.
1 M; R' _ I# @0 Vconfigure: error: libxml2 is required! Z0 ~ Z R/ A5 _/ g0 ?+ E
解决方法:. v: h1 {4 M6 s5 L
% ]* E- E" k) Z7 L9 F/ eyum install libxml2 libxml2-devel
% E1 L* h8 S% e" c) Y4.执行 /opt/tengine/sbin/nginx -m 时有警告4 D6 `, ]) k Q
7 ?; c" R6 L0 E" yTengine version: Tengine/2.1.0 (nginx/1.6.2)
7 b7 L$ `8 x% \' H3 N; Q" o+ cnginx: [warn] ModSecurity: Loaded APR do not match with compiled!& E; ? e8 P+ v' ?. [
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log1 l1 Z$ [$ q8 M) ]' ?
- N. B+ [6 E/ c" M+ g$ T' H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.& ^ B0 n& H* P3 A, O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
' |* p. l: P+ ?2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
; ]; Q( Q3 @ y. J0 y3 T5 W) _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"# { b, Y, q7 L9 X
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
7 s; O6 }' L0 B" E# V2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
# I; ~, j9 v/ B" ~解决方法,移除低版本的APR (1.3.9)% v/ Y: C* s9 \1 X p' K# Y1 C+ g! @4 p
% c' h6 ?$ {" T: w1 U- H' e. Q5 O
yum remove apr5 U& Y1 W3 n) O5 Y
5.Error.log中有: Audit log: Failed to lock global mutex
1 v$ M) U% ]) r* Q# N9 S+ X* h6 `, L1 q# c" y9 F0 Y9 W
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock * L& g O @& D2 v$ X8 y
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
. g6 F0 B' `1 I* a) L: ~6 P解决方法:, o6 C& I* t5 p$ c u: m; \, {
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:3 G% F! W- ]* Z' S( B5 \5 m
, I# J# x9 c: w* V; Z
SecAuditLogDirMode 0777: ^/ m | ^4 f/ l6 |
SecAuditLogFileMode 0550
( c7 C; Q& g( a5 xSecAuditLogStorageDir /var/log/modsecurity& a' _; y: Z- w* m# X
SecAuditLogType Concurrent
4 Q$ a! p6 B' J参考文章:
* o1 e0 y7 T- y$ _% V) @- rhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" q3 ~ o; _( F, i% p
http://drops.wooyun.org/tips/2614 |
|