|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。# M) h" Q. e0 [
l( f4 L1 m& B; x一.准备工作, k" M3 u; N* |- b& e: S3 @2 z }
4 I6 ]! L0 ~, N系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
, Z' @! g7 K4 }" \ S8 E* ?3 Q; u) f
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
5 M- v( R+ |6 K+ w1 s& D4 I
' j& ?, ~4 Z+ S5 r9 m/ T8 m/ Xmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz+ R z5 c$ a* A; `8 P% Y9 c) |
0 }# m: D8 K5 I
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs5 Y, y, O; R4 _ X6 }$ w
$ I0 }; o0 u$ I7 M) d2 c
依赖关系:- N! d* n) I5 ^+ [( H) }
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:3 h5 q1 O! T- J2 O$ G k# V# ~
$ U' a% B) Q3 y e
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel& Z! u! c0 q1 c3 U$ j
modsecurty依赖的包:pcre httpd-devel libxml2 apr% J. Y) R5 [8 m) V5 G
! v# R) {, ?# Y3 ?# C
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel8 u# E3 j$ ?8 d" d7 B
二.启用standalone模块并编译1 d0 j7 D3 ^3 N. W4 \
" I0 y1 H" O: z. I7 Y' n
下载modsecurity for nginx 解压,进入解压后目录执行:+ O1 C1 o$ J8 O4 o `) E8 U
' B- }9 l* Z- Z+ T" H
./autogen.sh
1 X3 l. X [/ U8 W+ Z./configure --enable-standalone-module --disable-mlogc
( Z. t7 e) L. m" s0 q/ k/ wmake
# ]& L! n% Z4 @& {; x三.nginx添加modsecurity模块. F# T; ]% P W6 @
6 V7 a7 w: _2 s" @6 f+ K在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:! X$ A" O1 |* t4 o- o+ f
+ |( F/ p% S8 x$ |- H: ~$ z$ w./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine) V, B: k" B* R: ~0 e. {
make && make install, A; j1 h" x- J# ?' ~
四.添加规则
" V: T* l6 n5 B
6 P7 n# g7 O/ v: ~modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。+ P6 W# s n" r q2 F; p
( P2 m+ x! t! m5 I/ A/ U) E7 ^
1.下载OWASP规则:
% {/ e; ?* [2 {
" D! k+ ?- L" l s% ngit clone https://github.com/SpiderLabs/owasp-modsecurity-crs9 Q3 f3 ]# N2 g5 G( o* Q/ }# L
K) m" b+ I& h, B' D jmv owasp-modsecurity-crs /opt/tengine/conf/
/ u- A9 _. b+ |0 Y5 E& \' V/ ]
: T! L( o D/ e1 a# Z* gcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
+ K6 ]! R) M2 E* k/ s1 e' c, Y2.启用OWASP规则:: y4 k) @! r, f2 R8 r1 [2 B
/ {+ k# ^4 |* g复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. ?7 t5 ^& n3 F Z
7 w* g2 ~3 A8 t+ i1 i
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on& \8 ]6 [! R* j0 w. l' z
6 Y; k6 u: c6 R4 Q+ x, nowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
8 _% V$ i9 O# o
; U6 P8 Z6 g* H. pInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf* Z( ~; N; B& b) ?& ~ B' G, l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf X0 A" h. \, P# ^
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
9 g* Z+ @6 O7 ^# v: S8 `5 [8 A9 c# wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
$ c8 _+ n0 t0 LInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
/ e* L4 S9 X vInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
0 g+ a" A& M, `+ TInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
5 B3 Y; a+ W6 A& ]五.配置nginx
- z6 ^- o9 Z* N3 F; a/ j
; P! ~& `5 G, D: U& ^在需要启用modsecurity的主机的location下面加入下面两行即可:
3 w' [; J; W- r% e9 G, Q
) X, I+ I3 f9 ^# J# {- N& Y6 Y. kModSecurityEnabled on;
6 g6 x) P& o V* D& SModSecurityConfig modsecurity.conf;
$ t' S1 g C6 m9 f下面是两个示例配置,php虚拟主机:, g& m& b2 ?9 X% e& }
( S' t$ X L9 i; [' Yserver {( e' l0 ~6 D4 X% r& P
listen 80;2 O1 P/ l) v9 i, J, c4 J
server_name 52os.net www.52os.net;
; ` }* n4 r+ d8 R- h
( l5 c& E4 l8 H; m* U3 s location ~ \.php$ {7 K! H! [. B9 A5 y
ModSecurityEnabled on; ( \- M0 B- X( c" b$ J) |
ModSecurityConfig modsecurity.conf;; Q( x9 Q+ v0 C& T9 V
& o" f% W: A" C# ? root /web/wordpress;
/ D1 j v. A( T& H( N index index.php index.html index.htm;
7 @$ z9 W5 k# y' R1 {/ i
0 A) }2 s3 Y6 c1 g0 F* h fastcgi_pass 127.0.0.1:9000;
H" u( P1 Q! Y9 P9 k9 e3 ~8 T1 c fastcgi_index index.php;
- P6 P$ M. P& s fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
" t" _8 }2 L. b! p% C include fastcgi_params;
* V+ n! ]. H9 M% T( e }5 Y7 ?! Y9 W, `
}4 H7 A& O; p, |
upstream负载均衡:
0 I) A- b; l" g; H4 `: V6 T+ Z0 @; O; x( b0 h
upstream 52os.net {* P4 e2 ?9 H! p& H" Q" P
server 192.168.1.100:8080;
0 ~* ]' S! C+ v4 ^* W6 G+ S) \ server 192.168.1.101:8080 backup;+ {! e; u, n/ H9 `0 S
}; Y7 ^9 @' ~6 t3 p* R
/ k! {; k9 s& |8 m, H+ x j& ]server {
1 x& v9 f( {8 S, x2 vlisten 80;
- ^* O$ J0 s, f* O$ f7 [! Qserver_name 52os.net www.52os.net;
5 I @0 d, |; T, ~" H
+ u& X7 b: u5 g; u# I' }* h! Hlocation / {
8 |- D- k3 `. n+ i1 c0 L ModSecurityEnabled on;
# D9 \! z0 h: m( x% ~6 }# W ModSecurityConfig modsecurity.conf;
6 ?6 X4 B1 h! p% c0 |$ l9 c: W a) z! Y
proxy_pass http://online;
7 r5 D2 E( J" I5 Z8 `5 s; h proxy_redirect off;
3 @- f$ F* ~1 f( B( N* z' g. x proxy_set_header Host $host;
" w- p7 |* M- }' t* f. } proxy_set_header X-Real-IP $remote_addr;
! [0 r# x. Q1 ~4 I proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# u6 V, }" ]" n }
% m$ l, W+ U% C}
8 A$ _5 I( c& M }: D" j六.测试
( F6 U$ o# S+ j8 G9 g8 j$ m
3 r; B6 E' w; j+ W4 M! X& Z我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& G, L3 M( _; h
7 L9 ~0 f/ T) [7 }<?php
# f, h0 e" \8 I" _- U phpinfo(); 0 w c l; B0 p. a" x
?>
. [. V3 Q# _0 I* X# h; z在浏览器中访问:
9 ]1 _1 Q5 W" n( x: |/ D$ v: A$ V& M0 u( z8 U$ _$ B
http://www.52os.net/phpinfo.php?id=1 正常显示。
* D: u* ~) @( Z/ e. {/ W: {& fhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。. N7 x$ q4 Z Z) @
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。0 d8 }, p" q: ]% V% m
说明sql注入和xss已经被过滤了5 f* F6 N& Q5 P& k' B6 `
- E/ J7 U# ]9 I七、安装过程中排错6 u$ k$ ]- {. q8 V7 s
7 o* w9 |1 q S6 A6 v1.缺少APXS会报错
8 Q7 O3 j, I$ ^% ~1 \) X
2 ] S. X' B) ~" \" l xconfigure: looking for Apache module support via DSO through APXS
. r1 |1 e# ?6 m3 s' B4 g( r) Mconfigure: error: couldn't find APXS
- @6 r1 n; W5 M& K2 w/ Z; ^5 Aapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。7 r$ n3 J) k- s! \
解决方法:
. s4 c' @; m% _7 T9 A$ p9 S$ r, t
6 g) I; P1 E( z" `3 ]4 P! ]7 R( Ryum install httpd-devel: e) w5 X. b$ e- _5 Z% H0 l
2.没有pcre
O( u/ I' E+ @! a& k& i' Y% Z: Y# S! l# g
configure: *** pcre library not found.: X$ \: i: p& w2 P; [
configure: error: pcre library is required
) a9 Y' g3 J3 c; Q* u* K- l: l2 ^解决方法:
( q; ]2 G1 V- a/ o3 H+ [# J
- ~# c: X- x* k7 A9 u b# Gyum install pcre pcre-devel
$ C) W: N7 Y' ~3.没有libxml2
( u% l- @+ W2 U* X/ Y. K+ t- P; X+ f8 j* }7 `- t8 b5 d
2 {8 f* B. m$ e# Mconfigure: *** xml library not found.. m$ w3 Q4 P6 r0 m$ R- i) P0 J
configure: error: libxml2 is required
; d$ L2 B! }9 {: s9 i- W k解决方法:
: q! I: S1 @8 H0 i
) _5 T" G' M1 P! b6 lyum install libxml2 libxml2-devel
1 S$ l- m; w9 T7 H! E' d+ }4.执行 /opt/tengine/sbin/nginx -m 时有警告4 C/ N( S5 }' Q- ]2 C: w
" R( I$ C c2 K" Z4 f) _+ a
Tengine version: Tengine/2.1.0 (nginx/1.6.2)( z; U5 Z* x3 D
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
- l0 x9 w. f2 U+ r* c; I9 h原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log9 Q: f: P6 }6 v8 M! i$ ?) r, D% |
3 u3 {/ w: M( j& ?2 P/ e5 V H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
) i2 ]. j$ z: B u& X% G! d) y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"9 K4 U0 i1 b$ y/ U" D8 [( g
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
. P* x/ s7 T' V/ D* d1 @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"% B% X1 {! W) `6 W3 g! @/ ]. B, R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"5 ^, X* Z K0 z& t% l9 L
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.6 V' |) t$ _0 e
解决方法,移除低版本的APR (1.3.9) a$ e, u+ ~+ c6 E
: N1 U* y6 t5 V# K9 B4 Eyum remove apr
. {1 \( L7 F) k% n* b. x5.Error.log中有: Audit log: Failed to lock global mutex) d% C9 u4 Z; `- q+ c
0 S5 c5 H* |: {2 w1 @6 b' T2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock / l7 h5 c* ]9 h4 `
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
n3 y& w" G6 e9 `8 e! h# @解决方法:
. m; f5 |2 T( ~/ b) ?2 M& v. f: t编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:" C5 o1 p. G5 w2 w1 |
0 Q/ V8 c: i& f1 r' Y$ v& D& |5 JSecAuditLogDirMode 07772 u7 c' E. v8 t9 w. b' F' Q/ j% @( [
SecAuditLogFileMode 0550
( g" m; I+ d6 ]5 H: c; lSecAuditLogStorageDir /var/log/modsecurity
. {. b6 g6 ~# n6 J6 oSecAuditLogType Concurrent3 s; _! a$ ~% C1 L
参考文章:
7 O' s- |+ a+ B3 [6 K# @/ phttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
" i3 w; v: Z/ _: B, P4 c& Rhttp://drops.wooyun.org/tips/2614 |
|