---
title: "1. Intro"
date: 2024-10-15
draft: false
---

PHP JS Java use http params to specify what is shown on a page. 
If these functions are not securely coded, attackers can manipulate them to display content of any local file on the host. 
This is known as LFI. 

**Most common place to find LFI is Templating Engines**
- Templating engine displays page that shows common static parts 
	- header
	- nav bar
	- footer
- then tdynamically loads other content that changes between pages. 

Otherwise, every page on server needs to be modified when changes are made to any static part 
This is often why we see `/index.php?page=about` where `index` sets static content (header/footer), then only pulls dynamic content specified in the parameter - in this case may be read from `about.php` 

Since we can control the `about` portion of the request its potentially possible to have the webapp grab other files and display them. 

LFI can lead to: 
- source code exposure
- sensitive data exposure
- even RCE under certain conditions 

### reviewing examples of vulnerable code
In `PHP`, we may use the `include()` function to load a local or a remote file as we load a page. If the `path` passed to the `include()` is taken from a user-controlled parameter, like a `GET` parameter, and `the code does not explicitly filter and sanitize the user input`, then the code becomes vulnerable to File Inclusion. The following code snippet shows an example of that:

```php
if (isset($_GET['language'])) {
    include($_GET['language']);
}
```

`language` passed into `include()` function - any path we pass into language parameter will be loaded into the page. 

not exclusive to include! `include_once()`, `require()`, `require_once()`, `file_get_contents()` functions can behave this way

**nodejs example**
```javascript
if(req.query.language) {
    fs.readFile(path.join(__dirname, req.query.language), function (err, data) {
        res.write(data);
    });
}
```
whatever param from url gets used by `readfile` function - writing the file content in http response 
`render()` function can be susceptible to this as well 


---
The following table shows which functions may execute files and which only read file content:

|**Function**|**Read Content**|**Execute**|**Remote URL**|
|---|:-:|:-:|:-:|
|**PHP**||||
|`include()`/`include_once()`|✅|✅|✅|
|`require()`/`require_once()`|✅|✅|❌|
|`file_get_contents()`|✅|❌|✅|
|`fopen()`/`file()`|✅|❌|❌|
|**NodeJS**||||
|`fs.readFile()`|✅|❌|❌|
|`fs.sendFile()`|✅|❌|❌|
|`res.render()`|✅|✅|❌|
|**Java**||||
|`include`|✅|❌|❌|
|`import`|✅|✅|✅|
|**.NET**||||
|`@Html.Partial()`|✅|❌|❌|
|`@Html.RemotePartial()`|✅|❌|✅|
|`Response.WriteFile()`|✅|❌|❌|
|`include`|✅|✅|✅|

File execution may allow us to execute functions and lead to code execution 
Only reading content can allow us source code review but not code execution 
