找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9495|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
' ^6 ?( ^0 Q8 U$ u" J2 s
5 d( R& ?) h* @) T一.准备工作
. f4 t: u2 s7 U" Y. @% ^2 ?3 u0 Y' s" m( u8 O
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
$ U7 t4 E' n! z3 f9 W: t, Y
1 }$ a; G9 K! a% |8 }tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
# `6 }& A$ O! e/ g8 k5 ?# {1 H1 P. R) q4 F& I& A
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz, k) k0 i; t8 m( x8 U& V- L

5 e0 L2 `  ^2 Z( `: OOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
( |+ L4 a( R' Q2 p% w6 @& p5 R! E9 E% x6 ?
依赖关系:$ O9 ?5 P0 Z/ `, n& e
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:; A4 X* M9 P9 p( h; t) f
: G9 x/ h( H6 r3 p8 R& q4 S
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
3 P1 s  I% b+ g& \modsecurty依赖的包:pcre httpd-devel libxml2 apr
9 i/ O% @7 `# G  |1 L2 q, ?- J" u5 {, q9 s) {
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
0 l# J& l# W3 D/ o3 D二.启用standalone模块并编译
8 @) H4 I! h7 D; H0 e$ x2 e
  o3 _4 G3 A: M( N- B下载modsecurity for nginx 解压,进入解压后目录执行:
8 u8 W0 I/ P1 |$ {6 I  v/ j9 H+ i2 s
./autogen.sh3 K" C: |2 \0 j  s5 g" i
./configure --enable-standalone-module --disable-mlogc9 a+ c! R2 l1 `+ u& Q+ d
make + O, P* p$ f: I0 F# w- c) g
三.nginx添加modsecurity模块7 V+ \# {' x3 {) \
7 a$ H3 k: f" s  [& J- I/ q
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
! [5 S9 H$ J' a; \  H( ^  v2 X" S. ^( E7 X8 u$ ^  {
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine6 U6 Y  R  P$ b0 ~
make && make install5 R) L6 W6 C$ ~: K. K: X& w
四.添加规则
8 B$ `9 ~& N* ^+ x
' C# ^( N  W: ?6 ~) {" h( smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。) K5 N+ F7 Y  l$ O: G( }
" @: O8 t" |  b
1.下载OWASP规则:
1 T2 u7 M( C( k; X  i# n$ _9 _$ H+ w" e/ W  @. t; M7 B% A
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs' h! ^" j4 I% w) _

9 K: N) d; Q% v  hmv owasp-modsecurity-crs /opt/tengine/conf/
& S' T8 V2 s2 o3 M( t1 Y* N$ I
2 |, |$ `7 l/ v: l: Z- e0 \! E, _cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
) J! _/ V$ j% Y1 v: V2.启用OWASP规则:
& R' I/ k* t0 p: f: \$ e, Y; Q' Y/ }  a( Y# @5 @$ r
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% S* V9 ^6 w3 Q( {- k. A
# C. @* W- u* [" X" u( O" n3 Y
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on6 Y7 r2 j" t0 @/ F8 e* Y: P+ w1 M

( d6 n  K1 a* J3 m/ H. oowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。- p& g9 ~7 m  h0 u- d4 B$ p
9 K; n/ z6 q% z0 B& |: \
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf; D6 e6 {* k% c+ y; a, ~) D0 G1 `' K
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf2 t1 [5 r6 C) W
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
, u2 ^4 N. Y5 v) MInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf" u8 r; K1 w7 F* f5 Z# e: m7 ^
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
6 e, C8 f/ P' |0 O2 j- EInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
$ s8 K& T! {+ {  @( o9 ]Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf8 E/ x" w9 Z) b& b' r3 l
五.配置nginx
# G, U* }' ?( }* A& ~, v0 k1 T3 O6 h$ J9 F5 R% Q% u6 o4 o
在需要启用modsecurity的主机的location下面加入下面两行即可:: @' J9 v; N1 o9 d  t+ j' J

4 r+ K' R4 }& }ModSecurityEnabled on;  
  [$ _) |4 ?) `" AModSecurityConfig modsecurity.conf;
6 }' d, H6 u, O下面是两个示例配置,php虚拟主机:, b: c% ^7 R. y. B8 j) o
; ]" ^/ V+ R) m; s+ V' h2 r) R
server {- Y" t. Z% G# U6 K1 p+ a: F
      listen      80;
* t$ [2 E+ n0 D      server_name 52os.net www.52os.net;
' E, J# @6 m& r3 g' D     
5 v  p) Z+ z/ n  o6 {; j+ i      location ~ \.php$ {5 j7 p5 Q; p( s
      ModSecurityEnabled on;  
* }- j/ o. G" d3 B) z      ModSecurityConfig modsecurity.conf;
$ |5 I% T4 x- t+ r! j+ q% h: |) p" n0 ]; @- p  R; z3 o
      root /web/wordpress;3 Q7 v+ Q: @' n9 N& R# j9 v
      index index.php index.html index.htm;
, Y1 A9 e$ T# ~: ~! M6 K  # M) G/ y# n, O
      fastcgi_pass   127.0.0.1:9000;: H- Q( @! J( ?4 ?
      fastcgi_index  index.php;
3 H( A; U; [8 Q$ v( c      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;. Q/ k, i% `# ~
      include        fastcgi_params;# E& e0 a; I* l  q8 J# q7 |5 k
      }
! ^. O9 b+ ?0 d, ~$ l  }0 S" `  E, `1 J, S
upstream负载均衡:
. P* J. |9 L: ?' K. [/ a, E. A; ~7 n9 B3 T
upstream 52os.net {
' R6 D; o; S  T0 o" Q# n    server 192.168.1.100:8080;
& n/ Z8 Z& T1 y1 D/ H2 P. }    server 192.168.1.101:8080 backup;  j2 O7 D2 N7 Y' j# e0 s! n
}- m# u0 V# H% z% b
1 w& G1 O& L( O' a8 @) X
server {
6 I' f3 r6 U0 v; b# |  \listen 80;3 `8 @) v' v. F$ W* r$ J
server_name 52os.net www.52os.net;
5 b% X& P0 U1 w4 A% r$ l0 n9 I: u2 o) ^
location / {
4 P, w! G- K7 Y- H' z1 H5 J    ModSecurityEnabled on;  
1 \4 B+ \9 q3 r    ModSecurityConfig modsecurity.conf;  
- ~5 q5 @* f  I0 c- l0 Z  X9 I$ v# r' N5 Q5 m/ u% l
        proxy_pass http://online;
: o& s  Z3 G/ K  G( H% s( o  [1 t2 |        proxy_redirect         off;
& O) t* X  m, v. T1 S+ q# S        proxy_set_header Host $host;
  l' K5 z: F2 C2 s  m# C        proxy_set_header X-Real-IP $remote_addr;+ L2 N( u" n% d6 x% P
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
2 R- _" ]6 F% y: o2 S' N( x  a    }) V9 r0 i8 e8 |: i- H
}9 P; F1 l  Z1 p" _& V; c- m% V/ s
六.测试3 w; z( u9 j2 W6 Y# C+ d! _: P; d8 m( V

5 s, u9 j; d8 ]1 N. |% ^% a6 h我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:. N9 f) e  @; n7 @, W& ^: S5 j

8 l! a3 w3 L' s! ^  o3 u<?php
( h- p5 d3 x! o5 T    phpinfo();    7 t. S8 F% p# E) K2 H. V
?>
& A7 p' k# _7 _0 b! U: Q- ~在浏览器中访问:& Q# a, Q3 A1 w! O

8 K9 a7 ?1 ?' T6 M$ Whttp://www.52os.net/phpinfo.php?id=1 正常显示。) w3 T1 S4 ], a: e9 `9 P" _
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
" e9 f- w0 K- w2 _, _http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
* g; E% k: h' e: E2 E7 {6 P* S说明sql注入和xss已经被过滤了
& q9 d0 m. }9 B( m& l* I5 F. H2 H: C, v* j$ G* C; p
七、安装过程中排错* y. y5 l- ^% t9 [/ n$ G7 k8 E  X+ o
7 e% }0 h: [; ^: `) n2 r$ A6 h
1.缺少APXS会报错
" c3 S( n8 n) @" w4 l5 d7 \% K; P/ `# c* c
configure: looking for Apache module support via DSO through APXS
' j% [0 |/ J, D+ H! b8 o& aconfigure: error: couldn't find APXS
4 J  d" Q4 g4 S- r9 `  r3 `apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
. `( c7 _1 c' k5 I2 }* h+ |解决方法:
% k0 ]: x) b% K2 \3 m" ^
' x7 |. f% J+ W. G' R& A# y! {8 |, lyum install httpd-devel4 V; L+ J9 ?" n. x' ~4 J; h
2.没有pcre! y* r/ f' [: |# g# o
# v$ p" `; Z0 j
configure: *** pcre library not found.  F2 x4 V$ m: k; q2 t
configure: error: pcre library is required$ K2 X* @7 N# Q' E7 a0 ~
解决方法:& }, a" N+ o' z# O4 y. L+ o
# b/ i* ~2 H9 M1 Y3 e8 O
yum install pcre pcre-devel
, g9 Z1 }% }5 o& Q, c3.没有libxml23 b% o" s( T. s0 }( c) V2 a

( ]  M. V! K3 [- P4 U; H1 ]' p5 O" I- q3 t/ H0 h& J
configure: *** xml library not found.
& J* u0 n$ ]% ^5 v! {configure: error: libxml2 is required$ Q& w7 p* v1 Z* s  L/ b: B/ N; K
解决方法:
) D' m% C' ~$ `7 V" [% M- K6 Q/ \' w& b) w2 g9 r% \* S% ?, I( y
yum install  libxml2 libxml2-devel
; c* @! D% k% I/ ^$ c4.执行 /opt/tengine/sbin/nginx -m 时有警告* O) q1 j0 V! e1 ?% U
& t7 s  ]) W8 ]; O6 w' M% k' t  T
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
3 J. L1 [8 ?4 c0 Bnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
  m+ I4 R! z+ g/ B/ U( A原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
6 A( a+ j; j* N# }2 A! \% a" k' G4 y+ ^3 N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
6 A% h+ k& w/ o9 \+ M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"3 o8 X; Z2 m0 f+ `1 j
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
: k3 ?. F  z2 D( E  p2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
# `* o$ F" b7 g- A2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
/ ], a7 p/ O; j, f1 I7 R+ X2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
6 o# b: S3 c6 O4 ^3 E解决方法,移除低版本的APR (1.3.9)
0 c& b" \1 L; s5 M+ ^1 `( I" T& y: R1 d* i9 |
yum remove apr# c6 e# n, {( w: \3 E; |# @
5.Error.log中有: Audit log: Failed to lock global mutex5 f" `4 O6 m4 a' Y
9 R+ N$ N: n/ _' f/ K# i
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
: d$ [& ~( e2 l; p8 }global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ I! Y; E0 T' ]) Y" T  [解决方法:
% G& f8 r; R- F( L# j- b; C编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:# T* M& g' O' Q1 Q# f7 \% x9 |. U' t

: l( [' ~1 J1 e, GSecAuditLogDirMode 07774 P' s" k* k# n: G0 _" S; `
SecAuditLogFileMode 05503 q4 B8 P) |4 t9 Y+ O  ^
SecAuditLogStorageDir /var/log/modsecurity& ^8 e) M  G7 }' I; M
SecAuditLogType Concurrent  t: `2 c3 ?, B) F0 p
参考文章:
* t& a" R/ E7 v1 L( s! ghttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX: E' I! a5 r3 W+ C  t
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-12-27 06:32 , Processed in 0.038991 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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