Browse Source

Enforce integrity of datastore options on assignment

Brent Cook 3 years ago
parent
commit
654590911b

+ 15
- 5
lib/msf/core/data_store.rb View File

@@ -13,6 +13,7 @@ class DataStore < Hash
13 13
   # Initializes the data store's internal state.
14 14
   #
15 15
   def initialize()
16
+    @options     = Hash.new
16 17
     @imported    = Hash.new
17 18
     @imported_by = Hash.new
18 19
   end
@@ -26,6 +27,14 @@ class DataStore < Hash
26 27
     @imported[k] = false
27 28
     @imported_by[k] = nil
28 29
 
30
+    opt = @options[k]
31
+    unless opt.nil?
32
+      unless opt.valid?(v)
33
+        raise OptionValidateError.new(["Value '#{v}' is not valid for option '#{k}'#{['', ', try harder'].sample}"])
34
+      end
35
+      v = opt.normalize(v)
36
+    end
37
+
29 38
     super(k,v)
30 39
   end
31 40
 
@@ -65,12 +74,12 @@ class DataStore < Hash
65 74
   # all of the supplied options
66 75
   #
67 76
   def import_options(options, imported_by = nil, overwrite = false)
68
-    options.each_option { |name, opt|
77
+    options.each_option do |name, opt|
69 78
       # Skip options without a default or if is already a value defined
70 79
       if !opt.default.nil? && (!self.has_key?(name) || overwrite)
71
-        import_option(name, opt.default, true, imported_by)
80
+        import_option(name, opt.default, true, imported_by, opt)
72 81
       end
73
-    }
82
+    end
74 83
   end
75 84
 
76 85
   #
@@ -119,13 +128,14 @@ class DataStore < Hash
119 128
   #
120 129
   def import_options_from_hash(option_hash, imported = true, imported_by = nil)
121 130
     option_hash.each_pair { |key, val|
122
-      import_option(key, val.to_s, imported, imported_by)
131
+      import_option(key, val, imported, imported_by)
123 132
     }
124 133
   end
125 134
 
126
-  def import_option(key, val, imported=true, imported_by=nil)
135
+  def import_option(key, val, imported=true, imported_by=nil, option=nil)
127 136
     self.store(key, val)
128 137
 
138
+    @options[key] = option
129 139
     @imported[key]    = imported
130 140
     @imported_by[key] = imported_by
131 141
   end

+ 2
- 3
lib/msf/core/module.rb View File

@@ -266,11 +266,10 @@ class Module
266 266
   end
267 267
 
268 268
   #
269
-  # Returns true if this module is being debugged.  The debug flag is set
270
-  # by setting datastore['DEBUG'] to 1|true|yes
269
+  # Returns true if this module is being debugged.
271 270
   #
272 271
   def debugging?
273
-    (datastore['DEBUG'] || '') =~ /^(1|t|y)/i
272
+    datastore['DEBUG']
274 273
   end
275 274
 
276 275
   #

+ 2
- 3
lib/msf/core/module/ui/message.rb View File

@@ -14,9 +14,8 @@ module Msf::Module::UI::Message
14 14
 
15 15
   def print_prefix
16 16
     prefix = ''
17
-    if (datastore['TimestampOutput'] =~ /^(t|y|1)/i) || (
18
-      framework && framework.datastore['TimestampOutput'] =~ /^(t|y|1)/i
19
-    )
17
+    if datastore['TimestampOutput'] ||
18
+        (framework && framework.datastore['TimestampOutput'])
20 19
       prefix << "[#{Time.now.strftime("%Y.%m.%d-%H:%M:%S")}] "
21 20
 
22 21
       xn ||= datastore['ExploitNumber']

+ 1
- 2
lib/msf/core/module_manager.rb View File

@@ -147,8 +147,7 @@ module Msf
147 147
     # @return [void]
148 148
     def auto_subscribe_module(klass)
149 149
       # If auto-subscribe has been disabled
150
-      if (framework.datastore['DisableAutoSubscribe'] and
151
-          framework.datastore['DisableAutoSubscribe'] =~ /^(y|1|t)/)
150
+      if framework.datastore['DisableAutoSubscribe']
152 151
         return
153 152
       end
154 153
 

+ 9
- 4
lib/msf/ui/console/command_dispatcher/core.rb View File

@@ -2171,10 +2171,15 @@ class Core
2171 2171
       return true
2172 2172
     end
2173 2173
 
2174
-    if append
2175
-      datastore[name] = datastore[name] + value
2176
-    else
2177
-      datastore[name] = value
2174
+    begin
2175
+      if append
2176
+        datastore[name] = datastore[name] + value
2177
+      else
2178
+        datastore[name] = value
2179
+      end
2180
+    rescue OptionValidateError => e
2181
+      print_error(e.message)
2182
+      elog(e.message)
2178 2183
     end
2179 2184
 
2180 2185
     print_line("#{name} => #{datastore[name]}")

Loading…
Cancel
Save