-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(magic): port of nfc-tools/libnfc#461
- Loading branch information
1 parent
c867aab
commit 912f03f
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package magic | ||
|
||
import ( | ||
"github.com/maitredede/gonfc" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const ( | ||
MAX_FRAME_LEN int = 264 | ||
SAK_FLAG_ATS_SUPPORTED byte = 0x20 | ||
CASCADE_BIT byte = 0x04 | ||
) | ||
|
||
// ISO14443A Anti-Collision Commands | ||
var ( | ||
abtReqa = []byte{0x26} | ||
strangeWupa = []byte{0x40} | ||
backdoorTest = []byte{0x43} | ||
abtSelectAll = []byte{0x93, 0x20} | ||
abtSelectTag = []byte{0x93, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} | ||
abtRats = []byte{0xe0, 0x50, 0x00, 0x00} | ||
abtHalt = []byte{0x50, 0x00, 0x00, 0x00} | ||
) | ||
|
||
// IsMagicCard is an implementation of https://github.com/nfc-tools/libnfc/pull/461 | ||
func IsMagicCard(device gonfc.Device) (bool, error) { | ||
l := device.Logger().Named("magic") | ||
|
||
// Configure the CRC | ||
if err := device.SetPropertyBool(gonfc.NP_HANDLE_CRC, false); err != nil { | ||
return false, err | ||
} | ||
|
||
// Use raw send/receive methods | ||
if err := device.SetPropertyBool(gonfc.NP_EASY_FRAMING, false); err != nil { | ||
return false, err | ||
} | ||
|
||
// Disable 14443-4 autoswitching | ||
if err := device.SetPropertyBool(gonfc.NP_AUTO_ISO14443_4, false); err != nil { | ||
return false, err | ||
} | ||
|
||
if err := transmitBits(l, device, strangeWupa, 7); err != nil { | ||
l.Debugf("This is NOT a backdoored rewritable UID card") | ||
return false, nil | ||
} | ||
if err := transmitBytes(l, device, backdoorTest); err != nil { | ||
l.Debugf("This is backdoored rewritable UID card") | ||
return true, nil | ||
} | ||
l.Debugf("This is NOT a backdoored rewritable UID card") | ||
return false, nil | ||
} | ||
|
||
func transmitBits(l *zap.SugaredLogger, device gonfc.Device, pbtx []byte, szTxBits int) error { | ||
abtRx := make([]byte, 264) | ||
_ /*n*/, err := device.InitiatorTransceiveBits(pbtx, szTxBits, nil, abtRx, nil) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func transmitBytes(l *zap.SugaredLogger, device gonfc.Device, pbtx []byte) error { | ||
abtRx := make([]byte, 264) | ||
_ /*n*/, err := device.InitiatorTransceiveBytes(pbtx, abtRx, 0) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |