找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12112|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。, U9 u$ b( J  E8 W2 B9 s! ^
2 M: A  p. N' v% Y
一.准备工作
( X/ H. ?  a* A, E9 C0 T/ J- Z! a
* x& l- [* ?1 Q% o+ |  {系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
. z. V) L/ G0 c( }9 U
! l1 ~" P6 w4 B) w( z; s' X, j8 rtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
# y: p. K5 n/ f5 E6 H6 U& M  p8 p/ C0 b1 I( D8 \
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz( K+ U# ?" D5 U  V6 m$ h3 W& U
. H! v2 O4 p! C1 B
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 s4 ~5 z7 Q" O" S" i; m  X2 s- C  d0 \; c
依赖关系:* u6 u% z) {  B$ C- L9 Y
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
3 T, o9 i' p" V' D, e& U! S! J
% `" b0 U: m* }yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel& ~9 B8 G( u- u2 c
modsecurty依赖的包:pcre httpd-devel libxml2 apr
; v" C; ]2 ^" g6 D; p' `% `% ]
2 H3 O: r& D% s! A8 d8 x4 H2 w7 A/ oyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
" q7 u* m- o) i7 K! p" o二.启用standalone模块并编译- b; b0 C" g; V! |; b( c

$ n; X) W. l* I+ w  y下载modsecurity for nginx 解压,进入解压后目录执行:" D- X$ x8 N& H8 w, @# M( q# q* U/ u
# C+ _: g( T7 h, q0 W* h
./autogen.sh8 a/ Q" S4 ~2 S/ M9 }& M% e
./configure --enable-standalone-module --disable-mlogc; g* p2 p! s% t0 C+ e1 y
make , l* c3 d3 i; X6 w
三.nginx添加modsecurity模块
  K1 l# U7 ?; e# ]% @* N9 f, @: z; o
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
% ]6 @6 L9 c( o& y' F- {2 u3 _  L- @. x- d; d7 r8 T4 }% a- B
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine1 Y: Y7 V. Y% K. e) v4 R
make && make install
6 h# F- r& q! \4 `* K1 Y$ A- J; X四.添加规则
) Y/ ]6 ]1 e3 |' c; \/ g' S3 C# t1 |) J  u4 \/ l* `# b
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
5 C; Y7 W. V' S: J( }
, s" ~, x, ]+ L; T1.下载OWASP规则:
+ l( d" k! ^9 p6 J! P5 J
4 _5 M* w& r4 d4 N6 L( p0 ngit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
& E( }6 L. E' c2 E+ s8 D" s/ E. }- _7 z: P( b) w2 \
mv owasp-modsecurity-crs /opt/tengine/conf/% b7 U8 X: _4 T4 f, I6 a

( R" |" Z7 V# ~3 ]8 B4 Icd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
- {# z# m9 A$ @( }; h0 s* z; H4 ?2.启用OWASP规则:
# b0 }- f" q+ \5 E9 N. x5 ?$ Q" |. e) ]6 O
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
$ G! P" ?" O# Q# u" X. O  H
& \0 h1 R9 w1 C9 {: |9 F编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
; Y3 c# u  \% d
+ a9 H$ B& \$ Vowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
" G  |4 @* s1 [8 V
; A# [; r& t8 `8 c9 e' WInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
  F* k, e3 P* }Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
9 {/ D0 a  n! o/ |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
5 H: C9 \2 y' |3 XInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf" H" F5 t" k# T/ R0 C. Z; K6 S
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf9 R- s8 X' k: w' L$ Z, D) o
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
! Z8 b& X4 N4 a7 j" I9 S5 K( ^7 [* H8 V/ lInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf1 m  y4 q* P6 Y5 V2 r3 N; ]+ i
五.配置nginx
6 Q3 I7 I4 d" `7 F1 v; ?7 U) A! I$ ?: p$ ?
在需要启用modsecurity的主机的location下面加入下面两行即可:9 C: B7 Y) M4 P6 N+ i

5 x% o4 ?, r. r& n. }! `* A3 gModSecurityEnabled on;  
; d( k# q4 Z" h* W  vModSecurityConfig modsecurity.conf;8 d% R% I' K2 ^5 e' s" h. w
下面是两个示例配置,php虚拟主机:" \  [% _9 W6 \$ l8 L
: a% y1 c$ p' ]2 T! ~; @
server {
% Y4 k( P+ ?: E      listen      80;
7 T; v6 N+ L7 L3 O) P. a/ B      server_name 52os.net www.52os.net;
! x: x: s7 D& _" Y2 e     9 Q. \1 o- u! z1 d6 F+ J0 O1 k
      location ~ \.php$ {
, O2 v, d( o5 H1 k5 p4 o9 e1 [4 N( Q      ModSecurityEnabled on;  
$ W- R2 C6 Z9 X, m( y8 x4 j      ModSecurityConfig modsecurity.conf;
5 a9 o! S* \* ~, w9 L1 x- L" ^) d" ^2 j, q
      root /web/wordpress;. t3 ^" c' V% Y9 H/ z/ s
      index index.php index.html index.htm;
; Z: q: Z/ b3 N  
/ I; {# m, @$ H6 o; |  i# e) D      fastcgi_pass   127.0.0.1:9000;) g( t. n7 y4 u% g' U2 v
      fastcgi_index  index.php;1 P9 V& f, P7 {$ G
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
. {8 w7 `4 I# A& s% q) ^+ V, N      include        fastcgi_params;/ d' x* V  s( {& R( u
      }% X5 K$ ?) p; `0 R( w# I
  }7 U0 M" d. Q7 x8 l1 K
upstream负载均衡:- e: i- A" X# u8 C( z

$ R# l6 e. c- Q  N+ m4 Yupstream 52os.net {: z( I5 J" M' ^2 J. v/ m
    server 192.168.1.100:8080;5 O0 K% C8 _: t
    server 192.168.1.101:8080 backup;
3 K2 Z+ q  w' h8 V1 o}+ O" U, F" _9 X* R

  K, n/ h7 C3 Q/ T4 A, wserver {; H8 L/ B7 A& {5 d% H
listen 80;
5 A! c" o" m0 s, z8 t/ ^( Aserver_name 52os.net www.52os.net;
) Y+ M% [4 C/ Q7 R9 o! O* s+ P5 N; X& M
location / {1 Q8 Q! R- _; i& K$ A  Q% K
    ModSecurityEnabled on;  
% K" p( \' g0 @  k    ModSecurityConfig modsecurity.conf;  
+ ?8 p3 s3 b" @% C( G, _, J& S) o) J5 r: e8 r
        proxy_pass http://online;
# B4 @' |( E/ L) I        proxy_redirect         off;
) C. o3 @; U' c- A: y! W1 M        proxy_set_header Host $host;4 H! M, \. g6 @  U0 W' }/ e
        proxy_set_header X-Real-IP $remote_addr;
1 ^7 I6 P* L: S8 E* j( U1 E        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
/ s9 D! O! R3 q& z) S( _- u, \    }( H, F6 r8 D9 y5 A+ D  V
}
6 k* u4 c3 ]  |六.测试
" W& D- y0 p! a+ X, n4 K1 W8 k5 P
2 N5 t3 |0 P: `. z我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
) C% _8 L! q8 d, e
( _: Z3 O! R; {2 l<?php
- k5 Y6 a( h" q. k' T    phpinfo();    - _' M; r9 C  I1 \
?># g" Q) y! G& \
在浏览器中访问:5 h% R# y; g7 T/ b+ U% m2 c
' a* I3 k3 W' |; z. t1 C6 F, q2 s
http://www.52os.net/phpinfo.php?id=1 正常显示。
1 x9 N& w  }* l4 w/ f, Jhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。6 u- @( ~' ]( ]! E& f$ O
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。1 p5 B9 g: i- w% X. n
说明sql注入和xss已经被过滤了5 [) Q+ l" j. Y0 a& Q7 h0 N
, b5 ~6 i! |3 G* H- n0 w8 o: A
七、安装过程中排错
7 |" J# ^1 L+ W
$ h7 c4 P" c( X( Z: S( Q1.缺少APXS会报错
2 |1 ~, ~" e3 o0 h+ U2 L* b" Z
" J& U3 w/ O% f1 i, {9 pconfigure: looking for Apache module support via DSO through APXS
8 \) Z3 B( }3 D- Y1 }  P' u% `configure: error: couldn't find APXS
% p) p" R2 [1 b8 b0 ]* b, ~apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! B" E( b+ a, t! S
解决方法:- o5 h, F& t* g! J3 T. r
2 V2 o& M; W" a+ F% n! @
yum install httpd-devel( `! V7 f6 s( d4 b9 Z4 \. F
2.没有pcre5 ^& B& j$ B5 v, \& E

6 @3 z7 ?3 K( C6 Y% econfigure: *** pcre library not found.
* G. J. z# C$ N" {2 Uconfigure: error: pcre library is required
5 K: ?& i% L; U2 n3 q0 R. t解决方法:2 ]5 w: m! j" a3 }

$ ^! ~  w; G! ~7 h. z0 o7 kyum install pcre pcre-devel( K( V5 ]' G  F1 {; u0 E0 ]; }
3.没有libxml2
$ }5 S+ Z. f* i
8 n1 ~. t& s7 f' d* s6 X0 {/ ?( O0 t
configure: *** xml library not found.( j& u$ G5 d1 |) y! f+ X
configure: error: libxml2 is required
! a6 G* C2 s; w/ r7 X4 w% U解决方法:( p; a, @/ y6 z9 q8 L

" E( n3 Z3 p% z3 Fyum install  libxml2 libxml2-devel4 r7 E% `. g) _7 M$ U
4.执行 /opt/tengine/sbin/nginx -m 时有警告
4 J9 P$ {" ^1 |, R: ?9 O" O$ Z/ S  Z% S, h( G# ^
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
7 @9 D1 X) ^5 D3 I# ~$ N, E2 D3 rnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
6 e/ p- J! C2 ]% D原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
# s, X, h: g  M) p
7 ?* E# T& _+ `2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
+ ~0 p: Y7 j0 C: b/ D6 r  t" Y! C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"/ Z$ e* u$ f9 c/ ^, c
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!$ L- Q! [4 s, K5 s) y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"8 c. I6 N$ a) m! v8 q2 s) |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6". b. w6 M# W5 S0 W% `& e
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.$ Q7 L) f7 \5 B# E: u2 O* O
解决方法,移除低版本的APR (1.3.9)7 D, f% W6 p) \: Z& k; F0 a1 v

' O2 T/ ?4 s; H; S* jyum remove apr! v/ E! A8 x# E  o: a9 d5 E& L
5.Error.log中有: Audit log: Failed to lock global mutex
- X# W/ E0 b0 D+ B
* n% g9 q" m' C0 X2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
) V( s/ u! H3 f- D- Fglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
4 E  x6 c& L$ S7 T& ]解决方法:: R  q: H0 M8 u
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:% f, z5 i$ `- s* W: D
# h6 ]: F$ h: I- l8 A7 p- j8 [
SecAuditLogDirMode 0777
& y. o) @2 V/ o- K/ E( M- bSecAuditLogFileMode 0550
+ Y& m" j  E3 d; [! VSecAuditLogStorageDir /var/log/modsecurity- _7 v+ [- x, c, Y
SecAuditLogType Concurrent
8 Z9 t. H* w& x参考文章:) [0 q4 v4 E3 y" r: R3 m7 M
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX8 y0 M9 J2 X0 s8 a
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-10 16:58 , Processed in 0.067145 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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