In a previous article we explained what path/directory traversal is. And we looked at what happens behind the scenes on the server side. In this article we will take a look at the source of the problem.
Clearly not a masterpiece of code that I’ve written here. But it does the job of demonstrating the vulnerability.
Here is a screenshot of the code. As you can see, it is straight forward. But let’s walk though it anyway.
On line 13 we are declaring a variable called file. The variable holds whatever is collected by the HTTP GET request. Which ,in turn, accepts the user input in a URL parameter called image.
In other words: User Input in image > HTTP GET > file variable
An example of this would be a user clicking on an image. The browser sends a GET request to grab the image from the web server. Then display the image in the browser.
The URL that does the magic, will look something like this
On line 15 we start a simple if/else statement.
If the value held by the file variable (in our case kittens.jpg) exists, then the server accepts it and can fetch it from its directories and displays it to the user. Otherwise, if a cat walks on the keyboard and ends up typing something random like asdfou.jpg, then a 404 error page is returned instead.
Directory Traversal Dogs
Assuming the user is a dog person, with no cats walking on their keyboard, they click on an existing image, and the following URL is created:
And therein lies the vulnerability. Because our code doesn’t do any checks on what is being “included” we have to wonder what happens if the user tries to include a file outside the web directory.
The result will be path/directory traversal and they’ll be able to display the contents of arbitrary files like the example below.
This of course leaves us with a final question. Are you a cat or a dog person?
I hope this clarifies file/directory traversal more.
For any feedback, suggestions or corrections, don’t hesitate to get in touch.