Class: Msf::Modules::External::Shim

Inherits:
Object
  • Object
show all
Defined in:
lib/msf/core/modules/external/shim.rb

Class Method Summary collapse

Class Method Details

.capture_server(mod) ⇒ Object


119
120
121
122
# File 'lib/msf/core/modules/external/shim.rb', line 119

def self.capture_server(mod)
  meta = mod_meta_common(mod)
  render_template('capture_server.erb', meta)
end

.common_check(meta = {}) ⇒ Object


39
40
41
# File 'lib/msf/core/modules/external/shim.rb', line 39

def self.common_check(meta = {})
  render_template('common_check.erb', meta)
end

.common_metadata(meta = {}) ⇒ Object


35
36
37
# File 'lib/msf/core/modules/external/shim.rb', line 35

def self.(meta = {})
  render_template('common_metadata.erb', meta)
end

.dos(mod) ⇒ Object


153
154
155
156
157
158
159
160
161
# File 'lib/msf/core/modules/external/shim.rb', line 153

def self.dos(mod)
  meta = mod_meta_common(mod)
  meta[:date] = mod.meta['date'].dump
  meta[:references] = mod.meta['references'].map do |r|
    "[#{r['type'].upcase.dump}, #{r['ref'].dump}]"
  end.join(",\n          ")

  render_template('dos.erb', meta)
end

.evasion(mod) ⇒ Object


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/msf/core/modules/external/shim.rb', line 163

def self.evasion(mod)
  meta = mod_meta_common(mod, ignore_options: ['payload_raw', 'payload_encoded', 'target'])
  meta[:platform]    = mod.meta['targets'].map do |t|
    t['platform'].dump
  end.uniq.join(",\n          ")
  meta[:arch]        = mod.meta['targets'].map do |t|
    t['arch'].dump
  end.uniq.join(",\n          ")
  meta[:references]  = mod.meta['references'].map do |r|
    "[#{r['type'].upcase.dump}, #{r['ref'].dump}]"
  end.join(",\n          ")
  meta[:targets]     = mod.meta['targets'].map do |t|
    if t['name']
      "[#{t['name'].dump}, {'Arch' => ARCH_#{t['arch'].upcase}, 'Platform' => #{t['platform'].dump} }]"
    else
      "[#{t['platform'].dump} + ' ' + #{t['arch'].dump}, {'Arch' => ARCH_#{t['arch'].upcase}, 'Platform' => #{t['platform'].dump} }]"
    end
  end.join(",\n          ")
  render_template('evasion.erb', meta)
end

.generate(module_path, framework) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/msf/core/modules/external/shim.rb', line 5

def self.generate(module_path, framework)
  mod = Msf::Modules::External.new(module_path, framework: framework)
  return nil unless mod.meta
  case mod.meta['type']
  when 'remote_exploit'
    remote_exploit(mod)
  when 'remote_exploit_cmd_stager'
    remote_exploit_cmd_stager(mod)
  when 'capture_server'
    capture_server(mod)
  when 'dos'
    dos(mod)
  when 'single_scanner'
    single_scanner(mod)
  when 'single_host_login_scanner'
    (mod)
  when 'multi_scanner'
    multi_scanner(mod)
  when 'evasion'
    evasion(mod)
  else
    nil
  end
end

.mod_meta_common(mod, meta = {}, ignore_options: []) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/msf/core/modules/external/shim.rb', line 43

def self.mod_meta_common(mod, meta = {}, ignore_options: [])
  meta[:path]             = mod.path.dump
  meta[:name]             = mod.meta['name'].dump
  meta[:description]      = mod.meta['description'].dump
  meta[:authors]          = mod.meta['authors'].map(&:dump).join(",\n          ")
  meta[:license]          = mod.meta['license'].nil? ? 'MSF_LICENSE' : mod.meta['license']
  meta[:options]          = mod_meta_common_options(mod, ignore_options: ignore_options)
  meta[:advanced_options] = mod_meta_common_options(mod, ignore_options: ignore_options, advanced: true)
  meta[:capabilities]     = mod.meta['capabilities']
  meta[:notes]            = transform_notes(mod.meta['notes'])

  if mod.meta['describe_payload_options'].nil?
    mod.meta['describe_payload_options'] = {}
  end
  meta[:default_options]  = mod.meta['describe_payload_options'].map do |name, value|
    "#{name.dump} => #{value.inspect}"
  end.join(",\n          ")

  meta
end

.mod_meta_common_options(mod, ignore_options: [], advanced: false) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/msf/core/modules/external/shim.rb', line 64

def self.mod_meta_common_options(mod, ignore_options: [], advanced: false)
  # Set modules without options to have an empty map
  if mod.meta['options'].nil?
    mod.meta['options'] = {}
  end

  options = mod.meta['options'].map do |n, o|
    next if ignore_options.include? n
    next unless o.fetch('advanced', false) == advanced

    if o['values']
      "Opt#{o['type'].camelize}.new(#{n.dump},
        [#{o['required']}, #{o['description'].dump}, #{o['default'].inspect}, #{o['values'].inspect}])"
    else
      "Opt#{o['type'].camelize}.new(#{n.dump},
        [#{o['required']}, #{o['description'].dump}, #{o['default'].inspect}])"
    end
  end
  options.reject! { |o| o.nil? }
  options.join(",\n          ")
end

.mod_meta_exploit(mod, meta = {}) ⇒ Object


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/msf/core/modules/external/shim.rb', line 86

def self.mod_meta_exploit(mod, meta = {})
  meta[:rank]        = mod.meta['rank'].nil? ? 'NormalRanking' : "#{mod.meta['rank'].capitalize}Ranking"
  meta[:date]        = mod.meta['date'].dump
  meta[:wfsdelay]    = mod.meta['wfsdelay'] || 5
  meta[:privileged]  = mod.meta['privileged'].inspect
  meta[:platform]    = mod.meta['targets'].map do |t|
    t['platform'].dump
  end.uniq.join(",\n          ")
  meta[:arch]        = mod.meta['targets'].map do |t|
    t['arch'].dump
  end.uniq.join(",\n          ")
  meta[:references]  = mod.meta['references'].map do |r|
    "[#{r['type'].upcase.dump}, #{r['ref'].dump}]"
  end.join(",\n          ")
  meta[:targets]     = mod.meta['targets'].map do |t|
    "[#{t['platform'].dump} + ' ' + #{t['arch'].dump}, {'Arch' => ARCH_#{t['arch'].upcase}, 'Platform' => #{t['platform'].dump} }]"
  end.join(",\n          ")
  meta
end

.multi_scanner(mod) ⇒ Object


143
144
145
146
147
148
149
150
151
# File 'lib/msf/core/modules/external/shim.rb', line 143

def self.multi_scanner(mod)
  meta = mod_meta_common(mod)
  meta[:date] = mod.meta['date'].dump
  meta[:references] = mod.meta['references'].map do |r|
    "[#{r['type'].upcase.dump}, #{r['ref'].dump}]"
  end.join(",\n          ")

  render_template('multi_scanner.erb', meta)
end

.remote_exploit(mod) ⇒ Object


106
107
108
109
110
# File 'lib/msf/core/modules/external/shim.rb', line 106

def self.remote_exploit(mod)
  meta = mod_meta_common(mod)
  meta = mod_meta_exploit(mod, meta)
  render_template('remote_exploit.erb', meta)
end

.remote_exploit_cmd_stager(mod) ⇒ Object


112
113
114
115
116
117
# File 'lib/msf/core/modules/external/shim.rb', line 112

def self.remote_exploit_cmd_stager(mod)
  meta = mod_meta_common(mod, ignore_options: ['command'])
  meta = mod_meta_exploit(mod, meta)
  meta[:command_stager_flavor] = mod.meta['payload']['command_stager_flavor'].dump
  render_template('remote_exploit_cmd_stager.erb', meta)
end

.render_template(name, meta = {}) ⇒ Object


30
31
32
33
# File 'lib/msf/core/modules/external/shim.rb', line 30

def self.render_template(name, meta = {})
  template = File.join(File.dirname(__FILE__), 'templates', name)
  ERB.new(File.read(template)).result(binding)
end

.single_host_login_scanner(mod) ⇒ Object


133
134
135
136
137
138
139
140
141
# File 'lib/msf/core/modules/external/shim.rb', line 133

def self.(mod)
  meta = mod_meta_common(mod, ignore_options: ['rhost'])
  meta[:date] = mod.meta['date'].dump
  meta[:references] = mod.meta['references'].map do |r|
    "[#{r['type'].upcase.dump}, #{r['ref'].dump}]"
  end.join(",\n          ")

  render_template('single_host_login_scanner.erb', meta)
end

.single_scanner(mod) ⇒ Object


124
125
126
127
128
129
130
131
# File 'lib/msf/core/modules/external/shim.rb', line 124

def self.single_scanner(mod)
  meta = mod_meta_common(mod, ignore_options: ['rhost'])
  meta[:date] = mod.meta['date'].dump
  meta[:references] = mod.meta['references'].map do |r|
    "[#{r['type'].upcase.dump}, #{r['ref'].dump}]"
  end.join(",\n          ")
  render_template('single_scanner.erb', meta)
end

.transform_notes(notes) ⇒ Object

In case certain notes are not properly capitalized in the external module definition, ensure that they are properly capitalized before rendering.


188
189
190
191
192
193
194
195
# File 'lib/msf/core/modules/external/shim.rb', line 188

def self.transform_notes(notes)
  return {} unless notes

  notes.reduce({}) do |acc, (key, val)|
    acc[key.upcase] = val
    acc
  end
end