-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
163 lines (145 loc) · 4.42 KB
/
example.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
#!/usr/bin/env python
# -*- coding: utf8 -*-
import RC522
import time,sys
continue_reading = True
wr = False
MIFAREReader = RC522.Reader(32766, 0, "U14_14")
try:
fun = sys.argv[1]
except:
print("Options: -d - dump")
print(" -w - write")
print(" -r - read")
print(" -c - change UID")
print(" -s - show UID")
continue_reading = False
def hex(hex):
ret = ""
for h in hex:
if(32<int(h, 16)<106):
ret+=chr(int(h, 16))
else:
ret+="."
return ret
print("Place card on reader")
while continue_reading:
# Scan for cards
status,TagType = MIFAREReader.RC522_Request(MIFAREReader.PICC_REQIDL)
# If a card is found
if status == MIFAREReader.MI_OK:
print("Card detected")
# Get the UID of the card
(status,uid) = MIFAREReader.RC522_Anticoll()
# If we have the UID, continue
if status == MIFAREReader.MI_OK:
# Print UID
print("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
for u in uid:
print(str(format(u, '02x')), end=" ")
print()
# This is the default key for authentication
if(fun != "-s"):
key = input("Key>")
else:
key = []
if(len(key)<17):
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
else:
key = key.split(" ")
wrb = []
for k in key:
wrb.append(int(k, 16))
key = wrb
# Select the scanned tag
MIFAREReader.RC522_SelectTag(uid)
if(fun == "-r"):
sec = int(input("Sector: "))
status = MIFAREReader.RC522_Auth(MIFAREReader.PICC_AUTHENT1A, sec, key, uid)
if status == MIFAREReader.MI_OK:
out = MIFAREReader.RC522_Read(sec)
print(out)
r = []
for t in out:
r.append(format(t, '02x'))
print(str(sec)+": "+" ".join(r)+"|"+hex(r))
else:
print("Authentication error")
elif(fun == "-w"):
sec = int(input("Sector: "))
status = MIFAREReader.RC522_Auth(MIFAREReader.PICC_AUTHENT1A, sec, key, uid)
# Check if authenticated
if status == MIFAREReader.MI_OK:
data = input("Data>").split(' ')
wrb = []
for d in data:
wrb.append(int(d, 16))
print("Current data:")
print(MIFAREReader.RC522_Read(sec))
if(input("Write? [Y/N] ")=="Y"):
print("Writing")
# Write the data
if(MIFAREReader.RC522_Write(sec, wrb)):
print("OK")
print("NOW:")
print(MIFAREReader.RC522_Read(sec))
else:
print("Auth error")
elif(fun == "-d"):
fn = input("File name: ")
if(len(fn)<1):
wr = False
else:
f = open(fn, 'w')
wr = True
for d in range(64):
status = MIFAREReader.RC522_Auth(MIFAREReader.PICC_AUTHENT1A, d, key, uid)
if status == MIFAREReader.MI_OK:
out = MIFAREReader.RC522_Read(d)
r = []
for t in out:
r.append(format(t, '02x'))
de = hex(r)
print(str(d).zfill(2)+": "+" ".join(r)+"|"+de)
if(wr):
f.write(" ".join(r)+"|"+de+"\n")
else:
status,TagType = MIFAREReader.RC522_Request(MIFAREReader.PICC_REQIDL)
if status == MIFAREReader.MI_OK:
(status,uid) = MIFAREReader.RC522_Anticoll()
MIFAREReader.RC522_SelectTag(uid)
print("Authentication error at: "+str(d))
elif(fun == "-c"):
new_uid = input("UID>").upper()
if(len(new_uid)<11):
new_uid = [0xDE,0xAD,0xBE,0xEF]
else:
new_uid = new_uid.split(" ")
nud = []
for k in new_uid:
nud.append(int(k, 16))
new_uid = nud
status = MIFAREReader.RC522_Auth(MIFAREReader.PICC_AUTHENT1A, 0, key, uid)
if status == MIFAREReader.MI_OK:
block_zero = MIFAREReader.RC522_Read(0)
bcc = 0
for i in range(len(uid)-1):
block_zero[i] = new_uid[i]
bcc ^= new_uid[i]
block_zero[len(uid)-1] = bcc
MIFAREReader.RC522_StopCrypto1()
if(MIFAREReader.OpenBackdoor() != MIFAREReader.MI_OK):
print("Backdoor error")
if(MIFAREReader.RC522_Write(0, block_zero) == MIFAREReader.MI_OK):
print("OK")
else:
print("Error writting")
else:
print("Auth error")
elif(sys.argv[1] == "-s"):
pass
MIFAREReader.RC522_StopCrypto1()
if(wr):
f.close()
if(fun != "-s"):
continue_reading = False