找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12401|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
( a+ M- b& _; B( T! B  D# K
5 b* r9 j1 m- p9 X一.准备工作
( h+ I( D6 X/ e8 Y# L9 A# D3 j% m- w4 Z, k6 v$ N
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0, a# g  m4 P4 j% w( J

+ E6 G; b" i1 Y6 E+ C( l8 f* ktengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
0 X" W, W' `6 H& A( ]; ~9 Q" A$ I& Q* J8 [) _5 R& B! |
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
' m. R3 p! g: b  ]8 h. k- a5 N( M5 ~7 z  V
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs' c$ Z; q( j- r$ h/ q# K' a

' p! `2 S/ C) R2 e依赖关系:
' }# ~' T- M. U+ J8 y8 j& J2 Ztengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:; O8 f6 E( i/ R8 Y* A  L$ a

& J+ d9 v/ `& [4 ^yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel, h' j# ]$ ]. y* {; }
modsecurty依赖的包:pcre httpd-devel libxml2 apr, X% l$ E* R8 b: F5 p4 k$ F  [
& D3 X" h$ W& w1 l: H% O' o2 L6 r
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
$ O  R) E0 B7 g1 ]3 i! r& y- ?二.启用standalone模块并编译
0 B( M4 V& ~! z2 p8 l$ r
1 D7 d$ `& l! R$ A8 K下载modsecurity for nginx 解压,进入解压后目录执行:
/ u- s( p# h1 y0 O. H  S. T, T
" r! n9 j' d" Y# X- Z" E2 i./autogen.sh# n2 z2 ^( l$ O9 {; O) A1 s' C9 L
./configure --enable-standalone-module --disable-mlogc4 q7 J$ F: S# D/ F
make 7 Y# {/ L; K  V+ {
三.nginx添加modsecurity模块
* P5 R3 h" Y& J7 r: i5 o# r1 C
! F: [8 q, n) w' O" w2 I+ a* L5 S在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:/ j& X) V, Y0 v+ v" y& M  m

8 {1 T: f+ t6 @2 p./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine6 _4 |* u/ t9 n3 G, k: J
make && make install
( c& }! ?$ K) H$ d四.添加规则
" ^# ]0 g+ M0 B0 E$ E, L: h
: \/ c9 ~5 \3 n  z% z. `modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。2 V8 v/ H" v2 d, t4 o1 }
3 ]$ v3 u- a/ Y; K
1.下载OWASP规则:
( l& n7 b+ V8 K5 c" p0 J
4 A$ Q5 y0 C4 Agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs6 m  |, w# y1 s0 P

8 q/ z2 I' i0 a% g- W  [mv owasp-modsecurity-crs /opt/tengine/conf/- L" _5 Y, H+ \0 i% u. O7 l
& V. p2 z4 _( ~3 [
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf# P: J6 A( \) t' \# v5 z0 n
2.启用OWASP规则:
# d6 b2 H/ @! e8 J( ^, ?! \7 ]/ |2 \0 j4 `0 u) b0 A  z" d. T- C
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& ~7 y* G, T+ s- M
3 M3 }4 ?+ M+ m3 w1 D( V
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
" A6 z: N, s- q% _! S4 _3 z) E# j2 v: K
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。0 Z; N7 o. @' h6 y9 ^4 K- J3 J" W

9 t; k) R) l4 J# w! w# b2 ^& E9 ZInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf% {: l8 m  l+ }
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
" T) O9 {/ ]' q" DInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf0 y" Y  ?: m" ^- t8 h, w
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf+ \# U5 T, o& H$ X9 k7 a# d+ F
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
- E8 h; f' A" `9 HInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
) S  g- i3 c+ s( ]Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
7 p" d+ E2 R. Z9 X1 r五.配置nginx* O8 A1 T: U7 c% }3 V( f5 f. M# S6 W' s
' `# X2 m+ j" U( p
在需要启用modsecurity的主机的location下面加入下面两行即可:6 h4 D4 c/ X& v$ m7 i

4 @5 @4 m" ~# a7 z  U7 |ModSecurityEnabled on;    W0 R, u! P6 C% ^! |
ModSecurityConfig modsecurity.conf;1 y( H5 N7 p8 V  q! O; F" r4 i
下面是两个示例配置,php虚拟主机:. @- E6 V$ H$ D
% C4 d/ E- D- d9 j& d" U
server {$ a# h! O# y! R- A
      listen      80;" h; K4 }  E, P) |7 o
      server_name 52os.net www.52os.net;
' J6 ~# R4 s8 q# ?4 T     
- _6 n, j7 `: y5 `5 C* E      location ~ \.php$ {# h  s+ b4 x/ v- D" c
      ModSecurityEnabled on;  ! W8 w! g0 c$ U) A, i, V
      ModSecurityConfig modsecurity.conf;9 b2 k; a3 V3 z* W
& h, k7 ^: w- g' U, q7 g
      root /web/wordpress;
! R. ?/ k! M& }: P      index index.php index.html index.htm;
3 ~& z7 g- w0 Z/ c( N: P: ^" ?: {6 o6 c7 J  
1 B; e2 q2 e7 i* l      fastcgi_pass   127.0.0.1:9000;1 k$ H4 k1 {- V* q0 C
      fastcgi_index  index.php;
; h) _8 b7 f* x      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
, E" b: C# {! V: u  j4 a% a      include        fastcgi_params;
2 a+ }9 V5 B  ]# @      }/ H5 G) N: T5 d- ^/ U5 c1 h
  }
# T3 _4 v: `& d5 e/ s% @upstream负载均衡:
) b) v* d# @1 t1 g% K$ ~) ^1 w& J  ]8 G$ a! i$ m0 m/ l
upstream 52os.net {
# |1 s# _7 c2 ]    server 192.168.1.100:8080;- ]& v! Y) f/ s; z& l/ T- [
    server 192.168.1.101:8080 backup;5 @# z" q1 H$ k( a: E
}1 j: @8 s8 {/ h
; A* \7 E4 J2 H- O& F- F9 k
server {
: l1 N: e  W% T$ [$ u! M: clisten 80;
( Y4 s+ ^' S8 P8 G# x( t3 Cserver_name 52os.net www.52os.net;
& I( Z9 y& y' B4 {& g& h+ g9 [$ p" P2 r
location / {' }- }# F1 D/ x0 p) l5 E/ T% y5 {' d
    ModSecurityEnabled on;  5 }2 ~1 W% D6 ~& S& ?
    ModSecurityConfig modsecurity.conf;  
* U2 m; {8 F8 D/ x# H' N" ~2 l, r6 m! E/ S; i% ~4 D* B
        proxy_pass http://online;( @6 E4 f3 \: k& c! c( E$ M
        proxy_redirect         off;
$ U+ V0 m6 X5 o& @$ `        proxy_set_header Host $host;
4 C' ~4 v! }5 ~6 F0 H        proxy_set_header X-Real-IP $remote_addr;* `* n6 v9 @% o, c1 O
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;9 I. n+ E/ K# p0 y
    }) D" ^0 @  w+ r/ D9 f4 c  M0 I3 p
}
/ b* N: ^6 ~/ s9 {& z, W2 K六.测试
) o3 T. r( V& h6 l9 Q
4 H7 f) D  A- m0 i6 o# t- V我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:. y8 |/ T2 N) D
2 r; d  F& P( `% O
<?php
' ^+ P$ y& t) O1 E4 ^8 k    phpinfo();   
% i, D: [$ f& p3 b8 Z?>
. l" `5 _: H) v- z% {在浏览器中访问:  [9 g. x8 y3 q, h; L1 m

' t: k) ^" O/ J3 z6 A" yhttp://www.52os.net/phpinfo.php?id=1 正常显示。
" l9 M, F- \5 h9 T7 D4 p; P1 ?9 ohttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。4 D5 N: k- ^  T
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。3 R/ f9 }) I% h  g/ @( D
说明sql注入和xss已经被过滤了- m& W8 \4 L/ u4 P/ m

8 e0 z; l3 A( v; `; T  D) |6 h3 B七、安装过程中排错* {& b4 s# c1 A6 m
. W6 q  x& y7 l5 C  ]
1.缺少APXS会报错/ a3 Z. }1 ~' Y; n  V- q4 o
" [, F: g! `; g, t, A4 ~& o2 v
configure: looking for Apache module support via DSO through APXS
: h/ l  k1 U' q# B8 wconfigure: error: couldn't find APXS: M0 w) o: L8 D  i
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
5 w8 D" O6 I/ ^1 j解决方法:; i8 S3 }' J, k% L
, G/ \- G' ^8 I: v
yum install httpd-devel6 f) \% y5 ?; u: _& v- Y
2.没有pcre; o$ |) L: D) {, P/ K
' u7 {$ B# ^$ C% A) \
configure: *** pcre library not found.- F0 o3 R* t) _* V5 {3 `
configure: error: pcre library is required6 D6 J" e1 f: I; ?! Q
解决方法:5 ^2 p) T8 _0 G5 h8 Y
6 n2 |7 c# P" X; Q
yum install pcre pcre-devel
7 A  b) p5 Y# O- v3.没有libxml27 T$ |' L# _; d5 z3 W: {

! M# @8 m# y' B9 d6 n/ L- z" a3 q- |% e. }/ T" u
configure: *** xml library not found.- i- p/ _& I$ Y4 ^% S/ p( y
configure: error: libxml2 is required
/ ?1 w* ?8 u$ k解决方法:
5 e7 V, s4 h0 u- }2 o; L4 q/ S
. u  `  s+ X3 K9 T: N. C: T4 }yum install  libxml2 libxml2-devel
' Z/ [, L8 a- E* b. W4.执行 /opt/tengine/sbin/nginx -m 时有警告
0 j$ f4 z* _/ m$ c: e/ N, D) [4 M6 I4 n8 `% {
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
; c9 S$ s/ y' A" Mnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
8 L: W$ G& P7 O1 [原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
. W0 R- c* F* v6 `/ x$ A- I$ O* l' Y/ ?2 ?, i+ _+ ?' e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured." h% I+ I+ u& Z3 j5 A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"% k. e7 x  j' y& S" i. t- n
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!" ?9 R# a- O- N3 ~' F4 F9 M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
+ x0 ~+ I( o. @, s- s2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
: \3 }; m$ V3 S- G2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# h  C! t4 U$ W7 l) c# E2 U: x
解决方法,移除低版本的APR (1.3.9)
1 }: p+ ]$ ~/ {. r
' p, D$ u0 O9 v5 ~% t. ryum remove apr
6 N- f: E) k$ f' K  h+ h5.Error.log中有: Audit log: Failed to lock global mutex6 y# Z* G  u+ V" ]+ `+ Q

7 n: A" b; Y0 v: D$ n2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     - q9 n4 y# V  a; c- m, V4 P
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]& e- r/ e4 U3 R
解决方法:
/ B  i3 j) D3 x# W! I编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ ?& L' l6 N" O! Y& ]( a
* F1 ?( Y" ^- ^9 }SecAuditLogDirMode 0777
2 G4 w* L, k/ R7 `SecAuditLogFileMode 0550
6 m/ V" M" ^0 mSecAuditLogStorageDir /var/log/modsecurity
  g& Y1 t  l" H! H' b, L, X2 M6 aSecAuditLogType Concurrent. t1 C. ~/ T$ {+ d* v, L/ p! Y
参考文章:4 T% b4 i& u' o# U. K! {* d3 \
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
  U' {* g( p: z2 d3 `7 D% vhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-2 21:40 , Processed in 0.042782 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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