Advertisement
asselinpaul

RFID Spoofer Source Code

Jan 3rd, 2012
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.85 KB | None | 0 0
  1. #include <Keypad.h>
  2. #include <stdio.h>
  3. #include <EEPROM.h>
  4. #include <avr/interrupt.h>
  5. #include <avr/io.h>
  6. #include <avr/sleep.h>
  7.  
  8. // uncomment the following line to get debug information dumped
  9. //#define SERIALDEBUG
  10.  
  11.  
  12. /***************************************************************************
  13.  *                                                                         *
  14.  *  A Universal RFID Key - Instructables Version                           *
  15.  *                                                                         *
  16.  *   Copyright (C) 2010  Doug Jackson (doug@doughq.com)                    *
  17.  *                                                                         *
  18.  ***************************************************************************
  19.  *                                                                         *
  20.  * This program is free software; you can redistribute it and/or modify    *
  21.  * it under the terms of the GNU General Public License as published by    *
  22.  * the Free Software Foundation; either version 2 of the License, or       *
  23.  * (at your option) any later version.                                     *
  24.  *                                                                         *
  25.  * This program is distributed in the hope that it will be useful,         *
  26.  * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
  27.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
  28.  * GNU General Public License for more details.                            *
  29.  *                                                                         *
  30.  * You should have received a copy of the GNU General Public License       *
  31.  * along with this program; if not, write to the Free Software             *
  32.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,                   *
  33.  * MA  02111-1307  USA                                                     *
  34.  *                                                                         *
  35.  ***************************************************************************
  36.  *                                                                         *
  37.  *    * * * * * * * *      W A R N I N G     * * * * * * * * * *           *
  38.  * This project implements what is effectively a universal skeleton key    *
  39.  * for use on a range of RFID access systems.  It is presented for         *
  40.  * educational and demonstration purposes only to enable others to learn   *
  41.  * about the design and limitations of RFID technologies.                  *
  42.  *                                                                         *
  43.  * The Author is not responsible for misuse of the technological system    *
  44.  * implemented by this software - USE AT YOUR OWN RISK!!                   *
  45.  *                                                                         *
  46.  ***************************************************************************
  47.  *
  48.  *
  49.  * Revision History
  50.  *
  51.  * Date     By  What
  52.  * 20101002 DRJ Initial Creation of Arduino Version
  53.  * 20101024     DRJ     Added facility to arbitrarily enter a facility and
  54.  *                      UserID number
  55.  * 20101025     DRJ     Added ability to enter decimal UserID
  56.  * 20101124     DRJ     Removed my Work specific functions for public release
  57.  ***************************************************************************
  58.  *
  59.  *  COMMAND STRUCTURE
  60.  *
  61.  * Mode key is pressed until appropriate mode is displayed on 4 upper leds
  62.  * Enter key triggers action
  63.  *
  64.  * Mode 1 - Sleep (power down till next reset)
  65.  * Mode 2 - Allow HEX facility code to be entered
  66.  *          2 decimal characters are then read into facility[] array;
  67.  * Mode 3 - Allow Decimal userID to be entered
  68.  *          8 decimal characters are then read into user[] array;
  69.  * Mode 4 - Dump data - Facility code and User code are output on 4 LEDs one byte at a time
  70.  * Mode 5 - Emulate Card
  71.  *
  72.  *
  73.  *************************************************************************************/
  74.  
  75. #define DATALED1 3
  76. #define DATALED2 2
  77. #define DATALED3 1
  78. #define DATALED4 0
  79. #define STATUSLED1 13
  80. #define STATUSLED2 9
  81.  
  82. // the Coil is connected to Analog 5 = Digital 19
  83. #define COIL 19
  84.  
  85.  
  86. const byte ROWS = 5; //five rows
  87. const byte COLS = 4; //four columns
  88. char keys[ROWS][COLS] = {
  89.   {
  90.     '1','2','3','A'                  }
  91.   ,
  92.   {
  93.     '4','5','6','B'                  }
  94.   ,
  95.   {
  96.     '7','8','9','C'                  }
  97.   ,
  98.   {
  99.     '*','0','#','D'                  }
  100.   ,
  101.   {
  102.     'N','M','F','E'                  }
  103.   ,
  104.  
  105. };
  106. byte rowPins[ROWS] = {
  107.   10, 11, 8, 17, 15}; //connect to the row pinouts of the keypad
  108. byte colPins[COLS] = {
  109.   12, 7, 16, 18}; //connect to the column pinouts of the keypad
  110.  
  111. byte facility[2]={ 0x02, 0x0C };
  112. byte cardID[8]={ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  113. int colsum[4]={ 0,0,0,0}; // storage for the column checksums
  114.  
  115. // delay between symbols when we are transmitting
  116. int bittime=256;
  117.  
  118. byte RFIDdata[128];
  119.  
  120.  
  121. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  122.  
  123. int clock=0;  // storage for the current state of our clock signal.
  124.  
  125. byte datapointer=0;
  126. byte state;
  127. byte mode=1;
  128.  
  129.  
  130. void setup()
  131. {
  132.  
  133.   pinMode(DATALED1, OUTPUT);
  134.   pinMode(DATALED2, OUTPUT);
  135.   pinMode(DATALED3, OUTPUT);  
  136.   pinMode(DATALED4, OUTPUT);
  137.   pinMode(STATUSLED1, OUTPUT);
  138.   pinMode(STATUSLED2, OUTPUT);
  139.  
  140.  
  141.   pinMode(COIL, OUTPUT);
  142.   //Start with it LOW  
  143.   digitalWrite(COIL, LOW);
  144.  
  145.   if (EEPROM.read(0)==0xa5)
  146.   {
  147.       facility[0]=EEPROM.read(1);
  148.       facility[1]=EEPROM.read(2);
  149.      
  150.       cardID[0]=EEPROM.read(3);
  151.       cardID[1]=EEPROM.read(4);
  152.       cardID[2]=EEPROM.read(5);
  153.       cardID[3]=EEPROM.read(6);
  154.       cardID[4]=EEPROM.read(7);
  155.       cardID[5]=EEPROM.read(8);
  156.       cardID[6]=EEPROM.read(9);
  157.       cardID[7]=EEPROM.read(10);
  158.   }
  159.   else
  160.  {
  161.     EEPROM.write(0,0xa5);
  162.     facility[0]=0x02; EEPROM.write(1,facility[0]);  
  163.     facility[1]=0x0c; EEPROM.write(2,facility[1]);
  164.       for (int i=0; i<8; i++)
  165.     {
  166.       cardID[i]=0; EEPROM.write(i+2,cardID[i]);
  167.     }
  168.   }
  169.    
  170.  
  171.     #ifdef SERIALDEBUG
  172.     Serial.begin(9600);  
  173.     delay(200);
  174.     Serial.println("  ");
  175.     Serial.println("RFID Spoofer (c) 2010 D Jackson");
  176.     #endif
  177. }
  178.  
  179. void WriteHeader(void)
  180. {
  181.   // a header consists of 9 one bits
  182.   RFIDdata[datapointer++]=1;
  183.   RFIDdata[datapointer++]=1;  
  184.   RFIDdata[datapointer++]=1;  
  185.   RFIDdata[datapointer++]=1;
  186.   RFIDdata[datapointer++]=1;
  187.   RFIDdata[datapointer++]=1;
  188.   RFIDdata[datapointer++]=1;
  189.   RFIDdata[datapointer++]=1;
  190.   RFIDdata[datapointer++]=1;
  191. }
  192.  
  193.  
  194. void WriteData(byte nibble)
  195. {
  196.   byte data;
  197.   byte rowsum=0;
  198.   for (int i=4; i>0; i--)
  199.   {
  200.     if ((nibble& 1<<i-1) ==0)  
  201.     {
  202.       data=0;
  203.     }
  204.     else
  205.     {
  206.       data=1;
  207.       rowsum++;  // increment the checksum value
  208.       colsum[i-1]++; // increment the column checksum
  209.     }
  210.  
  211.  
  212.     RFIDdata[datapointer++]= data;
  213.     #ifdef SERIALDEBUG
  214.       Serial.print((int) data);
  215.     #endif
  216.      
  217.   }
  218.   // write the row checksum out
  219.   if ((rowsum%2)==0)  
  220.   {
  221.     RFIDdata[datapointer++]=0;
  222.     #ifdef SERIALDEBUG
  223.       Serial.print((int)0);
  224.     #endif
  225.    
  226.   }
  227.   else
  228.   {  
  229.     RFIDdata[datapointer++]=1;
  230.     #ifdef SERIALDEBUG
  231.       Serial.print((int)1);
  232.     #endif
  233.   }
  234.  
  235.     #ifdef SERIALDEBUG
  236.       Serial.println();
  237.     #endif
  238.  
  239. }
  240.  
  241.  
  242. void WriteChecksum(void)
  243. {
  244.   byte data;
  245.   byte rowsum=0;
  246.   for (int i=4; i>0; i--)
  247.   {
  248.     if ((colsum[i-1]%2) ==0)  
  249.     {
  250.       RFIDdata[datapointer++]=0;
  251.      #ifdef SERIALDEBUG
  252.       Serial.print((int)0);
  253.      #endif
  254.     }
  255.     else
  256.     {
  257.       RFIDdata[datapointer++]=1;
  258.       #ifdef SERIALDEBUG
  259.       Serial.print((int) 1);
  260.       #endif
  261.     }  
  262.   }
  263.  
  264.   // write the stop bit
  265.   RFIDdata[datapointer++]=0;
  266.  
  267.       #ifdef SERIALDEBUG
  268.       Serial.print((int)0);
  269.       #endif
  270.  
  271. }
  272.  
  273.  
  274.  
  275.  
  276.  
  277. void BuildCard(void)
  278. {
  279.   // load up the RFID array with card data
  280.   // intitalise the write pointer
  281.   datapointer=0;
  282.  
  283.   WriteHeader();
  284.   // Write facility
  285.   WriteData(facility[0]);
  286.   WriteData(facility[1]);
  287.  
  288.   // Write cardID
  289.   WriteData(cardID[0]);
  290.   WriteData(cardID[1]);
  291.   WriteData(cardID[2]);
  292.   WriteData(cardID[3]);
  293.   WriteData(cardID[4]);  
  294.   WriteData(cardID[5]);
  295.   WriteData(cardID[6]);  
  296.   WriteData(cardID[7]);
  297.  
  298.   WriteChecksum();
  299. }
  300.  
  301.  
  302. void TransmitManchester(int cycle, int data)
  303. {
  304.  
  305.   if(cycle ^ data == 1)
  306.   {
  307.     digitalWrite(COIL, HIGH);
  308.   }
  309.   else
  310.   {
  311.     digitalWrite(COIL, LOW);  
  312.   }
  313. }
  314.  
  315.  
  316.  
  317.  
  318. void writedataLEDS(int temp)
  319. {
  320.   if (temp & 1<<0) digitalWrite(DATALED1,HIGH);
  321.   else digitalWrite(DATALED1,LOW);
  322.   if (temp & 1<<1) digitalWrite(DATALED2,HIGH);
  323.   else digitalWrite(DATALED2,LOW);
  324.   if (temp & 1<<2) digitalWrite(DATALED3,HIGH);
  325.   else digitalWrite(DATALED3,LOW);
  326.   if (temp & 1<<3) digitalWrite(DATALED4,HIGH);
  327.   else digitalWrite(DATALED4,LOW);  
  328.  
  329. }
  330.  
  331.  
  332.  
  333. void EmulateCard(void)
  334. {
  335.   #ifdef SERIALDEBUG
  336.   Serial.println("Emulate Card Entered");
  337.   #endif  // enter a low power modewritedataLEDS(0);  // turn off the LEDs
  338.  
  339.   BuildCard();
  340.  
  341.   #ifdef SERIALDEBUG
  342.   Serial.println();
  343.   for(int i = 0; i < 64; i++)
  344.   {
  345.     if (RFIDdata[i]==1) Serial.print("1");
  346.     else if (RFIDdata[i]==0) Serial.print("0");
  347.     else Serial.print((int)RFIDdata[i]);
  348.   }
  349.   Serial.println();
  350.   #endif  
  351.  
  352.  
  353.   while (1==1)
  354.   {
  355.     for(int i = 0; i < 64; i++)
  356.     {
  357.       TransmitManchester(0, RFIDdata[i]);
  358.       delayMicroseconds(bittime);
  359.       TransmitManchester(1, RFIDdata[i]);
  360.       delayMicroseconds(bittime);
  361.     }
  362.   }
  363. }
  364.  
  365. void PowerDown(void)
  366. {
  367.   #ifdef SERIALDEBUG
  368.   Serial.println("Sleep Mode Entered");
  369.   #endif  // enter a low power mode
  370.  
  371.   writedataLEDS(0);  // turn off the LEDs
  372.  
  373.   set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  374.   sleep_enable();
  375.   sleep_mode();
  376. }
  377.  
  378.  
  379.  
  380.  
  381.  
  382. void DumpData(void)
  383. {
  384.   #ifdef SERIALDEBUG
  385.   Serial.println("Dump Data Entered");
  386.   #endif  
  387.  
  388.   // dump the facility and card codes.
  389.   writedataLEDS(0);  // turn off the data LEDs
  390.   for (int i=0; i<2; i++)
  391.   {  
  392.     digitalWrite(STATUSLED1,HIGH);  
  393.     writedataLEDS(facility[i]);
  394.     delay(2000);  
  395.     digitalWrite(STATUSLED1,LOW);
  396.     delay(500);
  397.   }
  398.   writedataLEDS(0);  // turn off the data LEDs
  399.   digitalWrite(STATUSLED1,LOW);
  400.  
  401.   for (int i=0; i<8; i++)
  402.   {  
  403.     digitalWrite(STATUSLED2,HIGH);
  404.     writedataLEDS(cardID[i]);
  405.     delay(2000);  
  406.     digitalWrite(STATUSLED2,LOW);
  407.     delay(500);
  408.   }
  409.   digitalWrite(STATUSLED2,LOW);
  410.   writedataLEDS(mode);
  411. }
  412.  
  413.  
  414.  
  415. void LoadFacility(void)
  416. {
  417.   char key;
  418.   byte temp;
  419.  
  420.   #ifdef SERIALDEBUG
  421.   Serial.println("LoadFacility Entered");
  422.   #endif  
  423.  
  424.   writedataLEDS(0);  // turn off the data LEDs
  425.   for (int i=0; i<2; i++)
  426.   {  
  427.     writedataLEDS(facility[i]);
  428.     // wait for a keypress
  429.     key = NO_KEY;
  430.     while (key == NO_KEY){
  431.       delay(50);
  432.       key = keypad.getKey();
  433.     }
  434.     switch (key){
  435.       case '0': temp=0; break;
  436.       case '1':  temp=1; break;
  437.       case '2':  temp=2; break;
  438.       case '3':  temp=3; break;
  439.       case '4':  temp=4; break;
  440.       case '5':  temp=5; break;
  441.       case '6':  temp=6; break;
  442.       case '7':  temp=7; break;
  443.       case '8':  temp=8; break;
  444.       case '9':  temp=9; break;
  445.       case 'A':  temp=0x0a; break;
  446.       case 'B':  temp=0x0b; break;
  447.       case 'C':  temp=0x0c; break;
  448.       case 'D':  temp=0x0d; break;
  449.       case 'E':  temp=0x0e; break;
  450.       case 'F':  temp=0x0f; break;
  451.     }  
  452.  
  453.     digitalWrite(STATUSLED1,HIGH);  
  454.     facility[i]=temp;
  455.  
  456.     writedataLEDS(facility[i]);
  457.     delay(200);  
  458.     writedataLEDS(0);
  459.     delay(200);  
  460.     writedataLEDS(facility[i]);
  461.     delay(200);
  462.   }
  463.  
  464.   writedataLEDS(mode);
  465.   digitalWrite(STATUSLED1,LOW);
  466.   delay(100);
  467.   digitalWrite(STATUSLED1,HIGH);
  468.   for (int i=0; i<2; i++) EEPROM.write(i+1,facility[i]);
  469.   delay(200);
  470.   digitalWrite(STATUSLED1,LOW);
  471. }
  472.  
  473.  
  474.  
  475. void LoadCardID(void){
  476.   char tempchar[9];  // temporary storage for decimal to int conversion
  477.   char key;
  478.   byte temp;
  479.   long decimalval;
  480.  
  481.   #ifdef SERIALDEBUG
  482.   Serial.println("LoadDecimalCardID Entered");
  483.   #endif
  484.  
  485.   writedataLEDS(0);  // turn off the data LEDs
  486.   for (int i=0; i<8; i++)
  487.   {  
  488.     // wait for a keypress
  489.     key = NO_KEY;
  490.     while (key == NO_KEY){
  491.       delay(50);
  492.       key = keypad.getKey();
  493.     }
  494.    
  495.     tempchar[i]=key;
  496.    
  497.     digitalWrite(STATUSLED2,HIGH);  
  498.  
  499.     writedataLEDS(tempchar[i]);
  500.     delay(200);  
  501.   }
  502.  
  503.   tempchar[8]='\n';
  504.   #ifdef SERIALDEBUG
  505.     Serial.print("datastring="); Serial.println(tempchar);
  506.   #endif
  507.   decimalval=atol(tempchar);
  508.   #ifdef SERIALDEBUG
  509.    Serial.print("datalong="); Serial.println((unsigned long)decimalval);
  510.   #endif
  511.    
  512.   sprintf(tempchar,"%4.4X",(decimalval & 0xffff));
  513.   #ifdef SERIALDEBUG
  514.    Serial.print("dataHEXLO=");
  515.    Serial.println(tempchar);
  516.   #endif
  517.   for (int i=4; i<8; i++) cardID[i]=asciitohex(tempchar[i-4]);
  518.  
  519.   decimalval = ((decimalval & 0xffff0000) >> 16);
  520.   sprintf(tempchar,"%4.4X",decimalval);
  521.   #ifdef SERIALDEBUG
  522.    Serial.print("dataHEXHi=");
  523.    Serial.print(tempchar);
  524.   #endif
  525.   for (int i=0; i<4; i++) cardID[i]=asciitohex(tempchar[i]);
  526.  
  527.   writedataLEDS(mode);
  528.   digitalWrite(STATUSLED2,LOW);
  529.   delay(100);
  530.   digitalWrite(STATUSLED2,HIGH);
  531.   for (int i=0; i<8; i++) EEPROM.write(i+3,cardID[i]);
  532.   delay(200);
  533.   digitalWrite(STATUSLED2,LOW);
  534. }
  535.  
  536.  
  537. char asciitohex(char value)  {
  538.     char temp;
  539.     switch (value){
  540.       case '0':  temp=0; break;
  541.       case '1':  temp=1; break;
  542.       case '2':  temp=2; break;
  543.       case '3':  temp=3; break;
  544.       case '4':  temp=4; break;
  545.       case '5':  temp=5; break;
  546.       case '6':  temp=6; break;
  547.       case '7':  temp=7; break;
  548.       case '8':  temp=8; break;
  549.       case '9':  temp=9; break;
  550.       case 'A':  temp=0x0a; break;
  551.       case 'B':  temp=0x0b; break;
  552.       case 'C':  temp=0x0c; break;
  553.       case 'D':  temp=0x0d; break;
  554.       case 'E':  temp=0x0e; break;
  555.       case 'F':  temp=0x0f; break;
  556.     }  
  557.    return temp;
  558. }
  559.  
  560.    
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567. void loop(void)
  568. {
  569.   char key = keypad.getKey();
  570.  
  571.   if (key != NO_KEY){
  572.  
  573.     if (key=='M') {  
  574.       mode++;
  575.       if (mode>5) mode=1;
  576.       writedataLEDS(mode);
  577.       delay(100);
  578.       Serial.print("Mode=");
  579.       Serial.println((int)mode);
  580.     }
  581.  
  582.     if (key=='N')  {   // enter key pressed - dofunction
  583.       switch (mode){
  584.       case 1:
  585.         PowerDown();    // power down mode
  586.         break;
  587.       case 2:
  588.         LoadFacility(); // allow user to enter facility data
  589.         break;
  590.       case 3:
  591.         LoadCardID();   // allow user to enter the card id
  592.         break;
  593.       case 4:
  594.         DumpData();     // display the card data
  595.         break;
  596.       case 5:
  597.         EmulateCard();  // start card emulation
  598.         break;
  599.       }
  600.       #ifdef SERIALDEBUG
  601.        Serial.println(key);
  602.       #endif  
  603.     }
  604.   }
  605. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement