Open In App

Preventing Directory Traversal Vulnerability

Directory Traversal is a vulnerability that allows attackers to access files that are present outside the root directory or outside the home directory of that web server. The root directory has some internal files which are not accessible by the user. This vulnerability can be found in web servers or web application code. This type of attack is also known as a path traversal attack.

Directory traversal vulnerabilities can be found by testing all parts of the website that accept input from users, HTTP requests, forms, and cookies. The attacker makes use cd command with two dots (cd..) which changes it to its parent directory. By adding ../ directly to the file path in the URL, we can try to change it into higher directories to view system files. 



Some of the system files which can be accessed by the attacker:

For Unix-based operating systems :



For Windows Operating systems :

Example of a Directory Traversal attack :
A typical example of a vulnerable PHP code is:




<?php
  
    $file = $_GET['page']; //The page we want to display 
  
?>

Given Below is a URL that has an inclusion function as GET method request

http://192.168.29.23/dvwa/vulnerabilities/fi/?page=include.php

Using Directory Traversal Attack, an attacker can append ../ directly to the file path in the URL

http://192.168.29.23/dvwa/vulnerabilities/fi/?page=../../../../../../etc/passwd

Preventing Directory Traversal attacks :

Article Tags :