找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12811|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。1 w; e5 ]; C" r  I. ~+ t- l

$ p! m- m4 P6 t, i3 X/ Q一.准备工作
# L/ C% _0 s; q6 {9 p. _
6 n! ]* S$ G" \( o系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.07 S" h4 X+ w7 h+ {6 a

% M/ C1 `1 k5 E$ [( ~, \( M3 Ttengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
: N* L4 r7 Q+ Y  }0 Q5 z+ b
5 R( T3 @. P7 B2 l( m9 j$ Qmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz$ O( F' n: F  u

6 l, o9 J* [( h3 ~OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
; a0 r6 c: }' G0 U+ h
- _+ N" ?. p  M' x, L依赖关系:
9 o1 e% x! b& K' V3 s* Jtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
; j/ t. M$ E1 \3 K! B/ u
/ M# k& u0 q! @+ Y5 T4 o, k  J4 Dyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
% v6 f9 _; C6 {' n! U+ |7 \2 Imodsecurty依赖的包:pcre httpd-devel libxml2 apr% s1 |4 T& c7 R- Z! Z- q8 M

0 \+ a% J4 f6 M8 k( pyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel- \# x' D: }  d1 o% e
二.启用standalone模块并编译
8 O8 o: P9 ^3 r8 E" n
7 B8 z9 X) S: `- X下载modsecurity for nginx 解压,进入解压后目录执行:
$ `% V. q' P7 C; B+ i8 ^6 ]% N
./autogen.sh& w( s4 x# K' I4 g0 @9 \
./configure --enable-standalone-module --disable-mlogc) E8 d4 W. u( G4 M( q: Y$ Z
make
2 B- z1 h: ?$ ?! p. e6 l5 z( T三.nginx添加modsecurity模块9 s, R; n  v6 d" \; p# a5 a

8 @3 U: p" \: ]' \$ x在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:; p& d: G7 _1 a$ K/ R& S4 Z

, `2 h1 V# U+ ^./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
# Y5 y$ \/ S8 A  Fmake && make install
) u- n5 V, q  b0 ~; t四.添加规则
8 @/ O5 ^: Q7 g  g# ]' ]% G
7 J6 C: O+ |- I" d8 Wmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
% s6 w9 E3 e$ G. a& U
/ l+ Y6 K/ K: N1.下载OWASP规则:
: D9 B1 L8 f6 ~' |' {8 d1 D$ Y, w- K, z! }
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs# D8 c0 Q, J! w" P7 I, B" k) `3 G
" B" t0 m; M' S3 t1 K% f
mv owasp-modsecurity-crs /opt/tengine/conf/
2 u  j$ G* I6 L+ f: @
$ j. O& _, v/ D6 B& D; `& ycd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
" [# k& N) @6 H! g" l6 G2.启用OWASP规则:: H, Z- T% E! |9 N
1 t0 S5 M  [4 {) i) J; u
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 }( {" {+ F# k0 f$ G) n  e

$ B1 o- _4 ?5 U& B& U! |编辑modsecurity.conf 文件,将SecRuleEngine设置为 on1 k, ~9 ]& F( V: L+ K
: o% N* m, Z* |' j6 e: a
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
+ u; H% x; a& |! Y* [3 B3 _  Q# S3 P  m3 v8 u- P9 m( h- z+ F1 g
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
# \( s9 e" {  b# [( T+ O9 kInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf% Y9 F9 F! g; X! E6 g% r3 I! k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf, Q& n, q- S2 K: E; }
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
; \) o2 ~  q4 x2 qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
4 F+ ]& P1 ^' D0 y  o! p& jInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
) K& O6 v0 r' I6 |2 t4 s4 L& I( \Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
* \% i8 I* B) J五.配置nginx
# B. Y  c0 w. \0 x- q) i
4 D  W9 W1 d9 M8 ~; H. L' x在需要启用modsecurity的主机的location下面加入下面两行即可:
- }& D& e" ?, [- F& K, ]* a: l' Q) E0 f( x2 R( J
ModSecurityEnabled on;  
6 t; h: h8 T8 Q8 n' s0 S9 g8 {5 p! iModSecurityConfig modsecurity.conf;7 d3 e& \$ ^, B: S3 H( a: ^; h( o
下面是两个示例配置,php虚拟主机:0 a" I) j. Z7 `" L( Q1 e3 {$ x
+ h; B. l: ]: {0 o/ o+ s4 I) n% q( z
server {; w; }" Z" I7 q
      listen      80;
8 l0 b2 x- n! ?0 l      server_name 52os.net www.52os.net;2 v, K/ }' N, \& X$ k' r5 P
     5 r7 p, i* I+ k! M+ M, _  G
      location ~ \.php$ {# z- {. {: b/ q9 ^5 Q
      ModSecurityEnabled on;  / d# a2 T8 m6 w5 E: [
      ModSecurityConfig modsecurity.conf;: w6 D9 r6 u* s- S# W

6 L) D; C* {% f+ m      root /web/wordpress;: Q7 B' H: t/ j: n; O! ]- y2 `3 ?
      index index.php index.html index.htm;
, F% d4 T8 W% T+ G# r$ n  
( b5 V" n. ]! q6 O9 @, [6 ]3 @      fastcgi_pass   127.0.0.1:9000;8 V! C' M, S3 J3 G8 [$ X
      fastcgi_index  index.php;
( g5 i3 I+ X" ^      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
: \' e/ c# u! U7 v3 P      include        fastcgi_params;" M! Z4 B' L$ R. x" V- X, s$ t0 g( W4 \
      }& O6 h1 @0 ?' l1 N; S
  }
5 I3 E) H7 J& A8 ]7 {5 @7 Fupstream负载均衡:
5 ?1 ~* v9 h. V) {/ a. H' F4 `0 T
upstream 52os.net {
0 i* y  U. G- @" R2 ^6 ?! Q: l    server 192.168.1.100:8080;4 C9 _3 u  Y$ d" _9 Q1 S
    server 192.168.1.101:8080 backup;
: |" o% j3 v: V6 B$ |5 D1 q3 T}
# X" N% n' f) X: p' X
/ m6 a) R! J2 f3 o" A6 \" V8 jserver {
0 Z/ Y6 ]. ?( _" k. clisten 80;' U5 u$ m- F$ l
server_name 52os.net www.52os.net;' p3 J7 q) @# e1 Z% Q
7 o/ k$ E7 u- T7 p
location / {1 @. P% c. j6 k4 N# V, o9 \
    ModSecurityEnabled on;  * e2 _/ `0 M$ ~% k
    ModSecurityConfig modsecurity.conf;  / u, O0 _* H6 d) x6 G

* `* ?/ I+ [; s  ^# O+ X, [        proxy_pass http://online;4 `- T4 V* S: i) {- i3 L
        proxy_redirect         off;/ D& {# @" R0 W2 q
        proxy_set_header Host $host;
5 _& B6 A# s4 s2 U        proxy_set_header X-Real-IP $remote_addr;, G8 n& q" l# J
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;& N7 m8 k+ T2 W9 s- t
    }( t) x) G# U; @* B+ n- l0 O
}
1 m+ U0 u  s+ B, d0 r六.测试
( E/ z% ^4 K) `6 V8 O
6 U( |, O4 Y, K我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:+ [5 N6 n" z. L" l, v

6 r5 |6 L6 r7 u6 N<?php$ b) I) R$ B2 R$ q7 G
    phpinfo();   
7 n0 i7 P* ^3 [) y( ^1 _?>0 ~( Q0 F) L7 r* [* R. G5 {9 z
在浏览器中访问:. X2 z0 P2 p3 p9 ]4 T+ v% e- O: \
) `  V0 i( c7 G( y" \" |
http://www.52os.net/phpinfo.php?id=1 正常显示。
  v6 Z9 U9 ^. Q% u. t. X5 T( Zhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
" i7 e* K: ?! [% m% r6 Vhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
  o) H" J/ t' ~2 A8 O" [: d7 s& X+ X说明sql注入和xss已经被过滤了
5 E% \4 M3 x. g# W' y3 C0 }/ _7 _) J8 s. ^/ B$ \$ @& l! k
七、安装过程中排错
0 {2 Y5 B. N2 x/ Q% y$ c( w7 W& b( y& m: q
1.缺少APXS会报错
" k  Q: U5 B" Q2 s
- j  V7 ]$ f) b1 q* d9 I# Tconfigure: looking for Apache module support via DSO through APXS+ K- r' i# n# z# ~5 [$ {  \* I
configure: error: couldn't find APXS; g% s  `8 }* `! W
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。3 [/ c* T1 C0 D' `& B; l: b9 a
解决方法:
6 Y& h& {6 _  Q1 C
  [3 \+ Z/ ]" F: q2 Kyum install httpd-devel
7 k: r+ W7 ]/ W5 J. H2.没有pcre9 Q; h" O0 {  F2 x

! H; Y1 R% r! b7 p4 r/ q3 }+ Pconfigure: *** pcre library not found.) Z& w8 f5 F5 x+ o; j9 {) q
configure: error: pcre library is required8 |+ I1 ~4 s; F9 W* L: c; F
解决方法:
9 G; U6 k: A, Q2 c
6 Y9 d; ?0 ^5 @- R" _yum install pcre pcre-devel* r  K8 D& L$ q9 t; q* D
3.没有libxml29 n+ M. l- S1 ^9 `6 p

3 h( R/ L, \4 ~% ^+ a: \' v$ O) f7 M% E% e; X
configure: *** xml library not found.
- e. N& U! n# ]3 }configure: error: libxml2 is required" \+ n! s) }% p% J4 f! p" a
解决方法:
2 c/ w/ d# V- T0 C
6 c) \' `- d) S- ^4 Uyum install  libxml2 libxml2-devel" V, k7 O! F1 T& _; m7 Z, J  J0 x
4.执行 /opt/tengine/sbin/nginx -m 时有警告
  W  `% ?2 Y3 o! ^( j- h( N7 t  D. \. P. H/ c* C
Tengine version: Tengine/2.1.0 (nginx/1.6.2)8 D/ C+ h/ H* E" f
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
& _0 B; R2 [5 r. j, T原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log, z. x$ l; ^  ]( w

: K$ V# h8 W4 r0 f, m" h( X8 C3 t2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.! r6 Y1 t  Z1 j# r6 \" H- z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
, U1 k; z- A' f/ y0 S2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!& W" S" d5 R/ w9 M3 f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
& X- @+ T, x9 @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
7 _9 t) k5 O: l) |& u+ e4 q2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.- b% ]8 S2 B. j7 A$ q+ ?0 D
解决方法,移除低版本的APR (1.3.9)* ]* p5 B* j( g$ a3 T1 {
- v9 N' Q! E: f9 ^# N' d
yum remove apr6 P. d/ ^2 v, Z
5.Error.log中有: Audit log: Failed to lock global mutex$ {. @( p+ P2 D1 w% h2 M2 }

  ]4 j$ ^) a/ Q8 f2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     + y6 I8 m( b# L/ K7 Z' W& b
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
6 o3 I. {5 s( t, r3 n; b解决方法:
. E1 r2 c; Y2 Q; L" k编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:3 [1 K* O. M6 B7 L

* Q) G' q2 o6 G4 i- p4 l: ^( mSecAuditLogDirMode 0777
5 _! n6 ?7 j6 z: f1 T3 YSecAuditLogFileMode 0550
; l7 H% V4 L: U- Q$ i' FSecAuditLogStorageDir /var/log/modsecurity
/ i8 l3 e$ e2 t0 f+ `SecAuditLogType Concurrent
8 f; N* P/ g7 ]+ _+ B参考文章:
: e6 q. x, \' i% l4 G( chttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
. j7 T; l- @; |7 c( Z- E7 Yhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-14 06:38 , Processed in 0.063880 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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