flipperdevices / Flipper-Android-App

Android Mobile app to rule all Flipper's family
https://forum.flipperzero.one/c/mobile/14
MIT License
1.39k stars 159 forks source link

mfkeys app incorrectly displays and saves keys that contain leading zeros #532

Closed fersingb closed 1 year ago

fersingb commented 1 year ago

Describe the bug Keys that start with a 0 are not displayed/saved properly. The leading zero is missing, resulting in an invalid key

To Reproduce Steps to reproduce the behavior:

  1. create a file .mfkey32.log with the following content: Sec 0 key A cuid 2f05ec42 nt0 bebf5b50 nr0 d34d3251 ar0 a4463233 nt1 a3cfc299 nr1 507d9fde ar1 dac188a7
  2. Save if in /ext/nfc/
  3. Run the Android mfkey app in NFC Tools
  4. Notice that the leading 0 is missing

Expected behavior Leading 0 is correctly displayed and saved

Recovering the key using the CLI tool works without any issue:

./mfkey32v2 2f05ec42 bebf5b50 d34d3251 a4463233 a3cfc299 507d9fde dac188a7
MIFARE Classic key recovery - based 32 bits of keystream  VERSION2
Recover key from two 32-bit reader authentication answers only
This version implements Moebius two different nonce solution (like the supercard)

Recovering key for:
    uid: 2f05ec42
   nt_0: bebf5b50
 {nr_0}: d34d3251
 {ar_0}: a4463233
   nt_1: a3cfc299
 {nr_1}: 507d9fde
 {ar_1}: dac188a7

LFSR successors of the tag challenge:
  nt': 403f82e6
 nt'': c6b4c9a2

Keystream used to generate {ar} and {at}:
  ks2: e479b0d5

Found Key: [0a1b2c3d4e5f]
LionZXY commented 1 year ago

@fersingb i can't reproduce it :( photo_2023-03-23 17 01 39

fersingb commented 1 year ago

@LionZXY you actually reproduced it. The key is missing the first 0 The key should be 0a1b2c3d4e5f

EDIT: I think I found the issue here https://github.com/flipperdevices/Flipper-Android-App/blob/686127bf93878efc6d01e1f78080e6e75ba18a66/components/nfc/mfkey32/screen/src/main/java/com/flipperdevices/nfc/mfkey32/screen/viewmodel/MfKey32ViewModel.kt#L159

I guess you should use something like "%012X".format(key) instead to preserve the leading zeros.

The following code demonstrates the issue:

/**
 * You can edit, run, and share this code.
 * play.kotlinlang.org
 */
fun main() {
    val test = 0x0a1b2c3d4e5f;
    val teststr1 = test.toString(radix = 16).uppercase()
    val teststr2 = "%012X".format(test)
    println(teststr1)
    println(teststr2)

}

https://pl.kotl.in/kabpNz8qo

LionZXY commented 1 year ago

Oh, thank you, it's my fault