找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11926|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
, I" B. O& }3 a5 a' u
$ N2 P3 E( ~* R# W; \一.准备工作0 [/ S3 P3 e6 E2 z- W* ?
* G+ n' Q' |+ r7 g0 r
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.04 q4 i) }3 n) x3 t

) a9 @- \: ^1 ^" ^1 z8 Itengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz6 D' g7 s8 O/ m1 P; U: D$ S9 i
# G0 ^9 h4 D7 L6 Y# |& A
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
- {) o" v' u" O/ E+ V
, h# b) Z' d% N7 B: H9 J6 kOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs# d8 j0 T# j, _# I* W! A% U3 H
# U* h, ~* g+ V- G) F" Q
依赖关系:* U* S: H7 C9 q6 O
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:  |& u! w$ I+ S. S- p3 ~' n
" n  O9 ]' Q. E
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
5 S! Z) ]1 V3 c2 ]+ nmodsecurty依赖的包:pcre httpd-devel libxml2 apr
$ P  k1 W- `& P, l7 R
! K9 }+ ^# P3 `) D6 Yyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel# E- G' P1 ?, u" ^; F& K; R
二.启用standalone模块并编译8 K% w' q; Y8 f: k; S7 B
2 a8 B2 T/ N2 {2 d0 F/ @
下载modsecurity for nginx 解压,进入解压后目录执行:
- T8 I% g9 Z0 L$ S. Y3 K& b! K# b
  ?& D4 Q0 U1 A& O# e5 @% m./autogen.sh
( d" u3 `7 m& `- v" r5 ]./configure --enable-standalone-module --disable-mlogc
/ h  B3 Y) E" J5 L; w& l. Y) Nmake
: o! ~, ], `$ I; ^' K! ^三.nginx添加modsecurity模块/ C  g- u9 C, y! X( b8 z, \

0 k" ]) f1 g3 r9 c0 R; ^1 ]在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
6 g. u  o' t, e; p8 U. q* u6 |5 P! L" d2 c
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
" C$ X4 A0 z. ~0 xmake && make install
* o+ y& T8 ]3 I% ?" j7 B四.添加规则
/ t4 a8 w1 ^) d5 ?
$ X3 f7 [8 w1 n" i" l3 Kmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
9 x5 ~1 a. L5 B+ m8 R8 C8 j+ a- r" `& J3 S
1.下载OWASP规则:, z* ~& ]3 t6 A2 z
' s( b( o; b8 p$ {1 W/ I5 Y# }
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs3 {8 B# i- h, Y$ @% N0 L0 L

. o! `8 H. ?0 O8 x% i$ Fmv owasp-modsecurity-crs /opt/tengine/conf/1 J  m9 y7 Z) [) E9 J3 v# s
+ s" t' v: d# {0 s1 o
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; e. p+ h% _+ d% l; G7 q2.启用OWASP规则:
4 e( V. N. ~5 N- X# H
4 a% X9 [+ f7 P* u# p  d复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ E: O2 a4 e; z$ M) ^1 \
! v+ _. V4 j6 |+ i
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on4 l, d) y5 |! V% q+ f& J) j+ I

+ ?' H2 U, ~& n( t# Nowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。0 G5 h- v3 {0 Q' J
6 p! H8 \4 k5 \
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf5 x! C  f3 I4 W9 E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
, B. Q% g( e5 Q, V/ @; gInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf2 i0 Q; y! m( f1 N& F4 t8 L- n
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf( b9 j* U  w: n. `8 g& G# f$ x9 q3 G% ^0 G
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
, }( b! K; j6 k: G' hInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf/ M  I% o1 R  S' k
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf) L; z: u+ @- V) ^' w2 {. p2 j2 X
五.配置nginx$ R+ M* W$ L  d3 g6 E3 Z0 i

$ ~, b( A" _" z: d( l在需要启用modsecurity的主机的location下面加入下面两行即可:7 k4 X/ q9 \% }3 j5 k$ \
1 }9 a/ N$ w: J8 o
ModSecurityEnabled on;  
8 g2 e) A# V  R1 JModSecurityConfig modsecurity.conf;4 @1 |7 L$ n5 k7 o8 Y; \; q
下面是两个示例配置,php虚拟主机:0 A8 K3 K8 h# [

: {4 a* J1 L  b+ V) U' M' O) I; @  |server {
4 K7 @0 i' D8 {4 j7 m; D      listen      80;8 W) `5 q5 g4 d7 ~& H
      server_name 52os.net www.52os.net;2 q- r+ ~4 c  P. P
     * q# d# D' S; p4 d8 T0 @$ ~
      location ~ \.php$ {8 x4 ~3 o" B6 q1 Q. c0 D+ X4 R
      ModSecurityEnabled on;  
  B4 l- o* G% j0 @2 C+ S  w      ModSecurityConfig modsecurity.conf;
8 u& t$ o- Y9 z# G6 b: m9 s
& z6 u, ^  x: u( b      root /web/wordpress;' Y$ z6 G" J7 v7 \6 ?  ~" Q1 R* d
      index index.php index.html index.htm;$ f" k- [. D! x9 F5 {
  
9 r4 T8 l0 j" o( `& L9 G! g% l      fastcgi_pass   127.0.0.1:9000;
! W/ W1 ~1 @% q; }7 m- |      fastcgi_index  index.php;
5 i! Q2 z& Q4 }8 Q: t; i      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;+ g# I9 X# d8 u6 M+ v" J
      include        fastcgi_params;
+ V3 z9 v8 `( b- Q7 Y% B9 q      }
/ s/ q* k  j7 t! ]5 P( k  }3 p* n9 I7 h% Z7 L
upstream负载均衡:
: t" `$ K8 U" @
. |9 A2 x2 V' c$ Y% ^5 g* Z- Fupstream 52os.net {; G" u. c; f& u/ R
    server 192.168.1.100:8080;- B: X6 q2 J8 R4 ~$ C
    server 192.168.1.101:8080 backup;
) N) S+ \! ]# W4 h+ W}, ~) i, S- O1 I1 D
! L- U( O* [* @( t4 i  D6 [
server {- S: P+ p" i( g5 ]* X) |* z( S: w
listen 80;
5 V# o# w+ |0 \3 gserver_name 52os.net www.52os.net;( K5 K8 h4 K+ ]" F5 G7 ]

1 F% f% k: S' }5 Blocation / {( W6 p; R3 }2 `9 t( E! _" {
    ModSecurityEnabled on;  
) t* _% C- i! K    ModSecurityConfig modsecurity.conf;  ! d7 X3 Y% ^2 }* ?
* U) y+ a  U5 n+ d2 ^$ t2 s' u
        proxy_pass http://online;  D* e$ ?) V# o# ^3 g7 M" b
        proxy_redirect         off;. `5 D6 F1 v, W- i6 e. R# \
        proxy_set_header Host $host;
! f4 ]$ c) T9 e6 L        proxy_set_header X-Real-IP $remote_addr;
. r# J' T# T& s0 H. {        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;% s+ V+ q# p; w$ v4 s. F' Z
    }# T4 e* v; m  c; d2 N0 @4 O9 i
}8 o, ]& y( C, d. e
六.测试: x& G: ]! _- D7 ~3 d3 a
/ `' X1 i3 P# h" m3 B8 q; t% z; p
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
6 I6 t6 D% \$ s6 g' b& |/ Y9 b4 ?( L3 c# K% Q
<?php
7 O& S2 G) R' t    phpinfo();    2 t& A/ C; ^# W
?>$ m5 C( s1 S7 ]# t
在浏览器中访问:. O* Q4 S" K0 j" r/ |9 {
+ h2 l, g. U( T
http://www.52os.net/phpinfo.php?id=1 正常显示。
/ [5 J+ t' Y# c4 x) z3 V& Mhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
6 Y0 a* C" s3 Khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。! u+ n- q/ ]2 |
说明sql注入和xss已经被过滤了
0 S9 a+ v( d! H& W  l7 Y8 I2 W, ^% d3 E
七、安装过程中排错
6 Q* g; j4 C8 V
- r& M8 o0 m% `6 Q1.缺少APXS会报错, ?/ N% V( z; c9 s6 u! V
, Q+ W1 K" J# y
configure: looking for Apache module support via DSO through APXS
1 A) Z/ P/ f1 h- l( {6 \1 _: cconfigure: error: couldn't find APXS
( D( U/ r* e, `& N: Aapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
4 B! J2 o6 Y/ M, R解决方法:0 S9 G/ n5 I- n$ A) O
, I' g0 _/ |( y7 X
yum install httpd-devel
: D9 P9 l6 A2 s) X: w. N; U2.没有pcre
% Q8 i& m4 k9 [3 x' `% `& a
2 g5 g9 X% ~/ u9 P6 i+ w# Pconfigure: *** pcre library not found.0 I, u9 i( ]% y) w4 b7 B% r9 o
configure: error: pcre library is required8 I% n7 @5 a4 m; F: S
解决方法:
2 ]; s: O5 c1 ~# L: K6 ]3 P1 l, s- g* y
yum install pcre pcre-devel
0 G7 }* Y+ x# p  |. ]- `3.没有libxml2
0 y+ r# @. h" P. j: G( `
9 n: D( N  N+ }) b8 K3 i( W2 g( c& y
configure: *** xml library not found.' m" N; n+ j2 e6 y2 O
configure: error: libxml2 is required
. {9 a7 ^& \. r: c7 u解决方法:8 D& R" w+ h2 U7 b8 _3 H! H

: m* s6 p4 N! I' {% ^. Byum install  libxml2 libxml2-devel5 I, Q, L1 i9 u4 K& K2 N
4.执行 /opt/tengine/sbin/nginx -m 时有警告0 }6 U# \: k& `: u1 X& D
% Q7 n$ d& j# {8 }4 G' v" X/ C
Tengine version: Tengine/2.1.0 (nginx/1.6.2)$ e& |- K% B5 l
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
. i( ^( z' r! g9 J3 y. D原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log0 ^) J1 u9 J- y+ N

/ l# m0 ?  f5 F: W4 n+ B9 p9 R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.- Y  n! I" q# m7 G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
+ ?: O0 ^. x7 C- P) z3 D2 ~2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
3 B  k6 V" _* R( n3 U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
' j6 l8 w% a7 T, M& z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
* B' ?/ r5 S9 ^+ h2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
. X$ N; I) k- q' A1 G2 E解决方法,移除低版本的APR (1.3.9)
: @, O3 P; Q  j4 ^. q
+ Z4 n# M( l, l/ b% P; ^yum remove apr) s  n9 }& H" T
5.Error.log中有: Audit log: Failed to lock global mutex
* Y9 m1 y5 U3 L; r& Z7 l8 B1 b8 Y+ `. w, F$ s0 ?( ^) V
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     " b6 T3 s, \; @6 v% N
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ {3 @8 K; G+ r解决方法:
' C3 _( ~% T& `" a, v编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ c6 V) ~# h1 b$ z6 m# A  ^. K" h; M5 [0 l, U
SecAuditLogDirMode 0777: v+ Z1 ~, b2 s. e& _; {5 G
SecAuditLogFileMode 0550
7 n, C/ G, |# P! {. VSecAuditLogStorageDir /var/log/modsecurity9 n8 v6 W2 [9 W1 [: N8 U3 G
SecAuditLogType Concurrent
( v# j/ K6 m& G参考文章:1 h3 T9 M7 P/ O5 a4 n/ g: x/ s
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX, l6 M" L& o( R$ M. l! j
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-26 07:00 , Processed in 0.095718 second(s), 28 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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