找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10040|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
1 m# h; d5 q- N
! J1 K) G/ c. q! Q: ^3 {6 ?6 F一.准备工作  ]* k6 f9 R$ T3 Y8 M+ {# }$ j) ?
; _+ a# o! V! m3 I3 ]" j# E
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0* O5 Z* c5 G& M9 d$ r" N1 H8 v, S

/ e0 F3 }; o) q5 X) i, a$ Atengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 t8 q1 t! b4 O/ c
5 ~5 G( v! V- k+ |4 ^# i" T
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz0 \% E4 H! W+ a8 q, K
% n) S# G6 T! k& x8 a7 }# L
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
# X) r4 l) f% H% A4 A% ?% F$ M4 m# A, G& L3 {  I
依赖关系:
% H1 u3 g) k/ U: U* Vtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:* s  Y  `- F, m) h2 G

' E8 B/ n# o5 q" Fyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
2 r+ B- W/ j$ f0 r, F1 ?4 hmodsecurty依赖的包:pcre httpd-devel libxml2 apr7 k2 F! Q) `+ x8 C2 q3 q! ]4 t
7 y( Y/ C* o# f/ c4 M4 G4 B
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
) `; s# A: y' ~& H二.启用standalone模块并编译) ?% I5 c' h) W" }- Y2 E
+ J- P0 x6 V6 V7 f
下载modsecurity for nginx 解压,进入解压后目录执行:& e4 \0 y8 x; f1 v! c

! s$ E; S- h1 Y) D./autogen.sh- v; ~  e& c6 u8 i2 `
./configure --enable-standalone-module --disable-mlogc
" P6 p0 X% G" G! b% Bmake
( m5 p2 d: ~) U" F9 w: c三.nginx添加modsecurity模块
$ O, U+ h+ |2 A
: \9 w5 f, L! T0 l在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:4 f4 {, z9 U8 {4 P- o
5 C$ n, c0 Z" _& ?" L
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
6 F0 a1 }4 N" A( Z' }7 ]make && make install
0 c. R* Q' R7 f" M1 z四.添加规则
1 o& M0 W( q8 W9 }' X" {+ e
5 {$ o' {1 x# @" q0 z' \modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
. N0 R* i7 D# |% U* N" a8 X' V8 w$ D; p( q1 |  y
1.下载OWASP规则:
' l% u7 l: p% \7 x
6 j1 C3 L4 M! t# mgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
& k1 Z: H2 N% Q/ \
& L- ^) B& X9 T: ]/ q( g9 z& `mv owasp-modsecurity-crs /opt/tengine/conf/, d' [2 m' y6 p" z) O
& O; z% q' C" O0 O6 e' b
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
  e( M% Q) z! w$ T4 Q( A& M9 s1 t2.启用OWASP规则:
: m1 v+ i9 ?3 ]" p$ ^, U  c9 n( p( M, x. ~
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
1 Z/ n( H1 U9 w3 r5 }& _7 `; T7 y" x$ M: S
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
# L, h) Q; x+ F' c) N7 t9 Y  `* V4 v; Z, T) h. H/ _
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。- |8 Q/ y; Q1 y$ d  p; y; P

$ p* L$ F7 I: J. {; _- uInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf5 @0 g% J+ J1 y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf$ M4 |8 A0 T5 b8 A2 k% c. [
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf! L4 I' E8 [  r: T2 P% T- J
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf5 j8 i4 d% m9 R
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 O% K" H) X5 q2 g  x* h. f0 XInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
0 j( \; E* v  FInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf* p) M! y! |. d8 A' `
五.配置nginx
6 ~- u0 F% k6 ~0 D  P- @/ a- S, }& i5 F. m& F6 R1 |2 h+ Y
在需要启用modsecurity的主机的location下面加入下面两行即可:
" e+ O7 c5 T% ]7 x! |2 }( m9 A7 o6 W3 N" b% X( q
ModSecurityEnabled on;  
9 V; S& F& F% U8 \6 b; D2 i3 JModSecurityConfig modsecurity.conf;+ R/ k/ ?( ?9 ^1 V5 N8 Q: S( |4 g
下面是两个示例配置,php虚拟主机:
. p. F1 D9 i! I5 S
$ i8 D( M4 r+ R4 Dserver {9 f4 ^$ I$ V5 R+ X
      listen      80;
3 _' [& a6 N) i      server_name 52os.net www.52os.net;$ h7 K9 O  Q/ A2 O
     
/ i, ]; z: {2 }* U9 Q" z1 b      location ~ \.php$ {9 Z) C( }8 @9 ]+ A+ p8 g* t
      ModSecurityEnabled on;  
! A& M9 a' s( w& A/ T5 G! s      ModSecurityConfig modsecurity.conf;2 ?! V8 T% T9 i2 e  d: e& A% U

$ d, ~, I; G' Y/ R/ S      root /web/wordpress;
% V4 t8 j( a; O      index index.php index.html index.htm;3 t( P7 l% ?7 J& k( f2 l- d0 ~
  
, x) k* _  q3 c. P: G      fastcgi_pass   127.0.0.1:9000;
  R" l& q2 p4 \9 y. d9 t8 S      fastcgi_index  index.php;5 g% p* k+ Q1 Y$ G/ G% g: e
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;$ R) ?8 D+ [9 e, _7 s" p/ w
      include        fastcgi_params;
9 _. O$ A; |6 e* e      }' E, P* ]# }6 e+ z: B5 w
  }9 q8 s* U& d2 C  u% V
upstream负载均衡:
& ~/ h! F3 G1 [0 D. I& U, Y! V
upstream 52os.net {. {0 h3 E% A- h" s7 o7 Q
    server 192.168.1.100:8080;% y; ?" _* q1 E( U9 @2 _2 m
    server 192.168.1.101:8080 backup;
0 P; m% s% [9 @) k3 X4 Y}0 I! l+ u4 Q  `
" u# f. K$ w6 m4 Q
server {
; A, L9 w/ i6 mlisten 80;, h. x/ d: Y0 h) k$ b1 Q
server_name 52os.net www.52os.net;
  h3 e3 {5 g7 b8 x* u( F
5 ?, M8 S; e, dlocation / {
9 U3 ~3 V& e% y5 g2 c4 h    ModSecurityEnabled on;  
8 h  D5 c( d- x3 c8 J+ m/ A( f- ?$ s    ModSecurityConfig modsecurity.conf;    N- |0 p9 ~3 V6 s

5 A+ _% L' z7 H' @$ Z        proxy_pass http://online;  ^$ d% ~1 R9 }2 _+ b, _6 z9 K
        proxy_redirect         off;9 M, Z& i  ]  ]0 x
        proxy_set_header Host $host;
7 s" s7 T' O  Q% |        proxy_set_header X-Real-IP $remote_addr;
# D" f0 `+ c5 a9 w+ j/ I9 ^' y3 C        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
+ }* q& E$ g, w: S    }
7 ~# `: o! c' S' X+ u) j}
; _& J; L/ q6 `2 [  m六.测试% H! h: D7 h/ I* \3 ^# u3 ]+ h

! ]" \) z$ n7 k, Y1 Y$ z我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
6 R1 |3 {7 \7 O/ s$ K+ |- P
  ^1 i+ N0 |  Z0 g6 F<?php6 T5 z' e# Z5 M" r% S2 W; p
    phpinfo();   
+ L2 Z3 D5 @9 G1 q# S; w& w% U+ s?>
- f1 }2 W& Y0 s; E在浏览器中访问:% i/ H3 B! B9 _; m% p

7 [6 J4 _( s  ]1 p# ]http://www.52os.net/phpinfo.php?id=1 正常显示。
3 b! A' I- [) K4 ^http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
" O8 c( ?" @$ N  ehttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
" n" K5 R' P0 _8 B说明sql注入和xss已经被过滤了
( f# o7 ]! M2 M% e  w' ~) E5 g8 P
! D: ^! m8 B; v! [. g七、安装过程中排错
  c( q  n2 N' ]9 d% w) ^2 R" d4 C, \2 X, Y- J
1.缺少APXS会报错
. u8 o4 a6 i+ a' G- U* ^0 ]$ d. v, J) K' @" l- ~: Y
configure: looking for Apache module support via DSO through APXS
: ]' k& y' z" X. }5 ?7 k9 Dconfigure: error: couldn't find APXS
/ B8 w+ ^& k' v3 x" p( e/ ~apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! H3 s- D" A' H3 B5 g1 g
解决方法:5 G4 t( X3 g$ d. y7 P1 R# q$ X
8 P$ s# O! W: J7 z3 z8 ?) ^* p$ L' Q& Q
yum install httpd-devel
/ r) U2 {' D/ v# G6 l# J2.没有pcre, g: d; W) ?- i2 B7 x6 i

9 C' a: v% I  @; ]  Cconfigure: *** pcre library not found.5 a* F3 A; ]0 v/ k1 X- o; V
configure: error: pcre library is required
, w- n* O# {  w/ b( W/ H9 A解决方法:$ v: Y$ S* i4 E

$ H7 q+ B- T2 Lyum install pcre pcre-devel1 Q. x- O! J4 v/ v/ a! G/ R# N
3.没有libxml2" Y( O+ S+ H" {6 e. X3 g! L

0 y# L: ~' z* N' [# z6 L" k% B; s  ~2 L
configure: *** xml library not found.& B" d$ p, Q# G* Y% D' G9 ]
configure: error: libxml2 is required
# Y3 \/ k0 W1 p% N( K6 g解决方法:
8 d5 T- V5 r" r* t" r2 ^0 B! ]$ h- h% j" u2 c3 w
yum install  libxml2 libxml2-devel
' Y# b* L. o7 d) j2 w( ~" T4.执行 /opt/tengine/sbin/nginx -m 时有警告
$ w# `1 E$ A5 C5 K% I' u( ~' M; L
$ _  `5 X% i( t$ @8 }3 cTengine version: Tengine/2.1.0 (nginx/1.6.2): [# j1 @6 @+ s/ V1 Y7 Y
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
) B& U1 C9 g# W# d3 t原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log/ d) {, b1 R/ V3 p" S; U9 E' `
1 p4 P. }% y# Y! A: G+ Z  }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.9 N. N' l/ e. m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
: w  @/ r" v2 e& i+ u2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
+ |' V& K: g' `% q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* q& }: n- l  h, s* K; B
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
  f" f+ U$ D/ M8 g- U3 w  T2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.0 v8 [& I  K  g( ^) D2 p
解决方法,移除低版本的APR (1.3.9)
5 g# k" @# W+ x- c7 L9 y- {9 |4 M: f) i( t$ W- O3 a5 J
yum remove apr; u0 i  \$ ?! y* t
5.Error.log中有: Audit log: Failed to lock global mutex
+ y, y4 Q8 x- \8 s$ h: O0 n# j! l
$ A7 d; J- w0 \+ z  B2 z" ]9 M2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     0 m* C$ k% L0 u/ L- a3 F% T( Q
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]4 ?. h& f5 ^5 ~9 @& g- B
解决方法:
/ B! F; [. ^" `1 q编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
" r2 F+ S8 B; n# n* m' r7 `4 o* v% X; x( n3 ^& N/ S( E; k
SecAuditLogDirMode 07773 K8 n& o, r$ U' G- ]5 U6 h
SecAuditLogFileMode 0550
$ Q8 X* n* I1 r: t7 Q- G4 V+ @1 u7 GSecAuditLogStorageDir /var/log/modsecurity
- D- t! d" T$ c2 ^6 }SecAuditLogType Concurrent/ N* w) A* y2 V6 z3 S) D
参考文章:
/ q- y4 [/ V( O5 O7 Yhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
6 @% H, E0 J3 v' l8 P5 ]http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-2-23 17:48 , Processed in 0.054215 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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