找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13231|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
7 r- H5 a% g2 }# a" Y
" P. `5 L8 @; e& j( ~一.准备工作: G4 s  I+ _. w/ F0 ~; y
' N& J( a, Y% S6 a* C9 {
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
) ]5 C) {) {7 [
" P( [& d% A# ~+ y# l' y) i1 utengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
2 f6 A" }) n; j+ v6 _  o% r1 x! \% f0 n: {2 l7 |) y
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
  u! g7 c5 @: j0 Z  n+ ^+ F- s* t  p' v& M) Z1 ^7 E' z
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs) x" ?1 h$ b% Z
' G/ k* P& n- O) P* f; f2 W/ ]
依赖关系:% B  E2 U' O2 Z6 H) [& z
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
: c( X0 |1 {: |, X/ D2 }% h- \
2 z- l. U4 e5 Y5 Eyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel) {: d: I0 z/ w4 {8 Y+ K
modsecurty依赖的包:pcre httpd-devel libxml2 apr, b9 i7 L3 Q$ d& `* l0 @0 ~

6 O/ i8 f( G8 dyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
- b8 t+ j# o8 L- t0 [& D0 x9 ?5 n二.启用standalone模块并编译% E' P  I  j3 s& ~

: _) B" X" V5 h0 @) _下载modsecurity for nginx 解压,进入解压后目录执行:
* \( W' ~' w$ t# u9 u/ L
6 i" T$ n1 X' I% ?& y./autogen.sh6 Q4 _" p2 d- ~/ Z5 f6 u
./configure --enable-standalone-module --disable-mlogc
9 K: M3 O' x+ [make ) e8 ^8 H! p8 D- O+ X. W1 ?
三.nginx添加modsecurity模块9 A+ n1 U/ ]! q3 H) @

) L; t8 s4 T! v8 D+ D8 G& o, m在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
- N4 X7 |4 K& y0 \* h* L) H
0 [3 v% m' H6 q' M7 o./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
7 j1 G5 y. p) X7 b/ a# G# u& }make && make install
) b/ U0 m0 T/ t$ x四.添加规则
0 O; x% s  E: ?* F" b: L7 N$ j+ t0 e' ?. w5 |; t
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
9 j+ q' U& j( t. {/ T# g1 ]7 l# i+ I! N# Z# r: T
1.下载OWASP规则:7 P; ?7 l  f0 B6 s. R2 G# Z5 p

, e  {$ y( x$ G1 igit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
9 T9 ^, B  F3 y% E/ c+ e
) ~$ w) X4 a) i% amv owasp-modsecurity-crs /opt/tengine/conf/
! D" l% R( y. a5 @- y- E* x7 s# w& A; X% b9 R3 d6 G
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 A* A4 @2 [* e1 P
2.启用OWASP规则:* U* F0 N5 p4 Z
' x& R4 ?& W* r. `
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 y- l/ }- R1 O% s0 o

: q6 P6 }2 p- d! M7 L编辑modsecurity.conf 文件,将SecRuleEngine设置为 on3 r( F6 @1 k/ b; I" a( L/ ~3 t

# p2 J) {' b* a" Jowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
* z+ I: |+ h/ n7 w. M% F* ], [  x' x
; G- i" f1 N$ M6 M: I) g! sInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf* t' z7 b$ H8 u6 [8 M; I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf8 t! s7 g& m1 F2 k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf; L  Z" P5 z) @+ H- U8 B
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
1 Z% F8 Q# V8 J7 D8 HInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf0 D. z( }& c% i
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf9 M# T; @" x: B! q9 K) t
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
: w; M8 e# q% k9 Q# a五.配置nginx6 d3 l: z* y# z0 k, [& n9 `
, _" e& `( S# D" x8 ^5 q
在需要启用modsecurity的主机的location下面加入下面两行即可:
5 m, ~: w+ e8 y* i! K
0 M" l2 ~5 J0 U9 q+ T$ G% q. eModSecurityEnabled on;  
2 L2 R+ E7 B3 {0 [  i: W+ SModSecurityConfig modsecurity.conf;- o) d1 L- @/ J9 o6 A  p0 o
下面是两个示例配置,php虚拟主机:/ ^3 T6 Y2 ]8 z  Z4 q4 H
+ V+ U- D" E# y2 w
server {+ Y8 a$ `6 i5 C( L
      listen      80;
% Y% V& l- {- v5 L) `! r9 w4 }, b% b      server_name 52os.net www.52os.net;
  X2 @1 s" t0 u7 ?* Z$ R     1 ]$ w) v, D) C! L  L$ E/ Y: Y
      location ~ \.php$ {
- B% \  A# d* ^7 a4 L      ModSecurityEnabled on;  
1 C! k6 x& D9 s$ c# Q6 t- g      ModSecurityConfig modsecurity.conf;$ J' M' r1 \  T" Q
4 X# x$ _$ q* ?& p: X0 q) ?
      root /web/wordpress;
0 [" p( O. K$ @      index index.php index.html index.htm;( L# f+ x8 V% }, {9 \, f6 C, `7 k
  
* l4 @) d) @* T9 ~      fastcgi_pass   127.0.0.1:9000;
) Q) v: ]: I! I1 N4 w+ h1 Z$ I      fastcgi_index  index.php;
/ B2 W+ f' L$ c8 U& ^4 A      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
, ~9 ?- q, ]6 Z      include        fastcgi_params;3 \6 K' Y/ r" ^. ?
      }: Y0 ~$ {/ C1 }4 F; F
  }7 Q0 V1 T7 S! j4 I1 m
upstream负载均衡:4 G1 H1 }9 `* w( S* p3 p
1 k" U% T# \$ L, W, L
upstream 52os.net {$ t0 i1 j, a& C+ j, ^7 b/ F0 k) j
    server 192.168.1.100:8080;
2 C) R( G) U4 K; Z7 b, @. F: l    server 192.168.1.101:8080 backup;
$ q- C/ p; P, Y& X7 I7 j9 e: d}
" N) k/ U  x8 p- z* `# h9 t& ~2 }0 l, E/ m
server {9 w4 e9 J- u  G
listen 80;
9 |  r8 g% C) `' y" {server_name 52os.net www.52os.net;; O' ?- Y2 B' Y

4 }& ~  a+ V( X* flocation / {4 u$ ^) N6 h! ~9 w/ `7 M+ [" y
    ModSecurityEnabled on;  # R# P' x1 w3 R9 A4 N* m# a
    ModSecurityConfig modsecurity.conf;  
* X. \4 w8 ?: V( R( N" o4 g/ @$ L+ F
        proxy_pass http://online;
( O0 J8 V7 {% u3 P1 ]& |9 t        proxy_redirect         off;
1 d1 {% _8 ^! _9 T        proxy_set_header Host $host;
. y) S' O, V  v" f+ {6 u/ m8 W        proxy_set_header X-Real-IP $remote_addr;
! T# ?) ]: D2 y        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
% Q. g# W9 k& t/ ?2 v* G    }
% F7 w) M! ^$ |- x8 I}. v/ D2 z; `' g! I! Y  Z
六.测试
+ |+ }6 W: G$ x& t, V- J$ k1 W2 H$ M# W$ Q
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:8 j. {3 P, w9 r% Q" t) x" |8 N
5 `( W0 g$ o! U6 w. }5 r
<?php
# U# G7 r! K, ^4 a) e    phpinfo();    ' B) N. r, _1 `* O9 ]. f& D0 e
?>0 p( E" J$ M  w3 y" W5 V, T- Z' \9 {
在浏览器中访问:" {; C2 |! L9 g+ w' H
. R3 n* ^# ]5 i
http://www.52os.net/phpinfo.php?id=1 正常显示。7 v8 ~) t  S# }
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。& x; ?' @7 H: ^6 ^9 }) x7 m
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。& K) |! \8 v; a9 U
说明sql注入和xss已经被过滤了: E) {& G: K/ y' K

9 O( O7 `1 T1 A, I( @" Q; S七、安装过程中排错# \5 n7 ^. ^, o0 q0 [3 I, q

+ i& c! d( |' E1 Z7 S! [1.缺少APXS会报错
  n0 a8 k: M! `7 R) e3 u/ [0 b* G; g6 `5 ?8 x' m, T% v; E+ W% t" f- P
configure: looking for Apache module support via DSO through APXS- h3 ?. t' K/ K3 |+ _
configure: error: couldn't find APXS! i* R1 W7 d: Q. j5 l) }
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。: Q; j2 Z2 d# N' X0 b* B
解决方法:9 P  w$ [& `  {3 L# Z; ^

! S/ o& h4 N8 I! T7 O( U  tyum install httpd-devel& v" ], d- @8 E) j8 i# @! Z: h5 G
2.没有pcre, E0 M# T* ?; Y# C2 V. B( z7 S

/ g* o. D" _) f6 E' Pconfigure: *** pcre library not found.
& I: b) o+ N. F; @configure: error: pcre library is required* d' {+ G( g2 ]3 R
解决方法:
1 [# ?% Y- F% i8 Y1 G
) c% T$ n# p* q6 f- K0 o6 Tyum install pcre pcre-devel; Z# r1 g# |, |
3.没有libxml2- y' p4 {* i) j$ I: d5 O
# `, w8 L' {. j& h

6 o# x2 r7 g' w/ O# Y- cconfigure: *** xml library not found.
* m! v8 W; l0 D/ `configure: error: libxml2 is required5 R8 O6 i0 J6 x! X1 N
解决方法:
7 E2 T- _3 I% _" H- v0 N4 N9 n( d
yum install  libxml2 libxml2-devel$ R4 S) G$ }' ]3 g2 R: l3 ^) y
4.执行 /opt/tengine/sbin/nginx -m 时有警告# M! F3 _2 S' U" n6 `7 c
3 A" N/ l% p( u
Tengine version: Tengine/2.1.0 (nginx/1.6.2)2 O7 L! `, W! Q4 n% R+ q
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!, u( @; K) R$ z8 r
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
& j; I, C% J5 X9 t5 }) N2 w  [4 ^! K$ C( ^. Q6 O4 Z& e: Y& S: i
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.# G0 O, q' @) Z1 ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
9 r; b, x+ A( _) Y, A$ g5 G% f2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
0 P9 r" ^: S9 o( a+ `' z. c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
: o  o: o* c" ~2 A+ K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"; u- }/ W6 t* z4 z+ }% P9 ^
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.' U% D$ A5 l, ]
解决方法,移除低版本的APR (1.3.9)
# K8 D3 ~4 _7 I8 \) S! Y$ P! t, }( Z+ J4 T& ^9 y7 g& E
yum remove apr
: u- M3 T6 @3 w7 M7 z9 e7 b( ?+ q  ~" K5.Error.log中有: Audit log: Failed to lock global mutex, E  d8 v2 y- g! f9 ]9 ?
! Z# e0 f: V* M' _* x, ?
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     % p9 H9 {8 Q- v# E
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
9 T) R; I; Y( @9 x' Z) ~  m解决方法:: ~% {' E" y$ w& g% x1 I5 C' s
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:8 W! D6 R+ S, ?. A8 ^

! ?/ j4 H5 M' d6 w4 T6 B' n5 ]SecAuditLogDirMode 07775 U' N0 L2 a% K) a1 n
SecAuditLogFileMode 0550
! N/ |+ Y0 F* HSecAuditLogStorageDir /var/log/modsecurity
. t% F! M6 T4 ZSecAuditLogType Concurrent
. T( w1 _/ ]" |* [* B8 _1 c参考文章:
& I2 i: @3 ]  j! U4 xhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX! ?3 s: M( R$ E
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-26 19:02 , Processed in 0.112541 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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