找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11877|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
* i- }; E. v! e$ \8 u1 u7 s( @: x$ j4 x# Y
一.准备工作0 X: u# R5 p  X. g1 M# Z0 h

7 R8 s' q! i+ J" [/ @* R7 a系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0* r& y/ E2 d0 ]% q  {7 G. [

+ d! d4 d* p5 z* K3 }# \6 u3 l9 ltengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
, E  Y. p) n1 J( S7 K, F
0 J3 i% U5 \; X% {" fmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
1 J0 J8 I$ c: y5 L( a. C0 e1 m8 r. e* C- d
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs! `0 u6 n* Q/ x' z) ~0 M& O
$ c" F- m, \/ g/ E/ x( M, h
依赖关系:
& @' e4 P) e/ n0 x8 Y+ Ptengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
$ o1 U& y' I! c) S* k) |8 {5 n8 B8 ]% o0 c8 ^
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel, m: W5 ~% E6 j& R' T( A, L! N3 }
modsecurty依赖的包:pcre httpd-devel libxml2 apr
5 l! f0 c; w2 }$ y4 v% B0 A* z3 t& b8 n
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel. L8 |# \2 C1 n5 b2 q
二.启用standalone模块并编译
. g' x/ X6 l. a- ?( b, f5 K3 f
3 n1 b* z% U+ U& F. x下载modsecurity for nginx 解压,进入解压后目录执行:
% a( Y, o# X% ^8 T. K- o, ^
, j2 K0 k& g. w( |6 l1 u' ?./autogen.sh0 M) E: Z$ h5 `0 e* |) G+ ]
./configure --enable-standalone-module --disable-mlogc
6 `! m( w" G7 Y9 rmake
0 ]+ U/ J& n. n. k* U4 K# z三.nginx添加modsecurity模块
3 s: b0 k: K7 L' A' P, V
0 h. I+ \  E+ x在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
, B4 P) [1 W2 _: r
' m" U% o; ?( Y3 j1 j! {5 G8 O./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
! f' K% e; f" d. Amake && make install
5 k( ?) V4 U4 e2 t% t6 e3 ?% H* n& x四.添加规则
' Z% N- D; L' w2 C8 ~; X5 w" M1 Y6 `+ j6 h
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
7 Y( s% S4 [( c4 v5 h
6 g# x* h2 v/ T5 I4 A1.下载OWASP规则:
8 S- L: Y' i  n1 d( c
! {% v8 ?1 B4 Vgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
+ N% i- ~" D& Y4 E& {' M) Q* R- d! o; ?3 `: w9 U7 _
mv owasp-modsecurity-crs /opt/tengine/conf/$ `) L! D1 d& i8 e; d/ `0 d

$ T/ Y- G. Q1 I) l  zcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
9 I* o7 v% W( m1 Z2 T3 n2.启用OWASP规则:; v4 u5 N5 V' ]0 ]
5 n' n! N% Q) Z
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 W; V4 Z" \; v0 r

# B, v% _* K) g+ |! i- w编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( o5 X6 D2 V+ ^) @8 t9 d

+ m. a% z3 H5 E9 @owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。1 r% u( p1 W6 u9 q
& H* k& ?) @* h6 L
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf7 v1 U& Z! K( ]7 s5 P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf! U6 x9 E# E1 Q0 v3 z, L
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
. {4 [. g$ Y0 |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
1 C$ `3 D# Q8 v9 n0 g) h" gInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf6 e+ N  q$ \! W! N6 V& Z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
* U- L- u- P8 f# o% g; xInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf, C/ X: n( e" C  q# @
五.配置nginx
4 ?; C; ]6 s  \, {
: ^) w6 G$ m4 D* Y  G在需要启用modsecurity的主机的location下面加入下面两行即可:" f; m& R" p' }4 L0 ^$ J
' l% r% ^$ o7 ~! @* Y- X+ L
ModSecurityEnabled on;  
2 r5 v2 ]' i$ }+ K* DModSecurityConfig modsecurity.conf;+ O* Q7 U* J* O! r) b3 n
下面是两个示例配置,php虚拟主机:
  `) c% G- s8 H4 }$ e9 |
1 B- }7 k! y7 a5 Y' S8 X3 `! pserver {
4 _- s8 g" \5 p3 J      listen      80;
- H) q! k$ T  p$ Z: @( n; L& b' f      server_name 52os.net www.52os.net;
8 a3 [9 [% ]2 k) r4 C  K/ ^' f) p! e     
0 u  z4 B1 J! r' `6 L      location ~ \.php$ {4 v( U5 m/ k' Q% P1 j4 ^. C
      ModSecurityEnabled on;  
" p+ f' w9 b1 @      ModSecurityConfig modsecurity.conf;
7 R- t4 c# V6 d7 {7 x- b. N7 C3 L6 |: Q. B& S
      root /web/wordpress;
  Q8 j% ?. B" a* `) V      index index.php index.html index.htm;
7 q8 k9 d; O3 D- V/ F+ ]3 a- i  * L; ?' t) P  b. E( J, z4 _* Z/ N
      fastcgi_pass   127.0.0.1:9000;- o5 ~% B) P' H5 ]. e2 `
      fastcgi_index  index.php;
' Q& {% ^+ p# X0 `$ {9 a      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
  n5 Q  q3 a) u& ~! b* Y      include        fastcgi_params;
- a* P" ~3 S5 \& T+ V      }* \# U5 O3 _4 t' [
  }
& R7 N- E" j+ @2 v3 W, O: y! pupstream负载均衡:
! C. t6 J* b* H( G) D  k7 D
: |0 K1 j2 i0 s1 \# oupstream 52os.net {
* |4 G6 @2 s& y, y2 s  q    server 192.168.1.100:8080;
! {% P4 {6 D3 M% ~* e/ D    server 192.168.1.101:8080 backup;
: O, H. g- K1 V}, F( u0 n( d- s( V
/ r7 y/ |* @( J  x4 g" ~) W
server {
0 p4 z. q! e6 u8 F9 f* mlisten 80;
# r; d: @4 Y" Nserver_name 52os.net www.52os.net;0 W" F8 j) q; ^8 z" V0 L

) V! ?' ~$ @* f' b* Xlocation / {. s& M0 r) m& |8 [+ J1 I
    ModSecurityEnabled on;  . b  o5 |4 Q- ?$ k
    ModSecurityConfig modsecurity.conf;  
6 e$ ~1 e( ~* W3 L4 G: {
- v" t; j/ N' z! g, l6 P2 O3 e        proxy_pass http://online;
2 a  ?& |  w5 j1 \        proxy_redirect         off;; |5 F4 m" ?5 O: X
        proxy_set_header Host $host;2 W; ^6 m3 m$ N& o
        proxy_set_header X-Real-IP $remote_addr;
# R. F$ s* {" N        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;- j  @+ ?, b$ _$ C( O, ?/ p: V
    }
; s7 z- s+ m$ P- |+ D}% I# @% y, v5 X! d
六.测试
" c+ ]) b* i/ `# Y4 K7 f7 H( R
2 o* B; _+ ^+ _& Z" e3 w' {我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:( W% g! W: z7 D- ^' v

( x1 s% j5 t% E% @* o0 [7 }6 P<?php6 D: l# f! ?! ^3 Y, G3 z( [' w" {
    phpinfo();    ( D. `. ]  Y% J0 @
?>
* K: `2 i' j4 ?" ?在浏览器中访问:6 A2 Y5 K% C7 o
, V* x1 W. ?" ]
http://www.52os.net/phpinfo.php?id=1 正常显示。/ a& Q% O' |: }4 j
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。/ H" G( O) p: {" M/ i& T
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
6 K- X0 l7 W, e$ t2 V  V说明sql注入和xss已经被过滤了
- T! F- I; X8 ]- Z  I, Z7 c/ }) w! |
七、安装过程中排错# S; G4 M7 [. F' ?; y- X

4 b% z( M3 T5 y5 s; o3 @2 u1.缺少APXS会报错* C" U4 j3 v5 p/ U" w

/ \3 ?8 y4 V$ n% f& Sconfigure: looking for Apache module support via DSO through APXS& n; B5 ^# t: U2 l3 j; q! h
configure: error: couldn't find APXS
" N& [$ l$ G/ oapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。: n4 B: H5 U1 Y& A4 M# s3 a
解决方法:
& K7 ]5 a5 e2 q& u
- Y# \2 ^- n4 K: kyum install httpd-devel( N3 P. p2 b! w( a) K
2.没有pcre& q: j' `& e- Q/ B( f2 H

4 H: @, ]* S  wconfigure: *** pcre library not found.8 _6 [- V$ z' F0 r
configure: error: pcre library is required7 ^8 L# [; v9 \! c
解决方法:
& y5 M6 h! L5 B
2 a0 t: |7 H! t8 uyum install pcre pcre-devel, I' C/ j/ n$ [& u0 r
3.没有libxml2
! L7 M7 y3 z; j) f) }/ u  \3 N! I+ c& Z& Q: k4 I; ]0 i& j

8 f$ T) [  F" u  x  G, t0 R% r/ bconfigure: *** xml library not found.
0 {- Y" B  Q# K% N1 R/ t  Tconfigure: error: libxml2 is required; A; N" O3 E6 D# ]
解决方法:
1 `! `+ U; i: K* X
$ T0 i8 T- y8 F, ]' kyum install  libxml2 libxml2-devel
0 O  X! r" W8 w  b6 s4.执行 /opt/tengine/sbin/nginx -m 时有警告
$ p0 {  d! h- t, j" a+ j
' z% n) X# h9 R) J6 R% E8 HTengine version: Tengine/2.1.0 (nginx/1.6.2)1 ~1 |- x7 ~+ @' e. d& U
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
# d, Z9 V: O8 s" d0 ?原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
+ J5 A1 O" m) f4 K, {6 E4 k' g, T) t, O& u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.3 @7 H6 p0 C2 [  l  g* W1 r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
9 _! [4 K) |6 g% D7 k/ d: C0 l. y2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!4 l2 y( p0 M# k  U5 Y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"' G- v3 F! k4 ^% a* g+ ?+ o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
7 ~: S# B: H6 X# z, \* P2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 k6 w# c; w. x1 q. v2 `0 p
解决方法,移除低版本的APR (1.3.9)+ k5 O4 @" @- W( F% G0 K1 z

$ B. W- ?6 C2 p. ^/ Cyum remove apr. \" u; G& {( ?, \8 ^0 r. f
5.Error.log中有: Audit log: Failed to lock global mutex
( z' q5 Z+ N$ F6 ^6 _+ Y' F* L, f, S; |
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
5 ]1 B- t, k) x( fglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]' A1 A3 z* t$ X/ p3 x' }
解决方法:
7 U" t0 F& Q2 q+ H, J, D3 R, w$ s编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ O6 W; q2 A7 I+ ^. p+ T
3 Y8 D- U+ G# zSecAuditLogDirMode 0777  J, @$ a/ ~( o/ L; M; e
SecAuditLogFileMode 0550
& f% n/ c, U9 s% k' K; B& D( f3 @SecAuditLogStorageDir /var/log/modsecurity8 }+ Z1 g  w2 b/ m2 R& f# J
SecAuditLogType Concurrent
; S+ d, P4 i  W  m( X# X$ `参考文章:5 N5 }6 u0 \2 b5 p0 X
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
2 ^% A- R& V+ X; s# chttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-21 15:57 , Processed in 0.068853 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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