Skip to content

Commit 36026ba

Browse files
committed
Fixed active buses not being recorded. The 'connect' command now works for other extensions as well as modules. Added TesterPresent background packet transmissions to hold debugging sessions open.
Signed-off-by: Craig Smith <[email protected]>
1 parent 2012ebf commit 36026ba

File tree

2 files changed

+98
-9
lines changed

2 files changed

+98
-9
lines changed

lib/rex/post/hwbridge/extensions/automotive/automotive.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def is_valid_bus?(bus)
4141
valid = false
4242
get_supported_buses if buses.nil?
4343
unless bus.blank?
44-
buses.each do |b|
44+
self.buses.each do |b|
4545
valid = true if b["bus_name"] == bus
4646
end
4747
end
@@ -86,8 +86,8 @@ def set_active_bus(bus)
8686
end
8787

8888
def get_supported_buses
89-
buses = client.send_request("/automotive/supported_buses")
90-
buses
89+
self.buses = client.send_request("/automotive/supported_buses")
90+
self.buses
9191
end
9292

9393
def get_bus_config(bus)

lib/rex/post/hwbridge/ui/console/command_dispatcher/automotive.rb

Lines changed: 95 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ class Console::CommandDispatcher::Automotive
1313
include Console::CommandDispatcher
1414
include Msf::Auxiliary::Report
1515

16+
def initialize(shell)
17+
super
18+
self.tpjobs = []
19+
self.tpjob_id = 0
20+
end
21+
1622
#
1723
# List of supported commands.
1824
#
@@ -21,14 +27,16 @@ def commands
2127
'supported_buses' => 'Get supported buses',
2228
'busconfig' => 'Get baud configs',
2329
'connect' => 'Get HW supported methods for a bus',
24-
'cansend' => 'Send a CAN packet'
30+
'cansend' => 'Send a CAN packet',
31+
'testerpresent' => 'Sends TesterPresent Pulses to the bus'
2532
}
2633

2734
reqs = {
2835
'supported_buses' => ['get_supported_buses'],
2936
'busconfig' => ['get_bus_config'],
3037
'connect' => ['get_supported_methods'],
31-
'cansend' => ['cansend']
38+
'cansend' => ['cansend'],
39+
'testerpresent' => ['testpresent']
3240
}
3341

3442
# Ensure any requirements of the command are met
@@ -106,9 +114,10 @@ def cmd_connect(*args)
106114
end
107115
unless client.automotive.is_valid_bus? bus
108116
print_error("You must specify a valid bus via -b")
117+
print_line("Current active bus: #{self.active_bus}") if self.active_bus
109118
return
110119
end
111-
active_bus = bus
120+
self.active_bus = bus
112121
client.automotive.set_active_bus(bus)
113122
hw_methods = client.automotive.get_supported_methods(bus)
114123
hw_methods
@@ -141,7 +150,7 @@ def cmd_cansend(*args)
141150
data = val
142151
end
143152
end
144-
bus = active_bus if bus.blank? && !active_bus.nil?
153+
bus = self.active_bus if bus.blank? && !self.active_bus.nil?
145154
unless client.automotive.is_valid_bus? bus
146155
print_error("You must specify a valid bus via -b")
147156
return
@@ -154,17 +163,97 @@ def cmd_cansend(*args)
154163
success
155164
end
156165

166+
#
167+
# Sends TesterPresent packets as a background job
168+
#
169+
def cmd_testerpresent(*args)
170+
bus = ''
171+
id = ''
172+
stop = false
173+
stopid = 0
174+
tp_opts = Rex::Parser::Arguments.new(
175+
'-h' => [ false, 'Help Banner' ],
176+
'-b' => [ true, 'Target bus' ],
177+
'-I' => [ true, 'CAN ID' ],
178+
'-x' => [ true, 'Stop TesterPresent JobID']
179+
)
180+
tp_opts.parse(args) do |opt, _idx, val|
181+
case opt
182+
when '-h'
183+
print_line("Usage: testerpresent -I <ID>\n")
184+
print_line(tp_opts.usage)
185+
return
186+
when '-b'
187+
bus = val
188+
when '-I'
189+
id = val
190+
when '-x'
191+
stop = true
192+
stopid = val.to_i
193+
end
194+
end
195+
bus = self.active_bus if bus.blank? && !self.active_bus.nil?
196+
unless client.automotive.is_valid_bus? bus
197+
print_error("You must specify a valid bus via -b")
198+
return
199+
end
200+
if id.blank? && !stop
201+
if self.tpjobs.size > 0
202+
print_line("TesterPresent is currently active")
203+
self.tpjobs.each_index do |jid|
204+
if self.tpjobs[jid]
205+
print_status("TesterPresent Job #{jid}: #{self.tpjobs[jid][:args].inspect}")
206+
end
207+
end
208+
else
209+
print_line("TesterPreset is not active. Use -I to start")
210+
end
211+
return
212+
end
213+
unless stop
214+
jid = self.tpjob_id
215+
print_status("Starting TesterPresent sender (#{self.tpjob_id})")
216+
self.tpjob_id += 1
217+
self.tpjobs[jid] = Rex::ThreadFactory.spawn("TesterPresent(#{id})-#{jid}", false, jid, args) do |myjid,xargs|
218+
::Thread.current[:args] = xargs.dup
219+
begin
220+
loop do
221+
client.automotive.cansend(bus, id, "023E00")
222+
sleep(2)
223+
end
224+
rescue ::Exception
225+
print_error("Error in TesterPResent: #{$!.class} #{$!}")
226+
elog("Error in TesterPreset: #{$!.class} #{$!}")
227+
dlog("Callstack: #{$@.join("\n")}")
228+
end
229+
self.tpjobs[myjid] = nil
230+
print_status("TesterPreset #{myjid} has stopped (#{::Thread.current[:args].inspect})")
231+
end
232+
else
233+
if self.tpjobs[stopid]
234+
self.tpjobs[stopid].kill
235+
self.tpjobs[stopid] = nil
236+
print_status("Stopped TesterPresent #{stopid}")
237+
else
238+
print_error("TesterPresent #{stopid} was not running")
239+
end
240+
end
241+
end
242+
157243
#
158244
# Name for this dispatcher
159245
#
160246
def name
161247
'Automotive'
162248
end
163249

164-
private
165-
166250
attr_accessor :active_bus
167251

252+
protected
253+
254+
attr_accessor :tpjobs, :tpjob_id # :nodoc:
255+
256+
168257
end
169258

170259
end

0 commit comments

Comments
 (0)