# Source Code Examples

## DVWA - Low&#x20;

In the DVWA the LFI vulnerability page URL is /dvwa/vulnerabilities/fi/page=\<page>. The page URL parameter is vulnerable. Using directory traversal you can retrieve the /etc/passwd file.&#x20;

![Retrieving /etc/passwd using directory traversal in the page URL parameter](https://4166910944-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6GgEQGJyt_GNdNEMG7%2F-MPu6xCSFDjaAUGG6BJw%2F-MPuXQHEjCsyp7kiCRNY%2FScreen%20Shot%202020-12-31%20at%203.41.51%20PM.png?alt=media\&token=ccb6d2da-c6e4-4fa0-81e3-67493a5183c5)

The code that handles this does not validate the user input from the page URL parameter. It merely sets the URL parameter to the file variable.&#x20;

```
//low.php
<?php

// The page we wish to display
$file = $_GET[ 'page' ];

?>
```

The Index.php handles this file variable:&#x20;

```
//index.php 
// if( count( $_GET ) )
if( isset( $file ) )
	include( $file );
else {
	header( 'Location:?page=include.php' );
	exit;
}

```

Seen above, the low.php uses the URL parameter and sets the $file variable based on it. The code in Index.php then retrieves the file if it exists. There is no sanitization of the user input or checks to validate the accessibility of the file. Thus allowing LFI.&#x20;

## DVWA - Medium

The source code is modified in the Medium security level to validate the user input, remove http and https and strip dot-dot-slash characters.&#x20;

```
<?php

// The page we wish to display
$file = $_GET[ 'page' ];

// Input validation
$file = str_replace( array( "http://", "https://" ), "", $file );
$file = str_replace( array( "../", "..\"" ), "", $file );

?>
```

These attempted checks can be bypassed by merely entering _/etc/passwd_ in the _page_ URL parameter without using "../".&#x20;

![](https://4166910944-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6GgEQGJyt_GNdNEMG7%2F-MPu6xCSFDjaAUGG6BJw%2F-MPuZjJ8Q2T69CfFEgcJ%2FScreen%20Shot%202020-12-31%20at%203.52.29%20PM.png?alt=media\&token=2339c59f-c617-41cb-ac6d-039e8e6b480d)

## DVWA - High&#x20;

The high security level only allows inputs starting with word "file" or if the file is "include.php".&#x20;

```
//high.php
<?php

// The page we wish to display
$file = $_GET[ 'page' ];

// Input validation
if( !fnmatch( "file*", $file ) && $file != "include.php" ) {
	// This isn't the page we want!
	echo "ERROR: File not found!";
	exit;
}

?>
```

This can be bypassed using the "File" URI scheme since it starts with the word "File". The File URI scheme is used to access files on the localhost. Changing the parameter to file:///etc/passwd will successfully return the file.&#x20;

![](https://4166910944-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6GgEQGJyt_GNdNEMG7%2F-MPu6xCSFDjaAUGG6BJw%2F-MPu_oB26J337k4PePMY%2FScreen%20Shot%202020-12-31%20at%203.56.42%20PM.png?alt=media\&token=4da69fc5-6430-4edf-aaf8-9fd4221397be)

## DVWA - Impossible&#x20;

The code for the impossible security level only allows the "include.php" file or files 1-3 which are hard coded into the if statement. There is no way to exploit this to retrieve arbitrary files.&#x20;

```
//Impossible.php
<?php

// The page we wish to display
$file = $_GET[ 'page' ];

// Only allow include.php or file{1..3}.php
if( $file != "include.php" && $file != "file1.php" && $file != "file2.php" && $file != "file3.php" ) {
	// This isn't the page we want!
	echo "ERROR: File not found!";
	exit;
}

?>
```

## XVWA&#x20;

The source code that retrieves files for the LFI example is in xvwa/vulnerabilities/fi/home.php file.&#x20;

The URL to exploit is `http://localhost:8012/xvwa/vulnerabilities/fi/?file=/Windows/system.ini`

The "file" URL parameter is used in line 13 and if the parameter is not null it retrieves the files with no input checks or validation and returns the file to the user.&#x20;

```
//home.php
<div class="well">

    <p>
        <form method="get" action="">
            <div class="form-group">
                <br>
                <div class="text-left">
                <?php 
                    $f='readme.txt';
                    echo "<a class=\"btn btn-primary\" href=\".?file=$f\" /> Click here </a><br><br>";

                    if (isset($_GET['file'])) {
                        $file=$_GET['file'];
                        include($file);
                    }                 
                ?>
                </div>
            </div>
        </form>
    </p>

      
    <hr>
    
</div>
```

![](https://4166910944-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6GgEQGJyt_GNdNEMG7%2F-MPu6xCSFDjaAUGG6BJw%2F-MPubQO2JV2JZ_JppwLe%2FScreen%20Shot%202020-12-31%20at%204.03.06%20PM.png?alt=media\&token=2ff33ac7-685f-4a43-9718-3684cc4cc573)
