Skip to content

Commit 2031ea2

Browse files
author
manualbashing
committed
new post about secure string
1 parent d19f396 commit 2031ea2

File tree

1 file changed

+32
-0
lines changed
  • content/en/posts/decode-powershell-securestring-linux

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: Decode any PowerShell Secure String under Linux
3+
date: 2024-04-10T20:48:34
4+
draft: false
5+
tags:
6+
- PowerShell
7+
---
8+
9+
This is not a big deal, as *secure strings* are not encrypted under Unix systems. The password will instead be obfuscated as hexadecimal representation of the string's bytes.
10+
11+
According to [a user on reddit](https://www.reddit.com/r/PowerShell/comments/dtggfn/comment/f6wmpfu/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button), converting a string to a *secure string* it is basically doing this:
12+
13+
```powershell
14+
[BitConverter]::ToString([Text.Encoding]::Unicode.GetBytes('foo')).Replace('-','')
15+
```
16+
17+
This can be demonstrated in the following way:
18+
19+
```powershell
20+
$password = "foo"
21+
$secureString = $password | ConvertTo-SecureString -AsPlainText -Force
22+
$serializedSecureString = $secureString | ConvertFrom-SecureString
23+
$byteArray = [byte[]] -split ($serializedSecureString -replace '..', '0x$& ')
24+
$utf8 = [Text.Encoding]::UTF8
25+
$decodedPassword = $utf8.GetString($byteArray)
26+
27+
Write-Host "Password: $password | Decoded Password: $decodedPassword"
28+
```
29+
30+
Under Windows this will not lead to a useful value for `$decodedPassword`, as Windows systems encrypt the *secure string* based on the profile of the current User and Host. Under Linux the value for `$password` and `$decodedPassword` will be identical.
31+
32+
So better find another way to store your automation secrets under Linux or even better: avoid them alltogether by using certificates or managed identities if possible.

0 commit comments

Comments
 (0)