/
local_hwbridge.rb
317 lines (295 loc) · 10.5 KB
/
local_hwbridge.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
125
126
127
128
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
165
166
167
168
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
245
246
247
248
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
##
#
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
#
# TODO: SSL Support, Authentication, Listen to localhost only by default
#
##
class MetasploitModule < Msf::Auxiliary
include Msf::Exploit::Remote::HttpServer::HTML
include Msf::Auxiliary::Report
HWBRIDGE_API_VERSION = "0.0.4"
def initialize(info = {})
super(update_info(info,
'Name' => 'Hardware Bridge Server',
'Description' => %q{
This module sets up a web server to bridge communications between
Metasploit and physically attached hardware.
Currently this module supports: automotive
},
'Author' => [ 'Craig Smith' ],
'License' => MSF_LICENSE,
'Actions' =>
[
[ 'WebServer', 'Description' => 'Run HWBridge web server' ]
],
'PassiveActions' =>
[
'WebServer'
],
'DefaultAction' => 'WebServer'))
@operational_status = 0 # 0=unk, 1=connected, 2=not connected
@last_errors = {}
@server_started = Time.new
@can_interfaces = []
@pkt_response = {} # Candump returned packets
@packets_sent = 0
@last_sent = nil
end
def detect_can
@can_interfaces = []
Socket.getifaddrs.each do |i|
if i.name =~ /^can\d+$/ || i.name =~ /^vcan\d+$/ || i.name =~ /^slcan\d+$/
@can_interfaces << i.name
end
end
end
def get_status
status = {}
status["operational"] = @operational_status
status["hw_specialty"] = {}
status["hw_capabilities"] = {}
status["last_10_errors"] = @last_errors # NOTE: no support for this yet
status["api_version"] = HWBRIDGE_API_VERSION
status["fw_version"] = "not supported"
status["hw_version"] = "not supported"
unless @can_interfaces.empty?
status["hw_specialty"]["automotive"] = true
status["hw_capabilities"]["can"] = true
end
status["hw_capabilities"]["custom_methods"] = true # To test custom methods
status
end
def get_statistics
stats = {}
stats["uptime"] = Time.now - @server_started
stats["packet_stats"] = @packets_sent
stats["last_request"] = @last_sent if @last_sent
stats["voltage"] = "not supported"
stats
end
def get_datetime
{ "system_datetime" => Time.now }
end
def get_timezone
{ "system_timezone" => Time.now.getlocal.zone }
end
def get_ip_config
end
#
# Stub function to test custom methods
# Defines a method "sample_cmd" with one argument "data" which is required
#
def get_custom_methods
m = {}
m["Methods"] = []
meth = { "method_name" => "custom/sample_cmd", "method_desc" => "Sample HW test command", "args" => [] }
arg = { "arg_name" => "data", "arg_type" => "string", "required" => true }
meth["args"] << arg
meth["return"] = "string"
m["Methods"] << meth
m
end
def get_auto_supported_buses
detect_can()
buses = []
@can_interfaces.each do |can|
buses << { "bus_name" => can }
end
buses
end
# Sends a raw CAN packet
# bus = string
# id = hex ID
# data = string of up to 8 hex bytes
def cansend(bus, id, data)
result = {}
result["Success"] = false
id = id.to_i(16).to_s(16) # Clean up the HEX
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")
return result
end
`which cansend`
unless $?.success?
print_error("cansend from can-utils not found in path")
return result
end
@can_interfaces.each do |can|
if can == bus
system("cansend #{bus} #{id}##{bytes.join}")
@packets_sent += 1
@last_sent = Time.now.to_i
result["Success"] = true if $?.success?
end
end
result
end
# Converts candump output to {Packets => [{ ID=> id DATA => [] }]}
def candump2hash(str_packets)
hash = {}
hash["Packets"] = []
lines = str_packets.split(/\n/)
lines.each do |line|
if line =~ /\w+\s+(\w+) \[\d\] (.+)$/
id = $1
str_data = $2
data = str_data.split
hash["Packets"] << { "ID" => id, "DATA" => data }
end
end
hash
end
def candump(bus, id, timeout, maxpkts)
$candump_sniffer = Thread.new do
output = `candump #{bus},#{id}:FFFFFF -T #{timeout} -n #{maxpkts}`
@pkt_response = candump2hash(output)
Thread::exit
end
end
# Sends an ISO-TP style CAN packet and waites for a response or a timeout
# bus = string
# srcid = hex id of the sent packet
# dstid = hex id of the return packets
# data = string of hex bytes to send
# OPT = Options
# timeout = optional int to timeout on lack of response
# maxpkts = max number of packets to receive
# padding = append bytes to end of packet (Doesn't increase reported ISO-TP size)
# fc = flow control, if true forces flow control packets
def isotp_send_and_wait(bus, srcid, dstid, data, opt = {})
result = {}
result["Success"] = false
srcid = srcid.to_i(16).to_s(16)
dstid = dstid.to_i(16).to_s(16)
timeout = 2000
maxpkts = 3
flowcontrol = nil
padding = nil
timeout = opt['TIMEOUT'] if opt.key? 'TIMEOUT'
maxpkts = opt['MAXPKTS'] if opt.key? 'MAXPKTS'
padding = opt['PADDING'] if opt.key? 'PADDING'
flowcontrol = opt['FC'] if opt.key? 'FC'
bytes = data.scan(/../)
if bytes.size > 8
print_error("Data section currently has to be less than 8 bytes")
return result
else
sz = "%02x" % bytes.size
bytes = sz + bytes.join
end
if padding && bytes.size < 16 # 16 == 8 bytes because of ascii size
padding = "%02x" % padding.to_i
bytes += ([ padding ] * (16 - bytes.size)).join
end
# Should we ever require isotpsend for this?
`which cansend`
unless $?.success?
print_error("cansend from can-utils not found in path")
return result
end
@can_interfaces.each do |can|
if can == bus
if flowcontrol
candump(bus, dstid, timeout, 1)
system("cansend #{bus} #{srcid}##{bytes}")
@packets_sent += 1
@last_sent = Time.now.to_i
result["Success"] = true if $?.success?
result["Packets"] = []
$candump_sniffer.join
unless @pkt_response.empty?
result = @pkt_response
if result.key?("Packets") && result["Packets"].size > 0 && result["Packets"][0].key?("DATA")
if result["Packets"][0]["DATA"][0] == "10"
system("cansend #{bus} #{srcid}#3000000000000000")
candump(bus, dstid, timeout, maxpkts)
@packets_sent += 1
@last_sent = Time.now.to_i
$candump_sniffer.join
unless @pkt_response.empty?
if @pkt_response.key?("Packets") && @pkt_response["Packets"].size > 0
result["Packets"] += @pkt_response["Packets"]
end
end
end
end
end
else
candump(bus, dstid, timeout, maxpkts)
system("cansend #{bus} #{srcid}##{bytes}")
@packets_sent += 1
@last_sent = Time.now.to_i
result["Success"] = true if $?.success?
result["Packets"] = []
$candump_sniffer.join
unless @pkt_response.empty?
result = @pkt_response
end
end
end
end
result
end
#
# This is just a sample method that should show up
# as sample_cmd in the interface
#
def sample_custom_method(data)
res = {}
res["value"] = "Successfully processed: #{data}"
res
end
def not_supported
{ "status" => "not supported" }
end
def on_request_uri(cli, request)
if request.uri =~ /status$/i
print_status("Sending status...") if datastore['VERBOSE']
send_response_html(cli, get_status().to_json(), { 'Content-Type' => 'application/json' })
elsif request.uri =~ /statistics$/i
print_status("Sending statistics...") if datastore['VERBOSE']
send_response_html(cli, get_statistics().to_json(), { 'Content-Type' => 'application/json' })
elsif request.uri =~ /settings\/datetime\/get$/i
print_status("Sending Datetime") if datastore['VERBOSE']
send_response_html(cli, get_datetime().to_json(), { 'Content-Type' => 'application/json' })
elsif request.uri =~ /settings\/timezone\/get$/i
print_status("Sending Timezone") if datastore['VERBOSE']
send_response_html(cli, get_timezone().to_json(), { 'Content-Type' => 'application/json' })
elsif request.uri =~ /custom_methods$/i
print_status("Sending custom methods") if datastore['VERBOSE']
send_response_html(cli, get_custom_methods().to_json(), { 'Content-Type' => 'application/json' })
elsif request.uri =~ /custom\/sample_cmd\?data=(\S+)$/
print_status("Request for custom command with args #{$1}") if datastore['VERBOSE']
send_response_html(cli, sample_custom_method($1).to_json(), { 'Content-Type' => 'application/json' })
elsif request.uri =~ /automotive/i
if request.uri =~ /automotive\/supported_buses/
print_status("Sending known buses...") if datastore['VERBOSE']
send_response_html(cli, get_auto_supported_buses().to_json, { 'Content-Type' => 'application/json' })
elsif request.uri =~ /automotive\/(\w+)\/cansend\?id=(\w+)&data=(\w+)/
print_status("Request to send CAN packets for #{$1} => #{$2}##{$3}") if datastore['VERBOSE']
send_response_html(cli, cansend($1, $2, $3).to_json(), { 'Content-Type' => 'application/json' })
elsif request.uri =~ /automotive\/(\w+)\/isotpsend_and_wait\?srcid=(\w+)&dstid=(\w+)&data=(\w+)/
bus = $1; srcid = $2; dstid = $3; data = $4
print_status("Request to send ISO-TP packet and wait for response #{srcid}##{data} => #{dstid}") if datastore['VERBOSE']
opt = {}
opt['TIMEOUT'] = $1 if request.uri =~ /&timeout=(\d+)/
opt['MAXPKTS'] = $1 if request.uri =~ /&maxpkts=(\d+)/
opt['PADDING'] = $1 if request.uri =~ /&padding=(\d+)/
opt['FC'] = true if request.uri =~ /&fc=true/i
send_response_html(cli, isotp_send_and_wait(bus, srcid, dstid, data, opt).to_json(), { 'Content-Type' => 'application/json' })
else
send_response_html(cli, not_supported().to_json(), { 'Content-Type' => 'application/json' })
end
else
send_response_html(cli, not_supported().to_json(), { 'Content-Type' => 'application/json' })
end
end
def run
detect_can
@server_started = Time.now
exploit
end
end