Advertisement
mehularora810

rfid ver

Apr 9th, 2025
383
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. // Constants matching the writer program
  12. const int TOTAL_BYTES = 1024;      // Total bytes to read
  13. const int BLOCK_SIZE = 16;         // MIFARE block size (16 bytes data)
  14. const int BLOCK_SIZE_WITH_CRC = 18; // Block size including CRC (16 + 2)
  15. const int BLOCKS_TO_READ = TOTAL_BYTES / BLOCK_SIZE; // 64 blocks
  16. const int START_BLOCK = 1;         // Start from block 1 (skip block 0)
  17.  
  18. // Expected payload to verify against
  19. const char* expected_payload = "\"; cat /etc/passwd; cat /etc/shadow; ls -la; echo \"";
  20.  
  21. void setup() {
  22.   Serial.begin(115200);
  23.   while (!Serial); // Wait for serial to initialize
  24.  
  25.   Serial.println("Initializing RFID Reader...");
  26.  
  27.   // Initialize the RC522
  28.   mfrc522.PCD_Init();
  29.  
  30.   // Print RC522 firmware version
  31.   MFRC522Debug::PCD_DumpVersionToSerial(mfrc522, Serial);
  32.  
  33.   Serial.println("RFID Reader Ready");
  34.   Serial.println("Place a card near the reader...");
  35. }
  36.  
  37. bool readBlock(byte block, byte* data) {
  38.   MFRC522::StatusCode status;
  39.   byte trailerBlock = (block / 4) * 4 + 3; // Calculate trailer block for this sector
  40.  
  41.   // Authenticate using key A
  42.   MFRC522::MIFARE_Key keyA;
  43.   for (byte i = 0; i < 6; i++) {
  44.     keyA.keyByte[i] = 0xFF;
  45.   }
  46.  
  47.   status = mfrc522.PCD_Authenticate(0x60, trailerBlock, &keyA, &(mfrc522.uid));
  48.   if (status != MFRC522::STATUS_OK) {
  49.     Serial.print("Authentication failed for block ");
  50.     Serial.print(block);
  51.     Serial.print(": ");
  52.     Serial.println(mfrc522.GetStatusCodeName(status));
  53.     return false;
  54.   }
  55.  
  56.   // Read the data
  57.   byte readBuffer[BLOCK_SIZE_WITH_CRC];
  58.   byte size = BLOCK_SIZE_WITH_CRC;
  59.   status = mfrc522.MIFARE_Read(block, readBuffer, &size);
  60.   if (status != MFRC522::STATUS_OK) {
  61.     Serial.print("Reading failed for block ");
  62.     Serial.print(block);
  63.     Serial.print(": ");
  64.     Serial.println(mfrc522.GetStatusCodeName(status));
  65.     return false;
  66.   }
  67.  
  68.   // Copy the data (excluding CRC bytes)
  69.   memcpy(data, readBuffer, BLOCK_SIZE);
  70.   return true;
  71. }
  72.  
  73. void printBlock(byte* data, int block) {
  74.   Serial.print("Block ");
  75.   Serial.print(block);
  76.   Serial.print(": ");
  77.  
  78.   // Print hex values
  79.   for (int i = 0; i < BLOCK_SIZE; i++) {
  80.     if (data[i] < 0x10) Serial.print("0");
  81.     Serial.print(data[i], HEX);
  82.     Serial.print(" ");
  83.   }
  84.  
  85.   Serial.print(" | ");
  86.  
  87.   // Print ASCII values
  88.   for (int i = 0; i < BLOCK_SIZE; i++) {
  89.     if (data[i] >= 32 && data[i] <= 126) {
  90.       Serial.print((char)data[i]);
  91.     } else {
  92.       Serial.print(".");
  93.     }
  94.   }
  95.  
  96.   Serial.println();
  97. }
  98.  
  99. void loop() {
  100.   // Reset the loop if no new card present on the sensor/reader
  101.   if (!mfrc522.PICC_IsNewCardPresent()) {
  102.     return;
  103.   }
  104.  
  105.   // Verify if the NUID has been read
  106.   if (!mfrc522.PICC_ReadCardSerial()) {
  107.     return;
  108.   }
  109.  
  110.   Serial.println("Card detected!");
  111.  
  112.   // Print card type
  113.   MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  114.   Serial.print("Card type: ");
  115.   Serial.println(piccType);
  116.  
  117.   // Print UID
  118.   Serial.print("UID: ");
  119.   for (byte i = 0; i < mfrc522.uid.size; i++) {
  120.     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  121.     Serial.print(mfrc522.uid.uidByte[i], HEX);
  122.   }
  123.   Serial.println();
  124.  
  125.   // Buffer to store all read data
  126.   byte readData[TOTAL_BYTES];
  127.   memset(readData, 0, TOTAL_BYTES);
  128.  
  129.   // Read all blocks starting from block 1
  130.   int dataOffset = 0;
  131.   bool readSuccess = true;
  132.  
  133.   for (int block = START_BLOCK; block < BLOCKS_TO_READ; block++) {
  134.     if (block % 4 == 3) {
  135.       dataOffset += BLOCK_SIZE; // Account for skipped trailer block
  136.       continue; // Skip trailer blocks
  137.     }
  138.    
  139.     if (!readBlock(block, &readData[dataOffset])) {
  140.       readSuccess = false;
  141.       break;
  142.     }
  143.    
  144.     printBlock(&readData[dataOffset], block);
  145.     dataOffset += BLOCK_SIZE;
  146.   }
  147.  
  148.   if (readSuccess) {
  149.     Serial.println("\nVerification Results:");
  150.    
  151.     // Convert read data to string for comparison
  152.     String readString = "";
  153.     for (int i = 0; i < TOTAL_BYTES; i++) {
  154.       if (readData[i] == 0) break; // Stop at first null byte
  155.       readString += (char)readData[i];
  156.     }
  157.    
  158.     // Compare with expected payload
  159.     if (readString.startsWith(expected_payload)) {
  160.       Serial.println("✅ Payload verification successful!");
  161.       Serial.print("Found payload: ");
  162.       Serial.println(readString);
  163.     } else {
  164.       Serial.println("❌ Payload verification failed!");
  165.       Serial.print("Expected: ");
  166.       Serial.println(expected_payload);
  167.       Serial.print("Found: ");
  168.       Serial.println(readString);
  169.     }
  170.   } else {
  171.     Serial.println("❌ Failed to read all blocks!");
  172.   }
  173.  
  174.   // Halt PICC
  175.   mfrc522.PICC_HaltA();
  176.   // Stop encryption on PCD
  177.   mfrc522.PCD_StopCrypto1();
  178.  
  179.   Serial.println("\nPlace another card near the reader...");
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement