-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcard_proxy.py
executable file
·54 lines (47 loc) · 1.55 KB
/
card_proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
"""
Listens on 127.0.0.1:21111 and forwards all the data to the smartcard reader.
Useful for testing Specter-DIY simulator with a real card.
"""
import socket
from smartcard.System import readers
from smartcard.CardConnection import CardConnection
PORT = 21111
HOST = '127.0.0.1'
class ISOException(Exception):
def __init__(self, code):
self.code = code
def get_reader():
"""Returns first found reader """
rarr=readers()
if len(rarr) == 0:
raise RuntimeError("Reader not found")
return rarr[0]
def get_connection(reader=None, protocol=CardConnection.T1_protocol):
"""Establish connection with a card"""
if reader is None:
reader = get_reader()
connection = reader.createConnection()
connection.connect(protocol)
return connection
def main():
cardconn = get_connection()
print(f"Smartcard proxy started. Connect to {HOST}:{PORT}")
while True:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
apdu = conn.recv(1024)
print(">>>", apdu.hex())
if not apdu:
break
data, *sw = cardconn.transmit(list(apdu))
res = bytes(data)+bytes(sw)
print("<<<", res.hex())
conn.sendall(res)
if __name__ == '__main__':
main()