-
Notifications
You must be signed in to change notification settings - Fork 0
/
ultralight.py
313 lines (218 loc) · 7.8 KB
/
ultralight.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# A Python implementation for Mifare Ultralight cards.
#
# Copyright (c) 2015, Alexandre Hamelin <alexandre.hamelin gmail.com>
#
# This work is distributed under the LGPL license. See LICENSE.txt for details.
import sys
import os
__all__ = 'MFUCard MFUPage'.split()
class MFUCard:
def __init__(self, *, bytes=None, file=None, hexfile=None):
import builtins
if [bytes, file, hexfile].count(None) < 2:
raise RuntimeError('only one of bytes, file or hexfile'
' must be specified')
if bytes is not None:
if not isinstance(bytes, (builtins.bytes, bytearray)):
raise TypeError('invalid bytes parameter')
elif len(bytes) != 64:
raise ValueError('byte array must be 64 bytes long')
self._bytes = builtins.bytes(bytes)
elif file is not None:
# file can be any of a file object, a filename or a file descriptor
self._bytes = self._load(file, 'rb')
elif hexfile is not None:
# hexfile can be any of a file object, a filename or a file
# descriptor
content = self._load(hexfile, 'r', 1024)
content = (content.replace(b'\r', b'')
.replace(b'\n', b'')
.replace(b' ', b'')
.replace(b'\t', b''))
self._bytes = builtins.bytes(bytearray.fromhex(content.decode()))
else:
self._bytes = builtins.bytes(64)
assert type(self._bytes) is builtins.bytes
assert len(self._bytes) == 64
self._page_view_proxy = None
def _load(self, file, mode, count=64):
orig_file = None
if isinstance(file, str):
file = open(file, mode)
orig_file = file
if hasattr(file, 'fileno'):
file = file.fileno()
content = os.read(file, count)
if orig_file:
orig_file.close()
return content
def __iter__(self):
return iter(self._bytes)
def __len__(self):
return len(self._bytes)
def hexdump(self, file=sys.stdout):
for page in self.pages:
print('{:08x}'.format(int.from_bytes(page, 'big')), file=file)
def dump(self, file):
if isinstance(file, str):
with open(file, 'w') as fp:
fp.write(self._bytes)
elif hasattr(file, 'write'):
file.write(self._bytes)
else:
raise TypeError('invalid output file')
@property
def pages(self):
if self._page_view_proxy is None:
self._page_view_proxy = MFUPageViewProxy(self)
return self._page_view_proxy
def __getitem__(self, index):
if isinstance(index, (int, slice)):
return self._bytes[index]
else:
raise TypeError('invalid index: {}'.format(index))
@property
def id(self):
# 7-byte UID, excludes the two check bytes
# MF0ICU1, section 7.5.1
return int.from_bytes(self._bytes[:3] + self._bytes[4:8], 'big')
@property
def manufacturer(self):
return self._bytes[0]
@property
def bcc0(self):
return self._bytes[3]
@property
def bcc1(self):
return self._bytes[8]
@property
def id0(self):
return self._bytes[0]
@property
def id1(self):
return self._bytes[1]
@property
def id2(self):
return self._bytes[2]
@property
def id3(self):
return self._bytes[4]
@property
def id4(self):
return self._bytes[5]
@property
def id5(self):
return self._bytes[6]
@property
def id6(self):
return self._bytes[7]
@property
def serial(self):
# same as UID, ref: MF0ICU1.pdf, section 7.5.1
return self.id
@property
def internal(self):
return self._bytes[9]
@property
def lockbytes(self):
return self._bytes[10:12]
def is_readonly(self, index):
if index == 0 or index == 1:
return True
elif index == 2 or index == 3:
return False
else:
pass
class MFUPage:
# A page is just an indexed view in a card.
def __init__(self, card, index):
if not isinstance(card, MFUCard):
raise TypeError('invalid card object')
elif 0 <= index < 16:
self._card = card
self._index = index
else:
raise ValueError('invalid index {}'.format(index))
def __len__(self):
return 4
def __str__(self):
pagebytes = self._i2p()
return 'Page {}: {} {} {} {}'.format(self._index, *pagebytes)
def __getitem__(self, index):
if isinstance(index, (int, slice)):
pagebytes = self._i2p()
return pagebytes[index]
else:
raise TypeError('invalid index: {}'.format(index))
def __iter__(self):
pagebytes = self._i2p()
return iter(pagebytes)
def _i2p(self):
"""Index to page"""
pagebytes = self._card[self._index*4:self._index*4+4]
return pagebytes
def to_int(self):
return int.from_bytes(self._i2p(), 'big')
def to_hex(self):
return ''.join('{:02x}'.format(b) for b in self._i2p())
@property
def readonly(self):
if self._index in (0, 1):
return True
else:
locked_bits = int.from_bytes(self._card.lockbytes, 'little')
# page 2 is read-only only when the block-locking bits are all set
if self._index == 2:
return locked_bits & 0x0007 == 0x0007
elif self._index == 3:
return locked_bits & 0x0008 != 0
else:
return (1 << self._index) & locked_bits != 0
class MFUPageViewProxy:
def __init__(self, card):
self._card = card
self._pages = [MFUPage(self._card, i) for i in range(16)]
def __getitem__(self, index):
return self._pages[index]
def slice2range(s):
start = s.start if s.start is not None else 0
stop = s.stop if s.stop is not None else len(self)
step = s.step if s.step is not None else 1
return range(start, stop, step)
if isinstance(index, int):
if index < 0:
index += len(self)
if index < 0 or index >= len(self):
raise IndexError('invalid page index')
page = MFUPage(self._card, index)
return page
elif isinstance(index, slice):
pages = [MFUPage(self._card, i) for i in slice2range(index)]
return pages
else:
raise TypeError('invalid page index')
def __setitem__(self, index, value):
if isinstance(index, slice):
raise NotImplementedError('pages do not support slice assignments')
if not isinstance(index, int):
raise TypeError('invalid index type {}'.format(type(index)))
if index >= len(self):
raise IndexError('invalid page index {}'.format(index))
def set_bytes(fourbytes):
self._card._bytes = (self._card._bytes[:index*4] +
fourbytes +
self._card._bytes[index*4+4:])
if isinstance(value, int):
set_bytes(value.to_bytes(4, 'big'))
elif isinstance(value, (bytes, bytearray)):
if len(value) != 4:
raise ValueError('byte assignments must be 4 bytes')
set_bytes(bytes(value))
elif isinstance(value, str):
set_bytes(value.encode('latin-1'))
else:
raise ValueError('value must be a 4-byte string, int or byte array')
def __iter__(self):
return iter(self._pages)
def __len__(self):
return 16