# NFC

<details>

<summary><mark style="color:purple;"><strong><code>Main Fields</code></strong></mark></summary>

{% code title="File Example" overflow="wrap" %}
```
Filetype: Flipper NFC device
Version: 2
Device type: NTAG216
UID: 04 85 92 8A A0 61 81
ATQA: 44 00
SAK: 00
Signature: 1B 84 EB 70 BD 4C BD 1B 1D E4 98 0B 18 58 BD 7C 72 85 B4 E4 7B 38 8E 96 CF 88 6B EE A3 43 AD 90
Mifare version: 00 04 04 02 01 00 13 03
```
{% endcode %}

<mark style="color:purple;">These fields are part of the</mark> <mark style="color:orange;">**`NFC`**</mark> <mark style="color:purple;">protocol and are used to identify and configure the</mark> <mark style="color:orange;">**`NFC`**</mark> <mark style="color:purple;">tag:</mark>

{% hint style="info" %}
<mark style="color:orange;">**`UID (Unique Identifier)`**</mark>

* &#x20;<mark style="color:purple;">A unique identifier for the</mark> <mark style="color:orange;">**`NFC`**</mark> <mark style="color:purple;">tag. It’s like a serial number.</mark>
{% endhint %}

{% hint style="info" %}
<mark style="color:orange;">**`ATQA (Answer to Request)`**</mark>&#x20;

* &#x20;<mark style="color:purple;">Indicates the tag’s type and capabilities.</mark>
{% endhint %}

{% hint style="info" %}
<mark style="color:orange;">**`SAK (Select Acknowledge)`**</mark>

* &#x20;<mark style="color:purple;">Confirms the tag’s type and protocol compatibility.</mark>
* <mark style="color:purple;">This byte indicates the tag’s status in response to a request.</mark>
* <mark style="color:purple;">It’s often used in combination with</mark> <mark style="color:orange;">**`ATQA`**</mark> <mark style="color:purple;">to determine access rights and compatibility with certain devices or reader types.</mark>
{% endhint %}

{% hint style="info" %}
<mark style="color:orange;">**`Signature`**</mark>

* <mark style="color:purple;">A cryptographic signature used for authentication (optional).</mark>
{% endhint %}

{% hint style="info" %}
<mark style="color:orange;">**`Mifare version`**</mark>

* <mark style="color:purple;">Indicates the version of the</mark> <mark style="color:orange;">**`Mifare`**</mark> <mark style="color:purple;">protocol used by the tag.</mark>
* <mark style="color:purple;">In</mark> <mark style="color:orange;">**`Mifare`**</mark> <mark style="color:purple;">tags, each memory sector can be individually protected by keys.</mark>
* <mark style="color:purple;">These keys (often referred to as</mark> <mark style="color:orange;">**`Key A`**</mark> <mark style="color:purple;">and</mark> <mark style="color:orange;">**`Key B`**</mark><mark style="color:purple;">) can be used to lock down access to specific sectors, allowing different users or systems to read or write only certain sections based on the permissions associated with the keys.</mark>
{% endhint %}

</details>

<details>

<summary><mark style="color:purple;"><strong><code>Counters and Tearing Flags</code></strong></mark></summary>

{% code title="File Example" overflow="wrap" %}
```
Counter 0: 0
Tearing 0: 00
Counter 1: 0
Tearing 1: 00
Counter 2: 0
Tearing 2: 00
```
{% endcode %}

{% hint style="info" %}
<mark style="color:red;">**`Counters`**</mark>

* <mark style="color:purple;">These are</mark> <mark style="color:orange;">**`32-bit`**</mark>**&#x20;**<mark style="color:purple;">**counters**</mark> <mark style="color:purple;"></mark><mark style="color:purple;">that can be used to track how many times the</mark> <mark style="color:orange;">**`NFC`**</mark> <mark style="color:purple;">tag has been read or written.</mark>
* <mark style="color:purple;">Each counter has a</mark> <mark style="color:orange;">**`tearing flag`**</mark><mark style="color:purple;">.</mark>
{% endhint %}

{% hint style="info" %}
<mark style="color:red;">**`Tearing Flags`**</mark>

* <mark style="color:purple;">These are</mark> <mark style="color:orange;">**`8-bit`**</mark>**&#x20;**<mark style="color:purple;">**flags**</mark> <mark style="color:purple;"></mark><mark style="color:purple;">used to prevent</mark> <mark style="color:purple;"></mark><mark style="color:purple;">**tearing**</mark> <mark style="color:purple;"></mark><mark style="color:purple;">(partial writes) during a write operation.</mark>
* <mark style="color:purple;">Tearing occurs when power is lost during a write, leaving the tag in an inconsistent state.</mark>
* <mark style="color:purple;">The tearing flags help ensure that writes are atomic (either fully completed or not at all).</mark>
{% endhint %}

</details>

<details>

<summary><mark style="color:purple;"><strong><code>Tag Pages</code></strong></mark></summary>

{% code title="File Example" %}
```
Page 0: 04 39 91 24
Page 1: C2 FC 67 80
Page 2: D9 48 00 00
Page 3: E1 10 12 00
Page 4: 01 03 A0 0C
Page 5: 34 03 19 D1
Page 6: 01 15 55 04
Page 7: 79 6F 75 74
Page 8: 75 2E 62 65
Page 9: 2F 64 51 77
Page 10: 34 77 39 57
Page 11: 67 58 63 51
Page 12: FE 00 00 00
```
{% endcode %}

<mark style="color:purple;">The data is stored in hexadecimal format, and each page typically contains</mark> <mark style="color:orange;">**`4`**</mark> <mark style="color:purple;">bytes of data.</mark>

{% hint style="info" %}
<mark style="color:red;">**`Metadata Tags`**</mark>

<mark style="color:purple;">**`Pages 0-6`**</mark> <mark style="color:purple;"></mark><mark style="color:purple;">contain critical metadata and configuration data. Here’s a breakdown of what each page typically stores:</mark>

* <mark style="color:purple;">**`Page 0`**</mark><mark style="color:purple;">: Contains the</mark> <mark style="color:orange;">**`UID (Unique Identifier)`**</mark><mark style="color:purple;">.</mark>
  * <mark style="color:purple;">The</mark> <mark style="color:orange;">**`UID`**</mark> <mark style="color:purple;">is a unique serial number assigned to the tag during manufacturing.</mark>
* <mark style="color:purple;">**`Page 1-2`**</mark><mark style="color:purple;">: Contain the</mark> <mark style="color:orange;">**`Capability Container (CC)`**</mark> <mark style="color:purple;">and other configuration data.</mark>
  * <mark style="color:purple;">The</mark> <mark style="color:orange;">**`CC`**</mark> <mark style="color:purple;">defines the tag’s memory size, supported features, and access permissions.</mark>
* <mark style="color:purple;">**`Page 3`**</mark><mark style="color:purple;">: Often contains</mark> <mark style="color:orange;">**`lock bits`**</mark> <mark style="color:purple;">or additional configuration.</mark>
  * <mark style="color:orange;">**`Lock bits`**</mark> <mark style="color:purple;">determine which parts of the tag’s memory are read-only or writable.</mark>
  * <mark style="color:purple;">For tags that require tamper-proof data, this feature can be crucial. Once locked, a section of memory cannot be altered, which can prevent malicious or accidental modifications.</mark>
* <mark style="color:purple;">**`Page 4-6`**</mark><mark style="color:purple;">: May contain additional metadata, such as</mark> <mark style="color:orange;">**`manufacturer data`**</mark> <mark style="color:purple;">or</mark> <mark style="color:orange;">**`signature`**</mark><mark style="color:purple;">.</mark>
  * <mark style="color:purple;">These pages are often used for tag-specific data or reserved for future use.</mark>
{% endhint %}

{% hint style="info" %}
<mark style="color:red;">**`Data Tags`**</mark>

<mark style="color:purple;">When concatenated, these pages form the</mark> <mark style="color:orange;">**`URL`**</mark><mark style="color:purple;">:</mark> <mark style="color:orange;">**`youtu.be/dQw4w9WgXcQ`**</mark><mark style="color:purple;">, which is the classic</mark> <mark style="color:orange;">**`Rickroll`**</mark><mark style="color:purple;">:</mark>

* <mark style="color:purple;">**`Page 7`**</mark><mark style="color:purple;">:</mark> <mark style="color:orange;">**`79 6F 75 74`**</mark> <mark style="color:purple;">→</mark> <mark style="color:orange;">**`yout`**</mark>
* <mark style="color:purple;">**`Page 8`**</mark><mark style="color:purple;">:</mark> <mark style="color:orange;">**`75 2E 62 65`**</mark> <mark style="color:purple;">→</mark> <mark style="color:orange;">**`u.be`**</mark>
* <mark style="color:purple;">**`Page 9`**</mark><mark style="color:purple;">:</mark> <mark style="color:orange;">**`2F 64 51 77`**</mark> <mark style="color:purple;">→</mark> <mark style="color:orange;">**`/dQw`**</mark>
* <mark style="color:purple;">**`Page 10`**</mark><mark style="color:purple;">:</mark> <mark style="color:orange;">**`34 77 39 57`**</mark> <mark style="color:purple;">→</mark> <mark style="color:orange;">**`4w9W`**</mark>
* <mark style="color:purple;">**`Page 11`**</mark><mark style="color:purple;">:</mark> <mark style="color:orange;">**`67 58 63 51`**</mark> <mark style="color:purple;">→</mark> <mark style="color:orange;">**`gXcQ`**</mark>
{% endhint %}

{% hint style="info" %}
<mark style="color:red;">**`Termination Marker`**</mark>&#x20;

* <mark style="color:purple;">This marker tells the NFC reader where the useful data ends, so it doesn’t try to interpret the empty pages as part of the data message --></mark> <mark style="color:orange;">**`FE 00 00 00`**</mark>
{% endhint %}

</details>

<details>

<summary><mark style="color:purple;"><strong><code>Cryptographic Protection</code></strong></mark></summary>

* <mark style="color:orange;">**`Symmetric Encryption (Shared Secret Key)`**</mark><mark style="color:purple;">: In this system, both the</mark> <mark style="color:orange;">**`NFC`**</mark> <mark style="color:purple;">tag and the reader share the same key. The tag and the reader must first authenticate the key before communication occurs.</mark>

- <mark style="color:orange;">**`Asymmetric Encryption (Public/Private Key)`**</mark><mark style="color:purple;">: In more advanced systems,</mark> <mark style="color:orange;">**`NFC`**</mark> <mark style="color:purple;">tags may use asymmetric encryption, where the reader has a public key and the</mark> <mark style="color:orange;">**`NFC`**</mark> <mark style="color:purple;">tag has a corresponding private key.</mark>&#x20;

* <mark style="color:orange;">**`Hardware security modules (HSMs)`**</mark> <mark style="color:purple;">are sometimes used to store these keys securely on the reader or the tag itself.</mark>

</details>
