OS Library
The OS library provides different functionality that interfaces with an operating system. Examples include viewing file permissions, interacting with processes and various other operating system functions. Today we’ll look at using this library to list files in a particular directory
To start using a library, we need to import it as in line 1. Line 3 shows the listdir function taking the path as a parameter. Line 4 is printing the output of the variable and line 5 is a loop that iterates through the variable and prints the values in the variable. Running this python script gives this output:
The first line prints the type of the variable, which is a list. This makes sense as the following lines in the code iterate through the list and print the output.
There are various other useful functions in the OS library.
Zipfile Library
From time to time, we’ll encounter compressed files. A common compressed file format is the “.zip” format. We would want to extract these files for future use. We can do this using the zipfile library.
Like every library, we start out by importing it. Line 2 starts with the with keyword. The with keyword follows the following format
with some-code as variable:
do action
In the above, the zipfile library is taking the file provided by the path in read mode and creating a zipfile object with it, and referencing it as the variable zip_ref. On line 3, it’s referencing the variable to extract the contents of the zip file. The with directive is useful because it is used to manipulate files; all actions inside the directive are done while the file is open, which means that a programmer does not explicitly have to close a file after carrying out file manipulation.
The above piece of code taking a zip file at a particular path and extracts the contents of the file to a particular directory.
Exiftool
Exiftool is a command line tool that extracts metadata from files. Metadata is usually provides a trove of information such a file owner, software used to create the file, and more. This data is used as part of the enumeration step when carrying out attacks. For example, if a user finds a particular software name and version used to create a file, they can find exploits against these particular softwares.
The output of the command line tool usually looks as follows: