找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12210|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。9 s1 g' j, v# |1 C2 ^6 @+ n

% E! e/ k7 T& n* _$ y3 B/ k一.准备工作
2 |( g- u% G4 J' W
% r( f* }# K1 O! h$ [系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
0 z1 F! q* i0 I- M: w% m' N# o! w5 T2 p. }
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz) [: f1 u4 S' l
7 @9 P. a2 i% x" H) N
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz4 j2 V: n8 ^/ D, o) j  W
8 l! H# L, D2 Q6 Q+ y  c! {2 ^6 l. c
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs$ z2 j- X+ F* r+ a+ a9 J

) |. m* b; z6 O2 c2 n8 v依赖关系:. I" C+ X) ]2 K9 V4 E1 H
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
0 b" _& k* M% _  z6 Z7 c: i- }& @" l; t9 I& _4 _* d9 Q& i
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel0 |0 b5 ]/ F" Q+ l0 X/ [
modsecurty依赖的包:pcre httpd-devel libxml2 apr
% z/ i7 L' g# n: @4 K) Q# d! Y) h$ S" ?5 H) b
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
% z! O" z& g; P/ I( X9 r  Y% X二.启用standalone模块并编译; w( X+ z3 c1 p' w2 {! @

: S, Q0 v0 ^3 w" Q) o) j下载modsecurity for nginx 解压,进入解压后目录执行:
# w5 }+ }7 D4 m# _( Z7 b/ x& l( c' I! V6 ?+ R
./autogen.sh
. D5 c+ u: t9 }. F1 d./configure --enable-standalone-module --disable-mlogc
2 D- d1 N7 [+ X. H/ Jmake : A$ t+ o6 R/ m' q1 t4 T! p# \
三.nginx添加modsecurity模块
: G" B$ }" C, W  e+ ~% J+ F! x9 B" v: Z% D* }( }" y1 F4 L
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:" l' z: f7 k0 ^2 b: R/ F; }

: O# q) R. N; F- F* ~6 f4 B: r./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine/ m+ E' S1 O0 e8 `& b
make && make install
) \- K, l) l0 m( y" ~! b2 f& Y# Z- [四.添加规则( ?1 a; B. B7 x: Y, z
) @1 c0 `6 c; r
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
4 B" H2 X' E8 R( K$ c9 |8 }. I" F
7 A" q( Z$ x% [; M0 b1.下载OWASP规则:) w) k1 }; t0 f
3 b5 i  I7 u, X0 S9 o) x
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
( d$ z- S/ a, O" P
+ R/ a! m0 O+ |4 x# v# fmv owasp-modsecurity-crs /opt/tengine/conf/
3 i* q7 v5 E8 E7 C: u2 Q
0 V  w* y0 T" U  ocd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf, b, V8 G; _" m$ _( T
2.启用OWASP规则:
) j6 \3 _' e+ Q5 A+ l; k" }7 l% Z; s9 Z& R6 T& z1 P
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% q$ Y; b( G  i8 A* {( f
3 d: ?& |; \- ]$ w4 \4 W
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on0 g0 d2 j( |3 n' H; u) Z

0 w) X2 K! u+ Vowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
, F2 N( Q4 G' {1 G' x0 M) Y8 p' m0 N4 l: l3 T  m6 j
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
5 M1 ^: k# n% d, D' jInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
. C3 Y3 G0 Z6 j, TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf1 g4 _5 X3 K2 B$ z# Q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf# b5 W* s5 d! ]5 }( I9 ^. l$ _
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf8 y. r* x: X0 N' s
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf9 Z% B8 k% \( k4 [
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf5 Y: i; b3 L) A0 R
五.配置nginx/ _' j- ]( Z/ A' a+ w/ z
. U7 Q1 m4 H# u; z: }$ e. y
在需要启用modsecurity的主机的location下面加入下面两行即可:, B$ g8 q1 m+ k: _3 `

( l7 S3 z, B& X& fModSecurityEnabled on;  7 g* I4 V3 o# T( n/ \) O1 }
ModSecurityConfig modsecurity.conf;# ^* n( _  e' Z: v
下面是两个示例配置,php虚拟主机:' ?4 J' u$ r  @1 K3 L5 v  H

( M) h9 Q! x% _) o/ Wserver {
* e) c1 h3 T/ v! `( {9 ]- |      listen      80;- A  e# g) r6 y* ~, r: Z/ C0 R
      server_name 52os.net www.52os.net;
+ t+ E4 O% @  g$ O/ A) f$ i' \6 e/ P     
; }: q: m$ k: J+ y( w4 K2 H      location ~ \.php$ {
2 L9 W( G9 Y0 L; t      ModSecurityEnabled on;  
! j' L( |  Y* X/ }6 b      ModSecurityConfig modsecurity.conf;2 U" |& s9 t3 @+ c8 n
+ F# B& \5 J: A- s4 S9 Y
      root /web/wordpress;
* A3 c4 P( H: c4 ^" Z# ~. q( A$ B      index index.php index.html index.htm;
' t; I' g2 Z$ A( c- i1 G  
5 `' p" w, x1 E- `+ Z      fastcgi_pass   127.0.0.1:9000;
7 r, g, d" U, J5 C( `      fastcgi_index  index.php;
& p& N6 M& c3 g' j      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;9 Y* M5 v6 K6 Y0 `( c3 }& T
      include        fastcgi_params;
$ y/ q% Y5 d, h0 \% ?+ }4 z7 q  @& a      }3 x# o8 V& w* c$ _  Z" f: P6 H0 i' }" o+ f
  }7 Z6 w' e; x6 {8 _+ g5 g! O
upstream负载均衡:8 G. {! W6 R) o" V

- {& N" [+ A, hupstream 52os.net {3 s  t$ ~! y: N# G
    server 192.168.1.100:8080;
2 ~1 Z; y8 @1 Z9 h4 B    server 192.168.1.101:8080 backup;- b" T( |% A, v% i3 x! m
}) {  _# i) j" a1 a% _

6 A3 I2 w: Q# L* x: _server {
& O) N& S' W; g$ R" o7 w) E: Wlisten 80;' P& P3 n9 J% I0 |& T) \- k5 `1 k
server_name 52os.net www.52os.net;
0 K: p/ |2 z; s) `/ w3 i
* y3 p. l& ^9 H9 ]location / {
- J+ d; n' z6 x    ModSecurityEnabled on;  * U8 h8 r% [6 E9 o
    ModSecurityConfig modsecurity.conf;  
- I  j8 Q3 G/ ?* j
: B5 L# j, ?1 Q( x        proxy_pass http://online;. _# ?. h5 J. q* @$ H' @# r: S
        proxy_redirect         off;
( }: X1 y- Z; n% p$ _6 F, K2 {* L        proxy_set_header Host $host;
/ Q% u5 k: q. c) J: O1 \        proxy_set_header X-Real-IP $remote_addr;, V& {2 J7 Z2 b  I- z
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;0 v: X3 {0 C. _) s. I/ N
    }
. t1 o9 j/ C6 ?; @* q}! z/ Y: O& s7 k& F  @% ?3 R. c
六.测试. T0 |$ C/ q" o1 M8 o, i. K: I

1 D% V% H4 P: g我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
1 T$ @9 M; ], C4 ]3 r6 s, W% O3 M! M" d, {" n
<?php3 t3 Y+ j# A, q; ]
    phpinfo();    : G. `" D$ j5 P5 i9 Q3 z
?>
( a: h1 ~/ ?# Z7 ^- L1 s在浏览器中访问:
; g* y  U: i; S% u
1 z3 G& G2 f7 N9 Q$ jhttp://www.52os.net/phpinfo.php?id=1 正常显示。
" K9 j( w' u% O# Z; ohttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。& S8 @# a, R/ H8 c# }
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。9 c2 S* s9 j" Y" w, k0 V
说明sql注入和xss已经被过滤了
* B% q! U. F1 P& ?% l4 s: t! O5 a2 d  H8 k, I$ R. t  D( M6 d
七、安装过程中排错
4 b; [0 u3 y" z$ L' D( |$ W. m  e( Q2 U; O; r
1.缺少APXS会报错" H1 s5 t* @7 J9 j- n

6 j7 Y; k" V2 C6 ^; Econfigure: looking for Apache module support via DSO through APXS+ s! u: a  a) s* h% d! K9 S
configure: error: couldn't find APXS! c1 J6 c7 a2 D  w( B
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。0 ]. P" H8 f4 u* `2 s2 h
解决方法:$ i9 j  w2 W! h  x2 z  P. A8 z8 w
# @! X0 u) y, a1 O
yum install httpd-devel
' S. j5 k7 R9 f7 `" R5 C9 F% I2.没有pcre6 J. x& P5 O( }8 h& {! [2 B3 U
7 a- d' n# J3 i. ^
configure: *** pcre library not found.
  a, |4 A+ Z" }configure: error: pcre library is required& h, i# V' ?3 ^+ o5 B" l4 m. H. b
解决方法:' w6 Q! E8 B4 _9 M/ F

1 J( e  E, w% y3 C. t2 wyum install pcre pcre-devel' `+ U' Z8 m2 F8 E) \7 k* m
3.没有libxml20 n8 i  w1 c. I! W

1 R4 q' j1 S, G5 {8 x5 Y. S* Z* }' \( K& K1 i: s; z- Z' B
configure: *** xml library not found.
/ g1 K. U  Y0 k9 M# [5 a: X$ nconfigure: error: libxml2 is required
. X9 h0 I! q" c! V1 y7 y解决方法:
% [8 U4 `7 y1 M' v( t( B6 M4 J; o8 W- ^
yum install  libxml2 libxml2-devel
" z( s2 e! X* F( q; z4.执行 /opt/tengine/sbin/nginx -m 时有警告& L( f% a; V' H7 B: g
/ H- U# X% n" l7 W4 D
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
5 ^! E: W) R6 |nginx: [warn] ModSecurity: Loaded APR do not match with compiled!, ]/ O# m2 R. f2 b1 O
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ O& b+ N# [* g
* b' p  _* h. q3 c6 U: M. R/ i4 F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.; e4 e% ^# R& Q8 n8 k" Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
3 I, j) ^# ]9 c$ R9 k1 n) [2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!4 N1 j# a6 o' y$ h, D9 E' M8 S* u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
4 Q* r9 C; A! P* C; ^8 e8 O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
3 A" j9 @; Y7 X& L. {2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.7 T( i: {& G# N0 ]: [
解决方法,移除低版本的APR (1.3.9)1 G! H2 H4 @! U
$ [0 o+ w, V$ ]+ Q$ ~) w1 J* t2 t
yum remove apr: \7 o% X! z" _# g. y* {4 p- g
5.Error.log中有: Audit log: Failed to lock global mutex0 d. _) M( a  C
- v* c* T: E( V5 z6 f8 t
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
- C' V& P9 [# f% sglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]" X/ R+ A1 e( a0 y; B5 i
解决方法:' x; a' p3 a4 ^" p0 b5 N
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:% F; B) R) U- e7 K2 a
/ E: y4 X+ Z4 y# H2 e( D
SecAuditLogDirMode 07772 o$ i  I8 u* ]/ q% p7 X7 F
SecAuditLogFileMode 0550
9 E  o$ g# g8 O& e. h0 B1 WSecAuditLogStorageDir /var/log/modsecurity
% P. {4 B6 Q0 I! e% ?. q5 wSecAuditLogType Concurrent
4 t1 k9 d; F$ H3 W+ J' ]参考文章:
/ }2 C9 I  e# Vhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX6 t! c0 ^) ]) i1 D- r0 ~
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-16 16:35 , Processed in 0.071496 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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