找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12132|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
* f& _5 H5 ^" ?! {' Y8 o' V+ m$ ~
+ I$ H2 M# k, S$ g0 D一.准备工作$ G- z/ K, b( [. a

6 Q  m7 K! V) n( c! B) B系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0( X' c# G3 H" m' h
/ Y& H6 U1 v+ ^: I4 m8 V! p# \
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
4 T( e5 o. D2 U1 M( p! `  q9 U& g2 L  F* o; k9 T2 A0 v9 V
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz$ e+ y5 U" ]0 E5 q  ?

7 {7 i+ E; {' sOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs  t0 l& ~# a' c* p, L( l3 Q

- z% a) `' e6 g. U7 m依赖关系:
" ^* g5 i0 n9 Y+ Gtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:! D# D  Q! O( \& k
9 n2 j& j/ n# ?) G4 m. ^7 I( ^
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel' D  H9 o6 e9 r6 ^0 P
modsecurty依赖的包:pcre httpd-devel libxml2 apr
; J1 j3 y; g: P! _2 X/ R5 R6 [2 O4 Z# r6 `( f' f, x: s; O. ^' [
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel- P# n' L4 a" ^
二.启用standalone模块并编译
+ i! S8 k' Y6 J
5 _' Z/ x$ \  q4 G下载modsecurity for nginx 解压,进入解压后目录执行:
9 y0 J5 s4 y/ X! d+ x/ n3 s: E4 n; K8 `7 {
./autogen.sh
1 l7 P& R' L$ t* X9 h./configure --enable-standalone-module --disable-mlogc
9 \9 b& E! W( Z5 _make / P  m8 k" ?, b- c9 \
三.nginx添加modsecurity模块
5 N$ s; u* A& a1 v3 k0 j6 L! ~0 O3 y0 _4 `  x6 i! e" b, K
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:. H5 j( D7 {8 l# P3 C5 o3 B

7 ?* k! c1 ?3 i1 f- O$ o$ G./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine+ x: M5 X; V4 n; W9 t
make && make install
6 k1 K6 f- V7 K+ z四.添加规则
  B% ^; G2 L' X; `8 q& L( r$ a! @  O+ f9 S
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
* u) j) N8 x$ b+ |# `3 G# K
6 j, M0 z5 d9 K" v: v; S, o; @1.下载OWASP规则:( o- r: F4 N! c
& f; B7 m( U6 l/ A5 Q) k8 q
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
! }% f) e2 C; F6 e" }: O# T4 G1 S# `7 J8 ~% f! s
mv owasp-modsecurity-crs /opt/tengine/conf/) _0 C! }; b, k4 u

! R4 A, Q! Q6 ~2 P! Scd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf0 C) l5 _, j3 u( b
2.启用OWASP规则:4 n8 ~$ G2 C# T! @# e3 R" @  _
/ d. w7 f7 V. N0 G8 s: D; {# I
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& t/ W+ x2 M  U2 @
' `1 r5 J+ g# W' I$ F" E) Z
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on! l/ [" T& q, p9 \
* x6 g) u# p! e+ `+ J7 g( t
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。+ [6 i" S6 o0 v. v+ Q' H! t+ j
' R  ?6 g2 g* G# S
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
: S' P) U) C& H! T! WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf0 B5 _, g. n: _# E3 k% F4 o4 g5 I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
: G* w( w5 O* ]3 x, A; zInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
: E- n" V1 v( c6 O+ M8 ~! ^0 F$ ?Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
4 v2 Q# M. c2 h3 {( O: V% h9 L; tInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
. k; Y0 A9 Q9 NInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf9 i2 n) Y$ I0 I$ P- @7 E
五.配置nginx
9 ?: w( `1 p8 t2 `6 c/ N
0 s# |- P# E1 ~2 |  Q在需要启用modsecurity的主机的location下面加入下面两行即可:  N6 Y8 v: z0 B# T0 @

. W7 B% V2 F$ h' o  A! a7 n, c: _ModSecurityEnabled on;  8 F. ~. q$ F; h2 b0 ?, R
ModSecurityConfig modsecurity.conf;
+ N6 V. f! `/ ~  u) v+ ?, k下面是两个示例配置,php虚拟主机:
' `: T. `9 i3 ^/ X1 N. a- i4 L! @1 r2 }" j
server {! M& j" l' ^$ D% t! _+ L6 J
      listen      80;
2 j# A- f: u6 f! [8 [1 C! b5 _6 c      server_name 52os.net www.52os.net;
+ v* q) G/ F0 |2 J4 R1 i3 S     
8 n) W7 W* u1 E      location ~ \.php$ {
! T7 w2 g/ N$ a0 {: }5 g      ModSecurityEnabled on;  
8 l4 J3 ^; ]$ f: I0 A0 n! @      ModSecurityConfig modsecurity.conf;
5 {5 n, y( h7 h' c( N' n  [; \
# V% o8 F2 @* f: g      root /web/wordpress;
3 V8 I) g" |" h2 X      index index.php index.html index.htm;
9 O( o& D$ c/ c1 @; I. m7 @& x  
, q' {2 G5 `9 |- U, L      fastcgi_pass   127.0.0.1:9000;
; h7 X/ D: T/ k) D3 e+ j/ N      fastcgi_index  index.php;
) y. j0 U0 y( }! U+ ~1 P2 o      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;& s6 z- e: r, `+ G% _4 t
      include        fastcgi_params;# `) |0 U" e: \7 u% y$ h# E
      }- [1 t) n8 ~) m# e$ q5 @! J
  }
& ^& f. H( S( t8 ~3 _8 {upstream负载均衡:* U) K& T4 q! f3 h0 m' G  G

+ Q$ A8 o# F0 _$ Iupstream 52os.net {0 v$ g5 k- j7 J$ ~
    server 192.168.1.100:8080;6 S4 m% d; d: Y7 L0 |3 _
    server 192.168.1.101:8080 backup;
' v9 ?, m' k7 y, I) L# P+ ]}4 O3 F. d: J, r; F( e3 W. R, n4 b

8 f2 i) x# J; q# z+ ]# tserver {- u) P: X5 q2 P; K
listen 80;: j' c$ J; {) A' c. ^4 C
server_name 52os.net www.52os.net;" e! Q, X$ ?3 b

8 x9 W" X/ u" n9 ~% p, [/ Glocation / {( q* E1 O( p" \. H
    ModSecurityEnabled on;  
# E, S# ]2 B4 Q0 a+ R, j& A1 @    ModSecurityConfig modsecurity.conf;  6 h7 X: f; P/ R* i. a
; M" b3 U) D; Q* K& G$ t8 e# `5 w
        proxy_pass http://online;1 u4 ^4 U6 n0 r4 q+ k: k/ u' c7 O
        proxy_redirect         off;6 ~; P" ^* P) O* Q% w
        proxy_set_header Host $host;
+ ~/ `+ u; x" T0 H% U& k; m& [! u        proxy_set_header X-Real-IP $remote_addr;- x# z; E( S# e' o' Z5 ]1 o' j
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
6 e# c$ m. k) c; O+ l    }
! ~) X1 x6 d2 g7 Y/ m}
. p. Z- f/ W( u1 i& B. r  h六.测试
- E* q1 n5 }; P1 c$ z; ~( V' ^# g! H7 _- a2 e" ?: j
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:) a/ e* L$ [/ w7 \
# U( F: ]" o5 I# c
<?php  a4 q) W1 B- `; t
    phpinfo();    $ x" \- V$ h. M7 O" b6 A
?>6 @+ j# p( A) W  T! `4 Q) Q6 O5 ^
在浏览器中访问:( @  ~8 }( B( A
- t3 f! k, J4 r
http://www.52os.net/phpinfo.php?id=1 正常显示。
6 x5 W5 R8 p$ K" i) chttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
. `7 r$ a6 _& L& }! ehttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。! p7 ?  a* ?+ X$ K1 {
说明sql注入和xss已经被过滤了9 Y  o; w/ p+ r
$ n% r6 M0 e. |7 L  M! b6 U
七、安装过程中排错
' y% k- c. q1 B  a2 [8 e; ~; K* m, ?# S
1.缺少APXS会报错
: n- F: R2 O+ J6 ]! C5 I- K9 o: u" H1 f
configure: looking for Apache module support via DSO through APXS
7 G: k. A( P2 y$ c) Sconfigure: error: couldn't find APXS
0 B: r. M. S! j7 I8 W0 Y9 xapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) V$ a  f6 G/ i
解决方法:/ P9 ~( S1 P1 ~; e

/ @* |* _# }6 z0 B4 w. vyum install httpd-devel8 r. d# P" @% k2 a. G) u2 s
2.没有pcre
$ T" U/ j* d# a" f; j
$ _- u5 t6 x; g3 {( N: yconfigure: *** pcre library not found.1 ?7 F! h$ I( M" f" ~4 n
configure: error: pcre library is required" {' W7 X& @3 T+ q# m! {! i
解决方法:
. R3 d8 S1 }* }7 @8 X1 ]
* _0 z/ u1 J2 g4 e2 Myum install pcre pcre-devel3 o% K6 w; n/ `/ y$ G4 u3 t1 G
3.没有libxml2& N0 p6 C, i! y. ]' l; \: `  g
- l9 o! [' p0 C; Q
/ Z$ b7 j* O" s
configure: *** xml library not found.
8 W5 `* O/ i7 X1 y# l( p0 Tconfigure: error: libxml2 is required
0 S; y; i# p# z$ D+ w; q8 X2 ]解决方法:3 b: _5 O; b  ^

# _" y# h- [7 Eyum install  libxml2 libxml2-devel
8 D) |' s. X7 B2 w( Q4.执行 /opt/tengine/sbin/nginx -m 时有警告: O* A& C5 B# g: V! ]9 L

6 G' t- P1 ?( |, b, ?Tengine version: Tengine/2.1.0 (nginx/1.6.2)2 l  ?. ?( `7 S% U
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!6 b9 L, G$ i' ~
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ l$ F* S+ C9 a1 c. @* }7 u9 ?" T

/ H/ P1 q4 _% C7 Z. d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.6 b" s+ ^8 k3 G! Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
) b1 p. i# a' r+ `2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
% a$ F5 W0 \; L7 W2 u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
2 n/ h% g, S1 V3 X' S' i3 j2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"+ P0 @" \; n& T9 J' J
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.1 T5 M- M  i+ X4 `5 [' k
解决方法,移除低版本的APR (1.3.9)
% l5 A0 V2 F8 \2 `( F( x6 f7 _! K3 q; ?; F6 c! C
yum remove apr
6 H9 Y) ?& s& O& E# a5.Error.log中有: Audit log: Failed to lock global mutex4 I" w+ J" E; u& W  V- B( ^" X* B

: h" R; U: }; N2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     * x- }* o& S4 Z9 B" c3 i  |% z
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ J8 T; S# ~! ]7 v, K* P; m解决方法:
$ j+ f$ I! H: K) t( a1 A编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
" c9 ]; O& t$ E& o8 m9 Y% Y
- t- _  [! r' H5 |7 qSecAuditLogDirMode 0777
7 x& s8 j5 s9 ~; Z% Y$ A* E9 {0 t7 t1 @SecAuditLogFileMode 0550
3 h6 G9 Y& b* A1 GSecAuditLogStorageDir /var/log/modsecurity
) l; l2 p$ J* i1 l3 hSecAuditLogType Concurrent/ \& ?* K; m0 p" x
参考文章:3 j( |1 o- Q; S
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX1 f. {. I$ V& P
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-12 04:52 , Processed in 0.067785 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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