blob: 9730cf5ac5d96b15d898805ab7368cac835a0c20 [file] [log] [blame]
Christina Quastf2582fc2015-03-06 19:09:35 +01001#!/usr/bin/env python
2
3import argparse
4import sniffer
5import ccid
Christina Quast31b5b4e2015-03-09 17:15:13 +01006import ccid_select
Christina Quast381d0fc2015-03-15 15:13:38 +01007import phone
Christina Quastf2582fc2015-03-06 19:09:35 +01008
9import usb.core
10import usb.util
11
12import hashlib
13import os
14import random
15import re
16
17cmd1 = {0x00, 0x10, 0x00, 0x00}
18cmd2 = {0x00, 0x20, 0x00, 0x00, 0x02}
19cmd_poweron = {0x62, 0x62, 0x00, 0x00}
20cmd_poweroff = {0x63, 0x63, 0x00, 0x00}
21cmd_get_slot_stat = {0x65, 0x65, 0x00, 0x00}
22cmd_get_param = {0x00, 0x6C, 0x00, 0x00}
23
24class 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 Quast4bcc0232015-03-24 21:59:32 +010044def 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 Quastf2582fc2015-03-06 19:09:35 +010051
52# main code
53def 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 Quast31b5b4e2015-03-09 17:15:13 +010060 parser.add_argument("-S", "--select_file", help="Transmit SELECT cmd!", action='store_true')
Christina Quast381d0fc2015-03-15 15:13:38 +010061 parser.add_argument("-p", "--phone", help="Emulates simcard", action='store_true')
Christina Quastf2582fc2015-03-06 19:09:35 +010062
63 args = parser.parse_args()
64 print("args: ", args)
65
Christina Quast381d0fc2015-03-15 15:13:38 +010066
Christina Quastf2582fc2015-03-06 19:09:35 +010067# FIXME: why is it a ccid function?
68 if args.conf is not None:
Christina Quast4bcc0232015-03-24 21:59:32 +010069#FIXME: Change means to find devices
70 dev = find_dev()
71 dev.set_configuration(args.conf)
Christina Quastf2582fc2015-03-06 19:09:35 +010072
73 if args.read_bin is True:
74 ccid.pySim_read()
75
76 if args.cmd is not None:
Christina Quast4bcc0232015-03-24 21:59:32 +010077#FIXME: Change means to find devices
Christina Quastf2582fc2015-03-06 19:09:35 +010078 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 Quast31b5b4e2015-03-09 17:15:13 +010082# ret = dev.read(0x83, 64, 100)
Christina Quastf2582fc2015-03-06 19:09:35 +010083 print(ret)
84 if args.sniff is True:
85 sniffer.sniff()
Christina Quast31b5b4e2015-03-09 17:15:13 +010086 if args.select_file is True:
87 ccid_select.select()
Christina Quast381d0fc2015-03-15 15:13:38 +010088 if args.phone is True:
89 phone.emulate_sim()
Christina Quast31b5b4e2015-03-09 17:15:13 +010090
Christina Quastf2582fc2015-03-06 19:09:35 +010091 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
106main()