Class: Rex::Post::HWBridge::Ui::Console::CommandDispatcher::Automotive

Inherits:
Object
  • Object
show all
Includes:
Msf::Auxiliary::Report, Rex::Post::HWBridge::Ui::Console::CommandDispatcher
Defined in:
lib/rex/post/hwbridge/ui/console/command_dispatcher/automotive.rb

Overview

Automotive extension - set of commands to be executed on CAN bus

Instance Attribute Summary collapse

Attributes included from Ui::Text::DispatcherShell::CommandDispatcher

#shell, #tab_complete_items

Instance Method Summary collapse

Methods included from Msf::Auxiliary::Report

#active_db?, #create_cracked_credential, #create_credential, #create_credential_and_login, #create_credential_login, #db, #db_warning_given?, #get_client, #get_host, #inside_workspace_boundary?, #invalidate_login, #mytask, #myworkspace, #myworkspace_id, #report_auth_info, #report_client, #report_exploit, #report_host, #report_loot, #report_note, #report_service, #report_vuln, #report_web_form, #report_web_page, #report_web_site, #report_web_vuln, #store_cred, #store_local, #store_loot

Methods included from Metasploit::Framework::Require

optionally, optionally_active_record_railtie, optionally_include_metasploit_credential_creation, #optionally_include_metasploit_credential_creation, optionally_require_metasploit_db_gem_engines

Methods included from Rex::Post::HWBridge::Ui::Console::CommandDispatcher

check_hash, #client, #log_error, #msf_loaded?, set_hash

Methods included from Ui::Text::DispatcherShell::CommandDispatcher

#cmd_help, #cmd_help_help, #cmd_help_tabs, #deprecated_cmd, #deprecated_commands, #deprecated_help, #docs_dir, #help_to_s, included, #print, #print_error, #print_good, #print_line, #print_status, #print_warning, #tab_complete_directory, #tab_complete_filenames, #tab_complete_generic, #tab_complete_source_address, #unknown_command, #update_prompt

Constructor Details

#initialize(shell) ⇒ Automotive

Returns a new instance of Automotive.



15
16
17
18
19
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/automotive.rb', line 15

def initialize(shell)
  super
  self.tpjobs     = []
  self.tpjob_id   = 0
end

Instance Attribute Details

#active_busObject

Returns the value of attribute active_bus.



329
330
331
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/automotive.rb', line 329

def active_bus
  @active_bus
end

#tpjob_idObject (protected)

:nodoc:



333
334
335
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/automotive.rb', line 333

def tpjob_id
  @tpjob_id
end

#tpjobsObject (protected)

:nodoc:



333
334
335
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/automotive.rb', line 333

def tpjobs
  @tpjobs
end

Instance Method Details

#cmd_busconfig(*args) ⇒ Object

Retrieves the current confiugration of a bus



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/automotive.rb', line 71

def cmd_busconfig(*args)
  bus = ''
  bus_config_opts = Rex::Parser::Arguments.new(
    '-h' => [ false, 'Help banner' ],
    '-b' => [ true, 'Target bus']
  )
  bus_config_opts.parse(args) do |opt, _idx, val|
    case opt
    when '-h'
      print_line("Usage: bus_config -b <busname>\n")
      print_line(bus_config_opts.usage)
      return
    when '-b'
      bus = val
    end
  end
  unless client.automotive.is_valid_bus? bus
    print_error("You must specify a valid bus via -b")
    return
  end
  config = client.automotive.get_bus_config(bus)
  config
end

#cmd_cansend(*args) ⇒ Object

Generic CAN send packet command



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/automotive.rb', line 129

def cmd_cansend(*args)
  bus = ''
  id = ''
  data = ''
  cansend_opts = Rex::Parser::Arguments.new(
    '-h' => [ false, 'Help Banner' ],
    '-b' => [ true, 'Target bus'],
    '-I' => [ true, 'CAN ID'],
    '-D' => [ true, 'Data packet in Hex']
  )
  cansend_opts.parse(args) do |opt, _idx, val|
    case opt
    when '-h'
      print_line("Usage: cansend -I <ID> -D <data>\n")
      print_line(cansend_opts.usage)
      return
    when '-b'
      bus = val
    when '-I'
      id = val
    when '-D'
      data = val
    end
  end
  bus = self.active_bus if bus.blank? && !self.active_bus.nil?
  unless client.automotive.is_valid_bus? bus
    print_error("You must specify a valid bus via -b")
    return
  end
  if id.blank? || data.blank?
    print_error("You must specify a CAN ID (-I) and the data packets (-D)")
    return
  end
  success = client.automotive.cansend(bus, id, data)
  success
end

#cmd_connect(*args) ⇒ Object

‘connects’ to a bus, this retrieves the supported_methods specific to this bus



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/automotive.rb', line 99

def cmd_connect(*args)
  bus = ''
  connect_opts = Rex::Parser::Arguments.new(
    '-h' => [ false, 'Help Banner' ],
    '-b' => [ true, 'Target bus']
  )
  connect_opts.parse(args) do |opt, _idx, val|
    case opt
    when '-h'
      print_line("Usage: connect -b <busname>\n")
      print_line(connect_opts.usage)
      return
    when '-b'
      bus = val
    end
  end
  unless client.automotive.is_valid_bus? bus
    print_error("You must specify a valid bus via -b")
    print_line("Current active bus: #{self.active_bus}") if self.active_bus
    return
  end
  self.active_bus = bus
  client.automotive.set_active_bus(bus)
  hw_methods = client.automotive.get_supported_methods(bus)
  hw_methods
end

#cmd_isotpsend(*args) ⇒ Object

Generic ISO-TP CAN send packet command



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/automotive.rb', line 169

def cmd_isotpsend(*args)
  bus = ''
  id = ''
  ret = ''
  data = ''
  timeout = nil
  maxpackets = nil
  flowcontrol = false
  padding = nil
  cansend_opts = Rex::Parser::Arguments.new(
    '-h' => [ false, 'Help Banner' ],
    '-b' => [ true, 'Target bus'],
    '-I' => [ true, 'CAN ID'],
    '-R' => [ true, 'Return ID'],
    '-D' => [ true, 'Data packet in Hex (Do not include ISOTP command size)'],
    '-t' => [ true, 'Timeout value'],
    '-p' => [ true, 'Padding value, none if not specified'],
    '-C' => [ false, 'Force flow control'],
    '-m' => [ true, 'Max packets to receive']
  )
  cansend_opts.parse(args) do |opt, _idx, val|
    case opt
    when '-h'
      print_line("Usage: isotpsend -I <ID> -D <data>\n")
      print_line(cansend_opts.usage)
      return
    when '-b'
      bus = val
    when '-I'
      id = val
    when '-R'
      ret = val
    when '-D'
      data = val
    when '-t'
      timeout = val.to_i
    when '-p'
      padding = val
    when '-C'
      flowcontrol = true
    when '-m'
      maxpackets = val.to_i
    end
  end
  bus = self.active_bus if bus.blank? && !self.active_bus.nil?
  unless client.automotive.is_valid_bus? bus
    print_error("You must specify a valid bus via -b")
    return
  end
  if id.blank? || data.blank?
    print_error("You must specify a CAN ID (-I) and the data packets (-D)")
    return
  end
  if ret.blank?
    ret = (id.hex + 8).to_s(16)
    print_line("Default return set to #{ret}")
  end
  bytes = data.scan(/../)  # Break up data string into 2 char (byte) chunks
  if bytes.size > 8
    print_error("Data section can only contain a max of 8 bytes (for now)")
    return
  end
  opt = {}
  opt['TIMEOUT'] = timeout unless timeout.nil?
  opt['MAXPKTS'] = maxpackets unless maxpackets.nil?
  opt['PADDING'] = padding unless padding.nil?
  opt['FC'] = true unless flowcontrol == false
  result = client.automotive.send_isotp_and_wait_for_response(bus, id, ret, bytes, opt)
  if result.key? 'Packets'
    result['Packets'].each do |pkt|
      print_line pkt.inspect
    end
  end
  print_line(result['error'].inspect) if result.key? 'error'
  result
end

#cmd_supported_busesObject

Lists all thesupported buses



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/automotive.rb', line 49

def cmd_supported_buses
  buses = client.automotive.get_supported_buses
  if buses.empty?
    print_line("none")
    return
  end
  str = "Available buses\n\n"
  first = true
  buses.each do |bus|
    unless first
      str += ", "
    end
    first = false
    str += bus["bus_name"] if bus.key? "bus_name"
  end
  str += "\n"
  print_line(str)
end

#cmd_testerpresent(*args) ⇒ Object

Sends TesterPresent packets as a background job



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/automotive.rb', line 249

def cmd_testerpresent(*args)
  bus = ''
  id = ''
  stop = false
  stopid = 0
  tp_opts = Rex::Parser::Arguments.new(
    '-h' => [ false, 'Help Banner' ],
    '-b' => [ true, 'Target bus' ],
    '-I' => [ true, 'CAN ID' ],
    '-x' => [ true, 'Stop TesterPresent JobID']
  )
  tp_opts.parse(args) do |opt, _idx, val|
    case opt
    when '-h'
      print_line("Usage: testerpresent -I <ID>\n")
      print_line(tp_opts.usage)
      return
    when '-b'
      bus = val
    when '-I'
      id = val
    when '-x'
      stop = true
      stopid = val.to_i
    end
  end
  bus = self.active_bus if bus.blank? && !self.active_bus.nil?
  unless client.automotive.is_valid_bus? bus
    print_error("You must specify a valid bus via -b")
    return
  end
  if id.blank? && !stop
    if self.tpjobs.size > 0
      print_line("TesterPresent is currently active")
      self.tpjobs.each_index do |jid|
        if self.tpjobs[jid]
          print_status("TesterPresent Job #{jid}: #{self.tpjobs[jid][:args].inspect}")
        end
      end
    else
      print_line("TesterPreset is not active.  Use -I to start")
    end
    return
  end
  if stop
    if self.tpjobs[stopid]
      self.tpjobs[stopid].kill
      self.tpjobs[stopid] = nil
      print_status("Stopped TesterPresent #{stopid}")
    else
      print_error("TesterPresent #{stopid} was not running")
    end
  else
    jid = self.tpjob_id
    print_status("Starting TesterPresent sender (#{self.tpjob_id})")
    self.tpjob_id += 1
    self.tpjobs[jid] = Rex::ThreadFactory.spawn("TesterPresent(#{id})-#{jid}", false, jid, args) do |myjid,xargs|
      ::Thread.current[:args] = xargs.dup
      begin
        loop do
          client.automotive.cansend(bus, id, "023E00")
          sleep(2)
        end
      rescue ::Exception => e
        print_error('Error in TesterPresent')
        elog('Error in TesterPreset', error: e)
      end
      self.tpjobs[myjid] = nil
      print_status("TesterPreset #{myjid} has stopped (#{::Thread.current[:args].inspect})")
    end
  end
end

#commandsObject

List of supported commands.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/automotive.rb', line 24

def commands
  all = {
    'supported_buses'   => 'Get supported buses',
    'busconfig'         => 'Get baud configs',
    'connect'           => 'Get HW supported methods for a bus',
    'cansend'           => 'Send a CAN packet',
    'isotpsend'         => 'Send an ISO-TP Packet and get a response',
    'testerpresent'     => 'Sends TesterPresent Pulses to the bus'
  }

  reqs = {
    'supported_buses'  => ['get_supported_buses'],
    'busconfig'        => ['get_bus_config'],
    'connect'          => ['get_supported_methods'],
    'cansend'          => ['cansend'],
    'testerpresent'    => ['testpresent']
  }

  # Ensure any requirements of the command are met
  all
end

#nameObject

Name for this dispatcher



325
326
327
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/automotive.rb', line 325

def name
  'Automotive'
end