Files
iceman1001andClaude Opus 5.5 d2e077ded9 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>
2026-11-10 10:23:39 +07:00

321 lines
10 KiB
C

//-----------------------------------------------------------------------------
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// See LICENSE.txt for the text of the license.
//-----------------------------------------------------------------------------
// Routines to support SEOS <-> SAM communication
// communication and ASN.1 messages based on https://github.com/bettse/seader/blob/main/seader.asn1
//-----------------------------------------------------------------------------
#include "sam_seos.h"
#include "sam_common.h"
#include "util.h" // switch_clock_to_ticks / _countsspclk
#include "sam_sc.h"
#include "iclass.h"
#include "proxmark3_arm.h"
#include "iso14443a.h"
#include "iclass.h"
#include "crc16.h"
#include "proxmark3_arm.h"
#include "BigBuf.h"
#include "cmd.h"
#include "commonutil.h"
#include "ticks_apis.h"
#include "dbprint.h"
#include "i2c.h"
#include "protocols.h"
#include "optimized_cipher.h"
#include "fpga_loader.h"
#include "fpga_apis.h"
#include "pm3_cmd.h"
#include "cmd.h"
/**
* @brief Sends a request to the SAM and retrieves the response.
*
* Unpacks request to the SAM and relays ISO14A traffic to the card.
* If no request data provided, sends a request to get PACS data.
*
* @param request Pointer to the buffer containing the request to be sent to the SAM.
* @param request_len Length of the request to be sent to the SAM.
* @param response Pointer to the buffer where the retreived data will be stored.
* @param response_len Pointer to the variable where the length of the retreived data will be stored.
* @return Status code indicating success or failure of the operation.
*/
static int sam_send_request_iso14a(const uint8_t *const request, const uint8_t request_len, uint8_t *response, uint8_t *response_len) {
int res = PM3_SUCCESS;
if (g_dbglevel >= DBG_DEBUG) {
DbpString("start sam_send_request_iso14a");
}
uint8_t *buf1 = BigBuf_calloc(ISO7816_MAX_FRAME);
uint8_t *buf2 = BigBuf_calloc(ISO7816_MAX_FRAME);
if (buf1 == NULL || buf2 == NULL) {
res = PM3_EMALLOC;
goto out;
}
uint8_t *sam_tx_buf = buf1;
uint16_t sam_tx_len;
uint8_t *sam_rx_buf = buf2;
uint16_t sam_rx_len;
uint8_t *nfc_tx_buf = buf1;
uint16_t nfc_tx_len;
uint8_t *nfc_rx_buf = buf2;
uint16_t nfc_rx_len;
if (request_len > 0) {
sam_tx_len = request_len;
memcpy(sam_tx_buf, request, sam_tx_len);
} else {
// send get pacs
static const uint8_t payload[] = {
0xa0, 19, // <- SAM command
0xBE, 17, // <- samCommandGetContentElement2
0x80, 1,
0x04, // <- implicitFormatPhysicalAccessBits
0x84, 12,
0x2B, 0x06, 0x01, 0x04, 0x01, 0x81, 0xE4, 0x38, 0x01, 0x01, 0x02, 0x04 // <- SoRootOID
};
sam_tx_len = sizeof(payload);
memcpy(sam_tx_buf, payload, sam_tx_len);
}
sam_send_payload(
0x44, 0x0a, 0x44,
sam_tx_buf, &sam_tx_len,
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();
// A card that went away returns negative, which wrapped through
// uint16_t and was relayed on as a 253 byte answer.
if (nfc_res < 2) {
if (g_dbglevel >= DBG_ERROR) {
Dbprintf("SEOS relay: card exchange failed (%d)", nfc_res);
}
res = PM3_ECARDEXCHANGE;
goto out;
}
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);
// last SAM->TAG
// c1 61 c1 00 00 a1 02 >>82<< 00 90 00
if (sam_relay_complete(sam_rx_buf, sam_rx_len)) {
// tag <-> SAM exchange ends here
break;
}
}
static const uint8_t hfack[] = {
0xbd, 0x04, 0xa0, 0x02, 0x82, 0x00
};
sam_tx_len = sizeof(hfack);
memcpy(sam_tx_buf, hfack, sam_tx_len);
sam_send_payload(
0x14, 0x0a, 0x00,
sam_tx_buf, &sam_tx_len,
sam_rx_buf, &sam_rx_len
);
}
// resp for SamCommandGetContentElement:
// c1 64 00 00 00
// bd 09
// 8a 07
// 03 05 <- include tag for pm3 client
// 06 85 80 6d c0 <- decoded PACS data
// 90 00
// resp for samCommandGetContentElement2:
// c1 64 00 00 00
// bd 1e
// b3 1c
// a0 1a
// 80 05
// 06 85 80 6d c0
// 81 0e
// 2b 06 01 04 01 81 e4 38 01 01 02 04 3c ff
// 82 01
// 07
// 90 00
uint16_t plen = 0;
uint16_t ofs = sam_response_payload(sam_rx_buf, sam_rx_len, &plen);
if (request_len == 0) {
if (
!(sam_rx_buf[ofs] == 0xbd && sam_rx_buf[ofs + 2] == 0x8a && sam_rx_buf[ofs + 4] == 0x03) &&
!(sam_rx_buf[ofs] == 0xbd && sam_rx_buf[ofs + 2] == 0xb3 && sam_rx_buf[ofs + 4] == 0xa0)
) {
if (g_dbglevel >= DBG_ERROR) {
Dbprintf("No PACS data in SAM response");
}
res = PM3_ESOFT;
}
}
*response_len = (uint8_t)plen;
memcpy(response, sam_rx_buf + ofs, *response_len);
out:
BigBuf_free();
return res;
}
/**
* @brief Retrieves PACS data from SEOS card using SAM.
*
* This function is called by appmain.c
* It sends a request to the SAM to get the PACS data from the SEOS card.
* The PACS data is then returned to the PM3 client.
*
* @return Status code indicating success or failure of the operation.
*/
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;
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.
sam_sc_session_invalidate();
I2C_Reset_EnterMainProgram();
set_tracing(true);
StartTicks();
// Resetting the module resets the card, which then sends an ATR nobody
// asked for. Read it here rather than letting the first SAM APDU collide
// with it, and take the chance to say plainly when the slot holds
// something that is not a SAM.
smart_card_atr_t card;
if (GetATR(&card, false) == false) {
if (g_dbglevel >= DBG_ERROR) {
DbpString("SAM: no ATR - is a SAM in the slot?");
}
res = PM3_ECARDEXCHANGE;
goto err;
}
// step 1: ping SAM
sam_get_version(false);
if (skipDetect == false) {
// step 2: get card information
iso14a_card_select_t card_a_info;
// implicit StartSspClk() happens here
iso14443a_setup(FPGA_HF_ISO14443A_READER_MOD);
if (iso14443a_select_card(NULL, &card_a_info, NULL, true, 0, false) == 0) {
goto err;
}
switch_clock_to_ticks();
sam_capture_uid(card_a_info.uid, card_a_info.uidlen);
// step 3: SamCommand CardDetected
sam_set_card_detected_iso14a(&card_a_info, true);
}
// step 3: SamCommand RequestPACS, relay NFC communication
uint8_t sam_response[ISO7816_MAX_FRAME] = { 0x00 };
uint8_t sam_response_len = 0;
res = sam_send_request_iso14a(cmd, cmd_len, sam_response, &sam_response_len);
if (res != PM3_SUCCESS) {
goto err;
}
if (g_dbglevel >= DBG_INFO) {
print_result("Response data", sam_response, sam_response_len);
}
goto out;
err:
res = PM3_ENOPACS;
reply_ng(CMD_HF_SAM_SEOS, res, NULL, 0);
goto off;
out:
sam_reply_pacs(CMD_HF_SAM_SEOS, extReply && (cmd_len == 0), sam_response, sam_response_len);
off:
if (disconnectAfter) {
switch_off();
}
set_tracing(false);
StopTicks();
BigBuf_free();
return res;
}