From 498c4b83776cbb777f3f58815241e1f2350b0cda Mon Sep 17 00:00:00 2001 From: Thomas Sutter Date: Tue, 29 Oct 2019 10:22:47 +0100 Subject: [PATCH] Refactor FeliCa structs. --- armsrc/felica.h | 2 -- client/cmdhffelica.c | 33 ++++++++++++++++++++++++--------- client/cmdhffelica.h | 1 + include/mifare.h | 22 ++++++++++++---------- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/armsrc/felica.h b/armsrc/felica.h index 95e13a588..ef34dc108 100644 --- a/armsrc/felica.h +++ b/armsrc/felica.h @@ -18,7 +18,5 @@ void felica_sendraw(PacketCommandNG *c); void felica_sniff(uint32_t samplesToSkip, uint32_t triggersToSkip); void felica_sim_lite(uint64_t uid); void felica_dump_lite_s(); -void felica_create_read_block_frame(uint16_t blockNr); -void felica_send_request_service(uint8_t *request_service); #endif diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index 18a992ba7..74fea45bb 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -344,7 +344,7 @@ static bool add_param(const char *Cmd, uint8_t paramCount, uint8_t *data, uint8_ * @param rd_noCry_resp Response frame. */ static void print_rd_noEncrpytion_response(felica_read_without_encryption_response_t *rd_noCry_resp) { - if (rd_noCry_resp->status_flag1[0] == 00 && rd_noCry_resp->status_flag2[0] == 00) { + if (rd_noCry_resp->status_flags.status_flag1[0] == 00 && rd_noCry_resp->status_flags.status_flag2[0] == 00) { char *temp = sprint_hex(rd_noCry_resp->block_data, sizeof(rd_noCry_resp->block_data)); char bl_data[256]; strcpy(bl_data, temp); @@ -354,9 +354,9 @@ static void print_rd_noEncrpytion_response(felica_read_without_encryption_respon strcpy(bl_element_number, temp); PrintAndLogEx(NORMAL, "\t%s\t| %s ", bl_element_number, bl_data); } else { - PrintAndLogEx(NORMAL, "IDm: %s", sprint_hex(rd_noCry_resp->IDm, sizeof(rd_noCry_resp->IDm))); - PrintAndLogEx(NORMAL, "Status Flag1: %s", sprint_hex(rd_noCry_resp->status_flag1, sizeof(rd_noCry_resp->status_flag1))); - PrintAndLogEx(NORMAL, "Status Flag2: %s", sprint_hex(rd_noCry_resp->status_flag1, sizeof(rd_noCry_resp->status_flag1))); + PrintAndLogEx(NORMAL, "IDm: %s", sprint_hex(rd_noCry_resp->frame_response.IDm, sizeof(rd_noCry_resp->frame_response.IDm))); + PrintAndLogEx(NORMAL, "Status Flag1: %s", sprint_hex(rd_noCry_resp->status_flags.status_flag1, sizeof(rd_noCry_resp->status_flags.status_flag1))); + PrintAndLogEx(NORMAL, "Status Flag2: %s", sprint_hex(rd_noCry_resp->status_flags.status_flag1, sizeof(rd_noCry_resp->status_flags.status_flag1))); } } @@ -374,9 +374,9 @@ int send_request_service(uint8_t flags, uint16_t datalen, uint8_t *data, bool ve felica_request_service_response_t rqs_response; memcpy(&rqs_response, (felica_request_service_response_t *)resp.data.asBytes, sizeof(felica_request_service_response_t)); - if (rqs_response.IDm[0] != 0) { + if (rqs_response.frame_response.IDm[0] != 0) { PrintAndLogEx(SUCCESS, "\nGot Service Response:"); - PrintAndLogEx(NORMAL, "IDm: %s", sprint_hex(rqs_response.IDm, sizeof(rqs_response.IDm))); + PrintAndLogEx(NORMAL, "IDm: %s", sprint_hex(rqs_response.frame_response.IDm, sizeof(rqs_response.frame_response.IDm))); PrintAndLogEx(NORMAL, " -Node Number: %s", sprint_hex(rqs_response.node_number, sizeof(rqs_response.node_number))); PrintAndLogEx(NORMAL, " -Node Key Version List: %s\n", sprint_hex(rqs_response.node_key_versions, sizeof(rqs_response.node_key_versions))); } @@ -423,6 +423,10 @@ static bool check_last_idm(uint8_t *data, uint16_t datalen){ } } + + + + /** * Command parser for wrunencrypted. * @param Cmd input data of the user. @@ -460,9 +464,20 @@ static int CmdHFFelicaWriteWithoutEncryption(const char *Cmd) { if (!custom_IDm && !check_last_idm(data, datalen)) { return PM3_EINVARG; } + // Number of Service 2, Service Code List 4, Number of Block 2, Block List Element 4 + uint8_t lengths[] = {2, 4, 2, 4}; + uint8_t dataPositions[] = {10, 11, 13, 14}; + for (int i = 0; i < 4; i++) { + if (add_param(Cmd, paramCount, data, dataPositions[i], lengths[i])) { + paramCount++; + } else { + return PM3_EINVARG; + } + } flags |= FELICA_APPEND_CRC; flags |= FELICA_RAW; + return PM3_SUCCESS; } @@ -542,7 +557,7 @@ static int CmdHFFelicaReadWithoutEncryption(const char *Cmd) { datalen += 2; felica_read_without_encryption_response_t rd_noCry_resp; if ((send_rd_unencrypted(flags, datalen, data, 0, &rd_noCry_resp) == PM3_SUCCESS)) { - if (rd_noCry_resp.status_flag1[0] == 00 && rd_noCry_resp.status_flag2[0] == 00) { + if (rd_noCry_resp.status_flags.status_flag1[0] == 00 && rd_noCry_resp.status_flags.status_flag2[0] == 00) { print_rd_noEncrpytion_response(&rd_noCry_resp); } else { break; @@ -615,9 +630,9 @@ static int CmdHFFelicaRequestResponse(const char *Cmd) { } else { felica_request_request_response_t rq_response; memcpy(&rq_response, (felica_request_request_response_t *)resp.data.asBytes, sizeof(felica_request_request_response_t)); - if (rq_response.IDm[0] != 0) { + if (rq_response.frame_response.IDm[0] != 0) { PrintAndLogEx(SUCCESS, "\nGot Request Response:"); - PrintAndLogEx(NORMAL, "IDm: %s", sprint_hex(rq_response.IDm, sizeof(rq_response.IDm))); + PrintAndLogEx(NORMAL, "IDm: %s", sprint_hex(rq_response.frame_response.IDm, sizeof(rq_response.frame_response.IDm))); PrintAndLogEx(NORMAL, " -Mode: %s\n\n", sprint_hex(rq_response.mode, sizeof(rq_response.mode))); } } diff --git a/client/cmdhffelica.h b/client/cmdhffelica.h index 7626d002b..8d8156006 100644 --- a/client/cmdhffelica.h +++ b/client/cmdhffelica.h @@ -18,4 +18,5 @@ int CmdHFFelica(const char *Cmd); int readFelicaUid(bool verbose); int send_request_service(uint8_t flags, uint16_t datalen, uint8_t *data, bool verbose); int send_rd_unencrypted(uint8_t flags, uint16_t datalen, uint8_t *data, bool verbose, felica_read_without_encryption_response_t *rd_noCry_resp); + #endif diff --git a/include/mifare.h b/include/mifare.h index 7a40a26e4..699c7e1ec 100644 --- a/include/mifare.h +++ b/include/mifare.h @@ -174,25 +174,27 @@ typedef struct { uint8_t length[1]; uint8_t cmd_code[1]; uint8_t IDm[8]; +} PACKED felica_frame_response_t; + +typedef struct { + uint8_t status_flag1[1]; + uint8_t status_flag2[1]; +} PACKED felica_status_flag_response_t; + +typedef struct { + felica_frame_response_t frame_response; uint8_t node_number[1]; uint8_t node_key_versions[2]; } PACKED felica_request_service_response_t; typedef struct { - uint8_t sync[2]; - uint8_t length[1]; - uint8_t cmd_code[1]; - uint8_t IDm[8]; + felica_frame_response_t frame_response; uint8_t mode[1]; } PACKED felica_request_request_response_t; typedef struct { - uint8_t sync[2]; - uint8_t length[1]; - uint8_t cmd_code[1]; - uint8_t IDm[8]; - uint8_t status_flag1[1]; - uint8_t status_flag2[1]; + felica_frame_response_t frame_response; + felica_status_flag_response_t status_flags; uint8_t number_of_block[1]; uint8_t block_data[16]; uint8_t block_element_number[1];