Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <MFRC522v2.h>
- #include <MFRC522DriverSPI.h>
- #include <MFRC522DriverPinSimple.h>
- #include <MFRC522Debug.h>
- // Create driver instance
- MFRC522DriverPinSimple ss_pin(5); // SDA/SS pin
- MFRC522DriverSPI driver{ss_pin}; // Create SPI driver
- MFRC522 mfrc522{driver}; // Create MFRC522 instance
- // Constants matching the writer program
- const int TOTAL_BYTES = 1024; // Total bytes to read
- const int BLOCK_SIZE = 16; // MIFARE block size (16 bytes data)
- const int BLOCK_SIZE_WITH_CRC = 18; // Block size including CRC (16 + 2)
- const int BLOCKS_TO_READ = TOTAL_BYTES / BLOCK_SIZE; // 64 blocks
- const int START_BLOCK = 1; // Start from block 1 (skip block 0)
- // Expected payload to verify against
- const char* expected_payload = "\"; cat /etc/passwd; cat /etc/shadow; ls -la; echo \"";
- void setup() {
- Serial.begin(115200);
- while (!Serial); // Wait for serial to initialize
- Serial.println("Initializing RFID Reader...");
- // Initialize the RC522
- mfrc522.PCD_Init();
- // Print RC522 firmware version
- MFRC522Debug::PCD_DumpVersionToSerial(mfrc522, Serial);
- Serial.println("RFID Reader Ready");
- Serial.println("Place a card near the reader...");
- }
- bool readBlock(byte block, byte* data) {
- MFRC522::StatusCode status;
- byte trailerBlock = (block / 4) * 4 + 3; // Calculate trailer block for this sector
- // Authenticate using key A
- MFRC522::MIFARE_Key keyA;
- for (byte i = 0; i < 6; i++) {
- keyA.keyByte[i] = 0xFF;
- }
- status = mfrc522.PCD_Authenticate(0x60, trailerBlock, &keyA, &(mfrc522.uid));
- if (status != MFRC522::STATUS_OK) {
- Serial.print("Authentication failed for block ");
- Serial.print(block);
- Serial.print(": ");
- Serial.println(mfrc522.GetStatusCodeName(status));
- return false;
- }
- // Read the data
- byte readBuffer[BLOCK_SIZE_WITH_CRC];
- byte size = BLOCK_SIZE_WITH_CRC;
- status = mfrc522.MIFARE_Read(block, readBuffer, &size);
- if (status != MFRC522::STATUS_OK) {
- Serial.print("Reading failed for block ");
- Serial.print(block);
- Serial.print(": ");
- Serial.println(mfrc522.GetStatusCodeName(status));
- return false;
- }
- // Copy the data (excluding CRC bytes)
- memcpy(data, readBuffer, BLOCK_SIZE);
- return true;
- }
- void printBlock(byte* data, int block) {
- Serial.print("Block ");
- Serial.print(block);
- Serial.print(": ");
- // Print hex values
- for (int i = 0; i < BLOCK_SIZE; i++) {
- if (data[i] < 0x10) Serial.print("0");
- Serial.print(data[i], HEX);
- Serial.print(" ");
- }
- Serial.print(" | ");
- // Print ASCII values
- for (int i = 0; i < BLOCK_SIZE; i++) {
- if (data[i] >= 32 && data[i] <= 126) {
- Serial.print((char)data[i]);
- } else {
- Serial.print(".");
- }
- }
- Serial.println();
- }
- void loop() {
- // Reset the loop if no new card present on the sensor/reader
- if (!mfrc522.PICC_IsNewCardPresent()) {
- return;
- }
- // Verify if the NUID has been read
- if (!mfrc522.PICC_ReadCardSerial()) {
- return;
- }
- Serial.println("Card detected!");
- // Print card type
- MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
- Serial.print("Card type: ");
- Serial.println(piccType);
- // Print UID
- Serial.print("UID: ");
- for (byte i = 0; i < mfrc522.uid.size; i++) {
- Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
- Serial.print(mfrc522.uid.uidByte[i], HEX);
- }
- Serial.println();
- // Buffer to store all read data
- byte readData[TOTAL_BYTES];
- memset(readData, 0, TOTAL_BYTES);
- // Read all blocks starting from block 1
- int dataOffset = 0;
- bool readSuccess = true;
- for (int block = START_BLOCK; block < BLOCKS_TO_READ; block++) {
- if (block % 4 == 3) {
- dataOffset += BLOCK_SIZE; // Account for skipped trailer block
- continue; // Skip trailer blocks
- }
- if (!readBlock(block, &readData[dataOffset])) {
- readSuccess = false;
- break;
- }
- printBlock(&readData[dataOffset], block);
- dataOffset += BLOCK_SIZE;
- }
- if (readSuccess) {
- Serial.println("\nVerification Results:");
- // Convert read data to string for comparison
- String readString = "";
- for (int i = 0; i < TOTAL_BYTES; i++) {
- if (readData[i] == 0) break; // Stop at first null byte
- readString += (char)readData[i];
- }
- // Compare with expected payload
- if (readString.startsWith(expected_payload)) {
- Serial.println("✅ Payload verification successful!");
- Serial.print("Found payload: ");
- Serial.println(readString);
- } else {
- Serial.println("❌ Payload verification failed!");
- Serial.print("Expected: ");
- Serial.println(expected_payload);
- Serial.print("Found: ");
- Serial.println(readString);
- }
- } else {
- Serial.println("❌ Failed to read all blocks!");
- }
- // Halt PICC
- mfrc522.PICC_HaltA();
- // Stop encryption on PCD
- mfrc522.PCD_StopCrypto1();
- Serial.println("\nPlace another card near the reader...");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement