找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12948|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。; g; M- z, e7 N* Y! R) f
, y0 m/ W! g& V# t1 o
一.准备工作
2 k* w- s8 O" M. \4 B, U& f
$ T- ^! ~, q& q5 |* H系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 R/ S; t% S: ~  \$ b8 I# c
7 p% O& f, \: _; t; H# Ltengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz" Y2 H& J" \( o
+ Z8 m; x& q" m. k0 x1 ]! |
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz9 U. ^6 [2 e+ B- d
( d2 e. v- K+ ^' e% B
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
. C% y  S  w+ X5 d  t
/ L$ `. J5 P( A依赖关系:! P! ?2 I# U" y! M9 n
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:, l' A( D% G* U; J5 V9 ]
1 F3 P* l/ m  V- ~, e
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
9 R+ s4 k& `# Fmodsecurty依赖的包:pcre httpd-devel libxml2 apr
8 K3 f7 Z. F( }1 {' Q! j% C, K% v1 V; m
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel) M! T5 X2 J% y8 |& v- D
二.启用standalone模块并编译$ U1 P8 h$ u# E1 @* }

: N- L9 A( F/ b/ U, l下载modsecurity for nginx 解压,进入解压后目录执行:
8 C  X: H/ a) k* A
; ^4 W: a' P4 ?' T& O! v/ N* @./autogen.sh. g3 c- @' r( p7 b
./configure --enable-standalone-module --disable-mlogc
# P( i; G% ^9 Nmake 8 ~7 q4 F6 o( j$ Q+ d
三.nginx添加modsecurity模块3 h6 ?6 n% {' p) e* O; C4 K

+ r0 f7 R& K) b/ }6 M1 [' ^在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:8 d" r/ k" c( D" L7 D0 B5 l8 z9 h4 G

) ]6 C2 w- D9 K; }3 d8 V./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine2 R- H4 P9 e4 k( M& k; f0 h
make && make install
& [% @% s; D4 m3 P3 N) h' }四.添加规则
4 C+ V* G8 e6 s% }  t) S; w7 w0 G7 x, u. s
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。3 I4 s: b% z6 Z. C

4 `8 W1 I6 c% z( |' b1.下载OWASP规则:
! D3 S1 J$ R* X- K. S
9 k' n9 i, V- h( A/ ngit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
8 v! X; w: [8 p) C8 o; {5 M1 ?% Z  b
mv owasp-modsecurity-crs /opt/tengine/conf/5 Y9 m: C* ~5 X

: C/ d  b8 e6 icd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf1 X% q3 B) r  Z3 P3 S" Q
2.启用OWASP规则:
( V+ T, m8 U1 J# ~- E# w1 Q7 X& K) V) h% R" H' {4 }
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ Q+ O8 k2 S: `& H% ?
% a6 z: w5 U! A( k- k! @
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
7 c7 i! R2 T! \1 M9 @: W5 G5 g3 F, E5 B8 U/ {9 |/ u# R
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。2 ~8 @  U5 c, [$ F$ ~

3 V; x# T( B( n) k( rInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf$ r3 ^* j6 u( B: y  q. D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
- ~& `, K4 ~5 `) j7 l9 PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
8 e' y0 ?, |+ c+ C# n. E4 hInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf8 F% T" |# g; E& ~+ a
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf  I# h" ~* U( x$ P" j
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 g9 R' Y: ?- b6 b9 Q. \Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
4 c2 b% ]& S4 u5 U! {/ z5 \五.配置nginx0 N! B: _7 J& O% Y( U+ _. R

8 D! ^8 P6 y. H& d" D在需要启用modsecurity的主机的location下面加入下面两行即可:
( i4 ]2 G. P( R! N4 a% i" Y: p$ u. x
ModSecurityEnabled on;  6 Q- }. n8 l* V9 u% G
ModSecurityConfig modsecurity.conf;
0 j& A- x! v. R( x/ E下面是两个示例配置,php虚拟主机:. G3 n6 J0 y/ b$ C
( N8 C  T5 B. J  Y6 Y) e2 I
server {
3 J3 o$ W* h+ c9 [      listen      80;" o. j6 |2 H  l9 n/ v
      server_name 52os.net www.52os.net;
* J) I" U+ d* f7 x     , F8 |, W4 A" N1 o' R
      location ~ \.php$ {
( b* _6 e- ?- w7 C3 M; H' K" p      ModSecurityEnabled on;  
9 _) k- l, r! l' X" H/ H) o/ Z      ModSecurityConfig modsecurity.conf;
. D1 w. _% N* _; z& I6 S  W
7 @9 F' J% f/ D4 v) }      root /web/wordpress;: x! R) s! y* T, }* X* H
      index index.php index.html index.htm;' f( M8 U* y- i6 ?0 K* x" a! c* A
  
+ g3 D& v+ s# _" z& o1 {. J, |* @      fastcgi_pass   127.0.0.1:9000;* I$ ?6 O* W# D6 _8 U" v: x" Y9 N! T
      fastcgi_index  index.php;
; I: T7 G9 O9 y5 I& M) W3 m      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;( W- s- [2 D- h
      include        fastcgi_params;
( y( M. M7 O8 L1 R      }
1 ~- M% u. ?0 N& d1 M; A  }: x9 n3 p$ t4 V$ X: ^% @+ \& c$ |( n" M
upstream负载均衡:
; ]: a5 B! v& `' W- o0 Z9 p' j3 k  ~
upstream 52os.net {+ G- M$ Y( ?# e0 B. O. K) l
    server 192.168.1.100:8080;% r7 c' s. V4 W1 y* Y+ C+ H
    server 192.168.1.101:8080 backup;
" A- ]' b* `$ n. B+ W' P* o}% Y7 i* D) d' c$ n/ u4 A2 \4 |6 w) A" m

% F- G* T) T: T9 U+ Cserver {% s! y( P8 \7 [9 t- `- h0 I
listen 80;& Y0 B7 U+ s/ D, A7 ~# T# j7 L
server_name 52os.net www.52os.net;- A; [1 K+ {* e

! T; U8 c4 M7 f& A2 p( T$ Flocation / {
5 o  ~. p  H+ Q: m# T    ModSecurityEnabled on;  
+ e) {9 x! Y9 ~: n! u    ModSecurityConfig modsecurity.conf;  $ G- H6 P& g4 e. Q5 ~1 J
; M/ b2 w$ g* ~
        proxy_pass http://online;
2 y2 @, e2 |: R6 Y        proxy_redirect         off;
7 J/ ~" b0 X9 Z/ {        proxy_set_header Host $host;4 w' k" Q8 w. F& g; r; X. g9 Z
        proxy_set_header X-Real-IP $remote_addr;; d2 P3 I! j% z; P
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
) Q* x/ H8 I* E% Y' `  T& ^; D- g- B    }
, s8 k1 c0 {* d' B, J1 u$ ^0 `0 ]}
: q" Y: J: b( W) t六.测试. x+ V$ n$ g$ f- N2 A; X% p6 d
9 ~, [9 @  N! z
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
) J: D0 F4 K/ y3 O; q; O2 A9 }
2 C6 T* r5 n; I+ s! a7 u* A+ d2 g<?php% o3 a$ I2 q& Q0 D4 n/ E
    phpinfo();   
- ?! Q+ i* K" q: d& X( w?>! c5 m+ e+ `5 P9 X0 W9 i
在浏览器中访问:# V. w# x( L  \' Z

8 f; ^( E9 O* M8 H+ B$ b4 ghttp://www.52os.net/phpinfo.php?id=1 正常显示。
5 g, h2 K0 N. p0 E2 `4 B  L  Lhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。3 U3 E7 T0 Q$ S5 Y( i
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。6 {$ H! ^* a2 Q9 }  D1 R( s; D1 @
说明sql注入和xss已经被过滤了  C) a) i5 ^! A$ K

1 m2 W0 @# U: i) d2 q: s( t" m七、安装过程中排错! {( b5 u+ [# ]8 }+ R& \$ r

# A" D4 ]- T$ H, ~$ c0 i# Z5 [1.缺少APXS会报错
; e) D: k" S4 L
; a! K0 `) }% F  K2 k0 tconfigure: looking for Apache module support via DSO through APXS9 E4 O7 t5 O; u4 J
configure: error: couldn't find APXS6 \" M- u5 }3 w; e1 f# l6 ?; ^5 Z
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
5 T" P1 S. y4 @( K解决方法:
3 B+ R$ O& t5 u; T% u# H. w+ A3 [7 |
yum install httpd-devel
9 @, I9 q# a$ S& a2.没有pcre% G0 h, E. o& V5 A  t
: U5 O) O7 ]' m  W7 T7 R8 _4 q
configure: *** pcre library not found.; R$ w$ E" w3 y7 d+ h& v
configure: error: pcre library is required9 G/ ?& w# Y2 x, p
解决方法:, w6 @  ~* R' F
( Y8 f" e3 y. a5 U, b
yum install pcre pcre-devel
* ^. O2 h5 ~# ?- S/ E5 t7 X- A3.没有libxml2% P$ u/ Q' H7 P+ k
* ?, a0 n- e2 L/ @
, N/ B8 ?8 b; u% {8 |8 D# l" D
configure: *** xml library not found.
+ k3 X+ g  `7 p1 r2 @configure: error: libxml2 is required
( ?1 p6 [% J2 U6 B" O解决方法:
" a/ p. U9 B7 \0 ]
5 I% ^/ F) x) u% tyum install  libxml2 libxml2-devel, h3 s4 y3 C( w% B7 j; o  O. b
4.执行 /opt/tengine/sbin/nginx -m 时有警告9 U- ]( }' u' r$ I4 @
& a9 D8 c: ~# H% |" x
Tengine version: Tengine/2.1.0 (nginx/1.6.2)+ R/ L  k8 m, w+ n  J
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!* [# d  w  U5 g3 }) `
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log6 _: a  ~1 X( x/ L6 y2 A

# U' Y" U5 k$ d  c9 \& T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured., t) V/ [3 A) I  r5 d2 g6 W) ~* O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"" p# i6 z1 h8 T1 c2 V
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
7 N& E* l9 o$ O" z# }- D2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"1 }, G% r, e* l5 T7 Q5 q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"8 |4 N. h6 [4 I) d
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.* ]  f, }  m: i0 _, Y  d' E
解决方法,移除低版本的APR (1.3.9)
) I0 s1 p, u& ]' M
$ h0 R: _4 n' k2 E4 X2 z% G! Tyum remove apr
( J1 M* H$ f. s- g* {! x: G5.Error.log中有: Audit log: Failed to lock global mutex
! i- P6 ?8 @5 k& t
: f0 l1 }& C' Z  n0 B$ b1 z2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     + g4 I* R6 _( V  [6 ~& A
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
- s* P3 F* y# W/ D% K+ _解决方法:
5 @9 Z' d% m' l3 v编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:, y  k% t7 Z/ K* i: s9 U
  |" i" }/ m5 J2 g# G# C; _$ i7 d
SecAuditLogDirMode 0777. S  e) c- a9 r& j
SecAuditLogFileMode 05505 X6 w8 i' n: B7 F: D+ [' c
SecAuditLogStorageDir /var/log/modsecurity  A4 O" C  H. Y1 Q1 j  R
SecAuditLogType Concurrent
( T% Q6 b& Y5 K2 w$ p6 D5 T参考文章:
  |2 o- k5 X1 U6 e4 u& L8 l) z6 D. khttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
5 `/ {& q* }/ `http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-29 04:07 , Processed in 0.139467 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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