找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11797|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
" j: v( {3 A5 v7 U0 r7 J
, ^. A: p! l6 N0 z! P一.准备工作7 |6 i1 ?# h: r4 X4 ]
3 f: z6 n8 z8 |3 h4 I
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0  f( \) X" L7 A* P  q1 N3 I

- ^! _! a# g) J* O8 y& g4 Ktengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% v( W7 a. J4 M/ x; d8 t% f. t! d* r. v5 x2 K2 d, c- L  ~% Z" T
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
- l$ z3 n% c7 V1 Q+ \0 A
( z* r0 g' d: T6 nOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs4 u% W3 S/ @+ ?/ |, S

  a  ~& H& [6 g5 d1 H依赖关系:
5 [, w4 l' q+ E6 ?4 Z$ ztengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:& J2 h% ^0 @6 w/ t. t+ a
6 K4 l. ]' u+ q1 Z1 ], V
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
1 T5 k0 Y( B2 s) D' c* {: S6 D* qmodsecurty依赖的包:pcre httpd-devel libxml2 apr& @1 H& [2 N, \% n9 l! J( m- R
2 y* ~$ q$ S" e. s6 s
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel9 ]4 w7 |4 \8 ~) b  c6 j- Y) y/ g
二.启用standalone模块并编译/ e, G! H# a7 J! `; o
8 Z" \1 n# T' o9 l
下载modsecurity for nginx 解压,进入解压后目录执行:2 w2 m. m, f9 O& H

: I" |" h% y# F, D$ e3 \./autogen.sh
8 Q: w1 N) \( v" |, }./configure --enable-standalone-module --disable-mlogc' x2 C5 s+ I5 j8 T6 ^- T
make
# ~) a, }( N, E8 @" E三.nginx添加modsecurity模块
! ~; N" \- p4 _7 |6 i+ P9 a/ A; i  d: N$ H
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
- h2 D5 f2 i# R; G4 X* `  u+ E) m. ?0 b. E3 G
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
; Y8 S3 _# D* D- Gmake && make install
! K+ d3 c8 i! H* h; j4 u  z) y四.添加规则
* x) ?# O3 ?8 ?% k
( G" C5 |$ k! W) I: P5 kmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
) @( @9 [1 S) E9 V
# a2 |% ~& _- S, N  ~1.下载OWASP规则:: i, K, o, z* K* }6 ^3 I

; Z6 x3 O. p& N2 ~  W# mgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
  @- _/ T1 l2 H: c1 _$ R3 t
0 m0 D, e& ]. `mv owasp-modsecurity-crs /opt/tengine/conf/: B0 X+ P$ T; U0 W' Z

# ?5 b: Y* `+ s6 jcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
: ]6 v+ \3 W. }( s3 j4 y9 j4 t2.启用OWASP规则:+ j0 G5 p5 C0 L$ L9 t& r
$ e" R; G. @; i5 F+ j/ b
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。7 B, L0 D# H" @7 L3 ?
: X0 f7 i  w- P9 b8 ]# T/ y6 l
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on! ^4 [. Y. L/ k% j0 ], |5 A
7 g; T4 f$ j( H; }
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。  b! {, s4 T2 I
2 @( P, A% q0 o( n. j* a
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf& y% e9 o: ~9 w! z3 `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
, `) b6 G& y# ~' W  g  p1 w) uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf  g7 }. ^$ u3 [# E6 s, e3 A8 I. q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf0 d( s4 f1 B/ Y6 F6 b
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
* P3 \( W& q" g; Z* }Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf9 j. E: `: L1 |2 x( m
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
/ X+ k0 O$ U& o. y6 x8 n五.配置nginx) V7 k% Y" [( F. P) n3 R
- c7 \! O0 c# E. v1 l. ~/ J0 |" H' C
在需要启用modsecurity的主机的location下面加入下面两行即可:9 U/ c6 l) v: \, q6 f$ q: G; e! a

- M+ g$ J+ [. ~7 w/ m* A, eModSecurityEnabled on;  
/ m% K8 o, l, i. z( B$ |: i" YModSecurityConfig modsecurity.conf;
' Y, ~) y4 I* f& Y下面是两个示例配置,php虚拟主机:7 l; D2 V$ q7 [8 O% G3 B

4 u, r0 H; j  r5 T0 e1 S  aserver {8 x# @4 d8 s6 m+ l: o
      listen      80;
6 m# N  r* Z5 V$ ?/ i4 e      server_name 52os.net www.52os.net;
9 Y9 u$ ?0 h. B$ n- b& m     
) V. T  h- M9 D- z$ ~+ M: O; a  g      location ~ \.php$ {
3 o9 h$ E9 z& U5 Q/ Z" z! S6 Q7 ^      ModSecurityEnabled on;    b6 }& e/ n; y
      ModSecurityConfig modsecurity.conf;
' [+ ^0 ?' c. N/ `6 d0 E7 }
+ F9 i8 f' B& x      root /web/wordpress;
2 Q( n! c/ a( b$ \) o8 \      index index.php index.html index.htm;
. i: Q3 Y, A' _2 r% \  
. p( q1 ~  T- {) E+ j& T9 s      fastcgi_pass   127.0.0.1:9000;
: R, A' t% o7 [/ |+ Z' q      fastcgi_index  index.php;$ J# r1 o7 ^8 M. R* ~% y3 _
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;. ~: Q* a7 f  t
      include        fastcgi_params;) X5 t( _' u* m2 u" J/ U3 b" Y
      }
* Q( u% h4 \5 K' s: K" S  }- n0 M/ H, w  b; D
upstream负载均衡:
- a7 @- N  P$ [* j
) }! t8 x% y! w, p1 n8 dupstream 52os.net {
9 m1 C( u7 J% ]) N    server 192.168.1.100:8080;
% `& g, A( }' a% H6 C8 y0 \, b    server 192.168.1.101:8080 backup;
( c) _4 M- V' U# z1 @/ Z}
# U( |+ b' W2 R7 K: f
) m: Z* G6 \# y: w' @1 j9 Dserver {
9 E! Z5 `2 x, O. y  V/ S( M0 Xlisten 80;0 j1 I" X* }0 y" k% H6 ?( s3 d
server_name 52os.net www.52os.net;  d/ v* j, i. \. a: C

; s6 T/ D8 R) V3 J# W2 x/ f8 ?- M# ?location / {
& H# h! @( }% M7 z# Y- ~+ P$ T    ModSecurityEnabled on;  
, X) f4 ?, v; L0 c8 X8 ~, |0 Y    ModSecurityConfig modsecurity.conf;  ; t( Y0 F) p- `6 f
% W* v4 K. s& f7 e4 M+ o
        proxy_pass http://online;  ^* X1 _1 A! D. v/ E, c+ @! i
        proxy_redirect         off;
$ {0 F3 k6 E1 V3 b        proxy_set_header Host $host;
0 k6 N: |+ A4 s( o        proxy_set_header X-Real-IP $remote_addr;
" I  v& W7 @+ `1 o4 Q$ j3 d        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;3 h$ P% t0 p! f; c1 E- z  W! r8 t
    }# L/ \8 X% Y5 D: g) w: N1 G+ w
}/ V1 n7 G) E" a/ P) v1 R
六.测试
' J8 Y# a, ?0 ]6 w
5 n0 E4 M" X! s9 [0 p4 T* F+ }我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:$ I: ~/ O# O! m

' p& N+ V6 m1 G<?php0 T6 t0 Z. o3 v2 u4 P8 T6 ]
    phpinfo();    " c- Z- Y* |% s# k
?>: h5 Y/ I/ o, L4 I
在浏览器中访问:
4 ~$ _" Z+ u/ w2 f+ t
3 j) {5 b3 c5 z) N2 q0 hhttp://www.52os.net/phpinfo.php?id=1 正常显示。* v( \% ?  g) X0 d% A7 D  P
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。; _8 s3 z; e; @% D  y1 J; D0 L0 a8 O
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。1 v1 Z+ Q! V* _; P: H8 N
说明sql注入和xss已经被过滤了
7 I! g. N5 R/ v* o9 l* O
+ V/ g) A4 l4 E( C七、安装过程中排错6 x' K: ]& ~" a- J' H! K& B

7 N/ _$ h" G  Q( N0 y9 |1.缺少APXS会报错
# g! _* T/ U8 l+ f. g" b! c  l: K" {- s, U$ u* y/ E
configure: looking for Apache module support via DSO through APXS
# v  a% b8 u. |configure: error: couldn't find APXS* a6 g: [/ L1 `8 R
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
# t, T; w/ P5 Z+ r5 A$ s, G解决方法:4 }; `7 o' {8 m$ l4 ?! g7 H6 B

. U  C3 [& [9 e# Q# m7 Gyum install httpd-devel
7 k6 a* z* E- Y/ w8 a) G2.没有pcre
- _6 i% {( {! [4 C/ [- n* l$ R* i
1 O' [' V: Y0 Z6 y8 Xconfigure: *** pcre library not found.. C2 E2 C; b7 ~  v! @* O% ^2 Q
configure: error: pcre library is required
2 ^2 {' N; V) \6 z% o* [6 [解决方法:/ `$ }! P. Y' \" N9 k

+ e3 E! M' d8 f: @0 I9 kyum install pcre pcre-devel
$ b7 v8 F% z6 q& _& z* n9 {3.没有libxml2
- {# u$ B6 g% c, H: ^
2 H: T6 y9 D( _& b1 ^' R
# J5 w, N  l: h' k" p, v: Yconfigure: *** xml library not found.- _8 ^. o2 l2 {# o& H0 [
configure: error: libxml2 is required2 L! T9 \8 P7 s! ]
解决方法:
2 k/ U* f1 D. e; ~9 s9 x- \# n& e$ Z3 p) w
yum install  libxml2 libxml2-devel: [8 w1 x5 F2 `. ?/ G4 |+ y9 _
4.执行 /opt/tengine/sbin/nginx -m 时有警告& R. {- Q  }( ^( s

  t) l- Q$ b9 z5 M# n0 j+ hTengine version: Tengine/2.1.0 (nginx/1.6.2)
; q- M5 E( l" i1 M) inginx: [warn] ModSecurity: Loaded APR do not match with compiled!( r; e( W3 [, Z- c' ~
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log$ \5 L5 [4 R1 j. K6 x  `
3 M: ?1 m1 O1 O% V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.! d# d$ G: U2 g# a
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9". R3 e- R1 r. D7 J
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
, [5 l$ W$ E. Y, F: B  t! m: n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"& ]9 h1 u3 m8 G  j) l9 N% Z; h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"6 K: t8 t- ?5 V+ r3 Z% l. F1 b
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.! n5 G' m& ?% q6 X4 Y: Z
解决方法,移除低版本的APR (1.3.9)2 o3 w: _" l& o
0 t- S0 T( t2 ~+ z* Z8 k; T) k
yum remove apr8 J( F8 v0 I7 G5 O2 J' u8 ~; i
5.Error.log中有: Audit log: Failed to lock global mutex! @/ M) U* N! v/ C/ m3 q2 z- |- b6 g+ d
! E5 B7 k. J( s; O, R+ b5 Q7 Y
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     # q% W. u. l( u* X
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"], Z) @2 L' ^2 \& ~  E% [
解决方法:" G- A! Q/ u# w/ D  `" k9 X
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:  `5 v9 g1 q: c8 Z) x, m( W
$ T& ?" ?- z8 |! f: x2 D  a
SecAuditLogDirMode 0777. R, @2 E5 P7 P9 D5 s4 q8 ?
SecAuditLogFileMode 05509 |8 E; l2 X2 H( z8 y5 d# ~! A$ W
SecAuditLogStorageDir /var/log/modsecurity7 w, ^, }: @7 z! A! |
SecAuditLogType Concurrent/ w7 w8 ]4 B+ T6 s3 g- q. p
参考文章:
, F9 S) H- `$ f) k% \) ^5 V& Fhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
1 f/ |- r$ e6 f0 C2 o- ohttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-13 23:10 , Processed in 0.041858 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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