Skip to content

Commit 912f03f

Browse files
committed
feat(magic): port of nfc-tools/libnfc#461
1 parent c867aab commit 912f03f

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

magic/magic.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package magic
2+
3+
import (
4+
"github.com/maitredede/gonfc"
5+
"go.uber.org/zap"
6+
)
7+
8+
const (
9+
MAX_FRAME_LEN int = 264
10+
SAK_FLAG_ATS_SUPPORTED byte = 0x20
11+
CASCADE_BIT byte = 0x04
12+
)
13+
14+
// ISO14443A Anti-Collision Commands
15+
var (
16+
abtReqa = []byte{0x26}
17+
strangeWupa = []byte{0x40}
18+
backdoorTest = []byte{0x43}
19+
abtSelectAll = []byte{0x93, 0x20}
20+
abtSelectTag = []byte{0x93, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
21+
abtRats = []byte{0xe0, 0x50, 0x00, 0x00}
22+
abtHalt = []byte{0x50, 0x00, 0x00, 0x00}
23+
)
24+
25+
// IsMagicCard is an implementation of https://github.com/nfc-tools/libnfc/pull/461
26+
func IsMagicCard(device gonfc.Device) (bool, error) {
27+
l := device.Logger().Named("magic")
28+
29+
// Configure the CRC
30+
if err := device.SetPropertyBool(gonfc.NP_HANDLE_CRC, false); err != nil {
31+
return false, err
32+
}
33+
34+
// Use raw send/receive methods
35+
if err := device.SetPropertyBool(gonfc.NP_EASY_FRAMING, false); err != nil {
36+
return false, err
37+
}
38+
39+
// Disable 14443-4 autoswitching
40+
if err := device.SetPropertyBool(gonfc.NP_AUTO_ISO14443_4, false); err != nil {
41+
return false, err
42+
}
43+
44+
if err := transmitBits(l, device, strangeWupa, 7); err != nil {
45+
l.Debugf("This is NOT a backdoored rewritable UID card")
46+
return false, nil
47+
}
48+
if err := transmitBytes(l, device, backdoorTest); err != nil {
49+
l.Debugf("This is backdoored rewritable UID card")
50+
return true, nil
51+
}
52+
l.Debugf("This is NOT a backdoored rewritable UID card")
53+
return false, nil
54+
}
55+
56+
func transmitBits(l *zap.SugaredLogger, device gonfc.Device, pbtx []byte, szTxBits int) error {
57+
abtRx := make([]byte, 264)
58+
_ /*n*/, err := device.InitiatorTransceiveBits(pbtx, szTxBits, nil, abtRx, nil)
59+
if err != nil {
60+
return err
61+
}
62+
return nil
63+
}
64+
65+
func transmitBytes(l *zap.SugaredLogger, device gonfc.Device, pbtx []byte) error {
66+
abtRx := make([]byte, 264)
67+
_ /*n*/, err := device.InitiatorTransceiveBytes(pbtx, abtRx, 0)
68+
if err != nil {
69+
return err
70+
}
71+
return nil
72+
}

0 commit comments

Comments
 (0)