Christina Quast | 69d1f90 | 2015-04-03 11:41:23 +0200 | [diff] [blame] | 1 | import usb.core |
| 2 | import usb.util |
| 3 | |
Christina Quast | 95270b1 | 2015-04-04 19:59:03 +0200 | [diff] [blame] | 4 | import ccid_raw |
| 5 | import phone |
| 6 | |
Christina Quast | 69d1f90 | 2015-04-03 11:41:23 +0200 | [diff] [blame] | 7 | def find_dev(): |
| 8 | dev = usb.core.find(idVendor=0x03eb, idProduct=0x6004) |
| 9 | if dev is None: |
| 10 | raise ValueError("Device not found") |
| 11 | else: |
| 12 | print("Found device") |
| 13 | return dev |
| 14 | |
| 15 | |
| 16 | SIM_WR = 0x1 |
| 17 | SIM_RD = 0x82 |
| 18 | SIM_INT = 0x83 |
| 19 | |
| 20 | PHONE_WR = 0x4 |
| 21 | PHONE_RD = 0x85 |
| 22 | PHONE_INT = 0x86 |
| 23 | |
| 24 | def check_msg_phone(): |
Christina Quast | 95270b1 | 2015-04-04 19:59:03 +0200 | [diff] [blame] | 25 | cmd = dev.read(PHONE_RD, 64, 100) |
| 26 | if cmd is not None: |
Christina Quast | 69d1f90 | 2015-04-03 11:41:23 +0200 | [diff] [blame] | 27 | print("Phone sent: " + cmd) |
| 28 | return cmd |
Christina Quast | 95270b1 | 2015-04-04 19:59:03 +0200 | [diff] [blame] | 29 | cmd = dev.read(PHONE_INT, 64, 100) |
| 30 | if cmd is not None: |
Christina Quast | 69d1f90 | 2015-04-03 11:41:23 +0200 | [diff] [blame] | 31 | print("Phone sent int") |
| 32 | return cmd |
| 33 | |
| 34 | def write_phone(resp): |
Christina Quast | 95270b1 | 2015-04-04 19:59:03 +0200 | [diff] [blame] | 35 | dev.write(PHONE_WR, resp, 100) |
Christina Quast | 69d1f90 | 2015-04-03 11:41:23 +0200 | [diff] [blame] | 36 | |
| 37 | def write_sim(data): |
| 38 | return do_intercept(data, dwActiveProtocol) |
| 39 | |
Christina Quast | 95270b1 | 2015-04-04 19:59:03 +0200 | [diff] [blame] | 40 | def do_mitm(): |
Christina Quast | 69d1f90 | 2015-04-03 11:41:23 +0200 | [diff] [blame] | 41 | dev = find_dev() |
Christina Quast | 95270b1 | 2015-04-04 19:59:03 +0200 | [diff] [blame] | 42 | hcard, hcontext, dwActiveProtocol = ccid_raw.ccid_raw_init() |
Christina Quast | 69d1f90 | 2015-04-03 11:41:23 +0200 | [diff] [blame] | 43 | |
Christina Quast | 95270b1 | 2015-04-04 19:59:03 +0200 | [diff] [blame] | 44 | try: |
| 45 | try: |
| 46 | while True: |
| 47 | cmd = check_msg_phone() |
| 48 | if (cmd is not None): |
| 49 | resp = write_sim(cmd, dwActiveProtocol) |
| 50 | if (resp is not None): |
| 51 | write_phone(resp) |
| 52 | else: |
| 53 | print("No responses.") |
| 54 | finally: |
| 55 | ccid_raw.ccid_raw_exit(hcard, hcontext) |
Christina Quast | 69d1f90 | 2015-04-03 11:41:23 +0200 | [diff] [blame] | 56 | |
| 57 | except usb.USBError as e: |
Christina Quast | 95270b1 | 2015-04-04 19:59:03 +0200 | [diff] [blame] | 58 | print(e) |
Christina Quast | 69d1f90 | 2015-04-03 11:41:23 +0200 | [diff] [blame] | 59 | pass |