-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
content/de/posts/2024/2024-08-30-PowerShell-NTFS-Alternate-Data-Streams.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
--- | ||
slug: "powershell-ntfs-alternate-data-streams" | ||
title: "PowerShell NTFS Alternate Data Streams" | ||
date: 2024-08-30 | ||
comments: true | ||
tags: [powershell, ntfs] | ||
draft: true | ||
--- | ||
Das Dateisystem NTFS wird standardmäßig unter Windows verwendet. Und normalerweise hat eine Datei nur einen dazugehörigen normalen Datenstrom (Data Stream) mit dem Namen `:$DATA`. Aber es gibt auch die sogenannten "Alternate Data Streams" (ADS), die zusätzliche Daten enthalten können. | ||
|
||
Diese ADS werden teilweise vom System verwendet, könnten aber auch von Angreifern verwendet werden, um Daten zu verstecken. | ||
|
||
## NTFS Alternate Data Stream auslesen | ||
|
||
Aus dem Internet heruntergeladene Dateien erhalten bei vielen Browsern unter Windows ein "Mark of the web" (MOTW) im `Zone.Identifier` Stream. Dieser Stream enthält Infos zur Quelle der Datei - also z.B. dass sie aus dem Internet heruntergeladen wurde. | ||
Um den Inhalt eines ADS auszulesen, kann einfach `Get-Content` mit dem Parameter `-Stream` und der Angabe des Streamnamens verwendet werden. Alternativ kann an den Dateinamen `:<Name des Streams>` angehängt werden. | ||
|
||
```powershell | ||
# Zwei Varianten um den Zone.Identifier Stream auszulesen | ||
Get-Content .\Beispiel.txt -Stream Zone.Identifier | ||
Get-Content .\Beispiel.txt:Zone.Identifier | ||
# Rückgabe | ||
[ZoneTransfer] | ||
ZoneId=3 | ||
ReferrerUrl=https://diecknet.de | ||
HostUrl=https://github.com | ||
``` | ||
|
||
Der tatsächliche Inhalt von `Zone.Identifier` kann abweichen. Mal sind mehr oder weniger Infos enthalten. Die Info zur `ZoneId` sollte aber immer enthalten sein. Folgende Werte sind für die Zone möglich: | ||
|
||
| Wert | Bedeutung | | ||
|-------|-----------------------| | ||
| 0 | My Computer | | ||
| 1 | Local Intranet Zone | | ||
| 2 | Trusted sites Zone | | ||
| 3 | Internet Zone | | ||
| 4 | Restricted Sites Zone | | ||
|
||
Quelle: <https://learn.microsoft.com/en-us/previous-versions/troubleshoot/browsers/security-privacy/ie-security-zones-registry-entries#zones> | ||
|
||
Der Windows Smartscreen Filter legt teilweise auch noch einen eigenen ADS namens `SmartScreen` an. Wenn dort `Anaheim` als Inhalt steht, wurde die Datei als unsicher eingestuft. | ||
|
||
```powershell | ||
Get-Content .\Beispiel.exe -Stream Smartscreen | ||
# Rückgabe | ||
Anaheim | ||
``` | ||
|
||
Und falls es irgendwelche komplett anderen Streams gibt, könnt ihr sie natürlich mit der gleichen Vorgehensweise auslesen. | ||
|
||
### Mark of the web entfernen | ||
|
||
Der reguläre Weg um das "Mark of the web" zu entfernen ist per `Unblock-File` oder per Eigenschaften Dialog der Datei. | ||
|
||
```powershell | ||
Unblock-File .\MeineDatei.docx | ||
``` | ||
|
||
[![Setzen des Hakens 'Zulassen' in den Dateieigenschaften entfernt das Mark of the web](/images/2024/2024-08-30_NTFS_ADS_EigenschaftenDialog.jpg "Setzen des Hakens 'Zulassen' in den Dateieigenschaften entfernt das Mark of the web")](/images/2024/2024-08-30_NTFS_ADS_EigenschaftenDialog.jpg) | ||
|
||
## NTFS Alternate Data Streams finden | ||
|
||
Um herauszufinden, welche Alternate Data Streams vorhanden sind, kann der Parameter `-Stream` vom Cmdlet `Get-Item` genutzt werden. Mit `*` als Platzhalter können wir alle ADS finden. | ||
|
||
```powershell | ||
# Alle Streams von allen Daten im aktuellen Verzeichnis auflisten | ||
Get-Item * -Stream * | ||
``` | ||
|
||
Die Info, dass der Standard `:$DATA` Stream vorhanden ist, können wir aber auch wegfiltern: | ||
|
||
```powershell | ||
Get-Item * -Stream * | Where-Object {$_.Stream -ne ':$DATA' } | ||
``` | ||
|
||
Noch etwas flexibler geht es mit [dem Skript `Get-NTFSADS.ps1`](https://github.com/diecknet/diecknet-scripts/blob/main/Windows/Get-NTFSADS.ps1) welches ich auf GitHub veröffentlicht habe. | ||
|
||
## NTFS Alternate Data Streams schreiben | ||
|
||
Es ist möglich eigene Daten in ADS reinzuschreiben. Dafür kann z.B. `Set-Content`/`Add-Content` verwendet werden. | ||
|
||
```powershell | ||
# Zwei Varianten um den Beispiel Stream zu setzen, ggf. wird er überschrieben | ||
Set-Content .\MeineDatei.docx -Stream "Beispiel" -Value "Hallo PowerShell!" | ||
Set-Content .\MeineDatei.docx:Beispiel -Value "Hallo PowerShell!" | ||
# Zwei Varianten um den Beispiel Stream zu ergänzen | ||
Add-Content .\MeineDatei.docx -Stream "Beispiel" -Value "Hallo PowerShell!" | ||
Add-Content .\MeineDatei.docx:Beispiel -Value "Hallo PowerShell!" | ||
``` | ||
|
||
## NTFS Alternate Data Streams löschen | ||
|
||
ADS können wie normale Dateien auch per `Remove-Item` gelöscht werden. Die Datei und der `:$DATA` Stream würden im folgenden Beispiel weiter existieren: | ||
|
||
```powershell | ||
Remove-Item .\MeineDatei.docx -Stream "Beispiel" | ||
``` | ||
|
||
## Fazit | ||
|
||
NTFS Alternate Data Streams sind eine interessante Funktion. Als Einfallstor für Angreifer sind sie weniger geeignet, da bei einem normalen Download keine zusätzlichen Data Streams übermittelt werden können. Aber in manchen Containerformaten könnten auch ADS übertragen werden, wie z.B. innerhalb von `.vhdx`-Dateien für virtuelle Festplattenabbilder. Allerdings wäre die Verwendung eines ADS-fähigen Containers auch schon sehr auffällig. | ||
|
||
Ich denke, dass ADS dann eher von Angreifern verwendet werden könnten, wenn sie sich bereits in einem System eingenistet haben. Dann könnten Schadcodes oder andere Daten versteckt werden. Allerdings sollten (meiner Meinung nach) moderne EDR Lösungen anschlagen, falls ADS verwendet werden - technisch möglich wäre es z.B. per Sysmon Logging. |
106 changes: 106 additions & 0 deletions
106
content/en/posts/2024/2024-08-30-PowerShell-NTFS-Alternate-Data-Streams.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
--- | ||
slug: "powershell-ntfs-alternate-data-streams" | ||
title: "PowerShell NTFS Alternate Data Streams" | ||
date: 2024-08-30 | ||
comments: true | ||
tags: [powershell, ntfs] | ||
draft: true | ||
--- | ||
The NTFS file system is used by default in Windows. And normally a file has only one associated normal data stream with the name `:$DATA`. But there are also the so-called "Alternate Data Streams" (ADS), which can contain additional data. These ADS are not visible in the Windows Explorer or most other applications. | ||
|
||
ADS are sometimes used by the system, but could also be used by attackers to hide data. | ||
|
||
## Read NTFS Alternate Data Streams | ||
|
||
Many web browsers on windows stamp downloaded files from the Internet with a "Mark of the web" (MOTW) in the `Zone.Identifier` stream. This stream contains information about the source of the file - e.g. that it was downloaded from the Internet. | ||
To read the content of an ADS, you can simply use `Get-Content` with the `-Stream` parameter and the stream name. Alternatively, `:<name of stream>` can be appended to the file name. | ||
|
||
```powershell | ||
# Two variants to read the Zone.Identifier Stream | ||
Get-Content .\Example.txt -Stream Zone.Identifier | ||
Get-Content .\Example.txt:Zone.Identifier | ||
# Return | ||
[ZoneTransfer] | ||
ZoneId=3 | ||
ReferrerUrl=https://diecknet.de | ||
HostUrl=https://github.com | ||
``` | ||
|
||
The actual content of `Zone.Identifier` can vary. Sometimes it contains more or less information. However, the information on the `ZoneId` should always be included. The following values are possible for the zone: | ||
|
||
| Value | Meaning | | ||
|-------|-----------------------| | ||
| 0 | My Computer | | ||
| 1 | Local Intranet Zone | | ||
| 2 | Trusted sites Zone | | ||
| 3 | Internet Zone | | ||
| 4 | Restricted Sites Zone | | ||
|
||
Source: <https://learn.microsoft.com/en-us/previous-versions/troubleshoot/browsers/security-privacy/ie-security-zones-registry-entries#zones> | ||
|
||
The Windows Smartscreen Filter sometimes also creates its own ADS called `SmartScreen`. If it contains `Anaheim`, the file has been classified as unsafe. | ||
|
||
```powershell | ||
Get-Content .\Example.exe -Stream Smartscreen | ||
# Return | ||
Anaheim | ||
``` | ||
|
||
And if there are any other streams, you can of course read them using the same procedure. | ||
|
||
### Remove the Mark of the web | ||
|
||
The regular way to remove the "Mark of the web" is via `Unblock-File` or via the properties dialog of the file. | ||
|
||
```powershell | ||
Unblock-File .\MyFile.docx | ||
``` | ||
|
||
[![Setting the checkmark 'Unblock' in the file properties, removes the Mark of the web](/images/2024/2024-08-30_NTFS_ADS_Properties.jpg "Setting the checkmark 'Unblock' in the file properties, removes the Mark of the web")](/images/2024/2024-08-30_NTFS_ADS_Properties.jpg) | ||
|
||
## Find NTFS Alternate Data Streams | ||
|
||
To find out which Alternate Data Streams are available, the `-Stream` parameter of the `Get-Item` cmdlet can be used. With `*` as a placeholder, we can find all ADS. | ||
|
||
```powershell | ||
# List all streams of all data in the current directory | ||
Get-Item * -Stream * | ||
``` | ||
|
||
We can also filter out the information that the standard `:$DATA` stream is available: | ||
|
||
```powershell | ||
Get-Item * -Stream * | Where-Object {$_.Stream -ne ':$DATA' } | ||
``` | ||
|
||
With my [`Get-NTFSADS.ps1 script`](https://github.com/diecknet/diecknet-scripts/blob/main/Windows/Get-NTFSADS.ps1) it's even a bit easier and prettier. Check it out on [Github](https://github.com/diecknet/diecknet-scripts/blob/main/Windows/Get-NTFSADS.ps1). | ||
|
||
## Write NTFS Alternate Data Streams | ||
|
||
It is possible to write your own data into ADS. For example, `Set-Content`/`Add-Content` can be used for this. | ||
|
||
```powershell | ||
# Two variants to set the example stream, it may be overwritten | ||
Set-Content .\MyFile.docx -Stream "Example" -Value "Hello PowerShell!" | ||
Set-Content .\MyFile.docx:Example -Value "Hello PowerShell!" | ||
# Two variants to add to the example stream | ||
Add-Content .\MyFile.docx -Stream "Beispiel" -Value "Hello PowerShell!" | ||
Add-Content .\MyFile.docx:Example -Value "Hello PowerShell!" | ||
``` | ||
|
||
## Delete NTFS Alternate Data Streams | ||
|
||
ADS can also be deleted via `Remove-Item` like normal files. The file and the `:$DATA` stream would continue to exist in the following example: | ||
|
||
```powershell | ||
Remove-Item .\MyFile.docx -Stream "Example" | ||
``` | ||
|
||
## Conclusion | ||
|
||
NTFS Alternate Data Streams are an interesting function. They are less suitable as a gateway for attackers, as no additional data streams can be transferred during a normal download. However, ADS could still be transferred in some container formats, e.g. within `.vhdx` files for virtual hard disk images. However, the use of an ADS-capable container would also be very suspicious. | ||
|
||
I think that ADS could rather be used by attackers if they have already infiltrated a system. Malicious code or other data could then be hidden. However, modern EDR solutions should (in my opinion) take action if ADS is used. Technically, this would be possible via Sysmon logging, for example. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.