Skip to content

Instantly share code, notes, and snippets.

@minustore
Created December 2, 2018 07:20
Show Gist options
  • Save minustore/b872c5a5ec6c3bf3411a476f560ff8ce to your computer and use it in GitHub Desktop.
Save minustore/b872c5a5ec6c3bf3411a476f560ff8ce to your computer and use it in GitHub Desktop.
change the mifare classic 1K UID use back door,use Adafruit PN532 library
/**************************************************************************/
/*!
@file changeUID.pde
@author Adafruit Industries
@license BSD (see license.txt)
##change the mifare classic 1K UID use back door !!!##
This example will attempt to connect to an ISO14443A
card or tag and retrieve some basic information about it
that can be used to determine what type of card it is.
Note that you need the baud rate to be 115200 because we need to print
out the data and read from the card at the same time!
This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards
This library works with the Adafruit NFC breakout
----> https://www.adafruit.com/products/364
Check out the links above for our tutorials and wiring diagrams
These chips use SPI or I2C to communicate.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
*/
/**************************************************************************/
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SCK (13)
#define PN532_MOSI (11)
#define PN532_SS (10)
#define PN532_MISO (12)
// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines. Use the values below (2, 3) for the shield!
#define PN532_IRQ (2)
#define PN532_RESET (3) // Not connected by default on the NFC Shield
// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:
// Use this line for a breakout with a SPI connection:
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
uint8_t packet[32];
boolean sendCmdAndprint(uint8_t *cmd, uint8_t cmdlen)
{
boolean ret;
ret = nfc.sendCommandCheckAck(cmd, cmdlen);
if (ret) {
Serial.println("success send cmd and read ack");
}else{
Serial.println("failed send cmd");
return false;
}
// read data packet
nfc.readdata(packet, 20);
Serial.print("Return Value: ");
for (uint8_t i=0; i < 16; i++)
{
Serial.print(" 0x");Serial.print(packet[i], HEX);
}
Serial.println("");
return true;
}
bool WriteRegister(uint8_t* reg,uint8_t len){
uint8_t cmd[1+3*len];
uint8_t result[6+0+2];
cmd[0]=0x08;//WriteRegister
for(uint8_t i=0; i<3*len; i++){
cmd[i+1]=reg[i];
}
if(nfc.sendCommandCheckAck(cmd,1+3*len)){
//nfc.readdata(result,6+0+2);
return true;
}else{
return false;
}
}
bool InCommunicateThru(uint8_t* data,uint8_t len){
uint8_t cmd[1+len];
cmd[0]=0x42;//InCommunicateThru
for(uint8_t i=0; i<len; i++){
cmd[i+1]=data[i];
}
if(nfc.sendCommandCheckAck(cmd,1+len)){
return true;
}else{
return false;
}
}
bool Unlock(){
//HALT
uint8_t regState1[6]={0x63, 0x02, 0x00, 0x63, 0x03, 0x00};
if(!WriteRegister(regState1,6/3)){
return false;
}
uint8_t halt[4]={0x50, 0x00, 0x57, 0xcd};
if(!InCommunicateThru(halt,4)){
return false;
}
//UNLOCK1
uint8_t reg1[3]={0x63, 0x3d, 0x07};
if(!WriteRegister(reg1,3/3)){
return false;
}
uint8_t unlock1[1]={0x40};
if(!InCommunicateThru(unlock1,1)){
return false;
}
//UNLOCK2
uint8_t reg2[3]={0x63, 0x3d, 0x00};
if(!WriteRegister(reg2,3/3)){
return false;
}
uint8_t unlock2[1]={0x43};
if(!InCommunicateThru(unlock2,1)){
return false;
}
uint8_t regState2[6]={0x63, 0x02, 0x80, 0x63, 0x03, 0x80};
if(!WriteRegister(regState2,6/3)){
return false;
}
return true;
}
// Use this line for a breakout with a hardware SPI connection. Note that
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are
// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.
//Adafruit_PN532 nfc(PN532_SS);
// Or use this line for a breakout or shield with an I2C connection:
//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
void setup(void) {
Serial.begin(115200);
Serial.println("Hello!");
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
// Set the max number of retry attempts to read from a card
// This prevents us from waiting forever for a card, which is
// the default behaviour of the PN532.
nfc.setPassiveActivationRetries(0xFF);
// configure board to read RFID tags
nfc.SAMConfig();
Serial.println("Waiting for an ISO14443A card");
}
void loop(void) {
boolean success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
uint8_t block[16];
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if (success) {
Serial.println("Found a card!");
Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i=0; i < uidLength; i++)
{
Serial.print(" 0x");Serial.print(uid[i], HEX);
}
Serial.println("");
if(Unlock())
{
Serial.println("success unlock");
// read all blocks
#define WRITE_BLOCK0
#ifdef READ_BLOCKS
for(uint8_t currentblock = 0; currentblock < 64; currentblock++)
{
success = nfc.mifareclassic_ReadDataBlock(currentblock, block);
if (success)
{
// Read successful
Serial.print("Block ");Serial.print(currentblock, DEC);
if (currentblock < 10)
{
Serial.print(" ");
}
else
{
Serial.print(" ");
}
// Dump the raw data
nfc.PrintHexChar(block, 16);
}
}
#endif
// write block 0 to change uid
#ifdef WRITE_BLOCK0
uint8_t block_uid[16] = {0x12, 0x6B, 0x57 ,0xEB ,0xC5 ,0x08 ,0x04 ,0x00 ,0x02 ,0x2B ,0x09 ,0xE9 ,0x63 ,0x12 ,0xBC ,0x1D};
success = nfc.mifareclassic_WriteDataBlock(0, block_uid);
if(success)
{
Serial.println("success write block0");
}else{
Serial.println("failed write block0");
}
#endif
}else{
Serial.println("failed unlock");
}
#ifdef TEMP
// halt
packet[0] = 0x42; // InCommunicateThru
packet[1] = 0x50; // 50 00 57 CD halt
packet[2] = 0x00;
packet[3] = 0x57;
packet[4] = 0xCD;
if(!sendCmdAndprint(packet, 5))
{
return;
}
// 40(7-bits)
uint8_t reg1[3]={0x63, 0x3d, 0x07};
if(!WriteRegister(reg1,3/3)){
return false;
}
packet[0] = 0x42; // InCommunicateThru
packet[1] = 0x40; //
if(!sendCmdAndprint(packet, 2))
{
return;
}
#endif
// Wait 1 second before continuing
delay(5000);
}
else
{
// PN532 probably timed out waiting for a card
Serial.println("Timed out waiting for a card");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment