找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12398|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。3 {" ?3 N% D3 O2 I
% ]) E! }' G* ~
一.准备工作  K5 f2 a: N7 G6 a) D
  E( K! c- {  M6 H, r. K* i
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.01 ?; I2 g6 C) m# k& Z
& T" g' o) F6 ]! ~- E
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz5 `2 b; c9 a; U- T' f9 `
, w2 ^8 g4 G' ~2 E0 v
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
3 @) B+ p# o' V. D' x, b3 h0 F1 ?! J. u0 }
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs( d) }/ C( H* g' y/ C9 A, y: R

: _1 Z, x0 u* Q# x) \7 o- d3 \依赖关系:4 D% `! Y& I( G% c. a
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
1 w4 @. j$ i+ k1 ?* ]
, p9 O5 E* K$ `; `' `yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
2 _' d# j9 {8 b- T4 s  kmodsecurty依赖的包:pcre httpd-devel libxml2 apr
0 j$ Q, Z3 e7 K$ x9 R% J
/ G3 ~9 @0 {+ H$ Fyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
/ A/ N+ _) H& Z3 T& [- x& C二.启用standalone模块并编译! s& A3 t& i/ T0 G
$ r1 K4 C" c7 ~8 o" \7 @! b
下载modsecurity for nginx 解压,进入解压后目录执行:
2 C" C$ @( `2 a# \
" Z0 ~8 `" n4 N0 B./autogen.sh+ @* b2 a7 I9 G: y8 w
./configure --enable-standalone-module --disable-mlogc
' A/ M! Q# s" o- F' o/ f6 V. U  Amake % s) ?7 {; W0 z% G- c2 V
三.nginx添加modsecurity模块& a3 V$ Q+ U. T! t1 @  J! V

# H) L$ H+ F; k1 d在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
/ c! U# a0 g% Q1 C) g' ?& o1 B' C! @0 v3 V, l. w
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
# _0 Q: q1 S$ b  Hmake && make install
; L; f% s! t) @( [+ S! S" F& l% x四.添加规则: Q& e3 o3 S3 A

' v: C* T0 t$ R' D7 J3 jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- }) c2 h; Z8 N

4 @) V3 |0 l& J' e! [- s1.下载OWASP规则:2 ^% k: B0 I& `0 K8 P
4 z: H5 o" d2 {/ a% L* _
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
0 c: w7 @+ h; l4 [3 H8 n' y
: t6 P6 d- H! Y) j6 d: dmv owasp-modsecurity-crs /opt/tengine/conf// y# E8 \& T! o& E8 X7 V6 s5 q+ V2 S
! {8 R! e! Z. E; G7 f, i0 G0 c, P' u
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 Q# Z0 Q& P) {' ~$ Z5 _+ z2.启用OWASP规则:
  L' g" u; A% D! c3 S1 \: J2 v1 L0 @( B, [# P2 X  Y7 h
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% l( K: R5 G7 ^, J6 O) C
; U" ~- W. Y' a5 o& q6 j' K编辑modsecurity.conf 文件,将SecRuleEngine设置为 on" T6 ^6 J4 i, Y4 I

0 _/ _+ I: S1 ?$ {9 h6 z) W5 N8 Xowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。7 r3 F6 p+ w* k8 O- T6 B8 K

  P1 `' Y: N4 P) fInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf$ A4 D' H& [. l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
/ ?* W6 k: g1 e. B$ f) eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 a5 |* j7 ?8 {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf0 V& H1 ]5 p$ f6 p7 r
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! E* ]5 X! V) }Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf0 u# \% |  x  Z+ t
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
: n* b2 K- V9 m五.配置nginx
3 ^' ]; o4 V% `  R( z' U) h& V* Y$ h1 L4 d
在需要启用modsecurity的主机的location下面加入下面两行即可:3 h0 t4 N! [& n% S3 O3 C. T3 j
: f) U; [; E& o$ b. A( Y; i/ E9 b
ModSecurityEnabled on;  " R# d7 h( I$ W4 c
ModSecurityConfig modsecurity.conf;
6 m( s* t; K; y$ v; [1 z* x9 c$ D+ j下面是两个示例配置,php虚拟主机:
6 b. W7 g5 l& w4 @, }8 Y5 k( q$ S/ Z4 R$ f7 N% k
server {! w  J8 R5 w5 X! N" k
      listen      80;! t% U' P( }# ?
      server_name 52os.net www.52os.net;. Y" y$ r$ ~8 Y$ X& v0 J% _
     1 V1 s/ t9 [' C4 a  a% O; f+ Q4 o
      location ~ \.php$ {- L; a2 r9 m# L" o3 ?
      ModSecurityEnabled on;  
+ ]+ |; D- ^9 [3 C( C' j      ModSecurityConfig modsecurity.conf;
( t2 k+ G6 e6 ^5 G8 l, d, D5 W( U
1 S( a* f; K5 g      root /web/wordpress;
, z9 v3 c* S* F# X) }      index index.php index.html index.htm;1 ^5 ~* b$ u) H& O7 T- d* }' P
  5 ]; {0 |/ \3 \) W
      fastcgi_pass   127.0.0.1:9000;/ ?$ H6 G2 ~0 r$ Y7 G
      fastcgi_index  index.php;
( v" x" s! d( u. }7 G! k+ |      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
* s& e6 g. r# P" d8 D% D      include        fastcgi_params;  y) ?3 k, U9 @  s
      }3 r, S1 W' G$ G2 L8 d
  }
: i) r! ?# c' hupstream负载均衡:
: e; J( v2 }- \$ s4 |) ^. K0 F% x) V* I+ C  w+ j# f, Y& d
upstream 52os.net {
& v! K  t1 m5 s0 M2 Z: ~7 ]( ^    server 192.168.1.100:8080;
3 p4 p& b- O3 V# _0 E    server 192.168.1.101:8080 backup;& Q) b; p4 h( a% }; m
}: W! C- V# G+ b7 [: t( C
- e) x/ M, {5 U2 p( K% j+ ~7 d; A
server {
8 u' p; A1 h, [( ~. N( blisten 80;+ L4 k2 Q: e. }
server_name 52os.net www.52os.net;
  s8 {6 ^/ q* l! h2 J' V& X- j( C5 P1 e, B7 T+ q  s( m
location / {( a, I( P+ G$ r5 G$ R7 r7 G4 a
    ModSecurityEnabled on;  3 R7 N# Z; {7 E0 u7 p
    ModSecurityConfig modsecurity.conf;  
' n/ c$ ^  |0 E% e' d
6 {/ |+ D0 |2 @. }( `        proxy_pass http://online;8 G5 q9 e2 v* }; Z: N
        proxy_redirect         off;  [/ ]! p4 S' |( A
        proxy_set_header Host $host;
9 e* l2 E1 {/ t$ N6 y        proxy_set_header X-Real-IP $remote_addr;: x! x9 L* h/ K/ h, j7 M$ `6 E& w/ W
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;9 F0 @% u$ E2 j* k" f+ C( I
    }
; U/ Y) T$ A8 S& T* E9 e( a1 j}9 d: G- |9 e1 B
六.测试
9 d7 Z2 ?9 B6 |+ f& R: J. L7 K7 a$ `4 X
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:/ G+ y( j: N+ d! N  {. r
3 A8 r" c, {& B3 x
<?php8 }8 U3 l& ?2 l; Z5 V
    phpinfo();   
5 w4 z# Z7 U+ U( N?>
9 `* O2 X- e3 y2 D0 t在浏览器中访问:
$ `4 P3 ?3 }6 q4 v! R
1 t/ q3 l" J9 \  d  Phttp://www.52os.net/phpinfo.php?id=1 正常显示。9 c. J$ E6 `8 k
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
. y6 @5 N" A5 k. L: i. H0 lhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。8 f5 K( b  L3 }1 f* K, p; A
说明sql注入和xss已经被过滤了
. v$ E' x9 F/ B0 G. ?
7 `3 G6 k, M- x" }  ]+ \七、安装过程中排错
# w* Y. h% I0 e! ?. c% I6 d3 [( I
1.缺少APXS会报错9 ~  N9 g+ h+ C5 Z( ]1 X
9 d) A5 W5 M) {0 b, C* D1 b
configure: looking for Apache module support via DSO through APXS  _) c, ~/ u. g( T. I0 k. U
configure: error: couldn't find APXS
2 n2 k( }3 b' s/ f; yapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
5 Q; Y  Q5 \/ [( ]. R解决方法:
' s! q! @5 J: q: a' S8 M- v! x" N+ h. N/ B9 B* c
yum install httpd-devel
* x0 j* o+ G: b9 D3 m2.没有pcre
7 _" ^/ i* W( v% d( j, Q
; C6 V+ U7 X+ L2 Lconfigure: *** pcre library not found.2 t1 ~* M; |! u
configure: error: pcre library is required
4 X( J) V7 m* M  D/ N: o$ e解决方法:# Z8 P9 Z: ?  I3 {0 o
0 i1 U$ n3 Z( ?9 I* g
yum install pcre pcre-devel7 Y- Y/ A; T( `
3.没有libxml28 i% J* D: g: G

' C# V# q; i. Q* k5 W
$ [! Q1 T, e1 `8 H4 Z5 j2 _% zconfigure: *** xml library not found.
! W7 a* A+ c8 N$ K2 M6 M  Y! Kconfigure: error: libxml2 is required% ?1 m) p: x0 v8 X" E3 x
解决方法:) I7 O) E! m. C0 p  s4 ]
3 {/ b4 p3 @) Z
yum install  libxml2 libxml2-devel: [% B( {, T+ H
4.执行 /opt/tengine/sbin/nginx -m 时有警告
7 S; {5 G% U# v1 z5 v
& a- O1 v$ M2 P& g) w- \1 ETengine version: Tengine/2.1.0 (nginx/1.6.2)
8 d% h7 W7 N$ a0 ?' Q: X% R& K; Anginx: [warn] ModSecurity: Loaded APR do not match with compiled!. k4 l7 r8 d! q  g! @, m* I' c2 l
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
0 a( l- q& }+ Z, Y- u2 w* T  l( p1 _4 `4 P5 w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.: \$ z1 W5 }0 K" z3 N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
0 f7 S8 G/ S( ?" k) ]  f* T' w2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
& B7 u' d" @( z# G% G& N% \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
. c+ \1 Q, i6 ^# V/ {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
* A4 V2 X+ I/ R/ i" Z0 {2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On., @9 }' X- V5 S2 l- ]- l0 ~8 H
解决方法,移除低版本的APR (1.3.9)
/ [: U2 @( @0 O2 o% c1 `" k, @2 W  x0 O
yum remove apr
) T. T5 _! G6 c! p8 f$ |. i0 i5 ^9 M0 j3 j5.Error.log中有: Audit log: Failed to lock global mutex# r: W; y% e: Y; h) m
" n1 D& _) R0 r# b
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
* |* k( n4 ]$ u8 fglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]  r3 u: p/ D$ Q7 p& I8 `, u8 L
解决方法:. q; O. n4 m2 y% v- m* E/ H0 U
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
' `: g  Y; d* ~% o" W* T, n5 \5 J2 y7 S6 }
SecAuditLogDirMode 0777) k2 o* J# h$ U
SecAuditLogFileMode 0550/ ~' z+ v+ X- y- e
SecAuditLogStorageDir /var/log/modsecurity: d( @$ I7 C! K5 c  y9 M/ F
SecAuditLogType Concurrent
: B& {9 `( q4 x  H. W, f5 Q参考文章:
8 M$ {, Y' p8 @+ N) `( @https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
3 B1 S& R' B6 b# R9 B- |4 X5 yhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-2 14:26 , Processed in 0.070298 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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