mirror of
https://gitea.osmocom.org/sim-card/simtrace2.git
synced 2026-03-24 01:08:32 +03:00
array to hex function, reduced usb timeout
This commit is contained in:
@@ -5,6 +5,8 @@ import smartcard.util
|
|||||||
|
|
||||||
import array
|
import array
|
||||||
|
|
||||||
|
from util import HEX
|
||||||
|
|
||||||
class SmartcardException(Exception):
|
class SmartcardException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -22,7 +24,7 @@ class SmartcardConnection:
|
|||||||
print 'Reader:', reader
|
print 'Reader:', reader
|
||||||
print 'State:', state
|
print 'State:', state
|
||||||
print 'Protocol:', protocol
|
print 'Protocol:', protocol
|
||||||
print 'ATR:', smartcard.util.toHexString(atr, smartcard.util.HEX)
|
print 'ATR:', HEX(atr)
|
||||||
return array.array('B', atr)
|
return array.array('B', atr)
|
||||||
|
|
||||||
def reset_card(self):
|
def reset_card(self):
|
||||||
@@ -67,14 +69,13 @@ class SmartcardConnection:
|
|||||||
print 'Released context.'
|
print 'Released context.'
|
||||||
|
|
||||||
def send_receive_cmd(self, cmd):
|
def send_receive_cmd(self, cmd):
|
||||||
print("Cmd: ")
|
print("Cmd to SIM: " + HEX(cmd))
|
||||||
hresult, resp = SCardTransmit(self.hcard, self.dwActiveProtocol,
|
hresult, resp = SCardTransmit(self.hcard, self.dwActiveProtocol,
|
||||||
cmd.tolist())
|
cmd.tolist())
|
||||||
if hresult != SCARD_S_SUCCESS:
|
if hresult != SCARD_S_SUCCESS:
|
||||||
raise SmartcardException('Failed to transmit: ' +
|
raise SmartcardException('Failed to transmit: ' +
|
||||||
SCardGetErrorMessage(hresult))
|
SCardGetErrorMessage(hresult))
|
||||||
print 'Ans: ' + smartcard.util.toHexString(resp,
|
print 'SIM Ans: ' + HEX(resp)
|
||||||
smartcard.util.HEX)
|
|
||||||
return array.array('B', resp)
|
return array.array('B', resp)
|
||||||
|
|
||||||
def disconnect_card(self):
|
def disconnect_card(self):
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import phone
|
|||||||
|
|
||||||
from contextlib import closing
|
from contextlib import closing
|
||||||
|
|
||||||
|
from util import HEX
|
||||||
|
|
||||||
def find_dev():
|
def find_dev():
|
||||||
dev = usb.core.find(idVendor=0x03eb, idProduct=0x6004)
|
dev = usb.core.find(idVendor=0x03eb, idProduct=0x6004)
|
||||||
@@ -36,14 +37,15 @@ ERR_TIMEOUT = 110
|
|||||||
|
|
||||||
def poll_ep(dev, ep):
|
def poll_ep(dev, ep):
|
||||||
try:
|
try:
|
||||||
return dev.read(ep, 64, 1000)
|
return dev.read(ep, 64, 100)
|
||||||
except usb.core.USBError as e:
|
except usb.core.USBError as e:
|
||||||
if e.errno != ERR_TIMEOUT:
|
if e.errno != ERR_TIMEOUT:
|
||||||
raise
|
raise
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def write_phone(dev, resp):
|
def write_phone(dev, resp):
|
||||||
dev.write(PHONE_WR, resp, 1000)
|
print("WR: ", HEX(resp))
|
||||||
|
dev.write(PHONE_WR, resp, 100)
|
||||||
|
|
||||||
def do_mitm():
|
def do_mitm():
|
||||||
dev = find_dev()
|
dev = find_dev()
|
||||||
@@ -52,14 +54,15 @@ def do_mitm():
|
|||||||
while True:
|
while True:
|
||||||
cmd = poll_ep(dev, PHONE_INT)
|
cmd = poll_ep(dev, PHONE_INT)
|
||||||
if cmd is not None:
|
if cmd is not None:
|
||||||
print(cmd)
|
print("Int line ", HEX(cmd))
|
||||||
assert cmd[0] == ord('R')
|
assert cmd[0] == ord('R')
|
||||||
# FIXME: restart card anyways?
|
# FIXME: restart card anyways?
|
||||||
# sm_con.reset_card()
|
# sm_con.reset_card()
|
||||||
|
print("Write atr: ", HEX(atr))
|
||||||
write_phone(dev, atr)
|
write_phone(dev, atr)
|
||||||
|
|
||||||
cmd = poll_ep(dev, PHONE_RD)
|
cmd = poll_ep(dev, PHONE_RD)
|
||||||
if cmd is not None:
|
if cmd is not None:
|
||||||
print(cmd)
|
print("RD: ", HEX(cmd))
|
||||||
sim_data = sm_con.send_receive_cmd(cmd)
|
sim_data = sm_con.send_receive_cmd(cmd)
|
||||||
write_phone(dev, sim_data)
|
write_phone(dev, sim_data)
|
||||||
|
|||||||
5
usb_application/util.py
Normal file
5
usb_application/util.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
def HEX(vals):
|
||||||
|
if vals is not None:
|
||||||
|
return ' '.join('%.2x'%x for x in vals)
|
||||||
|
|
||||||
Reference in New Issue
Block a user