找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11988|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。2 {. l) ~# w7 b* o

9 S; M  t* d. A9 ]+ ]1 ]! i# N一.准备工作
" ?. d0 w8 v0 v* n; f# ~: ~- L- C: Z$ V$ `; {! B4 M3 L
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.07 Z& b0 `* x$ c2 v* w. q

1 p. f+ V" q, R. w/ \tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz7 Z9 ^' o" z7 h4 h

5 M4 l/ m# F7 O5 Rmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz. |1 U6 d( U+ h$ V. y' ?/ }3 }

$ w& Q, x3 ]! ], y& I: iOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: |. {8 M/ [" U) `# }1 i' [' S
) o0 M" o1 D$ {2 @& k0 @依赖关系:
* Y7 _7 B" Y8 w1 z/ h% Utengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
3 ]/ z4 W/ D* ^! J/ b1 ^. a( Y$ n; u% Q& a% k
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel1 p# |4 [( _3 I- Y
modsecurty依赖的包:pcre httpd-devel libxml2 apr
' }; X/ m" b# m! r* o+ I" Y
2 Y2 f; U: J7 a8 v1 `yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
$ \1 _+ _! z2 \0 V8 _. c* V- d二.启用standalone模块并编译& ~) M, m& d5 u5 r

' u6 C1 d$ i; p下载modsecurity for nginx 解压,进入解压后目录执行:/ |' {  h) T8 l9 i  Y0 c
2 g; q  z0 L8 [9 y" \  [. K
./autogen.sh
5 R8 H1 f$ R2 s9 }$ Y- L./configure --enable-standalone-module --disable-mlogc( e. Y- [( D+ g0 Z
make - c1 {) h$ S/ A) ~$ ?1 ?
三.nginx添加modsecurity模块8 f/ B" X  |+ d2 S
3 ?/ y7 n; C4 \+ e
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:; m4 d) F2 i/ B% z# q; N

" Q) {. W; q; K' c+ F" c- z./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine5 g8 B6 G+ S' h+ Z# g1 X1 m. u
make && make install6 S* u; [% v; I
四.添加规则% [5 t7 V5 g) }2 P
0 u" m4 c) ~- i4 F" B. B
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。4 A4 O5 }0 {, V0 y  C4 @
1 q3 x! j* m# N" O7 u# S
1.下载OWASP规则:" F, W7 Y( ~( J, s$ k5 H

! y& a! C+ s2 Zgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs+ u9 w4 y4 ~- Z% B0 g" D! u
! B/ c; P8 h* ^8 z7 u' u& I& Q
mv owasp-modsecurity-crs /opt/tengine/conf/
) U- u/ s0 @$ Q3 D' C3 D* N, |% i
& Y8 F& \& n& ~6 m! c# Ncd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
2 j4 A- g. a! b1 V' C2.启用OWASP规则:/ ]7 ?, N9 y4 u4 s" P
4 f3 B" R. _7 F; V- y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。! p& _- q4 p8 c: e5 B

/ x5 }1 g3 C9 J9 k: |9 ]+ T编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( V. {. s, {% {( h  J
; z% x: i8 q; w6 f: [
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
* g, y/ q: c- r7 F9 y; R/ s/ d/ ^2 D% z- D
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf- V* K8 z8 M+ A2 ?, I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
6 |( j6 v0 H' g& t+ aInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf& u5 d: {$ ^& f# P% K8 G, a
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
7 @, ]' ]% o6 |% G+ e% ZInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf/ g  p6 i6 B3 F- g
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf. @- S) n7 H: L! `! N
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf0 f+ W! f% t7 p1 `3 s+ {) ?
五.配置nginx
$ S4 K! X- t! L9 Y+ o. |& S% B8 Q6 u; H/ N9 g4 w9 L$ c' \
在需要启用modsecurity的主机的location下面加入下面两行即可:9 `( S- a/ T# m* {
. g$ L# H4 W: x3 b# \; L, E
ModSecurityEnabled on;  
" N6 ^+ k1 u7 U( J1 S2 P8 qModSecurityConfig modsecurity.conf;
& t0 p* {) W" |+ r' R6 L* {下面是两个示例配置,php虚拟主机:
% Q' n# i3 f# X+ H! F
% }! @, j' `, I/ v3 k+ lserver {  h0 M3 j: b7 E5 E8 |* [
      listen      80;
5 k9 j, \$ H% l) N" O      server_name 52os.net www.52os.net;# Y& @; {6 z9 W/ u
     1 G9 n  x6 S2 T1 t
      location ~ \.php$ {
3 m' S3 b" q/ N: |      ModSecurityEnabled on;  
% p4 ]4 r/ A; C3 h      ModSecurityConfig modsecurity.conf;  r; {  Q5 q8 r5 w, C
2 c0 e" G& V+ ~
      root /web/wordpress;
1 N! c# [6 `  j+ f8 V) _& `0 @$ C      index index.php index.html index.htm;" i* ^9 ?+ w: q! d& D: Y
  
' ^. b$ ^! g  E- x" F$ V5 v      fastcgi_pass   127.0.0.1:9000;
9 N  O' i. \& }      fastcgi_index  index.php;
& P6 {3 ~# h) ]; b0 N- I* i+ c3 u      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;" G! T( K# l# Z$ H1 j  R2 ]( b
      include        fastcgi_params;
1 d4 ]: e, t& Y      }9 ]+ L) N6 o8 }
  }* ~& v2 M9 R7 B8 j9 t0 {
upstream负载均衡:5 s, }0 z7 u  d+ Q9 t& z" U

2 w7 p8 d! v5 kupstream 52os.net {: y3 E5 s" T2 Y
    server 192.168.1.100:8080;
0 `8 Q) ^- U6 b. v- {    server 192.168.1.101:8080 backup;
6 s! N, f) ~' J$ \}  Q. X& E- L. G5 f8 q0 x
* e6 q5 \9 l* X/ N" ^3 D
server {
: {' e2 [) F2 B  V) Klisten 80;( w0 U( M8 h% L# b0 ?2 m7 U4 V" F1 \
server_name 52os.net www.52os.net;
- t* S4 @, U% r7 c3 d. r) L& Q* H  c2 Q
location / {, I7 N+ g3 E) u
    ModSecurityEnabled on;  
# n$ Q& r, U! v* M# {) W    ModSecurityConfig modsecurity.conf;  3 \- b; }4 {. M3 ?
" \8 h, m" h# q
        proxy_pass http://online;
  s4 D& G5 g( A* s% M+ R        proxy_redirect         off;) M3 w$ q, H# T' L8 e: d
        proxy_set_header Host $host;
; r8 j% n: Y8 }9 F; ]5 z        proxy_set_header X-Real-IP $remote_addr;3 a3 m% q! ^+ _! h/ {2 O$ d9 K
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;4 A2 T' T, D/ B  X$ |5 O* c/ v
    }
, R1 E( g+ h, a}/ y: w1 h+ P  g# [3 m" H. t. K
六.测试' W' E/ |6 I4 y, U6 m3 a; F
6 _+ h- [4 a2 ~; H$ k2 K  l% @/ B
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:$ Z6 x8 }5 Y; C; y2 R/ C
' J% c: c' I- C2 @
<?php
% f( x: Q' ?0 l9 Y9 `, d    phpinfo();    % Z9 [" d" @0 ~; p- T# I
?>
* X5 Q* h/ {% ]9 ~( V在浏览器中访问:
% Z1 K+ ?$ P- F
5 ~6 H* a1 x" x8 Y$ Y/ T4 ihttp://www.52os.net/phpinfo.php?id=1 正常显示。
: U6 Z0 L8 Y% z  Y+ Qhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
+ {# L! f8 I' [) ?http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。; W2 r* Z) }) t6 Y: f8 z
说明sql注入和xss已经被过滤了
- \! A! \$ Z7 {& P% n& v- o4 a5 W: v
七、安装过程中排错8 ^+ c7 q. k. F& T

* P0 f4 [& q7 I( P6 L) j7 E% Q1.缺少APXS会报错% ]! J+ X4 F- f3 H+ h

4 |! ?1 ]8 Y  e' i  D4 W8 Dconfigure: looking for Apache module support via DSO through APXS
" I5 M7 g& x! f8 R" J9 Cconfigure: error: couldn't find APXS$ ~. p8 B8 o' B# O
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
+ [% N2 J( e  c& z( A解决方法:
/ @# @# q7 z3 }
  W; u2 n9 ]2 N5 ?* E4 N2 Hyum install httpd-devel5 `! k1 D* Y. e) u; r+ e
2.没有pcre. m  d: L1 z1 y0 G5 e8 B9 q

& l/ a3 }" c/ r4 G& K  |configure: *** pcre library not found.
* |2 R# V4 h5 \# X9 Wconfigure: error: pcre library is required* e- r3 W. ?6 V
解决方法:
/ G% j) M3 Y! x% c7 P8 r( x
9 E& \0 W8 K# Z6 L7 |& fyum install pcre pcre-devel* |6 d/ p7 d" T3 \" V
3.没有libxml2
4 v& b  G/ c* k0 |
' u2 p6 Q- T5 @! s% ]
% g4 B/ t# n# {configure: *** xml library not found.
8 p! X& E2 E1 H$ H7 Nconfigure: error: libxml2 is required
5 j3 u# A4 q; G& }/ q3 i8 A. j9 A解决方法:
) O  R3 y; q6 Y; z3 e) p7 P" Q. j- e3 a' Z6 a& ~, W/ r
yum install  libxml2 libxml2-devel
4 Q) X& P% h3 |1 U/ i4.执行 /opt/tengine/sbin/nginx -m 时有警告0 O( J% w: R. u- R9 i6 E# z. v

- Z. o7 m; N- H3 }Tengine version: Tengine/2.1.0 (nginx/1.6.2)2 |4 J- e$ Y: L- B% G7 F0 S* j3 d" Q
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
. ~2 U( f) ^2 N8 V9 `原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log! w4 `( Z( n6 L) ?# r2 d

8 f  y* U6 e; l- g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
2 }! V! @0 J; x: @' S8 l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9": f( _$ ]8 [; w! H2 t1 L3 d/ O. G
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
/ m/ b, D* Q/ C6 U7 \4 {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
, P- M* Z* V+ [9 x! o0 ]2 G4 e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
3 d$ Y  N2 S: w: r0 b2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
8 b) v3 v. n3 s5 j. T) l解决方法,移除低版本的APR (1.3.9)
6 t) \# p" }9 H* Z
9 j0 j" J7 ^/ h& lyum remove apr
0 \5 {$ z; w' d4 u$ r9 M9 E/ D0 g5.Error.log中有: Audit log: Failed to lock global mutex
# v6 ?' [! B3 f+ c7 l) A9 g- U; r$ g+ F5 H+ |: t' d  A
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
3 Z* }% p3 [/ l# q' w5 k$ c) fglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
+ D! v; i9 r& T解决方法:
6 I9 @/ U" j, p编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:) H5 p0 Q9 {7 m2 O
5 v. e* F8 F3 W2 X: A
SecAuditLogDirMode 0777
9 }: x8 v0 Z. l, W5 |1 PSecAuditLogFileMode 0550
# |' v* f7 B/ m; WSecAuditLogStorageDir /var/log/modsecurity% `) e8 B" m% S& }* c: i; b7 }1 ^5 Q+ V2 t
SecAuditLogType Concurrent. f0 {) o( b! n6 H2 k2 g
参考文章:
! _; ], }! d" V/ Phttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX7 g: y' @1 T* I
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-1 11:21 , Processed in 0.072443 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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