没有合适的资源?快使用搜索试试~ 我知道了~
10 WAYS TO IMPROVE YOUR PROTECTION WITH MODSECURITY.pdf
需积分: 5 0 下载量 184 浏览量
2019-07-30
12:38:10
上传
评论
收藏 586KB PDF 举报
温馨提示
10 WAYS TO IMPROVE YOUR PROTECTION WITH MODSECURITY.pdf
资源推荐
资源详情
资源评论
10 WAYS TO IMPROVE YOUR PROTECTION WITH
MOD SECURITY
JOSH ZLATIN
DIRECTOR, RESEARCH & DEVELOPMENT
PURE HACKING PTY LTD
TABLE OF CONTENTS
1. Introduction ........................................................................................................................................... 2
2. Speeding Up Lua Script Execution in ModSecurity ................................................................................ 2
3. Virtual Patching Session Fixation ........................................................................................................... 3
4. Increasing ModSecurity Collection Size Limits ...................................................................................... 5
5. PHP/Java Floating Point DoS Protection................................................................................................ 6
6. ModSecurity Resource Profiling ............................................................................................................ 8
7. Virus Detection in ModSecurity ........................................................................................................... 10
8. Whitelisting in ModSecurity ................................................................................................................ 12
9. Mitigating Password Recovery Weaknesses with ModSecurity and Lua ............................................ 14
10. Enforcing Access Controls with ModSecurity ...................................................................................... 18
11. Protecting against CSRF attacks using Modperl and ModSecurity ...................................................... 20
10 Ways to Improve Your Protection with ModSecurity
2
1. INTRODUCTION
Web application defenders are often the first, last and only line of defence protecting your web based assets. When
your latest new Web 2.0 HTML5 and AJAX based application makes it through production, past QA and a cursory pen
test, the web defender may be the only thing standing in between malicious attackers and your company's crown
jewels.
Once web applications hit the production stage, making updates or applying security fixes are often difficult due to
infrequent change windows. One of the best tools a web defender can use to gain visibility into how attackers are
abusing the application as well as virtually patch security issues on the fly, is ModSecurity. We gathered the top 10
most popular ModSecurity recipes that defenders can use to dramatically improve their security posture with minimal
effort.
2. SPEEDING UP LUA SCRIPT EXECUTION IN MODSECURITY
Often when implementing customised ModSecurity solutions we need to extend the built-in functionality via Lua
scripting. One of the disadvantages to this approach is the added latency penalty paid for not using the native rules
language.
When web site performance is critical for business continuity, every additional millesecond counts. The current
trunk code fixes a long-standing limitation where ModSecurity needed to create a new VM for each request, which
added latency every time a Lua script was executed. By combining the new Lua VM implementation along with
replacing the Lua interperter with LuaJIT we can enjoy a significant speed increase in script execution.
To measure the speed difference we will test with a simple script used to generate a random token as part of a
predictable session token patch. The code is below:
#!/usr/bin/lua
function main()
-- Create random cookie value
local ip = m.getvar("REMOTE_ADDR")
local md5 = require "md5"
math.randomseed( os.time() )
randomtoken = md5.sumhexa(ip .. math.random())
m.log(3, "RandomToken: " .. randomtoken);
m.setvar("TX.token", randomtoken);
return 1
end
If we run this script 1000 times via the ModSecurity 2.6.2 engine, the script takes an average of 616 microseconds to
run, as shown below:
10 Ways to Improve Your Protection with ModSecurity
3
lab@lab:~$ for i in `seq 1 1000`; do wget -qO /dev/null http://localhost/;
done
lab@lab:~$ let sum=0;for duration in
$(grep "Script completed in " /opt/modsecurity/var/log/debug.log|
cut -d " " -f 8); { let sum=$sum+$duration; };
let average=$sum/1000;echo $average
616
As noted above we can significantly increase the speed execution by using the latest engine from trunk along with
using LuaJIT instead of the regular Lua shared library. The good news is setting this up is simple. As noted here as of
Lua 5.1 "LuaJIT is also fully ABI-compatible to Lua 5.1 at the linker/dynamic loader level. This means you can compile
a C module against the standard Lua headers and load the same shared library from either Lua or LuaJIT." In other
words, all we need to do to use LuaJIT with ModSecurity is to load the LuaJIT shared library in the Apache
ModSecurity module configuration. On a Debian Sid based system this looks like:
lab@lab:~$ cat /etc/apache2/mods-enabled/modsecurity.load
# Load libxml2
LoadFile /usr/lib/libxml2.so
# Load Lua
LoadFile /usr/lib/x86_64-linux-gnu/libluajit-5.1.so.2.0.0
# Finally, load ModSecurity
LoadModule security2_module /opt/modsecurity/bin/mod_security2.so
With these changes we will run the same script again and compare the results.
lab@lab:~$ echo " " >/opt/modsecurity/var/log/debug.log
lab@lab:~$ for i in `seq 1 1000`; do wget -qO /dev/null http://localhost/;
done
lab@lab:~$ let sum=0;for duration in
$(grep "Script completed in " /opt/modsecurity/var/log/debug.log|
cut -d " " -f 8); { let sum=$sum+$duration; };
let average=$sum/1000;echo $average
112
As you can see the script execution went from 616 microseconds down to 112 microseconds. While the example
script we used here was simplistic and added little overhead to begin with, with the changes described above more
complex Lua scripts can be run with greater efficiency.
3. VIRTUAL PATCHING SESSION FIXATION
On a recent engagement we gained unrestricted administrative access to a certain proprietary web application by
exploiting a Session Fixation flaw. According to the WASC Threat Classification v2, Session Fixation is an attack
technique that forces a user's session ID to an explicit value. In other words, by feeding the victim a session token
under the attacker's control, the attacker can bypass the authentication phase and gain unauthorized account access
as the victim. There are a number of techniques that can be used to feed the token to the victim. The most common
is by exploiting a XSS or header injection vulnerability.
10 Ways to Improve Your Protection with ModSecurity
4
Due to the fact that this was a proprietary application, the client was unable to easily resolve the issue. Today, we
will discuss two methods to virtually patch this flaw using ModSecurity. The first approach removes the session
token submitted within the authentication request. On some frameworks, the application will automatically assign a
new session token if none is submitted when succesfully logging in to the application. This approach uses inter-
module communication between ModSecurity and ModHeaders, as shown below.
# Set the enviroment variable REMOVE_COOKIE if the request is a
# login request (e.g. contains the passwd POST parameter) to force
# the back-end app to issue a new session token
SecRule REQUEST_URI "/login.php" "phase:2,chain, \
log,msg:'Removing Cookie'"
SecRule REQUEST_METHOD "^post$" t:none,t:lowercase,chain
SecRule &ARGS:passwd "@eq 1" setenv:REMOVE_COOKIE
# Have ModHeaders remove the Cookie header if the env variable
# REMOVE_COOKIE is set
RequestHeader unset Cookie env=REMOVE_COOKIE
While the above approach is pretty straightforward, it does not work in all situations. The next approach can be
used if the application does not set a new session token when an authentication request is submitted without a
session token. This method uses the session collection to tie a new token to the existing application-generated
session token. There are two parts to this solution, the ModSecurity rules and the Lua script. First, let's look at the
ModSec rules.
# Create the session collection
SecRule REQUEST_COOKIES:/^ASP.NET/ ^(.+)$ \
"phase:2,capture,log,pass,\
setsid:%{TX.0},\
setvar:SESSION.TIMEOUT=172800,msg:'captured sessid %{TX.0}'"
# If this is a login request, create an additional session token
# tie it to the SESSION and add it to the TOKEN enviroment variable
SecRule &ARGS:passwd "@eq 1" "phase:2,chain,log,allow, \
exec:/opt/modsecurity/etc/customrules/CreateSessionToken.lua, \
setenv:TOKEN=%{TX.token},setenv:AUTHENTICATED \
setvar:SESSION.token=%{TX.token}, \
msg:'Setting token to %{TX.token}'"
SecRule REQUEST_METHOD POST
# Have ModHeaders set our new session token if the AUTHENTICATED enviroment
# variable exists (set in the previous rule)
Header add Set-Cookie "SessionID=%{TOKEN}e; path=/; HttpOnly; Secure" \
env=AUTHENTICATED
剩余23页未读,继续阅读
资源评论
rhef
- 粉丝: 1
- 资源: 63
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- YOLO-yolo资源
- 适用于 Java 项目的 Squash 客户端库 .zip
- 适用于 Java 的 Chef 食谱.zip
- Simulink仿真快速入门与实践基础教程
- js-leetcode题解之179-largest-number.js
- js-leetcode题解之174-dungeon-game.js
- Matlab工具箱使用与实践基础教程
- js-leetcode题解之173-binary-search-tree-iterator.js
- js-leetcode题解之172-factorial-trailing-zeroes.js
- js-leetcode题解之171-excel-sheet-column-number.js
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功