Skip to content
Advertisement

How do I access a file for reading/writing in a different (non-current) directory?

I am working on the listener portion of a backdoor program (for an ETHICAL hacking course) and I would like to be able to read files from any part of my linux system and not just from within the directory where my listener python script is located – however, this has not proven to be as simple as specifying a typical absolute path such as “~/Desktop/test.txt”

So far my code is able to read files and upload them to the virtual machine where my reverse backdoor script is actively running. But this is only when I read and upload files that are in the same directory as my listener script (aptly named listener.py). Code shown below.

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

As I’ve mentioned previously, the above function only works if I try to open and read a file that is in the same directory as the script that the above code belongs to, meaning that path in the above content is a simple file name such as “picture.jpg”

I would like to be able to read a file from any part of my filesystem while maintaining the same functionality.

For example, I would love to be able to specify “~/Desktop/another_picture.jpg” as the path so that the contents of “another_picture.jpg” from my “~/Desktop” directory are base64 encoded for further processing and eventual upload.

Any and all help is much appreciated.

Edit 1: My script where all the code is contained, “listener.py”, is located in /root/PycharmProjects/virus_related/reverse_backdoor/. within this directory is a file that for simplicity’s sake we can call “picture.jpg” The same file, “picture.jpg” is also located on my desktop, absolute path = “/root/Desktop/picture.jpg”

When I try read_file(“picture.jpg”), there are no problems, the file is read.

When I try read_file(“/root/Desktop/picture.jpg”), the file is not read and my terminal becomes stuck.

Edit 2: I forgot to note that I am using the latest version of Kali Linux and Pycharm. I have run “realpath picture.jpg” and it has yielded the path “/root/Desktop/picture.jpg”

Upon running read_file(“/root/Desktop/picture.jpg”), I encounter the same problem where my terminal becomes stuck.

[FINAL EDIT aka Problem solved]:

Based on the answer suggesting trying to read a file like “../file”, I realized that the code was fully functional because read_file(“../file”) worked without any flaws, indicating that my python script had no trouble locating the given path. Once the file was read, it was uploaded to the machine running my backdoor where, curiously, it uploaded the file to my target machine but in the parent directory of the script. It was then that I realized that problem lied in the handling of paths in the backdoor script rather than my listener.py

Credit is also due to the commentator who pointed out that “~” does not count as a valid path element. Once I reached the conclusion mentioned just above, I attempted read_file(“~/Desktop/picture.jpg”) which failed. But with a quick modification, read_file(“/root/Desktop/picture.jpg”) was successfully executed and the file was uploaded in the same directory as my backdoor script on my target machine once I implemented some quick-fix code.

My apologies for not being so specific; efforts to aid were certainly confounded by the unmentioned complexity of my situation and I would like to personally thank everyone who chipped in.

This was my first whole-hearted attempt to reach out to the stackoverflow community for help and I have not been disappointed. Cheers!

Advertisement

Answer

A solution I found is putting “../” before the filename if the path is right outside of the dictionary.

test.py (in some dictionary right inside dictionary “Desktop” (i.e. /Desktop/test):

with open("../test.txt", "r") as test:
    print(test.readlines())

test.txt (in dictionary “/Desktop”)

Hi!
Hello!

Result:

["Hi!", "Hello!"]

This is likely the simplest solution. I found this solution because I always use “cd ../” on the terminal.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement