找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12322|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。6 `$ v7 Y- X6 I& p! w0 p* {- E/ q

$ f$ |+ c6 k% s2 b% I' H# _# x一.准备工作: M' Q* D# x# r& S

7 |) @6 a0 |! l; c& `: m系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.08 J  E0 N! I. Y  a. M; I
; A5 Q! V' L. \1 }7 I  S3 U7 J$ s$ D
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
/ [) y( U, ~/ |$ `7 y
/ J/ t  |; r! I) w# i8 qmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 H4 p* d2 @8 |1 N* @6 L9 o
. t# B) A1 f- v
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
. s3 N* L) H' G8 Z8 T1 i$ e
# B1 g: [! H2 L' ^8 H6 B依赖关系:$ O6 A3 B0 a% G8 G7 K5 H
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
3 D* b0 ]  f) j/ r  U- ?- [. W2 ^) B% S3 c. d' J) V
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel) B8 f  y, I; k2 y/ C. `( U
modsecurty依赖的包:pcre httpd-devel libxml2 apr
6 w/ h: `1 p7 n. a
" w! A5 G2 G+ f  X/ {, g8 r+ vyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
9 S' e; y8 W" O5 h& C二.启用standalone模块并编译
# X3 |1 C$ T- `: [4 S3 r
- ^' k4 V. j. Y' h# p+ C8 {下载modsecurity for nginx 解压,进入解压后目录执行:
3 {8 H1 J8 z  M
2 K4 d% L( @: P+ N./autogen.sh
+ Y8 _! @; _1 {7 Q  I./configure --enable-standalone-module --disable-mlogc8 R& x7 @. L: @8 y
make
9 t% R& K! b3 Y. M! |三.nginx添加modsecurity模块
1 z* q6 E. V: I; _7 m9 C/ v% p& c2 H4 [
4 E" E3 l- P4 F6 t+ l" f  S$ P在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
. V  T& T- }: w
3 [0 t6 A8 J+ R& T1 o* E2 n6 X./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine: w5 C6 ]% B5 V& b
make && make install, r- v; p# [9 Y4 |% Q
四.添加规则
$ B1 g7 K% i  w2 r( f
+ o" }8 I( ^; |modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
' c; W" z" x2 Y; F3 i* b) i9 J* e- w7 z5 `, K
1.下载OWASP规则:! O% @! u9 G. \& G( V" g

7 k# {1 w# T2 Xgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs! u/ X1 T* Z; w( f) }# y
8 {; C' R: K7 n$ ?( w) \2 Z3 S2 B
mv owasp-modsecurity-crs /opt/tengine/conf/
! P% e  v  S3 n3 v1 I! n# J3 ^# E! o- h& @
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf& B( Z+ \; {  m! r: F
2.启用OWASP规则:" O0 C$ x6 ?3 @+ A% ~
: q+ [; L- {3 Q
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) a6 {$ B; R2 r  ^2 |4 q7 p1 w1 e7 Q5 T3 ]) k" e6 P7 d. A* D
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 m+ ?' {) d0 `. ?( c- j2 J) r

; \8 f6 m7 @% M& z4 x2 bowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
, o4 R; e5 t' Q) p0 X/ t9 @6 c/ K0 f) d# N1 P7 M
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
( t# ?8 q1 y+ Z+ r2 F$ pInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf+ _3 A/ n5 s1 s  Q6 c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf5 J9 R- L: H6 E3 s* ]: V
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
: j5 T) b+ `2 |8 o2 V4 H2 uInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 f- q8 I: o) b& N1 P2 o% BInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
) @. |9 b4 ]; \Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf% w5 m/ G( }0 D3 _+ m
五.配置nginx- d7 B  m# u( o8 @

9 y6 M1 J% x& @) J  K4 Z* J在需要启用modsecurity的主机的location下面加入下面两行即可:( t6 Y3 G* k1 @3 }2 C5 U. S$ D
- c- V' y: e( k& F; s7 s
ModSecurityEnabled on;  7 T! Z( {- a* T2 w
ModSecurityConfig modsecurity.conf;' h2 C0 _+ A6 c& ?3 N/ t) m
下面是两个示例配置,php虚拟主机:
4 P/ g1 _! e1 T- L3 i- x+ ]- |6 m, p& y. f) U
server {
0 o2 R6 ]. K8 B* o5 b" [      listen      80;
  B+ ]  R, m2 \5 N% A0 F      server_name 52os.net www.52os.net;
. n# r. _' M6 Q2 n     
* [7 q' ?3 f+ H$ ^4 h; I3 P* S' P      location ~ \.php$ {- Q4 E$ w; k" X7 }9 N  B
      ModSecurityEnabled on;  : d: v8 f3 R: ]5 ?, p# |
      ModSecurityConfig modsecurity.conf;; [' M0 ]: T1 r3 S

" A" M5 F* m& v- Z( U' P; u      root /web/wordpress;$ s9 J, O2 m, d- A4 K6 t3 s
      index index.php index.html index.htm;: G: D" h# m0 p$ l
  
' q# p$ d9 L- L5 B      fastcgi_pass   127.0.0.1:9000;  v' Q' N( N0 g9 g. s
      fastcgi_index  index.php;
- }) M6 x5 ^6 g      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
/ Z% }. \0 o9 o$ P+ T- `      include        fastcgi_params;& t6 }; N! B& X, Z1 D* @
      }* e: O; q/ H3 b
  }
- ^; s0 G) c+ E  \2 |7 A% w% w! F/ Rupstream负载均衡:
3 ~2 y" p# J5 _8 E9 ~. ?$ \
6 k! p- i7 v* c  g% h# |4 {$ Mupstream 52os.net {2 x, l& h/ T  o. g- K& D
    server 192.168.1.100:8080;
1 n. t( X& a! ]2 h3 j7 S7 ]    server 192.168.1.101:8080 backup;
; I' W6 z" G, U+ e2 r}
1 I2 @+ x% ^, f1 V( K& l$ k3 O5 E+ B! w' d0 Z; y4 \2 c
server {
) d8 T0 _* z0 t! _5 T  alisten 80;
) V" ^: w! x1 r- I7 k5 m: q6 \server_name 52os.net www.52os.net;
4 d( n* t# F1 q* i9 s& d3 i; C4 l5 J$ L- u. D& p/ n: Z
location / {
! a- c" u! d& I1 w0 j    ModSecurityEnabled on;  
+ t3 |5 X: y; O2 `% N5 V6 f    ModSecurityConfig modsecurity.conf;  
( ?; j, `; f" G3 l: S
5 e" K" f1 X7 q2 C$ |+ R        proxy_pass http://online;0 a' y( Q. E4 p# [: S
        proxy_redirect         off;
1 ?+ y/ n7 D" R        proxy_set_header Host $host;/ X1 P# v( P: z$ l( g/ V
        proxy_set_header X-Real-IP $remote_addr;# }: L9 [. w  s( x
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;0 {1 n- \( ~9 I# U$ w& v5 n4 Y
    }& Q. y# x9 {5 O( d' z
}$ F$ j3 H( H& P" h; w
六.测试
& I! d4 l2 v! h1 L  T$ u0 @- t$ ?/ C6 m2 X$ ~9 e# K
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& f4 A$ t* t9 M$ v& l* P# e
( q6 n* Q  }0 n% \1 E* |( |: V% j<?php: ?4 d+ Y# j$ v  X: F$ {7 L
    phpinfo();    5 [. S& }) r6 _/ t" |
?>
. n; Q. v# W5 P2 [7 _" j在浏览器中访问:
. Y& y! ^% m$ U1 y" U9 i8 {
/ C8 P$ G9 o, zhttp://www.52os.net/phpinfo.php?id=1 正常显示。$ c4 S6 J& j2 f) K, s3 m
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。) @- ^! w2 O# g) n' V
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。- G; D0 r) n9 n- D" _% h
说明sql注入和xss已经被过滤了3 }5 A, e1 Z6 u- Q4 K& A% t

. Y. F# [' G( Y% _; h七、安装过程中排错* S0 b+ s4 v$ w9 E2 _: X$ k9 f

; t# t5 I1 t7 T$ x1.缺少APXS会报错" c8 v. Z, W  E5 G! z
  K: o- T+ I" l" V0 x+ ]
configure: looking for Apache module support via DSO through APXS: |" }1 t5 N8 `# E; l" e* \0 V
configure: error: couldn't find APXS' B( b, e' p, J# k. b
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
, {6 x6 q5 ]. Y% x解决方法:
, K4 T% ]- I" X4 B1 `' u( }) a$ r/ h/ X5 ]8 l  h$ a  M
yum install httpd-devel. M/ ~6 C8 W5 A, K" d
2.没有pcre$ f1 d" _: d1 U
1 S$ Q$ T9 U1 [+ O" f% a7 `4 q
configure: *** pcre library not found.
6 N  T, j. {- d, M7 rconfigure: error: pcre library is required7 d6 h# I: @6 S" f2 r
解决方法:/ U( E9 y) d& _) e5 n
2 A) x2 m. B* r+ u  t( n
yum install pcre pcre-devel8 d( z! N2 S  q% Q
3.没有libxml2* X. O* @" W0 S$ |) I$ P0 ?
7 @" E7 l2 w$ l1 u/ ~" E

% Q0 f' B2 b) Jconfigure: *** xml library not found.( p+ i* E0 j7 A5 M( \) {
configure: error: libxml2 is required/ |. }& U+ p$ J4 v! M
解决方法:
" m% a3 [0 r  S2 `. w
0 J8 @- [6 ~9 c  b8 dyum install  libxml2 libxml2-devel% K  z3 j" g" z9 J6 Q' H
4.执行 /opt/tengine/sbin/nginx -m 时有警告8 n0 D- u6 Z- f) \7 c6 T& v) N
% i$ S7 x5 @7 p/ Y' |5 F$ t
Tengine version: Tengine/2.1.0 (nginx/1.6.2)4 N" w1 B, d2 K% P5 ]- O2 t  U0 E
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
. N' w# w1 F5 @1 T  t/ U: A. v7 I原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log! \2 o7 X6 J6 M

7 u" t6 ~2 j/ ~' q9 f7 `2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.; Z% e* O- E$ x; z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
8 r2 Z4 I2 n% d9 T- a6 u2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!0 d1 J; a- O1 N% X
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"5 w  y4 o7 w9 e- U! Q) z7 ^5 F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"! v6 P5 O4 e, B8 \. E
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
4 F9 Q: f* u6 b: P$ R/ A+ c解决方法,移除低版本的APR (1.3.9)9 c1 }2 z; j: Z* A: b  [
& M+ l9 B. ^7 N) r3 D) P% `8 e
yum remove apr: c& j9 h$ o# f9 I
5.Error.log中有: Audit log: Failed to lock global mutex
' [1 x' V/ ]& E6 L9 J* f. L4 D: j  T8 k# h. Z4 W
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
5 K2 G. |* H7 zglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]4 D$ e: W: d( p  c% H# {& j( D
解决方法:
: J0 r' g% u& V编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:% G+ m) H2 i* \' P; E/ g$ p: `
; d, C, R' q  v
SecAuditLogDirMode 0777* v; V) P2 z  F; V$ r( i; C
SecAuditLogFileMode 0550
2 c- L' M! ~% z, S2 T4 fSecAuditLogStorageDir /var/log/modsecurity
5 l4 I" q* l' Y% b8 J# d+ [# TSecAuditLogType Concurrent
$ F) P! k, D& m& `参考文章:+ q: a/ X3 b. a* M1 j/ \( i9 h  p- \
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" y0 E- ^! K. l- K2 @
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-26 17:30 , Processed in 0.068782 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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