v is unsigned long; PRIu64 is "llu" on macOS, so clang fails the build
with the PrintAndLogEx format attribute.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The 4-byte packing loop read up to 3 bytes beyond src when length was not
a multiple of 4 (ASan heap-buffer-overflow in data test_ss8). Pack byte by
byte into the zeroed backing buffer instead.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
save_buffer8/32/S32 allocate a backing buffer that the self tests never
released. Found by cppcheck 2.22 follow-up, confirmed with LeakSanitizer
(696 bytes in 3 allocations per run of the three tests).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
size_t printed with %lu/%ld/PRIu64 and uint64_t with %lu are correct on
64-bit Linux/macOS only; with the new format attribute they fail -Werror
builds on 32-bit hosts (RPi, 32-bit Windows), and %lu for uint64_t also on
64-bit Windows. unsigned long printed with PRIu64 likewise.
Found by a 32-bit (arm-none-eabi) syntax-only pass over the client.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add a printf format attribute to PrintAndLogEx so gcc/clang flag
argument/specifier mismatches (previous commit fixed the 34 existing ones).
On mingw use __MINGW_PRINTF_FORMAT, matching the GNU printf selected by
_ISOC99_SOURCE rather than ms_printf.
PrintAndLogEx(x, "") is the established way to print an empty line, so
-Wno-format-zero-length is added to every build that compiles callers:
client Makefile, client CMake, experimental_lib, and cliparser (Makefile
and cmake).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Found by building with a printf format attribute on PrintAndLogEx.
User visible:
- hw version: GENERIC device/firmware name was passed as an unused arg
and never printed
- hf saflok selftest: PRIx32 lacked '%', printed a literal 'x'
- hf mfp: key index value was never printed
- hf waveshare: output filename was never printed
Also: size_t/int64_t/uint64_t specifiers (%zu, PRId64, PRIX64), int
macros printed with %zu, ' ' flag on %u, unused arg in flash_prepare,
and non-literal strings passed as the format.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The save-state self tests and add_more_candidates() dereferenced calloc
results without a NULL check. Follow each file's existing OOM handling.
Found by cppcheck 2.22.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
%llu reads a 64-bit argument for a size_t, which is only correct on LP64
hosts; 32-bit builds (e.g. RPi Zero, 32-bit Windows) print garbage.
Found by cppcheck 2.22.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The decrypted/selected ADF strings were strdup'd and uppercased for the
compare, but never freed on either return path and used without NULL check.
Use strcasecmp on the originals instead. Found by cppcheck 2.22.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
set_my_executable_path() checked my_executable_path instead of the freshly
allocated my_executable_directory, so a failed calloc led to strncpy on NULL.
Found by cppcheck 2.22.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Start and End columns were unsigned differences to the first frame,
so a frame stamped before it or after the timer restarted showed as
about 4294967296, eg -281 in traces/lf_hitag_crypto_dump.trace and
-29708 in traces/hf_14b_cryptorf_select.trace. Same as the frame delay
time in d11643c8f.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Most `xx list` aliases pass "<protocol> -c", so adding -c yourself, eg
`hf 14a list -c`, gave "extraneous option -c". Allow up to two.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Both lived in extract_uid, so a 14a anticollision after an iCLASS SELECT
overwrote the CSN and its CHECK gave no lookup line. Also take CL2 only
after a CL1 with the cascade tag, and CL3 after such a CL2, instead of
printing a UID made of whatever the buffer held.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
extractChall_ev2() only took a 21 byte tag reply, ie wrapped framing
without CID, so a native EV2 First / Non First auth, or one with a CID,
printed no challenge. Locate it like the AES branch does.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
loadFile_safeEx, loadFile_TXTsafe, loadFileEML_safe and loadFileMCT_safe
freed *pdata on a short read or a failed realloc but left the caller's
pointer set, so a caller keeping it freed it again. Same as
loadFileDICTIONARY_safe_ex already does.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The -r and --frame delay was an unsigned difference, so a frame starting
before the end of the previous one, or after the timer restarted, eg
between the sessions of traces/hf_14b_xerox_info.trace or the frames of
traces/lf_HitagS256_dump.trace, showed as about 4294967296 instead of
-83024 or -117.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With -r the Data header said "' denotes short bytes", but short bytes
are marked (n), and in both modes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It returned PM3_SUCCESS when the trace download or writing the file
failed, so scripts carried on as if the trace was saved.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CmdTraceListAlias() built "-t <protocol> <args>" in a 128 byte buffer,
so a longer command line was cut silently, eg `hf mf list -f` with a
long dictionary path tried to load a truncated file name and fell back
to the default keys. Allocate the buffer to fit.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The trace buffer was cleared before the load, so a missing or empty
file lost the trace already loaded. Loading straight into gs_trace also
left it dangling when loadFile_safe() freed it on a short read, to be
freed again by the next clear or load. Load into a local buffer and
swap it in on success.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A PM5 BigBuf is several hundred KiB, so its traces outgrow the 16 bit
trace length and record positions. The device already reports the
length as 32 bit in download_done_t.extra, but download_trace_ex() kept
it in a uint16_t and cut it modulo 65536, as `trace load` did for files.
Make the client side trace length and every record position 32 bit, and
drop the 64 KiB refusal from d36d821c5.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The trace buffer length and all record positions are 16 bit. `trace
load` and the spiffs import (ImportTraceBuffer took a uint16_t, its
caller passes a uint32_t) cut a longer length modulo 65536, eg a 70005
byte file loaded as 4469 bytes. Report it and keep the buffer empty.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Same order as the native branch. No change in output, the reply is a
tag frame the next pass ignores anyway.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
They were matched on the low nibble of the first byte only. A 14a CL2
SELECT, 95 70 UID BCC CRC, is 9 bytes like a CHECK and was printed as
its MACs, and a MIFARE AUTH B, 61 .., was taken as a SELECT and ate the
frame after it.
A SELECT now needs CSN[8] and a reply with a valid iCLASS CRC, which is
only skipped when it matched. A CHECK carries no CRC, so a frame with a
valid CRC_A is not one.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
They are static and were never cleared, so an iCLASS CHECK in a trace
without a SELECT gave an `hf iclass lookup` line with the CSN and epurse
of a trace extracted earlier.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Both ignored the result of download_trace(). After a timeout they went
on with whatever the client side trace buffer held, ie an older trace,
under a fresh "Recorded activity" line. Offline they printed the failed
upload error and then listed the buffer anyway.
Download only when a device is present and return its error, offline
uses the trace buffer like `trace save` does. The "removing parameter
-1" hint is only given when -1 was.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Any reader frame with the top two bits clear was searched for an auth
command at its first two INF bytes. A MIFARE Ultralight READBLOCK(10),
30 0A CRC, was reported as a native DESFire auth with the CRC byte as
key number and page data as challenge (traces/hf_14a_mfuc.trace), and
so was a native SelectApplication of an AID starting with 0A.
Require an I-block PCB, take the command after CLA 90 only when the
frame is wrapped, and the key number before the CRC. long_jmp now
follows the framing instead of guessing it from the frame length.
The switch loses one indent level, see git diff -w.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The ISO auth branch took a tag reply longer than 20 bytes as 3TDEA. A
native 3TDEA reply is 20 bytes, so it was shown as 2TDEA with half the
challenge. Use what is left after the framing, SW and CRC instead.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The OP card manager SELECT built its sAPDU_t from a static AID, so
clang emitted a constant template with an absolute pointer at offset 5
of the PACKED struct. Apple ld rejects it:
ld: pointer not aligned in 'l___const.gp_info.op'+0x5 (cmdhfgp.o)
Keep the AID on the stack, like the other sAPDU_t users.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The tag challenge of the DES, ISO and AES auth branches was read from
frame + 1. In native framing that is the AF status byte, and with a CID
it is the CID byte, so the challenge came out shifted by one, eg
traces/hf_mfdes_sniff.trace gave 000675359294E7CD instead of
0675359294E7CDA1.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The MFDES AES auth branch printed 8 of the 16 RndB bytes and 16 of the
32 reader bytes, and accepted a 9 byte tag reply.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The retry check estimated the next attempt from the longest one so far.
That does not bound it:
- get_pulse_length() waits up to 4 bit periods
- one find_double_listen_window() can take about 3.2 s with no signal
- a login + read attempt can take over 6 s
- setup before tstart was not counted
A slow attempt after fast failures could end after the client gave up,
leaving the late reply queued for the next command.
read and info now set a deadline after setup, 1 s before the client
timeout. The listen window, ACK and word decode loops stop at the
deadline, as do the retries. With no deadline set (write, brute, sim,
...) they behave as before. The client waits 5 s for read, as it
already did for info / dump.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hf gp info also tries the OpenPlatform card manager AID
A000000003000000 before falling back to the default applet. That
fallback now warns and is titled "Default applet" instead of
"Security Domain".
hf gp aids returns PM3_ESOFT when the card accepts any SELECT, instead
of PM3_SUCCESS.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- SW1 SW2 colouring wrote past line[] / sw_escapes[] for responses over
648 bytes. It is now limited to the rows actually printed (324 bytes),
like the CRC marker.
- The session opens on the ATS, not on RATS, so an ATS whose TL byte
looks like an I-block PCB is no longer decoded as an APDU response.
If no ATS was captured, the next reader frame opens the session.
- A command is decoded as an APDU only if Lc / Le matches the INF
length (ISO 7816-4 cases 1-4, short and extended). A response is
decoded only when it answers such a command. This keeps native
DESFire and similar I-blocks from being labelled as APDUs or coloured
as status words.
- `trace list -t 7816` and `-t fmcos` reset the 14a-4 session state
at the start, like the other 14a listings.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Next attempt must fit in 2.5 s, judged by the longest attempt so far.
Client waits 5 s for info/dump.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
New `hf gp` command family. Every subcommand works without keys and
never sends INITIALIZE UPDATE / EXTERNAL AUTHENTICATE, so no card
retry counter is used. All take -w for the contact interface, -a to
log APDUs and -v for verbose output.
hf gp info
Selects the ISD, or the default applet when A000000151000000 is
absent, and reads:
- Card Recognition Data, decoding the GP version, SCP and Java Card
version from their OIDs
- CPLC, with the fabricator name and BCD dates decoded
- the Key Information Template (KVN, key id, type, length, with
SCP02 / SCP03 key sets marked)
- IIN, CIN and the SCP sequence counter
GET DATA retries with CLA 00 on cards that reject CLA 80.
hf gp crs
GET STATUS on the Contactless Registry Service (GPC_SPE_025 3.11),
following 6310 to fetch more data. Lists each applet's AID,
lifecycle, contactless activation state, display data, URL, group
and CREL members. --aid filters on a full or partial AID.
hf gp aids
Finds applet AIDs by partial SELECT (P2=00, first occurrence):
- finds the shortest prefix the card accepts
- tries aidlist.json prefixes and every registered RID A0 00 00 0x xx
(--quick skips the RID sweep)
- extends each hit one byte at a time to the full AID
- --prefix limits the search to one subtree
Cards whose default applet accepts any SELECT are detected and
refused. The walk is capped at 200000 APDUs and can be stopped with
<Enter>. Without partial selection it falls back to the full
aidlist.json AIDs.
aidlist.json gains the GP ISD, CRS and ARA-M AIDs, plus USIM, ISIM,
CSIM, UnionPay, and ICCOA so found AIDs get named. The CCC digital key entry gets a description.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Inside an ISO14443-4 session, now decodes I-blocks
as APDUs instead of passing them to the plain 14a annotator.
- A session opens on RATS and closes on WUPA, REQA, HLTA, S(DESELECT)
or a new trace. Only I-blocks with a valid CRC_A are decoded.
- Commands are named by INS (SELECT FILE, GET CHALLENGE, ...), or shown
as 'APDU CLA xx INS yy'. The first block of a chain is marked
'(chained)'. Continuation blocks are skipped.
- CID and NAD bytes are skipped. CLA FF and INS 6x / 9x are rejected
as not being APDUs.
- Responses show the status word text only for errors. 9000 and 61xx
stay quiet.
- The reader's S(DESELECT) is labelled.
- With colours on, SW1 SW2 of a response APDU are shown in cyan.
annotateIso14443aLastHasSW() tells cmdtrace when the last frame had a
status word.
The ISO7816 INS name switch moves into iso7816_ins_name(), shared by
annotateIso14443a() and annotateIso7816(). The output of
does not change.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
clone and sim encoded only 7 BCD digits of the CN and the last two
digits of the year, which the demod maps to 1961-2060, so
--cn 12345678 wrote card 2345678 and --year 1950 the year 2050.
Both values were also cut to 32 and 16 bits first.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Implemented passive mode for COTAG sim:
- load modulation with an fc/2 subcarrier locked to the reader carrier.
- This mode is now a default as it has several advantages:
- phase coherent response,
- can detect reader field drop mid frame "transmission" and restart
immediately -- important with some readers that may drop field
early and send reactivation shortly afterwards expecting the
tag to respond there as well.
- "active tx mode" is now via CLI toggle --active: it may give better range,
but may not work with all readers.
- Improved COTAG activation sequence detection for a wider range of COTAG
readers that may vary slightly in activation sequence timings (thresholds
for the rise-to-rise mode).
ASKDemod_ext() was called with maxErr 0. The error is most often the
cut first bit of the read, outside every frame. cmdlfgallagher.c passes
100 in the same call since dfb7eaf06, and a frame still has to pass
both checksums.
A 0xBB0314FF first block, which getnoralsyBits() notes as seen, no
longer gets the unknown bits warning. The example in the comment is
such a card, its hex column said BB0214FF.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The page check covers the Hitag 2, S and µ `wrbl`, the data check S and
µ. `htu rdbl -c` stops at the 55 blocks the reply holds, `hts -m` above
3 is refused, `ta -h` shows its help and argument errors free the
parser.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Annotators, `trace extract`, `-x` and `-c` stay inside the frame, the
trace and the row buffer, `-t ht2` inside the Hitag 2 state, and frame
lengths are 16 bit wide. Topaz, FeliCa and five byte ISO14443-A frames
get the right CRC check, Topaz and iCLASS traces tell their ISO14443-A
and ISO15693 frames apart, and the SEOS, 14b, DESFire and Hitag µ
labels are corrected. Six make check expectations on the repository
traces cover the fixes.
Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>