找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12074|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
8 G4 V/ \! X" W. L+ q/ E2 ~
# [! r" q7 ?% ?! y, n- @一.准备工作
; i/ G* Z' r. i- J7 C# R
/ l5 Y" S8 w; J, F2 m7 }系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 s8 x" W% \: D3 z; ]
: M/ F, Z) P; wtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz4 ]' n. H8 M1 c. o$ R- V% a
/ v, i6 S" y+ `, q. Y$ L0 U
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
& G2 p# G* a/ w2 e6 `6 K% |7 g
" R5 C; z, ], q& i- d/ E  VOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs- ]4 O6 ^% d. j/ Y# B3 e4 o' O
* n' h+ }* w7 w8 n
依赖关系:3 A* k2 C* d2 o+ E* V$ B. E
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:2 l% F& x- D# A! b" l, }

* q% s: Z$ d% K% i4 {+ n+ ryum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel. j" R) L, ?) R  [! ?+ O" o8 }% `& J$ ^
modsecurty依赖的包:pcre httpd-devel libxml2 apr1 m6 a! |. P5 Z, k( ~5 E
( ~8 l7 E# T4 K4 |) w
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel! T4 L- j2 L5 P0 A; E) h
二.启用standalone模块并编译
$ w  N: _0 F+ Q+ G; u. z( J& l0 k! i
下载modsecurity for nginx 解压,进入解压后目录执行:6 m6 u2 W. e1 z5 |4 F

; h1 m4 P4 V* n( ]% D./autogen.sh
* A; @- L" W' g. d# M./configure --enable-standalone-module --disable-mlogc
0 t% S: C+ y) |' t6 _  @make
3 {& t7 p3 Y  s三.nginx添加modsecurity模块
5 |5 j6 {  ?0 }) P4 o
$ c9 u  ?3 k8 {3 R) C2 ?5 p5 d在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:; t. e* f  ?% G1 _; R: o+ D' I, n
- @7 i: }  g* c, {9 M( f4 {
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
8 I" K& Y( m3 ]1 B6 i/ y; i4 `1 gmake && make install2 E* O4 k  C4 ?2 G, ]  J) m
四.添加规则+ M- f" s9 T! j( V* a( t
: g* i; }7 A9 d- f
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。; k* s1 Y- ]4 w# r6 p& n
' ]" a* Z5 w3 \- t! m/ Y6 X: l
1.下载OWASP规则:
/ R, F! b8 Q9 }0 b
/ T; f7 b! \5 S) Zgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs+ W" y, p! j" u
5 s5 o: _: Y+ r5 h4 f7 @2 |
mv owasp-modsecurity-crs /opt/tengine/conf/$ E' J) q+ r% a  O4 A6 X$ j

2 j  o+ c. D0 }cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf* Y4 D, S8 l. x. M) D: n
2.启用OWASP规则:
. u$ Y4 \% e/ g
6 M! A  G" g9 b/ f( ~9 `5 C" @* u% D复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
- F# ^9 E7 l) D; [/ \% k
7 Q" Y( t. _. r1 O& a* L6 a编辑modsecurity.conf 文件,将SecRuleEngine设置为 on! z2 r6 u0 B1 M) a/ C5 j, l$ T
6 ~# |6 i6 ^+ S( P; }$ t
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 ]; A- V; h7 w; c: z
. t, Q* L! @: q. n  `4 D$ S
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ B! T, n5 g# E  |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; T' [0 e' t5 }4 Q# j
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf' ^8 Y/ U2 d# m5 g
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf/ S4 u7 O0 p! E  T: Z% o, {
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
* T6 y. y* W) X( t. ?Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf1 b* e4 M+ h2 n+ H1 @9 H% ?
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
/ g( ?3 b! D, x- O& J5 W五.配置nginx% ^- g2 Y% [6 O& X

' G1 _# [" C4 {( a在需要启用modsecurity的主机的location下面加入下面两行即可:5 h5 `, @  v$ ?+ a* n, @. O
4 `+ `  P+ ~" R8 u. n
ModSecurityEnabled on;  
' J) {4 w5 k, H/ y1 \- eModSecurityConfig modsecurity.conf;% X: V; F0 J* L1 t( V. B  T9 B
下面是两个示例配置,php虚拟主机:0 Z7 [4 T- ^+ q" {( @6 w, {

1 I  `4 j# Z9 @& aserver {
# o, H3 ]& h5 u. }' J4 ~( X. G      listen      80;
: O3 a! l- f: W' j% B  z; ]      server_name 52os.net www.52os.net;
+ e) H: U4 R: j6 F9 k& V     
$ r+ l- B" R: |      location ~ \.php$ {( `& M7 z: k! r
      ModSecurityEnabled on;  . f3 O& O/ Z9 m- ?
      ModSecurityConfig modsecurity.conf;
6 Y& \' w% Y. T" V! f
9 n; N. ]) L& y. _      root /web/wordpress;
9 \9 j4 R: @( n      index index.php index.html index.htm;
1 ^6 z9 E( G8 b: g1 `7 Q6 F/ ?& B    R" C5 \. o( ?5 z
      fastcgi_pass   127.0.0.1:9000;
$ s; g3 }( [4 v0 o, _      fastcgi_index  index.php;6 {" l+ J8 \+ W4 F  o
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;: z, k& s. e# K! p" ]* Q6 _
      include        fastcgi_params;
1 b( `# J) }$ J/ H; I1 `      }$ H. J- m. E' l( R, j- J' \# U
  }
0 S7 J. x" l# T# _: Yupstream负载均衡:
1 K  s$ ~7 r% [+ i* |% t& p6 @' q) D5 [1 b$ s
upstream 52os.net {9 O* E. a1 J' ?% j
    server 192.168.1.100:8080;
0 o' C4 Y4 y8 V- T    server 192.168.1.101:8080 backup;
) A& G0 n* w# r& J. s8 s) t' b}0 T1 u9 N6 [; t# P" `, D

' _" T0 e0 z* gserver {. g/ L* u8 r) l( i
listen 80;* J" R- B. P" ]
server_name 52os.net www.52os.net;
) z9 z6 B2 ], f7 M) p
4 b4 \4 O/ a+ ^3 l+ }location / {
+ C1 C( C: b, x6 @) j7 i    ModSecurityEnabled on;  8 I1 ?8 S8 N0 a5 S
    ModSecurityConfig modsecurity.conf;  
  n; R+ z  B& y# M' p$ G, A# \- z$ s8 d0 B! y7 B7 t% I- }
        proxy_pass http://online;7 r0 p  I% f% f) Z+ V! d/ n  ?' f
        proxy_redirect         off;1 O9 q$ z( `8 a# p
        proxy_set_header Host $host;, v+ A+ h2 E- c- g. r9 [0 |) e
        proxy_set_header X-Real-IP $remote_addr;8 D( g  o; y1 v  c% M
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;% i7 S1 ~" Y7 H# E" F" q
    }0 v) H0 C, I: j' F
}. Q  w6 e0 q$ i* c: n
六.测试/ r+ o3 C* i( W5 T/ T
7 d& `: T4 z* [8 t! A
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:% |& }2 Z: N- Q, X/ I
  |, H0 `1 K& B" o' @
<?php" F2 U' o5 f: M( j" m
    phpinfo();   
' O/ M9 Z% u, G7 p/ C! t1 I% n* t?>
$ T/ v  x8 ?# [5 C在浏览器中访问:
+ X& t. i% X, h; [. N9 a+ N; ?
; H4 s9 _$ `& O% h  ?! ahttp://www.52os.net/phpinfo.php?id=1 正常显示。
' j# O: Y& p4 ^1 h0 ohttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。9 S) S6 |$ r7 C  m4 g2 W7 f
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。4 M4 E! j9 D$ R4 B0 \6 y, x' D% T
说明sql注入和xss已经被过滤了
$ U4 Z- C2 ^, G4 G  L
, a# i" ^1 {6 d0 D5 }3 {七、安装过程中排错3 Y3 C, Y( V8 Y; y" h; k, c, I

* ^# y: h% j$ v  p- @2 C- v5 t# _( y7 \1.缺少APXS会报错
& Q( j' _. _% y2 i) T) X3 @* `1 [
' s2 w* D% F* D& `configure: looking for Apache module support via DSO through APXS7 C, i: S  {/ P* E; Q* p7 j' \
configure: error: couldn't find APXS$ b* {. R3 V  S! F7 A+ O5 y5 t
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
/ S+ Y' ]$ ]3 Q8 @2 l解决方法:
* O0 z/ Y$ ]! b
0 |- v, |: |7 u# [! o* [8 Qyum install httpd-devel
3 I; R* M9 E5 A. L2.没有pcre
6 `+ Q: p9 ^- ^9 P+ u6 X* u0 X6 j. x& {7 u! i+ a$ A8 _
configure: *** pcre library not found.
/ h# `6 G- y# C9 C2 U9 zconfigure: error: pcre library is required; H2 B% M3 `2 q2 n
解决方法:
3 Y/ ]' Q5 i1 @; F) E" D2 S: o/ k# L3 s# q, P. j6 M( r* _' f- s6 n
yum install pcre pcre-devel; k1 N+ M* U+ n3 K* V8 j
3.没有libxml2  V% i! F: c7 _. W+ p

' C  R0 j$ u0 K# d2 r! t. X9 Y3 U
" X4 W5 b0 L! A# ~2 H6 J. Xconfigure: *** xml library not found.) H/ `& o' r. M  a$ P# I% z8 G
configure: error: libxml2 is required
7 v* C! t: r# V' f  N解决方法:
% ]+ R6 B6 k+ V% v) x8 Q$ Z4 x, S/ H7 _/ i- E" I# Z6 a. D
yum install  libxml2 libxml2-devel
* ]5 H( y; i+ X$ e/ a- p4.执行 /opt/tengine/sbin/nginx -m 时有警告! p0 F/ k; w# b3 \& Y: c
8 h6 D* n5 b1 E+ r/ a
Tengine version: Tengine/2.1.0 (nginx/1.6.2). g' W: Y" _8 T  |" V, M& u
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!4 k4 w' A% E" I" _. G0 ^7 w  \
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
7 W1 f) L# K4 I6 Y7 A" M7 i) I, n0 u3 |- u5 a, X
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
( o! M; X( `# h! K% d  I9 ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
: p* m' b/ S" j. z! n2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
8 _" q$ o) x7 [$ T4 N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"9 I5 ~- R7 v/ v: j, [6 P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"8 y5 ?# t4 ?! `% Y, f+ t5 c
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
; O1 a1 B) t. A解决方法,移除低版本的APR (1.3.9)
  O! }* ?* r! W6 Q0 U9 N0 D
5 _- o( g0 i; J3 I) \7 Fyum remove apr/ l9 D+ o* J  M
5.Error.log中有: Audit log: Failed to lock global mutex
% L& w; f0 U" B: g9 c* K) B: o) z! r$ P  ?/ e( l/ l3 B* y
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     0 u0 l- z) U' c6 |$ K! j
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]% Q; H+ d( `# \8 r- F/ P
解决方法:
0 F  x) F5 ~; B. _2 r  E9 I编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
, l5 l  V/ r8 d, g' y/ M# C/ ]; O5 A; U* D% m% E
SecAuditLogDirMode 0777
" p5 T. m9 I" S3 M0 Z1 D( eSecAuditLogFileMode 05504 v* I, J/ ^# y" Q
SecAuditLogStorageDir /var/log/modsecurity
  D; r! _4 c: PSecAuditLogType Concurrent+ X$ Q. y; [8 v: Z, K
参考文章:: a; U; g2 z0 a5 e; x
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
7 \$ V5 n2 b' ~" a& x& b9 h& W+ w- Whttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-8 16:52 , Processed in 0.073148 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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