RunAsAccount.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<#
        All the hard decryption magic has been done by http://www.nccgroup.com/
 
        Released as open source by NCC Group Plc - http://www.nccgroup.com/
 
        Developed by Richard Warren, richard dot warren at nccgroup dot trust
 
        https://www.github.com/nccgroup/SCOMDecrypt
 
        Released under AGPL see LICENSE for more information
 
        .Synopsis
        Retrieves RunAs account password from a specified user. It can be used on SCOM 2016 or SCSM 2016
 
        .Description
        RunAs account passwords are stored encrypted in the SCOM or SCSM database. This module decrypts the corresponding password for a specified user. Works with SCOM 2016 and SCSM 2016
 
        .Parameter UserName
        Specify the user of a RunAs account
 
        .Example
        Get-RunAsCredential -Name 'SCOM Connector RunAs Account' -System 'SCOM'
#>

function Get-RunAsCredential
{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true,HelpMessage = 'Please specify a RunAs account display name.')]
        [ValidateNotNullOrEmpty()]
        [String]$Name
    )
            
    Try
    {
        $InstallDir = Get-ItemProperty -Path 'HKLM:SOFTWARE\Microsoft\System Center\2010\Common\Setup' -ErrorAction stop
        $null = [System.Reflection.Assembly]::LoadFile($(Join-Path -Path $InstallDir.InstallDirectory -ChildPath 'Microsoft.Mom.Sdk.SecureStorageManager.dll'))
        $null = [System.Reflection.Assembly]::LoadFile($(Join-Path -Path $InstallDir.InstallDirectory -ChildPath 'Microsoft.EnterpriseManagement.DataAccessLayer.dll'))
    }
    Catch [System.Management.Automation.ItemNotFoundException]
    {
        Write-Output -InputObject '[!] Unable to detect install directory server'
        return
    }     

    $SecStoreManager = New-Object -TypeName Microsoft.EnterpriseManagement.Security.SecureStorageManager

    Try
    {
        $DatabaseInfo = Get-ItemProperty -Path 'HKLM:SOFTWARE\Microsoft\System Center\2010\Common\Database' -ErrorAction stop
    } 
    Catch [System.Management.Automation.ItemNotFoundException]
    {
        Write-Output -InputObject '[!] Unable to detect SQL server'
        return
    }

    Try
    {
        $Reg = Get-ItemProperty -Path 'hklm:SOFTWARE\Microsoft\System Center\2010\Common\MOMBins' -ErrorAction stop
        $Key = $Reg.Value1
    }
    Catch [System.Management.Automation.ItemNotFoundException]
    {
        Write-Output -InputObject '[!] Unable to find key'
        return
    }

    Try
    {
        $SqlCommand = 'SELECT Name, UserName, Data FROM dbo.CredentialManagerSecureStorage;'
        $ConnectionString = "Server=$($DatabaseInfo.DatabaseServerName);Database=$($DatabaseInfo.DatabaseName);Trusted_Connection=True;"
        $Connection = New-Object -TypeName System.Data.SqlClient.SQLConnection -ArgumentList ($ConnectionString)
        $Command = New-Object -TypeName System.Data.Sqlclient.sqlcommand -ArgumentList ($SqlCommand, $Connection)
        $Connection.Open()
        $Adapter = New-Object -TypeName System.Data.Sqlclient.SqlDataAdapter -ArgumentList $Command
        $Dataset = New-Object -TypeName System.Data.DataSet
        $null = $Adapter.Fill($Dataset)
        $Connection.Close()
    }
    Catch
    {
        $Error[0].Exception.Message
    }
    Try 
    {
        $DataRow = $Dataset.Tables[0].Rows | Where-Object -FilterScript {
            $_.Name -ieq $Name
        }
    }
    Catch 
    {
        $Error[0].Exception.Message
    }
    If ($DataRow)
    {
        $Credential = @{}
        $Credential.Add('User',$DataRow.UserName)
        $Credential.Add('Password',[System.Text.Encoding]::UTF8.GetString(($SecStoreManager.Decrypt($DataRow.Data) | Where-Object -FilterScript {
                        ( $_ -ne 0)
        })))
        return $Credential
    }
    Else
    {
        Throw '[!] No RunAs account found'
    }
}