# NTFS Stream Manipulation

## NTFS (New Technology File System)

![](https://2575763014-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MNC3KGY9k0nvrU_5PW6%2F-MO1V-EXiH6cJ946sbyz%2F-MO1We-009HtLeL01BSM%2Fimage.png?alt=media\&token=90a6b922-8d22-41cb-bbcd-906ee8253853)

**NTFS** is a proprietary Microsoft file system and was introduced as a replacement for earlier file systems such as **FAT (File Allocation Table)** and **HPFS (High Performance File System)** and incorporates technical improvements over these. Among some of the advantages **NTFS** it includes improved metadata support, improvements in disk space management, performance enhancements, an improved security system and a file encryption system called **EFS (Encrypting File System)**.&#x20;

Since **Windows NT 3.1** it has been the default file system in the **Windows NT** family such as, for example, **Windows Server 2008**, **Windows 7** and **Windows 10** to name a few. It is supported on desktop and server operating systems. Support on **Linux** and **BSD** is possible through the **NTFS Driver (NTFS-3G)** which offers read and write support. On **macOS** read support is offered.

## NTFS Alternate Data Streams

![](https://2575763014-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MNC3KGY9k0nvrU_5PW6%2F-MO-cs-UdVucE-p4sexu%2F-MO-nbhjaVcf5bEcx2cM%2Fimage.png?alt=media\&token=eba9ac05-d159-4e66-b393-f65b67442cd4)

To be able to understand how the attack of `NTFS Stream Manipulation`, first we must know the basics about **`ADS`**` ``(alternate data streams)`. ADS is a feature of the **`NTFS`**` ``(NT File System)` file system that allows more than one `stream` of data to be associated with the same file. Each file has its main content known as **`default stream`** and can have one or more **`ADS`**.

These **`streams`** of data use a particular format: **`fileName:streamName:streamType`**. For example a  **`ADS`** whose **`stream`** is called **`payload`**  and is hosted inside a file called **`malicious.txt`** would look like this: **`malicious.txt:payload:$DATA`**. It is worth noting that ADS can exist on any type of file, including executables. The content of ADS can be of any type and therefore does not need to be the same type as the file that contains it. For example an image file **`JPG`** can contain **`streams`** data of type video, audio, etc.

Another characteristic of **`ADS`** is that their size is not reported as part of the total size of the file that contains them and they also do not appear listed in widely used Windows applications such as **`Windows Explorer`**. A file containing one or more ADS also does not alter the original functioning of the file and it will continue to work as always. For these reasons, files with malicious ADS are quite common.

{% hint style="danger" %}
When **`copying or moving`** files that contain **ADS** to file systems that do not support **`Alternate Data Streams`**, the user receives a warning that the **`streams`** will be lost. However, this warning **`is not usually issued`** when files are attached by **`email`** or are uploaded to a **`web`**. In those cases all information of **ADS** that was contained in the file will **be lost.**
{% endhint %}

## NTFS Stream Manipulation

The main idea behind the manipulation of `NTFS Data Streams` is to hide information inside another file, normally for malicious purposes. For example it allows an attacker to hide sensitive information collected from a system inside the user's own files without them noticing any change in their files. The attacker can then simply extract those apparently normal files from the target system taking with them all the relevant information stored in the **ADS**.

### Let's see the basic use of ADS using the Windows command console

In this part of the lab we will see how we can create a text file and then add **Alternate Data Streams** with information only visible to **NTFS**.

To begin we open the **Windows (cmd)** console and create our first file, in this case a text file. For this we use the command **`echo`** , the content of our file and finally redirect everything to our file using the character **`>`** followed by the **`file name and extension`** we want to create. Let's see this in the console:<br>

![](https://2575763014-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MNC3KGY9k0nvrU_5PW6%2F-MO1YOiFi3YD4s-E_Z5D%2F-MO1fH1Y-AhXWKP0_Od9%2Fimage.png?alt=media\&token=36df1843-2e08-4f44-acd4-0b1e61f7711a)

{% hint style="success" %}
**`echo`** *{content}* **`>`** *{file name}***.***{extension}*
{% endhint %}

As we can see in the image, the file is created correctly and has a size of **`36 bytes`**. We can use the command **`type`** to see its content:

<div align="center"><img src="https://2575763014-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MNC3KGY9k0nvrU_5PW6%2F-MO1YOiFi3YD4s-E_Z5D%2F-MO1fssHHR26DJSmR24v%2Fimage.png?alt=media&#x26;token=33eb95df-00d2-44ca-b331-aabfb7ba6e95" alt=""></div>

Now, using again the command **`echo`** as we did in step 1, we will add the first **`Alternate Data Stream`** to our file. The only difference is that in this case we must indicate the name of the **`stream`**. Let's see this in the console:

![](https://2575763014-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MNC3KGY9k0nvrU_5PW6%2F-MO1g338oGexZ4YmT3FK%2F-MO1h4aNfnRYmCZ7sFqq%2Fimage.png?alt=media\&token=bfc34556-efd7-4e40-a7b7-cb9fd9bed8a3)

As we can see the **`stream`** seems to have been added without error and our file reports the same size of **`36 bytes`**. We also see that nowhere is it reported that there is a **`ADS`** in the file. This is precisely what makes ADS a good option to hide information without the user noticing.

Let's add a second ADS and see how we can obtain information from them in the console:

![](https://2575763014-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MNC3KGY9k0nvrU_5PW6%2F-MO1g338oGexZ4YmT3FK%2F-MO1i8zxXuoJKfXxD0yQ%2Fimage.png?alt=media\&token=49ddd87e-0f9f-45d3-bbcc-fb14762ccd9e)

To see the content of **`additional data streams that a file may contain, we use the command`** dir /r **`which lists files including their`** . As we can see in the previous image the reported size of the file remains the same **`ADS`**&#x65;ven though it contains two **`36 bytes`** 23 bytes **`ADS`** version of **`each. If we pay attention to the available disk space after creating the file and after each creation of the`** , we can observe that **`ADS`**&#x64;oes correctly keep track of how much space is actually being used. **NTFS** If we open the file with Notepad we see that only the data of the

are presented **`default stream`**:

![](https://2575763014-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MNC3KGY9k0nvrU_5PW6%2F-MO1jFkVBpk2PiZxfOPe%2F-MO1l2Mx5u2_mkTRxf38%2Fimage.png?alt=media\&token=de120931-38e9-46da-accb-1aa338641266)

If we want to edit or view the content of each ADS, we must invoke it directly as follows:

![](https://2575763014-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MNC3KGY9k0nvrU_5PW6%2F-MO1l5a8wMhlaTqClTaE%2F-MO1l_gtOUsTqixw6juD%2Fimage.png?alt=media\&token=3bcd98eb-975f-4f8a-aea1-bb9bbed272c3)

As we said before the content of an ADS can be of any format just like the file that contains them. It is worth noting that **Alternate Data Streams** can be added to **`directories`** in the same way as wit&#x68;**`files`**. Let's see this quickly in the console:

![](https://2575763014-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MNC3KGY9k0nvrU_5PW6%2F-MO1l5a8wMhlaTqClTaE%2F-MO1n9dNByccSrT10Crj%2Fimage.png?alt=media\&token=b0084f32-a1b6-41d2-bc04-9af008e80446)

As we can observe in the image the folder **`DIRECTORY`** contains a **`ADS`** To Domains \[Sharing this NS] **`ADS1`**.

### ADS with PowerShell

PowerShell includes certain commands (**cmdlets**) that make it easier to work with ADS. Let's see how we can list the ADS present in the file we have created in this lab:

&#x20;

![](https://2575763014-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MNC3KGY9k0nvrU_5PW6%2F-MO1o9JaxEprfi96ZSWq%2F-MO1q217J5GMlKmcqR-e%2Fimage.png?alt=media\&token=f3991cc9-3def-49c4-8bd4-4cb39d42a7ab)

In the case of **PowerShell**, the **`default stream`** it is known as **`unnamed stream`**, or **`unnamed stream`** since it appears listed simply as **`:$DATA`**.&#x20;

To view the content for example of the **`ADS2`**, we can use the command **`Get-Content`** as follows:

![](https://2575763014-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MNC3KGY9k0nvrU_5PW6%2F-MO1o9JaxEprfi96ZSWq%2F-MO1qznmuOkYqe8CzOZ6%2Fimage.png?alt=media\&token=95e79157-98f1-412c-8059-b8d66114ecff)

If we want to add content to the ADS we can do it with the command **`Set-Content`**. Let's see how we can add inside a new text file a **ADS** To Domains \[Sharing this NS] **`payload`** that contains a **`executable file`** such as for example the classic **`Microsoft Paint`**. For that we use the following command:

{% hint style="info" %}
We can also make use of the **`cmdlet`** **`Add-Content`**.
{% endhint %}

```
Set-Content -path .\NTFS_ADS_DEMO.txt -value $(Get-Content $(Get-Command mspaint.exe).Path -readcount 0 -encoding byte) -encoding byte -stream payload.exe
```

In this command we explicitly indicate the **`encoding for the bytes`**, the **`readcount to 0`** (to read the file in a single operation and the command **`Get-Command`** to quickly obtain the path of **`mspaint.exe`** without having to specify it manually. Then we simply indicate the name of the **`stream`** that we want to add using the switch **`-stream {streamName}`**.

![](https://2575763014-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MNC3KGY9k0nvrU_5PW6%2F-MO1o9JaxEprfi96ZSWq%2F-MO1t7M6qqrJ6HvgV98K%2Fimage.png?alt=media\&token=04b37ed4-a05f-4001-b916-3f11d9e92573)

As we can see in the image, our text file contains a new **ADS**. Let's see how we can **`execute`** that **ADS** which we know contains an executable file. To achieve this we can use **`Windows Management Instrumentation`** to create a process that executes our **ADS**.&#x20;

{% hint style="danger" %}
This method of executing files using **`wmic`** seems to have been patched in Windows in the version I have in the VMs therefore when trying to execute it both in **`CMD`** and in **`Powershell`** I get an error and the program does not run. In vulnerable versions of Windows, the program would be executed directly and in our case we would see **`mspaint.exe`** running. I will investigate a bit more about other ways to execute ADS in updated versions of Windows, if I find any I will update this article.&#x20;
{% endhint %}

**Update:** After several attempts I managed to add the content of the Windows calculator **`calc.exe`** to our practice file and execute it correctly using **`wmic`**:

![](https://2575763014-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MNC3KGY9k0nvrU_5PW6%2F-MO1o9JaxEprfi96ZSWq%2F-MO2DJTzaJfzBvFf2imk%2Fimage.png?alt=media\&token=47c21349-cd93-445c-b21c-ef48691520e9)

As we see when the command runs correctly it generates a new process which returns a **`ProcessId = 4344`** and a **`ReturnValue = 0`** indicating that the process completed successfully. I do not understand why the initial attempt did not work, perhaps it is the executable of **`mspaint.exe`** or some error that I am making and cannot realize. If you notice the problem, let me know with a tweet to my account [@tzero86](https://twitter.com/Tzero86).

If we want to **delete** a **ADS** in particular we can use the **cmdlet** **`Remove-Item`** as follows. For example to remove our **ADS** To Domains \[Sharing this NS] **`payload`**:

```
Remove-Item -Path .\NTFS_ADS_DEMO.txt -stream payload
```

![](https://2575763014-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MNC3KGY9k0nvrU_5PW6%2F-MO1o9JaxEprfi96ZSWq%2F-MO1wvZ44H9ru9eIKOHQ%2Fimage.png?alt=media\&token=ff9e60b2-ce54-4b08-b12a-fa88af5c1b15)

As we can see our file now contains only two **ADS** additional. Up to here we saw and practiced a bit **NTFS Stream Manipulation**.

{% hint style="info" %}
These labs are subject to modifications and corrections; the most up-to-date version is available online at [the following link](https://tzero86.gitbook.io/tzero86/).
{% endhint %}
