RFID-Cloner.ino 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copy the RFID card data into variables and then
  3. * scan the second empty card to copy all the data
  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. * More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
  22. *
  23. */
  24. #include <SPI.h>
  25. #include <MFRC522.h>
  26. #define RST_PIN 9 // Configurable, see typical pin layout above
  27. #define SS_PIN 10 // Configurable, see typical pin layout above
  28. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
  29. byte buffer[18];
  30. byte block;
  31. byte waarde[64][16];
  32. MFRC522::StatusCode status;
  33. MFRC522::MIFARE_Key key;
  34. // Number of known default keys (hard-coded)
  35. // NOTE: Synchronize the NR_KNOWN_KEYS define with the defaultKeys[] array
  36. #define NR_KNOWN_KEYS 8
  37. // Known keys, see: https://code.google.com/p/mfcuk/wiki/MifareClassicDefaultKeys
  38. byte knownKeys[NR_KNOWN_KEYS][MFRC522::MF_KEY_SIZE] = {
  39. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // FF FF FF FF FF FF = factory default
  40. {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}, // A0 A1 A2 A3 A4 A5
  41. {0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5}, // B0 B1 B2 B3 B4 B5
  42. {0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd}, // 4D 3A 99 C3 51 DD
  43. {0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a}, // 1A 98 2C 7E 45 9A
  44. {0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}, // D3 F7 D3 F7 D3 F7
  45. {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, // AA BB CC DD EE FF
  46. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 00 00 00 00 00 00
  47. };
  48. char choice;
  49. /*
  50. * Initialize.
  51. */
  52. void setup() {
  53. Serial.begin(9600); // Initialize serial communications with the PC
  54. while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  55. SPI.begin(); // Init SPI bus
  56. mfrc522.PCD_Init(); // Init MFRC522 card
  57. Serial.println(F("Try the most used default keys to print block 0 to 63 of a MIFARE PICC."));
  58. Serial.println("1.Read card \n2.Write to card \n3.Copy the data.");
  59. for (byte i = 0; i < 6; i++) {
  60. key.keyByte[i] = 0xFF;
  61. }
  62. }
  63. //Via seriele monitor de bytes uitlezen in hexadecimaal
  64. void dump_byte_array(byte *buffer, byte bufferSize) {
  65. for (byte i = 0; i < bufferSize; i++) {
  66. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  67. Serial.print(buffer[i], HEX);
  68. }
  69. }
  70. //Via seriele monitor de bytes uitlezen in ASCI
  71. void dump_byte_array1(byte *buffer, byte bufferSize) {
  72. for (byte i = 0; i < bufferSize; i++) {
  73. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  74. Serial.write(buffer[i]);
  75. }
  76. }
  77. /*
  78. * Try using the PICC (the tag/card) with the given key to access block 0 to 63.
  79. * On success, it will show the key details, and dump the block data on Serial.
  80. *
  81. * @return true when the given key worked, false otherwise.
  82. */
  83. bool try_key(MFRC522::MIFARE_Key *key)
  84. {
  85. bool result = false;
  86. for(byte block = 0; block < 64; block++){
  87. // Serial.println(F("Authenticating using key A..."));
  88. status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, key, &(mfrc522.uid));
  89. if (status != MFRC522::STATUS_OK) {
  90. Serial.print(F("PCD_Authenticate() failed: "));
  91. Serial.println(mfrc522.GetStatusCodeName(status));
  92. return false;
  93. }
  94. // Read block
  95. byte byteCount = sizeof(buffer);
  96. status = mfrc522.MIFARE_Read(block, buffer, &byteCount);
  97. if (status != MFRC522::STATUS_OK) {
  98. Serial.print(F("MIFARE_Read() failed: "));
  99. Serial.println(mfrc522.GetStatusCodeName(status));
  100. }
  101. else {
  102. // Successful read
  103. result = true;
  104. Serial.print(F("Success with key:"));
  105. dump_byte_array((*key).keyByte, MFRC522::MF_KEY_SIZE);
  106. Serial.println();
  107. // Dump block data
  108. Serial.print(F("Block ")); Serial.print(block); Serial.print(F(":"));
  109. dump_byte_array1(buffer, 16); //omzetten van hex naar ASCI
  110. Serial.println();
  111. for (int p = 0; p < 16; p++) //De 16 bits uit de block uitlezen
  112. {
  113. waarde [block][p] = buffer[p];
  114. Serial.print(waarde[block][p]);
  115. Serial.print(" ");
  116. }
  117. }
  118. }
  119. Serial.println();
  120. Serial.println("1.Read card \n2.Write to card \n3.Copy the data.");
  121. mfrc522.PICC_HaltA(); // Halt PICC
  122. mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
  123. return result;
  124. start();
  125. }
  126. /*
  127. * Main loop.
  128. */
  129. void loop() {
  130. start();
  131. }
  132. void start(){
  133. choice = Serial.read();
  134. if(choice == '1')
  135. {
  136. Serial.println("Read the card");
  137. keuze1();
  138. }
  139. else if(choice == '2')
  140. {
  141. Serial.println("See what is in the variables");
  142. keuze2();
  143. }
  144. else if(choice == '3')
  145. {
  146. Serial.println("Copying the data on to the new card");
  147. keuze3();
  148. }
  149. }
  150. void keuze2(){ //Test waardes in blokken
  151. for(block = 4; block <= 62; block++){
  152. 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){
  153. block ++;
  154. }
  155. Serial.print(F("Writing data into block "));
  156. Serial.print(block);
  157. Serial.println("\n");
  158. for(int j = 0; j < 16; j++){
  159. Serial.print(waarde[block][j]);
  160. Serial.print(" ");
  161. }
  162. Serial.println("\n");
  163. }
  164. Serial.println("1.Read card \n2.Write to card \n3.Copy the data.");
  165. start();
  166. }
  167. void keuze3(){ //Copy the data in the new card
  168. Serial.println("Insert new card...");
  169. // Look for new cards
  170. if ( ! mfrc522.PICC_IsNewCardPresent())
  171. return;
  172. // Select one of the cards
  173. if ( ! mfrc522.PICC_ReadCardSerial())
  174. return;
  175. // Show some details of the PICC (that is: the tag/card)
  176. Serial.print(F("Card UID:"));
  177. dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  178. Serial.println();
  179. Serial.print(F("PICC type: "));
  180. MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  181. Serial.println(mfrc522.PICC_GetTypeName(piccType));
  182. // Try the known default keys
  183. /*MFRC522::MIFARE_Key key;
  184. for (byte k = 0; k < NR_KNOWN_KEYS; k++) {
  185. // Copy the known key into the MIFARE_Key structure
  186. for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) {
  187. key.keyByte[i] = knownKeys[k][i];
  188. }
  189. }*/
  190. for (byte i = 0; i < 6; i++) {
  191. key.keyByte[i] = 0xFF;
  192. }
  193. for(int i = 4; i <= 62; i++){ //De blocken 4 tot 62 kopieren, behalve al deze onderstaande blocken (omdat deze de authenticatie blokken zijn)
  194. 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){
  195. i++;
  196. }
  197. block = i;
  198. // Authenticate using key A
  199. Serial.println(F("Authenticating using key A..."));
  200. status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
  201. if (status != MFRC522::STATUS_OK) {
  202. Serial.print(F("PCD_Authenticate() failed: "));
  203. Serial.println(mfrc522.GetStatusCodeName(status));
  204. return;
  205. }
  206. // Authenticate using key B
  207. Serial.println(F("Authenticating again using key B..."));
  208. status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, block, &key, &(mfrc522.uid));
  209. if (status != MFRC522::STATUS_OK) {
  210. Serial.print(F("PCD_Authenticate() failed: "));
  211. Serial.println(mfrc522.GetStatusCodeName(status));
  212. return;
  213. }
  214. // Write data to the block
  215. Serial.print(F("Writing data into block "));
  216. Serial.print(block);
  217. Serial.println("\n");
  218. dump_byte_array(waarde[block], 16);
  219. status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(block, waarde[block], 16);
  220. if (status != MFRC522::STATUS_OK) {
  221. Serial.print(F("MIFARE_Write() failed: "));
  222. Serial.println(mfrc522.GetStatusCodeName(status));
  223. }
  224. Serial.println("\n");
  225. }
  226. mfrc522.PICC_HaltA(); // Halt PICC
  227. mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
  228. Serial.println("1.Read card \n2.Write to card \n3.Copy the data.");
  229. start();
  230. }
  231. void keuze1(){ //Read card
  232. Serial.println("Insert card...");
  233. // Look for new cards
  234. if ( ! mfrc522.PICC_IsNewCardPresent())
  235. return;
  236. // Select one of the cards
  237. if ( ! mfrc522.PICC_ReadCardSerial())
  238. return;
  239. // Show some details of the PICC (that is: the tag/card)
  240. Serial.print(F("Card UID:"));
  241. dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  242. Serial.println();
  243. Serial.print(F("PICC type: "));
  244. MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  245. Serial.println(mfrc522.PICC_GetTypeName(piccType));
  246. // Try the known default keys
  247. MFRC522::MIFARE_Key key;
  248. for (byte k = 0; k < NR_KNOWN_KEYS; k++) {
  249. // Copy the known key into the MIFARE_Key structure
  250. for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) {
  251. key.keyByte[i] = knownKeys[k][i];
  252. }
  253. // Try the key
  254. if (try_key(&key)) {
  255. // Found and reported on the key and block,
  256. // no need to try other keys for this PICC
  257. break;
  258. }
  259. }
  260. }