找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13237|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
( L  n6 D0 t- l7 y! q1 t5 Y
3 g1 R5 P& v! q3 P$ i一.准备工作
6 i4 u; f1 L* I" u, y. |8 \: ~, e( f& D# b  ?9 W" |  A4 d
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0$ G. T0 \# M" {* _+ [
2 C. L' A. b% n& j8 \/ p; w
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
8 g# \) l; t+ A6 {) X: T
" [5 U) y' O$ i* gmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz$ v  S- B) y, x

: x' s% T9 a5 w2 Q* V8 k) z+ nOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
  ?+ l7 T/ E2 _" ?5 p& \: X. c
* h2 G; N" z/ p' }, d  O" T依赖关系:+ v1 @! z0 D5 ^6 f4 v
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:, g0 x4 p; S$ J* J5 r
0 i% t+ L: @1 W7 T' I, r: m& D2 B
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
! l  q& `0 B* ^+ w# G# Zmodsecurty依赖的包:pcre httpd-devel libxml2 apr
5 E4 }1 x$ D$ {; ~% L5 g
0 Q/ d8 j4 N8 W4 Uyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
8 Y: |$ S, p. ~, R$ x: S- n4 e1 g二.启用standalone模块并编译
& b% `( u/ V* D3 o4 r& D* c2 J0 ^) u" w0 c0 L
下载modsecurity for nginx 解压,进入解压后目录执行:
& D& O5 o1 {' Q# q) H
" U, T8 M- T- j+ ]0 o; R./autogen.sh1 b7 T! b* V0 K5 H: D
./configure --enable-standalone-module --disable-mlogc5 ~) e$ e% F4 S- L* y
make
6 l" E/ A8 x, F' V三.nginx添加modsecurity模块# ^6 c( i" P+ B4 r# I
' j0 c7 J" v; h2 E' ^& v
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:/ Y* M, P6 t/ a# Q2 ]2 i& m
! q- u  s/ A" z1 `0 x
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine7 {! e. H% w0 g. K, W
make && make install( h8 o% s) Z& ?! u
四.添加规则, z5 y; y0 K0 F  R" e1 C  q5 R# N, k
- }  ?* t# V5 p! {
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 h" |5 Q5 X$ d8 l: D
; _: {+ ~; |, Y' r* J1.下载OWASP规则:
! [( H( ^  {) a2 }4 h2 T; P( q- y  e! X2 U: r+ Y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs, B  S( [' ^$ K- C
; t* P9 Q- i% K, ~+ e
mv owasp-modsecurity-crs /opt/tengine/conf/* O* x  I) o( b% z4 X
9 k+ C# X+ E# r7 K5 E( K
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
! I1 T, s/ K7 H* t2.启用OWASP规则:
, c) ]$ Y5 `% v, K. F" B3 o
8 w% X5 }+ j' g: E+ f9 k复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% g1 b8 L4 R; c$ e9 g$ P
# c1 u9 C1 h, x, Y
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 j* F8 c1 r; b

, x' i" J) A! V$ F$ x6 S! }: H' jowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
9 h2 s( o$ L! [; P
. u5 b5 ?# R7 [# qInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
9 g% o- h2 t- z$ H$ }1 YInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf, Z( S/ a3 ~6 y3 y- E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 j5 v3 ]  R1 g+ \) S9 J3 g$ lInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
2 {4 |7 l  ?& E% s8 o9 x: J/ AInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
" K' V  P. q% Y0 c/ MInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf- q+ T7 C! e) ^1 K- y. C6 s
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf! V% p, ~& c. V# s4 c( f
五.配置nginx
$ G# L: C( v9 e) p/ e7 S% k$ p6 ^& A! ?9 x5 V; s3 q; h9 m
在需要启用modsecurity的主机的location下面加入下面两行即可:
7 }4 R6 t* V" Z- h6 K/ W! @' E' h1 ^5 @' T
ModSecurityEnabled on;  3 V: D( T! b5 g  |; w
ModSecurityConfig modsecurity.conf;& Y7 U) |  ^& _
下面是两个示例配置,php虚拟主机:& p7 n4 @- E/ P

' Q5 F7 A( z3 _, k2 a: F1 A: Vserver {8 G4 }+ @+ N, Y+ s
      listen      80;
) v3 A1 |- |. J3 t      server_name 52os.net www.52os.net;
9 G; F1 R1 B/ B9 f     
7 F( q/ l: n! k6 y0 L  d$ `      location ~ \.php$ {
) K: Q" r* j6 t& A      ModSecurityEnabled on;  
0 M4 i! V. h5 L( q# x7 A4 }      ModSecurityConfig modsecurity.conf;5 w, K7 _" p7 {1 ?1 a" q! ^, {- d# t
. O! ?8 A# i# x
      root /web/wordpress;
% ~7 O$ M* A5 W8 t  u; |- R* ^, A7 v      index index.php index.html index.htm;# A' Q/ {6 U1 Z. e5 n; M) o2 _
  
% Z2 F* E: t- K! N8 n      fastcgi_pass   127.0.0.1:9000;7 B; }' g* O3 v1 S9 F  D
      fastcgi_index  index.php;
& W' {. C* c  \' O      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
4 }  v9 Y9 b# X/ X( ]5 k3 ?. b; Y      include        fastcgi_params;
% N5 p; p0 O. k5 y1 R) `      }
: s0 z# N7 s& k  }1 V& Y8 W5 }" P) g3 P# A* e
upstream负载均衡:* p8 h9 e. X! J2 ^4 g9 Z

. H, u6 |) @; J0 pupstream 52os.net {
8 G2 v0 e. z0 P, b    server 192.168.1.100:8080;6 I7 c* H" c; ^8 {0 [' c0 N
    server 192.168.1.101:8080 backup;
/ {1 v  i+ Y% P3 F& W- P+ P# w9 q}
: L3 d1 g8 [( l. i& n" W- B3 @0 F0 f$ e+ ~$ E7 T
server {9 z" D, K* `' C3 \: @% ?4 B
listen 80;
; I3 S& c# _- `" ]* C# Rserver_name 52os.net www.52os.net;
/ {4 z0 g6 x7 u+ A, L2 B# E' H& w# q# b+ t/ Z! D4 A
location / {
8 @" q7 g3 X5 I6 V4 e6 @    ModSecurityEnabled on;  2 ]0 H* R  X2 N, F/ i0 U- i2 w
    ModSecurityConfig modsecurity.conf;  1 I4 h" M9 h! |+ y, m
, t4 O' i. n$ p. K
        proxy_pass http://online;
8 j! @/ M- u8 r- [        proxy_redirect         off;0 e. m4 O# q) f; @) q' k
        proxy_set_header Host $host;3 I3 ]% G' A8 P3 j: C
        proxy_set_header X-Real-IP $remote_addr;  h+ V3 S0 w5 X( R
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;  j: O$ ^5 h- A" q6 c9 {9 A
    }
, K6 {, x2 `* q; b1 R}
% x, G! T4 a9 p6 q3 n, A7 q六.测试
# w- M. G/ o! u4 c% o. r+ P2 B9 @- T0 |
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# K8 W+ }3 H# K9 S  Z

6 o# _1 b8 j' X+ \4 e- Z% y<?php$ N# W# s& _5 u! {  ^
    phpinfo();    5 O4 R1 `+ G# E0 @
?>
/ W1 Z+ C4 ^% S- @- L在浏览器中访问:
/ I+ w4 }$ o' Y: s+ y% m% f# o2 j+ A/ G7 F. @  a
http://www.52os.net/phpinfo.php?id=1 正常显示。0 l3 ]; f2 P% H. n  I6 e/ t3 ]! L
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
; P2 L" ?- ?( yhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。8 A6 ?' D' k$ S) s2 I* G, A, d
说明sql注入和xss已经被过滤了# E- t* k) ]. S! _: ^0 y
% B- n0 G; ?/ X1 R- A) ]
七、安装过程中排错$ {4 g; p4 D$ E

, h- v$ O3 ]' Q7 Z' s1.缺少APXS会报错; d1 Y6 I$ C( \

1 W$ H  Q6 m6 K: [4 a3 p) r, Vconfigure: looking for Apache module support via DSO through APXS" h" s0 r( ~% e, B1 S
configure: error: couldn't find APXS8 T8 p0 b: \+ J, _7 H4 V) [: F
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。0 F2 q3 H4 g6 k
解决方法:# l+ Y* s' y* k+ ?/ p% x/ I1 S
/ X3 L9 k8 r0 a3 `% G
yum install httpd-devel; X# M: r, u4 s* N; ]; x
2.没有pcre
9 J: _! [- C& b# ^1 [- p1 M
. I4 g5 ?/ T9 j1 O8 d& p4 Z* `configure: *** pcre library not found.
8 Y- N- U- c6 q4 g" H. Fconfigure: error: pcre library is required2 h# C0 |- z8 \+ z0 k# Q
解决方法:
; D5 H2 B% Y& \5 l/ c( h0 [( v( \5 G) k6 s9 P
yum install pcre pcre-devel0 }, a9 ~; u8 K/ \# r
3.没有libxml2- f: h6 x, L, a. d
: y! a) |: S0 _5 p6 t3 l
& s" V' A/ N. j9 O4 P5 G
configure: *** xml library not found.5 i2 s( O# T1 z. \: C. Y
configure: error: libxml2 is required
  l( s0 d% r/ s- }解决方法:- q/ W8 E1 m2 f, a9 F; ]8 ~( I

' n$ u) n% [( gyum install  libxml2 libxml2-devel
' [8 s8 }( d, j0 ~  h1 ], u4.执行 /opt/tengine/sbin/nginx -m 时有警告" C: `# E+ y3 W3 H- v' j% `% k1 q

1 }8 i; n6 R$ v0 X; S! t; CTengine version: Tengine/2.1.0 (nginx/1.6.2)
  Z8 L) y. F/ O. P- C# ?nginx: [warn] ModSecurity: Loaded APR do not match with compiled!8 ^% e) E' m0 k7 }8 K2 W
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
" Z+ Y' H" }# c8 K$ y" h! b- N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.% S9 L# o# S" i
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"1 s& B0 ^: Y" z; D
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!& r$ H. ?) }# T6 e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
! y+ F2 F# G0 R& w# p2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"7 ^3 Y7 r0 q) |9 @/ @) a
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
+ n0 V; C/ b7 T解决方法,移除低版本的APR (1.3.9)1 j! Z5 M0 L5 @* Z0 y' [% H
" @/ @1 ]0 ]/ ]
yum remove apr) P- w- K+ Q( G* L  D$ A5 v  w
5.Error.log中有: Audit log: Failed to lock global mutex0 \' B7 n, [6 f+ h! o6 U' Y
4 e1 a# p( a2 R, V- l* `
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     : n! Q. J: M1 ~2 C' J
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]* M- H4 E) u4 l: }6 E
解决方法:
: n! b- ]; M+ G3 b4 N  x2 I! y/ T编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:9 g1 y  o. t# l$ q* Z% R# z& E( _
* v" L7 H( A, N) f8 A, y! {6 L: C5 v3 W
SecAuditLogDirMode 07772 _9 m3 }5 v9 H* S7 m  D
SecAuditLogFileMode 0550
) t" y4 Y$ N+ `SecAuditLogStorageDir /var/log/modsecurity
& e+ P: h( f3 z" A7 _SecAuditLogType Concurrent
. [/ P" H, I9 F# K3 U参考文章:/ v- o- Y4 h  ^' d9 ~* {
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
! X. R& [$ x4 a$ @  I+ Ihttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|第一站论坛 ( 蜀ICP备06004864号-6 )

GMT+8, 2026-11-27 17:42 , Processed in 0.082373 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表