Skip to content

Commit 97f09b6

Browse files
author
Brent Cook
committed
Land rapid7#4894: hmoore-r7 cache payload sizes on start
Avoid the hit of regenerating all of the static-size payloads when loading the framework. This will facilitate conversion of payloads to use metasm later.
2 parents 6031791 + 618fbf0 commit 97f09b6

File tree

241 files changed

+1381
-366
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

241 files changed

+1381
-366
lines changed

lib/msf/core/payload.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,36 @@ def staged?
159159
(@staged or payload_type == Type::Stager or payload_type == Type::Stage)
160160
end
161161

162+
#
163+
# This method returns an optional cached size value
164+
#
165+
def self.cached_size
166+
csize = (const_defined?('CachedSize')) ? const_get('CachedSize') : nil
167+
csize == :dynamic ? nil : csize
168+
end
169+
170+
#
171+
# This method returns whether the payload generates variable-sized output
172+
#
173+
def self.dynamic_size?
174+
csize = (const_defined?('CachedSize')) ? const_get('CachedSize') : nil
175+
csize == :dynamic
176+
end
177+
178+
#
179+
# This method returns an optional cached size value
180+
#
181+
def cached_size
182+
self.class.cached_size
183+
end
184+
185+
#
186+
# This method returns whether the payload generates variable-sized output
187+
#
188+
def dynamic_size?
189+
self.class.dynamic_size?
190+
end
191+
162192
#
163193
# Returns the payload's size. If the payload is staged, the size of the
164194
# first stage is returned.

lib/msf/core/payload_set.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def recalculate
155155
new_keys.push combined
156156

157157
# Cache the payload's size
158-
sizes[combined] = p.new.size
158+
sizes[combined] = p.cached_size || p.new.size
159159
}
160160
}
161161

lib/msf/util/payload_cached_size.rb

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

modules/payloads/singles/aix/ppc/shell_bind_tcp.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
module Metasploit3
1212

13+
CachedSize = 264
14+
1315
include Msf::Payload::Single
1416
include Msf::Payload::Aix
1517
include Msf::Sessions::CommandShellOptions

modules/payloads/singles/aix/ppc/shell_find_port.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
module Metasploit3
1212

13+
CachedSize = 220
14+
1315
include Msf::Payload::Single
1416
include Msf::Payload::Aix
1517
include Msf::Sessions::CommandShellOptions

modules/payloads/singles/aix/ppc/shell_interact.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
module Metasploit3
1212

13+
CachedSize = 56
14+
1315
include Msf::Payload::Single
1416
include Msf::Payload::Aix
1517
include Msf::Sessions::CommandShellOptions

modules/payloads/singles/aix/ppc/shell_reverse_tcp.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
module Metasploit3
1212

13+
CachedSize = 204
14+
1315
include Msf::Payload::Single
1416
include Msf::Payload::Aix
1517
include Msf::Sessions::CommandShellOptions

modules/payloads/singles/bsd/sparc/shell_bind_tcp.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
module Metasploit3
1212

13+
CachedSize = 164
14+
1315
include Msf::Payload::Single
1416
include Msf::Payload::Bsd
1517
include Msf::Sessions::CommandShellOptions

modules/payloads/singles/bsd/sparc/shell_reverse_tcp.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
module Metasploit3
1212

13+
CachedSize = 128
14+
1315
include Msf::Payload::Single
1416
include Msf::Payload::Bsd
1517
include Msf::Sessions::CommandShellOptions

modules/payloads/singles/bsd/x86/exec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
###
1818
module Metasploit3
1919

20+
CachedSize = 107
21+
2022
include Msf::Payload::Single
2123
include Msf::Payload::Bsd
2224

0 commit comments

Comments
 (0)