找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13018|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。7 u, c% f0 |. v( q
2 [: e/ {: X) v4 R' T( u7 d
一.准备工作4 y- T- P7 L; T, y; N

/ Q, K7 x/ q$ G& `( t1 ?/ {+ @1 I系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.08 ^# `/ G. W7 F. j7 p
- {. x3 x# j6 a  [
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz+ T$ B& O7 e" P* R8 _+ m# V

$ z# P* {/ \7 L" @7 cmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
- j7 d* W8 z$ A+ f+ S9 p1 {4 I) f! p7 i" L0 u" q, J
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs  {6 z4 V# c7 y+ X+ s- b9 [+ T
' e3 i3 q# d& ]9 \* N
依赖关系:6 P$ Q3 z, b7 \' y( P( |, n7 l& e; K
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:. U' ~- H& Y8 t

, ?- H( Q2 R9 N: m9 M7 Vyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
( N, A/ L* m2 E! L# L4 _modsecurty依赖的包:pcre httpd-devel libxml2 apr  L9 n4 i4 Q, t, o- r2 F% k

2 x  o4 F4 B. s  g$ X1 Kyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
6 O! A1 c8 K/ {, T4 [二.启用standalone模块并编译
2 L; @) l- d* a- X& M4 K& z" e; ^" X# _- ^
下载modsecurity for nginx 解压,进入解压后目录执行:
! L8 L5 `5 l& q1 I
- y1 N; R- q& Y./autogen.sh, C& W) n0 g% Y- `# P& L4 Q
./configure --enable-standalone-module --disable-mlogc
- G& j- }8 O3 I5 ~* m2 rmake
+ P- ^' h% B. a# @( K6 `三.nginx添加modsecurity模块3 R3 Y9 z9 D8 a$ m+ m
: G9 x0 p) \& S9 I7 C+ @
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
$ m1 g6 @, |. Z+ c5 e
/ w" S5 d% C5 _. h6 o3 }./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
% z' M8 N2 V2 x$ m; I4 D; g" s/ s5 g- Nmake && make install
" m! Q) V2 ]9 B/ s$ m- z9 T6 p& W四.添加规则( k* c6 W2 V. g& ^/ h' h7 m& }
: H  t( w% ^/ F
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 S( y, e; t/ w2 W3 M; s2 h# q, `& U! R- r. {+ [- C
1.下载OWASP规则:" P- l* y1 a3 ?

+ {9 C; Z4 _8 z" igit clone https://github.com/SpiderLabs/owasp-modsecurity-crs! p9 z5 P  l# E+ y0 q
8 ~: b( }* G/ k; _/ O$ `
mv owasp-modsecurity-crs /opt/tengine/conf/6 q" k. g9 h6 E4 {

- y! {8 V: _1 X" d" J& tcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf7 b' d7 ^/ C) j- |5 E
2.启用OWASP规则:5 S. J1 D+ i) F/ M9 z( U4 P, A0 D

! A) o4 C3 ~$ B5 H复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. l" `- T8 R* Y$ \- X- W
! z  g& t) f) z* b" U+ G编辑modsecurity.conf 文件,将SecRuleEngine设置为 on) X* \) V4 w  p5 R; s

" r: X( C+ f; W0 howasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
, V. s( [4 p0 [- U  I9 `5 @: |
: H: f& P7 k5 ~/ q2 DInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
# b1 O8 ]: n2 i6 {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
$ H" J8 D6 U' |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
  }4 ^3 w0 U: zInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
/ [% r3 ], G/ @" C, bInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf8 ?- u4 c/ p' v) a1 n$ H/ m; l
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
. [* z! j* p6 M( d) W' e# Z# _0 ^Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
3 C3 Z) J# h& v0 T- C$ G: ~( b五.配置nginx
9 O$ F% d5 B) X3 m( _6 P/ Z6 O2 |7 k; Q; g8 J% e) y. j
在需要启用modsecurity的主机的location下面加入下面两行即可:
. U' j9 c1 U0 Z8 O' S1 R; j6 z/ B2 ]' `
ModSecurityEnabled on;  ! O5 o/ D) l2 `9 L0 ~, {4 `
ModSecurityConfig modsecurity.conf;
3 e3 r! a: a% V; e& Y下面是两个示例配置,php虚拟主机:
7 I2 O% s& R0 n0 i* U7 c$ F# U3 \7 V2 h9 d
server {6 r. Y, o0 N2 z/ _4 d6 \
      listen      80;
% Y- V) b2 b- ^3 r      server_name 52os.net www.52os.net;. ]9 p) t9 B. c" ?( [
     / F- c- v" g& l' J4 l
      location ~ \.php$ {
2 u7 x1 g& _% |1 X1 P" V3 m      ModSecurityEnabled on;  - s3 _3 H, {% u! V' {
      ModSecurityConfig modsecurity.conf;0 L# j  o8 K" D$ j8 c' M" F

" V# x6 \+ g! f' J: P# \. ?      root /web/wordpress;
+ a2 @2 p& N- C6 c      index index.php index.html index.htm;
" o5 j1 [/ Y0 U0 J$ Y6 h$ Q  + u- U" O/ J! M# Y
      fastcgi_pass   127.0.0.1:9000;
6 m- u( u8 X+ {1 n8 F9 y6 Y! v      fastcgi_index  index.php;  i/ F7 b5 \& |8 E
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
0 ^! p! R/ s+ ?+ ]. Z/ k      include        fastcgi_params;
' y/ p4 H8 J* I4 q( e$ k7 l      }
3 l* g5 A+ C  D( \+ L( Q  }
9 E6 e  `: _$ [6 ]) y, H/ f# A4 qupstream负载均衡:
/ b6 P2 b6 t. u7 Z, X9 I6 q5 M+ X/ G8 I2 Y" \9 W" ^3 i
upstream 52os.net {! Q! z' y2 }  \6 Z
    server 192.168.1.100:8080;! E1 U" |# J+ j: Z: Z
    server 192.168.1.101:8080 backup;
9 _* k9 B' z/ x5 A: o; _& L}/ V) Y6 D% \# W6 ^# K

; V) K  m) o1 Q: q5 G: Z  }" i; [server {
' C5 j: ]" V5 V  hlisten 80;
4 Y1 {' _4 @9 Q( G% Z# F. [6 Zserver_name 52os.net www.52os.net;
5 h$ W% u" p; l# g
/ r% W& j8 v* S  U- Q; Vlocation / {; o4 L4 _- w/ f; k2 J
    ModSecurityEnabled on;  
' @- V6 |- y* @! U    ModSecurityConfig modsecurity.conf;  
7 b' Z3 S8 u) H! |/ t
" |4 b1 I- F* C( Y; x        proxy_pass http://online;+ t& Q: ~% T) H6 ]5 ?
        proxy_redirect         off;4 J3 x5 L: X( k
        proxy_set_header Host $host;
0 {/ A5 E, {! f, @) A7 E        proxy_set_header X-Real-IP $remote_addr;
( s: U: x, E% K, g: k* R        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;& i9 V/ X. m, e2 ], E" S) p
    }
7 a: C+ N* h! g" y9 g0 H; a6 e+ y}, X# g, j3 B! F2 W% p" X. t
六.测试, q" g0 ~% P  M

9 h9 ^) b4 T# @( @我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
: J' G7 |1 [4 F* p
& y* C( e: G$ ^" C/ [" o<?php
! d: N& B, h1 |2 `4 r' i    phpinfo();   
! Y$ C& W/ k, w) `) x2 a?>
% g5 u  Y: N) [3 ?$ E在浏览器中访问:
, F% @! m+ f* _
7 V& e- w% q9 g$ z3 |6 yhttp://www.52os.net/phpinfo.php?id=1 正常显示。1 |/ p' P& c) m, v$ f3 w
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。+ @4 E4 }3 Q% P3 ?) F
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。+ I) N# I0 G; Q
说明sql注入和xss已经被过滤了7 a$ S5 O  B& v- p: T6 x) q

7 j# f. |" j+ u- F七、安装过程中排错
* F& t0 I2 A* @* X" h" H
' O7 z" G) ], }1.缺少APXS会报错9 m) i4 i; M/ O! p

: `5 W3 X& ?9 e% B5 |, N+ Wconfigure: looking for Apache module support via DSO through APXS
0 F- A" M5 ^! z! B4 @* G( |, j' aconfigure: error: couldn't find APXS) x# _7 h$ D' r2 W1 [
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。' ]" V% }3 e, W; \5 @, |5 X
解决方法:7 g. ]+ i  s# ?
7 o1 b2 ^* }2 c, Z
yum install httpd-devel
6 r6 F. B# v6 z' U" a0 z+ T2.没有pcre' n: r+ C8 h0 [/ s

( T+ H8 U1 A$ [, T/ S( |configure: *** pcre library not found.' w/ y& d6 F4 x
configure: error: pcre library is required" P6 r. B/ C1 M0 m' b
解决方法:' @5 g+ X8 j% W# F: q' v/ M
5 v3 Y! L4 x( B% A4 ]" ?. E
yum install pcre pcre-devel
$ e/ n  _$ F9 g0 b! l9 I3.没有libxml2/ P9 u* |1 q8 F6 E# {1 K$ }$ ]
' T4 m. q6 P5 B
* U) m. @! J2 {6 ]* A0 |
configure: *** xml library not found.% J. h6 n# p# y) s" b! |' ?! s
configure: error: libxml2 is required; n3 z5 h! Z7 f
解决方法:: b: Y; @/ S5 T  Q
7 E0 M  ]! M/ K- V; J5 ]
yum install  libxml2 libxml2-devel
+ D8 f2 I9 \* p: m9 K2 @4.执行 /opt/tengine/sbin/nginx -m 时有警告
) r! E6 m7 g# |+ D+ o; `; q9 r' M- h& A% H6 A3 a8 [. u
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
  u! G  `1 `, s1 Y, xnginx: [warn] ModSecurity: Loaded APR do not match with compiled!. Q+ t, b7 v- ?- R+ X5 e
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
4 U8 S. G3 f- }0 K4 K. ^+ N3 R1 Y- R$ S5 Z* `) ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.! l8 m, Q9 ~  u; P! U# D: g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"% Y% p( C3 a5 M/ b* j
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!3 ?$ `" h$ O$ e# L' x/ X: [
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"5 k. _- ~. f' l% q* E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6". a& ^  k9 B; p" H
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.  h+ N5 E0 c+ Q! x% C. j4 r1 y
解决方法,移除低版本的APR (1.3.9)
/ v; b+ U* L2 `. _, ?" }* ]# U2 G- W9 \2 B) q- M4 |1 d+ L
yum remove apr
. s6 `8 V8 G& Y5 w1 I5 }  O! I5.Error.log中有: Audit log: Failed to lock global mutex
1 O( k: P# T, R! O6 h! {8 Y; ]# E  d# r# p) a# }0 {
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     2 h0 b$ p% X2 P
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
( J: b' ^3 D$ ]0 h; ]3 s6 J# ^解决方法:- o. }6 |4 N* N* i( D
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:1 g1 _3 b0 o0 F: Z0 s" U

, h% C, f, I& G2 p. d# YSecAuditLogDirMode 0777
! N4 E3 p2 E/ w; ISecAuditLogFileMode 0550/ N( |5 a& H! i* s# r3 A3 S* d
SecAuditLogStorageDir /var/log/modsecurity6 O1 h1 w1 v& S8 P3 ^% z
SecAuditLogType Concurrent
8 ^* W) \2 ^7 |2 i7 R% a参考文章:
) [) V" R4 d0 M' S: Dhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX3 l$ y$ F. G6 I% m7 o9 P+ ^7 r
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-4 03:37 , Processed in 0.068656 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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