Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/post/hwbridge/extensions/automotive/automotive.rb
Views: 11705
#1# -*- coding: binary -*-2require 'rex/post/hwbridge/extensions/automotive/uds_errors'3require 'rex/post/hwbridge/client'45module Rex6module Post7module HWBridge8module Extensions9module Automotive1011###12# Automotive extension - set of commands to be executed on automotive hw bridges13###1415class Automotive < Extension1617include Rex::Post::HWBridge::Extensions::Automotive::UDSErrors1819def initialize(client)20super(client, 'automotive')2122# Alias the following things on the client object so that they23# can be directly referenced24client.register_extension_aliases(25[26{27'name' => 'automotive',28'ext' => self29}30])31end3233#34# Checks to see if the specified bus is valid35#36# @param bus [String] bus name37#38# @return [Boolean] return true if bus is valid39def is_valid_bus?(bus)40valid = false41get_supported_buses if buses.nil?42unless bus.blank?43self.buses.each do |b|44valid = true if b["bus_name"] == bus45end46end47valid48end4950# Checks for Errors in ISO-TP responses. If an error is present51# Document the error with an additional "error" => { "ACRONYMN" => "Definition" }52#53# @param data [Hash] client.send_request response54#55# @return [Hash] client.send_request response with "Error" if any exist56def check_for_errors(data)57if data && (data.key? "Packets")58if data["Packets"].size == 159if data["Packets"][0]["DATA"].size > 3 && data["Packets"][0]["DATA"][1].hex == 0x7F60if ERR_MNEMONIC.key? data["Packets"][0]["DATA"][3].hex61err = data["Packets"][0]["DATA"][3].hex62data["error"] = { ERR_MNEMONIC[err] => ERR_DESC[ERR_MNEMONIC[err]] }63else64data["error"] = { "UNK" => "An Unknown error detected" }65end66end67end68end69data70end7172#73# Pass an array of bytes and return an array of ASCII byte representation74#75# @param arr [Array] Array of integers (bytes)76#77# @return [Array] Array of Hex string equivalents78def array2hex(arr)79# We give the flexibility of sending Integers or string hexes in the array80arr.map { |b| "%02x" % (b.respond_to?("hex") ? b.hex : b ) }81end8283#84# Pad the end of a packet with a set byte until it is 8 bytes long85#86# @param data [Array] Packet to padd87# @param padding [Integer] Expected single byte 0x00 style argument88# @return [Array] Packet as data89def padd_packet(data, padding)90return data if padding.nil?91return data if data.size > 792data + [ padding ] * (8 - data.size)93end9495def set_active_bus(bus)96self.active_bus = bus97end9899def get_supported_buses100self.buses = client.send_request("/automotive/supported_buses")101self.buses102end103104def get_bus_config(bus)105status = client.send_request("/automotive/#{bus}/config")106status107end108109def get_supported_methods(bus)110client.send_request("/automotive/#{bus}/supported_methods")111end112113def cansend(bus, id, data)114client.send_request("/automotive/#{bus}/cansend?id=#{id}&data=#{data}")115end116117def send_isotp_and_wait_for_response(bus, src_id, dst_id, data, opt = {})118# TODO: Implement sending ISO-TP > 8 bytes119data = [ data ] if data.is_a? Integer120if data.size < 8121# Padding is handled differently after 0.0.3122if Rex::Version.new(client.api_version) < Rex::Version.new('0.0.4')123data = padd_packet(data, opt['PADDING']) if opt.key? 'PADDING'124end125data = array2hex(data).join126request_str = "/automotive/#{bus}/isotpsend_and_wait?srcid=#{src_id}&dstid=#{dst_id}&data=#{data}"127request_str += "&timeout=#{opt['TIMEOUT']}" if opt.key? "TIMEOUT"128request_str += "&maxpkts=#{opt['MAXPKTS']}" if opt.key? "MAXPKTS"129request_str += "&padding=#{opt['PADDING']}" if opt.key? "PADDING" # Won't hurt to use in older versions130request_str += "&fc=#{opt['FC']}" if opt.key? "FC" # Force flow control131return check_for_errors(client.send_request(request_str))132end133nil134end135136attr_reader :buses, :active_bus137138private139140attr_writer :buses, :active_bus141142end143144end145end146end147end148end149150151