英文原版链接
http://nfc-tools.org/index.php?title=Libnfc
这是一篇介绍如何用树莓派使用PN532的随笔,介绍了具体的使用步骤。
首先介绍一下:
①、IC卡是非接触式的智能卡,里面一般是一个方形线圈和一个小芯片(用强光照着可以看到)。M1卡是IC卡的一种,一般水卡、公交卡都是这种。UID卡是M1的复制子卡,与M1完全兼容。M1卡0扇区的内容可读不可写,UID卡所有扇区均可读可写,所以M1卡的数据能复制到UID卡中,而不能复制到M1卡中。
平常用的M1卡有16个扇区,一个扇区4个块,一个快16个字节,一共1K数据。每个扇区的前三个块是数据区,最后一个块是keyA、控制段、keyB的存储区域,分别是6个字节,4个字节,6个字节。第0扇区的第0块记录了制卡厂家的或者卡的ID信息,只可读,不可写。
②、ITEAD PN532是为嵌入式设计的PN532板子,可以用树莓派控制(我用window读取不出来),它有2种数据传输模式,SPI和I2C。
操作步骤:
用的是I2C接口传输数据,SET0-->H,
SET1-->L
连线方法:
树莓派<----->PN532
4口 <-----> VCC
6口 <-----> GND
3口 <-----> SDA/TX
5口 <-----> SCL/RX
另附一张树莓派GPIO图:
①、在树莓派上安装必要的库:
sudo apt-get install libusb-dev libpcsclite-dev //这是libnfc依赖的库
sudo apt-get install automake autoconf //这是编译时用到的
如果安装libusb-dev和libpcsclite-dev报版本错误安装失败“ Unable to correct problems, you have held broken packages.”,
可执行命令:aptitude install libusb-dev ,然后依次输n、y、y 即可。
②、安装nfc操作模块:
libnfc(操作nfc):
1 2 3 4 5 6 |
|
mfoc(破解key,读出数据到文件):https://github.com/nfc-tools/mfoc
mfuck(破解全加密数据):https://github.com/nfc-tools/mfcuk
后两个下载解压后切换到目录里执行:
1 2 3 4 5 6 |
|
③修改配置文件(不要忽略这一步骤):
1 2 3 |
|
添加以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
④、开启树莓派i2c:
执行 sudo raspi-config ,在第5项里打开i2c。
⑤、测试
重启一下,看看有没有i2c设备:ls /dev 或者 lsmod
执行 i2cdetect -y 1 ,如果出现的不全是横杠,就代表连接成功了,如下图:
如果全是横杠的话,拨一下pn532的vcc线再插上试试。ps:我的也是死活显示没有,然后重插一下就有了。
放上一张卡执行 nfc-list,如果正常显示下面内容就表示读取到卡了。
如果提示closed,就是板子没正常接通。
⑤、读写数据:
mfoc -O output.mfd // 读出卡中的数据保存为文件output.mfd
mfoc 是读取数据,如果有加密就自动破解,如果全加密,就没法读取,可用mfuck命令破解。
nfc-mfclassic w a output.mfd output.mfd // 写入数据,w小写,如果大写是强写0扇区
由于每张卡的0扇区信息(UID)不一样,0扇区又不可写,不同的卡没法互写。但是可写入0扇区可写的UID卡。
我把读出来的数据写入它自己里可以写入,暂时没有UID卡,就没办法将数据写入空卡里。
参考:http://ju.outofmemory.cn/entry/204150
分类: 嵌入式硬件
如果是uart接口:
新方法
在kali2016.1中发现只需对libnfc进行简单的配置即可解决问题
编辑/etc/nfc/libnfc.conf
# Allow device auto-detection (default: true)
# Note: if this auto-detection is disabled, user has to set manually a device
#因篇幅问题 中间部分配置省略
# Note: if autoscan is enabled, default device will be the first device available in device list.
#device.name = "microBuilder.eu"
#device.connstring = "pn532_uart:/dev/ttyUSB0"
去掉最后两行的注释即可,如下。
# Allow device auto-detection (default: true)
# Note: if this auto-detection is disabled, user has to set manually a device
#因篇幅问题 中间部分配置省略
# Note: if autoscan is enabled, default device will be the first device available in device list.
device.name = "microBuilder.eu"
device.connstring = "pn532_uart:/dev/ttyUSB0"
配置用于指明所用设备为/dev/ttyUSB0,即libnfc绑定tty设备。