找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10657|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
/ X+ c( H- S3 U+ X1 M" ~6 [3 C. [, p1 P2 Q  l2 ^: D# {; K
一.准备工作
( z( M4 j) n; U, y5 H) y4 o/ n8 E
1 r8 |9 \3 P/ Q' o系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.06 d* e/ ?; R# w( A/ b0 U

- g) ?2 B/ B* D) e4 m6 l2 F. v/ ^tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz! R( o, d, U- V/ F- L9 X$ c4 D

8 }! G( L% _6 ?" g& n. E9 w; Smodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz# o0 A; v0 j4 ?3 _2 n2 f$ R
2 _, `1 K! n3 P) x% r# b# u
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs- ~" u( G0 W* R' g0 Z
& _' c7 Y7 w. w) r$ g, [( O
依赖关系:
# J! V, ^2 E2 Z; m0 s' j$ Ctengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:( e5 Y- Q! Y( b; r) c4 J6 [2 |
! f$ h3 B. U% e9 a& h" R0 Y6 y
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel- Z- t( j* K/ i* F3 m
modsecurty依赖的包:pcre httpd-devel libxml2 apr/ B: a6 z8 G# k1 a' n% Y% J4 b% j

- v) N" v1 ?4 H( p. r( W) Wyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel1 r% e" \7 K& q0 I( c* i8 B
二.启用standalone模块并编译
( d( k5 H6 P$ Y5 ?! j
- |4 w9 t& H: _# ]8 U' y下载modsecurity for nginx 解压,进入解压后目录执行:$ @5 \2 V4 h' M
# @6 ?4 Z" a* {
./autogen.sh
0 P7 K, |2 m' u+ E./configure --enable-standalone-module --disable-mlogc0 }: y- ?& ~% H$ d% J
make & n! ^# u# q7 ~% ^* X; J9 f
三.nginx添加modsecurity模块: Z' s; k; L5 I' d/ I6 q
' M5 e; K7 N' n( h) I/ n: t# q
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:3 {" C2 K# n, L! d2 e( ~

' R4 j5 g2 l6 `9 Y4 ]7 W; w./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
- }( W, d; Y6 z3 {) [make && make install
# P2 k5 O3 F5 z+ _1 x四.添加规则- M: Q' @/ _! k2 W: _: F' N

  I- V, S" s1 q9 u* ~/ v% Xmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。! I. [+ K9 \6 o2 W; n; V6 s* k
% ?" b& m% ]( p# d; |7 J2 {; ]4 C
1.下载OWASP规则:3 q4 ?% s8 R6 \' Z! i* S
, _+ m* U, p% u1 n* \8 h
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs, @/ E' n( Z/ [4 g: y' i7 D
. j/ y' f$ C, [9 L- T0 U; ?
mv owasp-modsecurity-crs /opt/tengine/conf/5 t. p9 D1 H& o) s# u
, x: n  Y) L1 D" i" A* K0 j
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf& ]8 x; U5 o1 S9 E: H: W/ |
2.启用OWASP规则:
4 c( S. p0 F- [: c( Y4 Z6 Y. _, {' p  {( {# n
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
- a1 f& Z3 L/ F8 G7 i; R4 G; Q) l, d0 n% Q6 O# a  S$ M0 T
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on" i. [2 c& m. L' a2 W$ W

8 t7 n4 V0 v2 b2 J$ Q7 z  N4 h$ F+ Vowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。" `* D3 L  u( j+ K9 t
" G. O& I, G$ @" `5 |8 c
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
& U; D% M5 [) t8 a3 f- g9 RInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
( _6 ~& A, M' r. N4 Q/ WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 R! q$ t: i8 N- o; t% vInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
, y0 ?2 ~/ @* T8 _+ d' {* x3 t3 QInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
# y. d# K9 V! t' u4 ^( L6 VInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf* H3 d% P) e: b# W* N
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf% C. e! x! P8 N0 u1 t( n( N
五.配置nginx
, }2 j$ J; P4 }, m( ?1 }! U8 A" h& `2 V) ?- d1 Y
在需要启用modsecurity的主机的location下面加入下面两行即可:
, v2 Y8 a( W5 y! N7 h
' z/ K8 \) m( Q" KModSecurityEnabled on;  , u3 ]" R6 o5 N% i: T
ModSecurityConfig modsecurity.conf;' [8 g, K6 C5 u
下面是两个示例配置,php虚拟主机:& ~/ M0 B. q( z/ j; u

( \6 @4 ?4 q& f3 x' Xserver {
( @  o8 a  H. w3 P' J7 C7 D% J9 o      listen      80;. q: z  U( ?" b/ P: x( t5 n2 H8 G
      server_name 52os.net www.52os.net;3 l1 d- h, Q3 q/ L0 x
     ) S6 p, `" W$ \- Z9 ^
      location ~ \.php$ {
+ Z* O& v9 |, g5 c      ModSecurityEnabled on;  
) i. {: O/ l! H$ Q2 ]3 ]      ModSecurityConfig modsecurity.conf;0 I4 o) E; b6 R8 Z
2 c/ A: [) @6 D5 ^% |" J4 w
      root /web/wordpress;! l/ S6 E! E0 Z- ^& N7 U
      index index.php index.html index.htm;4 W! A& j- ^+ m- R2 ]$ ]! I& C
  
8 ]' H* o/ i/ {8 j& b1 `      fastcgi_pass   127.0.0.1:9000;
4 D, [" Q" w) T7 b, [) ?5 L      fastcgi_index  index.php;
+ H- w9 `- }: N) ?      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;8 S  C) g+ d- ~- U7 z6 s
      include        fastcgi_params;; ^$ S; n, U6 ?! B+ ?
      }
* z4 H( u$ ~+ Y! N  }
# Y! C, f: B9 o( M5 ~upstream负载均衡:. W( g! k& s- j

6 @: R: r! D$ o" o( {5 Gupstream 52os.net {
4 `0 Q$ B0 a4 J    server 192.168.1.100:8080;! ^: B2 [$ t$ `4 X8 L. m( c
    server 192.168.1.101:8080 backup;
4 e4 [( G) G. e) t4 _- p}8 [8 t1 d+ N- k( }9 G1 ]

; k! z0 m$ t, {server {
" ?2 p( X' J8 Q' D5 L9 zlisten 80;
4 P. }( D+ n1 r6 x1 |3 F- k; Userver_name 52os.net www.52os.net;
2 O% f0 u9 E, w7 u( t, ^: g! q! |$ k" x" m0 [6 R: ~
location / {3 s: Z! S' P+ O7 w( q8 q) f
    ModSecurityEnabled on;  
5 Q8 _5 C* B- A. U* f2 x    ModSecurityConfig modsecurity.conf;  
5 u0 Q6 d* F- l% N/ K$ J+ n: M
! Q. k" ^+ `) C$ N        proxy_pass http://online;! ~" e/ m  F6 \9 G
        proxy_redirect         off;
0 N) r' Z4 f, V; I* B        proxy_set_header Host $host;& m4 u$ ?/ F+ _) G, \, c3 f$ _
        proxy_set_header X-Real-IP $remote_addr;
& j$ }) u; O1 X, F+ C; E+ T        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
% Q, V# _& P) O9 a, \* ~9 k    }! D% d+ ^! C" z! T
}0 g; ?0 V' w4 E+ w- I. }
六.测试
- s5 @  B  Z. l" W, g4 ]
9 Z4 V0 [; E! g9 v6 ^我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:& o6 Q$ Y& A+ t8 r# T" k# Q
2 X2 A4 ?* l7 r4 T9 j
<?php
9 m' O4 ^. k; O0 N# O% d    phpinfo();    1 P' f- C0 r! U, V$ y. k; ^
?>. T: c& Z7 @( k& d6 ~& _
在浏览器中访问:8 |9 Q$ A5 {  B$ V" d3 U! n

" ?. o- x9 {9 e; M1 t7 ehttp://www.52os.net/phpinfo.php?id=1 正常显示。
" O6 d" \  t4 G! l" L7 mhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。7 E! `+ F: H+ e9 l+ f% a& ]6 h( Z
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
7 I+ E+ ^( r: g说明sql注入和xss已经被过滤了
: u( @. G" d8 ]1 A4 c# |3 h
% }3 V: L( b0 r. C- @七、安装过程中排错3 [4 l: I8 \0 p

5 Q; C2 n4 A* u" A* T3 r1.缺少APXS会报错
  m0 \+ P/ u8 L6 I3 t/ Z/ M* A- P8 _# d% m9 x
configure: looking for Apache module support via DSO through APXS
* g* g! Q: x1 R) J" x) t2 Zconfigure: error: couldn't find APXS* u5 N, v1 J5 i1 }+ x4 `
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。8 g* g8 t0 H) _- U
解决方法:
; {: {+ @/ |, C2 S% F8 P4 R, v( O
$ R. p" }6 V+ Y9 |' I9 L4 s" n1 m2 f9 |yum install httpd-devel
3 C0 d9 {* f, C- Z, [$ e2.没有pcre
; X- u  |( Q; o  Y8 E3 j& d, V; I! _  l& j8 ~
configure: *** pcre library not found.! U- }( X5 X* o& m) ]5 c
configure: error: pcre library is required
# r; X0 S1 V! d/ m1 q4 _) x解决方法:
% ~( ^* Z1 t- J1 f) ^+ d/ O% H3 \: M. B" g% d
yum install pcre pcre-devel
1 v; K! [2 C) p1 X6 U' F3.没有libxml2) a9 M  U" `! P* V6 x4 O

2 p- p5 _6 l! Z3 P+ v5 R6 F8 i0 @& n9 Z( v
configure: *** xml library not found.
6 A- L0 a2 v/ a1 x& cconfigure: error: libxml2 is required
. {$ D2 _, w/ v1 `解决方法:
8 S) m' a* D" j7 J1 t2 T4 ?: X  |9 w3 s$ S
yum install  libxml2 libxml2-devel
3 R; O! B, H4 X: `6 C- d* r& T4 ?4.执行 /opt/tengine/sbin/nginx -m 时有警告
5 X" u# R1 @7 \  P  {8 m- G) h4 P) j2 @% i( X% ?3 P2 q
Tengine version: Tengine/2.1.0 (nginx/1.6.2): y' U0 G) l1 _
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
$ {' p+ O$ e# @1 l/ H原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log$ f' p" z) ^; s+ j# T5 P

' M& ~6 R6 i8 T) l; E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.! L% f6 }7 N7 v# Q* M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
4 [( v/ E0 X4 y/ O$ d1 v% y. P2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
' q3 d4 P) ]0 S# Z! Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"1 E- }$ h$ z# z, O3 _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"' v5 G- V% j8 x
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.6 ?) w7 n1 x( r7 K$ t( `. z
解决方法,移除低版本的APR (1.3.9)$ _$ h: @1 @6 ~3 ]- }/ A( B
1 T+ S/ w* x. @& ~6 u; F
yum remove apr* u% e. o" k. H" t8 W  [' Q
5.Error.log中有: Audit log: Failed to lock global mutex
2 X, ^/ _5 K" m9 @
# f! {0 V. u# ?7 z+ n. b4 j8 M! g2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     : v$ h3 W5 i- i
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]  W% o5 `) o/ x* r3 i- o; q
解决方法:
2 O7 i2 {& g9 ^# F, H* X编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
/ I# ]. O* h. T( N  y4 u+ a1 B# V3 I/ ]% T
SecAuditLogDirMode 0777
  U5 t6 S/ D8 P1 a2 t9 USecAuditLogFileMode 0550
3 \9 L/ f) f1 g3 C& L5 P# USecAuditLogStorageDir /var/log/modsecurity3 I1 P% {, |( l- `8 D6 x
SecAuditLogType Concurrent
+ V' M7 t' s) W8 O参考文章:& L; d( h* M0 i8 R
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
- W% l; F9 K4 I* v0 J& mhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-4-15 11:43 , Processed in 0.065283 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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