Merge pull request #1331 from Guilhem7/master
[RRG-proxmark3.git] / tools / recover_pk.py
blob470f406ae652b087ac6d3d4152d53e29ae68b82c
1 #!/usr/bin/env python3
2 # MIT License
3 # Copyright (c) 2020 @doegox
5 # Requirements:
6 # python3 -m pip install ansicolors sslcrypto
8 import binascii
9 import sys
10 import sslcrypto
11 from colors import color
13 debug = False
16 def guess_curvename(signature):
17 siglen = (len(signature) // 2) & 0xfe
18 if siglen == 32:
19 curves = ["secp128r1", "secp128r2"]
20 elif siglen == 48:
21 curves = ["secp192k1", "secp192r1"]
22 elif siglen == 56:
23 curves = ["secp224k1", "secp224r1"]
24 elif siglen == 64:
25 curves = ["secp256k1", "secp256r1"]
26 elif siglen == 96:
27 curves = ["secp384r1"]
28 elif siglen == 132:
29 curves = ["secp521r1"]
30 else:
31 raise ValueError("Unsupported signature size %i" % len(signature))
32 return curves
35 def recover(data, signature, curvename, alghash=None):
36 recovered = set()
37 curve = sslcrypto.ecc.get_curve(curvename)
38 recoverable = len(signature) % 1 == 1
39 if (recoverable):
40 try:
41 pk = curve.recover(signature, data, hash=alghash)
42 recovered.add(pk)
43 if debug:
44 print("Possible Pk: ", binascii.hexlify(pk))
45 except ValueError:
46 pass
47 else:
48 for i in range(2):
49 # Brute force RECID
50 recid = bytes([27+i])
51 try:
52 pk = curve.recover(recid + signature, data, hash=alghash)
53 recovered.add(pk)
54 if debug:
55 print("Possible Pk: ", binascii.hexlify(pk))
56 except ValueError:
57 pass
58 return recovered
61 def recover_multiple(uids, sigs, curvename, alghash=None):
62 recovered = set()
63 assert len(uids) == len(sigs)
64 for i in range(len(uids)):
65 data = binascii.unhexlify(uids[i])
66 if debug:
67 print("UID (%2i): " %
68 len(data), binascii.hexlify(data))
69 signature = binascii.unhexlify(sigs[i])
70 if debug:
71 print("Signature (%2i): " %
72 len(signature), binascii.hexlify(signature))
73 recovered_tmp = recover(data, signature, curvename, alghash)
74 if i == 0:
75 if recovered_tmp == set():
76 break
77 else:
78 recovered = recovered_tmp
79 else:
80 recovered &= recovered_tmp
81 return recovered
83 def selftests():
84 tests = [
85 {'name': "Mifare Ultralight EV1",
86 'samples': ["04C1285A373080", "CEA2EB0B3C95D0844A95B824A7553703B3702378033BF0987899DB70151A19E7",
87 "04C2285A373080", "A561506723D422D29ED9F93E60D20B9ED1E05CC1BF81DA19FE500CA0B81CC0ED"],
88 'pk': "0490933BDCD6E99B4E255E3DA55389A827564E11718E017292FAF23226A96614B8"},
89 {'name': "NTAG21x",
90 'samples': ["04E10CDA993C80", "8B76052EE42F5567BEB53238B3E3F9950707C0DCC956B5C5EFCFDB709B2D82B3",
91 "04DB0BDA993C80", "6048EFD9417CD10F6B7F1818D471A7FE5B46868D2EABDC6307A1E0AAE139D8D0"],
92 'pk': "04494E1A386D3D3CFE3DC10E5DE68A499B1C202DB5B132393E89ED19FE5BE8BC61"},
93 {'name': "Mifare Classic EV1",
94 'samples': ["0433619AB35780", "B9FAE369EC21C980650D87ED9AE9B1610E859131B4B8699C647548AB68D249BB",
95 "524374E2", "F8758CE30A58553A9985C458FB9C7D340FCFB04847B928A0667939272BC58B5E",
96 "53424B8A", "B4F533E8C06C021E242EFE8558C1672ED7022E5AE4E7AA2D46113B0AB6928AFC",
97 "BD2A4146", "19505576ED327D8F8870C86B1ED00898BFEDFFF27CC82FC515BA2EEC26050873"],
98 'pk': "044F6D3F294DEA5737F0F46FFEE88A356EED95695DD7E0C27A591E6F6F65962BAF"},
99 {'name': "DESFire Light",
100 'samples': ["0439556ACB6480", "D5BD0978106E1E38B513642335966AB21E9F950DCFCFAB45FF13D0DC3CA4C2AE7E0D671DF1240937D040DAC4601C5F66ED62C546EE03ED08",
101 "043B156ACB6480", "76B46932BF2FCF4931A24C755F5CB1686B914F1856177686B864BDAD58EFA6A7493E5C2232F3ADDAA434EA4647BFD1D385BDA6115E77D74C"],
102 'pk': "040E98E117AAA36457F43173DC920A8757267F44CE4EC5ADD3C54075571AEBBF7B942A9774A1D94AD02572427E5AE0A2DD36591B1FB34FCF3D"},
103 {'name': "DESFire EV2",
104 'samples': ["042A41CAE45380", "B2769F8DDB575AEA2A680ADCA8FFED4FAB81A1E9908E2B82FE0FABB697BBD9B23835C416970E75768F12902ACA491349E94E6589EAF4F508",
105 "045640CAE45380", "D34B53A8C2C100D700DEA1C4C0D0DE4409F3A418CD8D57C4F41F146E42AD9A55F014199ABBF5CA259C7799DB0AE20D5E77D4950AC7E95D33",
106 "040D259A965B80","B158073A7100C88C3726F4299FA58311FC3CB18744686DE3F234928AD74578F5CAD7FCEC1DCB962ECC7CC000B8557B37F45B76DC6573A58F"],
107 'pk': "04B304DC4C615F5326FE9383DDEC9AA892DF3A57FA7FFB3276192BC0EAA252ED45A865E3B093A3D0DCE5BE29E92F1392CE7DE321E3E5C52B3A"},
108 {'name': "DESFire EV3",
109 'samples': ["04448BD2DB6B80", "5CBB5632795C8F15263FEFB095B51C7B541AFD914A1AE44EF6FB8AF605EDF13DBFEE6C3A2DB372245E671DFE0D42CB1F0D0B8FE67A89D2F6",
110 "04445DD2DB6B80", "166BFD9F9BFAA451172566101580DF9894F582C4A4E258C15037AD2F35A475CF1D7FB817618623A6569F991931AFB2766984E21A18512A6D"],
111 'pk': "041DB46C145D0A36539C6544BD6D9B0AA62FF91EC48CBC6ABAE36E0089A46F0D08C8A715EA40A63313B92E90DDC1730230E0458A33276FB743"},
112 {'name': "Mifare Plus EV1",
113 # TODO one more Mifare Plus EV1...
114 'samples': ["042A2B221C5080", "BAC40CD88E9193C58ADA5055350C4F648EB5A7AEC4FCF9BD4CDD7B1C558DE5F59C6636F26286ED48622AAA2331D4DF1CEE23B57B94BDA631"],
115 'pk': "044409ADC42F91A8394066BA83D872FB1D16803734E911170412DDF8BAD1A4DADFD0416291AFE1C748253925DA39A5F39A1C557FFACD34C62E"},
116 {'name': "NTAG413DNA",
117 'samples': ["042468222F5C80", "B9211E320F321BD1D0E158E10FF15109B389638BAE15D9909D7725BF1250ED236D66F1AF75C94D60330E4E92535F5E6997675281A5687173",
118 "042938222F5C80", "18B642797D1FD71806146A7A6EC778D3FDD04F39C4A3B36A592BD1A114DC44E5528380FA766C0B7EA32B284AFBE84300B620369F0686D8CC"],
119 'pk': "04bb5d514f7050025c7d0f397310360eec91eaf792e96fc7e0f496cb4e669d414f877b7b27901fe67c2e3b33cd39d1c797715189ac951c2add"},
120 {'name': "NTAG424DNA",
121 'samples': ["0463474AA26A80", "27E9A50E6CA4BA9037C02F7D20A80D0284D0C1D83C67F5A5AC1D8A4EF86C9508417E4E9C6F85AA7920F0ABDED984CAF20467D66EA54BBF08",
122 "04C46C222A6380", "344A806EBF704C05C19215D2F840529CE365AAD2D08A469A95896D75D477D9FAB02A0C827E9F215BD8EB0E56A3A9A008FB75D706AABBD4DA"],
123 'pk': "048A9B380AF2EE1B98DC417FECC263F8449C7625CECE82D9B916C992DA209D68422B81EC20B65A66B5102A61596AF3379200599316A00A1410"},
124 {'name': "Vivokey Spark1",
125 # ! tag signature bytes output by pm3 must be read right to left:
126 # echo $sig |sed 's/\(..\)/\1\n/g'|tac|tr -d '\n'
127 # (and it uses a SHA256)
128 'samples': ["E0040118009C870C", "4B4E03E1211952EF6A5F9D84AB218CD4D7549D0CDF8CA8779F9AD16C9A9CBF3B",
129 "E0040118009B4D62", "25CF13747C3389EC7889DE916E3747584978511CC78B51CFB1883B494CBED7AB"],
130 'pk': "04d64bb732c0d214e7ec580736acf847284b502c25c0f7f2fa86aace1dada4387a"},
131 {'name': "ICODE DNA, ICODE SLIX2",
132 # ! tag UID is considered inverted: E0040118009B5FEE => EE5F9B00180104E0
133 # TODO one more ICODE-DNA...
134 'samples': ["EE5F9B00180104E0", "32D9E7579CD77E6F1FA11419231E874826984C5F189FDE1421684563A9663377",
135 "838ED22A080104E0", "CAE8183CB4823C765AFDEB78C9D66C959990FD52A5820E76E1D6E025D76EAD79"],
136 'pk': "048878A2A2D3EEC336B4F261A082BD71F9BE11C4E2E896648B32EFA59CEA6E59F0"},
137 # {'name': "Minecraft Earth",
138 # # uses secp256r1?, SHA-256,
139 # 'samples': ["aa", "DF0E506DFF8FCFC4B7B979D917644445F1230D2C7CDC342AFA842CA240C210BE7275F62073A9670F2DCEFC602CBEE771C2B4CD4A04F3D1EA11F49ABDF7E8B721"],
140 # 'pk': ""},
142 succeeded = True
143 for t in tests:
144 print("Testing %-25s" % (t['name']+":"), end="")
145 curvenames = guess_curvename(t['samples'][1])
146 recovered = set()
147 for c in curvenames:
148 for h in [None, "md5", "sha1", "sha256", "sha512"]:
149 recovered |= recover_multiple(t['samples'][::2], t['samples'][1::2], c, alghash=h)
151 if (len(recovered) == 1):
152 pk = recovered.pop()
153 pk = binascii.hexlify(pk).decode('utf8')
154 if pk.lower() == t['pk'].lower():
155 print("[OK]")
156 else:
157 succeeded = False
158 print("[FAIL], got %s" % pk.lower())
159 elif len(t['samples'])//2 == 1:
160 pks = [binascii.hexlify(pk).decode('utf8').lower() for pk in list(recovered)]
161 if t['pk'].lower() in pks:
162 print("[OK] (partial)")
163 else:
164 succeeded = False
165 print("[FAIL], got %s" % pks)
166 else:
167 succeeded = False
168 print("[FAIL]")
169 print("Tests: [%s]" % ["FAIL", "OK"][succeeded])
172 if __name__ == "__main__":
173 if len(sys.argv) == 2 and sys.argv[1] == "selftests":
174 selftests()
175 exit(0)
176 if len(sys.argv) < 3 or len(sys.argv) % 2 == 0:
177 print("Usage: \n%s UID SIGN [UID SIGN] [...]" % sys.argv[0])
178 print("Example: \n%s 04ee45daa34084 ebb6102bff74b087d18a57a54bc375159a04ea9bc61080b7f4a85afe1587d73b" % sys.argv[0])
179 exit(1)
180 uids, sigs = sys.argv[1:][::2], sys.argv[1:][1::2]
181 once = True
182 curvenames = guess_curvename(sigs[0])
183 for c in curvenames:
184 for h in [None, "md5", "sha1", "sha256", "sha512"]:
185 recovered = recover_multiple(uids, sigs, c, alghash=h)
186 if recovered:
187 if once:
188 print(color('curve=%s', fg='yellow') % c)
189 once = False
190 print(color('hash=%s', fg='yellow') % h)
191 print("Possible uncompressed Pk(s):")
192 for pk in list(recovered):
193 print(binascii.hexlify(pk).decode('utf8'))
194 once = True