找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12880|回复: 0

nginx配合modsecurity实现WAF功能

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

. l) N7 I0 t+ C6 D/ N一.准备工作. ^3 E* Q# A0 p# `) P) X3 C. n

: B; {+ I) ~( ?" Y0 c系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0; F  i2 S( p( l7 _/ r

+ @. [- a3 @& |7 V, X$ ztengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz! b* D% w  A+ ?  q$ G0 ]( A0 V
6 f1 b2 x# F* }, s8 D
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz  W# T/ c; y& i: [. m! Y
- O' l9 D- H% ?  @* S. l
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
. l& y( m. c! x3 f+ w
- ~: u/ A. H: p依赖关系:
* h1 O- R( B5 ?- l, Otengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
3 V8 v  L% ^6 ?$ M! s+ u  `' E: i1 K
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel  E+ ?3 r" n9 d  d( Z+ R
modsecurty依赖的包:pcre httpd-devel libxml2 apr
+ n. t4 V- w( V+ ^: @) B) m1 U) Y  y8 M  d& V* L
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel( p: w/ e$ a% ?" g8 _9 Y
二.启用standalone模块并编译, b+ Y3 @  t3 J/ Q8 S5 |, D4 w

) o+ s6 d! ]: s; H  b下载modsecurity for nginx 解压,进入解压后目录执行:* a7 L8 n# f5 r4 |

- c- A+ k, j0 g# k! e./autogen.sh, }/ Q: F& N0 N. w* z; N
./configure --enable-standalone-module --disable-mlogc
$ s1 j; j; x- @% W) jmake   W  N6 m8 O. T4 x1 `9 j" F4 I
三.nginx添加modsecurity模块
! `$ ~7 t( L& b5 g1 `, w8 K4 e# d- a0 |. B
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
* o' F% s) j, `
; U7 Z  P& _+ V, h& j& U/ G8 N& o* m./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine+ X8 ]  X1 S7 Y: Z7 c6 G0 a; \: [
make && make install$ C$ C$ L' |: n- }2 c
四.添加规则
% e5 f, N2 M# |; G6 b1 P8 C( R7 W- G
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
* o2 z" c; g6 o# g* S
) L( I' M3 I9 }) a1.下载OWASP规则:
5 @( Q7 _% A! g
& E! O& n& ]5 Z, _) agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs' |& P2 [7 I$ N9 v0 S

; l+ Q3 W6 {4 _. d3 k/ h9 umv owasp-modsecurity-crs /opt/tengine/conf/* v% u, Y9 C4 N

+ L$ S9 x& `5 kcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 m5 ^5 M8 E% i2 S. e2.启用OWASP规则:
* H# T/ {, m0 e0 h% ]# g0 o$ k- M/ V& y. @
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。) i5 W; |: k# x( ]3 a6 X; B

/ e& e+ {! g) m; I编辑modsecurity.conf 文件,将SecRuleEngine设置为 on4 h0 c( V; U+ [9 I# x3 M& ]

3 J8 m- k& j% [* aowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。. L7 A$ }, E/ w6 ?$ y7 w; Y- p
' p( j7 A/ r; V( z) B* Q& ?
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
: L2 x, F; \. O1 a: LInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf6 Y! n# @. }" B  ~
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
! M7 [; l  Y7 B  }Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
! Y2 \  c' l, \6 N! QInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 n& ~2 f& H( y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
. K. b* x' u* q7 FInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
' \9 F/ s" u6 c2 W' `五.配置nginx: l/ T+ J) ?: j: N: h
; A. J- P  L" E* {0 V  ^
在需要启用modsecurity的主机的location下面加入下面两行即可:
$ z& H% n: b6 V+ ?5 P- ^4 o# H' V5 d  C- \( g6 h* b& {+ f; O9 H! @- c. G" @( @
ModSecurityEnabled on;  ! ]7 L7 ?6 k9 b
ModSecurityConfig modsecurity.conf;
5 f0 N( _8 H- m* O1 ]8 k下面是两个示例配置,php虚拟主机:; N' }( T+ R7 a' o
3 W. E, S0 e; Z. N$ g0 O
server {0 w1 p' `2 ]$ t) t" ^* z
      listen      80;: q5 [+ S- a& T
      server_name 52os.net www.52os.net;' U, k" ^5 Z, r1 D9 y9 E
     
  z2 R0 n( }; ]      location ~ \.php$ {& E" y/ k0 y7 ~# N9 i7 ?
      ModSecurityEnabled on;  : B& z4 ~9 i7 v$ P
      ModSecurityConfig modsecurity.conf;6 M( _# C/ e0 P

/ d1 `: I3 k9 T" K$ U      root /web/wordpress;- v- E1 A! f8 c" T5 _! u
      index index.php index.html index.htm;8 \, r- l1 e$ z' \/ ?3 ]
  
5 E/ v/ x" l* e      fastcgi_pass   127.0.0.1:9000;
( n" i& L/ F5 r      fastcgi_index  index.php;% E1 W) h% m( J( M
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
/ w+ E4 u1 t0 n4 z      include        fastcgi_params;
3 a9 w* J8 s+ w7 w% q5 P2 l7 q      }9 A% {1 @( O( D6 G  g
  }7 G( a5 p+ }; r9 w- |0 b
upstream负载均衡:
0 Z$ G% i8 y% d; r" H/ d9 H6 P5 H
upstream 52os.net {3 l6 F$ j* m3 M  n: J" \5 k# |
    server 192.168.1.100:8080;' ~0 [* t, J2 z5 w3 k$ n6 A) g+ f
    server 192.168.1.101:8080 backup;# p9 k' h) I9 ~% y4 d( V
}! M% N# @  Y9 A) d; f$ j
0 s0 |* o# N/ M: B8 M3 }0 W0 W
server {
* M, X2 |) x5 }5 j9 n- {listen 80;6 k0 W1 c; _8 W/ R. w
server_name 52os.net www.52os.net;$ g2 N$ {+ s+ }/ @$ A. J
- v# G  y' n4 [% s( v2 L
location / {) J# c8 X; B& o! P
    ModSecurityEnabled on;  9 D" |8 {0 x; F" E
    ModSecurityConfig modsecurity.conf;  
& A; O& D' ~% R% g2 R+ T; w3 |- R6 v4 I, k6 W
        proxy_pass http://online;
4 q3 {1 j" g- {4 x/ ~- [4 D! x        proxy_redirect         off;
3 f2 i( \3 }8 O  r5 W        proxy_set_header Host $host;
. \8 b$ I7 X1 a1 y        proxy_set_header X-Real-IP $remote_addr;, }( N/ S5 m9 r: r8 m
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;% j, `8 H1 b9 M$ e1 y' r$ a
    }
+ r6 B/ X: z: S6 b8 _}! s1 _5 @* d$ m3 B* x% C
六.测试
. G& s* j7 i$ g9 K+ `$ L$ G
, ^* _0 @$ s$ h& f3 z我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
8 B( j6 E/ o: A9 C
+ ~. V! n; O% k/ A* Z<?php! J; W: K7 d$ B! @- u
    phpinfo();   
3 K! z7 s- ?1 |' p+ t?>
2 P* k; ~. p, O1 {! ^在浏览器中访问:0 V/ ^; i  e6 a' A$ M6 n  {

4 O, G, J- Q$ l- ahttp://www.52os.net/phpinfo.php?id=1 正常显示。* s5 i+ v7 x6 I+ ~) d' c
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
) X. T6 H1 V' g7 J6 A3 a4 s. ]# Dhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。) i2 S! v) T, ?% X! l) a4 d
说明sql注入和xss已经被过滤了1 _1 O* T* B2 {4 E

. k4 H( E( K/ e0 n七、安装过程中排错/ ?6 T: `0 i0 Z0 q
: E# B; |+ |- A2 ?3 \
1.缺少APXS会报错
( N1 G4 p2 G; g2 X/ S9 R8 _; L
5 c2 {* X$ Y. p4 [7 \" a/ t4 sconfigure: looking for Apache module support via DSO through APXS1 X9 S- N+ ]- \
configure: error: couldn't find APXS
0 d& o0 r# c" Z  `7 U0 Vapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
5 D' x, o" X" L7 C5 w4 r解决方法:* }/ e: q( C6 f' h7 `; N
/ d' @  l- Y& }& |. S/ p% [
yum install httpd-devel
/ g( C, t9 j$ h7 l2.没有pcre3 }) x! f/ ?3 {" b

. p- S% \! G% Z; l3 j- uconfigure: *** pcre library not found.
4 A, P* T: M6 f' k' Y  uconfigure: error: pcre library is required
* I$ V$ h) d" p/ A% y6 V: c解决方法:
* o+ V6 a% U; p" c1 i! G
2 q4 U% ]. T3 Kyum install pcre pcre-devel8 U/ G+ r+ r/ t9 U& f% u
3.没有libxml2( K7 l$ |& S, j8 v

; @5 X3 i, T5 \! p6 r, u, F9 D1 v2 m+ A0 G7 }: F
configure: *** xml library not found.
* s- t% M3 G4 D* ]. _5 |3 vconfigure: error: libxml2 is required. K$ \3 j/ s7 N+ o% q
解决方法:' I" g: ~, F( j: |) a- M* S
; _* C; c/ N) _' W
yum install  libxml2 libxml2-devel5 a# N/ u6 Z$ j) T4 [' ^" P/ j
4.执行 /opt/tengine/sbin/nginx -m 时有警告8 f" m: y" z9 G3 V! `# H
) w" [+ K' m* Y& p) E" J$ ^
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
* j- _0 m  \! u5 J) ^nginx: [warn] ModSecurity: Loaded APR do not match with compiled!& o- [6 W: O  o1 z
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log! y$ m, b/ b. j, q

# H0 R4 |# \2 U3 S$ m# a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.7 [" J) j% N$ A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
/ f1 C0 U! ~, ]' p8 U% M2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
) Z, [& d7 M/ t4 A4 \, R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"/ ^' U- s: K# y5 ^0 F" _* r/ ], F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6", c5 v( P4 L1 Y/ s0 \9 [1 x
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
" D0 a& c3 C6 P2 l3 M5 c; y解决方法,移除低版本的APR (1.3.9)
$ c( J$ v8 e2 Z
" O; C  \. R+ K2 p- m0 ]& hyum remove apr5 s0 P* P$ W' ^
5.Error.log中有: Audit log: Failed to lock global mutex; w; c5 z6 b! _1 u( k: v- F6 Z

- p" h$ h; M+ k( U' w2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
7 p( _9 ^4 L: R6 {global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
  J, E" K# {) ?& @7 V解决方法:$ \' C' Y% P# l
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( B/ V, U3 T5 E  B, K; B' K- O1 a- ]; [" V, E  W  h% j
SecAuditLogDirMode 0777
3 |5 c: E) j, K2 i- \SecAuditLogFileMode 0550
. t/ s# t3 ^' Y0 `! X- ASecAuditLogStorageDir /var/log/modsecurity3 S& {. h( M; C5 u; @7 P. y, v; O
SecAuditLogType Concurrent
6 L# Q$ X4 S* [; x参考文章:
, g8 d, @  e8 \( N: ahttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX7 x' |( Y3 k; v( C1 |& |; w
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-21 10:51 , Processed in 0.064950 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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