SlideShare a Scribd company logo
Customize Your Car
An Adventure in Using Elixir and Nerves to Hack Your
Vehicle's Electronics Network
Brien Wankel
@brienwankel@handsomeanchor
Bootleg
Simple deployment and server
automation for Elixir.
https://labzero.github.io/bootleg/
IT’S NOT MY FAULT
CAN bus
Controller Area Network (CAN) bus
01 Message-based multiplex protocol first release in 1986
02 Standard on most US cars manufactured since 1996,
CAN was made mandatory in 2008.
03 One of five protocols used in the OBD-II standard.
04 Messages about speed, brakes, gear, stereo, windows,
locks, horn, gps, lights, button states, etc…
CAN bus
Quick overview of the hardware side
01 Vehicle w/ CAN bus
03 CAN bus interface
04 Wiring
02 Raspberry Pi 3
Find a victim test subject
Find an interface (RPI3 w/ PICAN2)
Find your OBD-II port
Find your OBD-II port
Or find an alternative CAN wire location
Or find an alternative CAN wire location
Wire it all up
Wire it all up
Wire it all up
Quick overview of the software side
01 RPI3 with a CAN bus serial peripheral interface (SPI)
03 CAN communication library
04 Wifi w/ remote console and remote firmware push
02 nerves_system_rpi3_pican2
WARNING
Nerves Device Tree Overlays
Nerves Device Tree Overlays
# config/config.exs
config :nerves, :firmware,
fwup_conf: "config/fwup.conf"
# fwup.conf
file-resource mcp2515-can0.dtbo {
host-path =
"${NERVES_SYSTEM}/images/rpi-firmware/overlays
/mcp2515-can0.dtbo"
}
Nerves Device Tree Overlays
# fwup.conf
task complete{
# ... look for where `on-resource` directives are
already defined and add:
on-resource mcp2515-can0.dtbo {
fat_write(${BOOT_A_PART_OFFSET},
"overlays/mcp2515-can0.dtbo")
}
}
task upgrade.a{
# ...
on-resource mcp2515-can0.dtbo {
fat_write(${BOOT_A_PART_OFFSET},
"overlays/mcp2515-can0.dtbo")
}
}
# ... follow same pattern for upgrade.b
Loading the device driver
# fwup.conf
file-resource config.txt {
host-path = "${NERVES_APP}/config/config.txt"
}
# config.txt
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25
Reboot and check the logs
Interactive Elixir (1.5.3) - press Ctrl+C to exit (type h()
ENTER for help)
iex(1)> Nerves.Runtime.Shell.start
$ dmesg | grep mcp
[ 5.887757] mcp251x spi0.0 can0: MCP2515 successfully
initialized.
Bring up the can0 interface
System.cmd("ip",
~w{link set can0 up type can bitrate 125000}
iex(jolene@192.168.0.9)1> Nerves.Runtime.Shell.start
#PID<0.5075.0>
...
/srv/erlang[1]> cat /proc/net/dev
Inter-| Receive
face | bytes packets
wlan0: 22205600 121430
lo: 10409219 60935
eth0: 0 0
can0: 107198 16679
ng_can library
01 Requires socketcan, so it’s Linux only
02 Simple read and write capability
Connect to the bus
def open(pid, can_device, opts  []) do
GenServer.call(pid, {:open, can_device, opts})
end
def handle_call({:open, can_device, opts},
_from_, state) do
{:ok, socket} = Ng.Can.start_link
:ok = Ng.Can.open(socket, can_device, opts)
{:reply, {:ok, device: can_device},
%{state | socket: socket}}
end
Listen to messages
def handle_call({:listen}, _from_, %{socket: socket} =
state) do
:ok = Ng.Can.await_read(socket)
{:reply, :ok, %{state | socket: socket}}
end
def handle_info({:can_frames, _, frames}, state) do
IO.puts "GOT: #{inspect frames}"
{:noreply, state}
end
Write messages
<<id::size(32)>> = <<1,2,3,4>>
frame = {id, <<1,2,3,4,5,6,7,8>>}
Ng.Can.write(can_port, frame)
Elixir CANd library
01 Manages socketcand socket
03 Forwards messages from the CAN bus
02 Wraps the socketcand protocol
04 Helpers for sending messages back to the CAN bus
Connect to the bus
alias Cand.Protocol
{:ok, pid} = Protocol.connect(pid,
{192, 168, 0, 12},
28600)
{:ok, ‘can0’} = Protocol.open(pid, ‘can0’)
Connect to the bus
defmodule Cand.Socket do
...
def handle_call({:connect, host, port, opts},_from_,state) do
listener = Keyword.get(opts, :listener, Listener.Stdout)
{:ok, socket} = :gen_tcp.connect(host, port, opts)
{:reply, {:ok, host: host, port: port, opts: opts},
%{state | socket: socket, listener: listener}}
end
Listen to messages
defmodule MyCandApp.MyListener do
@behaviour Cand.Listener
use GenServer
def listen({:error, msg}) do
IO.puts "ERROR: #{msg}"
end
def listen("2EB " <> rest = msg) do
# handle this specific id
end
def listen(data), do: IO.puts(data)
# ...
end
Listen to messages
{:ok, lstnr} = MyCanApp.MyListener.start_link
Cand.Protocol.set_listener(lstnr)
# or send it as option to connect
{:ok, pid} = Cand.Protocol.connect(pid,
{192,168,0,12},
28600,
listener: lstnr)
Listen to messages
defmodule Cand.Socket do
...
def handle_info({:tcp, _, message},
%{listener: listener} = state) do
message
|> List.to_string
|> parse_messages
|> Enum.map(fn(message) ->
listener.listen message
end)
{:noreply, state}
end
Write messages
Protocol.send(pid, “295”, 8, “646E7470616E6963")
Protocol.send_integer(pid, “295”, 42)
Protocol.send_string(pid, “295”, “dntpanic”)
Tools / Resources
01 can-utils
03 Car Hacker’s Handbook
02 Kayak
03 https://github.com/brienw/cand
Brien Wankel
brien@labzero.com
@brienwankel@handsomeanchor

More Related Content

Similar to Customize Your Car: An Adventure in Using Elixir and Nerves to Hack Your Vehicle's Electronics Network

Linux router
Linux routerLinux router
DPDK & Layer 4 Packet Processing
DPDK & Layer 4 Packet ProcessingDPDK & Layer 4 Packet Processing
DPDK & Layer 4 Packet Processing
Michelle Holley
 
Tesla Hacking to FreedomEV
Tesla Hacking to FreedomEVTesla Hacking to FreedomEV
Tesla Hacking to FreedomEV
Jasper Nuyens
 
Intro to Ethernet
Intro to EthernetIntro to Ethernet
Intro to Ethernet
Belden Incorporated
 
20081114 Friday Food iLabt Bart Joris
20081114 Friday Food iLabt Bart Joris20081114 Friday Food iLabt Bart Joris
20081114 Friday Food iLabt Bart Joris
imec.archive
 
Hardwear.io 2018 BLE Security Essentials workshop
Hardwear.io 2018 BLE Security Essentials workshopHardwear.io 2018 BLE Security Essentials workshop
Hardwear.io 2018 BLE Security Essentials workshop
Slawomir Jasek
 
SR-IOV, KVM and Emulex OneConnect 10Gbps cards on Debian/Stable
SR-IOV, KVM and Emulex OneConnect 10Gbps cards on Debian/StableSR-IOV, KVM and Emulex OneConnect 10Gbps cards on Debian/Stable
SR-IOV, KVM and Emulex OneConnect 10Gbps cards on Debian/Stable
juet-y
 
L2 Attacks.pdf
L2 Attacks.pdfL2 Attacks.pdf
L2 Attacks.pdf
vinaykumar947680
 
Networking
NetworkingNetworking
Networking
Tarun Jaiswal
 
Practical steps to mitigate DDoS attacks
Practical steps to mitigate DDoS attacksPractical steps to mitigate DDoS attacks
Practical steps to mitigate DDoS attacks
Security Session
 
Otv notes
Otv notesOtv notes
Otv notes
Krunal Shah
 
How to Speak Intel DPDK KNI for Web Services.
How to Speak Intel DPDK KNI for Web Services.How to Speak Intel DPDK KNI for Web Services.
How to Speak Intel DPDK KNI for Web Services.
Naoto MATSUMOTO
 
IEFN - Ethernet module for LET-36 chassis
IEFN - Ethernet module for LET-36 chassisIEFN - Ethernet module for LET-36 chassis
IEFN - Ethernet module for LET-36 chassis
Ronald Bartels
 
Universal Reconfigurable Processing Platform For Space Rev Voice4
Universal Reconfigurable Processing Platform For Space Rev Voice4Universal Reconfigurable Processing Platform For Space Rev Voice4
Universal Reconfigurable Processing Platform For Space Rev Voice4
dseagrave
 
Wi Fi
Wi FiWi Fi
Controller Area Network(CAN)
Controller Area Network(CAN)Controller Area Network(CAN)
Controller Area Network(CAN)
Ashutosh Bhardwaj
 
SDN/OpenFlow #lspe
SDN/OpenFlow #lspeSDN/OpenFlow #lspe
SDN/OpenFlow #lspe
Chris Westin
 
managing your network environment
managing your network environmentmanaging your network environment
managing your network environment
scooby_doo
 
200-301-demo.pdf
200-301-demo.pdf200-301-demo.pdf
200-301-demo.pdf
CiscoExamDumpsarticl1
 
Cisco 200-301 Exam Dumps
Cisco 200-301 Exam DumpsCisco 200-301 Exam Dumps
Cisco 200-301 Exam Dumps
CiscoExamDumpsarticl2
 

Similar to Customize Your Car: An Adventure in Using Elixir and Nerves to Hack Your Vehicle's Electronics Network (20)

Linux router
Linux routerLinux router
Linux router
 
DPDK & Layer 4 Packet Processing
DPDK & Layer 4 Packet ProcessingDPDK & Layer 4 Packet Processing
DPDK & Layer 4 Packet Processing
 
Tesla Hacking to FreedomEV
Tesla Hacking to FreedomEVTesla Hacking to FreedomEV
Tesla Hacking to FreedomEV
 
Intro to Ethernet
Intro to EthernetIntro to Ethernet
Intro to Ethernet
 
20081114 Friday Food iLabt Bart Joris
20081114 Friday Food iLabt Bart Joris20081114 Friday Food iLabt Bart Joris
20081114 Friday Food iLabt Bart Joris
 
Hardwear.io 2018 BLE Security Essentials workshop
Hardwear.io 2018 BLE Security Essentials workshopHardwear.io 2018 BLE Security Essentials workshop
Hardwear.io 2018 BLE Security Essentials workshop
 
SR-IOV, KVM and Emulex OneConnect 10Gbps cards on Debian/Stable
SR-IOV, KVM and Emulex OneConnect 10Gbps cards on Debian/StableSR-IOV, KVM and Emulex OneConnect 10Gbps cards on Debian/Stable
SR-IOV, KVM and Emulex OneConnect 10Gbps cards on Debian/Stable
 
L2 Attacks.pdf
L2 Attacks.pdfL2 Attacks.pdf
L2 Attacks.pdf
 
Networking
NetworkingNetworking
Networking
 
Practical steps to mitigate DDoS attacks
Practical steps to mitigate DDoS attacksPractical steps to mitigate DDoS attacks
Practical steps to mitigate DDoS attacks
 
Otv notes
Otv notesOtv notes
Otv notes
 
How to Speak Intel DPDK KNI for Web Services.
How to Speak Intel DPDK KNI for Web Services.How to Speak Intel DPDK KNI for Web Services.
How to Speak Intel DPDK KNI for Web Services.
 
IEFN - Ethernet module for LET-36 chassis
IEFN - Ethernet module for LET-36 chassisIEFN - Ethernet module for LET-36 chassis
IEFN - Ethernet module for LET-36 chassis
 
Universal Reconfigurable Processing Platform For Space Rev Voice4
Universal Reconfigurable Processing Platform For Space Rev Voice4Universal Reconfigurable Processing Platform For Space Rev Voice4
Universal Reconfigurable Processing Platform For Space Rev Voice4
 
Wi Fi
Wi FiWi Fi
Wi Fi
 
Controller Area Network(CAN)
Controller Area Network(CAN)Controller Area Network(CAN)
Controller Area Network(CAN)
 
SDN/OpenFlow #lspe
SDN/OpenFlow #lspeSDN/OpenFlow #lspe
SDN/OpenFlow #lspe
 
managing your network environment
managing your network environmentmanaging your network environment
managing your network environment
 
200-301-demo.pdf
200-301-demo.pdf200-301-demo.pdf
200-301-demo.pdf
 
Cisco 200-301 Exam Dumps
Cisco 200-301 Exam DumpsCisco 200-301 Exam Dumps
Cisco 200-301 Exam Dumps
 

Recently uploaded

How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
safelyiotech
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Paul Brebner
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
What’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete RoadmapWhat’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete Roadmap
Envertis Software Solutions
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
VALiNTRY360
 
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabhQuarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
aisafed42
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
ISH Technologies
 
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptxMigration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
ervikas4
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
sjcobrien
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
Maitrey Patel
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 

Recently uploaded (20)

How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
What’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete RoadmapWhat’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete Roadmap
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
 
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabhQuarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
 
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptxMigration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 

Customize Your Car: An Adventure in Using Elixir and Nerves to Hack Your Vehicle's Electronics Network

  • 1. Customize Your Car An Adventure in Using Elixir and Nerves to Hack Your Vehicle's Electronics Network Brien Wankel @brienwankel@handsomeanchor
  • 2. Bootleg Simple deployment and server automation for Elixir. https://labzero.github.io/bootleg/
  • 3.
  • 5.
  • 7. Controller Area Network (CAN) bus 01 Message-based multiplex protocol first release in 1986 02 Standard on most US cars manufactured since 1996, CAN was made mandatory in 2008. 03 One of five protocols used in the OBD-II standard. 04 Messages about speed, brakes, gear, stereo, windows, locks, horn, gps, lights, button states, etc…
  • 9. Quick overview of the hardware side 01 Vehicle w/ CAN bus 03 CAN bus interface 04 Wiring 02 Raspberry Pi 3
  • 10. Find a victim test subject
  • 11. Find an interface (RPI3 w/ PICAN2)
  • 14. Or find an alternative CAN wire location
  • 15. Or find an alternative CAN wire location
  • 19. Quick overview of the software side 01 RPI3 with a CAN bus serial peripheral interface (SPI) 03 CAN communication library 04 Wifi w/ remote console and remote firmware push 02 nerves_system_rpi3_pican2
  • 21. Nerves Device Tree Overlays
  • 22. Nerves Device Tree Overlays # config/config.exs config :nerves, :firmware, fwup_conf: "config/fwup.conf" # fwup.conf file-resource mcp2515-can0.dtbo { host-path = "${NERVES_SYSTEM}/images/rpi-firmware/overlays /mcp2515-can0.dtbo" }
  • 23. Nerves Device Tree Overlays # fwup.conf task complete{ # ... look for where `on-resource` directives are already defined and add: on-resource mcp2515-can0.dtbo { fat_write(${BOOT_A_PART_OFFSET}, "overlays/mcp2515-can0.dtbo") } } task upgrade.a{ # ... on-resource mcp2515-can0.dtbo { fat_write(${BOOT_A_PART_OFFSET}, "overlays/mcp2515-can0.dtbo") } } # ... follow same pattern for upgrade.b
  • 24. Loading the device driver # fwup.conf file-resource config.txt { host-path = "${NERVES_APP}/config/config.txt" } # config.txt dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25
  • 25. Reboot and check the logs Interactive Elixir (1.5.3) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> Nerves.Runtime.Shell.start $ dmesg | grep mcp [ 5.887757] mcp251x spi0.0 can0: MCP2515 successfully initialized.
  • 26. Bring up the can0 interface System.cmd("ip", ~w{link set can0 up type can bitrate 125000} iex(jolene@192.168.0.9)1> Nerves.Runtime.Shell.start #PID<0.5075.0> ... /srv/erlang[1]> cat /proc/net/dev Inter-| Receive face | bytes packets wlan0: 22205600 121430 lo: 10409219 60935 eth0: 0 0 can0: 107198 16679
  • 27. ng_can library 01 Requires socketcan, so it’s Linux only 02 Simple read and write capability
  • 28. Connect to the bus def open(pid, can_device, opts []) do GenServer.call(pid, {:open, can_device, opts}) end def handle_call({:open, can_device, opts}, _from_, state) do {:ok, socket} = Ng.Can.start_link :ok = Ng.Can.open(socket, can_device, opts) {:reply, {:ok, device: can_device}, %{state | socket: socket}} end
  • 29. Listen to messages def handle_call({:listen}, _from_, %{socket: socket} = state) do :ok = Ng.Can.await_read(socket) {:reply, :ok, %{state | socket: socket}} end def handle_info({:can_frames, _, frames}, state) do IO.puts "GOT: #{inspect frames}" {:noreply, state} end
  • 30. Write messages <<id::size(32)>> = <<1,2,3,4>> frame = {id, <<1,2,3,4,5,6,7,8>>} Ng.Can.write(can_port, frame)
  • 31. Elixir CANd library 01 Manages socketcand socket 03 Forwards messages from the CAN bus 02 Wraps the socketcand protocol 04 Helpers for sending messages back to the CAN bus
  • 32. Connect to the bus alias Cand.Protocol {:ok, pid} = Protocol.connect(pid, {192, 168, 0, 12}, 28600) {:ok, ‘can0’} = Protocol.open(pid, ‘can0’)
  • 33. Connect to the bus defmodule Cand.Socket do ... def handle_call({:connect, host, port, opts},_from_,state) do listener = Keyword.get(opts, :listener, Listener.Stdout) {:ok, socket} = :gen_tcp.connect(host, port, opts) {:reply, {:ok, host: host, port: port, opts: opts}, %{state | socket: socket, listener: listener}} end
  • 34. Listen to messages defmodule MyCandApp.MyListener do @behaviour Cand.Listener use GenServer def listen({:error, msg}) do IO.puts "ERROR: #{msg}" end def listen("2EB " <> rest = msg) do # handle this specific id end def listen(data), do: IO.puts(data) # ... end
  • 35. Listen to messages {:ok, lstnr} = MyCanApp.MyListener.start_link Cand.Protocol.set_listener(lstnr) # or send it as option to connect {:ok, pid} = Cand.Protocol.connect(pid, {192,168,0,12}, 28600, listener: lstnr)
  • 36. Listen to messages defmodule Cand.Socket do ... def handle_info({:tcp, _, message}, %{listener: listener} = state) do message |> List.to_string |> parse_messages |> Enum.map(fn(message) -> listener.listen message end) {:noreply, state} end
  • 37.
  • 38. Write messages Protocol.send(pid, “295”, 8, “646E7470616E6963") Protocol.send_integer(pid, “295”, 42) Protocol.send_string(pid, “295”, “dntpanic”)
  • 39.
  • 40. Tools / Resources 01 can-utils 03 Car Hacker’s Handbook 02 Kayak 03 https://github.com/brienw/cand