找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11970|回复: 0

nginx配合modsecurity实现WAF功能

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

& |4 ~' K9 V2 h2 ]5 x5 D# S' x一.准备工作
3 l, }7 j5 ^/ N1 O% B1 p) ]# K3 }; \. I4 ?! x% |
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0: w$ j$ i/ r2 m% n2 j

* W( m. q/ b. E9 |% g9 ^1 x$ vtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
5 W  Q- @* u  Q! p1 I
/ s9 G& h4 C" x7 b1 Umodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz* g8 e* }  X. M9 |" ~

6 _$ A+ Y' g, n! OOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs: i5 c$ w* X, V7 @
* a5 E5 C- `& g- m2 K
依赖关系:8 D. p8 L. C$ P- Y. y: x2 I
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
' l1 E/ E% j( q# I
  Z5 {& M) l2 P! F7 Cyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
- u8 O: Z7 h. w2 {modsecurty依赖的包:pcre httpd-devel libxml2 apr
. @6 y6 _, j3 y# y9 v9 ]
: [& `0 }! l& K3 w2 U7 t! A; `+ iyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel0 \: i4 s4 c$ @. m/ B
二.启用standalone模块并编译7 q4 `) c' P! Z# I6 v. z. F

- m* w; n4 f8 [  b! e4 i2 ~  R5 T, U下载modsecurity for nginx 解压,进入解压后目录执行:) {; V* \# N8 c2 @$ T, U
6 F. P" g" U" `1 m
./autogen.sh, x: I8 T& K* v: i# I) T
./configure --enable-standalone-module --disable-mlogc: T; l. l+ q2 n! ]. t1 g8 H
make 5 s& U( f- ~- {. a! c* A: N
三.nginx添加modsecurity模块
8 j# l/ o1 u( m& A3 ]+ i
& j9 L+ d- }4 @4 e在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
8 [$ h/ R% K( U' m: ~' i: {; M8 e0 c+ i) I
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
3 U( W5 s( \$ u% T2 o; u+ l& Kmake && make install) Z2 n' [! s9 M+ D0 r
四.添加规则* `; n6 P+ e& y$ \$ e' C+ t

" l% s: X" \" T3 L- @( ~modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
4 K9 s9 E6 ~9 e
/ x, N; W4 r! ~% Q5 ~+ R1.下载OWASP规则:1 g9 Z/ X% q: U% G9 |

! b1 i. @1 Q, k2 ngit clone https://github.com/SpiderLabs/owasp-modsecurity-crs& ^6 y) F, U$ H$ S- S
$ I( h. d0 h9 a  i6 D1 Y0 z, @
mv owasp-modsecurity-crs /opt/tengine/conf/
% p4 v' [0 Y6 J8 x3 V/ g+ N# W! j
# o. Z1 u: W( D/ \cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
: k3 D; R# r0 ?9 ?2.启用OWASP规则:4 E, S1 W$ G# w2 v. X
/ M8 w2 x7 g! z- D  v% x
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( K3 A# D( @2 z9 R
% m$ }+ r" U% u% G  W6 H" ~4 U编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
( A1 v% a: o6 ^, q5 M8 y# {% g8 l$ q! O: P/ l  V
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
- {% V6 S: c- P4 }9 i6 S( c. q: J- C& _" w8 |$ \
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf6 w# h$ {+ |. O9 E$ c% d
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
, \  W$ {( |  s/ N8 l% D, }" iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf( N6 D# h2 t% ~
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
$ T& S$ v3 M$ mInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
: k) G1 J6 j1 P" ^" aInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf- ?$ G- d2 w" E  s. X# l/ s& V
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
) l* z9 h! y* ^% c  x4 m五.配置nginx
, s4 U& k4 P4 W1 @8 Y* N! z) A) r0 c  k2 J" a* f
在需要启用modsecurity的主机的location下面加入下面两行即可:4 o# z1 }$ N$ j8 i* c

1 i: C5 J# E6 J+ @1 NModSecurityEnabled on;  
  k, a; f% d7 h5 rModSecurityConfig modsecurity.conf;
1 s( ]* g% F7 G' M下面是两个示例配置,php虚拟主机:
  \1 P( d8 A; G2 c4 O6 a" U
5 ]1 x0 X7 g2 j9 ~* O2 }/ @3 hserver {
; z& @( I3 q5 Z7 ~7 C      listen      80;
  x$ ]3 D  w5 w1 \% `1 v2 F0 V      server_name 52os.net www.52os.net;
: \* l# `# b( V8 M; Y: p2 o     7 g% T/ W( z9 S3 H# R
      location ~ \.php$ {, u$ ~4 d* H+ W! w. z: |7 G/ l3 Q
      ModSecurityEnabled on;  % G9 g6 O! A$ S7 O: B6 o# Q
      ModSecurityConfig modsecurity.conf;
! J, s3 ^) ]- F' e1 a3 y% V
" F) [5 V4 y) d" _; o! ~      root /web/wordpress;
/ u+ z) N9 t0 q1 \9 d0 w. {      index index.php index.html index.htm;
, F, |" V8 l6 l' w) t' t  " g7 f5 g$ B6 Q
      fastcgi_pass   127.0.0.1:9000;( V$ a. ]# {8 ?4 _
      fastcgi_index  index.php;* l0 A7 x) f% Z# u3 V6 \9 g5 e" h
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;1 ]5 I: @' U$ t  b2 x. K
      include        fastcgi_params;2 m  f6 d0 F, z; ]
      }: x  K# [% R- s, Y, R6 m7 T
  }8 t/ u7 T! R' {) v- D6 D
upstream负载均衡:* \" D/ Y$ S; K" ?

; ]8 u' K% G3 R5 d& Q) k! uupstream 52os.net {) M+ |6 l! T* N. a! g
    server 192.168.1.100:8080;: Z9 M, p1 k+ k  {1 A
    server 192.168.1.101:8080 backup;+ U0 I# z  c) O
}
) g7 z8 C( R5 j( j8 \. C
, U2 V* L% l. b2 n1 Eserver {
0 m+ P( ~1 Y0 {, o4 S" {3 zlisten 80;" I1 N/ K3 T0 L) S2 x0 ~; H
server_name 52os.net www.52os.net;
0 [5 q2 w* S+ l. X: j# H  E! P4 G, ^( |) f6 e
location / {
+ ^% m7 J% V2 h& W2 q: P9 \: O; w    ModSecurityEnabled on;  
: u% P! S8 _- n, z& T# y  w    ModSecurityConfig modsecurity.conf;  
# R) `6 }4 G2 d' `) e4 d- s0 y+ E" R; v5 }# K. s: v
        proxy_pass http://online;5 w3 P5 O# i& a! c, v: q" n
        proxy_redirect         off;
: R- r( L6 {) ?, F+ V        proxy_set_header Host $host;8 L% v/ J, l3 \" F( T  P
        proxy_set_header X-Real-IP $remote_addr;9 m1 B8 _9 f; h$ {8 R3 U. o
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
# `3 B6 ?/ _0 V5 y    }4 g$ p# A! @; l
}5 F$ U. h2 j2 M( h: D. \1 S
六.测试$ O( e3 ~+ [) z" V

# n+ l5 |  }/ d2 q0 a我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
* A1 c( ~- }. K2 Y! f( S
& T6 J) ~7 u9 {( `<?php
, X3 z& F# v: a' W) E/ W- {    phpinfo();    . `) K/ }2 T' ]5 G' F; y$ E
?>6 R+ F8 m7 a! t/ f* _# }1 E! O
在浏览器中访问:
- w! ~: m" ^6 L
* G' ]* ?( A: V* @6 M2 l2 ?: E1 ]http://www.52os.net/phpinfo.php?id=1 正常显示。
, h% Q& x9 [; F4 h$ uhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
2 ~- V- T  ]/ S0 T) i. m. {2 ^http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
0 u  J7 z! R9 H说明sql注入和xss已经被过滤了
8 D9 J4 w( q( S# h
" m+ K! {. i6 \七、安装过程中排错" P& q) B% J3 v" c

" y: ~+ e+ B, n# ?6 i8 \1 Y1.缺少APXS会报错1 s7 Q% Q6 C" }( K6 F
- Q' D" S9 z  f% R+ a. V
configure: looking for Apache module support via DSO through APXS( a$ o) M: r: I$ y$ K' ]- o, m
configure: error: couldn't find APXS
6 @, A& ]2 A# Hapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! M( e. i) O. j8 @( ^
解决方法:
% O* D2 W$ j- I3 J5 C
) v1 L3 d$ o. x; j  T7 P& qyum install httpd-devel
: |- |0 x9 t  R, Y8 T9 M2.没有pcre
+ N2 X8 x4 j# u* t" [  L2 v# V' r; x. k3 L  t2 f, X8 |
configure: *** pcre library not found.
) v$ z, W8 g# Z/ g: oconfigure: error: pcre library is required
0 c  [1 _& b+ [( A8 ~* u解决方法:
& x3 g: Z2 w" \; \% G, l+ S8 C2 A
2 A/ h! b$ ^+ c. Pyum install pcre pcre-devel
& B' K0 b5 Z& X- r( N" X3.没有libxml2, ^! c# x$ ~" \, @6 M) N0 r

* h4 K8 f: G: ]' {: f( F, R; m8 c: |& y/ Q& e* C7 N
configure: *** xml library not found./ T4 D' I' R7 q/ z3 d
configure: error: libxml2 is required
' Q5 ^9 m7 x" v解决方法:
) w$ ~1 |8 v$ Z. C0 m! k
& d7 D+ c' e: y/ B) A3 r4 a: C( p. ~yum install  libxml2 libxml2-devel
$ u! _( Q6 Z* R( f4.执行 /opt/tengine/sbin/nginx -m 时有警告3 E. ~2 G4 e1 g: q' \( c9 g* n* l. W

' B3 x# _" z! _) l/ gTengine version: Tengine/2.1.0 (nginx/1.6.2)
6 X, [; c( t+ b/ [* Mnginx: [warn] ModSecurity: Loaded APR do not match with compiled!  d: ^3 v0 h1 p* s3 g
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
+ S+ k) }- I$ f9 q( G
  Q7 {& l' S4 @$ d" w" X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
- S* f2 d& u9 ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
) h0 R+ D) `& x' J: D2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
8 e0 Q; {( `! |! A$ ~0 {, I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"( _! _' _" Z6 x  w! E/ h+ c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
% s% x0 c9 t3 \) L0 j1 n2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
4 N" \0 F4 I0 W) w8 K& J( X4 K  G解决方法,移除低版本的APR (1.3.9)
% z* @% W% C) t5 r6 j; F2 P" ^/ z0 s8 q8 c$ ]
yum remove apr
7 {, |/ d6 l: q/ @- N5.Error.log中有: Audit log: Failed to lock global mutex
5 Y$ _$ d' g4 C4 M& m$ p5 O
6 C; y1 t2 ]' u% `( P6 O  a2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     9 K( t3 S# K* R$ o$ A& @
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"], U2 N( ]' a2 C
解决方法:- A" ?; x3 \9 e0 h/ e" l/ E
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
; o; T0 f; ~; M) J6 c& K1 r: k5 Q! l$ i$ R! `7 `, U2 R
SecAuditLogDirMode 07773 e. N: g6 w) ]1 W( E
SecAuditLogFileMode 0550) c9 ^5 V  ^* d5 u% y
SecAuditLogStorageDir /var/log/modsecurity
* w# ]% g# a) ]# s7 L0 ^% T7 ]* cSecAuditLogType Concurrent' q9 j) M. I3 i
参考文章:
" i  C; A4 c. A4 |. D& Bhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX  C! R" a1 }2 z
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-31 09:42 , Processed in 0.059839 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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