|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
9 N6 G' Y% S' K+ A+ C
/ |. @3 g" Q0 Z# O: ?0 b2 J( t一.准备工作
5 A. v, L! e/ q- X7 P/ H; Q' s% J
, c0 k6 I% E$ S系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0: g0 M) `0 F$ S9 M
& D9 [! @" C! l7 W1 ?) ~tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz: O! y. b1 A0 J$ H. ]' ]" k
3 `- f6 d2 B* M( [5 Imodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz' h i6 @, `* w( ?- R: e
, u3 R: N. r" b, L. ZOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
$ D4 [5 I7 h8 a& E% |9 i" L( R& p) I) F, }6 u* O
依赖关系:3 U1 [/ {! D+ N& W& j
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:. f( j2 {3 @7 `, u2 O9 s6 |% C
- q$ ^5 y) {- T1 Q' u# z& ]8 Oyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
( x8 d" F; i& e+ D- h/ v7 _modsecurty依赖的包:pcre httpd-devel libxml2 apr5 X: H9 i' B1 O" \3 N& P: I/ {
W) r2 \6 Q# T& B7 B& d( fyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel/ _+ C8 C1 J& |7 N( P
二.启用standalone模块并编译
3 v) |, U" x5 U2 _" s; p6 J
+ C% h' K) U2 t( X/ w8 x H下载modsecurity for nginx 解压,进入解压后目录执行:
& c; P, F) O% k/ ?& R* x9 M' |4 s+ V" |* `9 J& m# G
./autogen.sh
- b8 |0 S; T. \8 d) O! L0 ]./configure --enable-standalone-module --disable-mlogc: H! s$ v$ N% s$ y. E0 N
make 9 B5 I. s& I1 F* `0 E; L& Z- ?
三.nginx添加modsecurity模块
2 x6 q7 x, q+ S. E
& T% p& t, ?3 ]: I- @3 Q在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
, u( u& q+ t9 x; A8 d8 E0 ?2 V: |8 v2 \. w# [7 v. W& U7 T
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
$ r, Y, j v$ nmake && make install
- n7 S: @; O/ A' p7 |+ D' h四.添加规则9 q( c4 a6 o7 r+ R& D& a7 E" j" ^
1 a/ M& E* m2 I& ?) o- Smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。. T9 J! l- \( k7 j8 [" T
) D" P/ R3 v9 X& W0 B( }1.下载OWASP规则:4 Y6 ?! e# e; i6 v- d
) w$ ^- M* |) Q A4 J' t) B- E y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
. I# f4 o( E$ U2 w
5 F& @) c/ F; x* Xmv owasp-modsecurity-crs /opt/tengine/conf/
' Q$ t. O i% H9 I8 B! x0 o
* a. d, O9 C; [; j4 [3 g6 [+ icd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf& F7 N, Y' k/ [5 u. b G
2.启用OWASP规则:
9 h7 @0 G3 Q+ l4 [
& ^! N4 N6 a7 t3 L" ?1 b( `9 \复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& P/ Y" q: @# D2 x4 @. A
/ R3 a) M/ Q4 u, k( \* A. `% x, F( ^编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
5 i0 t6 S. \3 q0 ?2 x; t
* e) _8 J+ y' E6 L; ?( d4 ]* ~owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
7 N! @8 x( M+ [+ {1 u5 Y. w3 J" \( b7 h9 z- v! v3 a. P
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf. ^ b: g: i4 E1 M8 w
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf2 S8 C2 G5 X! }, z G/ c* |
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
/ U/ h; F2 D$ d+ sInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 `+ R W% k8 h& gInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
+ P& |" P( P ?9 ^* hInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf6 s- E8 ^* a$ E
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf% s: n( O0 b/ M3 l8 e) J0 K
五.配置nginx. A; O5 ~( i2 w8 ~; J6 u
+ C; |7 D0 [' {" c. q. Q) q6 q
在需要启用modsecurity的主机的location下面加入下面两行即可:0 M% ^0 S" L P$ b8 g# w
( y( V- W0 v) U4 U! i3 ?# m
ModSecurityEnabled on;
$ h) u9 j# [, W6 `' XModSecurityConfig modsecurity.conf;
P5 B% `' m: _下面是两个示例配置,php虚拟主机:
y/ f& m. Q! u3 g: z& _5 H+ s5 Y6 i: o8 e0 G* }
server {
9 L; ^' Y$ f7 Y& `: ^+ W listen 80;
" w9 @6 D, c- E server_name 52os.net www.52os.net;6 m$ G9 _- J: R J/ \
& _: o4 p Q( E( ` X' T
location ~ \.php$ {! x: W7 P: I) B0 a! R
ModSecurityEnabled on;
% v/ |: m. c% b8 C8 |7 Z9 \ ModSecurityConfig modsecurity.conf;
1 x8 K; b+ V# x$ ^; D, K: S
' Y+ o9 J0 ^: E; g8 r1 G root /web/wordpress;
+ m- @& `, b3 q* x% k index index.php index.html index.htm; v, H" Q, W: \/ a Z% B
9 j4 o8 A, G) |' g; q, X. U
fastcgi_pass 127.0.0.1:9000;
- T' ? `8 K; B7 {2 R# U fastcgi_index index.php; z. |+ G u( p1 u
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
; P' `5 @) j7 j6 Z+ T b0 ]+ p+ O( l include fastcgi_params;( q# _ E* s0 H1 x m4 D& p+ S- J
}
J$ Z! T5 ?# B$ t! ]% v }
8 b r8 [7 K" K [+ Mupstream负载均衡:
J6 q, p; F: t& L/ K: ~' M. y) Z) B8 P+ ~, l: m
upstream 52os.net {
; `5 f6 e% T8 Z* [ server 192.168.1.100:8080; }9 n+ W" k% o
server 192.168.1.101:8080 backup;
" ^3 D; ~) T$ W% e' B}# }+ g. u6 j- T; h# W
6 ?! i! L! E# ^4 g- w0 z" T
server {
- Y) f" l1 ^" M/ R+ `+ r6 |listen 80;/ z2 x# v3 j3 c% B' V6 _5 J& H
server_name 52os.net www.52os.net;3 |9 d2 [1 C- b
! C$ w& U/ ?8 _+ F4 T/ {location / {
' \& N+ j- u3 P. p ModSecurityEnabled on; 4 x5 J3 y! B; k N3 B
ModSecurityConfig modsecurity.conf; + ?0 H" D! g j( f. {0 h
, O3 r. f& s0 T ~! u- p' Z6 j" | proxy_pass http://online;
4 c9 Y @# w+ ~ M proxy_redirect off;
) ?0 L1 B0 q) }% \3 g2 t( Z. b# { proxy_set_header Host $host;/ j2 K8 n* r1 c4 A2 x
proxy_set_header X-Real-IP $remote_addr;/ P" A7 o9 x- z% s
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;4 H2 I3 L* ?: O6 I# x, y( T
}
9 S$ k/ T" t$ ]( n! n}) ^$ @- }. x. A) O
六.测试
1 I" i* L% ?9 p, D* _7 H) X0 _0 c2 ]4 g- h
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
: h4 M6 O C' B$ U' J0 b: @5 `' u6 v9 V. h, h! N2 b5 u
<?php3 y8 J |5 o+ v" o6 o, K' {9 t
phpinfo();
2 g3 N9 b. ?& W1 G, y0 R: _# Z5 e& |?>
0 [/ E! P1 C j& a& o% q在浏览器中访问:
7 \+ x" n0 Q6 f1 u2 i5 h& X u$ ?) H/ m
http://www.52os.net/phpinfo.php?id=1 正常显示。
3 w3 B+ P. v# \ {. r3 ]http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。0 ^# m% _0 T+ I# P) F$ a8 }0 w Q
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。" R1 e% t( i4 w/ U" |" }" ` F
说明sql注入和xss已经被过滤了; F# }* v& p+ W" ]( a# ^8 n% Y+ r
) H( b/ F* k* e3 Z7 f( ~
七、安装过程中排错
; Z/ {& n8 a; A, J3 c" n' H& z/ ~4 w Z: U; L
1.缺少APXS会报错' g3 Q$ r' V: [, G6 U
( G& F( B+ T- f
configure: looking for Apache module support via DSO through APXS
6 ]" w) a# L9 D1 \# qconfigure: error: couldn't find APXS; ]: L- K1 C% U9 d
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。. {' A' B: L+ n
解决方法:
$ _' ?1 r6 O/ h' o) r2 D* j
8 L/ b S3 T2 g5 S+ syum install httpd-devel1 P- R# f4 ^8 Y7 p2 _0 _. K
2.没有pcre7 X4 H W2 s( m/ A& F- ~
# H% l, I+ Y: U4 k- e; T2 V# G
configure: *** pcre library not found.
3 j: t. K+ P+ fconfigure: error: pcre library is required
/ \' \; A. Y8 x# H) O# H. z0 t) A解决方法:, R; v4 B2 a( d- c8 }7 J% I
" C1 `' ?" Y/ y8 l
yum install pcre pcre-devel w5 B& r5 A% l4 i0 U! ^
3.没有libxml2
* M& T/ g: b; l: u: W- S. d5 ^" q' Y: E' ]' p+ D3 S, ?4 B
$ I9 p6 L% ~7 a/ f9 Z# S* d
configure: *** xml library not found., e8 M( j7 H8 `0 i2 a
configure: error: libxml2 is required
* h( T. {8 U) E" `解决方法:' ~. J$ W6 g6 M4 N" D2 [' L
, ^2 }5 @; j9 j n
yum install libxml2 libxml2-devel; f8 m& c, a! q- h8 a3 ?* q
4.执行 /opt/tengine/sbin/nginx -m 时有警告+ r4 n! y/ K+ ?) ]+ C
3 J2 X2 Y" N4 j5 [3 G4 c
Tengine version: Tengine/2.1.0 (nginx/1.6.2)9 K2 \/ g4 k0 n) j" A+ c! u% {2 |
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
' j; b% l5 M8 K2 K) W* W: B原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
4 b1 n9 Z! d/ x0 L
, L7 [6 {. R6 O% E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
- U/ ~) v3 r. O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"# Q$ ~/ f6 D, Q6 R, \
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
& W, n/ V& X0 O$ m/ f: ^, s2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
/ n$ l5 W* R% j1 b" U$ [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"" i6 C- N2 s7 z. m( n# f9 A$ L
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
/ N+ ^" J' L2 f- Y/ F解决方法,移除低版本的APR (1.3.9)
# S' X7 M6 O* \$ g# `1 }+ g3 V- D0 O. c! ]6 S
yum remove apr' e9 _: I& `' c0 |) ?
5.Error.log中有: Audit log: Failed to lock global mutex3 h6 F' E6 J2 ^
, I& ^' f8 x6 |+ v2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ) K1 n! _" a2 A: m
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]1 M% t# Q: }, K, ~: n
解决方法:
8 M# |3 d+ S" D, V编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:% q0 q$ o1 n+ B( k4 X- S% A
7 d5 v4 a" O$ M9 L; d' {SecAuditLogDirMode 0777 R/ a* g! r/ X. Q
SecAuditLogFileMode 0550
0 L+ V2 Q; q) [: CSecAuditLogStorageDir /var/log/modsecurity
+ S Z# ]- X5 X& t7 r9 p eSecAuditLogType Concurrent
* f! w8 v/ M' s# ^+ f0 k参考文章:
6 Y4 G* _7 A& D; q4 phttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
* l5 [" X* @! T) u4 Phttp://drops.wooyun.org/tips/2614 |
|