Skip to content

Instantly share code, notes, and snippets.

@duzvik
Last active March 26, 2021 12:03
Show Gist options
  • Save duzvik/436d828edd94cae214615517a4e64a35 to your computer and use it in GitHub Desktop.
Save duzvik/436d828edd94cae214615517a4e64a35 to your computer and use it in GitHub Desktop.
cookies
$UserData="$Env:LOCALAPPDATA\Google\Chrome\User Data"
#Write-Host $UserData
$DebugPort=9142
$Chrome="C:\Program Files\Google\Chrome\Application\chrome.exe"
#$app = Start-Process -FilePath $Chrome -ArgumentList "https://google.com --headless --user-data-dir=`"$UserData`" --remote-debugging-port=$DebugPort" -PassThru
$app = Start-Process -FilePath $Chrome -ArgumentList "https://google.com --user-data-dir=`"$UserData`" --remote-debugging-port=$DebugPort" -PassThru
#return
#Write-Host $app.Id
$WebClient = new-object system.net.webclient
$json=$WebClient.DownloadString("http://localhost:$DebugPort/json") | ConvertFrom-Json
if ($json -is [array]) {
$wsDebugger=$json[0].webSocketDebuggerUrl
} else {
$wsDebugger=$json.webSocketDebuggerUrl
}
Try{
Do{
$URL = $wsDebugger
$WS = New-Object System.Net.WebSockets.ClientWebSocket
$CT = New-Object System.Threading.CancellationToken
$WS.Options.UseDefaultCredentials = $true
#Write-Host $URL
#Get connected
$Conn = $WS.ConnectAsync($URL, $CT)
While (!$Conn.IsCompleted) {
Start-Sleep -Milliseconds 100
}
#$Size = 4096
#$Array = [byte[]] @(,0) * $Size
$Command = [System.Text.Encoding]::UTF8.GetBytes("{`"id`": 1, `"method`": `"Network.getAllCookies`"}")
$Send = New-Object System.ArraySegment[byte] -ArgumentList @(,$Command)
$Conn = $WS.SendAsync($Send, [System.Net.WebSockets.WebSocketMessageType]::Text, $true, $CT)
While (!$Conn.IsCompleted) {
Start-Sleep -Milliseconds 100
}
$taskResult = $null
$buffer = [Net.WebSockets.WebSocket]::CreateClientBuffer(1024, 1024)
While ($WS.State -eq 'Open') {
$jsonResult = ""
do {
$taskResult = $WS.ReceiveAsync($buffer, $CT)
While (! $taskResult.IsCompleted) {
#Write-Host "Sleeping for 100 ms"
Start-Sleep -Milliseconds 100
}
$jsonResult += [Text.Encoding]::UTF8.GetString($buffer, 0, $taskResult.Result.Count)
} until (
$WS.State -ne [Net.WebSockets.WebSocketState]::Open -or $taskResult.Result.EndOfMessage
)
$z = $jsonResult | ConvertFrom-Json
Write-Output $z.result.cookies
$WS.Dispose()
}
} Until ($WS.State -ne 'Open')
}Finally{
If ($WS) {
$WS.Dispose()
}
}
Stop-Process -Id $app.Id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment