diff --git a/usb_application/ccid_raw.py b/usb_application/ccid_raw.py index 1a51b687..4447e348 100755 --- a/usb_application/ccid_raw.py +++ b/usb_application/ccid_raw.py @@ -5,6 +5,8 @@ import smartcard.util import array +from util import HEX + class SmartcardException(Exception): pass @@ -22,7 +24,7 @@ class SmartcardConnection: print 'Reader:', reader print 'State:', state print 'Protocol:', protocol - print 'ATR:', smartcard.util.toHexString(atr, smartcard.util.HEX) + print 'ATR:', HEX(atr) return array.array('B', atr) def reset_card(self): @@ -67,14 +69,13 @@ class SmartcardConnection: print 'Released context.' def send_receive_cmd(self, cmd): - print("Cmd: ") + print("Cmd to SIM: " + HEX(cmd)) hresult, resp = SCardTransmit(self.hcard, self.dwActiveProtocol, cmd.tolist()) if hresult != SCARD_S_SUCCESS: raise SmartcardException('Failed to transmit: ' + SCardGetErrorMessage(hresult)) - print 'Ans: ' + smartcard.util.toHexString(resp, - smartcard.util.HEX) + print 'SIM Ans: ' + HEX(resp) return array.array('B', resp) def disconnect_card(self): diff --git a/usb_application/mitm.py b/usb_application/mitm.py index 962bf88f..4a43d4e0 100755 --- a/usb_application/mitm.py +++ b/usb_application/mitm.py @@ -6,6 +6,7 @@ import phone from contextlib import closing +from util import HEX def find_dev(): dev = usb.core.find(idVendor=0x03eb, idProduct=0x6004) @@ -36,14 +37,15 @@ ERR_TIMEOUT = 110 def poll_ep(dev, ep): try: - return dev.read(ep, 64, 1000) + return dev.read(ep, 64, 100) except usb.core.USBError as e: if e.errno != ERR_TIMEOUT: raise return None def write_phone(dev, resp): - dev.write(PHONE_WR, resp, 1000) + print("WR: ", HEX(resp)) + dev.write(PHONE_WR, resp, 100) def do_mitm(): dev = find_dev() @@ -52,14 +54,15 @@ def do_mitm(): while True: cmd = poll_ep(dev, PHONE_INT) if cmd is not None: - print(cmd) + print("Int line ", HEX(cmd)) assert cmd[0] == ord('R') # FIXME: restart card anyways? # sm_con.reset_card() + print("Write atr: ", HEX(atr)) write_phone(dev, atr) cmd = poll_ep(dev, PHONE_RD) if cmd is not None: - print(cmd) + print("RD: ", HEX(cmd)) sim_data = sm_con.send_receive_cmd(cmd) write_phone(dev, sim_data) diff --git a/usb_application/util.py b/usb_application/util.py new file mode 100644 index 00000000..b6ef5a7f --- /dev/null +++ b/usb_application/util.py @@ -0,0 +1,5 @@ + +def HEX(vals): + if vals is not None: + return ' '.join('%.2x'%x for x in vals) +