Skip to content

Commit 9c2c667

Browse files
committed
Add MIFARE Classic key recovery tools and supporting files
- Implemented crypto1.c and parity.h for cryptographic operations. - Created common.h for shared definitions and utilities. - Developed mfkey32v2.c and mfkey64.c for key recovery from MIFARE Classic cards. - Added sleep.c and sleep.h for platform-independent sleep functions. - Introduced util_posix.c and util_posix.h for POSIX utility functions. - Included necessary headers and defined functions for handling cryptographic states and operations.
1 parent f17b061 commit 9c2c667

25 files changed

Lines changed: 3156 additions & 1 deletion

.github/workflows/pyinstaller.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ jobs:
3232
- name: Checkout repository
3333
uses: actions/checkout@v3
3434

35+
- name: Install MSYS2 toolchain
36+
if: runner.os == 'Windows'
37+
uses: msys2/setup-msys2@v2
38+
with:
39+
update: true
40+
install: >-
41+
base-devel
42+
mingw-w64-x86_64-toolchain
43+
path-type: inherit
44+
3545
- name: Set up Python
3646
uses: actions/setup-python@v4
3747
with:
@@ -43,10 +53,38 @@ jobs:
4353
pip install -r script/requirements.txt
4454
pip install pyinstaller
4555
56+
- name: Build mfkey tools (Linux/macOS)
57+
if: runner.os != 'Windows'
58+
run: |
59+
chmod +x script/build_helpers.sh
60+
./script/build_helpers.sh
61+
62+
- name: Build mfkey tools (Windows)
63+
if: runner.os == 'Windows'
64+
shell: msys2 {0}
65+
env:
66+
CC: gcc
67+
run: |
68+
chmod +x script/build_helpers.sh
69+
./script/build_helpers.sh
70+
4671
- name: Run PyInstaller
4772
run: |
4873
pyinstaller script/pyinstaller.spec
4974
75+
- name: Bundle mfkey binaries (Linux/macOS)
76+
if: runner.os != 'Windows'
77+
run: |
78+
mkdir -p dist/pn532_cli_main/mfkey
79+
cp build/mfkey32v2 build/mfkey64 build/staticnested dist/pn532_cli_main/mfkey/
80+
81+
- name: Bundle mfkey binaries (Windows)
82+
if: runner.os == 'Windows'
83+
shell: pwsh
84+
run: |
85+
New-Item -ItemType Directory -Force -Path "dist/pn532_cli_main/mfkey" | Out-Null
86+
Copy-Item -Path "build/mfkey32v2","build/mfkey64","build/staticnested" -Destination "dist/pn532_cli_main/mfkey" -Force
87+
5088
- name: Package artifact (Linux)
5189
if: runner.os == 'Linux'
5290
run: |

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ pn532_cli_main.exe --debug
3030
./pn532_cli_main --debug
3131
```
3232

33+
### PN532Killer MFkey workflow
34+
1. Build the standalone mfkey helpers once via `script/build_helpers.sh` (creates `build/mfkey32v2` and `build/mfkey64`).
35+
2. Switch PN532Killer to sniffer mode and capture an authentication:
36+
- Card present → run `hf mf mfkey64` to parse the sniff buffer and feed `mfkey64` automatically.
37+
- No card (UID emulation) → run `hf mf mfkey32v2` to pair nonce captures and call `mfkey32v2`.
38+
3. Use `--show-raw` to see the NT/NR/AR(/AT) tuples that were forwarded to the tools.
39+
4. The CLI leaves the PN532Killer sniff buffer untouched; run `hf sniff clear` manually when you want to discard captured frames.
40+
41+
### PN532Killer staticnested workflow
42+
1. Build the helper binaries via `script/build_helpers.sh` (creates `build/staticnested`).
43+
2. Authenticate to a sector with a known key and immediately to the target sector using `hf mf staticnested --known-key <hex> --known-block <dec> --target-block <dec>`.
44+
3. Optional flags: `--known-key-type`, `--target-key-type`, and `--show-raw` to display nonce/keystream details plus the `staticnested` stdout; the CLI auto-derives the required 8-byte datakey as `0x0000 || known-key` per the firmware spec.
45+
4. The command fetches nonce pairs via PN532Killer's `ReadUserDefData` helper and calls the bundled `staticnested` binary automatically; recovered keys are printed in-line.
46+
3347
## Features
3448
### PN532
3549
- [x] Read and write Mifare Classic Mini, 1K, 4K

script/build_helpers.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd)"
5+
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
6+
OUT_DIR="${OUT_DIR:-$ROOT_DIR/build}"
7+
CC="${CC:-cc}"
8+
9+
UNAME=$(uname | tr '[:upper:]' '[:lower:]')
10+
if [[ "$UNAME" == *mingw* || "$UNAME" == *msys* || "$UNAME" == *cygwin* ]]; then
11+
EXE_SUFFIX=".exe"
12+
else
13+
EXE_SUFFIX=""
14+
fi
15+
16+
if ! command -v "$CC" >/dev/null 2>&1; then
17+
echo "error: compiler '$CC' not found" >&2
18+
exit 1
19+
fi
20+
21+
mkdir -p "$OUT_DIR"
22+
23+
COMMON_FLAGS=(
24+
-std=c99 -O2
25+
-I"$ROOT_DIR/third_party/proxmark_mfkey/include"
26+
-I"$ROOT_DIR/third_party/proxmark_mfkey/common"
27+
-I"$ROOT_DIR/third_party/proxmark_mfkey/common/crapto1"
28+
-I"$ROOT_DIR/third_party/proxmark_mfkey/tools/mfc/card_reader"
29+
)
30+
31+
COMMON_SRCS=(
32+
"$ROOT_DIR/third_party/proxmark_mfkey/tools/mfc/card_reader/util_posix.c"
33+
"$ROOT_DIR/third_party/proxmark_mfkey/tools/mfc/card_reader/sleep.c"
34+
"$ROOT_DIR/third_party/proxmark_mfkey/common/bucketsort.c"
35+
"$ROOT_DIR/third_party/proxmark_mfkey/common/crapto1/crapto1.c"
36+
"$ROOT_DIR/third_party/proxmark_mfkey/common/crapto1/crypto1.c"
37+
)
38+
39+
LIBS=( -lpthread )
40+
41+
build_tool() {
42+
local target="$1"
43+
local src="$ROOT_DIR/third_party/proxmark_mfkey/tools/mfc/card_reader/${target}.c"
44+
local output="$OUT_DIR/${target}${EXE_SUFFIX}"
45+
echo "Building $target -> $output"
46+
"$CC" "${COMMON_FLAGS[@]}" "$src" "${COMMON_SRCS[@]}" "${LIBS[@]}" -o "$output"
47+
}
48+
49+
build_tool mfkey32v2
50+
build_tool mfkey64
51+
52+
STATIC_FLAGS=(
53+
-std=c99 -O2
54+
-I"$ROOT_DIR/third_party/proxmark_mfkey/include"
55+
-I"$ROOT_DIR/third_party/proxmark_mfkey/common"
56+
-I"$ROOT_DIR/third_party/proxmark_mfkey/common/crapto1"
57+
-I"$ROOT_DIR/third_party/proxmark_mfkey/tools/mfc/card_only"
58+
)
59+
60+
STATIC_SRCS=(
61+
"$ROOT_DIR/third_party/proxmark_mfkey/tools/mfc/card_only/staticnested_2nt.c"
62+
"$ROOT_DIR/third_party/proxmark_mfkey/tools/mfc/card_only/nested_util.c"
63+
"$ROOT_DIR/third_party/proxmark_mfkey/common/bucketsort.c"
64+
"$ROOT_DIR/third_party/proxmark_mfkey/common/crapto1/crapto1.c"
65+
"$ROOT_DIR/third_party/proxmark_mfkey/common/crapto1/crypto1.c"
66+
)
67+
68+
STATIC_OUTPUT="$OUT_DIR/staticnested${EXE_SUFFIX}"
69+
echo "Building staticnested -> $STATIC_OUTPUT"
70+
"$CC" "${STATIC_FLAGS[@]}" "${STATIC_SRCS[@]}" -lpthread -o "$STATIC_OUTPUT"
71+
72+
echo "Build artifacts are in $OUT_DIR"

0 commit comments

Comments
 (0)