Skip to content

Instantly share code, notes, and snippets.

@turowicz
Last active October 19, 2019 18:08
Show Gist options
  • Save turowicz/ac12957ee4e4ec8e896b626502d6ded1 to your computer and use it in GitHub Desktop.
Save turowicz/ac12957ee4e4ec8e896b626502d6ded1 to your computer and use it in GitHub Desktop.
Transmit files with Clipboard over RDP

DISCLAIMER: I know this is stupid. Sometimes necessary evil.

  1. Start send.ps1 with full path to the output file.
  2. Start receive.ps2 with full path to input file.
  3. If on a VM, make sure clipboard sharing between host and VM is disabled.
  4. If you see "9" and other numbers appearing slowly, it means its working.
  5. If you see "9" or "0" or anything appearing very rapidly, it means you should restart the process.
Param([string] $file)
Function SetClipboard()
{
Param([string] $text)
while (1 -eq 1)
{
try
{
Set-ClipBoard $text
$c = Get-ClipBoard
if ($c -eq $text)
{
return
}
}
catch
{
Write-Host $_.Exception.Message
Start-Sleep -Milliseconds 10
}
}
}
SetClipboard -text "listening"
Write-Host "listening"
$length = 0
while ($length -eq 0)
{
try
{
$clip = Get-ClipBoard
$length = [System.Int32]::Parse($clip)
Write-Host $clip
Start-Sleep -Milliseconds 100
}
catch
{
Start-Sleep -Milliseconds 1
}
}
Write-Host "Receiving $($length) bytes"
$received = 0
$current = Get-ClipBoard
$next = $current
$arrays = New-Object System.Collections.ArrayList
while ($received -lt $length)
{
SetClipboard -text "listening"
$next = Get-ClipBoard
while ($next -eq "listening" -Or $next.Length -eq 0)
{
if ($next -eq "listening")
{
SetClipboard -text "listening"
Start-Sleep -Milliseconds 300
}
$next = Get-ClipBoard
Start-Sleep -Milliseconds 5
Write-Host $next.Length
}
if ($next -ne $current)
{
[byte[]] $decoded = [System.Convert]::FromBase64String($next)
if($decoded.Length -gt 0)
{
$received += $decoded.Length
$arrays.Add($decoded) > $null
$current = $next
Write-Host $received
}
}
Start-Sleep -Milliseconds 10
}
Write-Host $received
Write-Host "Merging"
$result = New-Object byte[] $length
$count = $arrays.Count
$page = $arrays[0].Length
for ($i=0; $i -lt $arrays.Count; $i++)
{
Write-Host "Merging array $($i)/$($count)"
$buffer = $arrays[$i]
for ($w=0; $w -lt $buffer.Length; $w++)
{
$result[($page * $i) + $w] = $buffer[$w]
}
}
Write-Host "Writing file"
[System.IO.File]::WriteAllBytes($file, $result)
Param([string] $file)
Function SetClipboard()
{
Param([string] $text)
while (1 -eq 1)
{
try
{
Set-ClipBoard $text
$c = Get-ClipBoard
if ($c -eq $text)
{
return
}
}
catch
{
Start-Sleep -Milliseconds 10
}
}
}
SetClipboard -text "waiting"
Write-Host "waiting"
$status = 0
while ($status -eq "waiting" -Or $status -ne "listening" -Or $status.Length -eq 0)
{
try
{
if($status.Length -eq 0)
{
Write-Host "status 0"
SetClipboard -text "waiting"
}
$status = Get-ClipBoard
Start-Sleep -Milliseconds 100
}
catch
{
Write-Host $_.Exception.Message
Start-Sleep -Milliseconds 1
}
}
$position = 0
$written = 0
$page = 0
$length = 500000
$data = [System.IO.File]::ReadAllBytes($file)
Write-Host $data.Length
SetClipboard -text $data.Length
while ($written -lt $data.Length)
{
$status = 0
while ($status -ne "listening" -Or $status.Length -eq 0)
{
try
{
$status = Get-ClipBoard
if ($status.Length -eq 0)
{
Start-Sleep -Milliseconds 100
}
Start-Sleep -Milliseconds 25
Write-Host $status.Length
}
catch
{
Write-Host $_.Exception.Message
Start-Sleep -Milliseconds 1
}
}
$position = $length * $page
$remaining = $data.Length - $position
if ($remaining -ge 0)
{
if ($remaining -lt $length)
{
$buffer = New-Object byte[] $remaining
for ($i=0; $i -lt $remaining; $i++)
{
$buffer[$i] = $data[$position + $i]
$written++
}
$encoded = [Convert]::ToBase64String($buffer)
}
else
{
$buffer = New-Object byte[] $length
for ($i=0; $i -lt $length; $i++)
{
$buffer[$i] = $data[$position + $i]
$written++
}
$encoded = [Convert]::ToBase64String($buffer)
}
Write-Host $written
$sent = 0
while($sent -eq 0)
{
try
{
SetClipboard -text $encoded
$sent = 1
}
catch
{
Write-Host $_.Exception.Message
Start-Sleep -Milliseconds 25
}
}
$page++
}
}
Write-Host $written
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment