Skip to content

Commit 99e2b05

Browse files
author
HD Moore
committed
Move the cache update logic into a utility class
1 parent 60145ad commit 99e2b05

File tree

2 files changed

+66
-51
lines changed

2 files changed

+66
-51
lines changed

lib/msf/util/payload_cached_size.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# -*- coding: binary -*-
2+
###
3+
#
4+
#
5+
###
6+
7+
module Msf
8+
module Util
9+
10+
#
11+
# The class provides helper methods for verifying and updating the embedded CachedSize
12+
# constant within payload modules.
13+
#
14+
15+
class PayloadCachedSize
16+
17+
def self.update_cache_constant(data, cached_size)
18+
data.
19+
gsub(/^\s*CachedSize\s*=\s*(\d+|:dynamic).*/, '').
20+
gsub(/^(module Metasploit\d+)\s*\n/) do |m|
21+
"#{m.strip}\n\n CachedSize = #{cached_size}\n\n"
22+
end
23+
end
24+
25+
def self.update_cached_size(mod, cached_size)
26+
mod_data = ""
27+
28+
::File.open(mod.file_path, 'rb') do |fd|
29+
mod_data = fd.read(fd.stat.size)
30+
end
31+
32+
::File.open(mod.file_path, 'wb') do |fd|
33+
fd.write update_cache_constant(mod_data, cached_size)
34+
end
35+
end
36+
37+
def self.update_module_cached_size(mod)
38+
update_cached_size(mod, compute_cached_size(mod))
39+
end
40+
41+
def self.compute_cached_size(mod)
42+
return :dynamic if is_dynamic?(mod)
43+
return mod.new.size
44+
end
45+
46+
def self.is_dynamic?(mod,generation_count=5)
47+
[*(1..generation_count)].map{|x| mod.new.size}.uniq.length != 1
48+
end
49+
50+
def self.is_cached_size_accurate?(mod)
51+
return true if mod.dynamic_size?
52+
return false if mod.cached_size.nil?
53+
mod.cached_size == mod.new.size
54+
end
55+
56+
end
57+
58+
end
59+
end

tools/update_payload_cached_sizes.rb

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#!/usr/bin/env ruby
22
#
3-
# $Id$
4-
#
5-
# This script lists each exploit module by its compatible payloads
6-
#
7-
# $Revision$
3+
# This script updates the CachedSize constants in payload modules
84
#
95

106
msfbase = __FILE__
@@ -20,54 +16,14 @@
2016
require 'rex'
2117
require 'msf/ui'
2218
require 'msf/base'
23-
24-
25-
def print_status(msg)
26-
print_line "[*] #{msg}"
27-
end
28-
29-
def print_error(msg)
30-
print_line "[-] #{msg}"
31-
end
32-
33-
def print_line(msg)
34-
$stderr.puts msg
35-
end
36-
37-
def is_dynamic_size?(mod)
38-
[*(1..5)].map{|x| mod.new.size}.uniq.length != 1
39-
end
40-
41-
def update_cache_size(mod, val)
42-
data = ''
43-
File.open(mod.file_path, 'rb'){|fd| data = fd.read(fd.stat.size)}
44-
data = data.gsub(/^\s*CachedSize\s*=\s*(\d+|:dynamic).*/, '')
45-
data = data.gsub(/^(module Metasploit\d+)\s*\n/) {|m| "#{m.strip}\n\n CachedSize = #{val}\n\n" }
46-
File.open(mod.file_path, 'wb'){|fd| fd.write(data) }
47-
end
19+
require 'msf/util/payload_cached_size'
4820

4921
# Initialize the simplified framework instance.
50-
$framework = Msf::Simple::Framework.create('DisableDatabase' => true)
51-
52-
$framework.payloads.each_module do |name, mod|
53-
gsize = mod.new.size
54-
55-
if is_dynamic_size?(mod) && ! mod.dynamic_size?
56-
print_status("#{mod.file_path} has a dynamic size, updating cache...")
57-
update_cache_size(mod, ":dynamic")
58-
next
59-
end
60-
61-
next if mod.dynamic_size?
22+
framework = Msf::Simple::Framework.create('DisableDatabase' => true)
6223

63-
if mod.cached_size.nil?
64-
print_status("#{mod.file_path} has size #{gsize}, updating cache...")
65-
update_cache_size(mod, gsize)
66-
else
67-
next if gsize == mod.cached_size
68-
print_error("#{mod.file_path} has cached size #{mod.cached_size} but generated #{gsize}, updating cache...")
69-
update_cache_size(mod, gsize)
70-
next
71-
end
24+
framework.payloads.each_module do |name, mod|
25+
next if Msf::Util::PayloadCachedSize.is_cached_size_accurate?(mod)
26+
$stdout.puts "[*] Updating the CacheSize for #{mod.file_path}..."
27+
Msf::Util::PayloadCachedSize.update_module_cached_size(mod)
7228
end
7329

0 commit comments

Comments
 (0)