找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11803|回复: 0

nginx配合modsecurity实现WAF功能

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

" L% I; w; G5 d; b一.准备工作
4 {$ S+ j6 _9 ]3 r# [' A) \- T# v' L7 g! H) Y- H# F- ~
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0# @: r( j, N% W( l- e- _3 X

) u( ~/ e1 ^; F2 N; q: ?$ ~/ S& Ltengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
0 m* X+ H1 t& H: }2 p2 p
0 O/ y- k; ^. L& d. M# |5 Dmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
) h$ A, c$ B7 V( c6 ^2 Q5 y8 }4 }8 x8 K8 d
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
3 B. P6 ?/ p6 Q, J& U+ m0 Q0 g; Q& J- B& ?, _" Z! J
依赖关系:% w8 ^3 h0 i: v7 x& Y
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
0 H4 g/ b- h  O* P
. N4 P9 f) W5 Q+ Gyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel! H1 t- x0 n9 D& R  W6 b
modsecurty依赖的包:pcre httpd-devel libxml2 apr) M' S9 _: w4 b. M* [7 D% i! p4 c
  I7 e$ F% p6 d2 F
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel, ]: m, J" n2 v
二.启用standalone模块并编译
/ j& g0 g% @+ ]) W! T; Z
, @8 T1 |2 l$ [2 E下载modsecurity for nginx 解压,进入解压后目录执行:
5 Z+ X) x- C. U1 j. T/ d" f
) F) I+ k6 O2 ~, v. Z7 J& h$ x) }' k( K./autogen.sh1 J5 n* [1 M- t  z9 H' x$ z; k- _2 F
./configure --enable-standalone-module --disable-mlogc
; ]6 A8 d0 G+ F: b: }/ `make
( c$ C1 n% j+ H. u三.nginx添加modsecurity模块
! m/ Y4 l7 \! r$ u7 D, m! q6 x# X
$ r: R" ]7 ~: f5 r" W& a在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
9 r1 u- E* G( z, i" _& @; a( M) l4 z0 ]/ {
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine: C9 D3 g. m/ G8 n. h2 @4 q
make && make install$ q6 e2 X5 L7 N
四.添加规则' S" z6 m+ K" E6 \6 b4 p: J7 A
6 z& O: u5 a( \6 s% n# l2 N( y
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
: j3 Z. J1 t' S$ q; f: m% N9 N& x* p1 n# X# B
1.下载OWASP规则:2 j8 _+ D/ `: L3 n4 |2 F8 D
+ y9 ?3 I9 X1 R
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
! ~( n( Z- x- s& j+ T
+ w, B! ?  ]! L5 t+ \9 emv owasp-modsecurity-crs /opt/tengine/conf/
2 l& `4 l9 U9 f: E6 g
' a* P. A& o4 Xcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
, B, z7 H7 ^+ S0 H8 m8 |2.启用OWASP规则:9 v3 w; V# s( x1 I$ w  L

9 F+ a: z' t1 L0 x6 G' y4 L5 {$ O复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。! A: j: A! h& h
& p1 w( ?  L; G* H& e% `* G6 A  J
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
! b; }) ^0 i5 ^1 f3 i1 t1 r( L" {
& ]# S* I6 J9 u. e4 l) A6 powasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。& o: I! q/ \( B  |7 N

( C- n- X* f6 D& k4 K3 XInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf) a7 |+ I$ J1 r0 D9 A! }2 _+ s
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
9 b2 A0 v! w$ Z- X- d6 OInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 T; t" H" S3 c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
% y! J# o% K8 K% e9 ]% ~Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf: v$ P" V7 q1 f% G6 B! a: t
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf: p. C; I( e6 `1 v  H
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf2 Q, ?0 w6 M" ]6 j7 H: g! K
五.配置nginx
  k" [* R. ?( R' |0 z- {
" W, _# t$ |2 z% H在需要启用modsecurity的主机的location下面加入下面两行即可:$ |3 _& A! y. n/ O2 Y4 J
2 j, N3 v4 T0 {
ModSecurityEnabled on;  ( Q' V" d; _0 K! P
ModSecurityConfig modsecurity.conf;" ~$ X- D$ l: f2 a: `* I3 j! O
下面是两个示例配置,php虚拟主机:4 n  k4 O: O  P& |, S
3 Z7 m7 f% M9 A% e* {0 ^% W
server {
0 x1 t# N( q, [      listen      80;
) o8 |- q) J- g' D5 [) ~$ S      server_name 52os.net www.52os.net;7 s  q& i+ ]/ c' \
     / y; N% f( n7 D8 b
      location ~ \.php$ {+ g2 I, _3 y7 A$ y
      ModSecurityEnabled on;  
# @; D, ]1 X" X$ C. F      ModSecurityConfig modsecurity.conf;3 }& O- T2 N# H* F& p: Q

5 e# E0 m2 [' V7 T      root /web/wordpress;2 G) [1 O) V4 v7 P& t9 e! v
      index index.php index.html index.htm;9 D1 o6 L" ]4 l$ ?" U
  
+ U6 Q. o, Q$ s$ a. \      fastcgi_pass   127.0.0.1:9000;
1 d* ^8 L% U7 A' M! z      fastcgi_index  index.php;1 N: I- B% ~+ b( w, q) I
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
' U/ E* ?, ]* X9 u3 S( J      include        fastcgi_params;
4 K* ]- }( Y6 a) r      }
  f% n( V3 r' R  }/ z* O6 l. l( a+ n8 H# s
upstream负载均衡:
" j5 k) M) d$ M5 Q# e) f
6 w& j+ o2 U+ m( g# Y+ s# B+ V/ ^upstream 52os.net {2 C" s* K6 U1 e0 q4 r
    server 192.168.1.100:8080;* f7 S1 A) M/ z) e
    server 192.168.1.101:8080 backup;* w- R. G( {2 \" G+ I
}, T3 b! E6 N( ]4 k; G7 J

! I* g! m3 Y+ e  Pserver {
. y! [& c) E2 O. v% d2 A/ v0 q; |" Olisten 80;
+ l) e, G* C  S. |9 V: lserver_name 52os.net www.52os.net;
2 L; u+ v) t# {: X; m/ P+ l
8 X7 f7 L) I% ]location / {
; m3 m( `9 u: l9 ]  p. \, f! T    ModSecurityEnabled on;  0 M1 V* |" _+ U2 s
    ModSecurityConfig modsecurity.conf;  
& G8 |0 Z8 a. b
1 e# @$ x3 s4 Y+ {6 y        proxy_pass http://online;- _5 n  L* s3 b. T4 n
        proxy_redirect         off;: g  g% r$ K% G5 v
        proxy_set_header Host $host;: W3 }3 a6 L: I7 V3 ?' r3 g
        proxy_set_header X-Real-IP $remote_addr;
" i* x  I" Z% v" r4 A) w) n/ N8 f        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
. e, `" G* u% H, ?. s. U4 V    }
  `5 H" v. _4 a% p% Q: _! Z- o' E5 R}% [2 M* J6 J  {% b4 E  S& a8 Z
六.测试
0 r: U4 c% z* e4 x3 X5 }( |3 m& a
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:8 u& ?; M, g7 \/ |1 s* h
* f+ p* ]+ O/ I- O% ?0 O+ o
<?php) u$ w% p5 q' `2 n
    phpinfo();    ( K% h( s' p' m( s
?>$ E" ~" v% w( T- p6 C
在浏览器中访问:
& |% P% V/ u9 d3 k! l, [6 B/ H7 h3 j
7 L) n1 |0 E+ `) w0 {) Hhttp://www.52os.net/phpinfo.php?id=1 正常显示。% ?- y5 {7 J6 y* D( T4 W9 Q+ J/ k
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。2 K! z; `9 ?6 v% U
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
+ k1 E2 ?* m1 e0 r2 o说明sql注入和xss已经被过滤了
# M& {. S' f% O
8 q. O+ {6 [6 r- J七、安装过程中排错
* L) U' ^4 n6 R0 O/ z3 e9 i9 |9 M/ w5 c; ~% D) ~1 |+ t5 A
1.缺少APXS会报错- @7 R4 o0 Z# o, V* A7 e- T" u

; W  Y# X: P9 `' I% j+ t2 gconfigure: looking for Apache module support via DSO through APXS, g. C, N0 q0 t$ U* E0 F" E
configure: error: couldn't find APXS
  R; c3 x( b! m5 O' d8 b8 a9 lapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
% r! r& y4 ~3 W  o3 m- A解决方法:
- N. X- m1 K* l3 [- N9 s* i6 n+ w
$ }; ]& ~9 z( e# t9 L+ Iyum install httpd-devel9 F+ {% u/ F3 O9 O7 c8 O
2.没有pcre% d+ R' m) o6 J5 c
$ K$ [# X5 ]4 p
configure: *** pcre library not found.& d8 x! Y8 b8 m8 r9 Z9 m0 S
configure: error: pcre library is required
& f) v& k8 m; ?8 \, w1 z- e解决方法:) t: y2 ]1 u- n9 K* i+ [- E
2 A5 c9 C# t1 A( R2 B( X0 t' _' ?
yum install pcre pcre-devel9 e5 l  D* `+ H( X
3.没有libxml2/ Z$ J1 }' Y/ N! a
& H8 N5 S# Z' w9 p8 C% P

: X+ b8 r5 m; @/ G3 Fconfigure: *** xml library not found.* F+ R! z" k* \5 z) w. f  a
configure: error: libxml2 is required
& w7 H4 m1 l! J& Y8 v7 S5 c解决方法:) {6 e- Z7 N0 V, |: r

3 O0 L5 H4 d- i4 U& o( O& myum install  libxml2 libxml2-devel
/ r3 O1 ~1 T. J' X4.执行 /opt/tengine/sbin/nginx -m 时有警告
( D  c$ I/ l. ?2 U1 ]' z& F- f$ @/ r/ m, X
Tengine version: Tengine/2.1.0 (nginx/1.6.2)$ k1 h+ e' r. P: S; [' W2 j
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!3 n9 s1 v5 ?9 G) X
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
1 P! f5 v5 d% ~. `, j
) r/ i, M4 M; i6 f. d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
8 E( Y. _3 ^! ?2 ?. ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"7 c: u1 m: m/ {/ B% K; w. E# Y
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
) z! g4 H$ Z; M6 ^( i' d  z# K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"5 ?9 L9 \$ c/ z1 @  o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"6 w: {( A- P4 \7 N) B5 B
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.4 _# M9 U2 K7 O" V  }0 H1 t8 W
解决方法,移除低版本的APR (1.3.9)
, _) A- b3 @& m
* M2 H$ z/ i/ @- V5 |$ [  y8 M4 |yum remove apr
" R/ e% B* Y- h4 V5.Error.log中有: Audit log: Failed to lock global mutex! O# Y2 u# Z3 C4 S7 F

3 W0 D7 w7 Y9 m# h2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
3 |5 j- i* H2 u: v& |+ E0 U0 mglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]5 A7 z( C! J6 u7 V# W' A/ ~+ [+ t& `
解决方法:  i9 ^$ H6 G* p3 i9 O( `
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
0 U8 N0 O2 `$ R2 R! j1 x
% A1 G' E$ D% ZSecAuditLogDirMode 0777* u- K3 g& F" o! }) P! n" p; \3 l, h
SecAuditLogFileMode 0550
; N; i- L# \1 JSecAuditLogStorageDir /var/log/modsecurity- Z3 |6 x) }( P  [3 B
SecAuditLogType Concurrent
1 h  \: w$ U4 |5 w: `参考文章:) @) C  U. A$ ]! {0 M' c
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX  M7 L9 R) P  ^: l! I, d9 c! a
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-15 11:52 , Processed in 0.062190 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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