找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11635|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
  y& T5 D5 K6 K3 O1 I
9 |4 Y3 D0 e  v/ W. [一.准备工作7 z; G1 R1 @4 x5 [

* x* v) G! _3 y- G& f3 j/ A系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0. w  V+ X/ X; l) c
5 S. k- I" i9 M; d  w) B) f- V
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz% N$ i0 m% A, L. Y

$ C% [" m2 Z( L! Y# Imodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
5 S/ a5 Q, U2 Y. R1 }+ d4 ]
  {' K; H4 M$ g7 F. mOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
" v% y3 q7 ]; ^8 h. x, B5 y, E7 `9 [3 f
依赖关系:
% _6 i" q( l+ `" s2 ?8 K0 _1 ctengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:7 s( \3 g2 X$ A0 I; C6 b0 e

4 P8 h1 M9 N: f2 \yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
8 S0 O5 u" U3 }- R- c8 }' p% K( zmodsecurty依赖的包:pcre httpd-devel libxml2 apr
5 Z! D6 o! H) h+ o6 p* B
  E. U- P6 H0 c# z) H2 Zyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel/ f- j  A" O3 I+ S7 S8 D: f
二.启用standalone模块并编译4 o: V1 I" K6 }6 f9 x6 f! q
! Q# o; r# n( ^  A/ ]
下载modsecurity for nginx 解压,进入解压后目录执行:
1 e; x# O. k$ A. K5 W. L6 G( G  V6 d
2 |$ \: c7 `2 @./autogen.sh0 E# @2 u/ C8 ?- _
./configure --enable-standalone-module --disable-mlogc. i' K+ z5 n% K
make 4 r1 e( Q% K2 K6 O7 Q) c
三.nginx添加modsecurity模块
3 o* w. l* h- |8 i' S" x' w% \3 @8 d1 J: T, y
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
! r# v5 S: n5 n
3 c, U* _6 d, J; s- k# X./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
+ I) K& L, l( j  c" l# I: t+ \make && make install6 I+ z8 O+ g  ^7 Z5 l/ q2 A
四.添加规则
3 W, m  R4 T/ D+ y. f0 x6 c
, K+ N5 c2 E! v8 X* m4 amodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。; I" \5 E$ I' ~7 T( e
# J5 w; K' u: n
1.下载OWASP规则:& M( ]# r# H+ w7 [
9 f2 t" X: P- s' d
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs$ C' G* G- o& c, b
) n. W$ y8 X) t% L: o( t: v
mv owasp-modsecurity-crs /opt/tengine/conf/
( g/ [3 B) d3 u: N/ z8 _% r8 R
: l: }5 G  |" }. S- x2 zcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
: d( ?; u* f* P1 g2.启用OWASP规则:0 c8 z# R7 e1 R. k2 n

6 G% B; F+ M1 U- [! }$ @# E5 t复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 L/ N- P  a6 u' u* c: g5 C" i

! L+ o; @9 a8 o; `) Q6 w- h编辑modsecurity.conf 文件,将SecRuleEngine设置为 on& E# c5 F! x9 l/ P
0 p8 I) Z) B3 ?/ O2 i
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。8 W$ @" ], o. a+ W! C2 k
, N, k; K: L, B& L$ f1 t2 _" S
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf* t' U/ o% A( E9 G2 C: G. @8 c* r
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf( m/ P8 c( M8 I1 F& m) d* F
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
  r" d" s5 X9 s1 ^" i  eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
  A6 g' X6 Y8 oInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
  w. _8 z5 Z  H# K* _* PInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
% J3 f+ Z/ a* M! ]/ C) Q0 h; y& FInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
' x) J0 ~+ t$ E- p+ Y. A8 i8 V五.配置nginx
" ~8 v; W+ z6 p, W4 t% C2 i4 E1 g4 {* W2 o1 q/ ?
在需要启用modsecurity的主机的location下面加入下面两行即可:
) \2 z9 `2 d2 p" U4 `! V8 s, {+ F1 y( k! y: v% R3 k
ModSecurityEnabled on;  1 l- Z8 t/ H( L- z8 w3 q
ModSecurityConfig modsecurity.conf;5 o9 R  t& [9 Z6 b0 v  _
下面是两个示例配置,php虚拟主机:' b# |- T* R, [( y1 F$ U' S
* i1 K5 G9 [* h+ W# {
server {
* j& z9 D% @; U0 b# X7 R      listen      80;3 J. L- c: x0 X( y( V, G& ~
      server_name 52os.net www.52os.net;
# `( z% U' i4 n, @0 d     . i% b% u. W, {7 I( o% q
      location ~ \.php$ {% ?: u, X2 `; [! O% v: X+ |  e
      ModSecurityEnabled on;  , N; {4 ?: N; {) l5 S+ x) n8 u
      ModSecurityConfig modsecurity.conf;
1 E, n  q; U( ~" T& c+ q' v' x) i! |3 b! s. E  A. N/ |
      root /web/wordpress;) b; L  p( c0 U4 X
      index index.php index.html index.htm;
$ c6 Q; t. R2 U+ K( @  # ?. D+ I1 s, S& n6 P( X% [. a
      fastcgi_pass   127.0.0.1:9000;
8 x; k( v" W/ E9 P# r, y      fastcgi_index  index.php;
- q% u9 _5 o# z; H, r2 N7 u. A      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;2 ?! E: n$ r& K, e' m# Y) L  P4 g
      include        fastcgi_params;8 a9 U! p# A0 u+ I9 X8 y$ J
      }
. [# k/ |* ~, A$ u; k! o* D+ p  }, a8 n5 v* D/ |1 d" f# l$ R2 }0 U
upstream负载均衡:4 R$ U6 C; E. @. x% J2 t. E

; i) Z1 N  L! j' Y+ g* }4 hupstream 52os.net {9 k+ J: d4 R- M) F5 ~& C
    server 192.168.1.100:8080;
4 E, m4 A" N! X7 i0 D3 K    server 192.168.1.101:8080 backup;
! P2 a; k3 O' ?, p6 q& o}7 D" }+ x" W# U$ C1 {  V3 m8 N$ g5 S
+ @9 V, @6 v. o( V4 Y: v
server {
9 P7 F( E7 e  `3 \$ \listen 80;
: R5 {) m6 h$ i# y6 u3 qserver_name 52os.net www.52os.net;. p0 f, }6 s4 o& F2 H3 ]
, D1 \1 d( f3 d
location / {0 s# T! J( I+ Z1 e
    ModSecurityEnabled on;  
4 |6 b% D. {, z. r6 }! F  t5 S. \    ModSecurityConfig modsecurity.conf;  
! }3 J; j1 t5 i: T+ P  X" E. l4 R6 g5 h) r( i! W
        proxy_pass http://online;: @* S4 o3 X6 V7 z5 ]% K% |
        proxy_redirect         off;
9 X- U% z/ F% b- w        proxy_set_header Host $host;
9 k( F, t  l9 Z' q1 Q4 \* u' _        proxy_set_header X-Real-IP $remote_addr;! q2 g  T6 {9 K0 P6 \
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;# k- |* r( m- Z9 o) S+ s
    }$ L3 {( x! ]) l& z: }
}1 v, R( k; j. u8 h
六.测试
* n  ]/ z; I9 \# Q7 F0 l& a- P) s
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:- }8 E3 i1 M! p8 }
, K0 K+ P; S. n* H
<?php7 z5 l; z2 N( P, y& g
    phpinfo();    3 {) k- D8 ~, r! M& |+ r! V
?>
- N/ v- c6 P7 L$ P- X5 B在浏览器中访问:
, j8 l1 W) d2 V/ ]; k0 p
1 h6 K* l$ A% p& _4 uhttp://www.52os.net/phpinfo.php?id=1 正常显示。4 L1 M+ X& L5 [& K
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
, u/ Q7 i1 _' whttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
1 @( {0 [' o2 Z/ o" `+ U* x说明sql注入和xss已经被过滤了8 R+ e# N) c% a9 Y4 ~" e# q

4 J2 B& r, K5 W& r七、安装过程中排错& ~% i! q0 }% ?- S* T5 i
* k9 r2 J& I( R8 C( a5 k
1.缺少APXS会报错
, Z8 t7 L0 [( R! [1 q1 _3 M1 e- J- E0 P" z6 ]( e" w4 d9 ?  p: Q6 q
configure: looking for Apache module support via DSO through APXS
) f: A4 V; r2 v2 s* x* ^configure: error: couldn't find APXS6 e$ G* _: ]5 V
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
, Z7 o8 P0 O" I8 w! x, a, V* X. i7 i解决方法:
6 Q; R) w5 O8 O" }
1 `0 {8 W9 ?& R/ e: O: Zyum install httpd-devel
! m  n# Z) S6 q4 O' G2.没有pcre
) F- p7 L: y" a: V; g/ h. u. l: J1 K$ @# J
configure: *** pcre library not found.
+ y- m9 c1 N+ M( F) Dconfigure: error: pcre library is required
# @- V* Z6 w1 K解决方法:5 Y7 q3 U7 q. l$ ^7 C3 |
' o" {+ `+ l) U( k6 S8 F
yum install pcre pcre-devel
, r' x; B) c. x( `3 R" N+ Y, @3.没有libxml2
/ X( N0 j' b# t" m
! [+ @" n6 Q+ v! j; k0 S
2 m# k, J  M9 o& h& t9 ]configure: *** xml library not found., H; D% @% I6 l% Z- r* l
configure: error: libxml2 is required
! X" b/ y' x6 u: A1 {5 [$ Z解决方法:1 b% B) j% H' F# ^& E

8 q3 E- A8 }' t/ p# x/ C0 \yum install  libxml2 libxml2-devel
( v! r* a/ \3 M, Z4.执行 /opt/tengine/sbin/nginx -m 时有警告
; `7 F  K% t6 _& v
& `/ {* B3 l3 |4 Z& {Tengine version: Tengine/2.1.0 (nginx/1.6.2)+ m0 O8 t9 F6 F6 z0 b+ D
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
( Q- {( `1 I8 N% q9 u" m' V! [7 m原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log! L* Y5 o8 h: K
  [0 D0 \+ P; H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
6 `+ c; d$ ~3 Q8 c# w. T% N9 j; @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
! h4 a( u! K; A( T  r0 C2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!9 |" R% B* f- _5 f  l+ V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
7 X* Z4 w% N! Q' u- I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"+ Q/ ^3 T# c& z& w. _- _6 t* @
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.: L$ Z( l$ @7 T8 V. U  V
解决方法,移除低版本的APR (1.3.9)6 ~* @3 f& x$ s2 E) n

& N  c8 b' x/ n$ Vyum remove apr6 e" V) m( x+ P: F
5.Error.log中有: Audit log: Failed to lock global mutex
( N& U, L. ?9 p
' U% ]# G4 N5 r! Y2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
6 b* n% F' X% E; eglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
2 H4 B; x0 k$ {4 Q, s; P" a解决方法:
9 c: |5 C) m' D) N$ \编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:- n/ A9 a1 I$ S+ E. D
; P) _2 @1 q9 j9 u7 N$ M' Q
SecAuditLogDirMode 0777
* e" e0 Y/ r# K3 F* pSecAuditLogFileMode 0550
* u# p+ P, N. |* p. V) q1 m2 _  E7 FSecAuditLogStorageDir /var/log/modsecurity. ~) B9 g  u3 t+ y
SecAuditLogType Concurrent
; z) M# y7 N" F7 o: V- [) H$ C参考文章:7 U: A# g. k5 u
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
2 }( z* G/ V# p6 X+ Ohttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-2 16:37 , Processed in 0.085882 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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