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