找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11646|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
, ~! S% v# C6 d7 y
/ I2 n1 m7 d* q' g5 D4 v7 ]一.准备工作+ o* M8 O+ |' c: P  T0 C

0 ^: g% l3 h1 U3 Y+ t系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
$ K& w* n' A4 q, E  Z& x; N3 m* o! e5 v8 y" s5 k2 e/ u
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
2 e: @5 T( t5 f( b& r$ u: W
! Y0 ]  @/ V& l! ?. zmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz+ N9 p% X3 r7 _+ z$ j* o/ |) A. S

' G7 H) e  v( ?1 f6 E& P7 XOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ N7 H* p' I* ]& U) t
/ {$ r& q* w1 R# m: `3 \依赖关系:
% [" Y6 N1 F2 r! F5 x3 l- A* ^9 }tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
8 ^4 \) k8 ?' q
# w& i0 u5 f$ f. \yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel! }1 D/ M  u% Z7 B
modsecurty依赖的包:pcre httpd-devel libxml2 apr* K+ J1 K9 I. w& ?, }' j! @5 ^" Y

' u- q6 G; V7 xyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
: O0 ]: R$ {1 J3 C8 Y" w. B二.启用standalone模块并编译7 y! h& D% Y6 E. A

' O6 P2 x9 `3 @) s, e& T; t4 T下载modsecurity for nginx 解压,进入解压后目录执行:
% f' ]2 P7 h' q
& T; Q! h4 B# i, ^./autogen.sh( {+ e2 z# \  O! R: M6 Z
./configure --enable-standalone-module --disable-mlogc% B# N: G. [' h/ C) J
make * P3 ~( x! g* \+ J
三.nginx添加modsecurity模块
6 a( r0 ^' z8 ?0 f8 N0 B# Y) P7 R6 l
1 F6 C1 Z6 w7 g7 L( m) A$ b在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
) i/ }0 i$ b0 V5 W- [: z3 ~' }
1 p0 Z. [, p& S- d./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
6 c& V# k7 H2 f8 x) {make && make install/ X$ N9 k$ \. }( L
四.添加规则
2 j+ F, S2 q9 s3 s: Z' Y3 A: G5 t. `' w% \
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
) w: [9 w7 B5 X" B! `+ g* L0 O" Z
: v! I* H  i1 z2 \( x1.下载OWASP规则:5 }$ A- A4 \9 ^

$ s  ~8 |+ L- b; D& @5 dgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs5 q% N* G& h$ }' \
( [' G! ^2 v- y5 D
mv owasp-modsecurity-crs /opt/tengine/conf/! l1 [9 V/ O: K

+ i/ x9 ?) n4 i7 e# \cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf9 C) g2 N  c7 Z
2.启用OWASP规则:
- {0 s7 S1 Q; [0 y- e. V
* ^( }. Z( Z) S/ U: @复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: {/ ^  r8 B% J: c, N2 \
) a" l4 \( G. w- M8 L$ u+ b编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
; |, T2 {. x6 Q+ I1 n! O9 ~! ~; z; A. f3 O. J* {: a
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
/ t- C1 l$ F+ B: |4 i% t+ }% J! _4 H3 K
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf- Y  w! A* \$ K6 Q6 L+ r# O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf2 Y3 u- F8 J1 H0 c% `; k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf9 g; W) D6 j8 ?9 U& B" F
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
8 j: v0 i. _( X/ _/ _7 ]/ qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
: r/ O# k3 k$ h5 b% A" z) r6 iInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
% I( g8 `+ P3 Q( E& |2 P& q) BInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf) x9 A# e4 Y0 l4 l; N
五.配置nginx
1 R5 d# F- c$ \6 z
" s1 f: f( o# H& I在需要启用modsecurity的主机的location下面加入下面两行即可:/ T5 b* a  X5 N4 Y0 T! _# _6 n

: {  l* H$ G4 [8 h$ R* cModSecurityEnabled on;  
6 \0 b7 s$ r0 ]+ r/ aModSecurityConfig modsecurity.conf;: O: Y% ]* W! E* S2 S
下面是两个示例配置,php虚拟主机:! [3 J( t6 x; n2 X2 l

) B6 B/ a6 }+ }: B! S6 Fserver {$ U- _/ M8 {* l6 B$ D% u1 y. \
      listen      80;
: Q6 @; Y3 y  X, [5 }4 q% C3 B2 s      server_name 52os.net www.52os.net;0 ?: w9 e9 P7 o8 ~5 p
     
( g5 l* F: v. `1 _1 {9 P% j      location ~ \.php$ {
8 c8 E" P( Z9 B      ModSecurityEnabled on;  / ]- `# S* Q; F- [( @2 h
      ModSecurityConfig modsecurity.conf;
% \9 U! r4 N2 k/ w& c- Q5 q0 ~
! Y' U- `' z+ W9 u  Y      root /web/wordpress;
; Z. E% H& D! p* k' w% `      index index.php index.html index.htm;
/ l2 T( S0 e% G) Z, n/ ]  
$ u: V9 J' x, o( i8 H: \2 W      fastcgi_pass   127.0.0.1:9000;" W5 ~: X) |; o6 i$ o3 p2 ~- x$ o
      fastcgi_index  index.php;
6 U; W/ d) [4 Q; v0 N      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;+ E) a# F" m& `/ g; i! M/ z, `9 Q
      include        fastcgi_params;2 v5 d. f' u/ V. H1 C
      }
/ [) m$ J5 i9 V  }
8 O5 V2 a' @7 k6 E9 \upstream负载均衡:
' a3 ]% ?. l5 N  t$ `3 r9 j* j' q5 C& A; u/ L
upstream 52os.net {  f. ?; c& I5 O
    server 192.168.1.100:8080;
! r  D% o8 Z+ u: R    server 192.168.1.101:8080 backup;' P! x3 F7 s$ m% p! b7 u% Y& B. S
}
1 a/ H% {* l: _$ g3 w
( R2 v! I# g7 E+ B7 ~& eserver {
8 t/ Q" x2 Z; {listen 80;: t8 g6 [4 N' j) z0 u7 v) S
server_name 52os.net www.52os.net;
1 \6 L0 A- d; e6 o6 X8 R5 x
' [/ V& O9 M4 z" G5 s; M$ m8 l+ ilocation / {0 W* j0 ]2 e  @; y* W/ f1 Z
    ModSecurityEnabled on;  
3 F+ M4 x# ]8 q7 s6 p    ModSecurityConfig modsecurity.conf;  
- D6 N+ t) h! {2 `$ I5 m. M8 W+ E- R/ V: N  M  L& J. {+ w
        proxy_pass http://online;$ t5 t7 @/ O2 c: I) w  k9 s
        proxy_redirect         off;/ x& O! @. b9 E& @& \
        proxy_set_header Host $host;
7 E4 L6 _8 e, }; S% |" X# o) b        proxy_set_header X-Real-IP $remote_addr;
  {7 V# L0 u# t: u( E        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
: Q4 x0 r3 Q, h    }6 M) `$ A1 ?; Q% [# ?" p7 |
}" E0 `* T) l* y$ ~5 u& Q$ R9 r8 D
六.测试
) Z! d. }0 K# F  E& {. Y, T9 F! R
7 I8 D  U, p, ]9 k我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:0 Z3 T! ?/ v' b0 V+ q

" m! s  x# B- h8 m( y" |<?php8 \5 h' B) b9 V0 v
    phpinfo();   
5 P3 U2 G" n8 F( S- g?>
6 N* g, o) R& a1 O6 n/ p% `6 m; `在浏览器中访问:
! ^1 x) Y$ ?0 j" x$ t
8 Y2 Y, O5 `4 Ihttp://www.52os.net/phpinfo.php?id=1 正常显示。
" a# t: m* h4 Q0 F" Y9 `4 {4 Whttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
/ U5 A" y2 D9 s. u) hhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。7 o3 |- G2 V( N
说明sql注入和xss已经被过滤了
) T1 O) n2 e# l) }, w) _  [( M. p) K- g* H1 J& e) W
七、安装过程中排错' n# f9 k+ _; Y9 \# T% m; k
  B" {, W5 ?2 N) F6 r/ ?' `1 u5 a
1.缺少APXS会报错
  @+ n: b' @, J5 f
) N+ h! b6 I) v+ A6 L% Aconfigure: looking for Apache module support via DSO through APXS1 C1 `) m( M: J0 |7 P1 }( _0 {
configure: error: couldn't find APXS
% u6 V$ b$ t1 Oapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
/ ~5 K7 M9 K& z2 s- x2 `  i解决方法:
3 A& ?7 N! Z4 c! M3 Y/ v4 J4 V5 Y5 N+ M$ O* m& ~! W
yum install httpd-devel! e# O' U' Y% d/ V5 O( C" \( ~/ R- R. Q
2.没有pcre' Q$ R9 r- G# \- d" a, e; C

+ E/ C+ i2 }" L/ ]configure: *** pcre library not found.; Z, }; E4 c: [4 x/ A  @! l
configure: error: pcre library is required
' g: a0 E7 ^% T, y* n. s  w( T# w, d解决方法:
) J8 V( a) o7 f2 x; ^8 @7 k- d! z# z
" J! Y2 @6 f/ e: Gyum install pcre pcre-devel9 a, E7 L% C6 ^$ [- d* |
3.没有libxml2
1 k) [* o3 V# f, A( e  R. R: v. t7 U1 E  X

4 x- }% L% j* A9 aconfigure: *** xml library not found.- q9 D  ~3 D' D; P
configure: error: libxml2 is required, ]: C- Y5 e# ?# N0 O) i, C
解决方法:
: I# Z  E5 @9 C/ \' r; V4 M
8 h5 p4 ]. }. Q% t8 o* ~yum install  libxml2 libxml2-devel
! ~* X" g! W$ ^9 o* Q5 h& ]% Q4.执行 /opt/tengine/sbin/nginx -m 时有警告# z2 b% ]; q( y$ R5 ~* H
4 |+ P, _4 G7 S1 _. B. B9 d
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
  P8 J8 Y7 M1 B! [4 Qnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
  f* v3 n- @5 b* I原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
# i- X2 Z6 r, B4 ^! L( S: }4 e7 k: r: M! {$ u! J, d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
' G& t- Y/ |, u0 m/ Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
. j0 C4 J; t& [$ J3 L2 w2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
3 ?+ k. Y# o! }( H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
$ l6 \3 L3 v" |! u: v& w( W$ Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
+ Q3 ^/ e' h2 ^: @# ]6 _* x9 W- v2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.; X8 \) w" x! j$ f6 L# N! O
解决方法,移除低版本的APR (1.3.9), p+ r& h) o7 p# I

" P5 S# D+ S+ a1 X6 i9 eyum remove apr$ `5 b5 ?: `2 A
5.Error.log中有: Audit log: Failed to lock global mutex
5 |) g( ?/ G6 F, X; R
, h5 z5 N2 V7 B/ X( E2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     7 R2 |( t+ H4 L
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]( G" ?2 z! e6 K% `
解决方法:
/ ]- h& U$ B, N0 c2 d编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
, w5 h7 x9 x7 v9 \- C
8 b6 W" P  v. L5 |7 OSecAuditLogDirMode 0777. Y# H8 m' T' @1 T% K  d( D, H- Y( C
SecAuditLogFileMode 0550
9 Y. G6 J/ w7 m2 S+ sSecAuditLogStorageDir /var/log/modsecurity
* ?6 P) ?; a) c1 NSecAuditLogType Concurrent' {& O& F! |9 D9 k
参考文章:
# `1 D4 a/ `1 I( \https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
4 B9 i5 ~. f2 A3 shttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-3 11:25 , Processed in 0.062714 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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