HID SAM: return CSN / UID and the plaintext SIO with the PACS

Seader keeps the SIO it sees on the air while the SAM reads a card, which
is what lets it write an iCLASS SR downgrade. Do the same:

- iCLASS: READ / READ4 answers from block 6 on are kept; the SIO is the
  lowest captured block holding a SEQUENCE whose DER length is covered by
  contiguous captured blocks (so a legacy block 7 that happens to start
  with 0x30 is not taken for it).
- DESFire: the ReadData of file 0x0F Seader keys on, plus 91 AF
  continuations (Seader only keeps the first frame).

A new request flag (bit 7 for CMD_HF_SAM_PICOPASS, bit 2 for
CMD_HF_SAM_SEOS) asks for a sam_ext_reply_hdr_t ahead of the SAM
response. It is only sent for a plain PACS request, so -d, --info and
--break replies are unchanged. The capture lives in .bss and the reply
is built in BigBuf to keep the RDV4 stack untouched.

Also from Seader: a DESFire EV2 answers the Seos select with a 6F 22 85 20
FCI. Hide it from the SAM as 6A 82 and add Le to the DESFire select.

sam_set_card_detected_seos() moves to sam_common.c as
sam_set_card_detected_iso14a() so the MIFARE Classic path can share it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
iceman1001andClaude Opus 5.5 committed 2026-11-10 10:23:39 +07:00
1 parent 6100a46f12
commit d2e077ded9
5 files changed
+296 -81

No files matched your search

+234
View File
@@ -30,6 +30,7 @@
#include "iso15693.h"
#include "protocols.h"
#include "crc16.h"
#include "cmd.h"
/**
@@ -941,3 +942,236 @@ uint16_t sam_copy_payload_sam2nfc(uint8_t *nfc_tx_buf, uint8_t *sam_rx_buf, uint
memcpy(nfc_tx_buf, sam_rx_buf + p + 6, nfc_tx_len);
return nfc_tx_len;
}
/**
* @brief Sets the card detected status for the SAM (Secure Access Module).
*
* This function informs that a card has been detected by the reader and
* initializes SAM communication with the card.
*
* @param card_select Pointer to the descriptor of the detected card.
* @param with_atqa Include the ATQA node. Seader leaves it out for MIFARE Classic.
* @return Status code indicating success or failure of the operation.
*/
int sam_set_card_detected_iso14a(const iso14a_card_select_t *card_select, bool with_atqa) {
int res = PM3_SUCCESS;
if (g_dbglevel >= DBG_DEBUG) {
DbpString("start sam_set_card_detected");
}
uint8_t *request = BigBuf_calloc(ISO7816_MAX_FRAME);
uint16_t request_len = ISO7816_MAX_FRAME;
uint8_t *response = BigBuf_calloc(ISO7816_MAX_FRAME);
uint16_t response_len = ISO7816_MAX_FRAME;
if (request == NULL || response == NULL) {
res = PM3_EMALLOC;
goto out;
}
const uint8_t payload[] = {
0xa0, 8, // <- SAM command
0xad, 6, // <- set detected card
0xa0, 4, // <- detected card details
0x80, 2, // <- protocol
0x00, 0x02 // <- ISO14443A
};
memcpy(request, payload, sizeof(payload));
sam_append_asn1_node(request, request + 4, 0x81, card_select->uid, card_select->uidlen);
if (with_atqa) {
sam_append_asn1_node(request, request + 4, 0x82, card_select->atqa, 2);
}
sam_append_asn1_node(request, request + 4, 0x83, &card_select->sak, 1);
request_len = request[1] + 2;
sam_send_payload(
0x44, 0x0a, 0x44,
request,
&request_len,
response,
&response_len
);
// resp:
// c1 64 00 00 00
// bd 02 <- response
// 8a 00 <- empty response (accepted)
// 90 00
if (sam_bd_offset(response, response_len) == 0) {
if (g_dbglevel >= DBG_ERROR)
Dbprintf("Invalid SAM response");
goto error;
} else {
// uint8_t * sam_response_an = sam_find_asn1_node(response + 5, 0x8a);
// if(sam_response_an == NULL){
// if (g_dbglevel >= DBG_ERROR)
// Dbprintf("Invalid SAM response");
// goto error;
// }
goto out;
}
error:
res = PM3_ESOFT;
out:
BigBuf_free();
if (g_dbglevel >= DBG_DEBUG) {
DbpString("end sam_set_card_detected");
}
return res;
}
// Plaintext seen on the air during a SAM conversation, for the extended reply.
#define SAM_CAP_FIRST_BLOCK 6
#define SAM_CAP_BLOCKS (SAM_EXT_SIO_MAX / 8)
static struct {
uint8_t uid[10];
uint8_t uid_len;
uint8_t data[SAM_EXT_SIO_MAX];
uint16_t len; // DESFire: bytes captured
uint32_t block_map; // iCLASS: bit n set when block 6 + n was captured
bool desfire_more; // DESFire: last read answered 91 AF
} sam_cap;
void sam_capture_reset(void) {
memset(&sam_cap, 0, sizeof(sam_cap));
}
void sam_capture_uid(const uint8_t *uid, uint8_t uid_len) {
sam_cap.uid_len = MIN(uid_len, sizeof(sam_cap.uid));
memcpy(sam_cap.uid, uid, sam_cap.uid_len);
}
// iCLASS READ / READ4 answers from block 6 on. rx is the tag answer.
void sam_capture_iclass(const uint8_t *tx, uint16_t tx_len, const uint8_t *rx, uint16_t rx_len) {
if (tx_len < 2) {
return;
}
uint8_t blocks;
if ((tx[0] & 0x0F) == ICLASS_CMD_READ_OR_IDENTIFY && rx_len >= 8) {
blocks = 1;
} else if ((tx[0] & 0x0F) == ICLASS_CMD_READ4 && rx_len >= 32) {
blocks = 4;
} else {
return;
}
for (uint8_t i = 0; i < blocks; i++) {
uint16_t blk = tx[1] + i;
if (blk < SAM_CAP_FIRST_BLOCK || blk >= SAM_CAP_FIRST_BLOCK + SAM_CAP_BLOCKS) {
continue;
}
uint8_t n = blk - SAM_CAP_FIRST_BLOCK;
memcpy(sam_cap.data + (n * 8), rx + (i * 8), 8);
sam_cap.block_map |= (1u << n);
}
}
// DESFire ReadData of file 0x0F (what Seader keys on) plus its 91 AF continuations.
// rx is the R-APDU, SW included.
void sam_capture_desfire(const uint8_t *tx, uint16_t tx_len, const uint8_t *rx, uint16_t rx_len) {
static const uint8_t read_sio[] = { 0x90, 0xBD, 0x00, 0x00, 0x07, 0x0F, 0x00, 0x00, 0x00 };
static const uint8_t read_more[] = { 0x90, 0xAF, 0x00, 0x00, 0x00 };
if (rx_len < 2) {
sam_cap.desfire_more = false;
return;
}
bool first = (tx_len == 13 && memcmp(tx, read_sio, sizeof(read_sio)) == 0 && rx_len > 2 && rx[0] == 0x30);
bool more = (sam_cap.desfire_more && tx_len == sizeof(read_more) && memcmp(tx, read_more, sizeof(read_more)) == 0);
if (first) {
sam_cap.len = 0;
}
if (first || more) {
uint16_t n = MIN((uint16_t)(rx_len - 2), (uint16_t)(sizeof(sam_cap.data) - sam_cap.len));
memcpy(sam_cap.data + sam_cap.len, rx, n);
sam_cap.len += n;
}
sam_cap.desfire_more = (first || more) && rx[rx_len - 2] == 0x91 && rx[rx_len - 1] == 0xAF;
}
// DER length of the SIO at p, tag and length bytes included. 0 if unusable.
static uint16_t sam_sio_der_len(const uint8_t *p, uint16_t avail) {
if (avail < 2 || p[0] != 0x30) {
return 0;
}
uint16_t len;
if (p[1] < 0x80) {
len = p[1] + 2;
} else if (p[1] == 0x81 && avail >= 3) {
len = p[2] + 3;
} else {
return 0;
}
return (len <= avail) ? len : 0;
}
// The iCLASS SIO is the lowest captured block holding a SEQUENCE whose length
// is covered by contiguous captured blocks. Moves it to offset 0.
static uint16_t sam_capture_iclass_sio(uint8_t *start_block) {
for (uint8_t n = 0; n < SAM_CAP_BLOCKS; n++) {
if ((sam_cap.block_map & (1u << n)) == 0) {
continue;
}
uint8_t run = 0;
while ((n + run) < SAM_CAP_BLOCKS && (sam_cap.block_map & (1u << (n + run)))) {
run++;
}
uint16_t len = sam_sio_der_len(sam_cap.data + (n * 8), run * 8);
if (len) {
memmove(sam_cap.data, sam_cap.data + (n * 8), len);
*start_block = SAM_CAP_FIRST_BLOCK + n;
return len;
}
}
return 0;
}
// Sends the PACS reply, extended with UID and SIO when asked for. Falls back to
// the plain SAM response if the extended one cannot be built.
void sam_reply_pacs(uint16_t cmd, bool ext, const uint8_t *sam, uint8_t sam_len) {
if (ext == false) {
reply_ng(cmd, PM3_SUCCESS, sam, sam_len);
return;
}
sam_ext_reply_hdr_t hdr = {0};
hdr.magic = SAM_EXT_REPLY_MAGIC;
hdr.uid_len = sam_cap.uid_len;
memcpy(hdr.uid, sam_cap.uid, sam_cap.uid_len);
uint16_t sio_len = 0;
if (sam_cap.block_map) {
sio_len = sam_capture_iclass_sio(&hdr.sio_start);
} else if (sam_cap.len) {
sio_len = sam_sio_der_len(sam_cap.data, sam_cap.len);
}
hdr.sio_len = (uint8_t)sio_len;
hdr.sam_len = sam_len;
uint16_t n = sizeof(hdr) + sio_len + sam_len;
uint8_t *buf = BigBuf_calloc(n);
if (buf == NULL) {
reply_ng(cmd, PM3_SUCCESS, sam, sam_len);
return;
}
memcpy(buf, &hdr, sizeof(hdr));
memcpy(buf + sizeof(hdr), sam_cap.data, sio_len);
memcpy(buf + sizeof(hdr) + sio_len, sam, sam_len);
reply_ng(cmd, PM3_SUCCESS, buf, n);
}
+10
View File
@@ -17,6 +17,7 @@
#define __SAM_COMMON_H
#include "common.h"
#include "mifare.h"
static const uint8_t SAM_TX_APDU_PREFIX_LENGTH = 5;
static const uint8_t SAM_TX_ASN1_PREFIX_LENGTH = 6;
@@ -94,4 +95,13 @@ int sam_relay_iso15_loop(
bool *got_nr_mac
);
int sam_set_card_detected_iso14a(const iso14a_card_select_t *card_select, bool with_atqa);
// Capture of UID and plaintext SIO for the extended PACS reply (sam_ext_reply_hdr_t)
void sam_capture_reset(void);
void sam_capture_uid(const uint8_t *uid, uint8_t uid_len);
void sam_capture_iclass(const uint8_t *tx, uint16_t tx_len, const uint8_t *rx, uint16_t rx_len);
void sam_capture_desfire(const uint8_t *tx, uint16_t tx_len, const uint8_t *rx, uint16_t rx_len);
void sam_reply_pacs(uint16_t cmd, bool ext, const uint8_t *sam, uint8_t sam_len);
#endif
+9 -1
View File
@@ -172,6 +172,8 @@ static int sam_send_request_iso15(const uint8_t *const request, const uint8_t re
DbpString("ISO15 TAG RESPONSE: ");
Dbhexdump(nfc_rx_len, nfc_rx_buf, false);
}
sam_capture_iclass(nfc_tx_buf, nfc_tx_len, nfc_rx_buf, nfc_rx_len);
}
@@ -633,6 +635,7 @@ int sam_picopass_get_pacs(PacketCommandNG *c) {
const bool shallow_mod = !!(flags & BITMASK(4));
const bool info = !!(flags & BITMASK(5));
const bool emulate_from_file = !!(flags & BITMASK(6));
const bool ext_reply = !!(flags & BITMASK(7));
uint8_t *cmd = c->data.asBytes + 1;
uint16_t cmd_len = c->length - 1;
@@ -642,6 +645,7 @@ int sam_picopass_get_pacs(PacketCommandNG *c) {
uint8_t sam_response_len = 0;
clear_trace();
sam_capture_reset();
// This path resets the SAM, invalidating any secure-channel state held by
// CMD_HF_SAM_SC. Ensure the next SC request reopens instead of sending
// into a reset SAM with a stale routing/session flag.
@@ -686,6 +690,8 @@ int sam_picopass_get_pacs(PacketCommandNG *c) {
card_a_info.csn[6], card_a_info.csn[7]);
}
sam_capture_uid(card_a_info.csn, sizeof(card_a_info.csn));
// step 2: SamCommand CardDetected using CSN from dump
sam_set_card_detected_picopass(&card_a_info);
@@ -705,6 +711,8 @@ int sam_picopass_get_pacs(PacketCommandNG *c) {
switch_clock_to_ticks();
sam_capture_uid(card_a_info.csn, sizeof(card_a_info.csn));
// step 3: SamCommand CardDetected
sam_set_card_detected_picopass(&card_a_info);
}
@@ -728,7 +736,7 @@ err:
goto off;
out:
reply_ng(CMD_HF_SAM_PICOPASS, PM3_SUCCESS, sam_response, sam_response_len);
sam_reply_pacs(CMD_HF_SAM_PICOPASS, ext_reply && (cmd_len == 0) && (breakOnNrMac == false) && (info == false), sam_response, sam_response_len);
off:
if (disconnectAfter) {
+29 -80
View File
@@ -43,84 +43,6 @@
#include "cmd.h"
/**
* @brief Sets the card detected status for the SAM (Secure Access Module).
*
* This function informs that a card has been detected by the reader and
* initializes SAM communication with the card.
*
* @param card_select Pointer to the descriptor of the detected card.
* @return Status code indicating success or failure of the operation.
*/
static int sam_set_card_detected_seos(iso14a_card_select_t *card_select) {
int res = PM3_SUCCESS;
if (g_dbglevel >= DBG_DEBUG) {
DbpString("start sam_set_card_detected");
}
uint8_t *request = BigBuf_calloc(ISO7816_MAX_FRAME);
uint16_t request_len = ISO7816_MAX_FRAME;
uint8_t *response = BigBuf_calloc(ISO7816_MAX_FRAME);
uint16_t response_len = ISO7816_MAX_FRAME;
if (request == NULL || response == NULL) {
res = PM3_EMALLOC;
goto out;
}
const uint8_t payload[] = {
0xa0, 8, // <- SAM command
0xad, 6, // <- set detected card
0xa0, 4, // <- detected card details
0x80, 2, // <- protocol
0x00, 0x02 // <- ISO14443A
};
memcpy(request, payload, sizeof(payload));
sam_append_asn1_node(request, request + 4, 0x81, card_select->uid, card_select->uidlen);
sam_append_asn1_node(request, request + 4, 0x82, card_select->atqa, 2);
sam_append_asn1_node(request, request + 4, 0x83, &card_select->sak, 1);
request_len = request[1] + 2;
sam_send_payload(
0x44, 0x0a, 0x44,
request,
&request_len,
response,
&response_len
);
// resp:
// c1 64 00 00 00
// bd 02 <- response
// 8a 00 <- empty response (accepted)
// 90 00
if (sam_bd_offset(response, response_len) == 0) {
if (g_dbglevel >= DBG_ERROR)
Dbprintf("Invalid SAM response");
goto error;
} else {
// uint8_t * sam_response_an = sam_find_asn1_node(response + 5, 0x8a);
// if(sam_response_an == NULL){
// if (g_dbglevel >= DBG_ERROR)
// Dbprintf("Invalid SAM response");
// goto error;
// }
goto out;
}
error:
res = PM3_ESOFT;
out:
BigBuf_free();
if (g_dbglevel >= DBG_DEBUG) {
DbpString("end sam_set_card_detected");
}
return res;
}
/**
* @brief Sends a request to the SAM and retrieves the response.
*
@@ -183,12 +105,23 @@ static int sam_send_request_iso14a(const uint8_t *const request, const uint8_t r
sam_rx_buf, &sam_rx_len
);
// Seader's DESFire EV2 handling: an EV2 answers the Seos select with this
// FCI, which is hidden from the SAM, and wants Le on the DESFire select.
static const uint8_t select_seos[] = { 0x00, 0xa4, 0x04, 0x00, 0x0a, 0xa0, 0x00, 0x00, 0x04, 0x40, 0x00, 0x01, 0x01, 0x00, 0x01, 0x00 };
static const uint8_t select_desfire_no_le[] = { 0x00, 0xA4, 0x04, 0x00, 0x07, 0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x00 };
static const uint8_t ev2_fci[] = { 0x6F, 0x22, 0x85, 0x20 };
bool is_ev2 = false;
if (sam_relay_pending(sam_rx_buf, sam_rx_len)) { // commands to be relayed to card starts with 0x61
// tag <-> SAM exchange starts here
while (sam_relay_pending(sam_rx_buf, sam_rx_len)) {
switch_clock_to_countsspclk();
nfc_tx_len = sam_copy_payload_sam2nfc(nfc_tx_buf, sam_rx_buf, sam_rx_len);
if (is_ev2 && nfc_tx_len == sizeof(select_desfire_no_le) && memcmp(nfc_tx_buf, select_desfire_no_le, nfc_tx_len) == 0) {
nfc_tx_buf[nfc_tx_len++] = 0x00;
}
int nfc_res = iso14_apdu(nfc_tx_buf, nfc_tx_len, false, nfc_rx_buf, ISO7816_MAX_FRAME, NULL);
switch_clock_to_ticks();
@@ -204,6 +137,18 @@ static int sam_send_request_iso14a(const uint8_t *const request, const uint8_t r
}
nfc_rx_len = (uint16_t)nfc_res;
// R-APDU length, CRC stripped
if (nfc_tx_len == sizeof(select_seos) && memcmp(nfc_tx_buf, select_seos, nfc_tx_len) == 0 &&
nfc_rx_len - 2 == 38 && memcmp(nfc_rx_buf, ev2_fci, sizeof(ev2_fci)) == 0) {
is_ev2 = true;
nfc_rx_buf[0] = 0x6A;
nfc_rx_buf[1] = 0x82;
nfc_rx_len = 2 + 2;
}
sam_capture_desfire(nfc_tx_buf, nfc_tx_len, nfc_rx_buf, nfc_rx_len - 2);
sam_tx_len = sam_copy_payload_nfc2sam(sam_tx_buf, nfc_rx_buf, nfc_rx_len - 2);
sam_send_payload(0x14, 0x0a, 0x14, sam_tx_buf, &sam_tx_len, sam_rx_buf, &sam_rx_len);
@@ -289,6 +234,7 @@ int sam_seos_get_pacs(PacketCommandNG *c) {
const uint8_t flags = c->data.asBytes[0];
const bool disconnectAfter = !!(flags & BITMASK(0));
const bool skipDetect = !!(flags & BITMASK(1));
const bool extReply = !!(flags & BITMASK(2));
uint8_t *cmd = c->data.asBytes + 1;
uint16_t cmd_len = c->length - 1;
@@ -297,6 +243,7 @@ int sam_seos_get_pacs(PacketCommandNG *c) {
int res = PM3_EFAILED;
clear_trace();
sam_capture_reset();
// Like the PICOPASS dispatcher, this reset destroys a live SC session.
// Mark it stale so the next CMD_HF_SAM_SC call performs its reset/ATR
// synchronization path before sending a secured APDU.
@@ -334,8 +281,10 @@ int sam_seos_get_pacs(PacketCommandNG *c) {
switch_clock_to_ticks();
sam_capture_uid(card_a_info.uid, card_a_info.uidlen);
// step 3: SamCommand CardDetected
sam_set_card_detected_seos(&card_a_info);
sam_set_card_detected_iso14a(&card_a_info, true);
}
// step 3: SamCommand RequestPACS, relay NFC communication
@@ -358,7 +307,7 @@ err:
goto off;
out:
reply_ng(CMD_HF_SAM_SEOS, PM3_SUCCESS, sam_response, sam_response_len);
sam_reply_pacs(CMD_HF_SAM_SEOS, extReply && (cmd_len == 0), sam_response, sam_response_len);
off:
if (disconnectAfter) {
+14
View File
@@ -340,6 +340,20 @@ typedef struct {
bool EM;
} PACKED lf_hidsim_t;
// Extended PACS reply of CMD_HF_SAM_PICOPASS (flag bit 7), CMD_HF_SAM_SEOS and
// CMD_HF_SAM_MFC (flag bit 2). Followed by sio_len SIO bytes, then sam_len
// bytes of SAM response. Only sent for a plain PACS request.
#define SAM_EXT_REPLY_MAGIC 0xE5
#define SAM_EXT_SIO_MAX 128
typedef struct {
uint8_t magic;
uint8_t uid_len;
uint8_t uid[10]; // CSN for iCLASS, UID for ISO14443-A
uint8_t sio_start; // iCLASS block the SIO was read from, 0 otherwise
uint8_t sio_len; // plaintext SIO seen on the air, 0 if none
uint8_t sam_len;
} PACKED sam_ext_reply_hdr_t;
// For CMD_LF_FSK_SIMULATE (FSK)
typedef struct {
uint8_t fchigh;