找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10753|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
( R3 ^( p+ @7 ]0 x- J% I. [' U8 l2 {* m, M5 v
一.准备工作& v# L- P/ o0 D. j
) \$ d/ G$ `! g7 U
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 X* x$ n. x$ p" X
# E- {+ B( r( t/ L% Xtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* U$ Y  e5 m- S) w! t  N/ ]0 r6 w# h% s; C$ v0 Q; f
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz" U. c; B$ d$ B/ ?

6 Z/ ~3 C5 s0 T2 k, L7 \- k3 I9 r( }OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs/ ^4 E7 E0 M4 O" b# p0 k

8 y% P# U& ]1 Y4 |3 J( n3 P# f& h依赖关系:
! n7 |/ B  p, Z% c  H, x  Stengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
5 B4 x7 |( |' [$ ]* m' M, u
% T5 K3 {8 W3 V9 b6 C" c6 ^8 L" ^yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
- D7 U6 |3 u9 q! r7 K1 G4 mmodsecurty依赖的包:pcre httpd-devel libxml2 apr9 N: v9 G- X- Z$ X1 q, _1 u/ b- m
" P/ V/ U& ]1 A- ~6 M: d9 b' g
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
) C  i8 ?; [' v, \6 }二.启用standalone模块并编译
' f, ~# |. w% x9 ~2 M) T2 p0 m* L7 f+ T# Z
下载modsecurity for nginx 解压,进入解压后目录执行:  f# o; q4 r  ]# x4 I
% r" k6 ~) [9 u9 H& g
./autogen.sh. z$ S  s; q: m
./configure --enable-standalone-module --disable-mlogc! k$ V7 b" C( Q" R5 L7 W+ Q, R$ \
make
  t$ V' {: Z& g# }2 k/ W  G; ?9 S7 |三.nginx添加modsecurity模块
. Z: x4 i3 x7 z: K# E; y% o  d
& n( m/ R( F6 m) v; D( t1 J在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
  y8 d6 |0 D5 |0 H  k9 B
& [5 H/ w: _% y2 U5 F  N) U./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
+ `  }/ @  G+ h8 M& n! imake && make install  V' C* N; c3 L" r+ c5 l7 ?
四.添加规则! J. |0 d; j/ |
, a! I# Y$ e# i
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
# R3 g: Z* J8 K
; l! i3 s' Y, P) i/ g# w1.下载OWASP规则:, B4 X- Y% Z$ }( [4 H

, k4 `+ ^; v+ L+ l2 igit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
7 O' n' ~- h- K1 |8 }9 t' E! H" n% ~& O) @, Q9 {8 e' g0 N0 [
mv owasp-modsecurity-crs /opt/tengine/conf/
1 o. F/ t: ^& {6 B. Z0 M) J2 ?# E4 F$ F" ^
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
- c0 b' y6 Z6 J3 P2.启用OWASP规则:
- d$ U2 T* f9 z6 s/ `" F+ l  u: d5 Y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ F$ M. t7 _4 V/ c

8 e. l/ \/ G- ~# U- I编辑modsecurity.conf 文件,将SecRuleEngine设置为 on) l; {' _( m' h& o1 |

. y4 H/ P* L% q, _& qowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; S+ e5 b8 H0 g2 D
2 H' T- }% w9 w; _, }5 c
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
3 a1 W; f6 S; M2 ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf1 M6 D4 h( |. z, ]
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
4 E  p6 U# e- T/ w1 P0 lInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& a$ ~. n; [# a) V
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
5 }" x* Y4 b. N1 f1 C8 KInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf% z* A9 ~+ v. l2 k
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf4 T0 k/ h- W; F0 M9 w( O0 l. {5 h) |
五.配置nginx
9 v9 W0 o6 o" _2 k% ?5 A$ i) T0 c0 p" n0 ?& a1 x8 S& l$ m
在需要启用modsecurity的主机的location下面加入下面两行即可:/ \0 V6 F5 [+ w$ J1 X. K8 N
7 i' Q; T; c' n8 H. Q7 E% A6 |* c
ModSecurityEnabled on;  & j3 U: t# ~; f
ModSecurityConfig modsecurity.conf;$ ]6 G1 N- j( J) V: i3 f: g. L0 g
下面是两个示例配置,php虚拟主机:
9 c. e- ?3 M8 Y. t# \
0 H/ u- M0 S4 |server {6 V6 s, I- i1 n2 l5 j$ e
      listen      80;7 T( {" z7 Q- Z0 ?# H" @
      server_name 52os.net www.52os.net;
# s' w! |4 q$ V9 n% @  S. ~  |     
' }9 a9 t9 F" }8 g: p8 T( j* K      location ~ \.php$ {
( `9 r+ V6 m. b$ z% b7 i      ModSecurityEnabled on;  
- B3 D) }3 R% `/ b! O4 b6 T      ModSecurityConfig modsecurity.conf;* j" {) Y& K; M0 U* p' n1 n$ j
* Q: N; I+ D3 r  B4 L5 \
      root /web/wordpress;
( U8 }2 L+ C& v8 X: f' [      index index.php index.html index.htm;
% H8 ?2 j- V8 j- ~: y. ]  
+ G# z! f; w5 J+ n+ o      fastcgi_pass   127.0.0.1:9000;: q# n: v2 r$ Q5 I, k. A) J
      fastcgi_index  index.php;6 O. z0 m+ j. _! `4 N
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;' b) `8 q2 A/ q* G4 V% p
      include        fastcgi_params;
9 |, K0 x) {: {' b      }
4 b: T$ f2 Y% ~  }
2 j5 u& \( E* Wupstream负载均衡:
& |) N2 P4 ?# g, H1 ^! A& m# a; q8 r5 [2 `+ p2 O1 B% V
upstream 52os.net {
, B7 k& k. b" w+ W$ Q$ ^    server 192.168.1.100:8080;
. [0 n" P) `6 H% e2 M: k3 W! s    server 192.168.1.101:8080 backup;
6 Y( q: T; X( t}
) o& H) t: L# k+ D" K) o% q  j
9 x, A7 M+ B4 v, }server {3 |4 K7 E4 M" _, l# r- x3 C
listen 80;- x$ o+ J: k8 Z+ }4 C% R- A
server_name 52os.net www.52os.net;# f& S1 H4 g  |4 X2 U6 H

" F( t/ c3 i3 q1 N- w) K2 @; ulocation / {
6 e* L; }3 d  k  i    ModSecurityEnabled on;  6 N; o, Y- A9 e
    ModSecurityConfig modsecurity.conf;  
" e4 n2 e% D% v; k2 z- j
  T" Q8 s& ^, H- k7 v: n5 ?& i  S        proxy_pass http://online;
: g7 e( k8 v! x- K5 ~0 w) h        proxy_redirect         off;
' Q2 L7 H7 _4 e- L        proxy_set_header Host $host;
: |% t( r1 E1 r, ^        proxy_set_header X-Real-IP $remote_addr;6 q# y' @9 ?; A
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;+ S2 b3 R; w/ h2 i1 t0 }
    }% ]6 M7 c! f" e( o+ p; T4 d- J
}: V9 ?4 v. h/ i  ]& v* s/ B0 o! u
六.测试" Q* q% @0 P/ Y8 s9 b2 p3 b
. _% S, Y4 c# n. h3 f
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# y8 m+ r2 q$ W$ w5 P3 i: }
9 {- I4 Z! X% w/ p$ D: u( ^, R
<?php7 f) m6 l# W4 H7 n0 z
    phpinfo();   
+ E3 X" u0 u! Z* |  j2 i0 v* g3 q?>
- g  i, c- E' h0 G9 U* ]/ o: C在浏览器中访问:
" P: T' P8 P  ~0 q+ L6 X
* G& y0 u7 K8 y2 H/ p6 bhttp://www.52os.net/phpinfo.php?id=1 正常显示。
$ l* f- H+ z) v. w- X9 M+ K3 @http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
% O' ^& T1 U* s: N1 {3 ~http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
+ F- f& ]8 g: l9 y8 S; m说明sql注入和xss已经被过滤了
) a# B# h5 A; {0 l, d
( V" B* Q8 P6 E9 G) ?七、安装过程中排错! y- R0 _3 {6 Q4 \

& m8 j; X- C- @( X1.缺少APXS会报错
" }: l1 l+ C- b0 \# Z" e
7 @$ k' c/ |; }% @7 A/ a7 b1 uconfigure: looking for Apache module support via DSO through APXS
: m7 T# J& b% h' K  ~1 i' ?; R: Wconfigure: error: couldn't find APXS  p7 n/ N# i4 }/ {+ E" u
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。7 f* \9 O5 M2 \
解决方法:) w: k$ h1 A% u4 K# s6 |: S# _2 i
0 B+ \+ M/ V. y& J* A, h
yum install httpd-devel
, o" T' Y* c. w% o0 W2.没有pcre
% d, Y, `2 [" g  w& h( ^- _" T! j8 n; A5 K
configure: *** pcre library not found.
5 \& X' T  ^- z( j9 _7 F1 yconfigure: error: pcre library is required" t. j" V  S$ E: N
解决方法:
0 s# N8 {: j* o$ X4 @6 W0 A& g4 o% Z: k
yum install pcre pcre-devel
3 f, U$ [+ v# O% y9 m" Q2 P3.没有libxml21 p; }. Z  @% o1 h8 h

( B" R5 i0 v0 G) }# w7 Z; ~
* Z% C$ a* f# ^; [/ A" B9 q' Econfigure: *** xml library not found.
* |& _. |) T" [) l% `4 Sconfigure: error: libxml2 is required% w0 d! w- B4 ?$ _* y
解决方法:' O( @- h' Q3 V! W( o- J

5 S6 H" J9 A4 @" i7 A1 v' Cyum install  libxml2 libxml2-devel
2 l- K( r9 _2 F+ a4.执行 /opt/tengine/sbin/nginx -m 时有警告. s  e" |% ]2 }& X" A# y+ [, G
: B' m- i5 G" t3 W
Tengine version: Tengine/2.1.0 (nginx/1.6.2)9 j0 B" q2 [+ O% {' w0 D
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
, }! p% W7 I6 `, z. F原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log' |6 e7 p2 a# ?! b/ G. _
, i. T# O4 Z5 o3 ?2 p* v- ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.2 Z! l& A# @1 m& J3 L8 S4 ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"+ n, D9 I8 M% Y5 V3 G( T: O  w
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!! P: Z& \8 s1 F/ j1 l! @( S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"+ Z4 x. f# r- b+ d* \- ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"/ o( ^+ B4 f3 D  t2 J
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.1 J* _, V1 L' J$ p8 M' [+ e% j' N/ h
解决方法,移除低版本的APR (1.3.9)
/ g: b) i' C" ^3 C8 I7 O7 b1 T
; d  U5 N# }' A/ y2 V3 Y2 cyum remove apr
. d' n0 M5 Q7 R2 W7 Z5.Error.log中有: Audit log: Failed to lock global mutex
+ F6 P3 C( {# j. E$ N  e. J' C6 S
& \3 e9 o7 K& ~5 E  b2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     2 W8 _2 X+ D6 ]6 b# D8 M
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]) _+ g9 u& Q, w9 Y0 e  u
解决方法:; t0 r4 `0 K+ Z3 l6 ]
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
5 O' ?# V- K% M4 B( F2 C/ _+ w  G; q5 u# _$ X+ X$ K+ t$ r
SecAuditLogDirMode 0777, B6 _2 |; c9 g# J& G& E- K5 E4 H
SecAuditLogFileMode 0550
% ]; t$ M  c/ VSecAuditLogStorageDir /var/log/modsecurity" T' f) u( I; u. L
SecAuditLogType Concurrent
0 ^( M1 e0 G5 \9 R& c参考文章:
* E8 g7 ^% l' }: `: J( x  G. shttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX- P( _% I- e: }/ c) ?% n
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-4-24 14:57 , Processed in 0.069601 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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