Is it possible to use the Arduino Uno with two MCP2515's for a man-in-the-middle attack? I am trying to modify codes sent from a can module to the ecu to trick the ecu into thinking the module has no errors. I am able to read the messages coming off of either module, but I can't seem to get it to find when the line isn't busy to send the can message out. Any ideas?
I have the 120 ohm resistor connected on both modules by the way.
#include <SPI.h>
#include <mcp_can.h>
// Pins configuration
const int CAN0_CS = 10; // Chip Select pin for CAN0
const int CAN1_CS = 9; // Chip Select pin for CAN1
const int CAN0_INT = 2; // Interrupt pin for CAN0
const int CAN1_INT = 3; // Interrupt pin for CAN1
MCP_CAN CAN0(CAN0_CS); // Initialize MCP2515 on CAN0
MCP_CAN CAN1(CAN1_CS); // Initialize MCP2515 on CAN1
void setup() {
Serial.begin(115200);
while (!Serial); // Wait for Serial to be ready
// Initialize MCP2515 modules
if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) != CAN_OK) {
Serial.println("CAN0 Init Failed");
while (1);
}
Serial.println("CAN0 Init OK!");
if (CAN1.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) != CAN_OK) {
Serial.println("CAN1 Init Failed");
while (1);
}
Serial.println("CAN1 Init OK!");
pinMode(CAN0_INT, INPUT); // Set interrupt pins
pinMode(CAN1_INT, INPUT);
attachInterrupt(digitalPinToInterrupt(CAN0_INT), handleCAN0Interrupt, FALLING);
attachInterrupt(digitalPinToInterrupt(CAN1_INT), handleCAN1Interrupt, FALLING);
CAN0.setMode(MCP_NORMAL); // Set mode to normal
CAN1.setMode(MCP_NORMAL);
}
void loop() {
// Empty loop; everything is handled by interrupts
}
void handleCAN0Interrupt() {
unsigned long id = 0;
byte len = 0;
byte buf[8];
if (CAN0.readMsgBuf(&id, &len, buf) == CAN_OK) {
if (id == 0x500 && buf[0] == 94) {
buf[0] = 50; // Change data as specified
}
CAN1.sendMsgBuf(id, 0, len, buf); // Send modified message
// clearBuffer(rxBuf, len);
}
}
void handleCAN1Interrupt() {
unsigned long id = 0;
byte len = 0;
byte buf[8];
if (CAN1.readMsgBuf(&id, &len, buf) == CAN_OK) {
CAN0.sendMsgBuf(id, 0, len, buf); // Forward message to CAN0
}
}