找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13071|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: n6 f" a6 o3 D8 E/ g' t  D
! |4 p9 ]" |1 _+ \8 O一.准备工作
7 V& m/ T! T( @8 @$ w& q( V
7 `! j8 c; |3 C3 V. A2 m# _系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0  b5 m4 Q3 q* u: H
& |& d% _& U* I) t* k
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 {" k' g8 o" f
1 N( ^+ A( P4 s& D
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
2 d+ g* N; l1 t% V0 L" n) l+ c7 C: C$ k" M8 F
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs0 b* F- P) C* @: P& F9 y8 O
! ?) C$ R! p  {) r
依赖关系:
+ H+ B0 {) c! |; a9 P3 Q7 utengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
$ g7 R( h5 E: m' ]7 r/ |0 R* Q! H+ V0 E8 k" q' [- W
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel/ t1 b2 b# ]" M9 ~* e+ K
modsecurty依赖的包:pcre httpd-devel libxml2 apr
3 K- C& {2 l7 H1 t0 o' f/ R, |  O- B9 Z1 Y$ R* o# B3 |# O
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel0 a: G. s" A* A: B  j0 I
二.启用standalone模块并编译
4 |4 N5 q) Q# e. E; s1 v  g( p1 \0 z+ ~' Y3 Y* v7 U5 m
下载modsecurity for nginx 解压,进入解压后目录执行:
8 [9 g+ {$ P- F3 i; U9 t6 m7 i- s  W1 Z4 l1 s/ I& W/ @% t" ?
./autogen.sh
8 N( C% H: L5 r./configure --enable-standalone-module --disable-mlogc; b( K8 w- o1 k
make ) @7 H. V9 [( P1 Y
三.nginx添加modsecurity模块: {" {5 y+ J1 g2 f

  i  c: @. y1 v# p% q2 x& q在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:' V: g; f( Z6 M. {

7 o$ R# W$ b: a5 H; V./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine! n; r: s% s5 g2 W4 L4 ~5 F
make && make install8 Q4 V4 I9 h1 d4 m4 l1 C3 `
四.添加规则
  [3 W  B0 R: x# [! G/ V
2 ]* ^' N4 @5 n6 U3 Y& A0 ]# nmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。1 {* U- E2 b9 b7 m- y) o
# a' h$ f+ q8 h: I  A2 N2 B
1.下载OWASP规则:0 R& x/ v* f7 h  x9 i
/ u4 D) a6 C+ L2 c$ I
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs5 A( P# K8 I- _( F' Z* p

3 m. e% g' S3 L/ u' d  tmv owasp-modsecurity-crs /opt/tengine/conf/
/ T+ n" @( V' }2 z2 q% W! W4 @/ q. ~" S7 O) R8 @0 S
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf. j. e8 f) [' x# E  g
2.启用OWASP规则:
9 ]$ c0 i, w- B& Y4 E+ Q, [& m( Q6 ~4 ?' X% B7 v# ~
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
8 j- R/ |6 o; _' t2 x9 s- O8 [; k) S; M
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on6 `  S; L9 x6 q/ g) p

6 `" M% R- o1 N! J! M2 uowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
( o/ n; r+ `) R$ g
% J# S5 h7 o# N$ ^8 g- f) gInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
7 }0 Y$ G6 H; y  p( }7 n! eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
! ^* g$ V: b% L9 Z& q, Z8 c% CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf$ P" t& B1 ~( N" H' C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
4 {( i& q1 K9 t* v* sInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
5 S7 a9 e) ~& qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
2 a# |6 T( Y2 j& g; O8 b4 wInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
' m5 ~  d" r* v- z+ c( B五.配置nginx5 J( i# A. x+ A* @+ {  S6 x6 ^

" g2 ~& D5 e, A8 j在需要启用modsecurity的主机的location下面加入下面两行即可:
7 O, s  Z" b' E# b
' I, I- X* W4 D. z6 LModSecurityEnabled on;  
* U% C- J& e  YModSecurityConfig modsecurity.conf;/ v6 @7 k3 d0 R# N& [$ q: g) X
下面是两个示例配置,php虚拟主机:! ^- m. n' X7 c2 x! ^

' k4 c7 N' G; `3 Qserver {
4 Z  R6 D+ Z- E! n1 V9 r( A      listen      80;
1 {0 M/ h) w% a1 U$ a5 n6 P) `( |      server_name 52os.net www.52os.net;6 _  E" V; p; I3 l* @* q* o
     
8 q; R) C* }8 G/ C; j      location ~ \.php$ {
5 b$ Z. u. K2 o! r4 n      ModSecurityEnabled on;  / J3 [9 a9 u; P2 N
      ModSecurityConfig modsecurity.conf;# M  c  ^: x; b. }' `( l7 D) e

# Q' \  g6 `# h7 ^9 H, s: {$ U      root /web/wordpress;
+ G8 c* Z3 q; u5 X- X) X  |      index index.php index.html index.htm;; t5 z3 S6 a* y) _
  
! U# Z/ n7 W$ f' D9 |      fastcgi_pass   127.0.0.1:9000;
/ u6 v9 t2 _5 O( V% g      fastcgi_index  index.php;! r2 f+ Y6 ~) V+ T
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
/ ~# p% n9 O# l5 T. ^% z      include        fastcgi_params;) S! e, R' n( |. T: K
      }
. |9 F" V4 k- y2 _  }; Z7 R4 K3 ~+ Z1 S$ r  i1 V. R
upstream负载均衡:  l3 q+ p6 I) A+ l9 e
6 v0 k, B# p# ]( o* R: N
upstream 52os.net {2 s7 g0 W7 E" l
    server 192.168.1.100:8080;- i+ r, J3 K( n2 t
    server 192.168.1.101:8080 backup;
4 [: G: T/ p9 V" @% u}
2 N7 |; Q; v) H3 Q" d  g7 v8 d/ X1 I& ^3 S# E0 e# B3 A
server {
6 _$ A9 Z; [0 T5 hlisten 80;- w1 H% g5 p$ s3 _# Y' s$ {2 n
server_name 52os.net www.52os.net;/ \  Y3 {, \+ \" J# W

$ L" b7 d  i. \  B9 Ulocation / {4 i! f* x3 }; J& Q7 {( a# U
    ModSecurityEnabled on;  1 |; W0 a4 D, R1 M) l3 Q
    ModSecurityConfig modsecurity.conf;  
0 z! b+ H# l; |; Q) w: a4 a2 ^  ~# l1 y# R0 V3 m
        proxy_pass http://online;* n& O, O$ d8 f+ O2 ]
        proxy_redirect         off;6 P8 G/ r5 e& Y, X3 H
        proxy_set_header Host $host;
8 E+ i/ l/ i2 H4 C1 h$ G( H        proxy_set_header X-Real-IP $remote_addr;
0 G" T1 D8 G( X0 v8 S        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
& d' g! q- v% }  z3 M    }
4 _6 ?% f5 E7 L  `! [8 P}
( x" a5 o" c( i) }8 F/ o六.测试
0 g6 u( N& c% K8 t3 {, Q
5 Y. ]' l6 Y6 \我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:9 G) z! I# p. T! u
6 a( V( p! [( ?3 ~; ]0 J
<?php, p% O1 F' M6 L/ I2 U
    phpinfo();   
" t$ b, m& P* O+ ?3 `4 z2 @, d?>
9 J3 n  q3 ^1 c% K$ c在浏览器中访问:  l4 m9 C, n1 W/ H% S( Y

" W) H3 n( O+ Qhttp://www.52os.net/phpinfo.php?id=1 正常显示。
$ C1 H/ @# e: K, o' I8 Nhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。/ r" X4 m3 F  y5 x
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。$ e5 y! F$ x" a9 V" V; w
说明sql注入和xss已经被过滤了
% w4 ?# W7 q& |- {4 u" P6 }) R- U2 A  z  o' ]; g+ a
七、安装过程中排错
, r; U7 L" ~0 T5 j+ J# q: {1 p5 Q5 O6 t) u
1.缺少APXS会报错/ E' Q9 x# o. r* n& W
1 r6 ^: |* N: b2 e) f
configure: looking for Apache module support via DSO through APXS) O* R  s6 R: m8 ?9 \8 n8 C2 R
configure: error: couldn't find APXS
( o) l0 |$ p  zapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。- d& a+ d( [3 H. i0 m
解决方法:/ {' z2 y  r4 O

8 V6 }. k. g9 Y& b- x8 X0 ^yum install httpd-devel
/ x! ]- k6 K2 o7 u* Y2.没有pcre- I3 r5 @3 z/ ?: t' f6 x
, D9 f+ m/ }9 ^* c1 D' w
configure: *** pcre library not found.
5 y$ G" t0 N" T& e/ Uconfigure: error: pcre library is required
/ J% p  c# c; s3 y& K解决方法:
5 v9 e4 e( J1 J8 S6 Y; l: R$ K9 t; D& ]# e* u& r
yum install pcre pcre-devel
- H" W0 A1 W& Y3.没有libxml2
6 D8 p  o0 i8 j7 u/ `
) u) u9 J9 r. B# ^0 L+ u0 h. y* b, K# r( i" C6 {- s5 N
configure: *** xml library not found.
0 W- k/ |3 J% T2 G/ ^9 z9 xconfigure: error: libxml2 is required& @$ t4 v8 v* [% j8 _
解决方法:
* b  i3 n1 D7 A4 Z  Y
* U" p  J- w8 _5 t2 Xyum install  libxml2 libxml2-devel
  V/ C- i( [. n4.执行 /opt/tengine/sbin/nginx -m 时有警告
. k! i$ E7 p3 k" ~: s) {4 s
9 D$ e: F- ?3 u6 X$ A4 w) ZTengine version: Tengine/2.1.0 (nginx/1.6.2)$ f9 H3 {6 m# h
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!) y9 y/ Y0 E+ {/ F- S1 T- S
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
: i9 X- G. Z6 F9 p! w6 y" e2 c* u' i) D# M  F8 N: B: l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
# `$ ^( J9 G  B& B; q+ a, I, }* }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
* u* C& P' H/ A- W. O2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; v5 \: E: G, w: f5 u/ d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
5 a/ {$ e# @" \- q$ H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
% v2 b* Y9 J4 z, g  T4 F, F3 Y2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
, n) n3 X1 M1 P. Q- x  F解决方法,移除低版本的APR (1.3.9)9 G+ \, X$ J) x

/ _) |1 a0 P/ N* kyum remove apr
  \: D; l3 ^  o5.Error.log中有: Audit log: Failed to lock global mutex' J! U* n4 s% j* |
# r* i. X% n. e8 u
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
6 ]( Y. t/ D' V5 Cglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
5 M* C0 ~" r' ^7 {7 m1 Q6 P: ?8 J+ ?解决方法:9 T2 _% `6 a& a8 o' o2 @6 z
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:# ^, [6 y& y  N; H

+ k- j- i% y9 LSecAuditLogDirMode 0777* }* }" ^$ g! S; j* W
SecAuditLogFileMode 0550
8 [; G) \/ Q* A9 n# X/ L. |SecAuditLogStorageDir /var/log/modsecurity
! t2 _: W% v1 M: T7 o" E4 sSecAuditLogType Concurrent
. F7 }/ Q- x/ U, ~  @参考文章:" O+ e, ~5 ]* D- O- C) }: u' p/ x- j
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX: u+ ^6 {. }2 f9 O1 b2 l& E) X1 Q
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-10 19:06 , Processed in 0.048699 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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