Understanding Directory Traversal Vulnerability
In this step, we will explore the basics of directory traversal vulnerability and how it can be exploited.
First, run the following command to quickly deploy the lab environment (skip if already deployed):
docker run -d -p 82:80 --name pentesterlab-WebforPentest-1 -it jewel591/vulnbox:pentesterlab-WebforPentest-1 /bin/sh -c 'service apache2 start && tail -f /var/log/apache2/error.log' --registry-mirror='https://registry.docker-cn.com'
Open your web browser and navigate to the following address:
http://localhost:82
You will see several icons under "Directory traversal". Right-click the icon labeled "Example1" and copy the image address.
Open a new browser tab and paste the copied address. The URL should look similar to this:
http://localhost:82/dirtrav/example1.php?file=hacker.png
The key code in example1.php
is as follows:
<?php
$UploadDir = '/var/www/files/';
$file = $_GET['file'];
$path = $UploadDir . $file;
$handle = fopen($path, 'rb');
?>
As you can see, the $path
variable is constructed by concatenating $UploadDir
and $file
without any filtering or validation. This allows us to read files outside the intended directory by modifying the file
parameter.
Since the server is running on a Linux system, we can try to read the /etc/passwd
file. First, try the following payload:
http://localhost:82/dirtrav/example1.php?file=/etc/passwd
There will be no output, as the file
parameter specifies a relative path from the current directory. To access the /etc/passwd
file, we need to use the ../
sequence to traverse up the directory structure. Try the following payload:
http://localhost:82/dirtrav/example1.php?file=../../../../../../../../etc/passwd
You should now see the contents of the /etc/passwd
file displayed in the browser.