forked from Fluepke/konnektor-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_400m_euro.py
executable file
·103 lines (87 loc) · 2.77 KB
/
save_400m_euro.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
from os.path import dirname, realpath
import os
import sys
import socket
import errno
import struct
import logging
from argparse import ArgumentParser
sys.path.append(dirname(realpath(__file__)))
from virtualsmartcard.utils import inttostring, hexdump
from patch_card.vpc import (
vpc_connect,
vpc_send,
vpc_recv,
)
def parse_args():
"""Parse commandline arguments"""
parser = ArgumentParser()
parser.add_argument(
"-H", "--host", default="localhost")
parser.add_argument(
"-p", "--port", type=int, default=35963)
parser.add_argument(
"-s", "--pcsc-sock-name",
default="/var/run/old_pcscd.comm")
parser.add_argument(
"-r", "--reader-num", type=int, default=0)
return parser.parse_args()
# From VirtualSmartcard.py
VPCD_CTRL_LEN = 1
VPCD_CTRL_OFF = 0
VPCD_CTRL_ON = 1
VPCD_CTRL_RESET = 2
VPCD_CTRL_ATR = 4
def main(args):
"""
Virtual Smartcard with Filesystem Interception
"""
# Setup logging
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s [%(levelname)s] %(message)s",
datefmt="%d.%m.%Y %H:%M:%S")
# Patch environment
os.environ["PCSCLITE_CSOCK_NAME"] = args.pcsc_sock_name
# Connect to virtual card server
sock = vpc_connect(args.host, args.port)
# Import here, so the environment is patched before
# loading the python `smartcard` package
from patch_card.cards import PatchCard
card_os = PatchCard(args.reader_num)
while True:
try:
(size, msg) = vpc_recv(sock)
except socket.error as err:
logging.info(err)
sys.exit()
if not size:
logging.error(
"error in communication protocol (missing size parameter)")
elif size == VPCD_CTRL_LEN:
if msg == inttostring(VPCD_CTRL_OFF):
logging.info("power down")
card_os.powerDown()
elif msg == inttostring(VPCD_CTRL_ON):
logging.info("power up")
card_os.powerUp()
elif msg == inttostring(VPCD_CTRL_RESET):
logging.info("reset")
card_os.reset()
elif msg == inttostring(VPCD_CTRL_ATR):
vpc_send(sock, card_os.getATR())
else:
logging.info("unknown control command")
else:
if size != len(msg):
logging.error(
"expected %u bytes, but received only %u",
size, len(msg))
answer = card_os.execute(msg)
logging.info("response APDU (%d bytes):\n %s\n",
len(answer), hexdump(answer, indent=2))
vpc_send(sock, answer)
if __name__ == "__main__":
args = parse_args()
main(args)