找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10298|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
( _. S( ^9 Z8 C% U' b" A+ h5 f, }1 Q6 L
一.准备工作
- T' I3 Q) b2 S  W; A/ H8 i& g! ^. _5 p* [$ O9 P; ^2 O% Y& j3 `
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
: b, z3 I1 n- |1 g9 @
0 I! Y" l( ]5 {4 O6 H3 b0 i" ^tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" z6 v' O) p# T4 U' P# \( ?# i' ~$ m
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 e& |# u* m) h* W5 a3 S9 Y

3 o# s" H8 U/ T, C: S/ m# pOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs0 V6 h3 r1 S9 H' s1 x1 o9 c7 V

( w8 b6 o; F- v0 o/ I# G7 D依赖关系:
+ D( n- X' Z7 F* n! xtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:% G3 K, A: P; D5 m! z2 N- }$ `

$ z5 t) ~% v% J4 j9 Y( m+ ~& Qyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
, ~+ J$ l# i. u5 [# K4 ?- f, Mmodsecurty依赖的包:pcre httpd-devel libxml2 apr: i9 \" j" A* o( v

1 A  g# s" I1 i' O, Gyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
6 S( w# j9 X" M2 b  d二.启用standalone模块并编译+ Z9 t3 J4 y+ C2 g3 [

6 ^% E; w8 A" `% K  Z下载modsecurity for nginx 解压,进入解压后目录执行:+ P) _' z5 G1 b& F+ R2 T. j+ {

8 G$ g9 \! {# i9 E' p% Q7 U./autogen.sh" \* d3 u" b1 {% [3 Q
./configure --enable-standalone-module --disable-mlogc
6 Y  u! t- v# ]4 }1 Xmake   j, j$ r6 h* D1 d- L/ p
三.nginx添加modsecurity模块
5 l2 p; o+ f3 m" Z& N
8 X% i& k, c0 R$ A0 q9 c7 i/ Y在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:" _# {+ j" ~6 s( Z' o' O- ?

$ |2 _3 H# s8 H3 W* P./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
9 a- m7 g8 f) }4 E9 |( R3 A5 H5 Nmake && make install9 p2 Y: [0 P) I
四.添加规则  |# m& t3 d' h+ R, M/ @, u

: U  e6 F, _3 _. Kmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
2 }# A  J" R- }1 G% v" o
3 T6 d( ?2 |6 h; E! p4 b+ b( c1.下载OWASP规则:
. A9 T9 J  ~% K9 o0 ^+ U
) ?. p4 A3 V. Ugit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
* c$ R- X1 U  U! @) W" p
$ `3 }+ `& M" e: j1 }! emv owasp-modsecurity-crs /opt/tengine/conf/! S! ^" j" ?: X0 {2 @: {" n
2 \! C  T& n$ a4 I
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf5 [3 o+ d9 e1 R) c- h
2.启用OWASP规则:
: B9 q1 H) y. o& l. S8 J) ]# Z+ k+ u0 Y: @0 x, G. R( h
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
$ d4 _# U; V# F
5 J" a1 X( A1 }% R编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
1 r/ D( l9 }: {, J2 n6 S6 M0 C' l
4 p. _, Z# N5 Y) f# t; N: {; X# Jowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
4 B, `- A  L5 I- Q. J5 {+ _# f- I* H' t4 E& O/ K
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf5 f. T6 F& |' I& D. j" G
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf+ ^! O/ N' N$ q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf7 K4 a- l1 `/ |  i0 O( S, p5 ^! C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf8 Z) t* i' [9 Q3 G7 i$ C5 f
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf+ v9 T2 {, u1 ~4 E. A: u0 w
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
' u; J/ u# l" YInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
' f$ l! t  E0 E+ S4 n/ o9 ~) B8 z: I五.配置nginx
/ A0 P1 C$ h9 B, M  N6 x; E1 R( ^1 F5 E' ]9 s
在需要启用modsecurity的主机的location下面加入下面两行即可:
. s& C% I+ B  Y& Z; r3 Q
; H1 v) u7 ]; NModSecurityEnabled on;  
- @" P4 A; |/ u% G' W3 c" lModSecurityConfig modsecurity.conf;" F# J8 @7 ?6 e" c$ ~9 S
下面是两个示例配置,php虚拟主机:
$ V1 V' }$ C) C$ M& ?% w3 O
; Z, W6 U1 M' U: P' A1 D7 Xserver {8 B, u* o9 @) r) ^" }' v" O
      listen      80;7 o! [  D0 h$ O) U9 t* e
      server_name 52os.net www.52os.net;1 B* A9 J0 J: j' J- F4 t* d
     0 n; G" v# ]: \4 l/ T' \
      location ~ \.php$ {1 A% z  H' T' N/ v  H
      ModSecurityEnabled on;  
9 s& e6 w/ J8 x0 }* n      ModSecurityConfig modsecurity.conf;
2 f  j! ~& U' C0 ?$ C6 {- s- X% A+ m5 j% j: V1 d# Q7 B  x
      root /web/wordpress;& g  e' F% i9 F) j. K; B; m& S
      index index.php index.html index.htm;
! G5 a6 G( Y) `1 _  # {. q4 L" g: u; x7 ^! {
      fastcgi_pass   127.0.0.1:9000;
( j: b" I- e) y2 D9 e" Q      fastcgi_index  index.php;
' Z  L! o' e- z' J: |7 g      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;& ?* ~- d& }! g6 k- Q
      include        fastcgi_params;+ x- Y  ?/ W0 A: G
      }
; r1 h3 a9 V3 y! `8 Q, C  }
; I- {7 ]9 p, R: |, S# Q6 b9 Eupstream负载均衡:  o& B/ U/ L& N9 s0 p5 b

) S1 J! z7 M: @. y5 e) |" f5 vupstream 52os.net {
# A2 q; D; H& X) y9 \% G6 P" y    server 192.168.1.100:8080;) {' ]) }; S- X# F$ ^" u0 O
    server 192.168.1.101:8080 backup;
  J, h. d: m! f$ _  |/ j}8 X" }. D; D- N0 E
) U5 Q' J% @, }/ P* T
server {% Q1 ^8 ^  ~9 x- t
listen 80;0 E* U& @$ m. C- f
server_name 52os.net www.52os.net;
8 K. U6 z7 Q: g9 Z9 @& G* ~" n0 i7 @, P$ P) \0 m9 R) a' E9 F' h
location / {
0 o: G/ s1 M! ?9 `    ModSecurityEnabled on;  $ B( Q4 O, z6 T' }4 ^
    ModSecurityConfig modsecurity.conf;  
  S- Q) r, N1 e$ ?/ \7 E# A0 X& ?" P6 l; o- g6 U$ f1 v5 n( S
        proxy_pass http://online;
2 Y! P  J$ u3 w& _( T1 r# c        proxy_redirect         off;
9 P, E0 i+ c4 d( F0 r5 o/ H        proxy_set_header Host $host;
' {+ n- h1 {4 Y- l6 m) O% @3 f        proxy_set_header X-Real-IP $remote_addr;
) i1 P' y( \7 ]2 y/ U        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;1 C8 F7 M6 a" H5 Q4 v: Y  ^0 r( i
    }/ a) x* J  R- M
}4 M  K: P& t0 M" K
六.测试
) n" ?9 M- M/ g2 C
" J% i8 |  b, f7 O5 D9 M0 U我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:9 m3 h: e! X! j
4 `$ V( l) F; w5 W9 Q
<?php
  Y: w2 X6 V8 X/ T6 Q$ ]    phpinfo();    2 u4 N. l1 J0 ~1 |8 {* v
?>
/ H% Q; i: K9 ^2 t  G6 Y- Z) R8 c在浏览器中访问:
+ V- D& k. l/ e# c% ?
3 g7 j0 k5 S2 @. ^9 u; Y: ]2 j* fhttp://www.52os.net/phpinfo.php?id=1 正常显示。
! ~8 r3 s# a& B2 h! \http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。$ J0 X4 l& e& ]7 `& @" ^" J  H
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。5 j8 a5 C+ G( h$ C2 h( @8 ~2 ^$ o
说明sql注入和xss已经被过滤了% B: U* J. r2 F. H% X4 [
2 G, l9 ?4 k1 s. I! X
七、安装过程中排错# L6 W) G3 L* T6 q- L% H( j

+ w9 Q$ S6 z  `! b8 r3 d9 ^! W* `1.缺少APXS会报错
5 B, k/ p- \- N/ v# B: r0 Z0 m4 C7 d0 M; o& }. a% b7 t
configure: looking for Apache module support via DSO through APXS
1 \& G9 J  b+ w8 \1 a# e, Gconfigure: error: couldn't find APXS. s  V/ V  R. `4 i: q+ E
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& u% X5 b1 a! S( ]
解决方法:
4 o0 U  y4 g' \+ _: s- v( n- I; f/ C7 V, @2 C% P2 n+ D
yum install httpd-devel$ U( L3 M! F: d/ \: ^
2.没有pcre
7 a- H: L+ r' o: t6 K' Z
% C+ w. U* V0 hconfigure: *** pcre library not found.
3 ]2 {  K0 N5 W# d- O- |' cconfigure: error: pcre library is required! z/ u. @: A: R5 M7 i* O
解决方法:
0 w9 i2 k1 A5 o) L( V" L
) h; s' t5 M- H2 m  t- Fyum install pcre pcre-devel: J& C" |6 X8 P! ^5 \1 K9 x
3.没有libxml2
0 r/ a& L7 y' r& S% I0 G7 H
& j* m" ?0 J  G/ A
, S* _0 t- P, Rconfigure: *** xml library not found.
( E4 Q. Y4 H8 n+ V; hconfigure: error: libxml2 is required
( `/ r8 r5 X  y4 M" c: y  Y解决方法:% P$ u& e0 R, C' B

/ V1 {2 f' N! v6 Z. z& C# Fyum install  libxml2 libxml2-devel+ f$ l# {* i& u, k" S
4.执行 /opt/tengine/sbin/nginx -m 时有警告
# ?5 w+ c5 I* S& z/ L/ g/ P6 u- B* y. x, }6 O2 @) v7 @$ C
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
* D' r) m, M/ B; b1 Ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!
! a4 M6 P- g; v" ]原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( E2 {( x! T; h, g6 T+ E9 t1 ?
: D+ l9 R( q* B" Y4 `2 k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
. E- w4 @3 V3 \" i7 \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"8 R2 h! g7 E! {
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; T4 C; `" z5 r7 W4 G- H4 ?3 u+ B
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
0 l3 x1 p: |! y) e! D& z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"( ^; c5 D8 d6 \/ l, U$ t0 a2 f& |$ y5 i
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.; W3 e' K% H3 l8 c5 U/ D8 v. A
解决方法,移除低版本的APR (1.3.9)
" I+ b3 h, s: u  q3 b& N! g
4 ]; J( @( U. Pyum remove apr
7 X0 {3 i5 E/ q( o4 J2 l/ H2 B5.Error.log中有: Audit log: Failed to lock global mutex
% s0 s6 h$ k/ h% |0 x8 q8 f: Z9 |3 F" S' n, }5 e4 [0 K8 i6 K
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
9 C/ {& Y* A6 B3 H8 \  {2 B9 Gglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
4 c- G, E1 a1 U, H3 y. \' b7 S* L解决方法:  @' f! }' q2 n8 |  g  p+ g8 g
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ a6 v+ e7 L4 e3 A8 o5 {  U
8 I: ?; e6 U8 F5 SSecAuditLogDirMode 0777
; S' T; g: a8 L" J8 e# lSecAuditLogFileMode 0550
, t, U1 j3 e2 b) b( f. KSecAuditLogStorageDir /var/log/modsecurity
, l- b0 P( }! b' N' qSecAuditLogType Concurrent) K! W3 Q& e! S. t0 c/ u
参考文章:' u1 C9 I7 T2 G( C
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" C3 a. x: U1 Q8 T# T
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-3-20 03:40 , Processed in 0.154656 second(s), 30 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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