From 158c1dd448958d3e2f9079094d9cf852e67cd37c Mon Sep 17 00:00:00 2001 From: Christina Quast Date: Fri, 17 Apr 2015 20:19:29 +0200 Subject: [PATCH] simtrace.py: Use mitm.py in phone and mitm mode --- usb_application/mitm.py | 9 ++++-- usb_application/simtrace.py | 11 ++----- usb_application/smartcard_emulator.py | 45 +++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 usb_application/smartcard_emulator.py diff --git a/usb_application/mitm.py b/usb_application/mitm.py index 4ae7b0e6..0c9efd85 100755 --- a/usb_application/mitm.py +++ b/usb_application/mitm.py @@ -2,6 +2,7 @@ import usb.core import usb.util from ccid_raw import SmartcardConnection +from smartcard_emulator import SmartCardEmulator from contextlib import closing @@ -43,9 +44,13 @@ def write_phone(dev, resp): print("WR: ", HEX(resp)) dev.write(PHONE_WR, resp, 10) -def do_mitm(): +def do_mitm(sim_emul=True): dev = find_dev() - with closing(SmartcardConnection()) as sm_con: + if sim_emul == True: + my_class = SmartCardEmulator + else: + my_class = SmartcardConnection + with closing(my_class()) as sm_con: atr = sm_con.getATR() apdus = [] diff --git a/usb_application/simtrace.py b/usb_application/simtrace.py index 1cb9c7ca..4b8f3c78 100755 --- a/usb_application/simtrace.py +++ b/usb_application/simtrace.py @@ -4,16 +4,11 @@ import argparse import sniffer import ccid import ccid_select -import phone import mitm import usb.core import usb.util - -import hashlib -import os -import random -import re +import sys import time cmd1 = {0x00, 0x10, 0x00, 0x00} @@ -91,9 +86,9 @@ def main(): if args.select_file is True: ccid_select.select() if args.phone is True: - phone.emulate_sim() + mitm.do_mitm(sim_emul=True) if args.mitm is True: - mitm.do_mitm() + mitm.do_mitm(sim_emul=False) return diff --git a/usb_application/smartcard_emulator.py b/usb_application/smartcard_emulator.py new file mode 100644 index 00000000..aa401255 --- /dev/null +++ b/usb_application/smartcard_emulator.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python + +import constants +import array + +INS = 1 +CNT = 4 + +class SmartCardEmulator: + def getATR(self): + return array.array('B', constants.ATR_SYSMOCOM2) + + def send_receive_cmd(self, cmd): + if len(cmd) == 5: # Received cmd from phone + if cmd[INS] == 0xA4: + resp = [cmd[INS]] # Respond with INS byte + elif cmd[INS] == 0xC0: + data = [0x00, 0x00, 0x00, 0x00, + 0x7F, 0x20, 0x02, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x09, 0x91, 0x00, 0x17, + 0x04, 0x00, 0x83, 0x8A, + 0x83, 0x8A] + SW = [0x90, 0x00] + resp = [cmd[INS]] + data + SW # Respond with INS byte + #state = WAIT_RST + else: + print("Unknown cmd") + resp = [0x60, 0x00] + elif len(cmd) == 2: + resp = [0x9F, 0x16] + else: + resp = [0x60, 0x00] + + print("Cmd, resp: ") + print("".join("%02x " % b for b in cmd)) + print("".join("%02x " % b for b in resp)) + + return array.array('B', resp) + + def reset_card(): + pass + + def close(self): + pass