找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12697|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
9 L4 H. m/ f+ `$ Z3 _
) k( @- B: y( D) P8 Z一.准备工作
  t# P/ A  B6 [* F9 E4 v. T/ f' t* Q" Q% O3 l
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0) L5 H2 z6 V7 L* C7 U' ]
& K, O( F3 V3 h3 h$ p/ q( Z
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz6 @3 l! [3 @, _6 k+ I; y" ]

) r' j- W/ V: G4 cmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz8 R) a. C. K8 Z  I) V
& H% J8 @* @- ~9 ^% v4 o2 J
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
8 @) d4 T; a- P7 [" c  u
; L: G# T8 L4 C6 m5 Z依赖关系:" G. ~1 [# W' ~( K8 j6 J0 C& ^; i: O
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:: U/ g" c4 q( y
# q2 R: j- M- D4 d; L# r+ q
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel; L$ X# t7 \6 J% z
modsecurty依赖的包:pcre httpd-devel libxml2 apr$ [  Y1 d# T8 ^# v% p
# C* j( V6 j) u9 L8 G
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel  v# d$ O' I+ L5 C- |1 M3 H
二.启用standalone模块并编译
/ G0 L% S5 x' m; o+ `4 b9 b& j. Z7 A& Q* }% ~
下载modsecurity for nginx 解压,进入解压后目录执行:
) L/ j% X3 v- J4 r/ v$ n2 L* V3 z3 s7 t9 S5 @& x7 s% {1 e
./autogen.sh
/ V: O3 l# i6 b./configure --enable-standalone-module --disable-mlogc. d, f9 f+ Z7 }& x/ V5 [
make
9 h1 F/ X% Z6 O! P( A& d三.nginx添加modsecurity模块
" D4 A' N: r- s) T3 |
3 l. ]+ j9 q. J' |; C; X在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
2 ~6 `( W0 V- m# k* c; J, ]
2 a/ I- a- h2 P. A7 Z6 S./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
% e( m2 g) ~& q% O; rmake && make install1 E  y* I1 s1 o
四.添加规则
9 j% m: ~, s2 j: N( r8 [" E2 [: F
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。9 y% T, ]1 C2 P

/ d$ R- a' Z2 L2 e, U$ c1.下载OWASP规则:+ D( L" B& E# u* _
4 g; v) `4 \% m
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs) m9 k$ b! r# q3 g

1 R9 X! O7 U* _6 P1 w& H9 Vmv owasp-modsecurity-crs /opt/tengine/conf/
' A6 l' A9 d5 H: m8 @) Z0 B$ f7 @$ o7 {, s, G: u# b- y, h0 U
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
, M* ^% j+ D; L/ ~. s' j, y: M2.启用OWASP规则:
4 A; S* g  _' b4 m% a( d
  N1 _! ~/ w1 I. g% E3 ?& h复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。1 u, ?6 J# p- a

! g" b% Z; e4 d5 G6 E/ g/ G编辑modsecurity.conf 文件,将SecRuleEngine设置为 on6 k$ U/ R5 g2 i2 w
* `5 h" t) H8 M9 V$ Q
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。5 b% b2 z; F% d
' K+ K2 s% I# }  o
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf' }5 F9 c% Z1 [, D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
1 @* d4 [: \; R% RInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
+ n# @7 F) E, ]/ a4 J) S' GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
$ E- m. d0 @& ?  g% ^) {+ A) vInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf! x, F9 p2 p7 q* P6 w. r1 w, Z' W/ T
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf: x% Z8 a9 S/ P" b7 H. y) n) K
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
, g5 `, i) ?6 T) ]# h" S五.配置nginx. S& W0 a- `+ e8 ?9 @: j* d8 y
& y' \  q0 C& H) ]$ W  t  r
在需要启用modsecurity的主机的location下面加入下面两行即可:& p$ D+ @. {! w2 S7 t
; _% {% i6 y* T
ModSecurityEnabled on;  7 o$ Y) {* R$ A/ j, S
ModSecurityConfig modsecurity.conf;  b% a6 @7 r5 j7 ~" u5 L. H
下面是两个示例配置,php虚拟主机:. F/ e' e- r$ f; e5 u
, P5 I0 z2 ?3 B! k: j2 j% u
server {% i! K" h0 Q0 L5 Z8 p$ E. w% Y, o4 `
      listen      80;7 {# x3 w' T, q4 D
      server_name 52os.net www.52os.net;
$ t4 [( ~% k+ a. G$ _     
6 p5 u  s6 O. ~' O# e- \      location ~ \.php$ {
- @4 W: U" Y3 n! y2 T      ModSecurityEnabled on;  
6 N7 S- R) ^! t$ @/ S9 O      ModSecurityConfig modsecurity.conf;; }, d: Z/ i, h+ y+ @
# `- \1 W6 |- M  {( D0 K8 F' a
      root /web/wordpress;: D) F/ i5 x, v* |
      index index.php index.html index.htm;& s2 {  c0 D5 S/ ]8 N* C* h$ K
  
) ]  [, _  _' E- k  E8 H3 a  e      fastcgi_pass   127.0.0.1:9000;
1 p4 X9 D. ?4 C5 O$ D. V1 R      fastcgi_index  index.php;  @4 `. p4 e+ m8 b+ _8 l" b
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
- N! Z1 }# q# p" ]2 z/ Y. h" u- ]# G      include        fastcgi_params;
6 T7 G% _/ h2 c7 v      }
( Q  Q; q6 ~7 z6 O4 f: C4 f  }- m! k& V& s- l+ d2 h
upstream负载均衡:5 M) S$ Z4 U4 {0 r' u

1 C2 @- W  T4 _: A5 R/ @upstream 52os.net {
$ u/ s4 c  p3 Y. Q% h. x7 u    server 192.168.1.100:8080;
' u5 `2 v. J$ J+ p/ C8 k3 D    server 192.168.1.101:8080 backup;% g9 r. B- E" s4 C8 B1 {& k
}
4 L" x' z& ?% B+ Z8 z/ q2 H. @8 ]) G( {# p+ p6 @  @: ^+ _
server {
3 R: m+ J# f( s8 J+ Llisten 80;
! p7 D8 {% }" E, y, Wserver_name 52os.net www.52os.net;5 ~9 P0 n' z0 c3 G8 t  E/ j# s
/ Z  U4 U. h( Q+ B
location / {
4 J- c. t# L0 P+ _; j8 S* G    ModSecurityEnabled on;  : m9 e, N4 H- t
    ModSecurityConfig modsecurity.conf;  
3 V( Q, t4 G9 {0 r
/ T! L$ a+ ^" c; b        proxy_pass http://online;; R5 i$ G0 }+ R
        proxy_redirect         off;, N& {7 m6 }& g1 W0 v: x6 z
        proxy_set_header Host $host;6 {% X3 Q& {- i8 F; j4 v. {6 T" y
        proxy_set_header X-Real-IP $remote_addr;
# N* k9 H) S3 O& Q& v$ _9 F3 R& k) r        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
/ ]7 l' s. Z# i) C/ R    }
+ F( Q2 c; x' j8 P0 D, `}
( X1 W/ T  b$ d2 T# i六.测试
8 z  s* x+ w% b* C% z
# y2 ?' B) [8 _% d% R! U我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
. q0 U8 a% v9 g3 n/ i3 {$ j6 f! R% L) Z( w* q
<?php3 p4 {9 L3 Y! s9 Z5 E2 `* w) X
    phpinfo();    $ {  e* Q  J' e- ?1 p7 u" Z+ G% r
?>
8 D* ?: M; {' w在浏览器中访问:# [( R# |/ @6 F$ l0 r
& S& a% A) v4 K6 E+ i3 |: D
http://www.52os.net/phpinfo.php?id=1 正常显示。
7 L- C* q* E4 @http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。5 a$ K. B+ F2 E5 _+ t
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
: F' \# q! f, I7 W说明sql注入和xss已经被过滤了
& E; G1 H5 E' l0 a5 Q! [) \' }2 g+ @8 M( P9 h9 W
七、安装过程中排错2 x6 E# ]: \3 e# d; E

8 b$ u7 q( F! ?! A& l" l1.缺少APXS会报错
( g+ g8 _0 e. f9 y0 J: _/ h$ L5 y; C& K5 h" y3 h/ H% X
configure: looking for Apache module support via DSO through APXS  u% r9 W1 O4 w5 j$ G8 P7 e
configure: error: couldn't find APXS
7 w  U: Y2 h+ H: ^) z7 G( b: sapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
4 J7 \6 b4 _( M. ?' e解决方法:) ?$ G; G+ c2 G! C& ~( e0 K8 T/ [

' y" e9 ?! H3 {) `: O; [" ~  c1 kyum install httpd-devel! d; D1 o6 W) g  x
2.没有pcre
  l' _& |; |/ a5 W* e2 u) |6 U# C8 x" z
configure: *** pcre library not found.; Z9 M0 f7 A" o) m5 [
configure: error: pcre library is required& Z' D, e+ g1 l: h
解决方法:! J% J/ N! ]% c
" p5 @9 f$ w* ?+ D- ~/ p
yum install pcre pcre-devel
0 G# h( ^- n2 \: o, h3.没有libxml2' d) X1 X9 @- ~8 A6 j0 U* W2 [
: ]8 J0 H! U5 Z! B2 a- i5 C

6 o. x, _  W2 @. \  n7 x9 xconfigure: *** xml library not found.% d5 h$ k, q  Y
configure: error: libxml2 is required3 j: v8 ^; o5 X+ W! r" ~
解决方法:
1 k6 t- t3 I5 c' C: c6 F4 u( ]
4 @* z$ K8 t1 }0 {& {6 M  q5 r  \yum install  libxml2 libxml2-devel9 V' z# H0 K( y. t! [9 [' O2 w
4.执行 /opt/tengine/sbin/nginx -m 时有警告# l* n8 U0 T' o% S! R) b( y3 @
! s% e/ K  U  j
Tengine version: Tengine/2.1.0 (nginx/1.6.2)5 g# ?7 [8 G5 A0 F: ?# W7 K
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
  `) Z+ ^( o$ b/ c3 P& h  M原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' ]2 N' f' K% a" B: c
* j( Q, B8 M0 G2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured./ r( ]! M* X7 C" k5 V5 Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
$ ~; ^" |" T; `9 h. N8 n2 e" i0 v2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!4 R# r: j) ~) b: U( o4 l$ U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"1 i2 S" g( Z; p% u! c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
, a+ E& J8 H8 a1 P) z+ x2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
- t& L% b  r6 p, z/ P# I解决方法,移除低版本的APR (1.3.9)
& I# h# f& b7 m4 [( [+ q8 \; b+ f5 K$ ?3 q6 f
yum remove apr
$ V9 j7 M% Z5 O6 k0 l5.Error.log中有: Audit log: Failed to lock global mutex
- O: m$ v! g4 X4 A: D
- Q! J& n5 o% J0 M1 a* u2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
: {) O/ J2 u2 E9 K" R. ~global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]" E" H) `% v( c% _/ [$ s8 z; c
解决方法:6 i0 Z5 K' o" x; Y/ a2 h2 Q
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:/ f% Y5 S6 c" e* U5 Y, ~& F
' D& S4 L5 _! X
SecAuditLogDirMode 0777" c/ P* v1 Z$ h# q9 }1 @
SecAuditLogFileMode 0550
* P2 f2 a3 G' JSecAuditLogStorageDir /var/log/modsecurity  \& f, Y& r, z. D  R. v3 I
SecAuditLogType Concurrent
. M: W) H' W' \7 G# K" }参考文章:
( ]6 N! i5 m. }https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX. v1 a0 ~& F. q/ B
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-1 05:48 , Processed in 0.069185 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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