找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12276|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。* o# N% j' {" C  K9 ^6 K8 G
. _3 H% C$ V+ q; P" X4 Z
一.准备工作) p; v) a: t3 w4 }, x+ J5 w  N& E

  o  g/ {5 F. q: v( D* c6 G系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0; ?  d3 M! z# X. M0 m2 V: a

2 E, F: W5 i7 y6 ^% A* ]tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz7 v& P1 c+ }$ l- W9 W8 M3 `
/ `* i4 O2 ?7 K& _* K
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
$ h# Y; c0 G: G& ?& \+ B5 M2 U; T. r& ?$ F! c
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs+ }8 Z4 Z( e) M" u% y; _$ s7 U

8 _; d! b" Y" i: `" D依赖关系:
" C4 z  w+ y3 ?6 z" L% M$ S; ]tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:  D" p; y2 {  `* H: n5 t4 g. [- v

1 S6 ^6 K. [( S! O- Eyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
* f) c- A2 E0 s  V" ]/ `1 nmodsecurty依赖的包:pcre httpd-devel libxml2 apr
# u* ]6 H& U" q& z% m7 w  Z" A5 x7 a* Z' I) M
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel; s) n" C8 b, S6 Z+ S- f6 b
二.启用standalone模块并编译
; i3 g3 l- ?0 f# y5 i# P  M; @5 r  C. j6 p4 a
下载modsecurity for nginx 解压,进入解压后目录执行:, U1 ]2 |/ e+ n; v  f& {
: s- t9 H% o1 B/ q
./autogen.sh
" X& q! y% V$ q; D5 `1 g8 z9 ]./configure --enable-standalone-module --disable-mlogc
( P( ]) c, p9 Y  ^) T4 b& `make 5 s5 ^0 B9 A/ o" K
三.nginx添加modsecurity模块
8 X' w; F8 w1 H% s, `0 b# A  G! l' K# ?) I  d5 ?' A
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; r7 w4 c$ D. z& w. V
" q; |6 x. m2 `./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
  t' ~/ Q. q; \/ |make && make install7 T' q* y' L" Z8 a3 F  J
四.添加规则- g% ^2 h5 c" `* |4 U) w4 Y

7 J; ?0 L" ]9 }7 s& V4 C- E/ ymodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 W  L% K; I3 b9 I+ O
4 E( M" R6 k# ?& ?1.下载OWASP规则:
7 Z6 R) k# I0 o5 l# n" J/ d5 T( ^# N9 I& d: e
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs: I. p9 y6 R8 J8 |& i

4 \% L) }* d7 s- i3 t' q  ^mv owasp-modsecurity-crs /opt/tengine/conf/- Z& o. A% ?: w6 w/ J3 @- J

. x; c6 w7 |# `9 x3 j) Xcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf0 w  l0 Q& F! }) b& b; l  J' B" z
2.启用OWASP规则:7 k  _4 }; A3 H( C8 o" t
! @6 T( I2 B& M  T0 E4 p: u" n; U
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
; E1 F6 Q- N3 y6 [. ~
/ F" l* X7 P* R0 U0 q编辑modsecurity.conf 文件,将SecRuleEngine设置为 on- I- t& H2 W- i

. Q  D& P' l7 v- c% W+ Mowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。$ C! z! G" N6 D0 S: m
, |+ t  ^3 w  \* X; C
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
9 w* j# f: P. I# j6 X  cInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf2 `# |( C/ b8 O" ~- ~% O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf  S, s2 W$ z. m; T' }& ~  q8 H
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
$ F; l2 i; U% a  `9 F, RInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf7 W+ X: }  a" n' x' k
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
- D! V7 {6 v: MInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
2 |) ^$ c$ q9 ]) _) Z五.配置nginx; \, D" ^0 g- g1 F# n# z

& k! d8 [5 A" ~: n. }& j在需要启用modsecurity的主机的location下面加入下面两行即可:7 c/ X# J" E! S
4 {5 c: ]' v$ i3 _# T# {
ModSecurityEnabled on;  
# D% C, O* N" ]' j9 x0 sModSecurityConfig modsecurity.conf;
4 I8 F9 ]  I1 m# H! V% t下面是两个示例配置,php虚拟主机:3 T' ]* l- D$ |$ }
4 e+ \- c8 f- d/ ?
server {
- U3 l$ D& g4 s- d% f: L& _6 @      listen      80;
9 @  o( c9 q8 t3 I# W. I  @  N  @      server_name 52os.net www.52os.net;
+ @/ ?+ G4 H) w3 d0 N3 f     4 g! ]" u$ c4 s2 Y
      location ~ \.php$ {# P4 A4 c* v7 R  x9 b& h0 }
      ModSecurityEnabled on;  $ Z. C" ~' R+ |2 T, o
      ModSecurityConfig modsecurity.conf;6 [! d5 i3 H6 D7 U. Y

& T4 d* w, Y: `8 l      root /web/wordpress;
( x0 V3 a& N7 \" r' g, E      index index.php index.html index.htm;
( j8 K' @2 c2 x& u( d* A# X. ]  
4 }) x( m9 S# m4 i- m      fastcgi_pass   127.0.0.1:9000;
/ p- g; M. M$ r" R! ]8 g% A  ]% S      fastcgi_index  index.php;
" d2 N0 E) X6 U      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
: L' h' k4 ]8 _      include        fastcgi_params;* T! p' c+ x& U( O+ W4 v$ \/ R
      }6 U/ f; p( x9 n9 ]# o
  }# \' W- v/ U5 }  n( \( s$ [
upstream负载均衡:
7 H' p6 s7 g+ {+ W6 l, s! N6 u8 I4 }/ Q% B6 y0 U4 k9 r. E
upstream 52os.net {$ ]7 u9 L4 F- H9 y$ C. M
    server 192.168.1.100:8080;
- w1 f. K/ }7 Z- {0 w7 A. `    server 192.168.1.101:8080 backup;0 O2 ?# m. U% k* V8 f+ U- {0 d$ L
}
& L0 u' M1 p+ R& G) }, w1 W* i( a% I7 `* X3 [
server {2 j5 v3 i' K1 W  O/ j
listen 80;& l, E$ T* g, {1 {! F: l7 t. B
server_name 52os.net www.52os.net;* m# o& u( D" M5 @0 N
6 v6 F3 Y5 X. w. c5 U8 g1 v  `
location / {2 j9 \; \. O% {
    ModSecurityEnabled on;  9 H, d% s/ Z5 u; C
    ModSecurityConfig modsecurity.conf;  : ^) ~, u* e9 E: [! J
/ d3 i& a# T4 }' m2 O6 A9 Z* T: V4 C
        proxy_pass http://online;. |2 S  Y/ ~1 Q. `5 L" d9 ~+ E
        proxy_redirect         off;5 S" X6 W: H' d. g. C: m9 Z
        proxy_set_header Host $host;
6 _: h+ A4 T' [. u        proxy_set_header X-Real-IP $remote_addr;5 E! `, j1 ~! Z% H* G
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
. M# y1 s/ u, ~) u. z    }
/ X# Q  m0 a+ V6 o" g8 L% n}
6 [5 J; J( D2 d3 ~8 E& g( G) G六.测试
* _( V; |2 _" {; L7 o* a+ M) b
: S; J2 e# m6 D# \2 {) J8 ]5 `我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:" W7 g8 u2 B5 \) W

+ [  r# K" r- C% G" J3 o<?php1 l3 D0 O5 x- v* ?# z1 Q& M9 B6 a. r' I
    phpinfo();    1 k3 c3 q, I. f1 L1 \( I/ J+ o
?>
1 ^) X! `2 S, A+ q9 Z0 H/ B在浏览器中访问:. X0 c1 V. `' q$ V
9 P4 n' T: Y2 q9 q4 p6 ?2 O: O6 {
http://www.52os.net/phpinfo.php?id=1 正常显示。
5 C: e# @. O. U' Z# g' Lhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
. c" h6 S, d( V; o. l9 m  Ihttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
+ [+ ]* J) o# [3 ^- b( n说明sql注入和xss已经被过滤了
6 [) k  q; m% h5 c0 |* x4 n7 o. v
4 e) |. |- a9 W7 O8 k+ _七、安装过程中排错( S; M; N7 W- [
0 \; g; Y! _" n$ Q$ i* y; ?5 X9 s
1.缺少APXS会报错# @2 Z  r4 c3 f. j
  t3 `2 O. p6 X0 }& Q% M0 h4 Q
configure: looking for Apache module support via DSO through APXS
& S! K# y* n# Z1 Q: x! \* Uconfigure: error: couldn't find APXS; V% t) N; U2 Y6 @
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。/ M  K* `7 S8 z- s4 j1 t
解决方法:* P! ]3 A$ v0 r2 s
  l: V, Z! O) [' r8 U
yum install httpd-devel; I! }+ J* q: X: D; |6 S! j$ M
2.没有pcre; J% h2 e# C5 G
& U( n' b& L1 r1 P7 B5 E1 n& f8 E
configure: *** pcre library not found.- P8 u2 t* V3 D2 S6 O) r* w
configure: error: pcre library is required4 f. k9 j- {  y8 P1 N, e1 y
解决方法:
& \, s; e- q. Z+ T/ L: \1 m- b. @6 ]" U  K) y" e9 W: B
yum install pcre pcre-devel. w$ v- c) g- T7 B  {9 z
3.没有libxml2
3 v6 C) M9 |8 v; F0 |! a
6 l' `+ J- h' T# _0 \
+ h- o. @% f7 D- {configure: *** xml library not found.# T& u9 @2 P6 `8 p7 u7 q- i7 R. E
configure: error: libxml2 is required! k5 U1 c) B; d9 S/ z8 b8 y
解决方法:
% T: V( @8 Q. l) U9 O" S5 G- k& a/ Z- O: r# E$ F0 G7 `5 o& {
yum install  libxml2 libxml2-devel
1 o, M% l7 R5 r/ n, [1 r4.执行 /opt/tengine/sbin/nginx -m 时有警告/ |* Q6 l" @% c. I
' ]4 \8 p  A* h8 o
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
4 P7 e  S# z5 x3 C. s6 Unginx: [warn] ModSecurity: Loaded APR do not match with compiled!3 V1 K3 f8 r& l; J
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log/ ]8 k; [% H6 O1 K$ f% |
$ C" F, B& X- V/ n) ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
9 ~! {% }1 S! ~5 b5 }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
6 y6 N) v; h/ N! E8 j* q! T% r2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!6 T1 P. I* Q" q6 T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05": D& g0 L! T6 P- \; |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"9 m+ t% Q$ L5 }9 z' k: C% ~
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# v9 w: j/ [7 ~. m2 i- ]$ J
解决方法,移除低版本的APR (1.3.9)
- P2 U2 e9 x  X; D. U# d4 a4 v/ @, I
yum remove apr0 s( D5 e) R6 i4 N
5.Error.log中有: Audit log: Failed to lock global mutex
! W4 v( N$ o0 E3 M2 |) d, K% O
$ H9 V8 @3 G# f8 m& z; w0 r2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
7 X4 u; e5 ]. J1 t$ V% i9 r" H: x6 @global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]& A/ g$ a* I) D  @9 U* t. q7 j% R
解决方法:
0 J. o8 S) _8 P编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
% A7 Y# @6 I- n( q$ z5 V# v8 ^2 o) V1 `7 W! n+ ]" F+ u5 `% `5 B
SecAuditLogDirMode 0777/ {: O8 l2 z& z9 p/ K; ]6 B. N0 p
SecAuditLogFileMode 05502 i& H4 {) S4 X) t" S
SecAuditLogStorageDir /var/log/modsecurity& m; G; z/ n$ o: @5 d' W& j
SecAuditLogType Concurrent
) ]. ]8 b% v. P3 W7 w参考文章:
/ y- N  [/ ^' J' n; ?https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
2 F# Y4 N) o% T" w! lhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-21 20:16 , Processed in 0.068022 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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