找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11630|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。% S9 x" E% f( c) u: {

6 Z* G7 x; b4 a一.准备工作
# w) z' u4 |' ~2 K/ B3 ]7 e  x. \- r' p
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0, w/ D5 o+ P, U  e! ]6 e

) W0 U* u6 n7 r1 s+ Wtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
: ]( N3 L) z8 W8 ~8 x$ j& l/ o
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz* C8 Q2 h+ g9 E7 n

# X6 |8 h3 S/ r. UOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
4 B! V/ w; J" n2 l* m
" V% d# t+ F4 J9 O依赖关系:: O! r, H, k1 P6 k3 x
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
! ]3 [( E: b% F+ T+ A$ V8 K; B# Q5 f* d9 ]+ ~, R! [3 j  `; a' k0 d- ?! ~: y* T
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
* x, I3 H! `! ?$ `+ rmodsecurty依赖的包:pcre httpd-devel libxml2 apr
$ [' [) d; _/ p$ [0 C7 o. M  u' y# D' q/ Z, Q
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
5 s- V/ l# [7 j  J( K* p二.启用standalone模块并编译
* u: b7 C2 s9 M0 t; C# W) q, L0 D7 `: r5 D0 w7 Y
下载modsecurity for nginx 解压,进入解压后目录执行:
2 q+ O1 a% B: O& s0 k  I/ ?' t! N
./autogen.sh
" ?  v4 \0 m. L- Z; j9 ^3 }# y./configure --enable-standalone-module --disable-mlogc
* v# u( B9 ^) U" G% A( omake * _  u- f: z/ F$ ?1 P. |
三.nginx添加modsecurity模块
6 b# F8 J( c% m3 U0 {2 ^5 @* e& T0 k1 D. P" Y( `
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:+ C3 y$ k# r+ ~! ?& L" k

/ Q2 h- J# q% n9 r9 k6 W- U0 k./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
7 J& n+ v$ |5 z6 x/ u% a6 f6 Amake && make install
- f2 s$ h. ~9 e" L5 F四.添加规则
0 y' L& ^. N7 w( H7 m5 n* J! `" k# l* ~2 J
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
+ G; V8 z) t7 f) W) V- b; K
1 I5 r7 S. L8 E; c& i/ L1.下载OWASP规则:
; v8 S$ u9 n0 i' Y1 G7 e$ j" ^
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs) a1 K. D" a9 \3 j+ Q) W, v
: q% W' v. {$ N
mv owasp-modsecurity-crs /opt/tengine/conf/7 v1 g: d$ j/ ?; x
, u6 Y+ t* @. b7 U. f+ }. F
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
2 z: M; m  {# ^2 H2.启用OWASP规则:
2 {! S, s" h/ |: o4 `+ x- X# v2 H
+ k" \! P8 ]( W0 ]复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
$ i8 [- e) [( ~! W9 Q0 c6 [3 }4 Z7 i0 f
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
9 o- D2 _: d* q  j7 Z. g; M, A+ z; v  t, T4 u7 c2 c
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。# q6 z* ]0 m* E/ m& C3 ]% v

* l1 b8 u( p$ U3 }Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf0 ?! L0 ]$ E) `9 s% u; U- z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
3 y( P; ?8 n2 Y2 i$ l2 }3 xInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf) B  Y! X0 P3 B; j# }6 X% h4 x, Q# o6 |
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf4 U/ T* b  M* B8 H. a: a
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf3 o2 S! z3 C4 e$ q5 r+ ^% K
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
, R0 Q* @" p9 x6 S1 o6 h2 |Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf5 U' n0 F2 N! ?6 O6 ^! d
五.配置nginx  A! Z) L0 l( t  P

% @' i2 }+ X. V- S6 M在需要启用modsecurity的主机的location下面加入下面两行即可:. D8 Z* p% B, y5 x# n% Q
" T" T9 `) O5 J/ t
ModSecurityEnabled on;  
5 p9 X3 t7 P" K7 ^! XModSecurityConfig modsecurity.conf;7 O! R# D+ T- c
下面是两个示例配置,php虚拟主机:
/ G+ W3 W: F. G% J( K$ m- _6 ^# B7 j/ f
server {' C' f8 k' J) A# i; R, `4 n* A
      listen      80;
3 |. s) i( G$ W1 d7 h( J      server_name 52os.net www.52os.net;0 M! y4 A+ V/ n1 M6 s/ @9 A
     
) |7 [. B" N# u3 H( h5 }; L      location ~ \.php$ {! C) T  b. P5 d3 p3 v( {
      ModSecurityEnabled on;  
: n5 ]" J8 v) S* v      ModSecurityConfig modsecurity.conf;. N5 r/ H* y/ Q9 ?  A+ G

3 A5 E: E7 `6 W. D3 Q      root /web/wordpress;
! ~: [5 \( ~# w9 l& X      index index.php index.html index.htm;
& D/ r9 P, D* ]% w) v  O  
/ t8 |6 \% e4 w# }- Q      fastcgi_pass   127.0.0.1:9000;$ u7 b9 u& K9 y
      fastcgi_index  index.php;+ V# X0 g9 g" U, Z/ p1 _
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
+ O8 F  q- n- e" B( ]( F      include        fastcgi_params;
+ n( n3 F" g3 V      }
% m9 e3 T/ h. I- D/ A0 f/ A' ~7 H) Z0 l  }5 }# |) |' G+ ~5 d+ p
upstream负载均衡:; e5 [$ a0 E" x# k- l
6 N1 i" C2 p# a$ W3 w! N) X* q; _
upstream 52os.net {# f/ A' ?: v% ]5 j) @$ ]9 Y
    server 192.168.1.100:8080;
6 i) Z( A) P( Q! R6 X  G2 S    server 192.168.1.101:8080 backup;
! b% Y( J: B* ~" v) R- f( S}6 x  u5 u' ^3 t& A4 r
5 w$ |2 z$ y( f0 u
server {1 Q$ v' d7 t! q# B! n
listen 80;
& g0 Y, b3 U. }6 ?4 S. qserver_name 52os.net www.52os.net;- Z" i% j9 ?, o
% r, ]3 o8 h, I) y9 Q) w
location / {) G: Z6 n* r6 |6 n
    ModSecurityEnabled on;  # o: ?( q; Q# O& k6 ?7 T" K" J
    ModSecurityConfig modsecurity.conf;  
7 X& e  u8 g( l% o% B% W" t9 I* [: M8 ^( V- E+ E# N
        proxy_pass http://online;* B7 `: {( c. `  w( X
        proxy_redirect         off;% N) d7 v9 b  q
        proxy_set_header Host $host;, s( D5 i! ^+ T+ a* C* e+ }
        proxy_set_header X-Real-IP $remote_addr;' s+ ]- U. r0 [# U
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
/ l( F, P( x; `! e- n: M& O    }! \3 `8 }1 o; Z  ]
}1 U- Z6 _; r3 |: o' w5 s
六.测试
9 F) \& r$ ]7 j  z% r5 D/ {
2 X' M) ~! Z* U" F8 v0 m4 U5 f. r2 P我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:/ @# D1 a4 l8 D6 i! U* @6 y
. ^% a* p5 ?$ {6 b
<?php) D  K; _/ o1 F# D
    phpinfo();    5 k8 D3 [1 A  G' o6 ^9 d6 h
?>4 A) @+ t0 i2 M% y
在浏览器中访问:
& b0 V3 M+ l" i  |& M; I
4 _& v' J- X3 z% C- F$ ?http://www.52os.net/phpinfo.php?id=1 正常显示。3 y1 p9 V& ^& Z7 @6 W% k. |+ F1 _
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。  B7 v  i  s$ h' a- n
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。$ h4 X7 n2 u: S( t5 X- W: H
说明sql注入和xss已经被过滤了! F1 Y. W8 H% \7 E2 b2 y4 L
. G& m# l- _9 n6 K
七、安装过程中排错
2 u' q9 M4 ]6 q' V- K' f- }8 w$ `- D3 N' j$ X! H( l3 x
1.缺少APXS会报错' w( C8 Y$ g" K+ f* w! Z

6 J7 n- P. m* a9 x! L# [4 kconfigure: looking for Apache module support via DSO through APXS$ ?" c+ u6 v! @$ Z5 ^+ b
configure: error: couldn't find APXS
+ n9 A7 |" G+ L9 v/ i/ R7 eapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
* B' `* r# }% x* T+ A) \解决方法:
) @9 R3 R: R% p, M9 q% L- s3 u" x5 c7 b. _! X* T
yum install httpd-devel4 \3 `, i4 P/ `8 H, S
2.没有pcre
' s# Y9 f  `- B
$ N2 d+ ]8 k6 k) d/ [configure: *** pcre library not found.
) N0 N7 k5 m9 j4 h! g& bconfigure: error: pcre library is required! Q8 h4 S" D& W! n( r
解决方法:3 o5 W: j  P, B# t) y

1 X) l4 L; U1 q+ f) x. L" iyum install pcre pcre-devel+ ]. \! R% B: E( Q  u
3.没有libxml2, ?8 S; N9 w+ X5 A
0 _! L2 r3 B, l8 t, u
3 Z8 i6 B! B8 j8 z1 r8 [
configure: *** xml library not found.. I0 b; ?, w) o' p1 Z
configure: error: libxml2 is required$ F2 L7 b4 ^* N' b3 I
解决方法:
) J4 v5 W0 K! F0 w4 l, n3 h
; {3 }  x0 |' cyum install  libxml2 libxml2-devel
6 H0 P, h* V! v  X2 h  M4.执行 /opt/tengine/sbin/nginx -m 时有警告: H/ B8 M$ N8 h7 d, `
4 @! F5 j3 s# c8 h
Tengine version: Tengine/2.1.0 (nginx/1.6.2)" B0 O; j" `+ E' a% Z) W8 `
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!2 Y& R8 u* V) f8 Z4 k- o
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
% d! G+ L1 B1 [0 L; F
0 S1 ], B& h. r4 l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured./ s, C. g4 N- {9 ]" t( m6 o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"* L2 ~1 c6 T/ b2 Z
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
& p) J5 r! U% ?  r  C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
! `' J2 x3 X  o& L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
9 |2 M* Y7 A& u' a& G$ w& F. v2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
" d3 t% W2 |* i; f- L解决方法,移除低版本的APR (1.3.9), `. h1 N! `+ X4 D7 Q  A5 R+ v

/ T: P/ u" ~) N5 `7 a8 w. gyum remove apr, z4 \" F5 k  b5 F; i
5.Error.log中有: Audit log: Failed to lock global mutex
9 I7 x% v# e9 w# _. ^7 U! N. F" \# Z; p9 B$ y
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     9 s" b7 Z( B* p: u/ L
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
0 T0 f$ }4 n; e9 |6 n3 @解决方法:  t$ G  u: ~7 M- S% `$ z3 ~% J
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 E7 Z$ q. X+ ~5 a, [( n

4 ?6 g; c+ i, s1 b; ~/ ?SecAuditLogDirMode 0777+ V5 m2 P# d* o1 D+ D3 }7 V
SecAuditLogFileMode 0550- ^( \6 `: ^8 [0 _, a- P
SecAuditLogStorageDir /var/log/modsecurity
- Y- s* h* _9 x  ?; g$ b4 fSecAuditLogType Concurrent
' V( n( V+ U) A- z/ B! _参考文章:
/ a$ O& k( a! ~( {  {https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX! q7 A# f; g- K
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-2 07:54 , Processed in 0.074605 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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