找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12808|回复: 0

nginx配合modsecurity实现WAF功能

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

* Y  i6 t" [4 ^8 ^3 {0 a一.准备工作# N: t  k, E* X9 i6 V

9 i/ N3 j* W: K0 u7 U系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
7 m- u, r. r- X7 r1 o( [! {
& f9 P+ `& e" w1 B0 N8 w; J% Htengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
- q5 b' C- L6 c) j1 t2 b2 o$ U" `# N2 ~0 t- B0 Q
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz; z& ~/ A$ L% ?3 N' o; J* w: H
4 f# Z* f* a3 [1 N
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
; V# N9 O- m' G( Z5 H( F0 ?1 Q% R  [  p
依赖关系:4 R( t, g& x; Q4 a6 P4 K% y2 M
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
. W4 `# B; Q1 Z# @2 v. {5 y1 n- ^$ r
7 `& _$ s; ^1 tyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel) N, }7 b2 p) [1 e
modsecurty依赖的包:pcre httpd-devel libxml2 apr8 E& S$ t1 \, _7 `7 c( P
* o: T6 b& O6 }% s& l# i" i
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel1 E1 i5 U4 @! s* c5 r0 u+ f
二.启用standalone模块并编译
9 H/ y& q$ K5 x/ \  e9 D. H+ F7 V* i. u
下载modsecurity for nginx 解压,进入解压后目录执行:
6 N4 a. {0 G+ x+ P; c9 k
' i: j: _# ?: ~: h./autogen.sh  p* u4 d( c" h& Z! H+ J
./configure --enable-standalone-module --disable-mlogc# l8 w# Y* B2 S; O1 T/ u2 x
make 7 `* d* V: I3 ~4 A" }# z
三.nginx添加modsecurity模块7 p( P) b# _7 F: I: u4 V

) T9 Q, Q4 G; a/ ^% ]+ C4 [在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
' s) L6 f0 j, b) l) t
6 }/ }6 B' Y9 a  j./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
" `9 N( W) i- s7 J' a* X: Kmake && make install: ?/ R( ~, U1 z5 x! z/ E/ _  a: e+ {
四.添加规则0 @# j" o6 B, P4 m0 r
0 I0 l& @2 M3 o
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。5 A7 P" C2 K7 P! J

  p) d3 g/ `. b* }2 n9 j1.下载OWASP规则:
( {. P$ t9 S8 Y3 J% K8 X7 N( {  o
' t7 M7 ~8 j2 C2 F4 k, igit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
1 B0 J6 Q* L7 g2 ]6 Z
- [' b7 }" X+ X! }+ d" hmv owasp-modsecurity-crs /opt/tengine/conf/3 D, J) [7 v. [$ M/ R5 b4 D6 c

2 W) k; o: {% h, |3 wcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
, [; E" R. l0 x7 P2.启用OWASP规则:- l6 A3 x* ^8 y2 V2 Z) Z! m: K
) n* Q, \8 n3 ^; t: @
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。5 G4 M9 }/ B6 i( |) v' Y
; m* _* }! C" Y$ L: f! n5 W
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
, y" f+ H3 p: T9 _) R
# I: s5 n6 k* d9 I, B# \owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
$ z# R) {9 Y& Z# T
5 p/ ~7 X! h# _6 KInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf! t& h" x$ `- e( a6 Z' U
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf' p. f! O" q& k  }6 ]+ ?
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ x" C: G! ?, t4 ~" a4 i. z% PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
4 l& r2 y6 w1 t: Q3 AInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf: n* D! J! e' `' f3 Y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
! d* N  ~# b: c* P% WInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf" C7 f5 U; ]$ `7 ?0 k
五.配置nginx' S- x8 H  j1 @0 a0 n# c" f

4 s) W6 a2 r3 D9 L3 c% @在需要启用modsecurity的主机的location下面加入下面两行即可:
* M0 U3 k- O) C; n% a1 a
/ s- n$ k8 l/ ~/ U1 ^ModSecurityEnabled on;  : h1 b& R4 b! [8 p3 C' s
ModSecurityConfig modsecurity.conf;
9 G0 B* n7 q8 X7 f6 K6 {下面是两个示例配置,php虚拟主机:' Y" w$ ^9 Y# H, j6 O: _

. c) {4 v% h- k- S# ^server {
; H  h8 Q1 d) E/ S9 A) G      listen      80;
! H; ]: ^- D8 S  O      server_name 52os.net www.52os.net;% |9 \# T" F  Z% F0 N4 q4 s( R# ^
     + S% E+ f& Z6 u5 G* [( w, O! z
      location ~ \.php$ {
/ }; D. h$ |* H* i* o& r3 E. A      ModSecurityEnabled on;  
$ o/ q* `7 C8 l, N% Z      ModSecurityConfig modsecurity.conf;# _& s% w- z) c* H7 ^; V1 r

" `1 q. [) ]# e2 L      root /web/wordpress;
$ H: n2 D* t6 \      index index.php index.html index.htm;
( i) b! [. P2 }* `) n% ^  
& E1 L/ O  o$ e: x- E. ?0 p  y      fastcgi_pass   127.0.0.1:9000;
8 Z% v2 q. f' b      fastcgi_index  index.php;
: |0 u, K: G$ k      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;! t0 X2 }: R' K$ G2 d2 H
      include        fastcgi_params;
+ z3 o( U2 a6 l- @3 K      }( e  A! P8 }1 G& Q, j: _1 J
  }
9 Q( W- X& d* y4 rupstream负载均衡:$ P% V8 v9 @  i
, y7 O, H6 n. \! M) O
upstream 52os.net {4 T) P# w  v1 B7 T
    server 192.168.1.100:8080;
7 L* }& C4 K$ ?    server 192.168.1.101:8080 backup;7 y  p) `! k+ @, }
}
. r0 x- V; f) I: Q" p5 F% k( k! t& b) I* u% M1 q1 T" o  ~
server {
7 I( E. s7 R8 b8 K; y1 Q' Qlisten 80;  `# w" \( t4 _
server_name 52os.net www.52os.net;6 f3 s% a/ `6 e: w/ R+ [" r

+ I5 E* l  q0 _' rlocation / {
/ _% y8 S! Q- C    ModSecurityEnabled on;  8 d. u0 e; v4 f, L0 r1 j
    ModSecurityConfig modsecurity.conf;  
) D" [! ~; G0 g& _2 r* j6 r7 E2 E, Y/ G: x6 ?9 u; l
        proxy_pass http://online;
& [5 o* G$ a. @( I0 t        proxy_redirect         off;  t, l% \$ g& A. G1 }
        proxy_set_header Host $host;. E8 ^$ m/ {( h( f0 J+ {0 U
        proxy_set_header X-Real-IP $remote_addr;4 u1 P* P8 i( E
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;. b* o/ }/ v; x$ f( {' X
    }
# J4 p8 x5 I" V: ^}
/ z/ q6 L3 J( N六.测试
' W  p3 @5 I6 t& l$ b7 t8 x* w8 Y5 ]* |1 C9 \3 n! z% B9 `
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:- z9 s  L- C% o0 E: O8 \7 D2 y" K

9 W- E9 v( W" U5 @* M' p<?php  I, }8 T! Y* N# X  e0 I
    phpinfo();   
5 k+ j  ?  V% Y# U, H0 \?>) u4 V% ]1 Z5 E. H* V8 w6 C  h
在浏览器中访问:: j: u3 U7 y7 I/ c9 D9 x1 K; p& z
! p- w- s# o2 T2 n6 Z+ ?* U' ?
http://www.52os.net/phpinfo.php?id=1 正常显示。5 y" Z' \; E. s. ?% I- Q. s
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。( V  K/ A0 t+ x2 n$ K- v
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
! X$ \9 K2 t4 \8 L- o说明sql注入和xss已经被过滤了/ u4 u( c( I) ?8 j6 }5 S7 |# U1 M

9 s0 X0 f. r3 ]  Y2 m七、安装过程中排错
. e$ A3 @4 E* C
2 h9 s# e" X% Q. c4 M1.缺少APXS会报错' B) \6 M& ]5 b! p0 s
! P2 P7 V, i5 `- t( R& Z$ e
configure: looking for Apache module support via DSO through APXS6 p$ A$ |3 M- N
configure: error: couldn't find APXS
# o, j, M$ J1 Q: T1 Q9 F9 r" iapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
  K9 c( m6 J/ Y/ ?5 L) L解决方法:
. F: S( b9 e" ~5 V  }8 s
: v0 y( Q  f! E' m+ syum install httpd-devel
' J, I* Q7 A8 t0 j- [! u; ]$ E7 t% V2.没有pcre
# v9 O* E: k( N, a
+ i3 u; [# f- a0 j  u9 j. h! mconfigure: *** pcre library not found.
) {' v& U8 s$ j7 l% z  e5 u, lconfigure: error: pcre library is required5 M" t0 t' |6 S
解决方法:
9 l/ p' ]! M6 @
# K1 l, @' P9 \% P; Zyum install pcre pcre-devel
2 @2 q1 _' J( B  }& f3 }" k2 L( G3.没有libxml2/ U% l! v/ B- }3 V
# w, o9 [" C9 x: l5 p
# v8 R1 M( ]+ A! X9 n. S" a8 q' J
configure: *** xml library not found.
. g- M( |9 n4 Y" B# Nconfigure: error: libxml2 is required
: L6 c- Z3 s) p* r/ y4 \解决方法:
; \9 ]7 d% v& S: s7 r% m  z3 X3 R8 b. `; w7 Q, ]& A
yum install  libxml2 libxml2-devel
4 q7 W# V( R. {& R8 v9 `; U4.执行 /opt/tengine/sbin/nginx -m 时有警告% p0 V) C. H2 F2 x! C

  ]( o+ K+ W3 ]% ATengine version: Tengine/2.1.0 (nginx/1.6.2)7 M% Y' Z4 d8 z4 u$ B  P0 C
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!0 K5 B. J8 O# P5 z5 i" I6 F* I% ?
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log% H% I; R, c, _

/ C1 e; p) H9 H6 I, u1 D8 m& C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.. L% z8 ~+ x4 R0 u% T) c, o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"; Q! K) ~) [: h3 T% J( H
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!! W$ S! O+ o7 ~1 q. A: p' m( b
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
! R8 v1 t- o% `  N% l8 F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6": ]- e- B, n# l% u! ?; ]+ T
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.* n# W# X4 ?, _' i
解决方法,移除低版本的APR (1.3.9)4 e2 v) c- n% H% m+ z

3 E  |4 t% G3 ^0 J) [yum remove apr% v4 ?8 G2 O8 B: x( W
5.Error.log中有: Audit log: Failed to lock global mutex% c4 o- b9 Q9 E$ j5 s
0 \8 a2 E) F2 Z. [- }
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     3 v/ D1 _' U" e, W' N: \
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]* B1 T- \8 ?8 p5 D9 U6 g* O& |
解决方法:8 [5 I, D5 B5 @9 A5 ^* R% x
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
9 g* |: a' P0 m. q! e% ~( ]: {+ {& ~! a$ @& ~2 j3 r
SecAuditLogDirMode 07774 T+ D0 @0 G( g
SecAuditLogFileMode 0550+ @) D9 L' u, [/ f
SecAuditLogStorageDir /var/log/modsecurity5 ]3 V4 S4 u5 \  U* I
SecAuditLogType Concurrent
0 y7 k, I  R$ H) ?7 e3 n参考文章:
: G' w0 z# j2 x2 l8 Khttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX0 w7 @$ E  w, |& B2 e
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-13 10:27 , Processed in 0.059222 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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