找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12100|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 r$ j/ M! g" e4 y+ C. l  R
* x/ g( `2 E! F1 n) c- `5 M0 z一.准备工作5 u, C3 O9 @* t% \

3 Q0 q& r; a+ l* [7 c7 C系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0, A& ^3 O, j- N6 I/ E/ U
; s; l& g; X# b3 l/ x
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz9 X0 Z" u9 ~% g, r) c' z
0 F- F+ _0 b- V# y. Q8 I1 M% W4 T
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
$ \. b0 ?* {0 ^6 L6 Z0 |* D+ r; s1 H' [' J% r
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs5 V, q2 k6 f0 E
' i6 W; b6 K% L
依赖关系:
. k9 V5 m7 I* W# b* F" J3 Qtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:; u0 u0 e2 {6 i0 t

; V6 |3 O" v" gyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
4 w) [, U' _  R1 Dmodsecurty依赖的包:pcre httpd-devel libxml2 apr
4 [6 v8 ~, s3 Q8 N/ G3 [4 r
8 R6 W5 d* |2 T( q2 v6 m$ xyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel8 X# L$ h  b" I# |3 v! o
二.启用standalone模块并编译8 v$ z! `3 M& C7 Q

& c& V$ d& C" i, B下载modsecurity for nginx 解压,进入解压后目录执行:
' O( Z1 ^8 A3 I* x5 ]
' O$ @6 K1 `% ?./autogen.sh
+ ~$ W% I8 W* v# Z./configure --enable-standalone-module --disable-mlogc+ ~- a7 |/ L# b' I) j2 v, Y$ i
make
  o1 G. S' g  e4 m2 K三.nginx添加modsecurity模块  n  C. l# n: S4 q  Q1 U0 d
% N4 A' T* W4 e, m
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, E* J% ]9 r. H& K
& C: o2 u# b! m
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine) n0 D8 m/ P, g4 G5 u9 B
make && make install
" P1 ?# c- @/ Q' Y: o9 D/ s四.添加规则8 G; {+ o; [& `, H/ b* S/ n/ {
. o2 F$ v+ @  ]0 W1 x+ K$ q; h
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。1 L$ w) t6 @4 T* f  k

/ Z/ C% z  A3 |# u* `% X1.下载OWASP规则:' V1 U' _/ ~' d( c0 V

. W: ^; ^: t' x" i) v' S8 J& ogit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
( g" [% Q6 i( Q* I) o% b  ~( V/ r- S" D% _) u- P
mv owasp-modsecurity-crs /opt/tengine/conf/6 \1 M, @7 G/ }

, i! M5 b) d% F* ?, jcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
4 F/ j1 w5 ^3 X. [2.启用OWASP规则:4 B! ?7 T) @. _0 u4 N3 c

- @" ]! u; m: h& [' m/ I7 B. x复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( s3 l! N( J6 D7 |
4 E# @7 B  ]; T: e编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
$ H* _* s- n  j: s& O- Q4 V' k# d# |% M& S2 D/ ^
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: C& e& M" ?6 v' y
5 ]" h! ^1 ~% v7 L+ AInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
6 X# y9 T4 U- H5 c1 fInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
- _8 Q7 d% {  H' X, E( XInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 r9 j  G) C1 ~+ {! e* A1 y6 ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
, k" P! Z( p4 _; [* y5 M2 A) a  jInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
* ]' z' I# h, T+ WInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; B- K/ |7 s( G
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
: Y- D2 z+ a# R' @五.配置nginx# |6 d  R* r% [6 a# r

( F# ?6 G" H1 X# H7 N在需要启用modsecurity的主机的location下面加入下面两行即可:
5 C  D& ^, m8 x+ i' i7 J( B" |( e5 j+ I9 V& U
ModSecurityEnabled on;  # f1 @2 f3 v" f7 |' z3 Q, `
ModSecurityConfig modsecurity.conf;; L9 y" P) o( T9 Z' G
下面是两个示例配置,php虚拟主机:1 b& ^7 T$ K0 u8 o
3 q$ [2 Y" t1 E4 a
server {
2 B3 m9 R( @* V& L1 l% `      listen      80;
( N9 r( p2 Z4 w: g7 L3 K      server_name 52os.net www.52os.net;7 `/ E0 o- a" C
     / V" _. a5 s0 N2 q
      location ~ \.php$ {
8 o- ^7 B7 o4 e      ModSecurityEnabled on;  
+ T' F+ w! o6 H. v0 o+ o2 r      ModSecurityConfig modsecurity.conf;& ^% P2 C1 Q( I' c

  E* X' J# d# c* E1 n$ z      root /web/wordpress;
2 `0 b4 {" c6 V' y3 h* f2 o      index index.php index.html index.htm;1 B: Z; E3 r; G
  
' \  J+ W" _% M) H2 d+ B) \0 Z5 y      fastcgi_pass   127.0.0.1:9000;& t, r; B) P: T
      fastcgi_index  index.php;; n! V1 U; O) m
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
$ |  J* R3 F5 o. A5 n/ G- C      include        fastcgi_params;
/ H0 `  G' `; `( _1 V$ R+ u      }
, t* [5 e# W: m2 n5 O# |- d. Q' z  }
6 Z/ W6 m6 k' g; z: x6 ~! t8 f" ?$ hupstream负载均衡:3 x4 N' O2 m" Q
$ W3 x0 T2 m3 b
upstream 52os.net {
, W$ n- w5 Z$ r0 Q- e3 M4 i$ o    server 192.168.1.100:8080;
+ W( C4 Y' w4 [) p    server 192.168.1.101:8080 backup;) O  |4 e7 e- k. s: e6 l9 _: s
}
+ I6 H0 \& [; z( ]# L, ~3 b; n1 h% {$ [, Z" I+ B$ N) }
server {: U; ?8 N  ?) j  f: V/ J1 ?4 |& }
listen 80;! Z( K- l0 u% U& k2 e5 Q, O- K! t
server_name 52os.net www.52os.net;9 `, J. z& ~% H* o% \

9 K8 C# F6 J. w* a( Nlocation / {
. r7 C/ n8 R" @5 P& C( T& g& c    ModSecurityEnabled on;  
" V1 V9 m* b  s; `* v    ModSecurityConfig modsecurity.conf;  8 a: E/ J1 Z5 c. g

+ J* O3 d* R; h        proxy_pass http://online;. e- j2 p; w. t+ j; L
        proxy_redirect         off;: c9 G4 D5 ^+ a6 D- }* b+ \
        proxy_set_header Host $host;2 A% x8 ?, x2 A2 r9 s. t$ E! v3 I, I  c
        proxy_set_header X-Real-IP $remote_addr;
% m; v1 G4 j" G5 d5 U" r5 V' k0 v        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;9 d1 K, L% i0 w8 f' _
    }
4 ?  K# @3 ~4 Q" v5 g" u5 k. \}
% b4 \" c* A7 A0 k& r; `& c( u1 v六.测试$ R5 k; E9 V% n6 T2 L1 e2 a  ?) J
; o" ^- r7 w# U
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:: ~+ J' L( u& k, T  U

3 O8 U3 _, N  A! t* A<?php
' ]7 \/ ], f. T: g0 k3 Q+ Q& F3 x    phpinfo();   
( S+ b/ Q- N% v$ m# o7 r?>
" C, |, d* {' z在浏览器中访问:& ]0 Y; J( f, `, L) b

, q& R+ M+ }1 a7 ^7 e# Whttp://www.52os.net/phpinfo.php?id=1 正常显示。
9 A( e! f2 P1 p/ t4 ]6 ^8 j" ehttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
. D3 N6 M: V+ O7 Khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
. q  V4 ]0 ^4 M9 @说明sql注入和xss已经被过滤了/ j; g  F$ [) j( B
0 P5 c5 W- P1 x9 R( y- k5 |* M
七、安装过程中排错
: }! m/ `9 Z  `) \0 ?- {4 p
- V! |3 J# t5 b! j* T/ z2 T% b1.缺少APXS会报错+ u% w( Y5 H8 v

: l0 B$ T8 y* D0 o# L: fconfigure: looking for Apache module support via DSO through APXS
' |  l+ p: s1 Cconfigure: error: couldn't find APXS
+ i/ p2 _% H/ s3 z/ aapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
# p  F1 A1 i- M: Y, E' ^解决方法:: [* T0 y# N9 X; F% D8 x! l- K
1 u" `6 N9 }8 q% F
yum install httpd-devel
: w9 d* G1 \* L  Q; a1 m+ e, }2.没有pcre- O3 z2 u' _) C" q  H
* `# ^5 B3 c  N0 e% M* w7 E- e/ ?' Y! w
configure: *** pcre library not found.8 ]' X) D% o- K9 g8 W
configure: error: pcre library is required* z7 c; o/ T/ x; S5 x( a" b1 t4 J/ j
解决方法:$ p7 x" G9 i0 S5 U3 f2 x) q3 ^
+ m4 d* V1 K& H  a) v
yum install pcre pcre-devel
* C4 C( r2 W- A; I0 Y( ]6 }9 I4 V3.没有libxml2
# z; s& ~: G- y2 U' Z" S, j4 L5 o5 e' X+ d4 O* w$ n7 {, m

+ A- n( J6 M+ r  r: J# y, F1 Jconfigure: *** xml library not found.! B$ N. z' N0 G- K) t
configure: error: libxml2 is required0 G0 f* @; a8 E+ K& @
解决方法:7 |4 w( ?; K* ]2 P. V8 F0 k2 o

5 v' {1 ?* h+ {# I5 ~. w- byum install  libxml2 libxml2-devel
1 Q* H7 ?! J/ \4.执行 /opt/tengine/sbin/nginx -m 时有警告
8 d( u& h6 d; U) t0 C, G' h7 h+ D
% K% Z, [. Y& k% l; b9 v. CTengine version: Tengine/2.1.0 (nginx/1.6.2)
6 y' a) l( H7 X1 |( anginx: [warn] ModSecurity: Loaded APR do not match with compiled!
# q( C+ ?  j/ m# A0 ~原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
4 a4 F$ s& i2 K( ^7 K# q3 a  Y) k9 G2 M1 g: u" X- |+ U9 L
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.( c! Z3 e4 A8 T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"$ x# X2 H4 T# R0 }1 g& k
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
$ G1 W: J& {: r8 X- }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
6 B5 O' c' ]5 N% m$ Z! ^8 x  M: W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
8 e* s( L) }* Q8 M  w2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.6 K  k- b0 m+ h1 L" N& D
解决方法,移除低版本的APR (1.3.9)
8 N2 }2 f# t6 r
7 }0 N+ c: @# j: a$ ^yum remove apr
% V. s; z0 A4 `) x5 e! I5.Error.log中有: Audit log: Failed to lock global mutex
, c% U8 N! m& X6 E& b* ?0 u; h( g$ V5 h/ p7 j' a) y: Z  `  ?+ f
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ( T" H: ?2 h1 l
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
8 o+ b. b( q& Z' ^4 [* Q3 q% R解决方法:
: ~6 w: m2 g  e编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
- E, n; O" g8 L% K$ A2 c1 F# ^- q; n( r$ F. G/ ]+ @
SecAuditLogDirMode 0777- H- d, `# [, Z: o4 a: s( k
SecAuditLogFileMode 05504 a  a+ G) U5 ~. |( ^
SecAuditLogStorageDir /var/log/modsecurity) C+ I* B) ?# y+ g6 \
SecAuditLogType Concurrent
# N! h4 r; c$ R( V参考文章:% w5 l  p+ D# ~( \+ `# p- h( E
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
% f  M2 V3 n+ J4 R8 E* {http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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