描述:为目标列表中的每个变量执行外部程序。变量的内容作为命令行上的第一个参数提供给脚本。外部程序必须配置为该运算符的第一个参数。从版本2.5.0开始,如果提供的程序不是绝对路径,则将其视为相对于配置文件所在的目录。同样从版本2.5.0开始,如果确定文件名是Lua脚本(基于其.lua扩展名),则脚本将由内部Lua引擎处理。内部处理的脚本通常运行得更快(没有进程创建开销)并且可以完全访问ModSecurity的事务上下文。


@inspectFile运算符最初设计用于文件检查(这是该运算符名称的由来),但它也可以用于需要使用外部逻辑进行决策的任何情况。


OWASP ModSecurity核心规则集(CRS)在/util目录中包含一个名为runav.pl的实用程序脚本(http://mod-security.svn.sourceforge.net/viewvc/mod-security/crs/trunk/util/),通过集成ClamAV病毒扫描程序来进行文件检测。这对于防止病毒和漏洞通过文件上传进入Web服务器尤其方便。


#!/usr/bin/perl

#

# runav.pl

# Copyright (c) 2004-2011 Trustwave

#

# This script is an interface between ModSecurity and its

# ability to intercept files being uploaded through the

# web server, and ClamAV



$CLAMSCAN = "clamscan";


if ($#ARGV != 0) {

   print "Usage: runav.pl <filename>\n";

   exit;

}


my ($FILE) = shift @ARGV;


$cmd = "$CLAMSCAN --stdout --no-summary $FILE";

$input = `$cmd`;

$input =~ m/^(.+)/;

$error_message = $1;


$output = "0 Unable to parse clamscan output [$1]";


if ($error_message =~ m/: Empty file\.?$/) {

   $output = "1 empty file";

}

elsif ($error_message =~ m/: (.+) ERROR$/) {

   $output = "0 clamscan: $1";

}

elsif ($error_message =~ m/: (.+) FOUND$/) {

   $output = "0 clamscan: $1";

}

elsif ($error_message =~ m/: OK$/) {

   $output = "1 clamscan: OK";

}


print "$output\n";


Example: Using the runav.pl script:


# Execute external program to validate uploaded files

SecRule FILES_TMPNAMES "@inspectFile /path/to/util/runav.pl" "id:159"

Example of using Lua script (placed in the same directory as the configuration file):


SecRule FILES_TMPNAMES "@inspectFile inspect.lua" "id:160"

The contents of inspect.lua:


function main(filename)

   -- Do something to the file to verify it. In this example, we

   -- read up to 10 characters from the beginning of the file.

   local f = io.open(filename, "rb");

   local d = f:read(10);

   f:close();

 

   -- Return null if there is no reason to believe there is ansything

   -- wrong with the file (no match). Returning any text will be taken

   -- to mean a match should be trigerred.

   return null;

end


注意:从版本2.9开始,除非SecTmpSaveUploadedFiles指令为On,或者SecUploadKeepFiles指令设置为RelevantOnly,否则ModSecurity将不会填充FILES_TMPNAMES变量。

注意:请谨慎使用@inspectFile。将@inspectFile与FILES_TMPNAMES以外的变量一起使用可能不安全。其他变量(如“FULL_REQUEST”)可能包含强制您的平台将进程分离出来的内容,使攻击者可以使用Web服务器的相同权限执行代码。对于其他变量,您可能需要查看Lua脚本引擎。在我们的用户邮件列表中,“Gryzli”引起了我们的注意。


版本:2.x


libModSecurity支持:TBI


参考:http://blog.spiderlabs.com/2010/10/advanced-topic-of-the-week-preventing-malicious-pdf-file-uploads.html


参考:http://sourceforge.net/p/mod-security/mailman/mod-security-users/?viewmonth=201512



Created with the Personal Edition of HelpNDoc: Easy EPub and documentation editor