blob: 1cb9c7cac619c0cd01996d180779e56bcf3a5392 [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
Christina Quast362a4da2015-04-09 13:39:38 +020017import time
Christina Quastf2582fc2015-03-06 19:09:35 +010018
19cmd1 = {0x00, 0x10, 0x00, 0x00}
20cmd2 = {0x00, 0x20, 0x00, 0x00, 0x02}
21cmd_poweron = {0x62, 0x62, 0x00, 0x00}
22cmd_poweroff = {0x63, 0x63, 0x00, 0x00}
23cmd_get_slot_stat = {0x65, 0x65, 0x00, 0x00}
24cmd_get_param = {0x00, 0x6C, 0x00, 0x00}
25
26class find_class(object):
27 def __init__(self, class_):
28 self._class = class_
29 def __call__(self, device):
30 # first, let's check the device
31 if device.bDeviceClass == self._class:
32 return True
33 # ok, transverse all devices to find an
34 # interface that matches our class
35 for cfg in device:
36 # find_descriptor: what's it?
37 intf = usb.util.find_descriptor(
38 cfg,
39 bInterfaceClass=self._class
40 )
41 if intf is not None:
42 return True
43
44 return False
45
Christina Quast4bcc0232015-03-24 21:59:32 +010046def find_dev():
47 dev = usb.core.find(idVendor=0x03eb, idProduct=0x6004)
48 if dev is None:
49 raise ValueError("Device not found")
50 else:
51 print("Found device")
52 return dev
Christina Quastf2582fc2015-03-06 19:09:35 +010053
54# main code
55def main():
56 parser = argparse.ArgumentParser()
Christina Quast452aee32015-04-03 13:22:18 +020057 parser.add_argument("-C", "--conf", type=int, choices=[1, 2, 3, 4], help="Set USB config")
Christina Quastf2582fc2015-03-06 19:09:35 +010058 parser.add_argument("-b", "--read_bin", help="read ICCID, IMSI, etc.", action='store_true')
59 parser.add_argument("-c", "--cmd", help="cmds to send to sim card (Not supported yet)",
60 choices=["cmd1", "cmd2", "cmd_poweron", "cmd_poweroff", "cmd_get_slot_stat", "cmd_get_param"])
61 parser.add_argument("-s", "--sniff", help="Sniff communication!", action='store_true')
Christina Quast31b5b4e2015-03-09 17:15:13 +010062 parser.add_argument("-S", "--select_file", help="Transmit SELECT cmd!", action='store_true')
Christina Quast381d0fc2015-03-15 15:13:38 +010063 parser.add_argument("-p", "--phone", help="Emulates simcard", action='store_true')
Christina Quast80050a52015-04-04 20:00:15 +020064 parser.add_argument("-m", "--mitm", help="Intercept communication (MITM)", action='store_true')
Christina Quastf2582fc2015-03-06 19:09:35 +010065
66 args = parser.parse_args()
67 print("args: ", args)
68
Christina Quast381d0fc2015-03-15 15:13:38 +010069
Christina Quastf2582fc2015-03-06 19:09:35 +010070# FIXME: why is it a ccid function?
71 if args.conf is not None:
Christina Quast4bcc0232015-03-24 21:59:32 +010072#FIXME: Change means to find devices
73 dev = find_dev()
74 dev.set_configuration(args.conf)
Christina Quast362a4da2015-04-09 13:39:38 +020075 # Give pcsclite time to find the device
76 time.sleep(1)
Christina Quastf2582fc2015-03-06 19:09:35 +010077
78 if args.read_bin is True:
79 ccid.pySim_read()
80
81 if args.cmd is not None:
Christina Quast4bcc0232015-03-24 21:59:32 +010082#FIXME: Change means to find devices
Christina Quastf2582fc2015-03-06 19:09:35 +010083 devs = usb.core.find(find_all=1, custom_match=find_class(0xb)) # 0xb = Smartcard
84 for dev in devs:
85 dev.write(0x1, args.cmd)
86 ret = dev.read(0x82, 64)
Christina Quast31b5b4e2015-03-09 17:15:13 +010087# ret = dev.read(0x83, 64, 100)
Christina Quastf2582fc2015-03-06 19:09:35 +010088 print(ret)
89 if args.sniff is True:
90 sniffer.sniff()
Christina Quast31b5b4e2015-03-09 17:15:13 +010091 if args.select_file is True:
92 ccid_select.select()
Christina Quast381d0fc2015-03-15 15:13:38 +010093 if args.phone is True:
94 phone.emulate_sim()
Christina Quast80050a52015-04-04 20:00:15 +020095 if args.mitm is True:
96 mitm.do_mitm()
Christina Quast31b5b4e2015-03-09 17:15:13 +010097
Christina Quastf2582fc2015-03-06 19:09:35 +010098 return
99
100# (epi, epo) = find_eps(dev)
101 while True:
102 #ep_out.write("Hello")
103 try:
104 ans = dev.read(0x82, 64, 1000)
105 print("".join("%02x " % b for b in ans))
106 except KeyboardInterrupt:
107 print("Bye")
108 sys.exit()
109 except:
110 print("Timeout")
111 # print(ep_in.read(1, 5000));
112
113main()