找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10757|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
8 t2 ^8 k: ~4 C: s! }8 ~; Z) a* y
3 _* a) m- c# T1 y. c, O8 Z, G一.准备工作0 R2 N8 `% ?. t1 _& T6 V/ O

' j! u! v" S8 G1 G5 {0 ^系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
5 C9 `! ^+ X& {9 {6 L4 `/ k, n9 \! q: Q* m
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
3 N9 d: X, y: h9 N$ o. A/ D0 Z7 t" ]1 k+ b
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz, S6 ~% |, G0 A& Y% V

3 K- l5 u8 p( Y1 ~OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs# Y% a, K, F% C& d

2 p. L  L4 t+ ~- _! d依赖关系:2 e! n( Q0 r' U, ~. g
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:2 p! U' ?- o' `1 m8 \" K; U7 s
  d3 w0 D6 l6 h. g
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel" Y. Z% _4 _) A: x3 ]6 k
modsecurty依赖的包:pcre httpd-devel libxml2 apr
9 Z% `9 o" q# L
' A$ w& I2 j. j/ l! a# Pyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel, D; U1 X9 K3 }3 C0 p4 ^
二.启用standalone模块并编译. D7 r2 L6 p! t4 H

4 c) V' Y  V5 u& C7 p! E4 j下载modsecurity for nginx 解压,进入解压后目录执行:  e8 H' C  X' p& M
/ z+ S4 w6 q8 E6 P
./autogen.sh
& X: t1 U$ C* K; V2 E7 H) P./configure --enable-standalone-module --disable-mlogc! \. c; a; l: r! s4 J2 k$ P
make 3 j1 ?; h. V- V
三.nginx添加modsecurity模块) e) s. P* I' b
0 L! X. L+ H: E: ~7 N' G
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:- Z+ M7 T3 f8 w* _+ i8 E, l+ c: y6 Z

  A5 Y6 C% I- X6 h4 u; D1 R0 n./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
) g5 X( Z. P* X8 U* Ymake && make install2 a4 H8 c$ I! n/ u% ]4 {, g
四.添加规则: U# b/ o7 r1 N9 P/ E0 T8 O# v  C

: n, N( r) R% emodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。( V3 h( i6 y1 H: `  Z: ?$ H
  ~* |( u$ J1 S
1.下载OWASP规则:
0 }1 ^4 |9 k# o# I) m
; o) R4 R8 t7 e7 A. e* O. ygit clone https://github.com/SpiderLabs/owasp-modsecurity-crs2 E" f8 s) d, R- k& }1 D8 h

/ v+ s, j/ j; Y- @$ k/ G2 Hmv owasp-modsecurity-crs /opt/tengine/conf/
2 g/ m$ s: i8 r0 ~9 E
0 C) H+ o: Q& O* z* gcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
4 ]$ j" @1 T+ K6 H* N  W2.启用OWASP规则:
6 H$ U8 K% n6 }* q2 @6 y# a7 d. C/ b8 g
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。0 x( G4 F& [! w6 J% `: k" n5 a

" a5 m1 X5 Q: S! b编辑modsecurity.conf 文件,将SecRuleEngine设置为 on  w( t4 s8 @' m8 {4 l3 v

( {3 G* u! ]6 n0 Howasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; r6 }/ }6 h2 [3 L- Q& Y* R- |3 L

6 Q- J  E) I+ \9 T( hInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf2 v! U8 Y$ ]) `6 T6 J' t
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf4 K$ _/ |+ Y' `' f  q8 \, Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
5 f3 h  ^: e5 A8 E: s0 ^: n( vInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf( R3 Y) v6 U) k' W/ P+ P/ F8 F7 ^
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
# d2 u8 g2 \: f7 lInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf' E8 ?1 {! ?9 \
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf( m2 h9 c3 ~9 x% e+ K5 S
五.配置nginx
4 M/ `" R" @, e, [1 k3 {, K2 R1 m3 Q0 Z+ ~
在需要启用modsecurity的主机的location下面加入下面两行即可:
- X3 v& T0 _7 L0 V( r7 R+ ~& i* D& \, t. u5 D# E
ModSecurityEnabled on;  . @$ K  k6 E3 v; @* O
ModSecurityConfig modsecurity.conf;
* y6 f6 a0 h$ l8 e下面是两个示例配置,php虚拟主机:+ I0 c5 A8 p% H7 f4 s( }

& r- i; c& J$ ~6 L& X' cserver {7 ^  b$ D* P1 [- m/ x0 w8 h
      listen      80;' B6 \7 R% t9 M1 H) ^! y
      server_name 52os.net www.52os.net;
8 |+ p* m+ P* \* h6 m     6 U: Z, {3 m, l2 b; y
      location ~ \.php$ {
4 F$ d" i* @( `      ModSecurityEnabled on;  
, M8 H; ]) V9 n& v6 U; |      ModSecurityConfig modsecurity.conf;  C4 s0 F3 D* `$ {. P% C

% c. s8 A/ Z; I, C      root /web/wordpress;
- F) c6 q- N* ?, v      index index.php index.html index.htm;7 ^- g  r, p8 `) z" Q3 ^2 l7 d
  ! z0 h; x6 B5 H. f$ }
      fastcgi_pass   127.0.0.1:9000;
6 W" H8 M) `# l' V      fastcgi_index  index.php;
6 H- X& `) H* v  a. k. N" F! m      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
9 P# u: O; m0 |9 \6 I      include        fastcgi_params;; p8 m# _' P" w+ }
      }7 p" n; n6 S$ B5 f. J2 I; ?
  }" D5 O6 m  j/ }7 J( f6 m2 G4 D
upstream负载均衡:8 m* i* J! C) s% r4 \

8 d, j* p$ Q+ R6 v* @$ eupstream 52os.net {! z8 [! u) W- B8 I
    server 192.168.1.100:8080;5 S' k& j% F' R$ C% ?! D  ^, C
    server 192.168.1.101:8080 backup;* k  s7 j, @8 `. H
}/ [- G" Z  O# N6 C' q, Y
  P; A- A) Q. B+ S8 e4 D
server {
6 {! C" J& U! b+ s3 z+ D! _1 V: Hlisten 80;& A( m) s% R/ }# i" d: ]
server_name 52os.net www.52os.net;5 b* `8 v) H1 M4 N% y5 j/ x7 }# o9 Y

# r" {" `* k. y" o7 flocation / {! s6 p, d! s- u- m7 V- e6 X# d: C
    ModSecurityEnabled on;    K, S! [8 B  \/ V
    ModSecurityConfig modsecurity.conf;  
3 i3 e' i! i7 ?# N" ^4 M! J8 W+ I6 {: ?, L* n
        proxy_pass http://online;
8 l! p* W8 y8 C9 j' h        proxy_redirect         off;
' p+ c) _  o$ U- ~0 H        proxy_set_header Host $host;. ]* A& m+ ?# g" R& W) X# k/ T
        proxy_set_header X-Real-IP $remote_addr;
* [. v4 ^. g8 j% H        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
" o$ J% M& u# B. h! {" @5 [    }: y0 z6 [7 q' {0 a
}6 H9 g- a* X5 T& E/ N) w- ]9 N
六.测试
. K; e7 E" K. g  A! r7 ?: ?/ j" t4 b  Z& x7 q
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:3 A! _' Q% Q. s! Q: D
5 q# j* {4 Q4 p5 L
<?php: M7 ^1 x2 `* r! U9 ~0 e9 u  W
    phpinfo();   
% Q& ]" G( X1 l: e6 q2 ^, X  z+ }2 G6 s?>
3 }7 z4 }& k0 F, e. D- G) R5 x在浏览器中访问:0 x3 q% Q! E. P3 i
: a9 N. v5 H* R7 s+ J0 v! V0 j
http://www.52os.net/phpinfo.php?id=1 正常显示。- v+ [5 A' G8 N1 v
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
" y# z& Y: A/ @3 N, zhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
3 s/ l% P: H: a7 W  \说明sql注入和xss已经被过滤了
" c0 D  S3 |- @0 I. s; o
' U5 I4 y, Q7 r6 V$ J七、安装过程中排错
1 H  e  m  V2 q/ O! }# k" @4 g* \' p) i% u& f2 q( t* C9 {
1.缺少APXS会报错- `+ s* u- ?# l1 y' `

. D. b3 K% Q, c9 {configure: looking for Apache module support via DSO through APXS% g# i6 \; _' ?/ ?0 u% r) y' T% W+ @
configure: error: couldn't find APXS
7 {, l. ?; x1 e8 Iapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。  c0 G5 Z* O# c
解决方法:. Q2 l5 g$ G) |# x, H
1 a& k: I3 r7 ?0 r
yum install httpd-devel
; J% J% D6 n/ P1 M' t2 O2.没有pcre
( l1 c7 l9 G* N* h" {
: K( P6 Y. d. Lconfigure: *** pcre library not found.- a1 h$ M! a( v# x7 j) t8 L
configure: error: pcre library is required- Q% {2 t. u/ ]) W8 Z* y3 ^
解决方法:
& M6 {8 d- g  F9 D4 q  W# T& [
# [3 a- B7 f: f% Ayum install pcre pcre-devel0 g+ [: E1 O) G& B# ^4 l; w
3.没有libxml2) X0 u% D% R) {/ ^3 Z6 U5 S% A6 Z
; j; n: X$ M# ?8 H+ Z9 k: X

: B: U- K! {( e6 d# Sconfigure: *** xml library not found./ M- z" o' ~. d: l. y
configure: error: libxml2 is required
, K+ k" J3 `) E4 p/ q9 O* U/ j. Q2 @解决方法:5 Z0 w+ v( b9 l: P) A, k: y4 ^  \$ H
' ~; l7 a8 v  N! E9 Z
yum install  libxml2 libxml2-devel
2 X  u: s, l) r3 s( F0 }4 r6 X4.执行 /opt/tengine/sbin/nginx -m 时有警告
# b% m& D, P8 L4 o" d* W4 Y1 ]# Y& {3 I. _/ `
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
0 p0 I' F0 M- m! l) _) E# \' {# Xnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
1 U- n7 [6 X/ `* l2 ^2 E$ l+ j* z原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log& n4 S; V: E3 M, n* K0 ^
& e0 y3 X7 h7 A3 n, I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
; {+ H; T% }. G1 N" w8 B2 w2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"+ }7 q$ b$ n/ I0 S, S' d0 I
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
+ J, z4 c* Y* [+ k( W6 Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05") c# K7 M/ [/ {. \
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
/ B  m# C* g/ ]$ }2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
  A- Z7 s. S3 b) ^5 s解决方法,移除低版本的APR (1.3.9)
) I) u7 s6 A& X9 H3 ]( {! t* X) j
yum remove apr- p' |$ C7 z% C: G
5.Error.log中有: Audit log: Failed to lock global mutex
7 P" ]3 w0 `% n; s. n3 B% ]7 M" |/ K3 E7 n' `; G
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
( B, h1 I  L" _0 a9 H( H) Q; jglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]+ J1 }( a  N' Z. x: [) s
解决方法:3 T4 K: R& i) ?* p; [. @
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:/ T/ V3 B/ x9 @+ G0 f- N
; y* ?  x0 ?8 s$ Q0 b
SecAuditLogDirMode 07775 D; \' F; R8 A# A3 }, q
SecAuditLogFileMode 0550
; z4 o2 H9 C( p# ~+ ?2 Z8 z9 K" {3 g: fSecAuditLogStorageDir /var/log/modsecurity
$ p- U% i" W! DSecAuditLogType Concurrent
: C% V) g& |8 }8 e5 `9 O# U* y. `参考文章:& P( R5 q( y2 `; [* ^
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
* x1 d. y  F( c1 I* t$ U8 j/ ^, @http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-4-24 21:29 , Processed in 0.068767 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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