Skip to content

Commit 7f444c3

Browse files
committed
Land rapid7#7967, DTC Improvements and Fixes
2 parents 06ebb22 + 321988c commit 7f444c3

File tree

11 files changed

+2539
-369
lines changed

11 files changed

+2539
-369
lines changed

lib/msf/core/post/hardware.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: binary -*-
22
module Msf::Post::Hardware
33
require 'msf/core/post/hardware/automotive/uds'
4+
require 'msf/core/post/hardware/automotive/dtc'
45
require 'msf/core/post/hardware/zigbee/utils'
56
end

lib/msf/core/post/hardware/automotive/dtc.rb

Lines changed: 2084 additions & 0 deletions
Large diffs are not rendered by default.

lib/msf/core/post/hardware/automotive/uds.rb

Lines changed: 317 additions & 257 deletions
Large diffs are not rendered by default.

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ def initialize(client)
3737
# @param bus [String] bus name
3838
#
3939
# @return [Boolean] return true if bus is valid
40-
def is_valid_bus? bus
40+
def is_valid_bus?(bus)
4141
valid = false
42-
get_supported_buses if self.buses == nil
43-
if not bus.blank?
44-
self.buses.each do |b|
42+
get_supported_buses if buses.nil?
43+
unless bus.blank?
44+
buses.each do |b|
4545
valid = true if b["bus_name"] == bus
4646
end
4747
end
@@ -55,10 +55,10 @@ def is_valid_bus? bus
5555
#
5656
# @return [Hash] client.send_request response with "Error" if any exist
5757
def check_for_errors(data)
58-
if data and data.has_key? "Packets"
58+
if data && (data.key? "Packets")
5959
if data["Packets"].size == 1
60-
if data["Packets"][0]["DATA"].size > 3 and data["Packets"][0]["DATA"][1].hex == 0x7F
61-
if ERR_MNEMONIC.has_key? data["Packets"][0]["DATA"][3].hex
60+
if data["Packets"][0]["DATA"].size > 3 && data["Packets"][0]["DATA"][1].hex == 0x7F
61+
if ERR_MNEMONIC.key? data["Packets"][0]["DATA"][3].hex
6262
err = data["Packets"][0]["DATA"][3].hex
6363
data["error"] = { ERR_MNEMONIC[err] => ERR_DESC[ERR_MNEMONIC[err]] }
6464
else
@@ -78,16 +78,16 @@ def check_for_errors(data)
7878
# @return [Array] Array of Hex string equivalents
7979
def array2hex(arr)
8080
# We give the flexibility of sending Integers or string hexes in the array
81-
arr.map { |b| "%02x" % (b.respond_to?("hex") ? b.hex : b )}
81+
arr.map { |b| "%02x" % (b.respond_to?("hex") ? b.hex : b ) }
8282
end
8383

8484
def set_active_bus(bus)
8585
self.active_bus = bus
8686
end
8787

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

9393
def get_bus_config(bus)
@@ -103,21 +103,23 @@ def cansend(bus, id, data)
103103
client.send_request("/automotive/#{bus}/cansend?id=#{id}&data=#{data}")
104104
end
105105

106-
def send_isotp_and_wait_for_response(bus, srcId, dstId, data, opt={})
107-
# TODO Implement sending ISO-TP > 8 bytes
106+
def send_isotp_and_wait_for_response(bus, src_id, dst_id, data, opt = {})
107+
# TODO: Implement sending ISO-TP > 8 bytes
108108
data = [ data ] if data.is_a? Integer
109109
if data.size < 8
110110
data = array2hex(data).join
111-
request_str = "/automotive/#{bus}/isotpsend_and_wait?srcid=#{srcId}&dstid=#{dstId}&data=#{data}"
112-
request_str += "&timeout=#{opt["TIMEOUT"]}" if opt.has_key? "TIMEOUT"
113-
request_str += "&maxpkts=#{opt["MAXPKTS"]}" if opt.has_key? "MAXPKTS"
111+
request_str = "/automotive/#{bus}/isotpsend_and_wait?srcid=#{src_id}&dstid=#{dst_id}&data=#{data}"
112+
request_str += "&timeout=#{opt['TIMEOUT']}" if opt.key? "TIMEOUT"
113+
request_str += "&maxpkts=#{opt['MAXPKTS']}" if opt.key? "MAXPKTS"
114114
return check_for_errors(client.send_request(request_str))
115115
end
116-
return nil
116+
nil
117117
end
118118

119119
attr_reader :buses, :active_bus
120-
private
120+
121+
private
122+
121123
attr_writer :buses, :active_bus
122124

123125
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ module UDSErrors
101101
"SFNSIAS" => "Sub-Function Not Supoorted In Active Session",
102102
"SNSIAS" => "Service Not Supported In Active Session",
103103
"RTH" => "RPM Too High",
104-
"RTL" => "RPM Too Low".
104+
"RTL" => "RPM Too Low",
105105
"EIR" => "Engine is Running",
106106
"EINR" => "Engine is not Running",
107107
"ERTTL" => "Engine Run Time Too Low",

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ def commands
4040
#
4141
def cmd_supported_buses
4242
buses = client.automotive.get_supported_buses
43-
if not buses.size > 0
43+
unless !buses.empty?
4444
print_line("none")
4545
return
4646
end
4747
str = "Available buses\n\n"
4848
first = true
4949
buses.each do |bus|
50-
if not first
50+
unless first
5151
str += ", "
5252
end
5353
first = false
54-
str += bus["bus_name"] if bus.has_key? "bus_name"
54+
str += bus["bus_name"] if bus.key? "bus_name"
5555
end
56-
str+="\n"
56+
str += "\n"
5757
print_line(str)
5858
end
5959

@@ -76,11 +76,12 @@ def cmd_busconfig(*args)
7676
bus = val
7777
end
7878
end
79-
if not client.automotive.is_valid_bus? bus
79+
unless client.automotive.is_valid_bus? bus
8080
print_error("You must specify a valid bus via -b")
8181
return
8282
end
8383
config = client.automotive.get_bus_config(bus)
84+
config
8485
end
8586

8687
#
@@ -103,13 +104,14 @@ def cmd_connect(*args)
103104
bus = val
104105
end
105106
end
106-
if not client.automotive.is_valid_bus? bus
107+
unless client.automotive.is_valid_bus? bus
107108
print_error("You must specify a valid bus via -b")
108109
return
109110
end
110-
self.active_bus = bus
111+
active_bus = bus
111112
client.automotive.set_active_bus(bus)
112113
hw_methods = client.automotive.get_supported_methods(bus)
114+
hw_methods
113115
end
114116

115117
#
@@ -139,16 +141,17 @@ def cmd_cansend(*args)
139141
data = val
140142
end
141143
end
142-
bus = self.active_bus if bus.blank? and not self.active_bus == nil
143-
if not client.automotive.is_valid_bus? bus
144+
bus = active_bus if bus.blank? && !active_bus.nil?
145+
unless client.automotive.is_valid_bus? bus
144146
print_error("You must specify a valid bus via -b")
145147
return
146148
end
147-
if id.blank? or data.blank?
149+
if id.blank? || data.blank?
148150
print_error("You must specify a CAN ID (-I) and the data packets (-D)")
149151
return
150152
end
151153
success = client.automotive.cansend(bus, id, data)
154+
success
152155
end
153156

154157
#
@@ -158,7 +161,8 @@ def name
158161
'Automotive'
159162
end
160163

161-
private
164+
private
165+
162166
attr_accessor :active_bus
163167

164168
end

modules/auxiliary/client/hwbridge/connect.rb

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ def initialize(info={})
3939
'References' =>
4040
[
4141
[ 'URL', 'http://opengarages.org/hwbridge' ] # TODO
42-
],
42+
]
4343
}
4444
))
4545
register_options(
4646
[
4747
Opt::RPORT(8080),
4848
Opt::RHOST("127.0.0.1"),
4949
OptBool.new("DEBUGJSON", [false, "Additional debugging out for JSON requests to HW Bridge", false]),
50-
OptString.new('TARGETURI', [ true, "The path to the hwbridge API", '/'])
50+
OptString.new('TARGETURI', [ true, "The path to the hwbridge API", '/'])
5151
],
5252
self.class
5353
)
@@ -58,14 +58,14 @@ def initialize(info={})
5858
# Generic fetch json call. returns hash of json
5959
#
6060
def fetch_json(uri)
61-
tpath = normalize_uri("#{datastore["TARGETURI"]}/#{uri}")
61+
tpath = normalize_uri("#{datastore['TARGETURI']}/#{uri}")
6262
res = send_request_cgi({
6363
'uri' => tpath,
64-
'method' => 'GET',
64+
'method' => 'GET'
6565
})
66-
return nil if not res or not res.body or not res.code
67-
if (res.code == 200)
68-
print_status res.body if datastore["DEBUGJSON"] == true
66+
return nil if !res || !res.body || !res.code
67+
if res.code == 200
68+
print_status res.body if datastore['DEBUGJSON'] == true
6969
return JSON.parse(res.body)
7070
elsif res.code == 401
7171
print_error "Access Denied: #{res.body}"
@@ -97,7 +97,7 @@ def print_disclaimer
9797
# Uses status information to automatically load proper extensions
9898
#
9999
def autoload_extensions(sess)
100-
if self.hw_specialty.has_key? "automotive"
100+
if self.hw_specialty.key? "automotive"
101101
sess.load_automotive if self.hw_specialty["automotive"] == true
102102
end
103103
if self.hw_specialty.has_key? "zigbee"
@@ -109,7 +109,7 @@ def autoload_extensions(sess)
109109
# If the hardware contains custom methods, create functions for those
110110
#
111111
def load_custom_methods(sess)
112-
if self.hw_capabilities.has_key? "custom_methods"
112+
if self.hw_capabilities.key? "custom_methods"
113113
sess.load_custom_methods if self.hw_capabilities["custom_methods"] == true
114114
end
115115
end
@@ -119,23 +119,23 @@ def load_custom_methods(sess)
119119
#
120120
def get_status
121121
data = fetch_json("/status")
122-
if not data == nil
123-
if data.has_key? "operational"
122+
unless data.nil?
123+
if data.key? "operational"
124124
@last_access = Time.now
125-
if data.has_key? "hw_specialty"
125+
if data.key? "hw_specialty"
126126
self.hw_specialty = data["hw_specialty"]
127127
end
128-
if data.has_key? "hw_capabilities"
128+
if data.key? "hw_capabilities"
129129
self.hw_capabilities = data["hw_capabilities"]
130130
end
131131
end
132132
end
133133
end
134134

135135
def run
136-
print_status "Attempting to connect to #{datastore["RHOST"]}..."
136+
print_status "Attempting to connect to #{datastore['RHOST']}..."
137137
self.get_status()
138-
if not @last_access == nil
138+
if !@last_access.nil?
139139
sess = Msf::Sessions::HWBridge.new(self)
140140
sess.set_from_exploit(self)
141141

@@ -152,7 +152,9 @@ def run
152152

153153
attr_reader :hw_specialty
154154
attr_reader :hw_capabilities
155-
protected
155+
156+
protected
157+
156158
attr_writer :hw_specialty
157159
attr_writer :hw_capabilities
158160
end

0 commit comments

Comments
 (0)