From d0b9a3a4ae7abbb1a0e97616226d2b77d86f08e7 Mon Sep 17 00:00:00 2001 From: RebornedBrain <138568282+RebornedBrain@users.noreply.github.com> Date: Wed, 15 Nov 2023 11:02:35 +0300 Subject: [PATCH] [NFC] MF Ultralight no pwd polling adjustment (#3207) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Listener log level changed to Trace * Show pages count without pwd pages in case of no auth success * Fixed unit tests Co-authored-by: gornekich Co-authored-by: あく --- applications/debug/unit_tests/nfc/nfc_test.c | 19 +++++++++--- .../mf_ultralight/mf_ultralight_listener.c | 30 +++++++++---------- .../mf_ultralight/mf_ultralight_poller.c | 4 +++ 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/applications/debug/unit_tests/nfc/nfc_test.c b/applications/debug/unit_tests/nfc/nfc_test.c index 2d647f8ef56..ce5a9cb9cc5 100644 --- a/applications/debug/unit_tests/nfc/nfc_test.c +++ b/applications/debug/unit_tests/nfc/nfc_test.c @@ -203,10 +203,21 @@ static void mf_ultralight_reader_test(const char* path) { NfcDevice* nfc_device = nfc_device_alloc(); mu_assert(nfc_device_load(nfc_device, path), "nfc_device_load() failed\r\n"); - NfcListener* mfu_listener = nfc_listener_alloc( - listener, - NfcProtocolMfUltralight, - nfc_device_get_data(nfc_device, NfcProtocolMfUltralight)); + MfUltralightData* data = + (MfUltralightData*)nfc_device_get_data(nfc_device, NfcProtocolMfUltralight); + + uint32_t features = mf_ultralight_get_feature_support_set(data->type); + bool pwd_supported = + mf_ultralight_support_feature(features, MfUltralightFeatureSupportPasswordAuth); + uint8_t pwd_num = mf_ultralight_get_pwd_page_num(data->type); + const uint8_t zero_pwd[4] = {0, 0, 0, 0}; + + if(pwd_supported && !memcmp(data->page[pwd_num].data, zero_pwd, sizeof(zero_pwd))) { + data->pages_read -= 2; + } + + NfcListener* mfu_listener = nfc_listener_alloc(listener, NfcProtocolMfUltralight, data); + nfc_listener_start(mfu_listener, NULL, NULL); MfUltralightData* mfu_data = mf_ultralight_alloc(); diff --git a/lib/nfc/protocols/mf_ultralight/mf_ultralight_listener.c b/lib/nfc/protocols/mf_ultralight/mf_ultralight_listener.c index 70c6f6de2a8..5bef2a354c9 100644 --- a/lib/nfc/protocols/mf_ultralight/mf_ultralight_listener.c +++ b/lib/nfc/protocols/mf_ultralight/mf_ultralight_listener.c @@ -122,7 +122,7 @@ static MfUltralightCommand uint16_t pages_total = instance->data->pages_total; MfUltralightCommand command = MfUltralightCommandNotProcessedNAK; - FURI_LOG_D(TAG, "CMD_READ: %d", start_page); + FURI_LOG_T(TAG, "CMD_READ: %d", start_page); do { bool do_i2c_check = mf_ultralight_is_i2c_tag(instance->data->type); @@ -154,7 +154,7 @@ static MfUltralightCommand static MfUltralightCommand mf_ultralight_listener_fast_read_handler(MfUltralightListener* instance, BitBuffer* buffer) { MfUltralightCommand command = MfUltralightCommandNotProcessedSilent; - FURI_LOG_D(TAG, "CMD_FAST_READ"); + FURI_LOG_T(TAG, "CMD_FAST_READ"); do { if(!mf_ultralight_support_feature(instance->features, MfUltralightFeatureSupportFastRead)) @@ -206,7 +206,7 @@ static MfUltralightCommand uint16_t pages_total = instance->data->pages_total; MfUltralightCommand command = MfUltralightCommandNotProcessedNAK; - FURI_LOG_D(TAG, "CMD_WRITE"); + FURI_LOG_T(TAG, "CMD_WRITE"); do { bool do_i2c_check = mf_ultralight_is_i2c_tag(instance->data->type); @@ -235,7 +235,7 @@ static MfUltralightCommand static MfUltralightCommand mf_ultralight_listener_fast_write_handler(MfUltralightListener* instance, BitBuffer* buffer) { MfUltralightCommand command = MfUltralightCommandNotProcessedSilent; - FURI_LOG_D(TAG, "CMD_FAST_WRITE"); + FURI_LOG_T(TAG, "CMD_FAST_WRITE"); do { if(!mf_ultralight_support_feature(instance->features, MfUltralightFeatureSupportFastWrite)) @@ -261,7 +261,7 @@ static MfUltralightCommand UNUSED(buffer); MfUltralightCommand command = MfUltralightCommandNotProcessedSilent; - FURI_LOG_D(TAG, "CMD_GET_VERSION"); + FURI_LOG_T(TAG, "CMD_GET_VERSION"); if(mf_ultralight_support_feature(instance->features, MfUltralightFeatureSupportReadVersion)) { bit_buffer_copy_bytes( @@ -280,7 +280,7 @@ static MfUltralightCommand mf_ultralight_listener_read_signature_handler( UNUSED(buffer); MfUltralightCommand command = MfUltralightCommandNotProcessedSilent; - FURI_LOG_D(TAG, "CMD_READ_SIG"); + FURI_LOG_T(TAG, "CMD_READ_SIG"); if(mf_ultralight_support_feature(instance->features, MfUltralightFeatureSupportReadSignature)) { bit_buffer_copy_bytes( @@ -297,7 +297,7 @@ static MfUltralightCommand mf_ultralight_listener_read_counter_handler(MfUltralightListener* instance, BitBuffer* buffer) { MfUltralightCommand command = MfUltralightCommandNotProcessedNAK; - FURI_LOG_D(TAG, "CMD_READ_CNT"); + FURI_LOG_T(TAG, "CMD_READ_CNT"); do { uint8_t counter_num = bit_buffer_get_byte(buffer, 1); @@ -338,7 +338,7 @@ static MfUltralightCommand mf_ultralight_listener_increase_counter_handler( BitBuffer* buffer) { MfUltralightCommand command = MfUltralightCommandNotProcessedNAK; - FURI_LOG_D(TAG, "CMD_INCR_CNT"); + FURI_LOG_T(TAG, "CMD_INCR_CNT"); do { if(!mf_ultralight_support_feature( @@ -374,7 +374,7 @@ static MfUltralightCommand mf_ultralight_listener_check_tearing_handler( BitBuffer* buffer) { MfUltralightCommand command = MfUltralightCommandNotProcessedNAK; - FURI_LOG_D(TAG, "CMD_CHECK_TEARING"); + FURI_LOG_T(TAG, "CMD_CHECK_TEARING"); do { uint8_t tearing_flag_num = bit_buffer_get_byte(buffer, 1); @@ -410,7 +410,7 @@ static MfUltralightCommand MfUltralightCommand command = MfUltralightCommandNotProcessedSilent; UNUSED(instance); UNUSED(buffer); - FURI_LOG_D(TAG, "CMD_VCSL"); + FURI_LOG_T(TAG, "CMD_VCSL"); do { if(!mf_ultralight_support_feature(instance->features, MfUltralightFeatureSupportVcsl)) break; @@ -432,7 +432,7 @@ static MfUltralightCommand mf_ultralight_listener_auth_handler(MfUltralightListener* instance, BitBuffer* buffer) { MfUltralightCommand command = MfUltralightCommandNotProcessedNAK; - FURI_LOG_D(TAG, "CMD_AUTH"); + FURI_LOG_T(TAG, "CMD_AUTH"); do { if(!mf_ultralight_support_feature( @@ -474,7 +474,7 @@ static MfUltralightCommand static MfUltralightCommand mf_ultralight_comp_write_handler_p2(MfUltralightListener* instance, BitBuffer* buffer) { MfUltralightCommand command = MfUltralightCommandNotProcessedNAK; - FURI_LOG_D(TAG, "CMD_CM_WR_2"); + FURI_LOG_T(TAG, "CMD_CM_WR_2"); do { if(bit_buffer_get_size_bytes(buffer) != 16) break; @@ -492,7 +492,7 @@ static MfUltralightCommand mf_ultralight_comp_write_handler_p1(MfUltralightListener* instance, BitBuffer* buffer) { MfUltralightCommand command = MfUltralightCommandNotProcessedSilent; - FURI_LOG_D(TAG, "CMD_CM_WR_1"); + FURI_LOG_T(TAG, "CMD_CM_WR_1"); do { if(!mf_ultralight_support_feature( @@ -532,7 +532,7 @@ static MfUltralightCommand MfUltralightCommand command = MfUltralightCommandNotProcessedNAK; UNUSED(instance); UNUSED(buffer); - FURI_LOG_D(TAG, "CMD_SEC_SEL_2"); + FURI_LOG_T(TAG, "CMD_SEC_SEL_2"); do { if(bit_buffer_get_size_bytes(buffer) != 4) break; @@ -550,7 +550,7 @@ static MfUltralightCommand mf_ultralight_sector_select_handler_p1(MfUltralightListener* instance, BitBuffer* buffer) { MfUltralightCommand command = MfUltralightCommandNotProcessedNAK; UNUSED(buffer); - FURI_LOG_D(TAG, "CMD_SEC_SEL_1"); + FURI_LOG_T(TAG, "CMD_SEC_SEL_1"); do { if(!mf_ultralight_support_feature( diff --git a/lib/nfc/protocols/mf_ultralight/mf_ultralight_poller.c b/lib/nfc/protocols/mf_ultralight/mf_ultralight_poller.c index bf0ced38d0b..4ad7bc147d0 100644 --- a/lib/nfc/protocols/mf_ultralight/mf_ultralight_poller.c +++ b/lib/nfc/protocols/mf_ultralight/mf_ultralight_poller.c @@ -487,6 +487,7 @@ static NfcCommand mf_ultralight_poller_handler_try_default_pass(MfUltralightPoll sizeof(MfUltralightAuthPassword), config->password.data); config->pack = instance->auth_context.pack; + instance->auth_context.auth_success = true; } } @@ -496,6 +497,9 @@ static NfcCommand mf_ultralight_poller_handler_try_default_pass(MfUltralightPoll // original card config->auth0 = instance->pages_read; config->access.prot = true; + } else if(!instance->auth_context.auth_success) { + instance->pages_read -= 2; + instance->data->pages_read -= 2; } } while(false);