找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9722|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 W2 Z) m( r& }1 z: T! Q8 o. L: X2 X6 Y9 z1 P2 F
一.准备工作! h$ C1 W. v, M/ w
$ j% x. n$ [% K0 p2 I9 X/ F
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0' A4 i7 \5 v' D+ K/ r* q/ j. H
4 h& f& Y' y3 A3 }6 Y9 p
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
1 t0 P) N+ l, G; p$ l& a) y# C6 H8 [
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
* d7 }, |" P/ J0 Q  R8 ~( w& M
3 |3 ]: ?- ~4 x7 ~, r; hOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
- W$ K5 {& d0 L2 D0 W
" f1 S8 U2 o9 [依赖关系:/ I3 a+ x& I# [0 F# l
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
- V- p0 E. k. ~5 a! \% L; Y  {' Z. n  z
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel3 T- k9 s) x% t1 I& @
modsecurty依赖的包:pcre httpd-devel libxml2 apr: b% Z4 Y( F6 ~1 \7 h
# D8 b! ^+ ^" l, }) }1 k8 {  K
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
  y, F( P9 [  z  |0 j8 l1 P二.启用standalone模块并编译& b/ ^$ I5 j4 R' a+ ~
  u( t4 g8 v; v! s( {# u% a7 z
下载modsecurity for nginx 解压,进入解压后目录执行:
. u1 W, q! e5 x9 Y" \# u% s, {$ D7 n% E7 S. F) d9 y
./autogen.sh
: M2 |/ t* R1 o4 `8 n$ P% V5 T9 _./configure --enable-standalone-module --disable-mlogc
& ]) r5 v! M# h. e7 U5 emake
& `1 [' h: ~; {2 S* L) u% s三.nginx添加modsecurity模块
; t& W8 a! x; j0 G' a7 P  X& L' l. ^1 w* X+ Z
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
1 S# Q6 L( E6 Q6 T7 M: Q4 l) N( I
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
+ O5 N9 L; J) r: c3 Gmake && make install8 R! P" r' g% K/ p
四.添加规则7 O5 Q# @: t1 j2 a# D' i" J

9 Y$ q/ g! R2 X& Z( J2 E) _modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- V. x! L2 e/ r  {

9 Y& Q2 @+ l, k# [) r( O4 O% H/ E1.下载OWASP规则:5 v4 M/ @/ D5 o" |2 P; T

) a) V1 V  O) G0 zgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
. T/ g  E) b7 L- R0 `' `$ y. i& g' ~3 L# r1 c7 G& H, z; f- g
mv owasp-modsecurity-crs /opt/tengine/conf/$ i7 c' U$ F: o1 {( h8 m. V
# P3 c7 W+ _$ e( I$ z  i: x
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
7 ^1 Q3 o1 E) J4 ~9 \9 p. h2.启用OWASP规则:
  A  d1 j" F0 O$ `# [" a9 ~7 p; f" ]0 l! z& i; R, \0 J
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。# ]7 r; {4 l3 c0 ]3 {) [- Y

5 r4 M- `# Z5 ?编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
) [9 a; R5 O0 P0 ?, `
. h5 D* w+ k* o1 J6 u! iowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
" K4 D& g# \! H  W& G1 L  v6 d& W6 Q( ]( m) G; S3 U
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf+ Y2 y$ t$ a; v, w  e+ ?5 `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; `9 p% U/ C0 G% U1 w
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf. f# B" W2 A/ y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
! W8 M9 D1 z" L5 JInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf& ?1 O9 W/ u; S4 b& r% {9 Y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf  K$ i( q+ a4 g& i+ w
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 d' ^& g1 ]1 q) v8 [五.配置nginx- ?/ c* [8 b' r

% }. J; M; I1 j- G8 T6 v在需要启用modsecurity的主机的location下面加入下面两行即可:) d2 J' {- N- B+ G" C7 f7 j+ U
+ C' W$ Y5 k; @6 k& q
ModSecurityEnabled on;  ! h  ~  k# X; z3 R. R
ModSecurityConfig modsecurity.conf;+ L  P$ N# p, N+ Q6 H
下面是两个示例配置,php虚拟主机:: l( X, E9 G. L4 V* v( j
; W, b: N) k+ d+ U. t" o
server {
. k5 J2 s) K0 ?- N+ R      listen      80;, w# r: v# w& K, F  s- z0 o2 _
      server_name 52os.net www.52os.net;8 m% k; a7 K+ o, F1 S* d2 }  [
     , x$ n0 |. ?6 \$ k0 e- X! @' J
      location ~ \.php$ {
  r, e0 P$ i$ e+ F      ModSecurityEnabled on;  0 D4 k5 u* z" C
      ModSecurityConfig modsecurity.conf;
7 o. h, w9 k3 |! S) `& i1 M- T1 b2 I. i8 A1 ?0 @& i
      root /web/wordpress;
" E$ s" y8 s- ]9 D- x: z9 V6 ^# j. o      index index.php index.html index.htm;
& ?  v, |8 y- W/ ~& i% [  
# v" e; \8 ^: ?      fastcgi_pass   127.0.0.1:9000;) {+ N" l# t$ v
      fastcgi_index  index.php;
  y9 s8 O5 U2 @      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;% ~& |" e  y0 X  Z4 v1 C. S
      include        fastcgi_params;
* ?% T; V! J+ B$ I      }
. N( g) J2 B- @+ z6 H$ X3 e1 P  }
3 P1 z8 M" h+ v8 gupstream负载均衡:3 m5 f/ e  {. E, a) Q  O

6 j' c  Z  U) I2 Fupstream 52os.net {
9 `$ V2 J7 W; \0 S: a# _$ z    server 192.168.1.100:8080;
! h# h; o% a5 W* j: d$ d( ]7 J. d    server 192.168.1.101:8080 backup;; n& D5 M8 l( [) O3 I, x# p
}9 `# i% \. R* C0 W9 @  G' h* X

6 i+ ^, B+ @: O3 M: V/ [( P; w# C4 Kserver {
5 L: z3 m- s! w/ @* Plisten 80;0 k1 |( n( A+ S6 p
server_name 52os.net www.52os.net;* i; c! J- e. s5 ?+ k) t
1 u2 T, e' s( O+ d* l# y$ Z6 K' t! o
location / {; G* L1 D7 h" R" x" V8 b
    ModSecurityEnabled on;    u8 @6 x6 T( r2 M3 Z/ n
    ModSecurityConfig modsecurity.conf;  
( s9 K0 z% B4 `! X9 ^# ?4 J; I; a* R2 Y( ?
        proxy_pass http://online;
9 s0 U3 q) ~# f. V% z9 q7 x        proxy_redirect         off;
6 m0 y, G* |! D& r. x        proxy_set_header Host $host;
! M, n' u, H. d        proxy_set_header X-Real-IP $remote_addr;
( z8 w# x$ \, J/ y# Y! W9 S$ F; X        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;( z5 O/ c6 d/ k& H
    }
4 Q0 d0 ^  j0 `" G1 j: ^7 ]}1 d, N& h" g- _' i  a
六.测试
3 h5 B! R8 `/ G6 w- w/ d* D* C( K; ~- F1 X
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
% n* p* m# r  J4 S( l. |! U; A: b6 F! w0 M; S8 f9 s
<?php4 f. ]- m& x; [$ B9 t
    phpinfo();   
% w$ M% t! X- d" }0 f( W?>9 J! L& `# [, W+ Y0 O+ u# Q0 y7 A
在浏览器中访问:$ S" l2 x: U" k0 v' n( W4 N+ x
. \& E/ Y8 [% h! E: ?) s# \
http://www.52os.net/phpinfo.php?id=1 正常显示。
& Y! s; L5 R9 X9 E) z  Thttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
. z1 _9 X- U" `) m( khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。4 a: v3 C9 ^9 `4 \: k
说明sql注入和xss已经被过滤了8 ?7 j+ X% ?  r1 [0 d) k5 ^% _4 P

( z' e! j3 x# P6 n3 u' i七、安装过程中排错
% C& }3 \* Y. d4 ?& U! c: `# a
6 h( j8 ?* _) i# [1.缺少APXS会报错
9 F' w4 Y. @/ a5 \0 x6 O- R
" N$ L2 d# \# bconfigure: looking for Apache module support via DSO through APXS
. t( d6 s' I% C: q: Rconfigure: error: couldn't find APXS
0 o1 T+ g1 P2 Z: \apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
- ?2 f9 n! R" F; c. H) q解决方法:
8 C# x/ q+ ^4 @! S+ M* X4 s
1 [0 G& _' ~% z! y5 _. L1 pyum install httpd-devel
* T9 t$ d3 y  d9 |9 K( X+ o9 h1 u$ Q2.没有pcre' t2 A5 \( X, I

& m$ j+ {( \2 ~2 W0 a; i1 H3 fconfigure: *** pcre library not found.$ S) Z8 c1 |5 U* y+ a& Z' s
configure: error: pcre library is required
- X) t2 N  |) X解决方法:
7 D* x) W6 K2 t) i8 Z0 e& j4 Y
$ i: C! K% d. Q' ?( m$ ]( ?' ^8 m" pyum install pcre pcre-devel
- R9 \+ M' z6 G, ~+ n1 I3.没有libxml2
! [( V! ?0 ^# ^' N
6 `5 R- X4 Q3 G* O/ q& w' u- r. x7 r. t- L
configure: *** xml library not found.
0 _& c/ `% R% e5 G( Xconfigure: error: libxml2 is required
7 |2 ^$ {; r8 K; w5 t$ u+ X解决方法:
8 Z' Z. @/ x7 d* S. d' B; a4 Z; q6 X7 |7 T% e0 v# i2 u( U
yum install  libxml2 libxml2-devel
/ S5 y) W8 l' m, |( h4.执行 /opt/tengine/sbin/nginx -m 时有警告
$ d# k2 o' k, n9 d. S* A+ _6 e; W+ C  O: y4 y
Tengine version: Tengine/2.1.0 (nginx/1.6.2)8 g7 O+ ~# z" L  ^
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!  T, X1 Z* K/ r6 n
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
- `+ ]" _5 d. j$ g2 p
- L2 W# e0 Z! g: J6 K/ q1 Q5 f" S( X- m+ E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.  v/ W! J8 V$ N9 I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"7 i4 t( }8 u5 W% R# G' D, ]
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
, F! p  R+ i/ i% P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"8 j+ I1 ?! I' H7 x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
1 y6 k  u" g1 D+ v" d2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
" w1 Y- c5 n) T! L1 ?  y( W* u- `解决方法,移除低版本的APR (1.3.9)# y; D9 F9 `  q
+ x' a+ |; i8 c& }7 T* M  y1 E
yum remove apr2 q9 x6 ]  S7 i9 |
5.Error.log中有: Audit log: Failed to lock global mutex% J9 [2 M/ Q8 ?8 c
$ M8 Y% m- h8 W+ U9 m2 l
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
2 K) i& H8 j) t; L4 h1 D; a$ aglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
  i9 E$ U  d4 f& a6 d4 h1 s6 p, X解决方法:" g. B7 z4 f9 [4 i
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
4 Q2 D" i: r! l  [7 L
  ^2 h) u0 G( F0 e$ q8 |SecAuditLogDirMode 07776 k5 |" p% G$ |) [
SecAuditLogFileMode 0550* O7 ?% z% w! u9 [0 s
SecAuditLogStorageDir /var/log/modsecurity
0 \, i; P/ ?3 h1 W: U. B2 ySecAuditLogType Concurrent; x0 R- l" }4 m+ p# o( j
参考文章:
( B/ |, I9 L1 ^$ bhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
. l1 D& _3 ^: W9 C3 Z0 o( Rhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-1-25 23:44 , Processed in 0.068425 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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