|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。 T: W: U( U7 g% m' ?, u8 e
/ U% L# R1 g- U9 [2 D5 `9 W一.准备工作; Y O! S6 n1 R" h2 V8 C( _
# |! g5 m* U9 s1 l- c$ A系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0! H3 T7 o( E1 n, Y n* L1 b0 Z
: T( K5 ~. D+ dtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz6 f6 q, i) y0 `. F1 u4 l" `" R2 o
9 J6 ~; F* I& m) z$ h A& `+ b
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz: N \! U) o3 X( Z3 I s) G4 O
3 S2 ]8 t6 l$ ]5 OOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& U; l1 @1 d' F; ]- O* y! }
" |3 o) J% x" x; Y, R4 L/ @依赖关系:
( v6 K: w0 E d2 s) T( l6 dtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
7 k/ R% g: Q: r4 g: n" n1 h. G$ s% J7 S# D6 V" S
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
/ \2 S0 }- g7 M2 v. umodsecurty依赖的包:pcre httpd-devel libxml2 apr
% @& t0 v2 p5 W% q* {9 z" B _% C6 {& P9 X3 n: T* n% t2 s( f5 r
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
$ r4 g, ]- D6 K4 }0 y' X1 F5 w2 s二.启用standalone模块并编译# h. M$ n8 i! m/ F
# q! O# K- u! `
下载modsecurity for nginx 解压,进入解压后目录执行:
, z- b2 ?: ~0 ~1 T) a1 [( N
; ?( u) b4 i' |* K4 z./autogen.sh- _ O: H3 U) o$ u+ t3 q, V; {
./configure --enable-standalone-module --disable-mlogc
8 _. k! Q) u. y5 r! k( u+ @make
7 L. {& @; i8 `! U3 P+ p$ Z1 L0 `三.nginx添加modsecurity模块
( ~' d$ y: q! g' _% b% S) ~, u4 Y4 K* a
/ m0 i4 Q5 [1 L3 _ }在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:$ I, ], J: U1 O0 V+ S: H) S
: Z/ F, @7 `$ t/ s3 [./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
9 |, ^- ]; i$ j$ D) ~0 Qmake && make install
+ e! g3 |7 P e# V2 v; Z四.添加规则
' k% F6 ~6 `3 h, O) i
1 M0 a8 Y3 a; Y2 ~; e& Wmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。+ x! q4 q0 W4 s4 `& y( u
' o i3 G& A a; b
1.下载OWASP规则:
$ b$ P& `' H% W' v! E* _5 J" Z( `" O! N6 A! n8 M+ s8 N
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs ^! i4 _4 ?$ S$ Y( Y: {
& a' U0 d; D" l8 Y
mv owasp-modsecurity-crs /opt/tengine/conf/: |/ y s1 Z; s9 ]; a' f
# |2 {4 ~/ `' ]) {, b7 S
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
) [! T2 H1 P- g1 [; Z$ u2 _- s2.启用OWASP规则:- d- J5 b1 I9 Y( V& N+ F
0 i, V4 [$ T+ J# X( w8 m6 `复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: D$ P- t3 z$ ]9 H1 S
2 J8 n) e. e* l/ ]' L) B' w编辑modsecurity.conf 文件,将SecRuleEngine设置为 on. |! h" ^& l: g5 H" f& F
6 E: W2 s( }- @4 r& W- E' q( gowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, N$ i1 k8 R4 T0 d$ E& L
3 s# y9 P+ H( y, YInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf2 }" l. X( L! v% S
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
2 `5 D- L# U* X G' x: wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf' a' Q3 A- U. L; H C( Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
) i- a# u( a- qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf- A4 A g* L& B$ Y) I' d; i
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf5 p9 L0 ]' F! D* R* c- Z
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
( o* M: z/ o8 X) M1 U5 c+ `五.配置nginx
' f/ P1 C7 h# X& d/ `1 f
2 R+ [3 ~* i' {% R. K4 _4 }在需要启用modsecurity的主机的location下面加入下面两行即可:
8 j4 s% r* L% ~3 l. _, {
3 j; N2 y0 T6 [ModSecurityEnabled on; ! @( T, U& F3 z' h3 s% J
ModSecurityConfig modsecurity.conf;
3 I+ ~ J% K' n" a% V4 ^) E下面是两个示例配置,php虚拟主机:
( H* l" L5 h# Y" Z( ~5 | `# v4 L
$ b# _% `0 Z2 j, ^! T1 tserver {5 E; V `8 t E! G: R/ Q+ a
listen 80;3 L, }% F6 r8 l7 Z) Y: {
server_name 52os.net www.52os.net;" k5 [" h5 j9 a# @5 q" T
6 p0 ^1 |9 P3 g w- B2 l
location ~ \.php$ {# ?; b6 A& P' I9 r5 |6 v+ @
ModSecurityEnabled on;
: D3 R( u9 ~6 ]# v$ x( O+ N; [ ModSecurityConfig modsecurity.conf;
) e6 R4 L. w4 F* h
6 Q: z$ E& B+ G( x4 s. d root /web/wordpress;
L0 v* m ?) C0 u4 m7 m1 h index index.php index.html index.htm;+ X- Q/ q! E' m( m
% K9 G+ h$ p; g! ^" ] fastcgi_pass 127.0.0.1:9000;3 |2 a L9 ^& t! I- k
fastcgi_index index.php;
! [$ J! z; H+ H# O9 l fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;7 l2 I0 {$ H- M' }% C2 f2 L+ ~
include fastcgi_params;& B, k) _1 y7 i" K) _
}( V( @4 C3 H0 }! i% D) {
}
- j" F7 _9 L" f# y' h7 H. } q! j) Y0 o4 {upstream负载均衡:
$ w+ u* t8 k' s; `4 Y
/ |' i. }3 r/ m9 j! l, t6 Mupstream 52os.net {3 E9 J3 T2 j3 L% J2 Y: S2 B% R
server 192.168.1.100:8080;
7 e' Q9 ^+ D" t: m& k server 192.168.1.101:8080 backup;1 ^+ B+ M" e- L4 }) u6 ]% k
}
& g# o8 i7 ?- k0 d4 J5 h
8 o! h/ |$ R+ u* j2 j+ ?! I" cserver {% u* `1 R* R6 q
listen 80;8 {1 o" U& u$ ~/ T
server_name 52os.net www.52os.net;
. Z" y( X" s- _2 X& b. @* U& G
- ]3 B9 A. l+ B( S, {$ ?) \location / {. [4 ~8 i+ i6 ?' M1 x* B" K
ModSecurityEnabled on;
! P5 _! H4 s: c3 x& B E" n ModSecurityConfig modsecurity.conf; 3 T7 C; _+ X# u: K7 Q1 A# b
/ _. ~( v8 L! R: `5 J
proxy_pass http://online;
1 z& u; U1 T. D proxy_redirect off;5 ]! ^# E" r7 ?9 E
proxy_set_header Host $host;2 x. i; S7 s6 p2 K/ x6 c4 X
proxy_set_header X-Real-IP $remote_addr;
# }; a# ^$ x- U proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
6 G" e( d( F X& O# U }; r% L" e( W4 T$ @# z# `
}
; r+ e! f7 |* ~1 H; Q0 l六.测试
# @' C1 k. B8 N+ D6 \" f1 ^
! O0 z) Z6 S& y( H我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:: v8 T9 t1 H6 U2 C; g5 a- {$ h1 L, K
& y5 K" X- r1 D1 l, Y: W8 r- k2 [<?php6 S/ s, Y6 h$ h4 G, d
phpinfo();
& h9 R4 B* R* D. X/ J% D?>
* M9 q2 B& I/ @3 p' w$ F( C, V在浏览器中访问:' M; M6 M g8 Z) }' V" Q
; Q: b* @+ H! h& F
http://www.52os.net/phpinfo.php?id=1 正常显示。
+ J1 _* L, {- N6 N) yhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。6 B& p* i4 M) y9 z) p0 j _* D
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。( A8 I8 g* b3 Y2 h" |# h
说明sql注入和xss已经被过滤了
$ ~* Z# Q% q, U5 @- V3 U& s& n! R6 d0 f N$ \6 f+ r9 r0 S* H* h
七、安装过程中排错
6 y5 r* ], l! P
8 I+ A$ i; c7 M- h1.缺少APXS会报错
3 p( [! ^7 S0 u* `+ D& }8 W4 U. T5 f- p4 e- b! o3 @* H- w( x
configure: looking for Apache module support via DSO through APXS
) i. X/ T; j( h, bconfigure: error: couldn't find APXS$ V- s9 ^2 k F4 X& k
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
% ^* }: d8 O% R# Q6 K- O \7 u解决方法:# O" U: d+ l% ^* u' E% [7 q
6 w7 k% |$ [7 L8 p5 |5 v
yum install httpd-devel) R" D& O) X5 y) n" n
2.没有pcre
' J$ N7 G; }/ D& f" _" |$ V- A8 d
6 W4 c2 ^2 n2 w8 C7 J" Cconfigure: *** pcre library not found.( I N2 c M" k+ s! n3 f
configure: error: pcre library is required3 V6 T' L2 w9 z
解决方法:
. T9 V6 c+ {9 z) S2 z' r$ ? A0 `2 N1 K5 H2 v% p
yum install pcre pcre-devel
U. [! O7 e# L3.没有libxml28 P/ A4 w$ ?9 g) z- [" p! L
7 K; u6 K* m# l1 b7 d z4 P% F; I
% Q' H" {2 \- M2 k! a6 o$ l- q9 n/ Dconfigure: *** xml library not found.
- j% \/ d7 `9 h3 O$ Iconfigure: error: libxml2 is required
! L2 C; P0 V6 D6 ~" q4 Y& m8 W/ |解决方法:$ V. ~5 i* X, S# k' t0 ^# w' @
4 u9 }1 N8 Z: Cyum install libxml2 libxml2-devel) N* z/ ^5 R B; e4 F
4.执行 /opt/tengine/sbin/nginx -m 时有警告9 O. `9 F1 E+ D8 j! p
; S% |, e( `% ATengine version: Tengine/2.1.0 (nginx/1.6.2)3 ]. ]5 f1 A) O8 }9 S- d: A$ r
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!. {/ B! S' P1 d% g6 K( ]: d! C
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log, M: ^ m5 L! h' U! A6 C* w7 \* W
* t6 d7 p: V: O/ [7 y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
. ~ Z7 b" g4 M4 C) G8 G. d" h( V2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"% e8 W6 ~) A n* f" F
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
4 |" W1 @1 I0 O" h6 S! I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 d* ~2 E" I9 |+ U2 r `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
: U3 r9 }# i8 u) d% X/ E% \2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
" T' B# K$ h; H. x: {7 z" v: v H! j解决方法,移除低版本的APR (1.3.9)
* s1 g! ]3 J# P* e6 @, i" P
8 Q4 E7 |* p Q: G. ?1 hyum remove apr
9 C5 O- V8 o! x8 l) g5.Error.log中有: Audit log: Failed to lock global mutex
3 @: S: f5 g: Z3 M) J# \" D L7 `5 h
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
4 y; ?9 Y4 P' o8 b5 `% @! Qglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
% S/ Y {3 C9 V) a7 b解决方法:
" T1 J+ o6 ^( q6 W! K- M( n编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
. X* z6 f# E8 ]3 H; }- H3 n
( I5 q% Z! @ ^) g5 FSecAuditLogDirMode 0777) G9 O! r' O9 s6 D* d' b
SecAuditLogFileMode 0550
/ t; B/ P# Z; p M8 KSecAuditLogStorageDir /var/log/modsecurity
) c. R1 E/ a- \SecAuditLogType Concurrent
" `" _# u. @6 b参考文章:
% ~* {; z* j4 h& ehttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
3 d% T& H9 w1 L+ V0 @0 ^http://drops.wooyun.org/tips/2614 |
|