From 271cf40b31dabb5aa8ac62188667a0ac53e29ea6 Mon Sep 17 00:00:00 2001 From: Adam Laurie Date: Thu, 4 Oct 2012 20:23:30 +0100 Subject: [PATCH] more package changes - be sure to read INSTALL for small but important details! --- INSTALL | 25 +++++++ bruteforce.py | 11 +-- cardselect.py | 9 ++- copytag.py | 5 +- demotag.py | 7 +- eeprom.py | 7 +- fdxbnum.py | 10 +-- formatmifare1kvalue.py | 7 +- froschtest.py | 7 +- hidprox.py | 11 +-- hitag2brute.py | 9 ++- hitag2reset.py | 11 +-- isotype.py | 7 +- jcopmifare.py | 11 +-- jcopsetatrhist.py | 8 +- jcoptool.py | 11 +-- lfxtype.py | 7 +- loginall.py | 7 +- mrpkey.py | 14 ++-- multiselect.py | 8 +- pn532emulate.py | 10 +-- pn532mitm.py | 13 ++-- q5reset.py | 11 +-- readlfx.py | 9 ++- readmifare1k.py | 7 +- readmifaresimple.py | 11 +-- readmifareultra.py | 9 ++- readtag.py | 7 +- RFIDIOt/__init__.py => rfidiot/RFIDIOt.py | 74 ++++++++++--------- .../RFIDIOtconfig.py => rfidiot/__init__.py | 13 ++-- {RFIDIOt => rfidiot}/iso3166.py | 0 pn532.py => rfidiot/pn532.py | 0 {RFIDIOt => rfidiot}/pyandroid.py | 24 +++--- {RFIDIOt => rfidiot}/pynfc.py | 46 ++++++------ rfidiot/rfidiotglobals.py | 1 + send_apdu.py | 7 +- setup.py | 6 +- transit.py | 11 +-- unique.py | 11 +-- writelfx.py | 11 +-- writemifare1k.py | 11 +-- 41 files changed, 272 insertions(+), 212 deletions(-) rename RFIDIOt/__init__.py => rfidiot/RFIDIOt.py (98%) rename RFIDIOt/RFIDIOtconfig.py => rfidiot/__init__.py (95%) rename {RFIDIOt => rfidiot}/iso3166.py (100%) rename pn532.py => rfidiot/pn532.py (100%) rename {RFIDIOt => rfidiot}/pyandroid.py (91%) rename {RFIDIOt => rfidiot}/pynfc.py (94%) create mode 100644 rfidiot/rfidiotglobals.py diff --git a/INSTALL b/INSTALL index fcad15b..71906dd 100644 --- a/INSTALL +++ b/INSTALL @@ -2,3 +2,28 @@ to install: sudo python ./setup.py install +this will create a python library called 'rfidiot'. + +*** note - if you are upgrading from a version that didn't use an install script, +and you've written your own clients, you'll need to make the following changes: + + replace all instances of 'RFIDIOTconfig' with 'rfidiot' + + e.g. + + import RFIDIOtconfig + + becomes + + import rfidiot + + and + + args= RFIDIOtconfig.args + + becomes + + args= rfidiot.args + + etc. + diff --git a/bruteforce.py b/bruteforce.py index 3438122..eba783c 100755 --- a/bruteforce.py +++ b/bruteforce.py @@ -21,20 +21,21 @@ # -import RFIDIOtconfig +import rfidiot import random import sys import os try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -args= RFIDIOtconfig.args -help= RFIDIOtconfig.help +args= rfidiot.args +help= rfidiot.help -card.info('bruteforce v0.1h') +card.info('bruteforce v0.1i') card.select() print 'Card ID: ' + card.uid diff --git a/cardselect.py b/cardselect.py index e0a05a4..ee19259 100755 --- a/cardselect.py +++ b/cardselect.py @@ -22,18 +22,19 @@ # -import RFIDIOtconfig +import rfidiot import sys import os try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -args= RFIDIOtconfig.args +args= rfidiot.args -card.info('cardselect v0.1l') +card.info('cardselect v0.1m') # force card type if specified if len(args) == 1: card.settagtype(args[0]) diff --git a/copytag.py b/copytag.py index 5c90fd9..b3a6c35 100755 --- a/copytag.py +++ b/copytag.py @@ -22,14 +22,15 @@ # -import RFIDIOtconfig +import rfidiot import sys import os import string try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) card.info('copytag v0.1d') diff --git a/demotag.py b/demotag.py index 8478337..8a3d467 100755 --- a/demotag.py +++ b/demotag.py @@ -20,16 +20,17 @@ # GNU General Public License for more details. # -import RFIDIOtconfig +import rfidiot import sys import os try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(False) -args= RFIDIOtconfig.args +args= rfidiot.args print 'Setting ID to: ' + args[0] print card.demotag(card.DT_SET_UID,card.ToBinary(args[0])) diff --git a/eeprom.py b/eeprom.py index eb5b35b..1cdd113 100755 --- a/eeprom.py +++ b/eeprom.py @@ -21,16 +21,17 @@ # -import RFIDIOtconfig +import rfidiot import sys import os try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -card.info('eeprom v0.1c') +card.info('eeprom v0.1e') print 'Station:\t' + card.station() print 'Protocol:\t' + card.PCON() print 'Protocol2:\t' + card.PCON2() diff --git a/fdxbnum.py b/fdxbnum.py index 9b90e80..f16f6a5 100755 --- a/fdxbnum.py +++ b/fdxbnum.py @@ -21,20 +21,20 @@ # -import RFIDIOtconfig +import rfidiot import sys import os import string try: - card= RFIDIOtconfig.card + card= rfidiot.card except: os._exit(True) -args= RFIDIOtconfig.args -help= RFIDIOtconfig.help +args= rfidiot.args +help= rfidiot.help -card.info('fdxbnum v0.1e') +card.info('fdxbnum v0.1f') precoded= False diff --git a/formatmifare1kvalue.py b/formatmifare1kvalue.py index 10da69e..a2a4f40 100755 --- a/formatmifare1kvalue.py +++ b/formatmifare1kvalue.py @@ -21,17 +21,18 @@ # -import RFIDIOtconfig +import rfidiot import sys import string import os try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -card.info('formatmifare1k v0.1b') +card.info('formatmifare1k v0.1c') card.select() print 'Card ID: ' + card.data while True: diff --git a/froschtest.py b/froschtest.py index f79369f..ef093f3 100755 --- a/froschtest.py +++ b/froschtest.py @@ -20,16 +20,17 @@ # GNU General Public License for more details. # -import RFIDIOtconfig +import rfidiot import sys import os try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -card.info('froschtest v0.1c') +card.info('froschtest v0.1d') print print 'Trying Hitag1: ', if card.frosch(card.FR_HT1_Get_Snr,''): diff --git a/hidprox.py b/hidprox.py index cfc8b27..9da33b9 100755 --- a/hidprox.py +++ b/hidprox.py @@ -25,21 +25,22 @@ import sys import os import string -import RFIDIOtconfig +import rfidiot try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) - -card.info('hidprox v0.1c') +card.info('hidprox v0.1e') if not card.readersubtype == card.READER_OMNIKEY: - print 'Reader type not supported!' + print 'Reader type not supported!', card.ReaderSubType, card.READER_OMNIKEY os._exit(True) try: + card.select() prox= card.pcsc_atr[:6] type= card.HID_PROX_TYPES[prox] print ' Card type:', type diff --git a/hitag2brute.py b/hitag2brute.py index cc059cb..ee1e914 100755 --- a/hitag2brute.py +++ b/hitag2brute.py @@ -22,19 +22,20 @@ # -import RFIDIOtconfig +import rfidiot import sys import os import time try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -args= RFIDIOtconfig.args +args= rfidiot.args -card.info('hitag2brute v0.1b') +card.info('hitag2brute v0.1c') pwd= 0x00 diff --git a/hitag2reset.py b/hitag2reset.py index ae46922..8660719 100755 --- a/hitag2reset.py +++ b/hitag2reset.py @@ -21,20 +21,21 @@ # -import RFIDIOtconfig +import rfidiot import sys import os import string try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -args= RFIDIOtconfig.args -help= RFIDIOtconfig.help +args= rfidiot.args +help= rfidiot.help -card.info('hitag2reset v0.1d') +card.info('hitag2reset v0.1e') # standard config block #CFB='06' + card.HITAG2_TRANSPORT_TAG diff --git a/isotype.py b/isotype.py index b9028c1..48af0ea 100755 --- a/isotype.py +++ b/isotype.py @@ -25,15 +25,16 @@ import sys import os import string -import RFIDIOtconfig +import rfidiot try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -card.info('isotype v0.1l') +card.info('isotype v0.1m') typed= 0 if card.readertype == card.READER_ACG: diff --git a/jcopmifare.py b/jcopmifare.py index 23ddc0f..7cff75f 100755 --- a/jcopmifare.py +++ b/jcopmifare.py @@ -28,17 +28,18 @@ # 15/11/08 - ver 0.1a - first cut, seems to work. :) # 13/01/09 - ver 0.1b - add RANDOM UID mode -import RFIDIOtconfig +import rfidiot import sys import os try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -args= RFIDIOtconfig.args -Help= RFIDIOtconfig.help +args= rfidiot.args +Help= rfidiot.help # fixed values required by JCOP applet CLA= '00' @@ -49,7 +50,7 @@ RANDOM= '03' MIFARE_AID= 'DC4420060606' -card.info('jcopmifare v0.1d') +card.info('jcopmifare v0.1e') if Help or len(args) < 2: print '\nUsage:\n\n\t%s [OPTIONS] [SECTOR] [HEX DATA]' % sys.argv[0] diff --git a/jcopsetatrhist.py b/jcopsetatrhist.py index 5044484..f9ffbea 100755 --- a/jcopsetatrhist.py +++ b/jcopsetatrhist.py @@ -24,18 +24,18 @@ # GNU General Public License for more details. # -import RFIDIOtconfig +import rfidiot import sys import os import string try: - card= RFIDIOtconfig.card + card= rfidiot.card except: os._exit(True) -args= RFIDIOtconfig.args -Help= RFIDIOtconfig.help +args= rfidiot.args +Help= rfidiot.help # fixed values required by JCOP applet CLA= '80' diff --git a/jcoptool.py b/jcoptool.py index 14cf80c..7710507 100755 --- a/jcoptool.py +++ b/jcoptool.py @@ -21,7 +21,7 @@ # GNU General Public License for more details. # -import RFIDIOtconfig +import rfidiot import sys import os import string @@ -30,12 +30,13 @@ from pyasn1.codec.ber import decoder try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -args= RFIDIOtconfig.args -Help= RFIDIOtconfig.help +args= rfidiot.args +Help= rfidiot.help # fixed values required by JCOP applet CLA= '80' @@ -270,7 +271,7 @@ def decode_gp_registry_data(data, padding, filter): return False return True -card.info('jcoptool v0.1c') +card.info('jcoptool v0.1d') if Help or len(args) < 1: print '\nUsage:\n\n\t%s [OPTIONS] [ARGS] [ENC Key] [MAC Key] [KEK Key]' % sys.argv[0] print diff --git a/lfxtype.py b/lfxtype.py index ccda69d..36a6e70 100755 --- a/lfxtype.py +++ b/lfxtype.py @@ -22,17 +22,18 @@ # -import RFIDIOtconfig +import rfidiot import sys import os try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -card.info('lfxtype v0.1i') +card.info('lfxtype v0.1j') card.select() ID= card.uid if ID: diff --git a/loginall.py b/loginall.py index f094ee9..8bfa1d3 100755 --- a/loginall.py +++ b/loginall.py @@ -21,14 +21,15 @@ # -import RFIDIOtconfig +import rfidiot try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -card.info('loginall v0.1g') +card.info('loginall v0.1h') card.select() print '\ncard id: ' + card.uid diff --git a/mrpkey.py b/mrpkey.py index c9e89d5..8da8c78 100755 --- a/mrpkey.py +++ b/mrpkey.py @@ -33,7 +33,7 @@ # this needs fixing - MAX should be able to go up to size supported by device MAXCHUNK= 118 -import RFIDIOtconfig +import rfidiot import sys import os import commands @@ -987,16 +987,16 @@ def bruteno(init): return out try: - passport= RFIDIOtconfig.card + passport= rfidiot.card except: os._exit(True) -args= RFIDIOtconfig.args -Help= RFIDIOtconfig.help -Nogui= RFIDIOtconfig.nogui -DEBUG= RFIDIOtconfig.debug +args= rfidiot.args +Help= rfidiot.help +Nogui= rfidiot.nogui +DEBUG= rfidiot.debug -myver= 'mrpkey v0.1s' +myver= 'mrpkey v0.1t' passport.info(myver) TEST= False diff --git a/multiselect.py b/multiselect.py index ca9c446..3516ba4 100755 --- a/multiselect.py +++ b/multiselect.py @@ -22,20 +22,20 @@ # -import RFIDIOtconfig +import rfidiot import sys import os import time import string try: - card= RFIDIOtconfig.card + card= rfidiot.card except: os._exit(True) -args= RFIDIOtconfig.args +args= rfidiot.args -card.info('multiselect v0.1m') +card.info('multiselect v0.1n') # force card type if specified if len(args) == 1: diff --git a/pn532emulate.py b/pn532emulate.py index e5c7162..66ea76c 100755 --- a/pn532emulate.py +++ b/pn532emulate.py @@ -23,19 +23,19 @@ from pn532 import * -import RFIDIOtconfig +import rfidiot import sys import os try: - card= RFIDIOtconfig.card + card= rfidiot.card except: os._exit(True) -args= RFIDIOtconfig.args -help= RFIDIOtconfig.help +args= rfidiot.args +help= rfidiot.help -card.info('pn532emulate v0.1c') +card.info('pn532emulate v0.1d') if help or len(args) < 6: print sys.argv[0] + ' - Switch NXP PN532 chip into emulation mode' diff --git a/pn532mitm.py b/pn532mitm.py index 1332d3a..21332b6 100755 --- a/pn532mitm.py +++ b/pn532mitm.py @@ -22,8 +22,7 @@ from pn532 import * -import RFIDIOtconfig -import RFIDIOt +import rfidiot import sys import os import string @@ -108,14 +107,14 @@ def recv_data(host): return out[:-2] try: - card= RFIDIOtconfig.card + card= rfidiot.card except: os._exit(True) -args= RFIDIOtconfig.args -help= RFIDIOtconfig.help +args= rfidiot.args +help= rfidiot.help -card.info('pn532mitm v0.1d') +card.info('pn532mitm v0.1e') if help or len(args) < 1: print sys.argv[0] + ' - NXP PN532 Man-In-The-Middle' @@ -201,7 +200,7 @@ def recv_data(host): else: try: readernum= int(args[0]) - emulator= RFIDIOt.rfidiot(readernum,card.readertype,'','','','','','') + emulator= rfidiot.RFIDIOt.rfidiot(readernum,card.readertype,'','','','','','') print ' Emulator:', emulator.info('') if not emulator.readersubtype == card.READER_ACS: diff --git a/q5reset.py b/q5reset.py index ab0af87..f976723 100755 --- a/q5reset.py +++ b/q5reset.py @@ -21,20 +21,21 @@ # -import RFIDIOtconfig +import rfidiot import sys import os import string try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -args= RFIDIOtconfig.args -help= RFIDIOtconfig.help +args= rfidiot.args +help= rfidiot.help -card.info('q5reset v0.1f') +card.info('q5reset v0.1g') # standard config block CFB='e601f004' diff --git a/readlfx.py b/readlfx.py index 74a89b4..fa579f6 100755 --- a/readlfx.py +++ b/readlfx.py @@ -24,17 +24,18 @@ # # specifiy KEY for protected tags. If not specified, TRANSPORT key will be tried. -import RFIDIOtconfig +import rfidiot import sys import os try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -args= RFIDIOtconfig.args -help= RFIDIOtconfig.help +args= rfidiot.args +help= rfidiot.help Q5Mod= { '000':'Manchester',\ '001':'PSK 1',\ diff --git a/readmifare1k.py b/readmifare1k.py index 90b654b..38407ed 100755 --- a/readmifare1k.py +++ b/readmifare1k.py @@ -21,16 +21,17 @@ # -import RFIDIOtconfig +import rfidiot import sys import os try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -card.info('readmifare1k v0.1i') +card.info('readmifare1k v0.1j') card.select() print 'Card ID: ' + card.uid diff --git a/readmifaresimple.py b/readmifaresimple.py index 7c3acbf..afa9c68 100755 --- a/readmifaresimple.py +++ b/readmifaresimple.py @@ -22,17 +22,18 @@ import sys import os -import RFIDIOtconfig +import rfidiot import time import string try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(False) -args= RFIDIOtconfig.args -help= RFIDIOtconfig.help +args= rfidiot.args +help= rfidiot.help blocksread= 0 blockslocked= 0 @@ -69,7 +70,7 @@ print os._exit(True) -card.info('readmifaresimple v0.1g') +card.info('readmifaresimple v0.1h') if not card.select(): card.waitfortag('waiting for Mifare TAG...') diff --git a/readmifareultra.py b/readmifareultra.py index d7da797..8daa567 100755 --- a/readmifareultra.py +++ b/readmifareultra.py @@ -23,16 +23,17 @@ # -import RFIDIOtconfig +import rfidiot import sys import os try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -help= RFIDIOtconfig.help +help= rfidiot.help if help: print sys.argv[0] + ' - read mifare ultralight tags' @@ -40,7 +41,7 @@ print os._exit(True) -card.info('readmifareultra v0.1a') +card.info('readmifareultra v0.1b') card.waitfortag('Waiting for Mifare Ultralight...') blocks=16 diff --git a/readtag.py b/readtag.py index 2b5c924..cf4fd04 100755 --- a/readtag.py +++ b/readtag.py @@ -21,16 +21,17 @@ # -import RFIDIOtconfig +import rfidiot import sys import os try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -card.info('readtag v0.1e') +card.info('readtag v0.1f') card.select() print '\nID: ' + card.uid print ' Data:' diff --git a/RFIDIOt/__init__.py b/rfidiot/RFIDIOt.py similarity index 98% rename from RFIDIOt/__init__.py rename to rfidiot/RFIDIOt.py index d8601bb..64705c0 100755 --- a/RFIDIOt/__init__.py +++ b/rfidiot/RFIDIOt.py @@ -27,8 +27,6 @@ except ImportError: pass - - import os import sys import random @@ -61,12 +59,14 @@ MASK_11785 = 0x8408 MASK_CRC16 = 0xA001 # CRC16 mask (used in ARC files) -DEBUG= False -#DEBUG= True -NoInit= False -NFCReader= None class rfidiot: + DEBUG= False + readertype= None + readersubtype= None + NoInit= False + NFCReader= None + pcsc_atr= None "RFIDIOt - RFID I/O tools - http://rfidiot.org" # local imports from iso3166 import ISO3166CountryCodesAlpha @@ -75,15 +75,13 @@ class rfidiot: # open reader port # def __init__(self,readernum,reader,port,baud,to,debug,noinit,nfcreader): - global NoInit - global DEBUG self.readertype= reader self.readersubtype= reader readernum= int(readernum) - DEBUG= debug - NoInit= noinit - NFCReader= nfcreader - if not NoInit: + self.DEBUG= debug + self.NoInit= noinit + self.NFCReader= nfcreader + if not self.NoInit: if self.readertype == self.READER_PCSC: try: self.pcsc_protocol= smartcard.scard.SCARD_PROTOCOL_T1 @@ -103,7 +101,7 @@ def __init__(self,readernum,reader,port,baud,to,debug,noinit,nfcreader): self.readername= self.pcsc[readernum].name self.pcsc_connection= self.pcsc[readernum].createConnection() # debug option will show APDU traffic - if DEBUG: + if self.DEBUG: from smartcard.CardConnectionObserver import ConsoleCardConnectionObserver observer=ConsoleCardConnectionObserver() self.pcsc_connection.addObserver( observer ) @@ -128,11 +126,13 @@ def __init__(self,readernum,reader,port,baud,to,debug,noinit,nfcreader): else: # default to Omnikey for now self.readersubtype= self.READER_OMNIKEY - if DEBUG: + if self.DEBUG: print 'Reader Subtype:',self.readersubtype # create a connection try: self.pcsc_connection.connect() + if self.DEBUG: + print 'pcsc_connection successful' except: # card may be something like a HID PROX which only returns ATR and does not allow connect hresult, hcontext = smartcard.scard.SCardEstablishContext( smartcard.scard.SCARD_SCOPE_USER ) @@ -160,7 +160,7 @@ def __init__(self,readernum,reader,port,baud,to,debug,noinit,nfcreader): self.acs_set_retry(to) #libnfc device elif self.readertype == self.READER_LIBNFC: - self.nfc = pynfc.NFC() + self.nfc = pynfc.NFC(self.NFCReader) self.readername = self.nfc.LIBNFC_READER #Andoid reader elif self.readertype == self.READER_ANDROID: @@ -206,7 +206,7 @@ def __init__(self,readernum,reader,port,baud,to,debug,noinit,nfcreader): # # MRPmrzu: Machine Readable Passport - Machine Readable Zone - Upper # MRPmrzl Machine Readable Passport - Machine Readable Zone - Lower - VERSION= '1.0d' + VERSION= '1.0e' # Reader types READER_ACG= 0x01 READER_FROSCH= 0x02 @@ -740,7 +740,7 @@ def __init__(self,readernum,reader,port,baud,to,debug,noinit,nfcreader): def info(self,caller): if len(caller) > 0: print caller + ' (using RFIDIOt v' + self.VERSION + ')' - if not NoInit: + if not self.NoInit: self.reset() self.version() if len(caller) > 0: @@ -910,7 +910,7 @@ def pcsc_listreaders(self): print ' No: %d\t\t%s' % (n,reader) n += 1 def libnfc_listreaders(self): - self.nfc.listreaders(NFCReader) + self.nfc.listreaders(self.NFCReader) def waitfortag(self,message): print message # we need a way to interrupt infinite loop @@ -930,9 +930,13 @@ def waitfortag(self,message): time.sleep(0.1) return True def select(self): + if self.DEBUG: + print 'in select' self.uid= '' # return True or False and set tag type and data if self.readertype == self.READER_ACG: + if self.DEBUG: + print 'selecting card using ACG' self.ser.write('s') self.data= self.ser.readline()[:-2] self.tagtype= self.data[:1] @@ -946,6 +950,8 @@ def select(self): self.uid= self.data return True if self.readertype == self.READER_FROSCH: + if self.DEBUG: + print 'selecting card using FROSCH' if self.frosch(self.FR_HT2_Get_Snr_PWD,''): # select returns an extra byte on the serial number, so strip it self.data= self.data[:len(self.data) - 2] @@ -962,6 +968,8 @@ def select(self): return True return False if self.readertype == self.READER_PCSC: + if self.DEBUG: + print 'selecting card using PCSC' try: # start a new connection in case TAG has been switched self.pcsc_connection.disconnect() @@ -992,18 +1000,18 @@ def select(self): return False if self.readertype == self.READER_LIBNFC: try: - if DEBUG: + if self.DEBUG: print 'selecting card using LIBNFC' result = self.nfc.selectISO14443A() if result: self.atr = result.atr self.uid = result.uid - if DEBUG: + if self.DEBUG: print 'ATR: ' + self.atr print 'UID: ' + self.uid return True else: - if DEBUG: + if self.DEBUG: print 'Error selecting card' return False except ValueError: @@ -1011,16 +1019,16 @@ def select(self): if self.readertype == self.READER_ANDROID: try: - if DEBUG: + if self.DEBUG: print 'Reading card using Android' uid = self.android.select() if uid: self.uid = uid - if DEBUG: + if self.DEBUG: print '\tUID: ' + self.uid return True else: - if DEBUG: + if self.DEBUG: print 'Error selecting card' return False except ValueError: @@ -1367,7 +1375,7 @@ def iso_7816_get_challenge(self,length): "get random challenge - challenge will be in .data" ins= 'GET_CHALLENGE' le= '%02x' % length - if DEBUG: + if self.DEBUG: print "DEBUG: requesting %d byte challenge" % length return self.send_apdu('','','','','',ins,'','','','',le) def iso_7816_read_binary(self,bytes,offset): @@ -1447,12 +1455,12 @@ def send_apdu(self,option,pcb,cid,nad,cla,ins,p1,p2,lc,data,le): dlength += len(data) / 2 dlength += len(lc) / 2 dlength += len(le) / 2 - if DEBUG: + if self.DEBUG: print 'sending: ' + 't' + '%02x' % dlength + option + command self.ser.write('t' + '%02x' % dlength + option + command) # need check for 'le' length as well ret= self.ser.readline()[:-2] - if DEBUG: + if self.DEBUG: print 'received:',ret self.errorcode= ret[len(ret) - 4:len(ret)] # copy data if more than just an error code (JCOP sometimes returns an error with data) @@ -1490,17 +1498,17 @@ def login(self,sector,keytype,key): if keytype == 'FF': keytype= 'AA' if not sector == '': - if DEBUG: + if self.DEBUG: print 'sending:', 'l' + ('%02x' % sector) + keytype + key self.ser.write('l' + ('%02x' % sector) + keytype + key) else: - if DEBUG: + if self.DEBUG: print 'sending:','l' + keytype + key self.ser.write('l' + keytype + key) if key == '': self.ser.write('\r') self.errorcode= self.ser.readline()[0] - if DEBUG: + if self.DEBUG: print 'received:', self.errorcode if self.errorcode == 'L': self.errorcode= '' @@ -1689,7 +1697,7 @@ def frosch(self,command,data): commandlen= len(command) bcc= self.frosch_bcc_out(command,commandlen + 1) # send length + command + checkdigit - if DEBUG: + if self.DEBUG: print 'Sending: ', self.HexPrint(chr(commandlen + 1) + command + chr(bcc)) self.ser.write(chr(commandlen + 1) + command + chr(bcc)) @@ -1704,7 +1712,7 @@ def frosch(self,command,data): return False # now read the rest ret += self.ser.read(ord(ret[0])) - if DEBUG: + if self.DEBUG: print 'ret: %d ' % len(ret), self.HexPrint(ret) # check integrity of return @@ -1728,7 +1736,7 @@ def frosch(self,command,data): else : self.errorcode= self.ToHex(status) self.data= '' - if DEBUG: + if self.DEBUG: print "Frosch error:", int(self.errorcode,16) - 256 # reader may need resetting to normal read mode if command == self.FR_HT2_Read_PublicB or command == self.FR_HT2_Read_Miro: diff --git a/RFIDIOt/RFIDIOtconfig.py b/rfidiot/__init__.py similarity index 95% rename from RFIDIOt/RFIDIOtconfig.py rename to rfidiot/__init__.py index f9ac828..625da9a 100755 --- a/RFIDIOt/RFIDIOtconfig.py +++ b/rfidiot/__init__.py @@ -21,15 +21,14 @@ # GNU General Public License for more details. # +import rfidiotglobals + import RFIDIOt import getopt import sys import os import string - -debug= False - # help flag (-h) set? help= False @@ -146,7 +145,7 @@ def printoptions(): for o, a in opts: if o == '-d': - debug= True + rfidiotglobals.Debug= True if o == '-f': nfcreader= int(a) readertype= RFIDIOt.rfidiot.READER_LIBNFC @@ -159,7 +158,7 @@ def printoptions(): noinit= True if o == '-N': readertype= RFIDIOt.rfidiot.READER_LIBNFC - card= RFIDIOt.rfidiot(readernum,readertype,line,speed,timeout,debug,noinit,nfcreader) + card= RFIDIOt.rfidiot(readernum,readertype,line,speed,timeout,rfidiotglobals.Debug,noinit,nfcreader) card.libnfc_listreaders() os._exit(True) if o == '-r': @@ -172,14 +171,14 @@ def printoptions(): if o == '-L': readertype= RFIDIOt.rfidiot.READER_PCSC readernum= 0 - card= RFIDIOt.rfidiot(readernum,readertype,line,speed,timeout,debug,noinit,nfcreader) + card= RFIDIOt.rfidiot(readernum,readertype,line,speed,timeout,rfidiotglobals.Debug,noinit,nfcreader) card.pcsc_listreaders() os._exit(True) if o == '-s': speed= int(a) if o == '-t': timeout= int(a) - card= RFIDIOt.rfidiot(readernum,readertype,line,speed,timeout,debug,noinit,nfcreader) + card= RFIDIOt.rfidiot(readernum,readertype,line,speed,timeout,rfidiotglobals.Debug,noinit,nfcreader) except getopt.GetoptError,e: print "RFIDIOtconfig module ERROR: %s" % e printoptions() diff --git a/RFIDIOt/iso3166.py b/rfidiot/iso3166.py similarity index 100% rename from RFIDIOt/iso3166.py rename to rfidiot/iso3166.py diff --git a/pn532.py b/rfidiot/pn532.py similarity index 100% rename from pn532.py rename to rfidiot/pn532.py diff --git a/RFIDIOt/pyandroid.py b/rfidiot/pyandroid.py similarity index 91% rename from RFIDIOt/pyandroid.py rename to rfidiot/pyandroid.py index 166e97d..8467f08 100644 --- a/RFIDIOt/pyandroid.py +++ b/rfidiot/pyandroid.py @@ -28,10 +28,10 @@ import time import readline import socket +import rfidiotglobals # listening port PORT = 4444 -debug = False class Android(object): VERSION = "0.1" @@ -39,9 +39,9 @@ class Android(object): c = None def __init__(self): - if debug: + if rfidiotglobals.Debug: self.initLog() - if debug: + if rfidiotglobals.Debug: self.log.debug("pyandroid starting") self.configure() @@ -49,7 +49,7 @@ def __del__(self): self.deconfigure() def deconfigure(self): - if debug: + if rfidiotglobals.Debug: self.log.debug("pyandroid: deconfiguring") if self.c is not None: self.c.send("close\n") @@ -65,7 +65,7 @@ def initLog(self, level=logging.DEBUG): self.log.addHandler(sh) def configure(self): - if debug: + if rfidiotglobals.Debug: self.log.debug("pyandroid: Setting up listening port") if self.s is not None: self.s.close() @@ -79,7 +79,7 @@ def configure(self): print e def reset(self): - if debug: + if rfidiotglobals.Debug: self.log.debug("pyandroid: Resetting connections") if self.c is not None: self.c.send("close\n") @@ -89,11 +89,11 @@ def reset(self): self.configure() def select(self): - if debug: + if rfidiotglobals.Debug: self.log.debug("pyandroid in select statement") print 'Waiting for connection from Android device ....' self.c, addr = self.s.accept() # Establish connection with client. - if debug: + if rfidiotglobals.Debug: self.log.debug("pyandroid: Got connection from " + addr[0]) print "Got connection from ", addr # Get UID @@ -102,24 +102,24 @@ def select(self): return uid def sendAPDU(self, apdu): - if debug: + if rfidiotglobals.Debug: self.log.debug("Sending APDU: " + apdu) self.c.send(apdu + '\n') response = self.c.recv(1024) response = response[:-1] - if debug: + if rfidiotglobals.Debug: self.log.debug('APDU r =' + response) return response def sendResults(self, result): - if debug: + if rfidiotglobals.Debug: self.log.debug("Sending results: " + results) self.c.send('r:' + result + '\n') response = self.c.recv(1024) response = response[:-1] - if debug: + if rfidiotglobals.Debug: self.log.debug('Response r =' + response) return response diff --git a/RFIDIOt/pynfc.py b/rfidiot/pynfc.py similarity index 94% rename from RFIDIOt/pynfc.py rename to rfidiot/pynfc.py index cba7c91..6114972 100755 --- a/RFIDIOt/pynfc.py +++ b/rfidiot/pynfc.py @@ -33,6 +33,8 @@ import readline #import RFIDIOtconfig +import rfidiotglobals + # nfc_property enumeration NP_TIMEOUT_COMMAND = 0x00 NP_TIMEOUT_ATR = 0x01 @@ -241,8 +243,7 @@ def __str__(self): return rv class NFC(object): - - def __init__(self): + def __init__(self, nfcreader): self.LIB = ctypes.util.find_library('nfc') #self.LIB = "/usr/local/lib/libnfc.so" #self.LIB = "/usr/local/lib/libnfc_26102009.so.0.0.0" @@ -252,12 +253,13 @@ def __init__(self): self.device = None self.poweredUp = False - if RFIDIOtconfig.debug: + #if RFIDIOtconfig.debug: + if rfidiotglobals.Debug: self.initLog() self.LIBNFC_VER= self.initlibnfc() - if RFIDIOtconfig.debug: + if rfidiotglobals.Debug: self.log.debug("libnfc %s" % self.LIBNFC_VER) - self.configure() + self.configure(nfcreader) def __del__(self): self.deconfigure() @@ -273,7 +275,7 @@ def initLog(self, level=logging.DEBUG): self.log.addHandler(sh) def initlibnfc(self): - if RFIDIOtconfig.debug: + if rfidiotglobals.Debug: self.log.debug("Loading %s" % self.LIB) self.libnfc = ctypes.CDLL(self.LIB) self.libnfc.nfc_version.restype = ctypes.c_char_p @@ -306,13 +308,13 @@ def listreaders(self, target): # print ' \t\t\t\tSpeed:', devices[i].uiSpeed - def configure(self): - if RFIDIOtconfig.debug: + def configure(self, nfcreader): + if rfidiotglobals.Debug: self.log.debug("NFC Readers:") self.listreaders(None) - self.log.debug("Connecting to NFC reader") - if RFIDIOtconfig.nfcreader: - target= self.listreaders(RFIDIOtconfig.nfcreader) + self.log.debug("Connecting to NFC reader number %d" % nfcreader) + if nfcreader != None: + target= self.listreaders(nfcreader) else: target= None if target: @@ -320,14 +322,14 @@ def configure(self): self.device = self.libnfc.nfc_open(0, target) self.libnfc.nfc_device_get_name.restype = ctypes.c_char_p self.LIBNFC_READER= self.libnfc.nfc_device_get_name(self.device) - if RFIDIOtconfig.debug: + if rfidiotglobals.Debug: if self.device == None: self.log.error("Error opening NFC reader") else: self.log.debug("Opened NFC reader " + self.LIBNFC_READER) self.log.debug("Initing NFC reader") self.libnfc.nfc_initiator_init(self.device) - if RFIDIOtconfig.debug: + if rfidiotglobals.Debug: self.log.debug("Configuring NFC reader") # Drop the field for a while @@ -343,29 +345,29 @@ def configure(self): def deconfigure(self): if self.device != None: - if RFIDIOtconfig.debug: + if rfidiotglobals.Debug: self.log.debug("Deconfiguring NFC reader") #self.powerOff() self.libnfc.nfc_close(self.device) - if RFIDIOtconfig.debug: + if rfidiotglobals.Debug: self.log.debug("Disconnected NFC reader") self.device == None def powerOn(self): self.libnfc.nfc_device_set_property_bool(self.device, NP_ACTIVATE_FIELD, True) - if RFIDIOtconfig.debug: + if rfidiotglobals.Debug: self.log.debug("Powered up field") self.poweredUp = True def powerOff(self): self.libnfc.nfc_device_set_property_bool(self.device, NP_ACTIVATE_FIELD, False) - if RFIDIOtconfig.debug: + if rfidiotglobals.Debug: self.log.debug("Powered down field") self.poweredUp = False def selectISO14443A(self): """Detect and initialise an ISO14443A card, returns an ISO14443A() object.""" - if RFIDIOtconfig.debug: + if rfidiotglobals.Debug: self.log.debug("Polling for ISO14443A cards") #r = self.libnfc.nfc_initiator_select_tag(self.device, IM_ISO14443A_106, None, None, ctypes.byref(ti)) #r = self.libnfc.nfc_initiator_init(self.device) @@ -406,18 +408,18 @@ def sendAPDU(self, apdu): rxAPDU = ctypes.c_ubyte * MAX_FRAME_LEN rx = rxAPDU() - if RFIDIOtconfig.debug: + if rfidiotglobals.Debug: self.log.debug("Sending %d byte APDU: %s" % (len(tx),"".join(["%02x" % x for x in tx]))) rxlen = self.libnfc.nfc_initiator_transceive_bytes(self.device, ctypes.byref(tx), ctypes.c_size_t(len(tx)), ctypes.byref(rx), ctypes.c_size_t(len(rx)), -1) - if RFIDIOtconfig.debug: + if rfidiotglobals.Debug: self.log.debug('APDU r = ' + str(r)) if rxlen < 0: - if RFIDIOtconfig.debug: + if rfidiotglobals.Debug: self.log.error("Error sending/recieving APDU") return False, rxlen else: - if RFIDIOtconfig.debug: + if rfidiotglobals.Debug: self.log.debug("Recieved %d byte APDU: " % rxlen, rx[:]) rxAPDU = "".join(["%02x" % x for x in rx[:rxlen]]) return True, rxAPDU diff --git a/rfidiot/rfidiotglobals.py b/rfidiot/rfidiotglobals.py new file mode 100644 index 0000000..5beaf15 --- /dev/null +++ b/rfidiot/rfidiotglobals.py @@ -0,0 +1 @@ +Debug=False diff --git a/send_apdu.py b/send_apdu.py index 95ef750..9d7ee4d 100644 --- a/send_apdu.py +++ b/send_apdu.py @@ -23,16 +23,17 @@ # along with this program. If not, see . # -import RFIDIOtconfig +import rfidiot import sys import os try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -card.info('send_apdu v0.1') +card.info('send_apdu v0.1a') card.select() print '\nID: ' + card.uid print ' Data:' diff --git a/setup.py b/setup.py index 044ae95..a0b3b77 100644 --- a/setup.py +++ b/setup.py @@ -1,17 +1,17 @@ from distutils.core import setup, Extension -packages= ['RFIDIOt'] +packages= ['rfidiot'] scripts = ['cardselect.py', 'ChAP.py', 'copytag.py', 'demotag.py', 'eeprom.py', 'fdxbnum.py', 'formatmifare1kvalue.py', 'froschtest.py', 'hidprox.py', 'hitag2brute.py', 'hitag2reset.py', 'isotype.py', 'jcopmifare.py', 'jcopsetatrhist.py', 'jcoptool.py', 'lfxtype.py', 'loginall.py', 'mifarekeys.py', 'mrpkey.py', 'multiselect.py', 'pn532emulate.py', - 'pn532mitm.py', 'pn532.py', 'q5reset.py', 'readlfx.py', 'readmifare1k.py', + 'pn532mitm.py', 'q5reset.py', 'readlfx.py', 'readmifare1k.py', 'readmifaresimple.py', 'readmifareultra.py', 'readtag.py', 'send_apdu.py', 'sod.py', 'transit.py', 'unique.py', 'writelfx.py', 'writemifare1k.py', 'testacg.sh', 'testlahf.sh' ] -setup (name = 'RFIDIOt', +setup (name = 'rfidiot', version = '1.0', description = "RFID IO tools", author = 'Adam Laurie', diff --git a/transit.py b/transit.py index 2f72e94..51c0f84 100755 --- a/transit.py +++ b/transit.py @@ -21,20 +21,21 @@ # -import RFIDIOtconfig +import rfidiot import sys import os import string try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -args= RFIDIOtconfig.args -help= RFIDIOtconfig.help +args= rfidiot.args +help= rfidiot.help -card.info('transit v0.1a') +card.info('transit v0.1b') precoded= False diff --git a/unique.py b/unique.py index c897b1e..e32325e 100755 --- a/unique.py +++ b/unique.py @@ -25,21 +25,22 @@ # -import RFIDIOtconfig +import rfidiot import sys import os import string import time try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -args= RFIDIOtconfig.args -help= RFIDIOtconfig.help +args= rfidiot.args +help= rfidiot.help -card.info('unique v0.1k') +card.info('unique v0.1l') # Q5 config block Q5CFB='e601f004' diff --git a/writelfx.py b/writelfx.py index bb444a9..0919f68 100755 --- a/writelfx.py +++ b/writelfx.py @@ -20,17 +20,18 @@ # GNU General Public License for more details. # -import RFIDIOtconfig +import rfidiot import sys import os try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -args= RFIDIOtconfig.args -help= RFIDIOtconfig.help +args= rfidiot.args +help= rfidiot.help Q5Mod= { '000':'Manchester',\ '001':'PSK 1',\ @@ -41,7 +42,7 @@ '110':'Biphase',\ '111':'NRZ / direct'} -card.info('writelfx v0.1b') +card.info('writelfx v0.1c') # force card type if specified if len(args) > 0: diff --git a/writemifare1k.py b/writemifare1k.py index 156aa17..af404e0 100755 --- a/writemifare1k.py +++ b/writemifare1k.py @@ -21,21 +21,22 @@ # -import RFIDIOtconfig +import rfidiot import sys import random import string import os try: - card= RFIDIOtconfig.card + card= rfidiot.card except: + print "Couldn't open reader!" os._exit(True) -args= RFIDIOtconfig.args -help= RFIDIOtconfig.help +args= rfidiot.args +help= rfidiot.help -card.info('writemifare1k v0.1e') +card.info('writemifare1k v0.1f') card.select() print 'Card ID: ' + card.uid while True: