找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12123|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。4 t% I7 }7 [# r- G8 c5 A# M2 h, y

/ Y2 `3 b( V5 T3 p' A一.准备工作
  B, _9 d3 \  l' Q3 ?6 j) I) X
, r2 v6 @9 _9 V+ d- Y: F系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
/ l/ a! x  U1 z/ V, m( k3 G) Y3 X" {: A2 @: P0 [
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz; L- F2 e% y5 p

6 Z) G9 @4 ?) I/ F" |5 f+ f: t; m" `modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
# \  P7 g# u2 s7 r, T5 X
4 g# S* r& M8 K7 ^OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs: i; W6 S- o/ u! L

4 D. N# X. `$ I+ W5 V5 Y依赖关系:
; v% C/ Z) q; q6 }tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
, ?1 _, ?, O' |- e  {
! {8 a/ E7 x- |% e3 @6 H; @6 \yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel$ F8 k8 H2 C$ ^* u6 I
modsecurty依赖的包:pcre httpd-devel libxml2 apr; D8 \& |- A3 Q9 L" N; R' A
& e- }+ b+ i( }7 ?/ P% ^
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel7 U  v7 R1 H3 ~5 L1 ?
二.启用standalone模块并编译
# }+ l3 X. O- H) G: V/ x3 N" S
3 S3 u& ?( j" u! J( R9 B下载modsecurity for nginx 解压,进入解压后目录执行:# a+ \* F. }. ^1 v7 B
5 z2 E0 B! T2 ^7 v, q1 w" m
./autogen.sh
: j5 g' _2 E$ ]5 ^4 F: Z. @./configure --enable-standalone-module --disable-mlogc
! r& y% w. }4 [% x2 L+ _/ Q9 Smake
8 Y: t7 }" @$ F' i$ W) H$ Y三.nginx添加modsecurity模块
. W9 v* x" W& `3 g
0 |/ G4 a! M5 i; P' S在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:: ^; ]& R3 |+ ~1 ~- s5 j1 u; O7 U3 ~9 |

# @5 \3 ^/ ^+ J/ x./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine/ Z9 O5 K# Y- d) Z+ X3 h2 p- w0 K/ d
make && make install* ^; {; ~, P: K& x" ?4 ^7 ]
四.添加规则0 r/ U  z* m2 v8 A

  z2 A* n2 Z6 v! d' k$ _! jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
9 n2 U# Y9 V" s# Z9 u, t& d+ ~+ b  v3 }
1.下载OWASP规则:
; }+ i6 x: n" _+ O3 M- r5 V1 j( l0 V9 M, S. Z$ {8 ]+ B2 L
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs; w7 D& a# C# F4 c! Z* c
) [  x/ v/ d* b8 t
mv owasp-modsecurity-crs /opt/tengine/conf/, h8 R- Q4 |, [* ~# R

; n7 W$ G+ O2 M$ I- U& T6 rcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
/ |9 _. m, ]) B4 O) k  m) K- R2.启用OWASP规则:
9 C8 N) q; e/ z% S, {2 ]8 o3 b2 B
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ Q$ Z1 R* r/ I* w
- f% n9 o- C) }5 `  |
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on" W5 Q7 `% G" {# E! Y4 \$ B
$ l' `) y: m) q. y! x
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。: y# U( o  {' S: ^% b+ E

+ G7 l& H/ Y& Y; L  V# cInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
2 ?% a4 F: m5 WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf7 x% [6 e5 a" G' w9 q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf- a4 [; U% _/ t  V! z9 j/ o( g
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf  f/ @# v. ^6 h" v9 [' w& C
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf0 d, A3 C8 B/ r* [0 u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf- b( s" z* u0 E) {- j  L
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
2 |4 T5 x) [( N8 ]! K9 W五.配置nginx3 M+ x1 m! {% P; G
- p! J) c5 R& D
在需要启用modsecurity的主机的location下面加入下面两行即可:
" r/ w5 o- R, s
, m) r+ ~: Y4 H8 a! `7 N2 jModSecurityEnabled on;  
6 \. k: |" T* W, P' vModSecurityConfig modsecurity.conf;
+ f# n. l/ S: u( {$ }5 H9 p下面是两个示例配置,php虚拟主机:4 q6 b" @9 Z) O
: P! X; ?: u4 ~1 m
server {
3 [" U; d- C; D      listen      80;4 E3 d' {7 G& `( }; n
      server_name 52os.net www.52os.net;
* m  C1 g7 P, D$ U8 k/ z8 w8 Q) v     
4 j7 D0 k9 z8 Q      location ~ \.php$ {
+ N( A6 i* Z5 j! p3 o      ModSecurityEnabled on;  
! r  C% a/ {1 m# ^' h. u      ModSecurityConfig modsecurity.conf;
: ^* m2 @+ F3 U; i3 x$ P5 g1 E0 j2 G# R
      root /web/wordpress;- u/ ]) p  V; u% I5 b; q* d
      index index.php index.html index.htm;8 b( {6 \, V! }3 o" V
  5 o2 l9 i; I/ ?4 j$ O3 a
      fastcgi_pass   127.0.0.1:9000;
5 d& T/ b3 f1 ^9 T  H' C      fastcgi_index  index.php;
' [! @) S/ a& s, Q      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;5 U7 o0 ~/ O! j/ S# Z8 G
      include        fastcgi_params;
4 f# b5 |: N$ }# Q, X      }
0 d& X/ a  ~2 W) v$ b  }
4 o+ ?1 O; Y: Lupstream负载均衡:
$ O; q- L- t6 m
8 A/ K) i2 R9 v+ Q; J9 Z0 cupstream 52os.net {
3 h* N& l6 O+ L) x9 `# @& Z    server 192.168.1.100:8080;( \' B& [; H; a; J, l
    server 192.168.1.101:8080 backup;
3 W; r: ]# L2 W" [# v- f}
  L/ R( k# z% z' m3 T
& K7 {: S3 h$ Q7 Pserver {# f  ]+ m0 f9 H1 H5 I6 N! D
listen 80;
- y& V5 M( Q( P% x" A+ \& {server_name 52os.net www.52os.net;8 q: Q9 J. h) [! O8 b5 Q0 d
. d4 |( S; K) b# g6 i
location / {
) @! f( l4 Z. R, ~* L    ModSecurityEnabled on;  
2 I2 F. b. Q( t2 Z    ModSecurityConfig modsecurity.conf;  
# ?; Z1 p# P, Q/ h5 M0 X/ y/ O. e0 N: n
        proxy_pass http://online;
9 B1 I3 D' b; K. G0 L4 M        proxy_redirect         off;' w8 ~) L4 U0 f
        proxy_set_header Host $host;  `3 e, b; W( i9 Y1 {; r
        proxy_set_header X-Real-IP $remote_addr;, K  @1 L$ E, x
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;: m5 l- P- L* e. r! |, l4 X. Q
    }* G$ L$ b2 \. Y6 |& h
}
* p; R! W( r! a8 h六.测试4 l+ y: L2 p4 ?' p8 |

, o( S/ c, l; O* X我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:5 ^0 E4 R1 [* F  p
5 e5 o) g) X! c7 N$ T% F
<?php
% i& S; x* A" W  S0 s    phpinfo();    - r  z- i+ ^. C* `+ J' n- a
?>! F1 b* H3 T) Z
在浏览器中访问:
( Q' W5 @0 @5 R  _4 t
# Z' H4 o' y. O) whttp://www.52os.net/phpinfo.php?id=1 正常显示。8 W( |$ m& l5 L- i. G
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。2 w: q, B  L+ _. n$ J
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。4 G& a' x- I2 @/ L* L8 X
说明sql注入和xss已经被过滤了
$ f# \8 F4 q7 Z$ N
$ K0 g8 H2 R, ]" N- M6 t七、安装过程中排错
  p, ]9 x1 `" r6 N
; y( t. ^6 s# D3 J1.缺少APXS会报错1 [4 m0 C. j% s. ?2 a- x
( O0 l4 s0 V1 a6 X; a" n
configure: looking for Apache module support via DSO through APXS. _0 g8 R4 K  V9 @% W. {
configure: error: couldn't find APXS
  C4 P3 @' V0 yapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。1 `( |4 \+ V- N: E# s
解决方法:! _: p5 J! u6 Q/ l. M% k6 m4 j

$ E7 l- k. ~! j! [" u: pyum install httpd-devel
9 V5 J. r! `& M$ U* {2.没有pcre
! h1 I* u( q: ]/ A) V
) S3 V/ O5 S, j+ bconfigure: *** pcre library not found.
2 @) @3 m/ d# O  {* l7 Cconfigure: error: pcre library is required
! P9 I$ F0 O2 i* ]9 e) M, K/ a解决方法:
/ z  E! `4 G3 @" m5 F" G# `0 V& ^/ u
yum install pcre pcre-devel
+ H9 V3 i2 S+ g: z7 ~1 I3.没有libxml2& g, F( g+ {% o" l: S
9 {0 U6 u7 W% C: x5 S, Y

: B) O2 ~9 X8 x4 S* `2 xconfigure: *** xml library not found.
4 d/ [3 u: h/ Q4 l: Econfigure: error: libxml2 is required
! {# x" |5 `$ p5 S: d6 G  o解决方法:* U5 R3 p6 m3 D' J. g1 I5 ]

7 z; R& c( ~7 Z4 J% Eyum install  libxml2 libxml2-devel3 [0 k6 p: r; T# N& C
4.执行 /opt/tengine/sbin/nginx -m 时有警告" e% ^/ k7 Z" S5 R/ N! @# h

4 q' Z  y7 S. ~& M) F1 |, BTengine version: Tengine/2.1.0 (nginx/1.6.2)
( o2 l9 u7 x3 F* u. _5 G" N0 w  Lnginx: [warn] ModSecurity: Loaded APR do not match with compiled!% g& e5 O( D: H  a) L) c" U
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log/ ]) q+ \+ v; J/ c/ N

# \, [7 K& d6 L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
/ i2 A8 Q  P9 x; j  W8 _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
" g* @3 _" v5 i2 X5 S2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
: r9 m4 f& @3 g: k: D0 Y0 n' `2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"$ l1 Y/ f( Q6 K8 ?7 [1 \+ K
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"; a  d3 Z, j! L9 w! g
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.( z, J; C& h. Z$ j: C. c6 v
解决方法,移除低版本的APR (1.3.9)
. l, ^0 z& X" c  U! T3 T0 [) h  q6 @( o# y# K
yum remove apr9 D: E! Z! a+ |
5.Error.log中有: Audit log: Failed to lock global mutex5 ], U9 ?& O8 i9 P5 P) Y

' p6 a1 v# C5 G, ]: a; k- c. u2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     2 Q# q8 b5 \$ N0 x! t
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ F" J+ g  B/ ?# ~" c6 g  _' E解决方法:0 Q6 v) \4 X6 v1 J% T" F4 u3 |
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:! q: z! p' n4 c6 O: g- |- p/ g% h

8 o" G# Q) e! J' S7 A5 b& pSecAuditLogDirMode 0777
) ~+ V3 O" [# b( Y6 k, NSecAuditLogFileMode 0550* h4 K1 ~( `0 q  `" ^' W
SecAuditLogStorageDir /var/log/modsecurity6 b! [5 h+ j  h# G5 S8 b8 b4 }
SecAuditLogType Concurrent. n6 ^1 x) {, u  I/ k
参考文章:
: O( U1 N( L3 @5 C- E$ ^; ]0 Vhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
# {% ]5 j" r  ^) {7 w1 w* Uhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-11 16:43 , Processed in 0.086857 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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