找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12529|回复: 0

nginx配合modsecurity实现WAF功能

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

! n- ?3 E) ~% k8 ?: i- v一.准备工作
1 i  k. J1 q0 J/ _" s% l2 i0 I3 C9 P" _) Q1 @. t# N9 |
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
0 j+ L2 d  d2 j+ K; P  r3 i; ]
, T" W# @1 k0 r4 o# ztengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
1 }, z8 h; N* H, {  n+ ?0 D4 h* ]; J3 Y% C7 o3 S; O5 p' H
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz3 O' ~! R1 Q: s1 y
  ^8 x4 N- ]8 F! l$ c/ u; f
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs' [0 X- w# Y5 Z7 ?. y
( L9 g6 m  u$ D" o. l
依赖关系:
* L' t* ~, x: g: b+ ~tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:% u3 k+ f; n6 E0 g

0 U4 b6 q2 Z4 u$ v" S- K! ^- L+ Pyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
! R) J: M/ U* q% U7 Q) wmodsecurty依赖的包:pcre httpd-devel libxml2 apr
) V$ y- D5 w! V. _9 r+ E1 s+ v* B' e
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
; ?8 o! `9 g; F9 [  N. n8 g) O二.启用standalone模块并编译0 F2 ]; ]2 \, G  [5 ^& ?
0 o' j$ o+ H8 L% D
下载modsecurity for nginx 解压,进入解压后目录执行:1 H9 @. K4 E' H! h, m% b

% |9 Q8 ^8 ]/ {* {: v, t. W./autogen.sh
9 \8 X) U/ y& g: a* `8 l$ ~3 o  }./configure --enable-standalone-module --disable-mlogc  R: A" g5 h+ g
make : z- @& e" \+ P' ]2 [8 w
三.nginx添加modsecurity模块
* \7 J2 n7 o8 D" P6 `. S. {7 ^" `! f/ j2 _  B
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:& A2 _0 w$ {2 x/ K: Q

6 y7 _. B) m" w./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine+ |8 O/ |$ d8 o* A3 a
make && make install
: v% V" @4 z! B1 A1 P四.添加规则
: w( H! Y$ _" c, N0 \' c, ~$ w, r
- L+ R9 q; V5 p6 [' W) D5 W0 I, hmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
+ o/ o9 ]  h- |, [
  g' y$ w8 ]4 p, _5 c: d) H1.下载OWASP规则:
( n6 l) {: I8 D
6 Y- L* s( e6 G% Jgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs# f1 i' l1 {6 J3 G  P: T: ^+ Q$ R, j

; t  v' u  a3 b2 y, b2 V/ Pmv owasp-modsecurity-crs /opt/tengine/conf/
& x. E3 I( u; X; O
! _: w* R4 Z* y: V8 N: D: g" `cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 ]: l6 P* T* p) S2.启用OWASP规则:
5 ]8 h. ^! O4 a' e
. S6 Z8 _: ]  U: L. Q复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' B! |; G5 b& D5 p1 G
( C, Z3 C; y( ]% u, D7 B$ n* k9 Q
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
- ~" V6 n1 `+ G" H  K' t: _) X+ k7 m. A# w0 ]) H! e1 B3 H" c' H( q+ V
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
5 I  R3 t" M) B8 D- }: @1 F) M) R7 x8 [) G9 s
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
6 u9 V2 q4 r- o, K" G. YInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf1 R. `1 R- d- h' P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
! @% ^2 L& c. e' ?" g1 ^* KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
7 ?9 J. x& J9 V/ M' SInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 t7 e0 [6 V# o& w+ d+ ?! A0 TInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; |$ H3 G, f+ A
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
& _" G5 J5 |7 D6 l五.配置nginx. u1 y% X, F( n: M8 ]; e

( S' u: [+ ]( P0 K  M' ~7 H在需要启用modsecurity的主机的location下面加入下面两行即可:
/ V+ x* Q" m9 w% `
" C' B7 O+ x5 k! C% G" j$ mModSecurityEnabled on;  9 [) v6 W5 S6 {. B8 I: _
ModSecurityConfig modsecurity.conf;
% }& u9 i# v$ w5 d* Y, R下面是两个示例配置,php虚拟主机:
4 M3 D: H6 D9 J" V- `& V  Q- ?+ q5 L7 Z; n2 {( B& B& n& d
server {
2 e, J; ]3 a, T5 K9 i& g      listen      80;2 b1 ]6 a' ~! ~3 {2 O& p
      server_name 52os.net www.52os.net;& |1 ~! \( C: P0 ~: m  f
     
! w. d4 Q" @+ E; O+ j: e1 W      location ~ \.php$ {
; z- \: X) t$ O      ModSecurityEnabled on;  
$ L( N) H1 A2 T" c) R7 H      ModSecurityConfig modsecurity.conf;5 X- U' N  L1 g; o& m: y5 z% a

. P; t! K! H& u; F      root /web/wordpress;
9 J, J( |( X0 e: o      index index.php index.html index.htm;' T6 M! Y- a2 @3 v9 F
  ) U$ b+ \4 A( I, S: D& H0 N
      fastcgi_pass   127.0.0.1:9000;
" X6 A6 A7 ~$ t+ t, F      fastcgi_index  index.php;4 u8 n& r. E, q: z% A& O! d+ b
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;) A/ @+ G. f4 x! v1 e1 E$ R
      include        fastcgi_params;( a: S2 y9 r, _: D6 L
      }
; j- Q0 l. D- k  {' F7 O  }8 K" n5 i, q) n8 W3 M2 r9 I
upstream负载均衡:
% M5 k6 j" U; b; H  y2 S
2 g0 O/ {- N, ], L1 eupstream 52os.net {
3 A; ?, G* ?( {. G, o9 w    server 192.168.1.100:8080;
; S' k. _) h' Z5 Y1 `+ \    server 192.168.1.101:8080 backup;
2 j! G* ^2 f, {7 b& b5 a}
& A! R7 o. {! T! Q9 W
7 C* U# a  F6 O$ Y' ^server {( ]* n! ~+ ?5 P# z. o2 s& _7 B! J# ?
listen 80;4 u, t2 v- t0 j3 r" h
server_name 52os.net www.52os.net;6 ~# \, v  F% u/ x
, \2 c" C0 K/ W
location / {
4 G4 B. Q$ @4 m+ k    ModSecurityEnabled on;  
) F$ Z( L$ b' |9 s; ?    ModSecurityConfig modsecurity.conf;  
+ L+ Z, M6 S6 C- O
2 \' \2 T% [: Y5 |  |& _4 V. q' P        proxy_pass http://online;) A+ g+ n9 p) Y/ ~% J% w
        proxy_redirect         off;$ v" u, j5 P! D& T. N
        proxy_set_header Host $host;  n& _+ l2 S% b# Z/ O: E. @
        proxy_set_header X-Real-IP $remote_addr;5 n# r7 ]& e' z2 k0 _0 p% _
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;0 F7 S# n+ p7 D: Q* l
    }
; {, ], @6 R/ C! d0 W7 B}$ _4 ?3 L/ d5 x( [8 |
六.测试
- u6 N" j' U; t
' C  g9 p6 U* v我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:( S2 N8 U' ]2 |

1 ]2 o* S* W  D2 h+ V; \  z<?php; H5 g8 {4 G, s) ^* S- N9 F" W
    phpinfo();    5 }( X) g# f. H
?>/ K! K1 v8 N% l. O8 R
在浏览器中访问:- d' @3 r; f5 X: N% ?2 y
! q! V$ i9 d' d5 ?  d! @
http://www.52os.net/phpinfo.php?id=1 正常显示。
$ n9 |2 i( H9 o) w1 \+ N' ehttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。  V2 `  W/ w; J- x1 K: P
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。/ m: y  ?) y( h, m) G
说明sql注入和xss已经被过滤了- H' p) v- W; J$ r+ `. O

* @6 M- P% y/ N: s" X2 ^/ p七、安装过程中排错
- Z- `, O  Z) A% c, Z& y2 r! q; F, L3 r) ^& o  d
1.缺少APXS会报错7 ?6 N$ d+ p, S9 t7 @

  b% g7 I! H( Tconfigure: looking for Apache module support via DSO through APXS/ I4 [0 @2 v( N" i
configure: error: couldn't find APXS
- R  {% a3 p9 C" }1 ?. b* V4 Napxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
5 W; i% z8 T: [解决方法:/ r7 G) B- }! @3 ~( S

" ^- R: g2 r7 V7 X* L+ {6 c) j5 zyum install httpd-devel
9 R- |5 j7 i& `, M$ f) u  n/ B2.没有pcre# _8 a" s. j: |4 Z: {5 d$ x

& S( `6 N. Z/ n3 s" x9 Nconfigure: *** pcre library not found.
) d9 T4 \; T4 Gconfigure: error: pcre library is required' j; }# h9 z4 S
解决方法:
. ^9 i5 p. a, p' C
) u4 }2 ^, }% p9 L  o7 Xyum install pcre pcre-devel
2 h9 `4 L4 D1 j- \3.没有libxml2# r% F4 Q- g6 \* R5 d
$ T& v0 K7 b5 _( A1 a0 j- V. Z( r

  k% X! I# G  X4 V! vconfigure: *** xml library not found.) t* q: o+ @' `  p. v
configure: error: libxml2 is required
; u1 X. \2 a4 o1 m解决方法:1 O% b' R5 e$ G4 R
' G7 ~' j4 A6 q
yum install  libxml2 libxml2-devel
3 l! K2 y( x9 x  P4.执行 /opt/tengine/sbin/nginx -m 时有警告
. `2 w5 D$ s; b& U. s6 v; f1 w" v
Tengine version: Tengine/2.1.0 (nginx/1.6.2)7 G( |7 @# W( @9 d. K7 [- K" i
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
0 r1 Y& F" N+ j  R" _& @4 B原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
! v) g. p- `2 ]
# ^) g  k% S, r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.0 O( k& g# T# Q) v6 M- q3 J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"$ |: O; J" S  `9 d" e  J% l, `
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
) {  C1 Z2 F4 v2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"  H. H' S8 \& U: {2 ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6", w2 s; d5 V) }& I/ M. u- M
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
/ O5 U  r: L( m& E9 U. Z解决方法,移除低版本的APR (1.3.9): f! j; C! s; {# `1 a

" U. ]6 `6 G' Y2 B2 `/ K$ ayum remove apr
& p' M% N& U% t+ [# R1 f5.Error.log中有: Audit log: Failed to lock global mutex  z  c- J) \6 P8 V8 y$ I! C* t$ N

1 w9 ?5 Q* }' T( g' N4 G2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ' r( V8 W& K5 v
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
! l2 `, ^! h/ @& a) K9 m0 w7 E3 B9 m解决方法:& z7 G8 @3 p, V3 {( e/ i( G
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
7 Y6 n: p" u  i8 k2 Q  l4 {3 E4 S$ k! ~. @/ o. w
SecAuditLogDirMode 0777, D' Y. u; c* c& g2 G) H
SecAuditLogFileMode 0550
/ |3 z% _- G, }% V* U6 R. S0 g! QSecAuditLogStorageDir /var/log/modsecurity
; ]' [# ]+ M- g+ q! F6 {' GSecAuditLogType Concurrent
* g6 {7 H0 a1 T参考文章:; P9 k9 y' W* S! x9 \
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX8 r% i" r1 @/ t4 Q( y/ o: S
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-14 08:32 , Processed in 0.071846 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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