找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10135|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
; u1 q2 l7 c* p0 M& o4 r
- h6 p4 _3 j* u/ V$ ~8 g一.准备工作, T1 O  K0 I9 I5 W0 t/ B

: K/ V: }* u- D系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
* Q! t* m$ H0 i/ E3 W
4 m, O" J+ S) Z7 wtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
/ Q) E: k' x6 L5 [. O; q. w# w  U" ?  }9 P* \3 \$ j# {! e2 n
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz; G/ Q0 j3 w6 K* U0 [* y( l/ ?* D
6 y) H! d' }; j" B: s
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs8 R! n4 g* O9 R- N4 s/ U  {
  w, f. c: H! K" J- n) V$ U7 y+ a
依赖关系:/ p& C4 b( u6 r5 M4 _
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:7 t" f. W; P3 ^2 P0 f2 P8 ]

2 @5 r0 E$ v  X4 Q3 x& I* {" ]! N1 i( Ryum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
6 D+ Z3 \: T" K; kmodsecurty依赖的包:pcre httpd-devel libxml2 apr
3 [* Q' N: q' T1 Z8 t* o7 v+ |8 L( y; t5 \( a# i+ A/ M, H& n: z7 L( M
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
1 b9 k) P, W2 n( ?  V二.启用standalone模块并编译0 U  {, q5 T. t, ?# }/ X6 B

0 f2 d, R/ e. D3 b- M$ p下载modsecurity for nginx 解压,进入解压后目录执行:
- |2 q8 {3 y1 \9 p  K
, `2 v  ^; K; @./autogen.sh1 t' t6 m' R+ `1 |) W& a# y
./configure --enable-standalone-module --disable-mlogc- C4 A# w$ `9 M+ d; N7 Q
make / M  R6 C. u+ J/ Q6 ^; `
三.nginx添加modsecurity模块" g, L$ o1 e! T

+ Z. R7 H+ j# Z( w" x9 P9 ~$ B在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
9 r/ j' x  {. L0 F* h& w
. S& O& u# S9 _2 R8 ]* a./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine+ _6 l( ~9 x, |, P4 c+ L  s7 C# a1 w
make && make install
0 z- r& h1 {- F( s2 `$ j2 N四.添加规则6 B9 l9 ?3 J6 b( x& V
4 z+ K' X. N& E) r/ p
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
2 D/ X4 P1 f+ c2 v
& x1 I2 X3 H8 w( i2 |: G' l% m0 r1.下载OWASP规则:, a% C6 _% O9 V2 H5 w/ s
% t0 R6 Q  k1 G/ C! Q4 x+ k8 ]
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
; h; @  ^9 r9 I( X% C! J9 \9 _& t0 d8 V3 [5 S# c( w+ j5 w* [+ a
mv owasp-modsecurity-crs /opt/tengine/conf/
: N0 H- p7 w7 B( ^9 B% b- w! ?* O4 A' \8 c0 J' u" ]. x4 U
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf% O  p5 J8 N- G- z* N
2.启用OWASP规则:
" T4 v* f1 L% P6 A$ m  x" i% @8 Y5 c1 k, C/ J7 \+ G
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 `4 R6 S$ m2 n6 r& Z+ _* x
+ O4 ^" Z: A4 ~" {- `) x* ^
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
, Q0 v% z' V) r1 x3 M  W* S
% U0 G$ {8 j# z6 wowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
$ }9 m- g7 M6 Q+ m4 j/ u6 f' ]: B
. t+ y! k: ^( H5 c" W6 |% W* i2 @  YInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ y" l+ Q1 x/ g" _/ [; C; f) Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf5 }! Q; ~! e, ^& j: O( y+ n
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf. G7 Q% r  q+ I4 Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
% ^0 K0 X4 p8 A/ k) ^) R8 MInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf- _8 s9 d  O! ]7 L
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf* f. p  j# P& }( d$ q
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
/ S2 B. q8 u8 l% U7 |& b4 `* Y五.配置nginx
# t( s; k; z9 v/ [1 O  V: C. v, |3 ^( ?# _/ |( A4 J) e+ t
在需要启用modsecurity的主机的location下面加入下面两行即可:4 V. s6 Z; |! J% C+ i

% R0 X4 S! Z- A: }4 o" \4 z8 [ModSecurityEnabled on;  
7 Q2 u; a) t7 J/ c3 G! IModSecurityConfig modsecurity.conf;- Y- d# p, N# m" G& i5 e+ x! t
下面是两个示例配置,php虚拟主机:! b( {& v% B, t
0 ]: R2 c5 s1 m
server {
) {$ o5 _3 v  N& n1 y4 \" K      listen      80;& a. y2 _6 Y. f1 g, h9 q& O/ k
      server_name 52os.net www.52os.net;: v+ R: o$ J: D. m. i7 V
     3 t& q( I* Y# M1 E9 L5 |2 p
      location ~ \.php$ {
4 }9 l1 \$ z& O1 E$ u/ U      ModSecurityEnabled on;  
- [0 l/ Y# s0 s! S: ~      ModSecurityConfig modsecurity.conf;- w* P( N! D/ ]9 |: o; [( J

3 n! w/ @  I+ C6 N7 E9 M      root /web/wordpress;
& E# Y& |  s7 s* C* }0 T      index index.php index.html index.htm;
2 l7 x* F3 w. D6 J  
( Y" W* |0 F4 [4 P      fastcgi_pass   127.0.0.1:9000;
8 G  B7 A. D6 C) M' @      fastcgi_index  index.php;/ v8 A/ n$ g0 h  N4 z7 ~' R
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
  d: ~* F, g$ E- M5 i% X      include        fastcgi_params;7 F$ W$ z) o$ Y' w, F
      }
* z8 }, {; u5 z/ a+ S- g9 W! O6 q% g  }
8 J& C" P$ ]; m) U; m) l7 i' Fupstream负载均衡:
+ r' g4 e* c$ i7 ~  c4 m6 C8 G& N1 {0 B) G: r$ q
upstream 52os.net {0 D5 H+ n( S9 j4 J' f  x3 M
    server 192.168.1.100:8080;
. B) M8 T( w! |* u    server 192.168.1.101:8080 backup;
& C; L; k. O: X( L, }}
* _5 x4 G2 z* B+ B# g
, p. K# t, E2 e; U3 p# C( V8 sserver {$ h# z( y. b$ j- {$ {
listen 80;
- s/ W0 ~8 o: P8 Q( y( ?server_name 52os.net www.52os.net;
) Y# l/ p1 S& }. F
2 x$ `& X# U- R" |location / {# F2 C& S! ^5 ?8 _
    ModSecurityEnabled on;  
2 F& y# G! I. l( ]' S, v    ModSecurityConfig modsecurity.conf;  5 w1 v6 n5 o" G! O7 j, n$ x

) ]* u4 L$ e8 |! \6 f. z+ Z        proxy_pass http://online;( K2 C! d% P( ]0 x6 S2 {
        proxy_redirect         off;
- y2 S# G9 m6 X. S8 {" \        proxy_set_header Host $host;& X8 j& U  m  O
        proxy_set_header X-Real-IP $remote_addr;+ b* F+ V; ^. J
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;4 ^1 G% s  P6 Y; H
    }1 Q. P( E' U+ F7 N4 e% O7 z
}
, n& N- A: m% G( a, J六.测试
. t8 W  g& U0 H/ I" N6 g5 j- q* V3 o0 _9 b; u
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:& a  ~6 N( K$ E

/ ?4 q3 [4 y& H/ A6 v: k8 e! ^<?php
7 F! d) @1 i' m. O# _- ?. C) x    phpinfo();   
  f7 r. e& B' z! O3 Y. Q: n/ h?>
& d; @5 ^% i: G, |在浏览器中访问:) n& \! M1 t: j* e" y

0 h4 A# e* @- n- g$ I  P; qhttp://www.52os.net/phpinfo.php?id=1 正常显示。
* c- ]# W& N3 {1 [. Q/ |http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。. C/ r) m( J# {
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
  j# |8 v$ i$ Q7 Q1 r说明sql注入和xss已经被过滤了8 O" L( ?, ]& J, L2 \) m% ?' A; P
# I; [: |* L9 G6 u
七、安装过程中排错! l9 e2 c! x' F5 s
4 w9 j7 u9 _& B0 ~
1.缺少APXS会报错! D( I8 u8 `: U1 }( A# O
# t  i+ L' }3 T5 F% T
configure: looking for Apache module support via DSO through APXS: \, k+ @! Z9 N  z
configure: error: couldn't find APXS
) ?& B. V8 c9 Y( j! fapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。% ^: \8 J% C0 x! }% s3 [
解决方法:
. p( _* C+ q7 L, l$ U0 B9 d( y( g) d7 n- m5 q
yum install httpd-devel2 }2 l1 @6 @8 P. w/ f, Z
2.没有pcre
+ N; z# B  y, F* p8 {8 k% ~5 Q/ d2 M
configure: *** pcre library not found.) N+ V) M5 P; e  k) a
configure: error: pcre library is required8 k/ @# \) n% V, a
解决方法:( ~- t7 `7 \7 J9 I, F/ B

/ |& [# a( `. q% A8 B1 }yum install pcre pcre-devel
  ?' u1 h( ]" W1 F3.没有libxml2+ C8 m: E) [9 ?4 L
' d, h6 m0 ~5 h+ l: Y
$ g. R& G% _" Q2 P
configure: *** xml library not found.
3 m: ]1 k1 {) |8 b3 ^configure: error: libxml2 is required
6 B# U6 ?0 X# K/ W5 X, E* c1 i解决方法:: _$ a% x1 j8 y, v
( ], v5 Z$ I( n
yum install  libxml2 libxml2-devel5 d; N% I! w  x, g
4.执行 /opt/tengine/sbin/nginx -m 时有警告1 \4 Q8 R, y- ?" e

* j- p5 \3 p, F) M, ^6 g9 `Tengine version: Tengine/2.1.0 (nginx/1.6.2)4 r8 I( g; g: y/ ?4 g, t
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
8 [  j9 L5 E# q4 r9 ^: o1 j原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log4 @" n6 u- K  h- j0 D4 l
4 _) Q" c2 L+ [) ]% S. N3 r; t; n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
7 `& i0 I7 y1 Q5 P  B6 g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"; `+ O, U1 n, o: {
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
+ i7 O0 K$ Y& M: |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"8 p5 {! U- s5 S8 b+ v) v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
! [, D: K! n7 n: y( z2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
$ P- V- Z% b4 |$ `( ]! d解决方法,移除低版本的APR (1.3.9)8 y1 z: ^! E2 a

! P2 E2 T5 Z8 Oyum remove apr: g; ~* I6 a5 y& c' d' a" |5 c6 h0 |
5.Error.log中有: Audit log: Failed to lock global mutex
! R, x" ~& X) p, K& A+ f+ n- W" Z+ i* d
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     + [0 e# p, d- }. L' ^
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
8 B) G. b8 Y4 M# v; V$ Q8 I3 e# _解决方法:# K( o; B5 X) f& q& K8 k( |( z
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:$ P4 o8 j0 ~! {4 H2 h
8 @+ x0 W( v6 L- @* g1 P& @
SecAuditLogDirMode 0777
* E$ d' Z; s# n/ ^# ySecAuditLogFileMode 0550
% N- [0 R1 c. y8 s0 O  o/ rSecAuditLogStorageDir /var/log/modsecurity! R1 ~1 j8 U0 A! {
SecAuditLogType Concurrent
& x* s" {  R5 m参考文章:  C7 w2 K: w1 D8 n6 s$ I. o
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX! F: ^% L8 ]4 d7 u7 z7 v
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-3-4 12:49 , Processed in 0.069451 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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