From baa288b6d0d17666b10ac404b187b923d0431e71 Mon Sep 17 00:00:00 2001 From: passbe Date: Sat, 16 Jul 2011 00:21:51 +0000 Subject: [PATCH] First chop at target parsing code, needs rework git-svn-id: https://beef.googlecode.com/svn/trunk@1065 b87d56ec-f9c0-11de-8c8a-61c5e9addfc9 --- core/main/configuration.rb | 6 +-- core/module.rb | 83 ++++++++++++++++++++++++++++++++++++++ core/ruby/hash.rb | 15 +++++-- 3 files changed, 98 insertions(+), 6 deletions(-) diff --git a/core/main/configuration.rb b/core/main/configuration.rb index fb8e86b83..4480a5801 100644 --- a/core/main/configuration.rb +++ b/core/main/configuration.rb @@ -74,7 +74,7 @@ module Core subkeys.each{|v| hash = {v.to_s => hash} } - @config = @config.recursive_merge(hash) + @config = @config.deep_merge(hash) return true end @@ -85,7 +85,7 @@ module Core Dir.glob("#{$root_dir}/extensions/*/config.yaml") do | cf | y = self.load(cf) if y != nil - @config = y.recursive_merge(@config) + @config = y.deep_merge(@config) end end end @@ -98,7 +98,7 @@ module Core y = self.load(cf) if y != nil y['beef']['module'][y['beef']['module'].keys.first]['path'] = cf.gsub(/config\.yaml/, '') - @config = y.recursive_merge(@config) + @config = y.deep_merge(@config) end end end diff --git a/core/module.rb b/core/module.rb index bfec99b74..ae88459f5 100644 --- a/core/module.rb +++ b/core/module.rb @@ -67,6 +67,89 @@ module Module return false end end + + # Translates module target configuration + def self.parse_targets(mod) + target_config = BeEF::Core::Configuration.instance.get('beef.module.'+mod+'.target') + if target_config + targets = {} + target_config.each{|k,v| + if BeEF::Core::Constants::CommandModule.const_defined?('VERIFIED_'+k.upcase) + key = BeEF::Core::Constants::CommandModule.const_get('VERIFIED_'+k.upcase) + if not targets.key?(key) + targets[key] = [] + end + targets[key] << self.parse_target_browsers(v) + else + print_debug "Module \"#{mod}\" configuration has invalid target status defined \"#{k}\"" + end + + } + end + puts targets + end + + # Translates browser target configuration + # TODO: problems, once yaml merges duplicate keys, the item can either be an array or hash. What happens if there is a hash inside of the array + def self.parse_target_browsers(v) + browser = nil + case v + when String + if BeEF::Core::Constants::Browsers.const_defined?(v.upcase) + browser = BeEF::Core::Constants::Browsers.const_get(v.upcase) + end + when Array + v.each{|c| + if BeEF::Core::Constants::Browsers.const_defined?(c.upcase) + if browser == nil + browser = [] + end + browser << self.parse_target_browsers(c) + end + } + when Hash + return + if BeEF::Core::Constants::Browsers.const_defined?(v.upcase) + details = {} + if v.key?('max_ver') and (v['max_ver'].is_a(Fixnum) or v['max_ver'].is_a(Float)) + details['max_ver'] = v['max_ver'] + end + if v.key?('min_ver') and (v['min_ver'].is_a(Fixnum) or v['min_ver'].is_a(Float)) + details['min_ver'] = v['min_ver'] + end + if v.key?('os') + if v['os'].is_a(String) + if BeEF::Core::Constants::Os.const_defined?('OS_'+v['os'].upcase+'_UA_STR') + details['os'] = [BeEF::Core::Constants::Os.const_get('OS_'+v['os'].upcase+'_UA_STR')] + else + print_debug "Could not identify OS target specified in module \"#{mod}\" configuration" + end + else v['os'].is_a(Array) + v['os'].each{|o| + if BeEF::Core::Constants::Os.const_defined?('OS_'+o.upcase+'_UA_STR') + details['os'] = [BeEF::Core::Constants::Os.const_get('OS_'+o.upcase+'_UA_STR')] + else + print_debug "Could not identify OS target specified in module \"#{mod}\" configuration" + end + } + end + end + targets[key] << BeEF::Core::Constants::Browers.const_get(v.upcase) + targets[key][BeEF::Core::Constants::Browers.const_get(v.upcase)] = details + else + print_debug "Could not identify browser target specified in module \"#{mod}\" configuration" + end + else + print_debug "Module \"#{mod}\" configuration has invalid target definition" + end + + if not browser + print_debug "Could not identify browser target specified in module \"#{mod}\" configuration" + return + end + browser + end + end end diff --git a/core/ruby/hash.rb b/core/ruby/hash.rb index df4c04c1e..97f7725fd 100644 --- a/core/ruby/hash.rb +++ b/core/ruby/hash.rb @@ -13,9 +13,18 @@ # See the License for the specific language governing permissions and # limitations under the License. # -# http://snippets.dzone.com/posts/show/3401 class Hash - def recursive_merge(h) - self.merge!(h) {|key, _old, _new| if _old.class == Hash then _old.recursive_merge(_new) else _new end } + + # http://snippets.dzone.com/posts/show/4706 + def deep_merge(hash) + target = dup + hash.keys.each do |key| + if hash[key].is_a? Hash and self[key].is_a? Hash + target[key] = target[key].deep_merge(hash[key]) + next + end + target[key] = hash[key] + end + target end end