You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: private/functions/Get-DecryptedObject.ps1
+66-45Lines changed: 66 additions & 45 deletions
Original file line number
Diff line number
Diff line change
@@ -1,32 +1,60 @@
1
1
functionGet-DecryptedObject {
2
2
<#
3
-
.SYNOPSIS
4
-
Internal function.
3
+
.SYNOPSIS
4
+
Internal function.
5
+
6
+
.DESCRIPTION
7
+
Decrypts credentials or linked server passwords from a SQL Server instance using the service master key.
8
+
This is necessary because SQL Server does not allow retrieval of plaintext passwords for security reasons.
9
+
By leveraging the service master key and the encryption mechanism used by SQL Server, this function can extract the actual passwords for credentials and linked servers.
10
+
11
+
This function is used by the following public functions:
12
+
- Copy-DbaCredential
13
+
- Copy-DbaDbMail
14
+
- Copy-DbaLinkedServer
15
+
- Export-DbaCredential
16
+
- Export-DbaLinkedServer
17
+
18
+
This function is heavily based on Antti Rantasaari's script at http://goo.gl/wpqSib
@@ -71,34 +99,38 @@ function Get-DecryptedObject {
71
99
$ivlen=16
72
100
}
73
101
74
-
<# NOTE: This query is accessing syslnklgns table. Can only be done via the DAC connection #>
75
-
76
102
$sql=switch ($Type) {
77
103
"LinkedServer" {
78
-
"SELECT sysservers.srvname,
79
-
syslnklgns.name,
80
-
SUBSTRING(syslnklgns.pwdhash,5,$ivlen) iv,
81
-
SUBSTRING(syslnklgns.pwdhash,$($ivlen+5),
82
-
LEN(syslnklgns.pwdhash)-$($ivlen+4)) pass
104
+
"SELECT sysservers.srvname AS Name,
105
+
NULL AS Quotename,
106
+
syslnklgns.name AS [Identity],
107
+
SUBSTRING(syslnklgns.pwdhash, 5, $ivlen) AS iv,
108
+
SUBSTRING(syslnklgns.pwdhash, $($ivlen+5), LEN(syslnklgns.pwdhash) - $($ivlen+4)) AS pass,
109
+
NULL AS MappedClassType,
110
+
NULL AS ProviderName
83
111
FROM master.sys.syslnklgns
84
112
INNER JOIN master.sys.sysservers
85
-
ON syslnklgns.srvid=sysservers.srvid
86
-
WHERE LEN(pwdhash) > 0"
113
+
ON syslnklgns.srvid = sysservers.srvid
114
+
WHERE LEN(syslnklgns.pwdhash) > 0"
87
115
}
88
116
"Credential" {
89
-
#"SELECT name,QUOTENAME(name) quotename,credential_identity,SUBSTRING(imageval,5,$ivlen) iv, SUBSTRING(imageval,$($ivlen + 5),LEN(imageval)-$($ivlen + 4)) pass FROM sys.credentials cred INNER JOIN sys.sysobjvalues obj ON cred.credential_id = obj.objid WHERE valclass=28 AND valnum=2"
90
-
"SELECT cred.name,QUOTENAME(cred.name) quotename,credential_identity,SUBSTRING(imageval,5,$ivlen) iv, SUBSTRING(imageval,$($ivlen+5),LEN(imageval)-$($ivlen+4)) pass,target_type AS 'mappedClassType', cp.name AS 'ProviderName' FROM sys.credentials cred INNER JOIN sys.sysobjvalues obj ON cred.credential_id = obj.objid LEFT OUTER JOIN sys.cryptographic_providers cp ON cred.target_id = cp.provider_id WHERE valclass=28 AND valnum=2"
117
+
"SELECT cred.name AS Name,
118
+
QUOTENAME(cred.name) AS Quotename,
119
+
cred.credential_identity AS [Identity],
120
+
SUBSTRING(obj.imageval, 5, $ivlen) AS iv,
121
+
SUBSTRING(obj.imageval, $($ivlen+5), LEN(obj.imageval) - $($ivlen+4)) AS pass,
122
+
cred.target_type AS MappedClassType,
123
+
cp.name AS ProviderName
124
+
FROM sys.credentials cred
125
+
INNER JOIN sys.sysobjvalues obj
126
+
ON cred.credential_id = obj.objid
127
+
LEFT OUTER JOIN sys.cryptographic_providers cp
128
+
ON cred.target_id = cp.provider_id
129
+
WHERE valclass = 28
130
+
AND valnum = 2"
91
131
}
92
132
}
93
133
94
-
Write-Message-Level Debug -Message $sql
95
-
96
-
<#
97
-
Query link server password information from the Db.
98
-
Remove header from pwdhash, extract IV (as iv) and ciphertext (as pass)
99
-
Ignore links with blank credentials (integrated auth ?)
100
-
#>
101
-
102
134
Write-Message-Level Verbose -Message "Query password information from the Db."
103
135
104
136
if ($server.Name-like'ADMIN:*') {
@@ -188,24 +220,13 @@ function Get-DecryptedObject {
0 commit comments