找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12300|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
) E3 ]: Z  F% f: I* T* [" q& y
7 `7 m4 n- q0 a  N9 a# [一.准备工作
2 U0 ^9 h( z; s* D( b( l. h/ Z, @: e# L+ t& e6 N) g! r3 v; d
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
( @+ t% p1 F6 D8 m* y8 q# G8 ~. O5 p& r0 `- Q
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* K" v% p7 T& ?/ M- N3 G# E* q' Q/ r3 e* m1 _6 e0 ]* ?
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
, b+ ^0 E( [% d. @
- }" R" f# x- C3 N7 qOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs$ |7 s& W* h) l: ^9 P) ^. t
! [- o# ]5 U% q$ l
依赖关系:' u1 `' G/ F& _2 w  D" t
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
1 q" K1 b( c6 Q+ C! v, ^0 w6 s& @5 q! W. D2 q( F3 x# w6 Y5 P2 O
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
9 [8 S$ U/ A: H3 B% y0 vmodsecurty依赖的包:pcre httpd-devel libxml2 apr0 l% E! u; q" j$ u: _9 t3 x
3 O+ i3 ]- ^3 v2 c
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
+ y  F' F# O2 J( _# U' H# \二.启用standalone模块并编译2 Z; P5 d$ P) E/ Z! @; i- D8 r1 X1 T* j
- i9 D6 v! c0 U
下载modsecurity for nginx 解压,进入解压后目录执行:
& A7 s. T% F$ p1 q) _5 t6 ?! t4 K. ?1 ?4 X
./autogen.sh
. u0 a5 ]3 z9 \* Q- n1 [./configure --enable-standalone-module --disable-mlogc* q0 M; U' x& T
make
+ M$ O- h4 A# E6 {! b三.nginx添加modsecurity模块9 z. V: d0 t, j2 q. E6 L! _: a

9 M; q( x$ C! U& h在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
5 [9 }  B) t" r: g$ Z' K
: U* c* _2 t! T) i# D./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
9 B4 U; a; x% p! W: L# gmake && make install6 M4 V- y0 O7 V* k6 V2 A) `5 b6 X
四.添加规则
; S! o0 V  w- z+ G, h
' q& ]' q- b: Dmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
/ S, m$ r& I6 Z+ y/ y' X
( U' O, [" ?4 F# C2 E( ~5 w1.下载OWASP规则:
: n& G) j+ q6 O, Q: _
7 N& d* p: V  [' Ggit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ `; ~$ A5 U, a! |, C# O5 f) }6 K, I
mv owasp-modsecurity-crs /opt/tengine/conf/
9 {# Z6 X7 P( f9 S) Y/ `5 {6 K$ V6 ?8 F. O
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; z& W" {& |, m% B$ s; x2.启用OWASP规则:. f! Y( B& A, A: x
0 }) n- X! z) E' V! k0 B* H1 J! S
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
+ y! k4 m( d5 F/ V: A+ G% T9 G$ q+ g" J& U0 E
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( I2 X' D) a3 R  d6 ^- e- I  D

  F% R- H* T# d/ a) V6 ?2 q8 bowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' z* S+ V3 b; y
0 ~2 q( C$ ~! @! O) s4 SInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
) Z$ v; P9 n7 S( [& R# D* rInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf% w+ {! O8 `4 P% U! \+ v6 j
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf3 ]2 ~& j4 {; h: p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf: x+ w- [+ u0 H% R& j0 n, O' D1 M
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf$ e' u$ Z" g3 `! {0 r9 ^
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 r5 C1 K$ E  h& z1 H7 o6 \+ ]Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
3 B5 t  W+ Y9 `5 ?1 y" v+ K* o五.配置nginx
8 j9 Y6 w- _$ w+ b" W
- {+ ?# s& T* G" S: `在需要启用modsecurity的主机的location下面加入下面两行即可:5 r" D5 F4 `  Q. @; H$ y) @- o9 W
& m2 t1 P) @! U4 M4 I
ModSecurityEnabled on;  
  G4 q- E/ {/ DModSecurityConfig modsecurity.conf;
( n  G4 z# u1 }$ W下面是两个示例配置,php虚拟主机:
; ]* a+ a* \8 Q+ e1 y
$ n5 ~( T. H- S6 v. r4 s" aserver {
/ [* [* T* I" C! e2 ^      listen      80;
* S3 b6 A8 Z3 `9 t      server_name 52os.net www.52os.net;/ j& G4 ^$ C, u# T+ p; p
     
2 U0 v5 y4 i* E1 [, l      location ~ \.php$ {; L: O: ]  K7 ^
      ModSecurityEnabled on;  
! ]( E( U, _) n/ W/ z0 \      ModSecurityConfig modsecurity.conf;
5 j) i; L5 l. V/ {
5 p3 ?: ^( {+ B4 d. Y" f      root /web/wordpress;( Z5 B1 ^* E4 a7 |$ G# I
      index index.php index.html index.htm;
0 y' N, [, n" m7 W+ H* v  ; I" [' I. h* g* `: M! v9 U
      fastcgi_pass   127.0.0.1:9000;5 e) I! B7 a6 s
      fastcgi_index  index.php;
- \0 z" y+ ]4 F( [( u      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
4 A$ a* {2 Z1 ]9 y2 Q( B6 j) G      include        fastcgi_params;
7 u# }# k6 j$ v) Z. {+ t- O      }
% J/ v9 O: A7 s: x8 y- p5 X  }
9 Z% j  {* r* X, C. zupstream负载均衡:1 ]* Q& n1 v( M! ^! m: V3 [" |
+ s# _% r, R, t2 t3 ?
upstream 52os.net {3 g( y* H! t" J! i3 d2 b! b+ l
    server 192.168.1.100:8080;. o: u! w# ?) d/ L* }$ z" B4 e$ C
    server 192.168.1.101:8080 backup;& ]( C  H3 K# T$ {% v5 ?
}' L2 Y# {) f3 X" ]* w- F  U/ Z
, q. @/ Q  s/ j5 m8 ~- H
server {
: `( `* u+ _* F3 T' Elisten 80;
- O0 _2 _2 O9 `server_name 52os.net www.52os.net;$ |1 s8 T& N  D. ]3 \
' d" [* m6 Y- `- E9 R! x4 t& g' h
location / {) J5 ~' T& H" ?8 |' _* E
    ModSecurityEnabled on;  ! S8 c. u: K! ~2 M$ \
    ModSecurityConfig modsecurity.conf;  
: y! C% X+ R8 ]6 w" ~7 E# T' b: J* l5 U% O% n' b5 B  T7 N
        proxy_pass http://online;
6 P6 f# ]3 N+ A! d; G2 V! p% h, _% s        proxy_redirect         off;
7 E# w* u9 j, F4 t0 h7 x4 |: ~        proxy_set_header Host $host;: b0 P* T  X  l7 m9 K3 H0 ^3 P
        proxy_set_header X-Real-IP $remote_addr;
( B% H* n+ A& U' H6 ?        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;" g4 T# P+ H: X& Q; I9 d
    }
& {7 e/ n9 l- v  a$ ]7 E}
8 ?7 r. N! t; H: q  W- P六.测试
# k; z! ?" `+ }# W& Z, W* r6 ?( z
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# M) J+ T: b$ [! B' i# ]: k

# g; X9 b, i5 x<?php/ t* Y, A$ |* @. F+ F$ z$ D
    phpinfo();    ' M  b6 ]2 u7 p
?>- y4 S- s  g" e9 m0 l  u: t
在浏览器中访问:1 C+ z# ^8 p" n2 i, a

& b, J" h$ S" N& V. y+ {http://www.52os.net/phpinfo.php?id=1 正常显示。
0 P! _0 X8 S8 g$ k; L3 qhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
: q) b3 V+ j, `' F* {. J) I0 N" uhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。/ l: I9 ~4 g( q- q1 N
说明sql注入和xss已经被过滤了
% d- v1 h5 w! C( q  E! @; U& k4 G
七、安装过程中排错
! i2 I  \( P4 J" E3 K2 c
( q/ I& I& e) i: z/ M$ U1.缺少APXS会报错
5 Z3 P; d7 ?2 b# G6 J
+ R7 S, l. w* q1 v7 }configure: looking for Apache module support via DSO through APXS4 y, Y% \, H2 p7 ]
configure: error: couldn't find APXS
5 \9 h# S7 m. u- rapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。1 G9 ?9 `9 ^/ ^* Z$ Q* `
解决方法:
) c2 w( {+ G! b$ C! S8 Y) q/ z8 `$ a# P
yum install httpd-devel  C* b! I/ s# V' X) V$ E
2.没有pcre
4 p3 ~" C4 C5 H6 t( o& K# ^7 n& s( j' `7 ^# {6 ~3 f
configure: *** pcre library not found.
- Y" w* Z; j, qconfigure: error: pcre library is required
7 g) ?* g) n, t7 v解决方法:" h( L# Y1 `9 R: `

/ n$ N# O! A, P* s/ v/ Qyum install pcre pcre-devel7 t5 j* g1 t  p' N
3.没有libxml2) b( _" ~8 ~6 v, T: u4 ^
) T+ P" ~# v; @+ w, l

5 x3 C7 Q! e2 h. ~! Pconfigure: *** xml library not found.
/ E; w- N  @2 ]3 \configure: error: libxml2 is required
0 K4 p3 C# V4 O; @& h; @解决方法:7 v! I0 S) J1 d

' @+ p0 a* A+ P5 n' X$ h- T" c5 O! Zyum install  libxml2 libxml2-devel* j& r$ {  c8 y9 w' p7 @* p  g
4.执行 /opt/tengine/sbin/nginx -m 时有警告
' i; i4 I2 o# u4 A8 J+ x' o
: k! A4 m( j& d( n  C5 l! J3 BTengine version: Tengine/2.1.0 (nginx/1.6.2); C* V9 f3 h; f8 ~" p+ O! D2 B
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!1 D* v% r- l0 m, Z: y& {0 `
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log8 W! z  n- T( |/ j9 m7 o" y* S
, x" i- @8 J% S" X0 `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.7 P2 ?% H# U6 }) X1 U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
5 [  A6 z6 @( _* x2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 _# ^& X0 G+ n2 b
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"# {5 K5 S" [6 r" ]5 ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"; B0 d1 V1 k7 m/ B# V2 f1 V6 ]
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.: w% X7 i' c3 I
解决方法,移除低版本的APR (1.3.9), J; _& G5 m. @2 z

/ d* d/ P0 T* j3 F' H1 cyum remove apr0 W6 y4 @) ?. D; r2 s
5.Error.log中有: Audit log: Failed to lock global mutex
. \% H# P7 m8 F9 t0 m( S  ~! _- ?- _5 u5 C! y6 a+ S& R
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
0 E5 P  F1 r+ x! J5 j* `global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"], I+ ]- y4 C+ Z
解决方法:# z+ c7 E# t( T4 |* }7 n
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
- @2 G; z; j. S' l
& r7 B1 ]) V0 nSecAuditLogDirMode 0777* j! u. e, |. w; m- q1 p
SecAuditLogFileMode 0550- V6 q7 g( s( I5 d9 p5 h
SecAuditLogStorageDir /var/log/modsecurity
: {3 I# y% i; ?' dSecAuditLogType Concurrent0 S& e% T; V  S/ Y
参考文章:9 C3 @4 P! W* n  x/ S: ?7 S
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
/ a* ~0 ^& i0 Uhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-24 18:50 , Processed in 0.076129 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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