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