找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13038|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
" d$ b2 n& i& B/ ]8 S2 g
( R6 I0 Q% t- Y7 U一.准备工作
# U" p# B. m) a* }5 s
) I9 `8 M4 B+ g; x% v# @系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0& D0 f* b) Y- ~" Q: Y8 H9 F

- @: r$ L5 d- {; v. `tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz& O: D' b0 s/ U1 G# A
! b) O8 t# [! J& P2 \6 Z
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
$ Z+ y) t# h9 y9 {- k
0 ^4 L8 x8 s9 LOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ N2 h5 E; T: k, C1 j- }+ p0 t6 w1 n+ x, j; x3 Z( |1 y
依赖关系:$ I" c; o, D2 `/ E+ N; w/ m% k
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
5 G( G+ h$ t; ~) ?$ G3 ?; k' y. v8 t+ Z) `  `3 K5 ]
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
0 z/ X, `' |$ x" T. Nmodsecurty依赖的包:pcre httpd-devel libxml2 apr% F0 B! ^' [/ H, O1 q9 `& w
+ I* U( o  d+ a7 z
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
  A2 d! {8 L' g/ C' p) f二.启用standalone模块并编译7 i( S+ Q1 B: W1 l$ k: e1 i! X; a7 m

" I. W2 {  K4 P, `下载modsecurity for nginx 解压,进入解压后目录执行:' o/ R9 b5 p0 e6 @9 ^
, Q: ?% M, |+ z# w3 ?
./autogen.sh5 ~& B  Q: Y/ Q' u) |2 O
./configure --enable-standalone-module --disable-mlogc
! v2 e% N0 d0 i0 |$ I7 y8 p9 C, Y2 smake % u% C/ ?2 F: ^! J. Q: x& w! F9 U
三.nginx添加modsecurity模块# S) d- N' O% x" H% f3 I* i
0 ~: G3 p/ D: U# z0 c
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:9 E% F$ E+ }/ V; O) u& B

6 l( M& W' T4 c  C3 x0 C./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine! E4 _  v4 t, V7 v3 y. W# k
make && make install+ c; D3 L, O& N( d
四.添加规则
- \9 ]3 {* j6 u& k7 ^0 h
$ S  S" ?5 B& `: I1 L. s: jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。! t8 f4 [! k+ L: w& S9 ]. K* {

. s+ Y' S) V; Z6 r+ c) y( |1.下载OWASP规则:
; d% Z4 [, F0 ~
5 o. m( U! n" cgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs0 t  O  z' I* \

- J8 P* E4 v/ P3 tmv owasp-modsecurity-crs /opt/tengine/conf/
( ?- F8 s! L, }( C( C! p, ^+ f/ S
& i% |" x; U, ?8 S9 E/ `" Xcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf% b, S* H( ?# i6 ~4 O/ V- {
2.启用OWASP规则:* i2 q% ~" G$ h
" P3 d1 O. u, |
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
8 u7 G: M3 A& s9 {, J0 x
1 p& y+ A# @, {, C编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
5 `, X, @% h* L8 s3 r0 I1 ~3 N, X% x, d+ P
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: U8 w* b: D! D1 C' j; @) W* B' _* R6 D+ C3 R
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf& x& u4 Q% x& s4 k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
: L7 y0 `" D1 t3 {9 a( YInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
& d! H* N/ g& c4 n' @4 KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf3 @9 m1 {2 p+ i( F7 a$ }2 u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
  T1 f) P$ ]: ~, }7 yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
* d. @. [) V6 W0 Y8 _! LInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf" k0 G6 J2 F% q4 W* q# C
五.配置nginx
- L  f; P& J' H+ p& i3 ?
, l  I. t4 C+ S2 j+ r) ]4 U在需要启用modsecurity的主机的location下面加入下面两行即可:
$ u! N  M. n' \; |4 v
' J3 `6 D# b9 b* pModSecurityEnabled on;  3 Y4 t; E) @/ U) a2 ]: |/ Z
ModSecurityConfig modsecurity.conf;
( y" S  |  U' H, V& i% R: d7 p下面是两个示例配置,php虚拟主机:/ f, z6 I3 ]: y/ K+ q2 @
9 L6 Q% T. j+ ~) Z$ ^
server {! {& b0 n# e- g/ Y! F
      listen      80;4 {7 x* I- C, f6 j1 p
      server_name 52os.net www.52os.net;
5 P; f" {- q9 Z# ~7 f% C7 F     ' e/ @, q9 d* \% H9 h( n( ^) m+ n7 W) j
      location ~ \.php$ {
% s  E# K0 p# o; S# I! s      ModSecurityEnabled on;  1 `) n' k. s0 z- I0 z
      ModSecurityConfig modsecurity.conf;
) L( T7 B$ r! B# |, v; x4 @% h
2 l5 q% d+ N1 y# J7 a      root /web/wordpress;
+ {* F+ k6 c5 K' }$ n. W      index index.php index.html index.htm;& ?1 \. j/ w$ @7 @% s2 X  t/ L) `
  $ q) I0 N( e9 T7 ?+ W, T
      fastcgi_pass   127.0.0.1:9000;
6 L3 I$ M4 ]/ D% A; V( d      fastcgi_index  index.php;
* [6 p) w* f$ A' N' a      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;: c3 l. x/ e9 A' c" I
      include        fastcgi_params;2 U: c. m: B) w/ L  m8 Y& T
      }; ]& X1 {% e7 {9 J6 C; h
  }
7 Y) c8 p3 \' D; W7 f2 Z  xupstream负载均衡:
; A- y4 B7 Q3 ?+ G1 m( a& J2 e5 u; v9 V' A
upstream 52os.net {) ~, `2 O; M, w' c4 Q- d
    server 192.168.1.100:8080;
( F, n7 o) ]  l$ |    server 192.168.1.101:8080 backup;
+ }1 \9 l4 J+ D& n4 W9 v}" y& U' y6 N+ A+ ~
+ B! G( ]" y& n4 N2 O; y0 j
server {
5 E$ K/ e: F. I& _listen 80;
4 ^7 L" T% d3 W! E4 f) aserver_name 52os.net www.52os.net;
1 I9 o' J5 N7 i% ^( s3 Q
$ A. m7 o2 ~' n/ glocation / {- R' [3 r2 ^' N, J/ A7 z
    ModSecurityEnabled on;  . d2 b/ B+ c& X# U
    ModSecurityConfig modsecurity.conf;  
( N4 V! b/ I; S! L+ U5 V9 ?9 S1 m+ f6 }- O* K
        proxy_pass http://online;; }' t+ d: b8 G; U4 |. i& S
        proxy_redirect         off;
  {* u3 Q/ d- R! J) Y        proxy_set_header Host $host;6 {5 N) N+ V2 A+ \) x& B: g& K
        proxy_set_header X-Real-IP $remote_addr;% |5 w( }" U+ U2 T
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
* G; I2 I& }9 v( L+ ~& {    }
. d4 z' \# \8 p1 g, o3 f% q}- M: {* \! J5 M: b. E+ w8 }! T
六.测试) b) Q8 @; ?3 U7 V$ L
  _5 v# |- a7 W- i
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
, q/ I& j  W& y& b/ z
( z3 H. \# x% R! D<?php7 j- y+ `& ]4 D3 B- Q4 J
    phpinfo();    : O' t4 N! {: D; a; i0 P! ]0 q
?>
' H; o5 ]3 B6 V' c2 T3 p在浏览器中访问:; D1 ^0 ?6 F# J
; k: n  X" i7 W% e8 [
http://www.52os.net/phpinfo.php?id=1 正常显示。
5 E8 t8 m* x/ A; q7 |http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
4 I9 g; t9 M4 _- s* \! bhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
5 g2 K" e% V, _0 q- F说明sql注入和xss已经被过滤了
( }- D8 l5 E( M! z2 S  p  b+ F% @) d' Y$ s
七、安装过程中排错1 K" X- c6 A, n4 M  E2 e6 S* c

$ G( x8 w4 P; F1.缺少APXS会报错$ P# a$ \4 E6 A8 ]4 _
) I$ n! j$ T* h& G- ?
configure: looking for Apache module support via DSO through APXS
+ r  G7 k# s& ]) P( C% _6 rconfigure: error: couldn't find APXS' r( f' B9 X0 D- W
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。7 }+ P9 a$ |) Y: p" W* M8 P1 T  s
解决方法:+ I+ q( G2 y7 i6 C

, J1 {. a9 Z" Dyum install httpd-devel
; W% `) q- C/ r2.没有pcre
2 ?  S1 q& ~* k( x
* \; G$ @5 n- K- u9 ]& kconfigure: *** pcre library not found.
: ^, i7 R) F7 H4 Yconfigure: error: pcre library is required
8 e0 o3 n+ o) `$ b* p5 L  ~/ X解决方法:5 V8 W* {& \6 s( x3 _; P. g' f. X

* U) S% ^7 l$ a+ j6 C6 S, K; ]' Wyum install pcre pcre-devel
4 G- O! |" R) y3.没有libxml2
- K& G, s. S6 W  n
7 m/ m, s# `) P, f
3 j: M4 Q1 c$ Qconfigure: *** xml library not found.
0 V& N; c1 L( h, d* xconfigure: error: libxml2 is required2 n0 {% {: o- u
解决方法:
" w) v6 m) N: A2 m0 k
) @5 j" N( t* t1 dyum install  libxml2 libxml2-devel
" U  _, K1 P& Q3 n0 q9 C% r4.执行 /opt/tengine/sbin/nginx -m 时有警告" {: k: V# `1 T# s& r

: J- q& S7 d1 K! X+ i5 yTengine version: Tengine/2.1.0 (nginx/1.6.2)
8 o/ ~% _& h1 gnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
5 x( A1 l/ }8 s) S2 S1 ]6 @2 g原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
( }' P8 B: j1 x( r5 y- M9 e! p& B+ Q$ O1 l/ {# f3 r8 ?9 I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
0 j- T5 K& u+ ~' ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
' Y2 O7 e3 @$ O! o2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; o5 f! c  m; D% v6 f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* j  \% ^1 X  S# Z3 `0 d+ A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
2 ~6 n8 D3 D) [4 _3 C5 A, R! y2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
  x) u# v9 v! [. C1 p/ @解决方法,移除低版本的APR (1.3.9)
  R! L- D5 n) E2 T5 W2 A
4 t% T6 P+ B7 {3 ?, fyum remove apr+ e% v. O  a. Q& J8 o5 {
5.Error.log中有: Audit log: Failed to lock global mutex
2 w" P# {$ w! L  \) a8 a1 g/ C( W
: @1 S" \/ m5 r  D6 o* l; k2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
! h- H: t9 Y: a' m4 gglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]& ?2 E/ _1 S" t7 X
解决方法:) ]  x7 [+ ]- N, S# Q9 q
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:8 v. c, I& ~. f& s2 A; u# T7 ^
  u8 t- C5 ^. R) a$ w
SecAuditLogDirMode 0777( [/ E" G% ^3 s" S- l
SecAuditLogFileMode 0550; d1 h! z. K7 d# V1 f) ?8 i
SecAuditLogStorageDir /var/log/modsecurity
& x  v! ^$ l' w- d4 HSecAuditLogType Concurrent6 B1 V7 o+ E4 g: b3 T9 z4 b
参考文章:
, ?. h( w' E" T& z, ]6 b6 q, N! Lhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
3 h* U/ v& F6 e6 ahttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-6 10:42 , Processed in 0.065885 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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