找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11851|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
. g: w# v: M8 J+ N
  H! d  x: z& g( G3 n一.准备工作
& ~  @3 C$ d6 a
- w! X6 }- J/ y系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0: r3 r6 p- Y- [8 v

+ x. f. u( @/ k) I$ ?, A' T# ytengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
9 R! m& |' n, b2 ~' q
' B, A/ J; s' G; ]* h, smodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz) w+ K( n4 D/ A0 R& Z
; N$ f( b- d: R
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs! J( J3 g5 f& q, X4 |* l
+ v% k! e2 W! j- J$ d0 ]$ f/ Y$ D
依赖关系:
- `9 M6 D2 Z. P+ e' Rtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
* t9 M& x; _, T2 }3 v; ~; O' ?1 F/ D7 F) H$ E7 n- h/ b& E
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel+ R0 }6 \( W1 k- _; Q& O" C
modsecurty依赖的包:pcre httpd-devel libxml2 apr' \3 {* L* Z% g& }+ r  k
+ j7 y3 E- g3 k' P6 L4 m( j4 p" U0 J
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
) @6 F6 c0 F+ I6 x1 y% c* e二.启用standalone模块并编译* ^9 }* H, l( k& ?5 n( Z* s

$ M* N1 c6 B& `3 P下载modsecurity for nginx 解压,进入解压后目录执行:0 L! ]3 d7 g* \/ g7 R2 j4 x
+ z0 X* z- a- a
./autogen.sh- P! ?0 n0 R3 P) a- C  N4 X- V
./configure --enable-standalone-module --disable-mlogc
/ ~* u/ I" h, u3 x: ymake
( u$ k+ ~0 A% B% Q; [: }/ r8 q+ X三.nginx添加modsecurity模块" ?# X& v. s4 N# G" R; g

1 ]6 A  q* Y" C" q# O1 u  e8 d. \在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
1 ~/ s' e) ]# `0 w5 B
1 r- C7 r8 h8 y/ Y) T" p4 J./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine) g" k7 s) g2 Y7 ]' U( W2 G* T
make && make install
' i3 O( x9 i! @! j/ [+ X四.添加规则
; K5 E; w& P: f: H( U! r6 r" L0 ], H8 l. E
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。& Y- h: v, C: z. p
9 T7 M& l8 j  p) b& R- o. ~1 j, c
1.下载OWASP规则:5 Q9 u6 l% B' e0 `+ u

; \7 A# V8 t: b# f% h4 g  q! ggit clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 Y3 b) c0 U9 {/ y8 S' `7 H3 n$ r
: ~, s! A2 t' t3 M4 `  E2 a
mv owasp-modsecurity-crs /opt/tengine/conf/: s6 N2 s! Z5 C' L% O6 }) u
+ y7 z7 i  C7 g
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf5 P! |( W1 y# L4 D" d% A
2.启用OWASP规则:8 x' {) V7 T7 l

  J; A- v0 k* O' x4 T2 h* Y  i; B复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。- `7 s' u  ~# Z& S/ v
2 ^1 w: z) a  J
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on/ ^+ Y* f: P( o! I! H7 h

; t9 b( s% Z9 j& H$ s2 k/ Towasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
& S/ [+ e; g; v, T
) }* a' B( e4 n9 EInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
6 u* k& i* K) K- LInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf$ e2 L3 W- r3 I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
3 d- ~% ~; R- m& k; L4 aInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf# H3 s9 w. ]! D8 i# ~$ ]( t6 g0 ~
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
. G3 f" Z. {$ Z# W) h. ]Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf' A: I; ^" F  ?% u
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
! A7 a3 t( y" ^+ l五.配置nginx
3 l% T: \$ B. J9 P& n! r3 [" c% h, q4 t& z1 v% H  G# l) n/ M
在需要启用modsecurity的主机的location下面加入下面两行即可:
$ \% {# `/ {& y( `/ u/ \& S1 ?7 E# T1 U, l7 p4 o
ModSecurityEnabled on;  ( [# q3 x* R7 }
ModSecurityConfig modsecurity.conf;" J' Q8 a2 K5 G7 F  U
下面是两个示例配置,php虚拟主机:
; e- A$ z4 f- U- o
: |9 t3 E5 t. ?$ t7 \9 n2 kserver {$ l& f! m  x( \
      listen      80;
$ L$ P8 Z$ u0 `# Y# P+ y! W      server_name 52os.net www.52os.net;- D# f" T% w( a$ g
     " a8 o* e: `/ Q& v# g. C
      location ~ \.php$ {, C; H; Q! I& Y0 B8 c/ X/ B- c5 b
      ModSecurityEnabled on;  ; H7 E7 u, Z' `8 C: G
      ModSecurityConfig modsecurity.conf;( D4 M% Z0 c/ |: A! e; k
- [2 P0 e* I8 P7 e& d
      root /web/wordpress;
. O6 |/ i) K& Q! }, g, h& t      index index.php index.html index.htm;9 C8 P1 c$ I( u' ~
  3 R0 p4 _# y$ X: j9 \; h
      fastcgi_pass   127.0.0.1:9000;3 w+ P0 s9 d$ n7 T$ \
      fastcgi_index  index.php;
: S: g  h* x& N- i! {- T! g      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;: J1 G  T) N; _2 J8 X' N% C
      include        fastcgi_params;" p- a$ C2 ~0 p6 J6 C7 S
      }9 ~# }- Q, f) V' s1 V- `4 u8 C7 _
  }7 o, A; n; _! g' l3 t& V) c
upstream负载均衡:  [. F$ I- |5 h5 a. D& _
/ L: Q+ S; }3 b. M3 M$ K- {4 _
upstream 52os.net {
- v% Z6 }4 Y0 b0 P    server 192.168.1.100:8080;( c7 @( Y8 o6 O  N
    server 192.168.1.101:8080 backup;: U. A& R# B8 L8 ^* r
}
; n' |6 _0 V5 l/ D# o" i) r0 k) A& B# Y( p
server {* e$ |9 n- k  ?( a+ D6 C) J
listen 80;: S7 y7 D( U& |1 Z, D2 C
server_name 52os.net www.52os.net;
# U0 r* d) r  t; \. f
) L8 v4 T  `( h# o5 ?location / {' C( i5 g, y  L7 i  l8 `
    ModSecurityEnabled on;  
$ [7 [' M, Y: g# V! M& U: j; s) F    ModSecurityConfig modsecurity.conf;  
0 i6 v( B2 \7 {  N) }3 t7 y' H3 L) {$ e6 d
        proxy_pass http://online;. y  x8 E9 `  t9 `: |
        proxy_redirect         off;/ }! ]3 m6 x1 h; t! Q* }, @. Q
        proxy_set_header Host $host;
- H7 B2 g) ?" z% h$ F: ~        proxy_set_header X-Real-IP $remote_addr;
6 z+ Z4 V* h: n' K- Y# ^        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;  }' ~8 x* D. @
    }
; {$ U8 `; s$ S4 \& o) y" L}5 r- Q; F5 N/ H
六.测试
: W0 _0 m" y/ \+ c- T5 M8 }$ O2 }* X/ r; b
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:/ w8 U: h/ S8 F$ I* S. W' b: y
  X, f+ K2 ?4 C! E
<?php
/ o0 d( A7 i& _$ X4 M. U7 M* ?    phpinfo();    . s' ?7 {9 T5 l9 D
?>$ Y, v$ Y+ T$ B0 Z3 p2 ^* P
在浏览器中访问:! G6 C% A( D6 I+ t: `0 A  G: y' o
, o3 h% [8 l( q  L2 @
http://www.52os.net/phpinfo.php?id=1 正常显示。
8 Y- P( P3 X( {# B% i" M! t2 _http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。3 v* H! T. T6 X
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
) g; v, B8 I6 ~( U0 p0 _7 f说明sql注入和xss已经被过滤了
, o: ~. t5 i& n% Q5 V- s: y5 E7 G
! D: ?' ~, R  H% ]七、安装过程中排错
; T# I; D9 e( t( R% Z- w2 m" [1 }
( V  _% [2 k+ t$ y: d% R; ]1.缺少APXS会报错
1 c  q9 T2 w* [* H' d7 B5 ^2 o$ p% ~
configure: looking for Apache module support via DSO through APXS) Y: m" s' E; q4 d# A( S4 N
configure: error: couldn't find APXS: m3 a* o3 j3 a
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
( y" |2 B+ [3 k4 k* h# [9 o解决方法:" V- A' `  v' t7 m) V) w8 N! k

$ X7 e# w7 N% S6 o$ y$ |yum install httpd-devel
( o: g& g8 [! r, K* M! a2.没有pcre
9 b7 M6 E0 k3 S. _2 M. t+ Z4 A) X' w
configure: *** pcre library not found.4 m+ c' O; d: x& ~2 q$ u
configure: error: pcre library is required
: x- @& }  E) W9 U* }解决方法:
# P" I9 l9 M4 F) V5 `+ A6 ]2 W5 N3 U
8 m# s7 V; F: [" t: K8 qyum install pcre pcre-devel
, e. K- T1 C3 J$ x3 X: m5 G' v3 {3.没有libxml2  O2 E/ `6 @" u7 g/ P4 J# ^0 I" X# W
! n0 K2 K) l/ ~$ s

' a$ t  ]7 B$ _1 F  Pconfigure: *** xml library not found.; ^* Q& P4 o( @- ]
configure: error: libxml2 is required
+ V! Y. B! T  O- L0 S1 M6 i解决方法:% W, o; N5 [& I/ o; A0 p; r
/ r+ q2 f/ E3 k" E* A/ s' ?2 H0 ]) E
yum install  libxml2 libxml2-devel
$ C/ B1 c* R' u6 {& Q) R/ s4.执行 /opt/tengine/sbin/nginx -m 时有警告8 R! [1 ~, y. P2 Z. ~* n5 ?! @
' T5 b6 _5 P- e$ |0 X3 n4 K
Tengine version: Tengine/2.1.0 (nginx/1.6.2), _1 r! f3 _5 U6 E5 P, U. t5 U( n
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!4 z6 i# Y- u4 a" @% a0 i& T
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
( j4 v& o% |0 M4 B  C7 Y5 |* u2 x; h9 S2 K5 _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.1 |) v! z/ j8 ^4 z  p. {( T# s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"& c, ]* w! V# _
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!* j# L$ l: D" V# B5 k1 I6 z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
+ @9 C6 a+ q* m2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
. C" R+ L/ x# m1 R" i4 {2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
8 ^: q( X! [8 H. o1 p解决方法,移除低版本的APR (1.3.9)% F; u4 j% Z+ `" E

/ D1 X8 h( e8 o9 b2 R9 Z& f2 lyum remove apr
: ^8 I& ]; c$ Z) E! L/ Q5.Error.log中有: Audit log: Failed to lock global mutex
0 e+ @& ?7 L4 U3 T+ I! z& J: q2 f6 j- A' g/ d( T
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     . y1 |3 J& j) c! m8 ~
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]5 S+ m. m. H$ J5 F
解决方法:& Z" A$ T. S3 ?1 e* p1 C. d8 l2 a
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:; y/ p3 u8 b$ p# i/ g3 r) ~+ K6 a# R

( W0 U* D/ r+ p) E) _+ D6 a, fSecAuditLogDirMode 07770 n3 }9 {8 N3 H5 N& y( C9 c. g# X* S
SecAuditLogFileMode 0550* K: [2 W  k8 I1 q3 z
SecAuditLogStorageDir /var/log/modsecurity9 U7 V% k8 c! B7 _
SecAuditLogType Concurrent
& c, _! E: [9 {- R, H  z参考文章:5 c! @" l9 t1 v
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
% A- A! n/ M5 ], k) bhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-18 09:50 , Processed in 0.067634 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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