blob: 08d2c84be9a84dce45831bc2f7d81304d3545462 [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 Quast80050a52015-04-04 20:00:15 +02008import mitm
Christina Quastf2582fc2015-03-06 19:09:35 +01009
10import usb.core
11import usb.util
12
13import hashlib
14import os
15import random
16import re
17
18cmd1 = {0x00, 0x10, 0x00, 0x00}
19cmd2 = {0x00, 0x20, 0x00, 0x00, 0x02}
20cmd_poweron = {0x62, 0x62, 0x00, 0x00}
21cmd_poweroff = {0x63, 0x63, 0x00, 0x00}
22cmd_get_slot_stat = {0x65, 0x65, 0x00, 0x00}
23cmd_get_param = {0x00, 0x6C, 0x00, 0x00}
24
25class 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 Quast4bcc0232015-03-24 21:59:32 +010045def 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 Quastf2582fc2015-03-06 19:09:35 +010052
53# main code
54def main():
55 parser = argparse.ArgumentParser()
Christina Quast452aee32015-04-03 13:22:18 +020056 parser.add_argument("-C", "--conf", type=int, choices=[1, 2, 3, 4], help="Set USB config")
Christina Quastf2582fc2015-03-06 19:09:35 +010057 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 Quast31b5b4e2015-03-09 17:15:13 +010061 parser.add_argument("-S", "--select_file", help="Transmit SELECT cmd!", action='store_true')
Christina Quast381d0fc2015-03-15 15:13:38 +010062 parser.add_argument("-p", "--phone", help="Emulates simcard", action='store_true')
Christina Quast80050a52015-04-04 20:00:15 +020063 parser.add_argument("-m", "--mitm", help="Intercept communication (MITM)", action='store_true')
Christina Quastf2582fc2015-03-06 19:09:35 +010064
65 args = parser.parse_args()
66 print("args: ", args)
67
Christina Quast381d0fc2015-03-15 15:13:38 +010068
Christina Quastf2582fc2015-03-06 19:09:35 +010069# FIXME: why is it a ccid function?
70 if args.conf is not None:
Christina Quast4bcc0232015-03-24 21:59:32 +010071#FIXME: Change means to find devices
72 dev = find_dev()
73 dev.set_configuration(args.conf)
Christina Quastf2582fc2015-03-06 19:09:35 +010074
75 if args.read_bin is True:
76 ccid.pySim_read()
77
78 if args.cmd is not None:
Christina Quast4bcc0232015-03-24 21:59:32 +010079#FIXME: Change means to find devices
Christina Quastf2582fc2015-03-06 19:09:35 +010080 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 Quast31b5b4e2015-03-09 17:15:13 +010084# ret = dev.read(0x83, 64, 100)
Christina Quastf2582fc2015-03-06 19:09:35 +010085 print(ret)
86 if args.sniff is True:
87 sniffer.sniff()
Christina Quast31b5b4e2015-03-09 17:15:13 +010088 if args.select_file is True:
89 ccid_select.select()
Christina Quast381d0fc2015-03-15 15:13:38 +010090 if args.phone is True:
91 phone.emulate_sim()
Christina Quast80050a52015-04-04 20:00:15 +020092 if args.mitm is True:
93 mitm.do_mitm()
Christina Quast31b5b4e2015-03-09 17:15:13 +010094
Christina Quastf2582fc2015-03-06 19:09:35 +010095 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
110main()