More comfortable interface to brutefid

git-svn-id: svn+ssh://localhost/home/henryk/svn/cyberflex-shell/trunk@112 f711b948-2313-0410-aaa9-d29f33439f0b
This commit is contained in:
hploetz 2006-08-24 07:02:05 +00:00
parent dce8c9f90f
commit 97af2bd2ed
2 changed files with 79 additions and 6 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
import pycsc, utils, cards, TLV_utils, sys, binascii, time
import pycsc, utils, cards, TLV_utils, sys, binascii, time, getopt
STATUS_INTERVAL = 10
@ -11,11 +11,84 @@ top_level = None
start_time = time.time()
loop = 0
if __name__ == "__main__":
if len(sys.argv) > 1:
top_level = binascii.unhexlify("".join( sys.argv[1].split() ))
OPTIONS = "r:l"
LONG_OPTIONS = ["reader=", "list-readers"]
exit_now = False
reader = None
pycsc_card = pycsc.pycsc(protocol = pycsc.SCARD_PROTOCOL_ANY)
def list_readers():
for index, name in enumerate(pycsc.listReader()):
print "%i: %s" % (index, name)
def connect(reader = None):
"Open the connection to a card"
if reader is None:
reader = 0
if isinstance(reader, int) or reader.isdigit():
reader = int(reader)
readerName = pycsc.listReader()[reader]
else:
readerName = reader
newState = pycsc.getStatusChange(ReaderStates=[
{'Reader': readerName, 'CurrentState':pycsc.SCARD_STATE_UNAWARE}
]
)
print "Using reader: %s" % readerName
print "Card present: %s" % ((newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT) and "yes" or "no")
if not newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT:
print "Please insert card ..."
last_was_mute = False
while not newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT \
or newState[0]['EventState'] & pycsc.SCARD_STATE_MUTE:
try:
newState = pycsc.getStatusChange(ReaderStates=[
{'Reader': readerName, 'CurrentState':newState[0]['EventState']}
], Timeout = 100
) ## 100 ms latency from Ctrl-C to abort should be almost unnoticeable by the user
except pycsc.PycscException, e:
if e.args[0] == 'Command timeout.': pass ## ugly
else: raise
if newState[0]['EventState'] & pycsc.SCARD_STATE_MUTE:
if not last_was_mute:
print "Card is mute, please retry ..."
last_was_mute = True
else:
last_was_mute = False
print "Card present: %s" % ((newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT) and "yes" or "no")
print "ATR: %s" % utils.hexdump(newState[0]['Atr'], short = True)
return pycsc.pycsc(reader = readerName, protocol = pycsc.SCARD_PROTOCOL_ANY)
if __name__ == "__main__":
(options, arguments) = getopt.gnu_getopt(sys.argv[1:], OPTIONS, LONG_OPTIONS)
for (option, value) in options:
if option in ("-r","--reader"):
reader = value
if option in ("-l","--list-readers"):
list_readers()
exit_now = True
if exit_now:
sys.exit()
del exit_now
if len(arguments) > 0:
top_level = binascii.unhexlify("".join( ["".join(e.split()) for e in arguments] ))
pycsc_card = connect(reader)
card = cards.new_card_object(pycsc_card)
cards.generic_card.DEBUG = False

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Project SYSTEM "Project-3.7.dtd">
<!-- Project file for project cyberflex-shell -->
<!-- Saved: 2006-07-18, 13:42:27 -->
<!-- Saved: 2006-07-24, 17:00:49 -->
<!-- Copyright (C) 2006 Henryk Plötz, henryk@ploetzli.ch -->
<Project version="3.7">
<ProgLanguage mixed="0">Python</ProgLanguage>