找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12244|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。7 h! ^( |) M1 F1 J' Q* j1 G4 m

# {6 P, M3 L4 P8 T+ e一.准备工作
% p$ Y. e/ a; m. l. f# w$ n
" b$ ?4 n' _8 b( F/ U系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
9 y2 Y( B' E! I2 g- g
6 ]' j" ^' U; u- Ztengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
8 P$ Q& _7 t% O- V
3 Z. |' D' D9 F9 m$ D& m# h4 r( \modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
% g  o0 L% {2 \% u1 E3 Z; s( i* @3 E4 ?' g
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ v9 D1 A" \: D/ ?+ s. V, @9 p0 i! b
依赖关系:* ^' B/ V& A- X( N
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
  \" [1 k4 O3 u8 ~5 h
, h+ a* V2 U% Jyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
( v8 u" @$ }- t- q( Qmodsecurty依赖的包:pcre httpd-devel libxml2 apr
* o  W! i& t. \( {6 h
3 r" s9 H  U/ P7 tyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel& R9 g  e! z% A, Y$ s: v  d
二.启用standalone模块并编译
1 |' M7 T- l! z1 m) ~7 J% A$ E7 l1 p4 P; F: H8 B* d
下载modsecurity for nginx 解压,进入解压后目录执行:% o, Z6 o3 c6 x4 G4 ~( Q

* N3 B/ H% E' ], a4 }+ x( c./autogen.sh
4 r' q2 r( Q7 Q  P" H+ E  @./configure --enable-standalone-module --disable-mlogc
8 j5 c7 j# I3 tmake
  |$ u/ y# F* b. M4 w4 w三.nginx添加modsecurity模块
- W- y+ l/ x& Q& R" H
( l0 l! e5 t+ Y  Z7 l在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:: ^% m- n* r# }3 L( d6 x3 p. D

. T+ u# U5 }1 S& Y3 h+ F+ L: W./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
1 d# f$ L2 s( s& J3 m& m6 A7 Omake && make install, h$ O7 h, [0 q* c4 [
四.添加规则" M  l- \/ ]" E, T9 _) J
; E) i9 B4 s  A% g
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 m1 |4 a# o: p( o
- S( \! j* d- t2 G) W1.下载OWASP规则:; s1 R2 ]2 X, w, }8 f9 T, G

* S9 j$ Z4 j1 S2 u# `8 l& E8 l% ?git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
8 X8 e5 Q% ~) D2 H/ }9 A; _! R3 C! R0 a, w" w/ [) x
mv owasp-modsecurity-crs /opt/tengine/conf/
4 `! S0 B, Z, p, o  ^* `8 @! V8 k) I7 g
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf0 G6 A( z0 H* w" h/ r$ j
2.启用OWASP规则:
! b3 |+ l. I) S3 z$ J2 t7 s4 W" E, E' }# H4 ]& k5 l
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 x0 k* q5 p4 q0 ~$ u
. [0 `5 A4 x! e0 N% o
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on8 U/ O% R; c' q" M6 i
5 z+ ]4 L, `& l6 _) ^- B
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 Z! {# [$ H) \
. G. Q/ r0 @, P* UInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
0 E9 `& }0 i1 KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf) @1 a1 n5 Q$ a6 p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
" g8 X( z* i$ }4 g* r$ vInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
+ Z/ N: h7 l2 t" D- cInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf  J: [; M2 O5 [4 B& L: p3 T
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf4 @  J  P- f  H7 S3 ^* S/ C: Z8 |# [
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf6 U, O# {% W5 B, E: S8 q
五.配置nginx
/ h2 m* W5 W$ v0 B, |: l# f# B7 ?$ M& s/ ~
在需要启用modsecurity的主机的location下面加入下面两行即可:
8 ]+ n+ f' P# N6 k" [
8 M: |7 U( [6 g9 XModSecurityEnabled on;  
! }. k! p( [: W. |ModSecurityConfig modsecurity.conf;
' I! y- E- q; c; g, N- p5 o, F+ e下面是两个示例配置,php虚拟主机:
1 [& q; [! x/ `& H, x: s
3 j) B) F  Y; P5 N+ ~server {) ?. F* X" z1 B% ]( E; _
      listen      80;
! z$ O! C) e1 W+ K+ y4 ?* j      server_name 52os.net www.52os.net;. k3 x$ d2 A8 U$ m
     9 O( I  P8 m4 R: D) F$ ~
      location ~ \.php$ {
. e  Q! I7 ^. L( G      ModSecurityEnabled on;  9 n. Y! L- H0 n& x, h, B5 C
      ModSecurityConfig modsecurity.conf;
3 s$ T5 |: ^  r4 Y# Q8 S/ I) k  y- |) L( ~
      root /web/wordpress;" ^0 [5 Z* e$ T% W
      index index.php index.html index.htm;
7 L+ l$ l3 ^9 R& n3 n! }  4 L$ V; y  ~+ a3 j/ P6 x2 b& [* g
      fastcgi_pass   127.0.0.1:9000;& s0 y, K1 Z  m' `
      fastcgi_index  index.php;
* `* _. G; U" _( z2 y, X      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;9 C1 s. \2 [% ?
      include        fastcgi_params;
4 T; Q. U' a/ ^" @6 X3 q% ^      }# u: A/ p$ y4 N+ w, J& T5 ]9 z6 a
  }
) V0 n$ G! X( n  Q" fupstream负载均衡:: N9 v0 f! V. j* i- h
) a8 ^+ P. m) }4 N1 R$ M
upstream 52os.net {
4 O' d" z% w6 F" h' T  k+ B  A    server 192.168.1.100:8080;( I! z6 {5 K; I3 }1 _% z; W
    server 192.168.1.101:8080 backup;
+ X& a! M; n/ ~# @- t) l}
4 T; p0 J+ z( e+ S: O) V2 f5 N# e2 m) {& j( m
server {: H$ T2 D& I8 P
listen 80;
* _/ ~/ ]0 Q3 k+ h6 g" Mserver_name 52os.net www.52os.net;. U* f4 z, k: j0 j" o, f4 r; Q( X

7 G0 Y9 y+ \* F  F+ s% llocation / {* f  D! i% \) E) [5 i8 Y* a( @
    ModSecurityEnabled on;    E7 m( L8 s5 c
    ModSecurityConfig modsecurity.conf;  
( l  _8 @$ ^* f5 O; W3 R
! E9 ~; X# L, j+ c& y. o) y! R( B        proxy_pass http://online;
/ R- V+ l. i$ N; @. E( c# _        proxy_redirect         off;$ j, U  [$ Q1 Y! ^- w
        proxy_set_header Host $host;  ]/ J2 x( o5 @) v+ D+ x
        proxy_set_header X-Real-IP $remote_addr;/ B$ g4 _' {4 e0 j" f. |. ^$ F3 {/ f
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
& i/ t: ?2 \! q' ~    }
* h4 ]) y; y( R4 S. {}
) `. ^: b' `  C8 M六.测试" a2 M9 P$ f' e: r4 u
# \3 z- d3 s+ d( y
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
3 M1 g: U* h) E
  X; K0 G% M( |  |2 V, j* s<?php" ~7 ^0 m7 ]% a* i7 l) |3 t
    phpinfo();   
, `6 h4 X0 |. R3 Y! c& z1 W  o?>; S1 u; ?: o* G: {
在浏览器中访问:
/ }1 b9 m7 `( B* }8 o! R/ }, ]" X! @' ~% q0 V( N6 C
http://www.52os.net/phpinfo.php?id=1 正常显示。9 G* d6 Q& ^: R5 y. H
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
7 [! d; a" X% u& b1 G% ghttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
4 H4 D4 H5 V. Z0 |" x1 |7 [" k说明sql注入和xss已经被过滤了
: R4 ]* Q7 H: V/ u8 I
$ B' [4 o6 s! v6 [2 U( @: J七、安装过程中排错- Q) a, V, F) W( n7 ^7 O4 w& w: l" L
6 u2 A; a2 k: L' n0 z. q  p
1.缺少APXS会报错/ y  D! W9 K3 w2 N) X% j

6 A- c/ d% {7 s0 l* Oconfigure: looking for Apache module support via DSO through APXS0 u' I3 y! V9 {" Q+ U& c9 ]
configure: error: couldn't find APXS
: ]3 w. _% k" S% o& e% Eapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
7 B8 M$ d4 J2 ~解决方法:1 a- z. l- D/ u! z0 N

4 t5 b; R+ N/ Q7 Z' ?$ Yyum install httpd-devel% Z2 |3 E, j! @% t
2.没有pcre/ f+ d- Q" G4 j+ D2 H/ p
$ B& i  p! x) D3 q. }
configure: *** pcre library not found.) y- n* J- F3 `8 O* U
configure: error: pcre library is required
& R9 b6 z- c: S) T解决方法:
- S. U0 t+ Y1 U  P7 K4 U2 y8 K& i& E) s& o: i1 |
yum install pcre pcre-devel
0 z+ t, V" d/ p5 ^- ?- C3.没有libxml28 J, H" i( `' k1 c! e! b) m

6 `7 @6 F7 a8 W( s# l( y3 h5 @( t# F" M# h7 [! p+ R: O9 X
configure: *** xml library not found.
9 M% _3 N$ w  |8 T7 _+ ?, ^configure: error: libxml2 is required1 y1 x# r* x" b1 E. m. s
解决方法:
1 l. J% j0 m" D0 M+ C9 i2 F8 g
0 Y9 t; [4 U8 x0 I- Y; n! Z- R3 cyum install  libxml2 libxml2-devel
8 A7 [: X$ H, C9 h* M1 B4.执行 /opt/tengine/sbin/nginx -m 时有警告. g; A  I' h+ e. @# e

! M& O4 A8 y6 v$ M# m; D! d* I0 BTengine version: Tengine/2.1.0 (nginx/1.6.2)+ \7 {* o( F; P) ]) [2 k/ C
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!1 X6 z6 f% {4 N
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log5 y9 B$ a: P! f0 `, }0 X
9 m9 i1 M/ h: ^( X" w) z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
& n' G% }8 ~3 Z- @+ Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
3 O, {8 {+ Y8 W: [2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
; |5 L+ _! E. ^3 {5 m6 q2 Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
  K4 N# A3 u  ~4 O* j! `/ h$ w! N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
: I7 b+ w, X. U2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# h  l* S; H. V1 s
解决方法,移除低版本的APR (1.3.9)
1 Y/ `( B. v$ m3 T# g6 v! G
9 h; q/ E: p1 F# H2 ]- pyum remove apr
' B% U4 a8 U" [6 d' t) ~5.Error.log中有: Audit log: Failed to lock global mutex
1 [/ t* q% @& y1 v
1 n0 o- U3 ^# a# i5 ~, F7 ]4 W9 u3 X2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     , t1 Q. Q: ?2 _) Z( d
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]  ]4 M% l* W- E3 t9 V3 a9 @
解决方法:
+ ?3 P  @% k: V0 B% K! \编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
6 d- X3 e$ S. D3 \0 {
# R: A: h# s7 H' U5 g' {SecAuditLogDirMode 0777
' R/ F5 A, o! X3 g" fSecAuditLogFileMode 0550: {( |7 |( \# d/ s6 t: y! E. U
SecAuditLogStorageDir /var/log/modsecurity
3 j- a2 E, O+ V2 h. bSecAuditLogType Concurrent
- U' O! \! T# h& S- B7 N参考文章:! \3 y# Y! A0 h% \
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX3 F2 _" I% v# M& p
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-19 02:18 , Processed in 0.049371 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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