Christina Quast | f2582fc | 2015-03-06 19:09:35 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import argparse |
| 4 | import sniffer |
| 5 | import ccid |
Christina Quast | 31b5b4e | 2015-03-09 17:15:13 +0100 | [diff] [blame] | 6 | import ccid_select |
Christina Quast | 381d0fc | 2015-03-15 15:13:38 +0100 | [diff] [blame] | 7 | import phone |
Christina Quast | f2582fc | 2015-03-06 19:09:35 +0100 | [diff] [blame] | 8 | |
| 9 | import usb.core |
| 10 | import usb.util |
| 11 | |
| 12 | import hashlib |
| 13 | import os |
| 14 | import random |
| 15 | import re |
| 16 | |
| 17 | cmd1 = {0x00, 0x10, 0x00, 0x00} |
| 18 | cmd2 = {0x00, 0x20, 0x00, 0x00, 0x02} |
| 19 | cmd_poweron = {0x62, 0x62, 0x00, 0x00} |
| 20 | cmd_poweroff = {0x63, 0x63, 0x00, 0x00} |
| 21 | cmd_get_slot_stat = {0x65, 0x65, 0x00, 0x00} |
| 22 | cmd_get_param = {0x00, 0x6C, 0x00, 0x00} |
| 23 | |
| 24 | class find_class(object): |
| 25 | def __init__(self, class_): |
| 26 | self._class = class_ |
| 27 | def __call__(self, device): |
| 28 | # first, let's check the device |
| 29 | if device.bDeviceClass == self._class: |
| 30 | return True |
| 31 | # ok, transverse all devices to find an |
| 32 | # interface that matches our class |
| 33 | for cfg in device: |
| 34 | # find_descriptor: what's it? |
| 35 | intf = usb.util.find_descriptor( |
| 36 | cfg, |
| 37 | bInterfaceClass=self._class |
| 38 | ) |
| 39 | if intf is not None: |
| 40 | return True |
| 41 | |
| 42 | return False |
| 43 | |
Christina Quast | 4bcc023 | 2015-03-24 21:59:32 +0100 | [diff] [blame^] | 44 | def find_dev(): |
| 45 | dev = usb.core.find(idVendor=0x03eb, idProduct=0x6004) |
| 46 | if dev is None: |
| 47 | raise ValueError("Device not found") |
| 48 | else: |
| 49 | print("Found device") |
| 50 | return dev |
Christina Quast | f2582fc | 2015-03-06 19:09:35 +0100 | [diff] [blame] | 51 | |
| 52 | # main code |
| 53 | def main(): |
| 54 | parser = argparse.ArgumentParser() |
| 55 | parser.add_argument("-C", "--conf", type=int, choices=[1, 2, 3], help="Set USB config") |
| 56 | parser.add_argument("-b", "--read_bin", help="read ICCID, IMSI, etc.", action='store_true') |
| 57 | parser.add_argument("-c", "--cmd", help="cmds to send to sim card (Not supported yet)", |
| 58 | choices=["cmd1", "cmd2", "cmd_poweron", "cmd_poweroff", "cmd_get_slot_stat", "cmd_get_param"]) |
| 59 | parser.add_argument("-s", "--sniff", help="Sniff communication!", action='store_true') |
Christina Quast | 31b5b4e | 2015-03-09 17:15:13 +0100 | [diff] [blame] | 60 | parser.add_argument("-S", "--select_file", help="Transmit SELECT cmd!", action='store_true') |
Christina Quast | 381d0fc | 2015-03-15 15:13:38 +0100 | [diff] [blame] | 61 | parser.add_argument("-p", "--phone", help="Emulates simcard", action='store_true') |
Christina Quast | f2582fc | 2015-03-06 19:09:35 +0100 | [diff] [blame] | 62 | |
| 63 | args = parser.parse_args() |
| 64 | print("args: ", args) |
| 65 | |
Christina Quast | 381d0fc | 2015-03-15 15:13:38 +0100 | [diff] [blame] | 66 | |
Christina Quast | f2582fc | 2015-03-06 19:09:35 +0100 | [diff] [blame] | 67 | # FIXME: why is it a ccid function? |
| 68 | if args.conf is not None: |
Christina Quast | 4bcc023 | 2015-03-24 21:59:32 +0100 | [diff] [blame^] | 69 | #FIXME: Change means to find devices |
| 70 | dev = find_dev() |
| 71 | dev.set_configuration(args.conf) |
Christina Quast | f2582fc | 2015-03-06 19:09:35 +0100 | [diff] [blame] | 72 | |
| 73 | if args.read_bin is True: |
| 74 | ccid.pySim_read() |
| 75 | |
| 76 | if args.cmd is not None: |
Christina Quast | 4bcc023 | 2015-03-24 21:59:32 +0100 | [diff] [blame^] | 77 | #FIXME: Change means to find devices |
Christina Quast | f2582fc | 2015-03-06 19:09:35 +0100 | [diff] [blame] | 78 | devs = usb.core.find(find_all=1, custom_match=find_class(0xb)) # 0xb = Smartcard |
| 79 | for dev in devs: |
| 80 | dev.write(0x1, args.cmd) |
| 81 | ret = dev.read(0x82, 64) |
Christina Quast | 31b5b4e | 2015-03-09 17:15:13 +0100 | [diff] [blame] | 82 | # ret = dev.read(0x83, 64, 100) |
Christina Quast | f2582fc | 2015-03-06 19:09:35 +0100 | [diff] [blame] | 83 | print(ret) |
| 84 | if args.sniff is True: |
| 85 | sniffer.sniff() |
Christina Quast | 31b5b4e | 2015-03-09 17:15:13 +0100 | [diff] [blame] | 86 | if args.select_file is True: |
| 87 | ccid_select.select() |
Christina Quast | 381d0fc | 2015-03-15 15:13:38 +0100 | [diff] [blame] | 88 | if args.phone is True: |
| 89 | phone.emulate_sim() |
Christina Quast | 31b5b4e | 2015-03-09 17:15:13 +0100 | [diff] [blame] | 90 | |
Christina Quast | f2582fc | 2015-03-06 19:09:35 +0100 | [diff] [blame] | 91 | return |
| 92 | |
| 93 | # (epi, epo) = find_eps(dev) |
| 94 | while True: |
| 95 | #ep_out.write("Hello") |
| 96 | try: |
| 97 | ans = dev.read(0x82, 64, 1000) |
| 98 | print("".join("%02x " % b for b in ans)) |
| 99 | except KeyboardInterrupt: |
| 100 | print("Bye") |
| 101 | sys.exit() |
| 102 | except: |
| 103 | print("Timeout") |
| 104 | # print(ep_in.read(1, 5000)); |
| 105 | |
| 106 | main() |