2017-01-07 03:51:41 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# This module requires Metasploit: http://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.1"
|
|
|
|
|
|
|
|
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' ]
|
|
|
|
],
|
|
|
|
'PassiveActions' =>
|
|
|
|
[
|
|
|
|
'WebServer'
|
|
|
|
],
|
|
|
|
'DefaultAction' => 'WebServer'))
|
|
|
|
|
|
|
|
@operational_status = 0 # 0=unk, 1=connected, 2=not connected
|
2017-03-08 17:21:33 +00:00
|
|
|
@last_errors = {}
|
2017-01-07 03:51:41 +00:00
|
|
|
@server_started = Time.new
|
2017-03-08 17:21:33 +00:00
|
|
|
@can_interfaces = []
|
2017-01-07 03:51:41 +00:00
|
|
|
@pkt_response = {} # Candump returned packets
|
2017-04-20 18:41:35 +00:00
|
|
|
@packets_sent = 0
|
|
|
|
@last_sent = nil
|
2017-01-07 03:51:41 +00:00
|
|
|
end
|
|
|
|
|
2017-03-08 17:21:33 +00:00
|
|
|
def detect_can
|
|
|
|
@can_interfaces = []
|
2017-01-07 03:51:41 +00:00
|
|
|
Socket.getifaddrs.each do |i|
|
2017-03-08 17:21:33 +00:00
|
|
|
if i.name =~ /^can\d+$/ || i.name =~ /^vcan\d+$/ || i.name =~ /^slcan\d+$/
|
2017-01-07 03:51:41 +00:00
|
|
|
@can_interfaces << i.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-08 17:21:33 +00:00
|
|
|
def get_status
|
|
|
|
status = {}
|
2017-01-07 03:51:41 +00:00
|
|
|
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"
|
2017-03-08 17:21:33 +00:00
|
|
|
unless @can_interfaces.empty?
|
2017-01-07 03:51:41 +00:00
|
|
|
status["hw_specialty"]["automotive"] = true
|
|
|
|
status["hw_capabilities"]["can"] = true
|
|
|
|
end
|
|
|
|
status["hw_capabilities"]["custom_methods"] = true # To test custom methods
|
|
|
|
status
|
|
|
|
end
|
|
|
|
|
2017-03-08 17:21:33 +00:00
|
|
|
def get_statistics
|
|
|
|
stats = {}
|
2017-01-07 03:51:41 +00:00
|
|
|
stats["uptime"] = Time.now - @server_started
|
2017-04-20 18:41:35 +00:00
|
|
|
stats["packet_stats"] = @packets_sent
|
|
|
|
stats["last_request"] = @last_sent if @last_sent
|
2017-01-07 03:51:41 +00:00
|
|
|
stats["voltage"] = "not supported"
|
|
|
|
stats
|
|
|
|
end
|
|
|
|
|
2017-03-08 17:21:33 +00:00
|
|
|
def get_datetime
|
2017-01-07 03:51:41 +00:00
|
|
|
{ "system_datetime" => Time.now }
|
|
|
|
end
|
|
|
|
|
2017-03-08 17:21:33 +00:00
|
|
|
def get_timezone
|
2017-01-07 03:51:41 +00:00
|
|
|
{ "system_timezone" => Time.now.getlocal.zone }
|
|
|
|
end
|
|
|
|
|
2017-03-08 17:21:33 +00:00
|
|
|
def get_ip_config
|
2017-01-07 03:51:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Stub fucntion to test custom methods
|
|
|
|
# Defines a method "sample_cmd" with one argument "data" which is required
|
|
|
|
#
|
2017-03-08 17:21:33 +00:00
|
|
|
def get_custom_methods
|
|
|
|
m = {}
|
|
|
|
m["Methods"] = []
|
2017-01-07 03:51:41 +00:00
|
|
|
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
|
|
|
|
|
2017-03-08 17:21:33 +00:00
|
|
|
def get_auto_supported_buses
|
2017-01-07 03:51:41 +00:00
|
|
|
detect_can()
|
2017-03-08 17:21:33 +00:00
|
|
|
buses = []
|
2017-01-07 03:51:41 +00:00
|
|
|
@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`
|
2017-03-08 17:21:33 +00:00
|
|
|
unless $?.success?
|
2017-01-07 03:51:41 +00:00
|
|
|
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}")
|
2017-04-20 18:41:35 +00:00
|
|
|
@packets_sent += 1
|
|
|
|
@last_sent = Time.now.to_i
|
2017-01-07 03:51:41 +00:00
|
|
|
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|
|
2017-03-08 17:21:33 +00:00
|
|
|
if line =~ /\w+\s+(\w+) \[\d\] (.+)$/
|
2017-01-07 03:51:41 +00:00
|
|
|
id = $1
|
|
|
|
str_data = $2
|
|
|
|
data = str_data.split
|
2017-03-08 17:21:33 +00:00
|
|
|
hash["Packets"] << { "ID" => id, "DATA" => data }
|
2017-01-07 03:51:41 +00:00
|
|
|
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)
|
2017-03-08 17:21:33 +00:00
|
|
|
Thread::exit
|
2017-01-07 03:51:41 +00:00
|
|
|
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
|
|
|
|
# timeout = optional int to timeout on lack of response
|
|
|
|
# maxpkts = max number of packets to recieve
|
2017-03-08 17:21:33 +00:00
|
|
|
def isotp_send_and_wait(bus, srcid, dstid, data, timeout = 2000, maxpkts = 3)
|
2017-01-07 03:51:41 +00:00
|
|
|
result = {}
|
|
|
|
result["Success"] = false
|
|
|
|
srcid = srcid.to_i(16).to_s(16)
|
|
|
|
dstid = dstid.to_i(16).to_s(16)
|
|
|
|
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
|
|
|
|
# Should we ever require isotpsend for this?
|
|
|
|
`which cansend`
|
2017-03-08 17:21:33 +00:00
|
|
|
unless $?.success?
|
2017-01-07 03:51:41 +00:00
|
|
|
print_error("cansend from can-utils not found in path")
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
@can_interfaces.each do |can|
|
|
|
|
if can == bus
|
2017-03-08 17:21:33 +00:00
|
|
|
candump(bus, dstid, timeout, maxpkts)
|
2017-01-07 03:51:41 +00:00
|
|
|
system("cansend #{bus} #{srcid}##{bytes}")
|
2017-04-20 18:41:35 +00:00
|
|
|
@packets_sent += 1
|
|
|
|
@last_sent = Time.now.to_i
|
2017-01-07 03:51:41 +00:00
|
|
|
result["Success"] = true if $?.success?
|
|
|
|
result["Packets"] = []
|
|
|
|
$candump_sniffer.join
|
2017-03-08 17:21:33 +00:00
|
|
|
unless @pkt_response.empty?
|
2017-01-07 03:51:41 +00:00
|
|
|
result = @pkt_response
|
|
|
|
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"] = "Succesfully processed: #{data}"
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2017-03-08 17:21:33 +00:00
|
|
|
def not_supported
|
2017-01-07 03:51:41 +00:00
|
|
|
{ "status" => "not supported" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_request_uri(cli, request)
|
|
|
|
if request.uri =~ /status$/i
|
2017-04-20 20:18:24 +00:00
|
|
|
print_status("Sending status...") if datastore['VERBOSE']
|
2017-01-07 03:51:41 +00:00
|
|
|
send_response_html(cli, get_status().to_json(), { 'Content-Type' => 'application/json' })
|
2017-03-08 17:21:33 +00:00
|
|
|
elsif request.uri =~ /statistics$/i
|
2017-04-20 20:18:24 +00:00
|
|
|
print_status("Sending statistics...") if datastore['VERBOSE']
|
2017-01-07 03:51:41 +00:00
|
|
|
send_response_html(cli, get_statistics().to_json(), { 'Content-Type' => 'application/json' })
|
2017-03-08 17:21:33 +00:00
|
|
|
elsif request.uri =~ /settings\/datetime\/get$/i
|
2017-04-20 20:18:24 +00:00
|
|
|
print_status("Sending Datetime") if datastore['VERBOSE']
|
2017-01-07 03:51:41 +00:00
|
|
|
send_response_html(cli, get_datetime().to_json(), { 'Content-Type' => 'application/json' })
|
2017-03-08 17:21:33 +00:00
|
|
|
elsif request.uri =~ /settings\/timezone\/get$/i
|
2017-04-20 20:18:24 +00:00
|
|
|
print_status("Sending Timezone") if datastore['VERBOSE']
|
2017-01-07 03:51:41 +00:00
|
|
|
send_response_html(cli, get_timezone().to_json(), { 'Content-Type' => 'application/json' })
|
2017-03-08 17:21:33 +00:00
|
|
|
elsif request.uri =~ /custom_methods$/i
|
2017-04-20 20:18:24 +00:00
|
|
|
print_status("Sending custom methods") if datastore['VERBOSE']
|
2017-01-07 03:51:41 +00:00
|
|
|
send_response_html(cli, get_custom_methods().to_json(), { 'Content-Type' => 'application/json' })
|
2017-03-08 17:21:33 +00:00
|
|
|
elsif request.uri =~ /custom\/sample_cmd\?data=(\S+)$/
|
2017-04-20 20:18:24 +00:00
|
|
|
print_status("Request for custom command with args #{$1}") if datastore['VERBOSE']
|
2017-01-07 03:51:41 +00:00
|
|
|
send_response_html(cli, sample_custom_method($1).to_json(), { 'Content-Type' => 'application/json' })
|
2017-03-08 17:21:33 +00:00
|
|
|
elsif request.uri =~ /automotive/i
|
2017-01-07 03:51:41 +00:00
|
|
|
if request.uri =~ /automotive\/supported_buses/
|
2017-04-20 20:18:24 +00:00
|
|
|
print_status("Sending known buses...") if datastore['VERBOSE']
|
2017-01-07 03:51:41 +00:00
|
|
|
send_response_html(cli, get_auto_supported_buses().to_json, { 'Content-Type' => 'application/json' })
|
2017-03-08 17:21:33 +00:00
|
|
|
elsif request.uri =~ /automotive\/(\w+)\/cansend\?id=(\w+)&data=(\w+)/
|
2017-04-20 20:18:24 +00:00
|
|
|
print_status("Request to send CAN packets for #{$1} => #{$2}##{$3}") if datastore['VERBOSE']
|
2017-01-07 03:51:41 +00:00
|
|
|
send_response_html(cli, cansend($1, $2, $3).to_json(), { 'Content-Type' => 'application/json' })
|
2017-03-08 17:21:33 +00:00
|
|
|
elsif request.uri =~ /automotive\/(\w+)\/isotpsend_and_wait\?srcid=(\w+)&dstid=(\w+)&data=(\w+)/
|
2017-01-07 03:51:41 +00:00
|
|
|
bus = $1; srcid = $2; dstid = $3; data = $4
|
2017-04-20 20:18:24 +00:00
|
|
|
print_status("Request to send ISO-TP packet and wait for response #{srcid}##{data} => #{dstid}") if datastore['VERBOSE']
|
2017-01-07 03:51:41 +00:00
|
|
|
timeout = 1500
|
|
|
|
maxpkts = 3
|
2017-03-08 17:21:33 +00:00
|
|
|
timeout = $1 if request.uri =~ /&timeout=(\d+)/
|
|
|
|
maxpkts = $1 if request.uri =~ /&maxpkts=(\d+)/
|
2017-01-07 03:51:41 +00:00
|
|
|
send_response_html(cli, isotp_send_and_wait(bus, srcid, dstid, data, timeout, maxpkts).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
|
2017-03-08 17:21:33 +00:00
|
|
|
detect_can
|
2017-01-07 03:51:41 +00:00
|
|
|
@server_started = Time.now
|
2017-03-08 17:21:33 +00:00
|
|
|
exploit
|
2017-01-07 03:51:41 +00:00
|
|
|
end
|
|
|
|
end
|