找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11302|回复: 0

nginx配合modsecurity实现WAF功能

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

( x2 i* B& V1 M/ ]: ^0 V一.准备工作
$ \  q3 Y) @3 F0 z& a7 n7 V5 P6 |  D: G
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0/ _, \. U  {' J0 R1 F

: s4 F, `- i# _tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" v% |+ M3 h4 m# z
0 P# z$ `4 ~+ ]+ L; xmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
' y  F4 M, V; G: P, k- a$ U+ r( w$ _1 `7 k, J
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
* E* V* ~3 J5 t. N7 [! Q, z! b6 b
  o/ ]( A- O2 F+ y6 F: x6 @8 F# P依赖关系:* F- v* x4 E" @+ s+ ^- o' [3 }0 i
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
2 ^) l8 {- a- a& j( h9 g. {- `* q9 v( I7 Y0 X( b, T- r. X& i8 c
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
0 T) u$ B* F, \6 n# U9 Rmodsecurty依赖的包:pcre httpd-devel libxml2 apr
) T( Z" m4 }9 E/ R2 u1 H  C, h- ^0 e& Z
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel1 J- J0 S% H+ K. S& ~
二.启用standalone模块并编译
+ j9 f+ E+ b+ s2 b
1 F4 m/ L4 {& E' g9 p下载modsecurity for nginx 解压,进入解压后目录执行:
# _; d* }9 t0 f* m3 L, s5 z+ ^3 C- @- P& Q/ i" C: {
./autogen.sh
( y# }4 ?0 g8 z./configure --enable-standalone-module --disable-mlogc7 n. M0 }8 N$ q  C% k( Z
make , I% W# b7 W0 R4 _! Z& }. v
三.nginx添加modsecurity模块
+ o$ O5 h% d- p
! z- A" W( S% G: w) R# @在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; l% m8 J8 m5 O; ~. l5 o
2 _( ^0 f) s2 n' F0 t; c./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
; L3 b) X8 O$ _# L4 b$ cmake && make install
$ Q) o& P- g9 f+ \% D四.添加规则
  j& D6 P) q) \" H. @9 x- _: s$ y$ i$ L+ S
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。7 L  q1 g' d+ e, I# F0 Y* m  s

4 l% K0 V9 B. m, K1.下载OWASP规则:
  }8 {  a% b, `% B( Z  N; \
, T4 }3 o9 W, K0 h. _git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
3 d2 }3 I* Q/ J; ~: ]/ _3 d
- e( A0 j  Y/ m, V) wmv owasp-modsecurity-crs /opt/tengine/conf/2 f5 `4 D' L+ I2 k0 o3 r: l+ C
% r7 Y4 U+ ^/ B7 J3 v5 y7 I
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf: q) O5 ^% u9 B( I+ m+ ~) f
2.启用OWASP规则:5 l9 Y4 L* ~( Q: Y/ X
* e2 [) w8 e2 G, L' r- x
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。5 [3 u/ {1 x6 D3 V$ ]

( K$ @6 s, n6 F4 u- R编辑modsecurity.conf 文件,将SecRuleEngine设置为 on+ `1 v  F0 M" p) Z4 {- O
$ l, k3 r5 F) Y' Z: H+ i- c
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
9 ?4 Y" k* _0 I. Z* D* P  C) h$ [$ u5 p/ Y; f
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf" m& k' H1 W  e0 a1 l) O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
- I8 b. M$ b& NInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ \( l' D) t: K% m! ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf/ M) Y: m; g' {' @$ H- q$ j
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf5 P) A' z) x" m7 B2 D- ]: ~0 D
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
* J$ f/ ?& C2 F. ZInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
6 g" T' F: I4 |3 r, F  H五.配置nginx9 y4 S9 n" I% Y- N% B

1 B# d5 l8 j; \" \' Z0 G在需要启用modsecurity的主机的location下面加入下面两行即可:
6 M$ _1 H$ L0 H; z( u+ E
% t$ s5 q& ^" JModSecurityEnabled on;  4 k5 j3 R2 ^9 |. ~# u
ModSecurityConfig modsecurity.conf;
& h) ]4 O$ G' f3 R下面是两个示例配置,php虚拟主机:, k$ P$ U% R+ R

' L4 U& }0 W% Wserver {5 V5 b' f. U) U' O
      listen      80;+ @3 P1 |# s" T5 c" I
      server_name 52os.net www.52os.net;
7 V, i2 p# y0 O$ h     ' N2 f4 [+ H* X- _* }
      location ~ \.php$ {% R( L. [) G8 t. k
      ModSecurityEnabled on;  
- V* x! @2 N: T, O4 s! V% ]6 X      ModSecurityConfig modsecurity.conf;" M* y3 P6 P6 w! e/ T. L
& `- `. ~6 B0 c
      root /web/wordpress;
2 e! v# R; M* n+ w8 E      index index.php index.html index.htm;
* b1 J5 S$ v1 P( g0 r  4 U$ t" H% i$ c  Z
      fastcgi_pass   127.0.0.1:9000;' ]( J% A- d. [
      fastcgi_index  index.php;, C- D0 p8 R5 f+ n' ]
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;) P+ u* C+ j3 i8 X9 T
      include        fastcgi_params;
7 L4 Z* O4 V) `( I$ s      }
" `. x0 [; e) ?  }
! D6 [* w* X& k. I2 ]# {, wupstream负载均衡:, {0 C% ~; h) p" Y& @

! h; s7 B/ J/ ~8 G6 b# o/ wupstream 52os.net {
4 S, U' f  m5 d- m$ }. P7 Y    server 192.168.1.100:8080;
, H8 O) }) K: t" X( u0 j# y    server 192.168.1.101:8080 backup;
* d: Y% ^* l6 S4 s}/ h1 a7 T0 [0 X/ z% v
6 ~: C- {" F  [' o1 V4 G5 N
server {' d4 M- \0 C6 U" b
listen 80;
3 h# v! `# Z. q- R/ Z1 @server_name 52os.net www.52os.net;
) `1 B( X' s2 n' N. H
8 S* N5 A% B! |0 q7 M' t: d% ilocation / {
8 {2 R3 p( m. ?- Z7 i  \, F$ \    ModSecurityEnabled on;  
: {+ w" l- L4 v* n" d) b# b    ModSecurityConfig modsecurity.conf;  5 d. K+ J4 ?6 R  Y$ G& ~

* Q( U9 P, O6 B  b9 }3 l        proxy_pass http://online;% t1 R/ H8 L+ b  x( G7 x
        proxy_redirect         off;" M) u6 ^+ u+ h6 @4 a0 d% x
        proxy_set_header Host $host;% Y2 P3 L+ F8 J
        proxy_set_header X-Real-IP $remote_addr;
* ]1 a7 Q; K* i9 C( ]7 i5 N3 B        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;1 Q4 N1 r7 E, e
    }
, E$ v2 K1 r% U! q' M}
& m8 _3 h- M8 n+ Z: _5 P: |六.测试/ ^$ E9 F% n. n/ G
& i' l0 o" j0 \
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:, m% K- g$ b8 I  r. }
+ E' a! C0 i8 l- ?4 W; E( f& {
<?php) g6 C8 Y6 V( o, C7 V
    phpinfo();   
3 e( K2 C# K+ L) _?>
+ |$ C+ S" |) O3 k4 Y# ]在浏览器中访问:
( n6 m& q8 W- R2 H
4 q; Q: w2 L7 whttp://www.52os.net/phpinfo.php?id=1 正常显示。$ X( U4 W' v. U# {" @$ \9 s
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。7 r) n# I: `5 u: K  j
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
: A; G' {: m! ^3 \说明sql注入和xss已经被过滤了
' H9 c8 o5 n8 w" b2 e5 X
" b6 o6 |" a. e0 r# h七、安装过程中排错
7 \( F0 ^8 X2 e' \% o7 h" \' H
, X) j# P' S) Z6 i3 q5 k5 `+ G1.缺少APXS会报错, K6 y% I, Q. D/ u1 \( x

: L% l# I( b4 H( Wconfigure: looking for Apache module support via DSO through APXS$ ?- P# ]! S/ G2 z8 I
configure: error: couldn't find APXS
% O+ }2 i  {9 l) ^apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! W3 u. o4 }) f
解决方法:
, f9 v+ q! c7 @3 N' y/ ?4 K, e" I1 \) r
yum install httpd-devel
  l8 y7 w' X8 K2 y% V- V# \2.没有pcre
1 P! J7 H9 Y" \6 o% X4 n# m" P( A: |" t
configure: *** pcre library not found.' T  _% N3 L: Q* @
configure: error: pcre library is required
; I  E  q0 ^4 k+ K/ `' M1 ?解决方法:
& s* v5 o; e9 @+ p$ J  u
8 M0 K2 _; u% e# ^* w% x% y, L  pyum install pcre pcre-devel
$ U0 ?0 E! a  ^3.没有libxml27 M3 a3 }1 c- U; j, K) D- u
& x# X7 j! \+ r6 _3 _  \

! e/ C9 ?% x% Uconfigure: *** xml library not found.
6 |, |$ j$ `* F6 lconfigure: error: libxml2 is required
0 s. {: u' l! m解决方法:
6 y$ |7 V7 Y  _/ N
2 a0 D4 D+ z- o$ d3 Tyum install  libxml2 libxml2-devel
; t' P5 V4 ~7 j' s0 S/ {8 o! [1 j9 k4.执行 /opt/tengine/sbin/nginx -m 时有警告5 `' Z4 L" n1 P

! t! @$ E8 n* u3 ~Tengine version: Tengine/2.1.0 (nginx/1.6.2)
  c% H# o: G& G5 n( h+ \. {nginx: [warn] ModSecurity: Loaded APR do not match with compiled!4 a7 K% ~) J8 V8 G
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log; [/ u# H( n% ?7 p; v+ Q+ k

" s$ W3 z0 d1 Z# C, K8 i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
) D. J5 ^; y: J$ d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
3 L7 `. k/ b2 @2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
; a2 M0 _: Y% Q& V$ k5 K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"6 n% w' j9 \& n: [% U  O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"6 }; L- G9 w, L/ X
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
. V6 z) B) ^3 U# B7 S& {8 l解决方法,移除低版本的APR (1.3.9)
% I( h; G1 G, U9 W2 ]0 V: k
2 h0 h% S: C& ?. [9 Iyum remove apr8 e2 p$ [4 i8 N
5.Error.log中有: Audit log: Failed to lock global mutex
$ n( [3 Y) G2 B4 S( {- g3 `/ d) A: R1 x. `# n4 _& n9 _$ u
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     / S& F" e! R& U2 ^3 g
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]+ D7 W) i9 ]3 b' ^4 e" I& z, G
解决方法:
# g/ F( D' Y# Y编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
7 U9 p, q  h$ U1 o+ R3 M) m8 K. S; V+ u5 }2 Y* x0 D. ]& z- L: h
SecAuditLogDirMode 07770 ]! u. z4 o5 J# o! Z/ B
SecAuditLogFileMode 0550
$ S6 r, x8 A" OSecAuditLogStorageDir /var/log/modsecurity
8 k+ `! n; Q$ E$ ]) h% p8 Z  _% aSecAuditLogType Concurrent6 R* k7 d+ a1 x+ |1 v3 g2 l+ l
参考文章:0 {3 k- g5 b( H- M2 _
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX8 \" R( D; i4 X6 k0 n) \
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-6-7 23:00 , Processed in 0.093496 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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