找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12636|回复: 0

nginx配合modsecurity实现WAF功能

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

2 D" r) d, Z; f" @* W% Q1 ~9 a一.准备工作; j! n2 v7 A0 R' U5 g$ Z; w  e
2 p8 d: `' ~& ], X) A$ ~) z( P& Z
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
; }( N, a4 l9 L' M+ A0 P" L% @8 Y% E; ]
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz$ u0 j( O) o- D% t4 {
) x, H0 Z/ ^6 j8 m
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
1 t% d9 Y: B! L
# p* r$ q7 S6 m! k6 B: s$ [4 q# hOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
4 w3 U4 \# _9 q6 U4 F# n0 d& `% R' i9 _- K! @9 \. M
依赖关系:
* n0 k/ T- I7 ^tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:1 U5 P4 X( g9 H3 i

" u7 W: [& z8 F6 J4 g* ]& kyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
7 w8 j/ g, o2 _9 _: emodsecurty依赖的包:pcre httpd-devel libxml2 apr1 D! H4 L/ I6 B7 _% Y9 a

8 ~1 c. s  C  w* yyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel8 E/ U! C: k7 t8 g% }
二.启用standalone模块并编译% t0 ^: u7 Z3 ?  R, D
- \/ a/ N- c  |5 o5 Q
下载modsecurity for nginx 解压,进入解压后目录执行:
2 R* g4 o  J! ^! s4 T6 C9 h) o! R6 |( S8 Z
./autogen.sh) q) Y! I* R) K" a) u
./configure --enable-standalone-module --disable-mlogc
2 \7 R$ k( K6 K: O5 Zmake 6 G& T  @3 C' f8 H
三.nginx添加modsecurity模块
3 G1 t8 s# w8 @, `* H; G6 N* b. ^! ]: m
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:$ V2 X% }/ e) k9 z6 n2 l/ I6 D
/ q. N! y9 B1 C' M5 x+ r. Z- Q4 s- V
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
9 x9 l, Z2 b% Z  c  j7 p% M' v6 d& lmake && make install
" I9 M! ?; C! z四.添加规则( T' F0 T  U! z* q4 u. W1 B

: T' s7 S+ [+ }" O# ]8 dmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
' q# V* E+ g- t$ f, R- d( Q* g1 z! V+ w! J
1.下载OWASP规则:* J1 ^. O6 d, Q2 I. [3 s

2 c# `; d( V! o" @git clone https://github.com/SpiderLabs/owasp-modsecurity-crs. d% z9 T4 t2 s4 P& a5 x
4 H3 t; n0 a! H0 C
mv owasp-modsecurity-crs /opt/tengine/conf/" f* i/ E& X" \

5 ^3 r9 _5 i6 jcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
3 e! q, F4 e% g  k9 a% p2.启用OWASP规则:
  f% D/ K6 h1 S; Y) p) i9 |7 L. h+ W$ p) `2 T" N/ _) @
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% ]- G& a7 d3 @5 f4 R0 E2 ]! A$ d
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
) J1 }* c5 m1 `! C4 W  X* ]1 P0 ?
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。2 F8 \3 i& _" J$ t7 [

+ x0 G6 r1 m$ d6 qInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
7 [+ N/ ~. W: I0 e- TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf& i! M9 L- P' W1 O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf: U* R& G3 ~# f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
: b, p% W* _9 n6 ^9 d0 JInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
  X9 b' C: j1 T! lInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf/ \/ v" x1 k/ p8 D
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf1 u& _+ V! y9 c, x/ j
五.配置nginx8 Q9 K8 x# S0 {: W% S- V

0 P1 T' S' Z+ p/ y: b. C7 L  `在需要启用modsecurity的主机的location下面加入下面两行即可:
. g' n# J5 \8 J( Q. F
  \9 x' {2 z5 OModSecurityEnabled on;  . H" q' t! e, z5 ~
ModSecurityConfig modsecurity.conf;; H6 w, w/ M4 z  h4 p
下面是两个示例配置,php虚拟主机:* S, V8 j+ d7 z8 F: X

' I4 ~+ g) A( Gserver {
+ f# O9 z# M2 I* U7 x% n* Q7 E      listen      80;. A# j" s3 }" }2 i/ F% a9 z; a
      server_name 52os.net www.52os.net;
7 @7 L. _9 V: l5 e1 @, ]     
/ e1 z3 J% E4 }2 P1 j- p      location ~ \.php$ {- \9 u6 u: M3 ^6 N5 L% O* ~
      ModSecurityEnabled on;  
5 H8 `% m0 J' F3 p1 D      ModSecurityConfig modsecurity.conf;3 s+ ?2 S* N/ f& u, m
  B# r' n6 \5 l$ P
      root /web/wordpress;
* ~+ y/ Q' M. h: @      index index.php index.html index.htm;; \7 U% N. B" P/ l7 n2 ?
  
; o' G( a; k- B; G; Y! o      fastcgi_pass   127.0.0.1:9000;& D' t8 C5 t6 {2 c! W% Z
      fastcgi_index  index.php;+ X/ I7 a' Q! k( h" b
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;* [- Q* ]9 @; V" M5 G8 n) S$ k
      include        fastcgi_params;
/ V1 Y0 s% i, \4 f: i7 F9 ~% }8 \7 q      }" ^7 X& X6 c; d: ~
  }5 m( M' g( X7 k
upstream负载均衡:5 B  z% d$ V; m+ Z* u5 O8 ~

6 N! `/ i* T- P) Q8 B+ nupstream 52os.net {, C/ \) G8 D+ D' y
    server 192.168.1.100:8080;# K5 Q" U% x$ J* ?+ l
    server 192.168.1.101:8080 backup;+ ]$ H8 p" A( L$ P
}  d. v% O7 S4 N
1 }1 j5 U7 L+ v1 r: c4 o+ ~
server {
" m5 o( e9 O: \; t. ~, jlisten 80;* C0 t0 k& {3 s3 C' l6 P8 t
server_name 52os.net www.52os.net;4 J6 |  Y- j% u+ ^

0 G! @7 N0 @+ u1 ?% L3 xlocation / {( S3 t0 ~8 L/ ?7 s! @! Z
    ModSecurityEnabled on;  1 [% V9 \2 ^( o/ {
    ModSecurityConfig modsecurity.conf;  
! p, T! F- {: E% q& F+ P" B% ]* W9 Y' q, M  N) j+ k7 \, n
        proxy_pass http://online;
2 y2 A" h" r8 d8 s) \        proxy_redirect         off;" R) G$ I. F/ z7 }% G
        proxy_set_header Host $host;% u+ y5 s7 _" N3 J- {: g% [* z
        proxy_set_header X-Real-IP $remote_addr;
4 C- _# B2 Q/ f$ t0 D& U. `/ _        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
+ |+ V; v, {5 ]3 F    }
! Z) i; X& d( I$ k% ~+ W}9 C: ^3 U- ]6 \, c$ d
六.测试
7 Y: q4 F( `' L. Q
+ }7 V7 v3 n* e; r( b/ a: A% B我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:  _  |! H! |. _- J' u" U
; z( E/ m$ s+ w
<?php
8 F# v% V1 e5 X. p# J+ ^) L3 H3 W3 o    phpinfo();   
9 T, e$ T4 R+ x- k?>- ?7 V, b8 S) U- }
在浏览器中访问:3 U% z6 L0 ]7 X4 x( M

' J  R/ m& I7 {0 |http://www.52os.net/phpinfo.php?id=1 正常显示。
7 F( o  v6 {+ f9 ~( l% k# Ehttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
4 j% s4 v% T7 q5 `http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。8 B/ a! V; y; ~' Y# l' N
说明sql注入和xss已经被过滤了
# R# a; w5 w7 M9 d
4 u' x8 x- u/ I! {, `( A+ t  s, s' w七、安装过程中排错
6 q' e, w) @  C) J1 o! p/ d9 m' b! K8 d3 g' Z: J2 p5 Z* [* }
1.缺少APXS会报错# U9 g9 _! h' K; w6 h0 f: ]) w
. ?4 `! |" H2 @, T( ?: I
configure: looking for Apache module support via DSO through APXS6 `% ~, X: N, [  a- }
configure: error: couldn't find APXS
& f! N( \& \5 {+ o. j5 vapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
  b, m6 \8 r& w: {" G解决方法:! P- l& B! C- D) ^: e

% q! w' H6 v5 D8 p6 E4 O) U2 Kyum install httpd-devel$ k( `" i# ]0 ?# d, L1 I/ z
2.没有pcre
; I7 ^4 s& L7 K# @* ?; X+ C, {  }( ~7 i3 }
configure: *** pcre library not found.
* g. m  l9 D$ e2 _configure: error: pcre library is required! V% B+ O0 X1 _7 K& B( F
解决方法:
$ X- ~' h/ @8 b2 Y: y$ \; j- p- Q  O
yum install pcre pcre-devel4 Z  J& f7 Y! g+ ]
3.没有libxml2
0 d2 s9 N4 F3 x& n/ U- c
" q3 [( `# G0 E5 o  X1 z  m# I: k4 a/ U& Q8 v
configure: *** xml library not found.+ a$ o+ j8 O. ^/ H4 \6 b
configure: error: libxml2 is required6 T( L7 R$ I9 Z- @/ x* W6 V( w
解决方法:# F8 F* f6 V6 x3 y" V) l

& u$ q. r+ y  T7 n; eyum install  libxml2 libxml2-devel
8 ], D2 h5 p2 c& b# E4.执行 /opt/tengine/sbin/nginx -m 时有警告' m4 o7 ~4 z' r" ?2 f! e- Y

, `9 g7 z8 i$ R/ D! CTengine version: Tengine/2.1.0 (nginx/1.6.2)
+ m) i3 i; v+ y  b' T: Hnginx: [warn] ModSecurity: Loaded APR do not match with compiled!( a6 o- x1 O' p
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log8 V4 M5 ]2 \5 a6 e
9 N+ Z% Y! o9 }  r' o1 c1 t0 |  e$ M% V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.8 y, u( r$ P0 {$ W7 G! q$ Q- N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"; ?6 [) b% O( M% R/ b5 \, m/ n
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!8 o& \! D* K) {+ Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
) y; u  y: |- q6 W* u% ^5 I6 e4 q" I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"2 i& s# B) g3 C- t
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.  A# P- D0 R6 Z# z$ g  J1 z4 _+ r
解决方法,移除低版本的APR (1.3.9)
7 n; b$ ]( z, ?. ~; j9 ^, d. e' w6 \, g4 |! m9 O
yum remove apr
: R& ?+ [: \+ `5.Error.log中有: Audit log: Failed to lock global mutex, _' p6 b1 D# f* q$ C
% D8 x5 ~! l9 a# d: f% q% o
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
4 C- k. L9 k: w6 c1 E" J# Eglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]% n4 v1 l3 p% ?% A6 X
解决方法:
' D6 S6 p' A& U: [1 b6 x编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:8 N- S& Q8 ~7 v$ j
9 r% p8 u) N8 H: o4 k1 a5 z
SecAuditLogDirMode 0777
  l! s0 A" E& x0 W/ z9 @5 B' r- C2 mSecAuditLogFileMode 0550
% G. E. s! y: j0 q; Y/ [& x5 l( ?  y- |SecAuditLogStorageDir /var/log/modsecurity, @! u9 A1 i" w! w- F
SecAuditLogType Concurrent
" l9 f# t+ J& w+ o# N3 C# W参考文章:
9 A0 I1 g( w/ u' Ihttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
; B; ?2 f9 n: m2 vhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-25 03:52 , Processed in 0.088521 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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