|
| 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 | + # Insert a new CachedSize value into the text of a payload module |
| 18 | + # |
| 19 | + # @param data [String] The source code of a payload module |
| 20 | + # @param cached_size [String] The new value for cached_size, which |
| 21 | + # which should be either numeric or the string :dynamic |
| 22 | + # @return [String] |
| 23 | + def self.update_cache_constant(data, cached_size) |
| 24 | + data. |
| 25 | + gsub(/^\s*CachedSize\s*=\s*(\d+|:dynamic).*/, ''). |
| 26 | + gsub(/^(module Metasploit\d+)\s*\n/) do |m| |
| 27 | + "#{m.strip}\n\n CachedSize = #{cached_size}\n\n" |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + # Insert a new CachedSize value into a payload module file |
| 32 | + # |
| 33 | + # @param mod [Msf::Payload] The class of the payload module to update |
| 34 | + # @param cached_size [String] The new value for cached_size, which |
| 35 | + # which should be either numeric or the string :dynamic |
| 36 | + # @return [void] |
| 37 | + def self.update_cached_size(mod, cached_size) |
| 38 | + mod_data = "" |
| 39 | + |
| 40 | + ::File.open(mod.file_path, 'rb') do |fd| |
| 41 | + mod_data = fd.read(fd.stat.size) |
| 42 | + end |
| 43 | + |
| 44 | + ::File.open(mod.file_path, 'wb') do |fd| |
| 45 | + fd.write update_cache_constant(mod_data, cached_size) |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + # Updates the payload module specified with the current CachedSize |
| 50 | + # |
| 51 | + # @param mod [Msf::Payload] The class of the payload module to update |
| 52 | + # @return [void] |
| 53 | + def self.update_module_cached_size(mod) |
| 54 | + update_cached_size(mod, compute_cached_size(mod)) |
| 55 | + end |
| 56 | + |
| 57 | + # Calculates the CachedSize value for a payload module |
| 58 | + # |
| 59 | + # @param mod [Msf::Payload] The class of the payload module to update |
| 60 | + # @return [Fixnum] |
| 61 | + def self.compute_cached_size(mod) |
| 62 | + return ":dynamic" if is_dynamic?(mod) |
| 63 | + return mod.new.size |
| 64 | + end |
| 65 | + |
| 66 | + # Determines whether a payload generates a static sized output |
| 67 | + # |
| 68 | + # @param mod [Msf::Payload] The class of the payload module to update |
| 69 | + # @param generation_count [Fixnum] The number of iterations to use to |
| 70 | + # verify that the size is static. |
| 71 | + # @return [Fixnum] |
| 72 | + def self.is_dynamic?(mod,generation_count=5) |
| 73 | + [*(1..generation_count)].map{|x| mod.new.size}.uniq.length != 1 |
| 74 | + end |
| 75 | + |
| 76 | + # Determines whether a payload's CachedSize is up to date |
| 77 | + # |
| 78 | + # @param mod [Msf::Payload] The class of the payload module to update |
| 79 | + # @return [Boolean] |
| 80 | + def self.is_cached_size_accurate?(mod) |
| 81 | + return true if mod.dynamic_size? |
| 82 | + return false if mod.cached_size.nil? |
| 83 | + mod.cached_size == mod.new.size |
| 84 | + end |
| 85 | + |
| 86 | +end |
| 87 | + |
| 88 | +end |
| 89 | +end |
0 commit comments