找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13042|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。3 S& c: C- g3 \, N) ?, |

$ ^4 X7 O5 O. L. }0 b4 O; }" J  o一.准备工作/ v  |! u1 r4 a

- _, R. ?7 h+ k' K系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
' X& @4 g& y' K( ^2 O& T9 ?
' _# c' Y0 O' Y2 s  R6 u; T5 Etengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
0 C& \( ^1 r' v1 R+ u( X* }
4 d( h7 }# X  j4 r+ hmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz& Q6 L1 k) j4 T3 p' S
' A$ I8 B9 U$ S" \+ |  ~
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 w9 r( G$ h% y: l1 _
- N4 O0 G. o+ _依赖关系:+ k0 q! N! U" H% d0 _4 s) _
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:; z6 m$ b$ b4 t2 N( [
# A5 i8 d* z8 y( F
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
) c2 J6 A/ E# V. d0 V0 p! wmodsecurty依赖的包:pcre httpd-devel libxml2 apr2 R, f3 f" a0 e
! @" V8 Q! `3 |* d8 H
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
2 T; ?# k- F% ]& w+ x' G4 h二.启用standalone模块并编译; s/ o- @4 z9 z. q

6 k' x& Y8 W- x5 n  i, I8 q下载modsecurity for nginx 解压,进入解压后目录执行:" t2 d4 _" e8 ^. D% v' v

- b0 b; ]8 e& Q3 S; p./autogen.sh8 H1 K2 l; Q) F* S& k$ e
./configure --enable-standalone-module --disable-mlogc% o. r9 e. ^( t/ z& m! M2 K
make
6 m0 G+ d0 ]- A" A3 x9 m& k三.nginx添加modsecurity模块
( N9 D) h- e8 q. n# ?+ d9 a1 a% G5 h0 Y, g
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:8 ?5 f7 ^) R- U+ [) ^/ k' z
2 i8 B# q. q6 C: \  A
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
$ t& C+ e; e/ E* w6 l; }$ ]) imake && make install& L6 P# E& P! K3 |
四.添加规则  `# v( y3 Q7 A9 A( V' z; t0 Z/ z
% j& d% P0 k+ f5 n& E  G
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
& Q8 P4 t  Z: m
& h6 t+ S4 \1 w* W. M6 @  z/ y1.下载OWASP规则:9 t) y' ?8 W- _8 M2 p7 p

. a( D% A/ \( p- \6 ^0 i' _git clone https://github.com/SpiderLabs/owasp-modsecurity-crs! a& g* w" `# c: M7 h6 N: M9 ~1 N

. B7 g8 P6 t: W/ Lmv owasp-modsecurity-crs /opt/tengine/conf/' P6 n8 a. E7 Z( z) `! w" Y
4 U& r* W& k$ O' q6 G( v9 ?
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
3 c8 p1 Y  s. N  {" C2.启用OWASP规则:# i. O& l* K" T1 x( v
+ x- t) a7 @: |( e
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: y3 S7 N+ ?! {; N; |1 _! W" B  z% ^  G/ X  N$ w+ }4 B' n( m. ]6 O4 F7 S
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
, {4 \* e1 n7 X. \) ?
! {$ U: p$ G/ L' i4 K, B5 iowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。9 r& X' u7 C7 l" E: b3 n  L" _
7 `! E/ M0 R0 y. K4 ~
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf# |* j, c" G& ]) n" f0 Q3 m* i
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf6 ]  {* U3 o5 T5 _, E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
/ Y; z: P  F: r4 H, oInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' M; p9 |  R& ~1 {9 Y% X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
  R& ^& Q  M/ r- @Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf" v+ ]) x5 B) r) d
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf0 _/ h. j3 E9 t  _5 Q. q; c2 F8 u0 h
五.配置nginx
/ I" D8 H; p* t0 z2 C) h
8 F5 ?/ Y/ v6 T$ ?& f+ b; W在需要启用modsecurity的主机的location下面加入下面两行即可:
5 K3 ~# V7 T1 X, i( P
8 o; I7 v4 O8 ?( y+ }$ nModSecurityEnabled on;  
9 r! r. k$ ~8 H  u% ~ModSecurityConfig modsecurity.conf;) `2 {. R5 V9 v. w8 }
下面是两个示例配置,php虚拟主机:7 S' z1 I% I& {( ]! C# @

3 w# c: j% r1 I* yserver {  s1 B* @) H) I5 \( s4 v( X! y
      listen      80;
( ?+ V9 D0 e& E: R      server_name 52os.net www.52os.net;
4 _6 O) ^0 Z# f! F1 v* O     $ V4 a5 o! q" Q2 A: }* Y# M
      location ~ \.php$ {6 q! ~; _" X% f$ n
      ModSecurityEnabled on;  5 W9 D+ @, t4 S  b6 W
      ModSecurityConfig modsecurity.conf;
6 A' w5 {5 I, D; o7 j! R9 J! p. Y5 a, S: L! `7 Q
      root /web/wordpress;
  n9 L9 B, Y3 B, z      index index.php index.html index.htm;
- ]$ ]# b- i& r7 L  
* D: j+ v# G1 n0 N, V      fastcgi_pass   127.0.0.1:9000;$ j9 I( G) H+ n; g
      fastcgi_index  index.php;! D  w$ m9 M2 r4 l; J  x1 U4 R/ ^
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;: B4 |( S1 S( R' G4 o2 L
      include        fastcgi_params;# X. d5 `: J5 f" N# _8 G
      }2 u, ]( b/ Q' U8 C. O
  }
7 e" A( Q0 i6 K$ o/ b) Oupstream负载均衡:
( X7 D5 l2 T# @% x7 m( Y6 t
$ x' Q/ }$ e( L9 M' D/ e  Kupstream 52os.net {# r4 u" t% x5 S: B8 C( j; W: ?
    server 192.168.1.100:8080;
' N' c# F9 r4 t6 e0 H, b6 j    server 192.168.1.101:8080 backup;
; q. {+ y6 r+ e: g' C! I+ }- t5 ]}
- G0 F" w4 k+ s* y8 G
( g+ P/ ~  p) d6 F: p. V7 |server {# S- K( S5 O5 _1 j9 @
listen 80;  u+ y$ W: E1 \" Y8 O  f  Z( B% V: {
server_name 52os.net www.52os.net;* S8 r8 ~: I3 F0 M1 f9 Q, ~' G
3 _/ ~8 v8 b$ ]
location / {
, R- Y: D/ k( {3 ?$ S) U* l    ModSecurityEnabled on;  
; U8 N% T' Z+ k/ @0 c1 w    ModSecurityConfig modsecurity.conf;  7 H: R. X6 v; c" O
* V  i  {3 R3 o. [, W4 y) t2 B
        proxy_pass http://online;& v; g* p# ~9 @$ ]+ X
        proxy_redirect         off;
" |, I1 ?# [) d& V( }        proxy_set_header Host $host;7 r6 x& u4 G" @# l" E2 t( B
        proxy_set_header X-Real-IP $remote_addr;
; n9 ?1 R" V4 t& S9 p! t0 r        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
% h# f3 A6 z3 f# f- _3 q    }& x; A4 V, ?4 |% i
}- C- D+ V) U  N& V
六.测试
1 t; s" x, m1 H+ N' R6 \  X' `# N* r6 t1 k. O7 q* g* Q% s
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
5 Q; s0 j, ?- z% E8 H5 x- r$ j. U: k  g
<?php
. b% Z+ R6 _4 ^- `- ^    phpinfo();   
$ M  z5 Z, {/ V4 U) W6 M?>
! m1 c- m5 y9 l3 ^; \' Q$ V) ]在浏览器中访问:& p8 @9 y9 ?' M9 A2 U& \! x

; r. A5 A$ B% J; J( ~http://www.52os.net/phpinfo.php?id=1 正常显示。9 A+ R! ?  m' }2 z
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。1 V  E4 F) y- o4 U+ b
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
1 D7 h4 u0 I) T( X说明sql注入和xss已经被过滤了
  Q* t7 Y5 p' D+ E( I' M; Z% H4 b
+ R7 F0 y  V; k0 ?. `, n" \! M6 f七、安装过程中排错$ I  _1 M% s  E8 D$ \; a

  g) C( H' n1 A1 m9 e; Y1.缺少APXS会报错; x  j2 m' x6 ]$ c3 U$ C
% c! w7 {9 ^& U! |! L0 m1 C
configure: looking for Apache module support via DSO through APXS  r$ F" j! b' h" T0 q0 \' b
configure: error: couldn't find APXS+ R# q% d& B; v  W2 W
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。; i8 D- s3 b' o
解决方法:; _! f! Q, }1 _, U; d
( z/ M8 Y, c. M6 s, Y6 ^
yum install httpd-devel
% j+ n: `+ y, W2 |4 P2.没有pcre
- f! m+ x( e( g* ?% o( p) Z  d
2 ~; G4 R! G) xconfigure: *** pcre library not found.
& ^% |9 t4 n2 J0 econfigure: error: pcre library is required
: T" q& _. p* B: z& v6 L解决方法:
4 g  x+ B* [; E' ^  b% V' ]& w$ P6 M0 q
yum install pcre pcre-devel9 c! r$ _  {# Z8 L$ [8 Q8 s
3.没有libxml2
# S& g5 Y9 K- Y9 y* c" x$ ^
+ `3 @& E. D$ k. Q/ l! B2 H0 h' N' J( n+ g+ h4 e
configure: *** xml library not found.
# j0 X8 J5 h, g; m9 H, B  X* Nconfigure: error: libxml2 is required
" S$ c  ^/ C/ \0 r& x1 j解决方法:
3 m, y! _. ]3 I* J8 d- P4 \0 ?9 S
yum install  libxml2 libxml2-devel
/ l" B7 Y7 g! R$ n5 k4.执行 /opt/tengine/sbin/nginx -m 时有警告
! m! C* F: q( e( o  A/ s9 o& e4 m* V* V/ q
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
" z. B( l$ H( R" p7 k: bnginx: [warn] ModSecurity: Loaded APR do not match with compiled!/ [" {( V" Z5 E% Q& p
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log, C( G# F  \" u, U( R0 i* ?. }
' C, ?) z5 W' K! G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
+ z! [1 J" {7 S9 W6 N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
/ L/ s" H; q6 p# E8 w4 j( X2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!4 c- ]7 h5 c, h: z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
3 a% t; y3 u* U' n" t2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"* S+ i4 o. r/ Y; L0 V+ B& T
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On." b$ p, J* b9 O0 s! }  x' k
解决方法,移除低版本的APR (1.3.9)% g( q* h( b. d
: H. Y6 u* i$ w1 @! |
yum remove apr5 T' I6 E/ W: P9 a! {. c
5.Error.log中有: Audit log: Failed to lock global mutex- I$ j* }* N$ u

- E+ D2 [) }- z6 ], ~& b2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ( x4 w0 Q2 r% F2 T- f: v, }# Y
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]" A" ^5 w: l( M3 E$ y6 p
解决方法:
8 M& m9 k8 W: J编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
! l  @1 [' V) N7 }5 I( P' M9 D# ~6 S. J) ?
SecAuditLogDirMode 0777: n8 o' z+ x! n; I
SecAuditLogFileMode 05509 ]+ r2 m2 b+ q9 U! L: ]0 }
SecAuditLogStorageDir /var/log/modsecurity
! Y' n4 H1 Z: q' {SecAuditLogType Concurrent
: o) N2 v0 p) S3 G参考文章:" K9 p% H8 y% v6 A, V$ k
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
& G+ Q: L( z0 r$ w1 e5 E* Nhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-7 09:14 , Processed in 0.058143 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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