找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12643|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
' A5 [1 r- \6 Z2 N
. _) h% {/ `% ^; h8 d* Q  S一.准备工作7 C2 f% h' s4 g- e+ |
2 Z& b& b6 z. y7 _( m$ i
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0- h' x$ |. ?. I* r, s

" @2 V. [& z7 H, s/ ^tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
  r2 R9 p/ T2 A7 a) G6 l" B. D7 j2 h" U3 Z
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz% y4 e! E) J( H, M# B

% }3 }4 c- F/ ]7 l7 `, e" HOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs6 ]) ^, t+ \+ ]

7 X! [8 m7 y- z# k3 [8 o; f依赖关系:% B) e+ \0 ^% e1 H/ R0 ^0 M
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:/ O. d, h* j) _' D9 U0 z

# |6 {8 ]' B7 [& H  L5 G+ [yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
8 c9 \4 a# b" e# d6 r7 r8 k6 nmodsecurty依赖的包:pcre httpd-devel libxml2 apr
2 }. B' i5 J- k; c; ?! C" T& K- A* B! c
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
' t- c. V( T  M- F二.启用standalone模块并编译
  o  m* d% h" o
0 U5 ]' m/ L- ]) D# j8 x下载modsecurity for nginx 解压,进入解压后目录执行:+ R% r# t5 a; I8 u1 n4 I

8 _( f/ D; Y  u5 b./autogen.sh% d8 P/ r# E5 x) z5 @' y
./configure --enable-standalone-module --disable-mlogc+ v9 c6 O7 a' D$ F' @6 {
make
& d  _' E; m$ K  P/ Q! e0 W2 q三.nginx添加modsecurity模块# x7 X1 D4 V9 G8 O) h& r5 L5 w
$ V3 k2 V% n- v) j
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:' m9 y) ~: j" Y9 `8 k  i
  |. g0 r9 n4 G, m; ]/ F! P; O3 o
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine, i7 b% }7 `  \6 a) Q3 z7 g9 y0 g: y
make && make install
8 {2 y0 \  y! X四.添加规则3 V& V) n9 U: `- ?' c& D
) s! p7 X# f" f( C
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。* j3 l0 ?2 K( N& c1 S: r4 G6 `( o5 h7 c" f

0 g0 _- y; i1 Y+ r/ w% h0 s( }1.下载OWASP规则:3 ]; W) ^" {( g$ T
( J, O1 m- i1 H
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
0 L* C$ |/ X2 b& t
3 q: T$ V* Z( fmv owasp-modsecurity-crs /opt/tengine/conf/# s/ ]6 F% s6 e$ M
+ K8 o* V. e- a, ^" e( r6 K- M
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
: [) j1 F2 z0 ~2.启用OWASP规则:) S% w% ~9 X5 G% s1 ~
% ?% N8 y" Y/ f1 o3 p* C
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
1 C4 S' o, j/ ^+ E; Q) ~2 x& y# A' ~& Z% J0 p, K) c3 [
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
. r" |9 Y% }3 M5 c8 Y' V9 I# F) c& Y  q* A9 w+ k
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 j! T0 c9 [* U) H! K; k
3 L7 s3 d( ?% t2 y
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
( z# x/ M. H/ {: [' Y$ _7 C' BInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf1 a& V- M! E# `; \+ N
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
4 M6 i3 g) D: ?4 |# i+ @Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 H  e$ |& m$ A3 T+ F; jInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 q: M6 a6 M) u$ k# f  tInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
9 J! F! |/ g0 e- e# hInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 [. n  L/ g6 ?, R% ?) d' ^& g五.配置nginx
1 H4 x5 X; Q+ d8 E
8 E5 K% v) L) `+ ~+ F) B在需要启用modsecurity的主机的location下面加入下面两行即可:
  a2 L8 o5 a! L! ]+ _
$ U2 F1 `3 E' Y$ R3 d6 C. ]+ HModSecurityEnabled on;  
( U9 k1 d- {+ i' T: ]ModSecurityConfig modsecurity.conf;
4 ?- [% T& c  L0 S$ Q下面是两个示例配置,php虚拟主机:' Q. ^2 `! |& H) g+ C* k

+ X: ?# \* e" @server {
8 |6 `3 X+ R  Q2 I* P      listen      80;
  x+ y% {0 t+ ^4 `2 l      server_name 52os.net www.52os.net;6 ?8 r8 g  Y, ?
     , I+ p9 E  q1 s3 K/ _! {4 u. x
      location ~ \.php$ {
- r4 U; p, ?5 ^6 ~9 q/ Y8 b      ModSecurityEnabled on;  
0 f$ O! o$ F4 i/ y0 n9 E3 g      ModSecurityConfig modsecurity.conf;6 l/ X! _$ d. a" I9 c) [

% W- h1 D) O% i7 k; U* E* V; m      root /web/wordpress;2 ^& B" x. i1 l3 _9 V
      index index.php index.html index.htm;2 m# G- G% x( t3 G& K) `, y3 m
  
5 S% A0 T$ A' Y6 ~$ [      fastcgi_pass   127.0.0.1:9000;
! Y5 n$ H  h* ?' ^% o' _      fastcgi_index  index.php;& P" t. q! ^4 x) P
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;; ^3 y1 j& n( P/ G$ S3 c# U6 F
      include        fastcgi_params;2 T" @5 {8 j% l, f
      }
' v( }1 M/ ]) t6 K' o( N2 g  }/ ~) t5 y0 T5 d
upstream负载均衡:, o& R- U$ r/ d: d+ N) i
! X9 n5 t7 D; t7 J9 }
upstream 52os.net {
6 W  m0 F2 j  ^    server 192.168.1.100:8080;* E+ M8 {/ }1 S" R
    server 192.168.1.101:8080 backup;
, F& X7 |3 \; ^$ B4 `}9 @/ |) Y* ]# L: i4 j% e3 t
6 |0 {" T  q4 D: m1 ^
server {
( _  e! |# ]$ n6 p2 klisten 80;
5 W7 v3 {8 m9 {' T6 ~$ K9 [% Xserver_name 52os.net www.52os.net;2 `) q2 e; q2 X  M* ?, B: Q

) \3 V0 J% `3 K5 v5 \/ |/ i( q, Ilocation / {
7 ~$ D- }& U4 B; K    ModSecurityEnabled on;  
, E9 k$ u9 v' }8 r9 ?: ?1 T    ModSecurityConfig modsecurity.conf;  
! b, p& }) a( e3 s6 e- L: J2 i9 y4 {
        proxy_pass http://online;% h0 ?5 [" T+ }4 |" a+ z
        proxy_redirect         off;4 ^( F& ^( l5 y9 e
        proxy_set_header Host $host;1 U3 G+ J" r1 _. g
        proxy_set_header X-Real-IP $remote_addr;
- a0 W/ q0 v1 ~. t/ p* d! A: O; S        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
+ T: u$ h; w" C) v5 [9 q% I    }
, J, x1 D" y$ Q- S2 V+ V! W, J6 }}
* b& }+ ^9 U% D7 g六.测试
0 O/ y3 S" r: N2 q7 t+ T
6 X3 `' N" S' O5 C3 w& L* i我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
: m; j) c: m5 h" J2 y$ N" |) r+ N2 T( N4 \0 A. p9 ]2 c1 s
<?php1 T% ?3 s1 u. `) q5 L# {# V% h! K
    phpinfo();   
. b) ^: C. W/ X) h/ Y) a3 G! p?>
7 X0 p0 K8 c" Q6 |在浏览器中访问:9 T7 [% o* z7 r, j* L
& v/ U! u% B( ]
http://www.52os.net/phpinfo.php?id=1 正常显示。
' }4 Q0 ]( v, ~. ]# d0 ]9 ehttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
6 b) |6 V1 i' O( X4 o! K) `1 nhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
4 m& U( l& }- f/ w: W  g3 A说明sql注入和xss已经被过滤了
0 U/ j5 Z/ M, M& B( ?2 t9 R& o: M
4 N& G) w+ d8 W- ~9 ]七、安装过程中排错' p! D3 N& j2 g% p1 ~. c% N% W9 Z
; I$ H7 ^; b# {) U
1.缺少APXS会报错
9 r# s  m, C7 g6 C1 {5 s8 n+ }3 J7 k: x. H$ h0 K( f2 P1 Y
configure: looking for Apache module support via DSO through APXS
8 W/ ~# p* \' |configure: error: couldn't find APXS3 Q/ R7 N; O- h) u1 S' q  D
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
6 `8 ~% A3 h8 k! ~1 ?解决方法:2 U5 g( b) i1 z( {

; Q4 f3 w# V. M0 B. Q7 pyum install httpd-devel
0 I* K3 V( t: R+ h" {$ [2.没有pcre
& w* x- p, J) n; o) l1 j0 z- \+ d
9 s. g' f) N! D) j- [0 |! Sconfigure: *** pcre library not found.0 p5 ?8 ?0 R3 [
configure: error: pcre library is required$ w" K9 c: |1 g9 @. Z
解决方法:
$ M% I; w( y+ H" V: Q+ H/ x; Q
" z' A- x5 [/ r5 ^yum install pcre pcre-devel% w5 z) r  _6 X
3.没有libxml2
3 J( t( S0 Y4 G; d2 G% N" \  F3 a; ~: c# I' R1 P  Z3 L, C
7 H1 J& j' j  R% x' T- c1 f% v4 J
configure: *** xml library not found.
& N8 b- J2 Q5 F; Zconfigure: error: libxml2 is required" _; O8 ~9 h) L# }$ j
解决方法:4 l$ O4 \  \* d" }# G

$ T9 c. g% ~4 w3 Z/ j) Y: O$ tyum install  libxml2 libxml2-devel
, H) F5 T! ~6 e; C) }4.执行 /opt/tengine/sbin/nginx -m 时有警告/ O" E' ~' e5 i5 r; h

  j; I0 ^! I7 O" S+ u  c4 yTengine version: Tengine/2.1.0 (nginx/1.6.2)2 K4 @8 ~" d! U: h
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!0 _9 p& Y( g& Z- G
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
( u( j( u$ A" c; t. Y/ d( Z5 t% l- V& x& ^( e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
. ]. @  C7 o, M3 d( L4 F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"5 _1 z5 Y; D* D2 {. R* _) g
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!. ]2 [& D* d' o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05") [+ l) S0 h4 H! \8 p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
, T7 H8 y) \3 R& M2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
# o- ?8 c" U: v) V解决方法,移除低版本的APR (1.3.9)4 A  m& i  }% ^; M

( E/ ^9 i' F9 {2 g0 _6 N5 b! pyum remove apr
$ [+ e* `4 j/ C+ x/ f% z4 v2 ^5.Error.log中有: Audit log: Failed to lock global mutex
% f2 Q% c# I( q3 m$ B6 K- p$ w' k4 W3 {( J: @
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     # G7 m7 B; B5 L! G5 W# h1 X8 K& M
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
1 b) F+ o8 Z; h3 y: R- m- v解决方法:
5 o- `! e' h2 B# e' [1 l2 \编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
7 b- X! Y; E) T) {2 a$ d1 I$ M: }* l3 u/ p% R" `
SecAuditLogDirMode 0777( i2 Q6 i! J. [
SecAuditLogFileMode 05509 v5 E6 M) Z. @
SecAuditLogStorageDir /var/log/modsecurity' ?* A/ ~  x  n  V, ~
SecAuditLogType Concurrent
( l% a$ S$ O  M7 L" x参考文章:) T# F8 D& M9 b# Z: j
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX- m- F/ h' j: o* i6 T: L" T
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-25 17:29 , Processed in 0.069725 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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