【问题标题】:Using Chrome Cookies for Invoke-WebRequest in Powershell在 Powershell 中使用 Chrome Cookie 进行 Invoke-WebRequest
【发布时间】:2018-12-23 18:36:31
【问题描述】:

有没有办法将已经从 Powershell 中现有 Chrome 网络会话中获取的 cookie 用于 SessionVariable?

我知道 SessionVariable 的字段是:

Headers               : {}
Cookies               : System.Net.CookieContainer
UseDefaultCredentials : False
Credentials           : 
Certificates          : 
UserAgent             : Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) 
                        WindowsPowerShell/5.1.17134.407
Proxy                 : 
MaximumRedirection    : -1

我是否可以做一些事情来为网络请求设置这些值,例如:?当会话可能是多个/不同类型的请求时,我是否需要特定的标头?

$ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
    $Params = @{
        Headers = $headers 
        Cookies = $cookies 
        UseDefaultCredentials = "False" 
        Credentials = $creds
        UserAgent = $ua
        Proxy = ""
        MaximumRedirection = "-1"
    }

如果可以做到这一点,我也有点困惑如何输入 cookie。我是从Getting Cookies using PowerShell 找到的:

$webrequest = Invoke-WebRequest -Uri $url -SessionVariable websession 
$cookies = $websession.Cookies.GetCookies($url) 
 
# Here, you can output all of $cookies, or you can go through them one by one. 
 
foreach ($cookie in $cookies) { 
     # You can get cookie specifics, or just use $cookie 
     # This gets each cookie's name and value 
     Write-Host "$($cookie.name) = $($cookie.value)" 
}

我可以从任何给定站点以这种格式导出 cookie 的 JSON 格式,但如果需要的话,我也可以将其缩减为 "name" = "value"。

[
  {
    "domain": "",
    "expirationDate": "",
    "hostOnly": "",
    "httpOnly": "",
    "name": "",
    "path": "",
    "sameSite": "",
    "secure": "",
    "session": "",
    "storeId": "",
    "value": "",
    "id": ""
  },
  {
    "domain": "",
    etc...
  }
]

我不知道如何在 $cookies 中将每个部分指定为 $cookie,也不知道如何以不同的方式添加它们,因为它不是来自 URL/WebRequest 而是来自 JSON。

感谢您的帮助!

【问题讨论】:

  • 每个浏览器都有自己的 cookie 存储方式。我刚刚了解到,Chrome 将 cookie 存储在您配置文件 (stackoverflow.com/questions/31021764/…) 中的 SQLite db 文件中,因此无论如何您都必须从那里提取 cookie 信息以在脚本中使用。如果可能的话……

标签: powershell web cookies session-cookies scrape


【解决方案1】:

这是使用 PSSQLite 的 PowerShell 脚本解决方案:

# One time setup
    # Download the repository 
        $RepositoryZipUrl = "https://api.github.com/repos/RamblingCookieMonster/PSSQLite/zipball/master"
        Invoke-RestMethod -Uri $RepositoryZipUrl -OutFile "PSSQLite.zip"
    # Unblock the zip
        Unblock-File "PSSQLite.zip"
    # Extract the PSSQLite folder to a module path (e.g. $env:USERPROFILE\Documents\WindowsPowerShell\Modules\)
        Expand-Archive -Path "PSSQLite.zip" -DestinationPath $(Resolve-Path "$env:USERPROFILE\Documents\*PowerShell\Modules" | Select-First 1) -Force -Confirm 
    #Simple alternative, if you have PowerShell 5, or the PowerShellGet module:
        Install-Module PSSQLite

# Import the PSSQlite module
    Import-Module PSSQLite    #Alternatively, Import-Module \\Path\To\PSSQLite
# Specify for which domain you want to retrieve cookies
    $domain = "mavaddat.ca"

# Create a SQLite connection to Cookies
    $cookiesSQL = New-SQLiteConnection -DataSource "$env:APPDATA\Opera Software\Opera Stable\Cookies"

# Investigate the db structure
    $cookiesSQL.GetSchema()

# Investigate tables' structures to formulate a SQL query
    $cookiesSQL.GetSchema("Tables")

# Based on the schema of table `cookies`, form the query
    $query = "SELECT `"_rowid_`",* FROM `"main`".`"cookies`" WHERE `"host_key`" LIKE '%$domain%' ESCAPE '\' LIMIT 0, 49999;"

# Read the cookies from the SQLite
    $cookies = Invoke-SqliteQuery -Query $query -DataSource $cookiesSQL.FileName

# Get Chromium cookie master key
    $cookiesKeyEncBaseSixtyFour = (Get-Content -Path "$env:APPDATA\Opera Software\Opera Stable\Local State" | ConvertFrom-Json).'os_crypt'.'encrypted_key'
    $cookiesKeyEnc = [System.Convert]::FromBase64String($cookiesKeyEncBaseSixtyFour) | Select-Object -Skip 5  # Magic number 5
    $cookiesKey = [System.Security.Cryptography.ProtectedData]::Unprotect($cookiesKeyEnc,$null,[System.Security.Cryptography.DataProtectionScope]::LocalMachine)

# Create a web session object for the IWR work
    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession

# Stuff the cookies into the session
    foreach($cookie in $cookies){
        $names = $cookie | Get-Member | Where-Object -FilterScript {($_.MemberType) -eq 'NoteProperty'} | Select-Object -Property Name 
        foreach($name in $names)
        {
            $path = if ([string]::IsNullOrEmpty($cookie.'path'))
            {
                '/'
            } 
            else 
            { 
                $cookie.'path'
            }
$cipherStream = [System.IO.MemoryStream]::new($cookie.encrypted_value)
$cipherReader = [System.IO.BinaryReader]::new($cipherStream)
$nonSecretPayload = $cipherReader.ReadBytes(3) # Magic number 3
$nonce = $cipherReader.ReadBytes([System.Security.Cryptography.AesGcm]::NonceByteSizes.MinSize)

            $session.Cookies.Add([System.Net.Cookie]::new(#)
        }
    }

# Now use the session to IWR
    $downloadToPath = "c:\somewhere\on\disk\file.zip"
    $remoteFileLocation = "http://somewhere/on/the/internet"
    Invoke-WebRequest $remoteFileLocation -WebSession $session -TimeoutSec 900 -OutFile $downloadToPath

我是如何实现上述的

Chromium-based browser 使用存储在二进制 SQLite 数据库文件中的 cookie。请参阅超级用户问题,了解如何为 Chrome 查找此数据库,"Is there a way to watch cookies and their values live?"

对我来说(在 Windows 10 上使用 Opera 74.0.3911.107),此文件名为 Cookies,它位于 %appdata%\Opera Software\Opera Stable\Cookies。使用PSSQLite,我可以看到数据库有两个具有以下模式的表:

Cookies

Name Type Schema
creation_utc INTEGER "creation_utc" INTEGER NOT NULL
host_key TEXT "host_key" TEXT NOT NULL
name TEXT "name" TEXT NOT NULL
value TEXT "value" TEXT NOT NULL
path TEXT "path" TEXT NOT NULL
expires_utc INTEGER "expires_utc" INTEGER NOT NULL
is_secure INTEGER "is_secure" INTEGER NOT NULL
is_httponly INTEGER "is_httponly" INTEGER NOT NULL
last_access_utc INTEGER "last_access_utc" INTEGER NOT NULL
has_expires INTEGER "has_expires" INTEGER NOT NULL DEFAULT 1
is_persistent INTEGER "is_persistent" INTEGER NOT NULL DEFAULT 1
priority INTEGER "priority" INTEGER NOT NULL DEFAULT 1
encrypted_value BLOB "encrypted_value" BLOB DEFAULT ''
samesite INTEGER "samesite" INTEGER NOT NULL DEFAULT -1
source_scheme INTEGER "source_scheme" INTEGER NOT NULL DEFAULT 0
source_port INTEGER "source_port" INTEGER NOT NULL DEFAULT -1
is_same_party INTEGER "is_same_party" INTEGER NOT NULL DEFAULT 0

Meta

Name Type Schema
key LONGVARCHAR "key" LONGVARCHAR NOT NULL UNIQUE
value LONGVARCHAR "value" LONGVARCHAR

查看stackoverflow问题"How to read Brave Browser cookie database encrypted values in C# (.NET Core)?"我也以@lawrencegripperthis gist的PowerShell cookie方法为模板。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    相关资源
    最近更新 更多