例如:下面的例子显示了在主apache配置范围的哪个地方启用ModSecurity,不过,你可以为你的虚拟主机做不同的配置。第一个例子中,头一个虚拟主机没有继承ModSecurity的主配置指令,而第二做到了。
SecRuleEnine On
SecDefaultAction log,pass,phase:2 ...
SecDefaultAction log,deny,phase:1,redirect:http://www.site2.com ...
SecRuleInheritance On SecRule ARGS \...
可选的值如下:
On - 从父环境下继承规则 Off - 不从父环境下继承规则 注意:
Configuration Sections是apache的一个概念,指令
SecRuleEngine
描述:配置规则引擎
语法:SecRuleEngine On|Off|DetectionOnly 示例:SecRuleEngine On 阶段:Any 范围:Any 版本:2.0.0
备注:这个指令针对每一条规则处理也可以通过ctl动作进行控制(ctl:ruleEngine=off)
可选的值: On - 处理规则 Off - 不处理规则
DetectionOnly - 处理规则但不中断事务,即使规则配置了也不做。
十三、配置指令(十)
SecRuleRemoveById
描述:使用ID方式从上级环境中删除规则
语法:SecRuleUpdateActionById RULEID ACTIONLIST 示例:SecRuleRemoveByID 1 2 \阶段:Any 范围:Any 版本:2.0.0
备注:这个指令支持多个参数,每个参数可以是一个规则ID,也可以是范围。带有空格的参数必须使用双引号括起来。
SecRuleRemoveById 1 2 5 10-20 \
SecRuleRemoveByMsg
描述:使用规则方式从上级环境中删除规则 语法:SecRuleRemoveByMsg REGEX 示例:SecRuleRemoveByMsg \阶段:Any 范围:Any 版本:2.0.0
备注:这个指令支持多个参数,每个指令是一个应用于消息的正则表达式(指定使用的消息动作)。
SecRuleScript (试验性的)
描述:这个指令创建一个特殊的规则,执行Lua脚本来决定是否匹配,和SecRule主要的不同是这个没有目的也没有操作符,这个脚本可以从ModSecurity环境中取到所有的变量,并使用(Lua)操作符来进行测试,第二个参数可选,与SecRule相同,是一些动作列表。 语法:SecRuleScript /path/to/script.lua [ACTIONS] 示例:SecRuleScript \阶段:Any 范围:Any 版本:2.5.0 备注:None 注意
所有的Lua脚本在配置时进行编译并存入内存,要重载脚本,你必须通过apache重启来重载整个ModSecurity配置。 示例脚本:
-- Your script must define the main entry -- point, as below. function main()
-- Log something at level 1. Normally you shouldn't be -- logging anything, especially not at level 1, but this is -- just to show you can. Useful for debugging. m.log(1, \
-- Retrieve one variable.
local var1 = m.getvar(\
-- Retrieve one variable, applying one transformation function. -- The second parameter is a string.
local var2 = m.getvar(\
-- Retrieve one variable, applying several transformation functions. -- The second parameter is now a list. You should note that m.getvar() -- requires the use of comma to separate collection names from -- variable names. This is because only one variable is returned.
local var3 = m.getvar(\
-- If you want this rule to match return a string
-- containing the error message. The message must contain the name -- of the variable where the problem is located. -- return \
-- Otherwise, simply return nil. return nil; end
第一个例子,我仅每次提取一个变量,这种情况下,你需要事先知道变量,然而很多时候,你想检查一些事先不知道的变量名字,就象下面这个例子。
Example showing use of m.getvars() to retrieve many variables at once:
function main()
-- Retrieve script parameters.
local d = m.getvars(\
-- Loop through the paramters. for i = 1, #d do
-- Examine parameter value.
if (string.find(d[i].value, \
-- Always specify the name of the variable where the -- problem is located in the error message.
return (\
end end
-- Nothing wrong found. return nil; end 注意
到http://www.lua.org/了解更多有关Lua程序语言,参考手册在http://www.lua.org/manual/5.1/能在线阅读 注意
Lua支持被标示试验方式的编程接口可以继续发展,同时我们努力让其最佳方式运行,感谢任何人为些提供编程接口。
SecRuleUpdateActionById
描述:按ID方式更新指定规则的动作列表
语法:SecRuleRemoveById RULEID ACTIONLIST
示例:SecRuleUpdateActionById 12345 deny,status:403 阶段:Any 范围:Any 版本:2.5.0
备注:本指令合并指定的动作列表和规则的动作列表,存在两个限制,一是规则ID不能被改变,也不能换阶段。另外还需要注意的是动作可以多次被附加到原来的。
SecAction \\
\
SecRuleUpdateActionById 12345 \message'
The example above will cause the rule to be executed as if it was specified as follows:
SecAction \\
\A new message'\
SecServerSignature
描述:通知ModSecurity改变响应头令牌数据中当前使用的\语法:SecServerSignature \示例:SecServerSignature \阶段:N/A 范围:Main
版本:2.0.0
备注:按指令的先后顺序执行,你必须完整的配置apache ServerTokens指令。通过该指令,ModSecurity会改写服务器签名数据并存入内存空间的数据集中。如果ServerTokens没有完整设置,那么内存空间就没有希望的足够大来存入我们看到的新数据。
SecTmpDir
描述:配置临时文件创建的路径 语法:SecTmpDir /path/to/dir 示例:SecTmpDir /tmp 阶段:N/A 范围:Any 版本:2.0.0
备注:需要提供apache用户进程的可写权限,这个目录的位置也是当apache检查数据用完内存时(比SecRequestBodyInMemoryLimit指令指定的还多的数据)会将数据交换到磁盘上的位置。
SecUploadDir
描述:配置拦截文件存储的目录 语法:SecUploadDir /path/to/dir 示例:SecUploadDir /tmp 阶段:N/A 范围:Any 版本:2.0.0
备注:This directory must be on the same filesystem as the temporary directory defined with SecTmpDir. This directive is used with SecUploadKeepFiles.
SecUploadFileMode
描述:用8进制数(和chmod一样)配置所有上传文件的模式(权限) 语法:SecUploadFileMode octal_mode|\示例:SecUploadFileMode 0640 阶段:N/A 范围:Any 版本:2.1.6
备注:这个功能在不支持8进制文件模式的操作系统上不可用。默认模式(0600)仅允许有读写许可的帐号修改这个文件,如果其他帐号也需要访问(使用clamd是个好例子),那么这个指令是必须的。不管怎样,要慎用这个指令,避免将敏感数据显露给未授权用户,使用\值时,还原默认设置。
SecUploadKeepFiles