找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10874|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
! E# t; M" A# v  u2 s
! v. Z; a6 h# u4 v( Q一.准备工作; s. g1 `+ W- t4 {7 o
3 @) a' y  |- |: H
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0& e# _/ r! j/ e2 G0 ^: z
3 X& b- h$ f- l- r5 r2 Y
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz" i! r* G  \3 Q3 E% z1 o# M

0 t) N" F& {1 J% q* ymodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz* e# M  F  ~( d4 d+ z

$ M  G; s% s6 BOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
8 M. S! w- E" x- r. `5 r) S/ c( C/ R! e8 q
依赖关系:# w$ x' T7 c: @( l1 ?) C" X
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:+ l8 `8 t3 ~1 t) q, l$ U
4 i! }! ?  b4 I1 V7 U% d2 {
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
6 F9 n! i0 g' H; Q$ Tmodsecurty依赖的包:pcre httpd-devel libxml2 apr  [! P" W& s* s$ z
+ p/ ~1 s) @7 f0 Y3 ]( M
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
2 ~6 ~/ h. H1 t* U* ?4 T  j二.启用standalone模块并编译8 Z1 R4 `$ U6 z) b8 Y
7 g! j( ]& Z9 j2 e) K3 y4 z
下载modsecurity for nginx 解压,进入解压后目录执行:
% I4 m4 X+ h+ u" s- d0 u
* ^5 \1 l; L4 e( K' L4 d$ u./autogen.sh
; o/ h, m' r: e3 V, Y./configure --enable-standalone-module --disable-mlogc) C7 T3 N8 r! z  d- E
make ' e6 l$ }5 a9 q2 |
三.nginx添加modsecurity模块' x5 t. r, ~% Q0 H

; i! \' q! U  i) T1 L! M在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:0 d2 G( N; H: M8 B
& n/ O6 k: u, R- i* X" H
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
  C% V: H/ m) P* g8 n3 a# w7 dmake && make install
  X% q3 D* h$ o  L四.添加规则2 _3 D/ x8 S! r5 d. z8 n

& {. Z0 M3 P3 }& |modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
" P& _; [' A& K6 l2 N" u, d" w, q: O1 s4 x% u. j1 m
1.下载OWASP规则:0 z7 Q1 V8 ]' [9 c5 F

+ V# ?$ |- |& P* Ggit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
9 P7 b  ~9 g2 W' f* w  n, r
7 H4 f* J+ P+ J3 w* X, c5 xmv owasp-modsecurity-crs /opt/tengine/conf/
9 L1 C, k' j( ?& o
0 W1 |: R& @& ~' icd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
1 `( \# Q# v" b$ W: C( D- @. v$ H+ x2.启用OWASP规则:7 ^6 ~; P3 X+ \; S

7 {8 f8 }. U2 J3 _复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' B+ g0 X' W8 w: {6 ]

2 D& X0 X  @3 k" F( b编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
0 b: k9 J- ?6 j; y1 o' J/ r: Y
7 ]* w' W/ m$ qowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
; }8 T7 d3 S( r, I, R* r- @9 h/ w4 l2 r9 s& W  Z1 _; S( U
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
- h$ B! N$ m- X7 P# N0 I6 p  X4 lInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
% l; ?0 e3 t) j) z- D1 |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf3 Q% s; J7 Y/ u* [% v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf! m% V) o8 y8 G3 d5 n- o
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
8 V5 V% N) W5 B6 GInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf5 d# W3 J$ A+ W- R6 b
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
/ k' p  L! \; _/ O6 `5 F$ {7 g& X五.配置nginx
  I4 r# }- [& K& T, I" u' Z3 s! ~& c2 x- J
在需要启用modsecurity的主机的location下面加入下面两行即可:7 |8 ~$ ^& V: i" k2 L& j1 d1 I' x
$ `$ V) I3 `: x# f: `
ModSecurityEnabled on;  
/ C4 T% P& g% UModSecurityConfig modsecurity.conf;
  r5 X  _5 V2 l" ]5 n1 J下面是两个示例配置,php虚拟主机:# _$ W5 v/ ]+ f9 u/ E

0 M0 b9 m- n5 C# S7 f- kserver {7 [& }/ \( m- o7 w
      listen      80;& E% Y: B6 [! Z- _* D
      server_name 52os.net www.52os.net;* x8 V% E! ~1 z9 N
     ( z6 w* f5 N' _# u3 j1 H+ U
      location ~ \.php$ {1 z+ m! m( o- B
      ModSecurityEnabled on;  
+ {7 w& M! Z+ Y      ModSecurityConfig modsecurity.conf;( S! `8 ?) d' m+ j9 f

. z! e0 x/ _1 F      root /web/wordpress;5 X8 B' W  m& D
      index index.php index.html index.htm;
3 y5 ?' _0 a$ y+ I  m! g% h' B4 S* ]3 a  + B; ~: X7 b- \% v; ]4 Q
      fastcgi_pass   127.0.0.1:9000;
. f. L' {7 p- f6 ^  H      fastcgi_index  index.php;
9 x+ e) E5 `. Q' z+ F  j# @% F6 Y      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
; j( |7 W6 q# D6 U      include        fastcgi_params;+ Q* d$ B" V: i" a2 Y8 B4 f& q
      }+ g- R! T! h$ I" ]+ [6 f! n" H
  }3 R# P  k; B, [/ j' m( U
upstream负载均衡:
- T; h/ N2 n4 b( Q2 y( s& w/ u9 x5 y
upstream 52os.net {
- C5 f  o' t! V    server 192.168.1.100:8080;+ u" x# B) m$ p3 E8 Q+ Y# l
    server 192.168.1.101:8080 backup;7 e& ~1 D$ [2 z( o7 F( ?0 U
}
2 h8 M+ M4 b! d+ O" _8 H$ @7 V. M; X
server {+ U7 B1 e2 u% T+ ~& F: y: G* {# `
listen 80;
5 ]. L- n2 E% Q" I% P: z7 \server_name 52os.net www.52os.net;! ~* |/ L& Y  I: C/ z' B

' I; \- g6 ?& d4 slocation / {$ L# [- h. x' t6 p4 \% E
    ModSecurityEnabled on;  1 y1 M) L1 B$ a( I* o% U
    ModSecurityConfig modsecurity.conf;    P/ X# u; y  X

/ B! l! Z6 ~# s6 o5 S        proxy_pass http://online;
/ ~8 e' @" ^; |) n        proxy_redirect         off;
8 Y& V3 I5 v' q; O& R' j+ I6 {        proxy_set_header Host $host;
% `2 Y* g. J$ K- {, I        proxy_set_header X-Real-IP $remote_addr;- B) o2 Y+ d( @! J  d, Z4 c
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
7 R0 U6 B$ m7 ~    }% b# G0 _6 q% p
}
  m; N  O7 q# e  H0 o六.测试! g4 N* a: L) D- {: g( u# L. F
3 [/ y: b# I/ `* h& n/ l8 y
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:0 D( D4 d. Q" g# l

  |& p* E! U. r) S; G1 B- ?<?php) Y, u3 |' s- W! t5 q
    phpinfo();   
1 x% ~! r: u9 n4 ~?>
& ?1 V/ _! q8 F, n8 x; r3 ^# h# S在浏览器中访问:
+ \- |! |& ]  ^3 X9 ^
/ K. T8 H2 O( [; Thttp://www.52os.net/phpinfo.php?id=1 正常显示。
! t- l( _+ Z0 `/ v! chttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
, v5 t( D8 d# b# K. P" thttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。0 I6 {( m3 p& l( @
说明sql注入和xss已经被过滤了
  |" w' B$ u% u3 i1 `' t, ]  P% i7 S$ U/ O! f5 T% g& b# ~
七、安装过程中排错
; x. E# o8 x5 E9 o6 m' O/ Z7 d
* K! a% E; V" O3 l1.缺少APXS会报错
. `; H1 a& ?( J: ^* k: ]( S, i. @# i" m
configure: looking for Apache module support via DSO through APXS: P* R7 S' d! N
configure: error: couldn't find APXS9 ]8 w3 s  O% }8 j8 n& a  f6 @+ c/ Z
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。- ?# {0 w* E6 E3 V
解决方法:7 \$ e; z  G  F2 f9 z/ f: g" K/ h
1 T8 v( V; H- ]" ^2 ?9 x8 x
yum install httpd-devel7 G- c  {) \  `5 w8 t
2.没有pcre2 X6 o" V. f$ b0 X5 A. |0 i
! q  {# ?& F: Z  {/ S* I
configure: *** pcre library not found.+ d: {& B! o! s# H! m
configure: error: pcre library is required
+ F1 ~; k5 c1 }9 l- `0 Q. \8 o. ]解决方法:
, ?& V+ O) O7 U1 h
' ?3 w5 a7 |8 \yum install pcre pcre-devel! b, G  o+ r7 x: x
3.没有libxml2
) i) h9 [' E) c2 n* N( Z0 O% l% A7 q

! ]) F, q0 _. a7 z: o3 @9 lconfigure: *** xml library not found.7 \( U/ ?; s% f
configure: error: libxml2 is required
3 y4 q4 s6 U9 r& O解决方法:$ n) J  B  q' C
1 l6 E7 E" w5 |7 f; m  D: M
yum install  libxml2 libxml2-devel
% U4 p" ^  Y. q4.执行 /opt/tengine/sbin/nginx -m 时有警告# a% ~% i8 W8 g* m/ K8 \' J' G

- l8 \, D: ], P; yTengine version: Tengine/2.1.0 (nginx/1.6.2)
! X. p, ]8 ^" A& q6 l' R# ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!
( p% J+ j& d. Z& Z: _) m原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
5 K& N# h7 X3 G& q# {7 h/ I8 D5 c+ l7 d6 {; d3 t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
2 q1 b9 l7 ~. G& p2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
, N0 I4 G+ v9 d! _6 `2 J' q1 n2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
: N  u* J& P* C" ]% K: D6 I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"  v5 w) R# s4 A1 n. C: k" ~0 R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
, q4 b  c. A) L0 F5 ]2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 a# R1 E0 Z& E
解决方法,移除低版本的APR (1.3.9)
( ?! y( H0 x; S7 O, d7 s7 F# U; @+ z) j
yum remove apr; g6 S: O- z  V  ], s  W
5.Error.log中有: Audit log: Failed to lock global mutex
; b: L7 l2 U* J2 S3 o. A  r; p6 z1 [# X$ S# ?2 D" e5 O2 K7 k. G) ^, A: ?- P
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     7 t5 T5 k/ O4 s9 _! i( b( k
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
  w, n$ D( j* J. U4 g2 q+ }解决方法:
! ^( K1 g* B# h; h编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
+ M; H9 j0 t& z+ a0 J+ H! z3 I7 l  w4 W1 u: _. k% L, A2 `
SecAuditLogDirMode 0777
" p) ?# Z3 \& W  D; Z) gSecAuditLogFileMode 0550* R1 u( |2 `! {
SecAuditLogStorageDir /var/log/modsecurity
- u5 M1 x0 K; K) X4 _8 V# tSecAuditLogType Concurrent
% m1 l* g5 t# ?7 x参考文章:
) L* c' N1 v- W% ghttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX* e% A) S1 _, Z' S: {1 z/ U" v
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-5-4 07:54 , Processed in 0.064919 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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