找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11879|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
0 W, \$ Q8 w. \
+ {/ i: @. J  k! {4 q* n一.准备工作
' N3 A/ p% ]/ z. a! u8 A7 y) h+ ~8 ]$ [( ~+ y
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0+ G/ R* ^% _2 X" k
' z# a1 H$ X( F6 i
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz" ~8 T  w0 Y1 \8 _5 U
( W% v3 y  h% s+ g+ E% J
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
5 P$ P. O1 C6 q- v; P* K" Y; |% X. V- ?% Z# `
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs) [6 m9 q. k4 x$ d1 U4 B/ g4 F1 u
, J* X! V9 Z' x
依赖关系:
3 W* Q8 M, C( l, y8 _0 btengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ i! y$ e5 j1 q% H# g
, \8 p$ E6 J* `3 J" qyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel% W- q9 ?' T% F
modsecurty依赖的包:pcre httpd-devel libxml2 apr
; V6 Y2 n- K8 f: n/ j8 n- [7 b
  Q1 l  C7 y* x) C5 y9 tyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
6 \; Z. o3 a( x0 O- I3 H4 ^二.启用standalone模块并编译
+ r3 L1 Z9 K3 a: X
- L& C+ Z- J( n4 b( ~3 n下载modsecurity for nginx 解压,进入解压后目录执行:
; @. a  [. P. w8 Y& L* I0 ^$ U7 p. T# ?$ ]" D6 J$ m4 v! ^" _
./autogen.sh
4 P3 u2 U! J% m4 b  T./configure --enable-standalone-module --disable-mlogc
% x8 f1 z; h3 a* q' h0 \/ S, \make % b; _) y, P, D$ z+ H7 R' B) e. c
三.nginx添加modsecurity模块
! Q: A" o' y6 m9 f& k  I" p
$ v1 e3 I9 P) c; c在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
+ L1 `$ J, f2 {. L# s
, W! z5 Y6 J. A, A./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine% B/ K+ m8 @5 }9 [
make && make install1 `0 w% a* n0 {& T( o
四.添加规则' M$ q3 O7 i  y& g; a& h% H2 s
$ P4 G  X+ h) i$ C. l
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。0 E6 x$ e; L) r+ p/ d

% ^7 j2 X# G6 t6 ~7 _# U; `2 ~1.下载OWASP规则:0 y$ A+ ~3 B4 ~- a6 D5 P& ?

2 X$ Z8 T: Z% @  Jgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs! n7 x' W  G; n: }) Q
" J6 v: @/ y, C5 A9 r
mv owasp-modsecurity-crs /opt/tengine/conf/
8 C' a& z# h# ?! ?/ h# ?( M$ a4 `4 e9 r: z4 ~9 U# ?2 \
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf! N: v' H) n2 G( x5 G# m! }! ?* k
2.启用OWASP规则:9 z. s& k) |/ U7 W
  u; Z( i: q/ @3 r
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
1 i5 W/ d9 ^* N8 v5 F
1 A( _7 C  X+ V编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
0 j9 R8 W; ^- N
+ W3 ?. J$ M+ ]! o2 E6 s1 [& _3 powasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
4 d/ S/ o7 D) L/ N- D4 B; X3 V# U" w4 l/ s& @2 ?7 Y
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf7 {4 ~; D2 R: ?5 F: o
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
! p* c- u5 J2 [% b, OInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf* ^7 Y. ^* C* Z. u2 T( k5 v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf3 C8 r# F( q, A" O7 p5 m* K
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf" U$ m0 H* J0 s, W: ?) I0 r! v/ o
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf# ]1 r% u. B. B* r! }; H
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf) b: R. h) v, x/ h4 H
五.配置nginx
) H# v; c: B. Z) C: s( ?. f9 S# Q+ L( h7 i2 c
在需要启用modsecurity的主机的location下面加入下面两行即可:
6 ~/ R( a- ^. o" u' w! T! o
- |; D6 L2 j% \" C8 p/ e) y/ EModSecurityEnabled on;  
- m8 j5 L9 U1 RModSecurityConfig modsecurity.conf;' l0 c) \8 M+ }
下面是两个示例配置,php虚拟主机:; H" P) z2 k9 [1 d0 ]6 n3 E1 M8 a# m
! s! T# j$ [3 {6 D. M' H1 x
server {1 Y$ }6 }5 b; g: @# f8 D
      listen      80;
8 E" p# k: q5 A' ~$ ?9 z5 u. e, A" U      server_name 52os.net www.52os.net;" z$ Y4 M$ `3 s; H
     
, p2 [" J8 `. b6 Y0 O! k" t" k      location ~ \.php$ {
% R& v8 {! @6 k$ o      ModSecurityEnabled on;  
8 n9 y4 b. D7 r) r! D; ^      ModSecurityConfig modsecurity.conf;+ ~( g1 y, `5 H: G
2 r+ t( {. Q+ {( j+ r  A
      root /web/wordpress;
  [4 z) b# }2 [- m      index index.php index.html index.htm;& a$ D( s, H' {- X0 _
  : a( P, d+ k7 ^7 D- I. d! l( S# Y
      fastcgi_pass   127.0.0.1:9000;+ ^$ D9 S; ]; f9 b: j( I
      fastcgi_index  index.php;- h; _# z% O% c# @* G% [  u
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;) d8 M  w# u! s  j% E! A
      include        fastcgi_params;- {9 U4 S6 h; `9 B6 C5 V7 g9 Q
      }
5 ]1 y1 |7 Z, q2 S. Y  }/ \) r; d% l3 f
upstream负载均衡:3 L% @8 ~( r& k" G* ~/ r( Y) R
7 \/ r& E( C' o! u  @; N, W1 @
upstream 52os.net {
( b9 ^6 c4 E4 T4 w. c6 Z' H    server 192.168.1.100:8080;! R; x6 b. i# _4 B1 w4 ~6 X4 }
    server 192.168.1.101:8080 backup;
# c  L" I0 B  r- u( @( b}
2 \, ~9 N2 J: v% |' |0 T( c9 S( O. l" d! F" M0 e
server {
; W& H; L+ s$ e% Blisten 80;$ g# j2 G( m* c# K7 n
server_name 52os.net www.52os.net;
( D/ {0 g' R2 z3 V" G5 E. e) y- B
location / {1 j4 Q7 P2 d8 D; B3 l' t5 R# z
    ModSecurityEnabled on;  ' Q* L' T5 p  c, M; m7 r$ N
    ModSecurityConfig modsecurity.conf;  ; Y8 G* j" l" [0 X- ~$ u+ S" G

* E& i7 F- ]* h: ?        proxy_pass http://online;! t, S6 M. f) N! I& u
        proxy_redirect         off;
3 N/ g( l% j- ?& B        proxy_set_header Host $host;! H- t  s! v) F8 B& S, n
        proxy_set_header X-Real-IP $remote_addr;
+ r( [( w* @. y" J        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;4 f5 j: }( I; m  `
    }
: v) V5 m8 y5 f: D7 W' P5 a, n' C}
( I. L% t% o  ?六.测试1 R5 \$ Q2 j  Q9 T6 a0 J5 Q; v4 }) M( M

6 z2 l5 e8 L2 _1 i7 w4 H* D我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
4 p$ T2 k  i1 z% S2 M6 y
) Z8 u9 t. B0 O<?php0 V' I& u( N5 r6 ^; T$ D9 O
    phpinfo();   
' p# C, D, ?" `# Z( m5 T/ e! J?>7 E$ }. Z/ X* X: V
在浏览器中访问:
' ^0 N0 b+ _2 Z. n) c* Y& l/ }! C. k: }
http://www.52os.net/phpinfo.php?id=1 正常显示。: a7 W- {$ r1 y9 }! I8 D
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
3 d- j' Z$ b* ?- f8 l2 B" M. Qhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
& m2 w2 y3 [5 m; ]5 N9 Y# o说明sql注入和xss已经被过滤了, t- \4 n" H. S: ~+ l, n& h4 n

0 I/ g' |2 R1 x/ O/ o' M0 z- N七、安装过程中排错
8 H. b0 G# x5 |6 s; V& h2 ?% X
" T- M! G# k% M1 h/ R+ l1.缺少APXS会报错
  `/ ^: D7 C& K: }% r# S; A
' V2 r/ o2 M, }. d3 yconfigure: looking for Apache module support via DSO through APXS; y2 A$ b' H0 s
configure: error: couldn't find APXS2 ~, P2 ^& \# p$ G
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。+ u1 a% L5 j! k' ]& Q
解决方法:
5 l4 |6 ^- A2 ]  ^) n3 V' G, C1 A% U, G; ~/ f( [) p# q- M
yum install httpd-devel( ]4 a# ?: m( {/ |5 \$ H: C3 ]
2.没有pcre+ z  _2 p. u9 a
; ~) K  G) @* B6 N; h0 n
configure: *** pcre library not found.
5 B1 u  G7 t' d8 Q* ^configure: error: pcre library is required! D9 v' p+ v: o( _) r
解决方法:0 F$ U1 x2 H# [4 ~; k

* @& O# N# a, h8 d4 T5 S' Vyum install pcre pcre-devel
+ w* F1 g/ ^: g( o  q! O3 k3.没有libxml21 k& ^8 A* }, M7 b

9 c3 R% g6 {* R4 R
; B; Y5 L$ P. Oconfigure: *** xml library not found.
8 L0 p) N- `; w1 bconfigure: error: libxml2 is required  m4 @1 D" b9 g6 H* L( T& g# H
解决方法:
; {6 @' D! k0 \0 e( d
, \. j* ]; {. L" k, B( zyum install  libxml2 libxml2-devel
# y! g3 R# H, y2 q' n9 |+ S# A0 r. Y' ^4.执行 /opt/tengine/sbin/nginx -m 时有警告
9 x' r( W" Q- F2 Q6 n( c
6 y% A; k0 \+ q" a* dTengine version: Tengine/2.1.0 (nginx/1.6.2)
  W3 j! q" r0 p2 @& E0 T( v- D- @) snginx: [warn] ModSecurity: Loaded APR do not match with compiled!' J! G/ b3 v  ~" |  [8 q* V
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log4 ]) d, Q9 ^& V( P& j  i

5 E8 N9 Y# ^+ C' [/ J2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
! A- e3 R8 G! J/ c3 p- I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"6 ^- P* i1 p' D) s; w: q2 q
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
6 O0 U* R* l; I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
8 Y+ j& C" V4 t( D1 y  R- b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
& }+ @% L9 i, [7 n2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
8 ]4 `2 d$ b9 Z$ J1 D解决方法,移除低版本的APR (1.3.9)
/ \+ ?& x& l, A2 O2 z' P" ~! e+ a5 @; ?: k. i6 m
yum remove apr" ]: D/ w. K1 H& b3 w
5.Error.log中有: Audit log: Failed to lock global mutex
& Z$ M; X) \8 V: K7 P  M6 l
* ]5 p: L1 T! H2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     $ M# |3 X# n4 M5 N" o% X$ t5 O+ J
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]  T) M: _$ u, z8 d+ l
解决方法:6 {, O4 O5 s0 S+ q0 p
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:+ D5 W5 J0 {0 o; t: M  _
% P, A9 m% n, n) }, k. y* `7 D
SecAuditLogDirMode 0777
# h; t. R/ t5 c, uSecAuditLogFileMode 05502 R. E0 l7 }5 W9 C
SecAuditLogStorageDir /var/log/modsecurity
7 G) [% v) `) `& nSecAuditLogType Concurrent
/ I% Y  j3 O1 r. Y" s9 f4 n# x/ y参考文章:
  S6 J- W  a2 Mhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX2 j9 |& P! ~( y
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-21 23:39 , Processed in 0.075475 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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