0

I'm lost my password to RDP in Azure. But I have cscfg file and certificate which encrypt password. How I can get passwords from cscfg file?

Alexey Gusarov
  • 337
  • 2
  • 9

2 Answers2

6
function DecodePassword([string] $encodedPassword)
{
    $encodedMessage = [Convert]::FromBase64String($encodedPassword);
    $cms = New-Object System.Security.Cryptography.Pkcs.EnvelopedCms;
    $cms.Decode($encodedMessage);

    $store = $null;
    try
    {
        $store = New-Object System.Security.Cryptography.X509Certificates.X509Store('My', 'CurrentUser');
        $cms.Decrypt($store.Certificates);
    }
    finally
    {
        $store.Close();
    }

    return [Text.Encoding]::UTF8.GetString($cms.ContentInfo.Content);
}
Alexey Gusarov
  • 337
  • 2
  • 9
  • 1
    Use this in powershell. Also change line 4 add "System" to the namespace. $cms = New-Object System.Security.Cryptography.Pkcs.EnvelopedCms; – MichaelLake Feb 15 '14 at 23:50
0

Thank you! This was a serious life saver for me! Here's a version I tweaked for easy command line invocation (and added loading of System.Security):

$error.clear()
function DecodePassword([string] $encodedPassword)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.Security") | out-null;
    $encodedMessage = [Convert]::FromBase64String($encodedPassword);
    $cms = New-Object System.Security.Cryptography.Pkcs.EnvelopedCms;
    $cms.Decode($encodedMessage);

    $store = $null;
    try
    {
        $store = New-Object System.Security.Cryptography.X509Certificates.X509Store('My', 'CurrentUser');
        $cms.Decrypt($store.Certificates);
    }

    finally
    {
        $store.Close();
    }

    return [Text.Encoding]::UTF8.GetString($cms.ContentInfo.Content);
}

$password = DecodePassword($args[0]);
Write-Host Decoded password: """$password""";

Which can be simply invoked as:

>powershell -f DecodeRemotePassword.ps1 "PASTE_ENCODED_PASSWORD_HERE"

(Please DON'T up-vote since this is just a tweak of Alexey's excellent answer)