Advertisement
michalmonday

RFID cloner

Mar 29th, 2017
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.49 KB | None | 0 0
  1. /*
  2.  * Copy the RFID card data into variables and then
  3.  * scan the second empty card to copy all the date
  4.  * ----------------------------------------------------------------------------
  5.  * Example sketch/program which will try the most used default keys listed in
  6.  * https://code.google.com/p/mfcuk/wiki/MifareClassicDefaultKeys to dump the
  7.  * block 0 of a MIFARE RFID card using a RFID-RC522 reader.
  8.  *
  9.  * Typical pin layout used:
  10.  * -----------------------------------------------------------------------------------------
  11.  *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
  12.  *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
  13.  * Signal      Pin          Pin           Pin       Pin        Pin              Pin
  14.  * -----------------------------------------------------------------------------------------
  15.  * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
  16.  * SPI SS      SDA(SS)      10            53        D10        10               10
  17.  * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
  18.  * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
  19.  * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
  20.  *
  21.  */
  22.  
  23. #define BTN_PIN 9
  24. #define LED_YELLOW_PIN 4
  25. #define LED_GREEN_PIN 5
  26.  
  27.  
  28. byte workState;
  29.  
  30. byte uidBuff[4];
  31.  
  32. #include <SPI.h>
  33. #include <MFRC522.h>
  34.  
  35. #define RST_PIN         8           // Configurable, see typical pin layout above
  36. #define SS_PIN          10          // Configurable, see typical pin layout above
  37.  
  38. MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
  39.  
  40. byte buffer[18];
  41. byte block;
  42. byte waarde[64][16];
  43. MFRC522::StatusCode status;
  44.    
  45. MFRC522::MIFARE_Key key;
  46.  
  47. // Number of known default keys (hard-coded)
  48. // NOTE: Synchronize the NR_KNOWN_KEYS define with the defaultKeys[] array
  49. #define NR_KNOWN_KEYS   8
  50. // Known keys, see: https://code.google.com/p/mfcuk/wiki/MifareClassicDefaultKeys
  51. byte knownKeys[NR_KNOWN_KEYS][MFRC522::MF_KEY_SIZE] =  {
  52.     {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // FF FF FF FF FF FF = factory default
  53.     {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}, // A0 A1 A2 A3 A4 A5
  54.     {0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5}, // B0 B1 B2 B3 B4 B5
  55.     {0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd}, // 4D 3A 99 C3 51 DD
  56.     {0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a}, // 1A 98 2C 7E 45 9A
  57.     {0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}, // D3 F7 D3 F7 D3 F7
  58.     {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, // AA BB CC DD EE FF
  59.     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}  // 00 00 00 00 00 00
  60. };
  61.  
  62. char choice;
  63. /*
  64.  * Initialize.
  65.  */
  66. void setup() {
  67.     pinMode(BTN_PIN, INPUT_PULLUP);
  68.    
  69.     //Serial.begin(9600);         // Initialize serial communications with the PC
  70.     //while (!Serial);            // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  71.    
  72.     SPI.begin();                // Init SPI bus
  73.     mfrc522.PCD_Init();         // Init MFRC522 card
  74.     //Serial.println(F("Try the most used default keys to print block 0 to 63 of a MIFARE PICC."));
  75.     ////Serial.println("1.Read card \n2.Write to card \n3.Copy the data.");
  76.  
  77.     for (byte i = 0; i < 6; i++) {
  78.         key.keyByte[i] = 0xFF;
  79.     }
  80. }
  81.  
  82.  
  83.  
  84.  //Via seriele monitor de bytes uitlezen in hexadecimaal
  85.  
  86. void dump_byte_array(byte *buffer, byte bufferSize) {
  87.     for (byte i = 0; i < bufferSize; i++) {
  88.         //Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  89.         //Serial.print(buffer[i], HEX);
  90.     }
  91. }
  92. //Via seriele monitor de bytes uitlezen in ASCI
  93.  
  94. void dump_byte_array1(byte *buffer, byte bufferSize) {
  95.   for (byte i = 0; i < bufferSize; i++) {
  96.     //Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  97.     //Serial.write(buffer[i]);
  98.   }
  99. }
  100.  
  101. /*
  102.  * Try using the PICC (the tag/card) with the given key to access block 0 to 63.
  103.  * On success, it will show the key details, and dump the block data on Serial.
  104.  *
  105.  * @return true when the given key worked, false otherwise.
  106.  */
  107.  
  108. boolean try_key(MFRC522::MIFARE_Key *key)
  109. {
  110.     boolean result = false;
  111.    
  112.     for(byte block = 0; block < 64; block++){
  113.      
  114.     // Serial.println(F("Authenticating using key A..."));
  115.     status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, key, &(mfrc522.uid));
  116.     if (status != MFRC522::STATUS_OK) {
  117.         ////Serial.print(F("PCD_Authenticate() failed: "));
  118.         //Serial.println(mfrc522.GetStatusCodeName(status));
  119.         return false;
  120.     }
  121.  
  122.     // Read block
  123.     byte byteCount = sizeof(buffer);
  124.     status = mfrc522.MIFARE_Read(block, buffer, &byteCount);
  125.     if (status != MFRC522::STATUS_OK) {
  126.         //Serial.print(F("MIFARE_Read() failed: "));
  127.         //Serial.println(mfrc522.GetStatusCodeName(status));
  128.     }
  129.     else {
  130.         // Successful read
  131.         result = true;
  132.         //Serial.print(F("Success with key:"));
  133.         dump_byte_array((*key).keyByte, MFRC522::MF_KEY_SIZE);
  134.         //Serial.println();
  135.        
  136.         // Dump block data
  137.         //Serial.print(F("Block ")); Serial.print(block); Serial.print(F(":"));
  138.         dump_byte_array1(buffer, 16); //omzetten van hex naar ASCI
  139.         ////Serial.println();
  140.        
  141.         for (int p = 0; p < 16; p++) //De 16 bits uit de block uitlezen
  142.         {
  143.           waarde [block][p] = buffer[p];
  144.           //Serial.print(waarde[block][p]);
  145.           //Serial.print(" ");
  146.         }
  147.        
  148.         }
  149.     }
  150.     //Serial.println();
  151.    
  152.     //Serial.println("1.Read card \n2.Write to card \n3.Copy the data.");
  153.  
  154.     mfrc522.PICC_HaltA();       // Halt PICC
  155.     mfrc522.PCD_StopCrypto1();  // Stop encryption on PCD
  156.     return result;
  157.    
  158.     start();
  159. }
  160.  
  161. /*
  162.  * Main loop.
  163.  */
  164. void loop() {
  165.   start();
  166.    
  167. }
  168.  
  169. void start(){
  170.   //choice = Serial.read();
  171.  
  172.  
  173.   ///////////////
  174.   if(digitalRead(BTN_PIN) == LOW)
  175.   {
  176.     workState++;
  177.     delay(700);
  178.   }
  179.   ///////////////
  180.  
  181.   if(workState == 1)
  182.   {
  183.     digitalWrite(LED_YELLOW_PIN, HIGH);
  184.     digitalWrite(LED_GREEN_PIN, LOW);
  185.    
  186.     ////Serial.println("Read the card");
  187.     keuze1();
  188.          
  189.   }
  190.   else if(workState == 3)
  191.   {
  192.     //Serial.println("Copying the data on to the new card");    
  193.  
  194.     digitalWrite(LED_YELLOW_PIN, HIGH);
  195.     digitalWrite(LED_GREEN_PIN, HIGH);
  196.     keuze3();
  197.    
  198.   }
  199.   else
  200.   {
  201.     digitalWrite(LED_YELLOW_PIN, LOW);
  202.     digitalWrite(LED_GREEN_PIN, LOW);
  203.   }
  204. }
  205.  
  206. void keuze2(){ //Test waardes in blokken
  207.  
  208.   for(block = 4; block <= 62; block++){
  209.     if(block == 7 || block == 11 || block == 15 || block == 19 || block == 23 || block == 27 || block == 31 || block == 35 || block == 39 || block == 43 || block == 47 || block == 51 || block == 55 || block == 59){
  210.       block ++;
  211.     }
  212.  
  213.   //Serial.print(F("Writing data into block "));
  214.   //Serial.print(block);
  215.   //Serial.println("\n");
  216.  
  217.     for(int j = 0; j < 16; j++){
  218.       //Serial.print(waarde[block][j]);
  219.       //Serial.print(" ");
  220.     }
  221.     //Serial.println("\n");
  222.    
  223.   }
  224.  
  225.   //Serial.println("1.Read card \n2.Write to card \n3.Copy the data.");
  226.   start();
  227. }
  228.  
  229. void keuze3(){ //Copy the data in the new card
  230. //Serial.println("Insert new card...");
  231.   // Look for new cards
  232.     while ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial())
  233.     {
  234.       delay(50);
  235.       if(digitalRead(BTN_PIN) == LOW)
  236.       {
  237.         workState = 2;
  238.         delay(700);
  239.         return;
  240.       }    
  241.     }
  242.  
  243.      
  244.    
  245.  
  246.  
  247.  
  248.    
  249.    
  250.     // Show some details of the PICC (that is: the tag/card)
  251.     //Serial.print(F("Card UID:"));
  252.     dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  253.     //Serial.println();
  254.     //Serial.print(F("PICC type: "));
  255.     MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  256.     //Serial.println(mfrc522.PICC_GetTypeName(piccType));
  257.    
  258.     // Try the known default keys
  259.     /*MFRC522::MIFARE_Key key;
  260.     for (byte k = 0; k < NR_KNOWN_KEYS; k++) {
  261.         // Copy the known key into the MIFARE_Key structure
  262.         for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) {
  263.             key.keyByte[i] = knownKeys[k][i];
  264.         }
  265.     }*/
  266.     for (byte i = 0; i < 6; i++) {
  267.         key.keyByte[i] = 0xFF;
  268.     }
  269.  
  270.   for(int i = 4; i <= 62; i++){ //De blocken 4 tot 62 kopieren, behalve al deze onderstaande blocken (omdat deze de authenticatie blokken zijn)
  271.     if(i == 7 || i == 11 || i == 15 || i == 19 || i == 23 || i == 27 || i == 31 || i == 35 || i == 39 || i == 43 || i == 47 || i == 51 || i == 55 || i == 59){
  272.       i++;
  273.     }
  274.     block = i;
  275.    
  276.       // Authenticate using key A
  277.     //Serial.println(F("Authenticating using key A..."));
  278.     status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
  279.     if (status != MFRC522::STATUS_OK) {
  280.         //Serial.print(F("PCD_Authenticate() failed: "));
  281.         //Serial.println(mfrc522.GetStatusCodeName(status));
  282.         return;
  283.     }
  284.    
  285.     // Authenticate using key B
  286.     //Serial.println(F("Authenticating again using key B..."));
  287.     status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, block, &key, &(mfrc522.uid));
  288.     if (status != MFRC522::STATUS_OK) {
  289.         //Serial.print(F("PCD_Authenticate() failed: "));
  290.         //Serial.println(mfrc522.GetStatusCodeName(status));
  291.         return;
  292.     }
  293.    
  294.     // Write data to the block
  295.     //Serial.print(F("Writing data into block "));
  296.     //Serial.print(block);
  297.     //Serial.println("\n");
  298.          
  299.     dump_byte_array(waarde[block], 16);
  300.    
  301.          
  302.      status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(block, waarde[block], 16);
  303.       if (status != MFRC522::STATUS_OK) {
  304.         //Serial.print(F("MIFARE_Write() failed: "));
  305.         //Serial.println(mfrc522.GetStatusCodeName(status));
  306.       }
  307.    
  308.        
  309.      //Serial.println("\n");
  310.      
  311.   }
  312.  
  313.   RewriteUID();
  314.  
  315.   mfrc522.PICC_HaltA();       // Halt PICC
  316.   mfrc522.PCD_StopCrypto1();  // Stop encryption on PCD
  317.    
  318.   //Serial.println("1.Read card \n2.Write to card \n3.Copy the data.");
  319.  
  320.   workState = 0;
  321.   //start();
  322. }
  323.  
  324. void keuze1(){ //Read card
  325.   //Serial.println("Insert card...");
  326.   // Look for new cards
  327.     while ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial())
  328.     {
  329.       delay(50);
  330.       if(digitalRead(BTN_PIN) == LOW)
  331.       {
  332.         workState = 0;
  333.         delay(700);
  334.         return;
  335.       }    
  336.     }
  337.  
  338.     SaveUID();
  339.  
  340.     // Show some details of the PICC (that is: the tag/card)
  341.     //Serial.print(F("Card UID:"));
  342.     dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  343.     //Serial.println();
  344.     //Serial.print(F("PICC type: "));
  345.     MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  346.     //Serial.println(mfrc522.PICC_GetTypeName(piccType));
  347.    
  348.     // Try the known default keys
  349.     MFRC522::MIFARE_Key key;
  350.     for (byte k = 0; k < NR_KNOWN_KEYS; k++) {
  351.         // Copy the known key into the MIFARE_Key structure
  352.         for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) {
  353.             key.keyByte[i] = knownKeys[k][i];
  354.         }
  355.         // Try the key
  356.         if (try_key(&key)) {
  357.             // Found and reported on the key and block,
  358.             // no need to try other keys for this PICC
  359.             break;
  360.         }
  361.     }
  362.  
  363.   workState++;
  364. }
  365.  
  366. void SaveUID()
  367. {
  368.   for(byte i=0; i<mfrc522.uid.size; i++)
  369.   {
  370.     uidBuff[i] = mfrc522.uid.uidByte[i];
  371.   }
  372. }
  373.  
  374. void RewriteUID()
  375. {  
  376.   if ( mfrc522.MIFARE_SetUid(uidBuff, (byte)4, true) ) {
  377.     //Serial.println(F("Wrote new UID to card."));
  378.   }
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement