找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13132|回复: 0

nginx配合modsecurity实现WAF功能

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

% a' w. S2 Y. i& G) T一.准备工作1 q$ r! P( M2 ^* Z3 U( {
3 K9 X* R! l, n- J3 E
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0# g. r! O$ @* V3 S( P# {8 E

+ j7 q3 j# R6 ]+ O* Stengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" z; |7 V" ]- N  C
- z9 ]5 p: B# ?& K( Fmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz4 o3 j! ~5 ~* j$ h) w: m
/ c! C0 S, k: i! ^9 h
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs* W7 G6 u4 F* M+ W1 @

9 l. l9 H# _1 S. N8 t依赖关系:
+ g% W3 q9 _9 n: n* S) s$ l" j  dtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
2 d* x6 i7 r9 t6 r9 `6 @2 B7 x7 K" n! ]4 q2 b: q- I1 ]
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel, l) B4 {, C6 R; K4 |
modsecurty依赖的包:pcre httpd-devel libxml2 apr: ~7 d& _% g& L) }+ k8 ~
; E6 R& i% p' H8 J
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel4 j/ [, {0 V" X. ~  Z% G
二.启用standalone模块并编译
6 k9 X* D) n% _' {
8 O$ q" v5 o0 u6 _下载modsecurity for nginx 解压,进入解压后目录执行:
4 w/ B! V4 _, ~5 D( A+ }  P/ k1 m* Q. [% d! [3 K) h
./autogen.sh
1 ?9 T2 m5 d* j7 I, @./configure --enable-standalone-module --disable-mlogc1 A' ]" q. K% B9 V- n! i
make
4 I& b8 F3 `" R+ f$ ?, ~三.nginx添加modsecurity模块
5 A/ r2 q3 S2 [& j& @2 z! E6 p
) X" |8 ?, V6 v% c* l' f: H# O在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
7 N1 e6 F2 p5 v# \; W
3 Y( [, Y9 h& L  a9 b/ `./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine9 N/ Z8 Q9 v+ z$ }1 o
make && make install* x$ \' V: u! M: @3 x
四.添加规则
; O/ y4 j4 ?/ x  {# t6 m3 S# b
: P3 ?+ _, k4 a, p, H& smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。, l) q. K- h# P, v
3 `  ^0 o$ T& w, s% O4 c* n- k4 E
1.下载OWASP规则:0 L/ u! ^% k  r
8 S( m$ x, Z- n" v
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
# D" C0 D- G: p9 Z2 e% z. x; e- G5 Z
6 ?) y( N$ N* c. o3 f0 N9 t2 O5 w. Lmv owasp-modsecurity-crs /opt/tengine/conf/
. g, W# S3 N3 I- T: |; w4 c/ w# z" R8 L# b" p8 D1 L! k- @
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf8 B! q3 O7 K. s! o1 ?9 b, G
2.启用OWASP规则:' e9 a+ u/ n  K$ e6 T

+ J* v) U5 ?) X0 F2 j. p9 Y复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& x+ `, W5 m! q4 u* e4 ]  y
9 p% a$ E6 d) m3 g5 H) G
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
/ \5 r' c$ r" |& L3 n, z
$ j; o: V& O9 N5 N7 Q5 Xowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
4 t2 H  Z0 ?* M/ b) x: u
. T0 M  T6 O& u2 a  CInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
8 D6 E+ A; U, u8 u2 kInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
8 F. ~3 M+ I: \7 {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
4 f) g, m+ z6 u  i: e( U6 jInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
, \, v) F4 U) _1 MInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf) |# B0 z) A0 D4 o0 F
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
# E% U- S- P  X3 T5 F+ BInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf1 l# W5 m9 ^9 a. S, n# D
五.配置nginx' }0 H# S! V4 Z- z7 [4 t0 i
9 ?2 R4 K$ M9 u
在需要启用modsecurity的主机的location下面加入下面两行即可:4 K9 v6 F) E4 s. K3 O

4 D0 S8 q% |  v3 FModSecurityEnabled on;  " g& U0 c# T3 r4 |) v% t
ModSecurityConfig modsecurity.conf;, [5 P" t, A& E5 U% Y
下面是两个示例配置,php虚拟主机:
* v0 O1 c- c1 X, E# Z. r
0 H& e" @- ]3 @6 Iserver {
8 F$ w$ Z$ o9 W9 G! ^0 q. j# g4 Q. _      listen      80;
* p( O/ e( K8 V. k% a5 d      server_name 52os.net www.52os.net;
( W7 i% \! r6 R* B  V( |     1 H1 h; Y5 O) T6 r0 B
      location ~ \.php$ {
3 _, a: U' ~' _: ~% _, S: f      ModSecurityEnabled on;  - t2 B1 _+ u  @/ U* G
      ModSecurityConfig modsecurity.conf;3 P1 ]" B7 q9 F9 X# g2 Q& e
+ h1 b- v' ?5 x
      root /web/wordpress;
, l0 W- m% o; p0 q, c  S0 Y! h* s      index index.php index.html index.htm;
, W" Z/ F7 O" p8 `6 T1 v  
# J3 s* ]& R6 ~8 y9 j      fastcgi_pass   127.0.0.1:9000;9 E: `) H9 n) X
      fastcgi_index  index.php;
# p6 M- X0 h0 @3 Y- S; F      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
4 _, O; x! a$ S# O! S      include        fastcgi_params;
! P( A$ ~1 h7 T" V      }
5 w3 ^- k# `" V  F  }
0 a/ g4 h' p% z2 [: X7 v% w$ Dupstream负载均衡:1 e( y( n! B5 P1 F
7 g4 p# X! B) `, U" N
upstream 52os.net {
: R; O+ Q* z4 A" C! l6 g7 d    server 192.168.1.100:8080;
$ t3 G% E  m4 U: a( P& A$ _2 A    server 192.168.1.101:8080 backup;
+ E6 P4 u! ^( f  R: r- T+ F3 k}
6 P- V- ^6 v1 B! \
! t* y# |. P* G. b' n, Rserver {3 \0 h$ e) {+ |+ B- F5 P4 L, A& d8 S
listen 80;- E& W, j) Q/ j' ~3 k# s) D
server_name 52os.net www.52os.net;2 `) x$ s2 e1 _7 X  m8 [3 d
8 _4 M7 X; C  I( q
location / {, m* R4 o. d2 M$ F% D$ t& T
    ModSecurityEnabled on;  
9 j' |0 @: e( ~* H. S    ModSecurityConfig modsecurity.conf;  . ^9 V: U: ?8 P5 e& t

+ ?' S6 c" t3 Q4 o9 N! _* _        proxy_pass http://online;: e0 R' }' n' l' Q3 K1 `* e
        proxy_redirect         off;1 D! F  _: a3 R2 Q9 @0 ?
        proxy_set_header Host $host;+ C6 M; n% l) y! R6 v
        proxy_set_header X-Real-IP $remote_addr;
6 ?6 ]  E! U5 L- ~" B0 Q2 N. }        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;. h4 N  ?9 d, j  i/ `
    }
& B# ^# l* H# Y8 ~  A}5 p- A2 u7 A: |+ P) b
六.测试9 w0 u. O3 W, V8 l
; J# _5 M/ ]$ Z! l! U' o
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:- ^/ f' [- \7 q
( {4 P* c7 P9 O7 O: N+ g6 N2 t
<?php
  X7 M7 Z0 p( q6 z, v7 R/ o& f5 y    phpinfo();    9 u- |0 G! U6 t7 ~
?>1 g- [) J+ ]. p
在浏览器中访问:
& @) C6 H9 h+ F& S' \: W5 M) l( U2 D/ P: _
http://www.52os.net/phpinfo.php?id=1 正常显示。/ Z  A, q: j* V1 K. j) {7 N5 |+ \
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
1 S$ N# X  m. A7 B5 ~http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
) Q" F, p4 y& P$ V: ^1 s0 b3 h8 T说明sql注入和xss已经被过滤了$ {: R! N2 g6 o9 j

' R( ^" F! p7 z( `七、安装过程中排错* ?9 N: P' `$ O% X' y, Y

9 R2 ~% R* s3 A( y3 W! P1.缺少APXS会报错
- j1 k5 c- v0 @( G0 ^9 V4 u- F7 R
configure: looking for Apache module support via DSO through APXS6 u, F( G$ P, }: ]8 _
configure: error: couldn't find APXS
' x# e6 s# M: H: Vapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
( U' ~4 q6 I& r0 e& T: B! C解决方法:1 F. `! o" E# k7 z7 V2 o4 [
4 x7 O/ q5 v  Z) k: w; z1 ^1 O
yum install httpd-devel3 b" X* }$ g( p0 p
2.没有pcre% P6 f; Q3 I- U8 C2 a7 l0 H
+ v1 M' _) k# r5 Z# g
configure: *** pcre library not found.
0 M, ?5 E, Y( v& B; Q" Bconfigure: error: pcre library is required; y/ h. j2 \3 N) Q; c" P
解决方法:( j) p& G: u- Q6 D9 h: p

# \/ l2 p$ W3 l9 C( h7 `yum install pcre pcre-devel# S, C0 J9 u4 `; G/ k! s
3.没有libxml29 d, y9 h5 A( v$ P6 d* L7 O/ H
1 O0 _2 {7 u4 R, N% m+ {: P

$ B6 b; A2 e3 p: \% p- E* Mconfigure: *** xml library not found.7 ~6 _1 P0 e) F$ f
configure: error: libxml2 is required
/ |3 ]1 y4 Y& ~- g" M  N# N解决方法:
- r2 _) M8 A4 A: i- F
, |4 g+ l7 F- n; ^  s$ ryum install  libxml2 libxml2-devel' a) C2 Y- h9 X4 q9 _
4.执行 /opt/tengine/sbin/nginx -m 时有警告/ e% g" b9 s( O& n
3 j5 ^: w4 t+ h( n- Q% P1 A6 z
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
" L7 m! U: S9 J3 Y9 O# g& z( znginx: [warn] ModSecurity: Loaded APR do not match with compiled!
2 w' x# u8 B' _' i! b原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log' d& u) J6 m$ ^7 t2 o
$ x9 S- ]/ U: y. I. g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.) g4 j8 |. y0 C4 s7 }' r$ V4 t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
1 z0 w- s6 j: X. ], z5 B2 }9 G/ D2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
) J. {- q- J6 e* s, s3 {+ Y  ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"! ]1 \9 R# Q- _: G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
* l7 X2 t, y) n' w2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
3 q4 Q' G0 @) Q- L( y解决方法,移除低版本的APR (1.3.9)
0 S0 C/ y3 Q- k
9 o: g  x" ]9 P- Tyum remove apr
; F1 m# Y' d. R3 Y5.Error.log中有: Audit log: Failed to lock global mutex  H& `+ y/ P6 E" d. @6 k4 f

3 }1 N5 o4 `: z( q2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ' o$ i. q& _! H4 Q. Z
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]6 i$ E/ F5 W2 Z. w
解决方法:" S, M8 r3 ?9 x  v3 J9 b
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:: N0 @- S6 T7 R* A: c1 e) }
- l+ }0 P# n( o; c1 t4 J, T
SecAuditLogDirMode 0777
" H6 p& ]# _4 n0 c& j( W7 CSecAuditLogFileMode 05508 U0 z* H7 {( u! g+ x  R9 a
SecAuditLogStorageDir /var/log/modsecurity
0 e1 i& K, Q4 f7 v9 h/ iSecAuditLogType Concurrent; t6 m; d1 k$ k0 D9 w0 ^" j$ ~
参考文章:
0 l+ S4 @/ W$ P- M$ Yhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX+ a& h# L! d# O9 _, i/ {7 F  Q
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-18 07:05 , Processed in 0.067686 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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