系统:ubuntu18.04
libnfc官网:libnfc
libnfc github: libnfc github
硬件:pn532、ch341usb转串口模块

1.安装git

1
sudo apt install git

2.安装依赖库

1
sudo apt-get install libusb-dev libpcsclite-dev libusb-0.1-4 libpcsclite1 libccid pcscd

3.插上usb转串口模块,检查驱动有没有安装过,终端运行:

1
lsmod | grep usbserial  

若有

1
usbserial              45056  1 ch341

这样的,说明系统支持usb转串口。终端运行:

1
dmesg | grep ttyUSB0

如果出现以下连接成功的信息,则说明系统已识别该设备了。

1
usb 3-4.3: ch341-uart converter now attached to ttyUSB0

驱动已安装好。如果没有,需安装usb转串口驱动。终端运行:

1
sudo apt-get install minicom

安装好后,终端运行:

1
sudo minicom -s

这时会弹出配置界面,使用方向键选择 Serial port setup ,按Enter键,进入设置环境。
输入a或者A,选择串口设备,在这里我使用的是USB转串口,将/dev/tty8修改为/dev/ttyUSB0。
配置完串口设备后,按Enter键。输入E可以配置波特率,pn532按默认配置即可,不用改波特率 115200 8N1 (波特率:115200,数据位:8,奇偶校验位:N 无,停止位:1)。
输入F,配置硬件流控,选择NO,再继续配置软件流控,也选择NO。都配置完后,按下Enter键返回上一界面,选择save setup as dfl(即将其保存位默认配置),再选择Exit,关闭minicom。
再次输入命令 sudo minicom,使刚才的配置生效,可以看到串口输出信息,串口配置完成。

4.在etc下新建nfc文件夹,放配置文件(这一步解决后面会遇到的执行nfc-list时出现No NFC device found的问题)

1
2
sudo mkdir /etc/nfc && sudo mkdir /etc/nfc/devices.d/
sudo vim /etc/nfc/devices.d/pn532_via_uart2usb.conf

pn532_via_uart2usb.conf文件里添加以下配置语句:

1
2
3
4
5
# Typical configuration file for PN532 board (ie. microbuilder.eu / Adafruit) device 
name = "Adafruit PN532 board via UART"
connstring = pn532_uart:/dev/ttyUSB0
allow_intrusive_scan = true
log_level = 3

5.选择一个libnfc要安装的目录,在该目录下终端运行:

1
git clone https://github.com/nfc-tools/libnfc.git

然后libnfc会下载到该目录的libnfc文件夹下。

6.终端运行:

1
2
3
cd libnfc
sudo autoreconf -vis
./configure --prefix=/usr --with-drivers=pn532_uart --sysconfdir=/etc

可能会遇到的错误:
1.’doxygen is mandatory.’
安装doxygen:
选择一个doxygen要安装的目录,在该目录下终端运行:

1
2
3
4
5
6
git clone https://github.com/doxygen/doxygen.git
cd doxygen
mkdir build
cd build
cmake -G "Unix Makefiles" ..
make

没有cmke的再安装一下cmake:

1
sudo apt install cmake

make完后,终端运行:

1
sudo make install

以安装doxygen。
2.’PKG_CHECK_MODULES ERROR’

1
2
./configure: line 14085: syntax error near unexpected token `libusb,'
./configure: line 14085: 'PKG_CHECK_MODULES(libusb,libusb,HAVE_LIBUSB=1,HAVE_LIBUSB=0)'

论坛上说安装pkg-config,安装了后可以解决。实测安装了后这个问题还在,由于支持的库已经确认安装好了,我的做法是直接vim打开configure文件,找到对应行的位置,注释掉。一共有3处PKG_CHECK_MODULES。(vim 冒号加行数+回车即跳转到该行,加#号注释)。注释完后configure可以执行完毕。最后可以看到:

1
2
3
4
5
6
7
8
9
Selected drivers:
acr122_pcsc...... no
acr122_usb....... no
acr122s.......... no
arygon........... no
pn53x_usb........ no
pn532_uart....... yes
pn532_spi....... no
pn532_i2c........ no

7.在libnfc文件夹下终端运行:

1
make

8.安装libnfc:

1
sudo make install

9.验证安装是否成功:

将PN532接上usb转串口模块,usb转串口模块插入PC USB接口,然后进入如下目录:
libnfc/utils,运行程序:

1
sudo ./nfc-list

出现以下信息表示安装成功:

1
NFC device: Adafruit PN532 board via UART opened

下面就可以把卡放在PN532上去使用libnfc了。
libnfc常用指令:

1
2
3
4
5
6
7
8
9
10
11
12
13
nfc-list:
Lists the first target present of each founded device

nfc-mfclassic:
Usage: nfc-mfclassic r|w a|b <dump.mfd> [<keys.mfd>]
r|w - Perform read from (r) or write to (w) card
a|b - Use A or B keys for action
<dump.mfd> - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)
<keys.mfd> - MiFare Dump (MFD) that contain the keys (optional)
Or: nfc-mfclassic x <dump.mfd> <payload.bin>
x - Extract payload (data blocks) from MFD
<dump.mfd> - MiFare Dump (MFD) that contains wanted payload
<payload.bin> - Binary file where payload will be extracted

10.安装mfoc

选择一个mfoc要安装的目录,在该目录下终端运行:

1
git clone https://github.com/nfc-tools/mfoc

克隆好后,打开文件夹:

1
2
3
4
5
cd mfoc/
autoreconf -vis
./configure
make
sudo make install

即可安装完成,测试是否安装好:

1
2
cd src/
mfoc -h

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Usage: mfoc [-h] [-k key] [-f file] ... [-P probnum] [-T tolerance] [-O output]

h print this help and exit
k try the specified key in addition to the default keys
f parses a file of keys to add in addition to the default keys
P number of probes per sector, instead of default of 20
T nonce tolerance half-range, instead of default of 20
(i.e., 40 for the total range, in both directions)
O file in which the card contents will be written (REQUIRED)
D file in which partial card info will be written in case PRNG is not vulnerable

Example: mfoc -O mycard.mfd
Example: mfoc -k ffffeeeedddd -O mycard.mfd
Example: mfoc -f keys.txt -O mycard.mfd
Example: mfoc -P 50 -T 30 -O mycard.mfd

This is mfoc version 0.10.7.
For more information, run: 'man mfoc'.

安装完毕。