用树莓派3B+和 ITEAD PN532 读取、破解、写入M1卡

英文原版链接

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-->HSET1-->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

wget http://dl.bintray.com/nfc-tools/sources/libnfc-1.7.1.tar.bz2

tar -xf libnfc-1.7.1.tar.bz2

cd libnfc-1.7.1

./configure --prefix=/usr --sysconfdir=/etc

make

sudo make install

mfoc(破解key,读出数据到文件):https://github.com/nfc-tools/mfoc
mfuck(破解全加密数据):https://github.com/nfc-tools/mfcuk
后两个下载解压后切换到目录里执行:

1

2

3

4

5

6

automake

autoconf

autoreconf -is

./configure

make

make install

修改配置文件(不要忽略这一步骤)

1

2

3

cd /etc

sudo mkdir nfc

sudo nano /etc/nfc/libnfc.conf

  添加以下内容:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

# Allow device auto-detection (default: true)

# Note: if this auto-detection is disabled, user has to manually set a device

# configuration using file or environment variable

allow_autoscan = true

 

# Allow intrusive auto-detection (default: false)

# Warning: intrusive auto-detection can seriously disturb other devices

# This option is not recommended, so user should prefer to add manually his/her device.

allow_intrusive_scan = false

 

# Set log level (default: error)

# Valid log levels are (in order of verbosity): 0 (none), 1 (error), 2 (info), 3 (debug)

# Note: if you compiled with --enable-debug option, the default log level is "debug"

log_level = 1

 

# Manually set default device (no default)

# To set a default device, users must set both name and connstring for their device

# Note: if autoscan is enabled, default device will be the first device available in device list.

device.name = "Itead_PN532_I2C"

device.connstring = "pn532_i2c:/dev/i2c-1"

  

④、开启树莓派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设备。


 

好的,这是一个较为复杂的问题,需要多个模块的协作。我将简要介绍一下实现的步骤。 步骤1:硬件连接 将PN532模块与C51单片机进行连接。PN532模块需要连接到单片机的串口,同时需要连接到单片机的SPI总线。M1卡需要连接到PN532模块的天线接口。 步骤2:编写PN532模块驱动程序 PN532模块需要通过驱动程序与单片机进行通信。可以采用SPI协议或者UART协议,这里我们以SPI协议为例。编写SPI总线的初始化函数、写数据函数和读数据函数。 步骤3:编写M1卡驱动程序 M1卡需要通过PN532模块进行读写操作。编写M1卡的初始化函数、读函数和写函数。 步骤4:编写主函数 在主函数中调用PN532模块和M1卡的驱动程序,实现向M1卡写入HelloWorld的功能。 下面是一个简单的代码示例: ```c #include <reg52.h> #include <stdio.h> #define PN532_CS_PIN P2_0 #define PN532_SPI_SCK P1_5 #define PN532_SPI_MISO P1_6 #define PN532_SPI_MOSI P1_7 #define M1_CARD_TYPE 0x04 #define M1_CARD_BLOCK 0x08 void spi_init() { //初始化SPI总线 } void spi_write(uint8_t data) { //写数据 } uint8_t spi_read() { //读数据 } void pn532_init() { //初始化PN532模块 } void pn532_write(uint8_t* data, uint8_t length) { //向PN532模块写数据 } void pn532_read(uint8_t* data, uint8_t length) { //从PN532模块读数据 } void m1_card_init() { //初始化M1卡 } uint8_t m1_card_read_block(uint8_t block, uint8_t* data) { //读M1卡块数据 } uint8_t m1_card_write_block(uint8_t block, uint8_t* data) { //写M1卡块数据 } void main() { uint8_t data[] = "HelloWorld"; uint8_t block_data[16]; //初始化SPI总线 spi_init(); //初始化PN532模块 pn532_init(); //初始化M1卡 m1_card_init(); //向M1卡写入数据 m1_card_write_block(M1_CARD_BLOCK, data); //从M1卡读取数据 m1_card_read_block(M1_CARD_BLOCK, block_data); //输出读取到的数据 printf("%s", block_data); } ``` 需要注意的是,代码中的SPI总线和串口通信的具体实现需要根据具体的硬件和驱动程序进行修改。此外,M1卡的读写操作需要遵循M1卡的协议规定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值