Cách khai thác lỗ hổng Local File Inclusion của Website

TricksMMO

Administrator
Staff member
### Cách khai thác lỗ hổng bao gồm tệp cục bộ (LFI)

** tl; dr **

Các lỗ hổng bao gồm tệp cục bộ (LFI) xảy ra khi một ứng dụng web bao gồm nội dung của một tệp từ một nguồn từ xa hoặc địa phương.Điều này có thể được các kẻ tấn công khai thác để đọc các tệp nhạy cảm, thực thi mã tùy ý hoặc thậm chí nắm quyền kiểm soát máy chủ.

** Bao gồm tệp cục bộ là gì? **

Các lỗ hổng bao gồm tệp cục bộ (LFI) xảy ra khi một ứng dụng web bao gồm nội dung của một tệp từ một nguồn từ xa hoặc địa phương.Điều này có thể xảy ra khi một ứng dụng web sử dụng hàm dễ bị tổn thương, chẳng hạn như `bao gồm ()` hoặc `yêu cầu ()`, để bao gồm các nội dung của tệp.

Ví dụ, hãy xem xét mã sau:

`` `
$ file = "/etc/passwd";
Bao gồm ($ File);
`` `

Mã này sẽ bao gồm nội dung của tệp `/etc/passwd` trên máy chủ.Nếu tệp `/etc/passwd` chứa thông tin nhạy cảm, chẳng hạn như tên người dùng và mật khẩu, thì kẻ tấn công có thể sử dụng lỗ hổng này để đọc thông tin đó.

** Làm thế nào các lỗ hổng LFI có thể được khai thác? **

Các lỗ hổng LFI có thể được khai thác theo nhiều cách khác nhau.Dưới đây là một vài ví dụ:

*** Đọc các tệp nhạy cảm: ** Kẻ tấn công có thể sử dụng lỗ hổng LFI để đọc các tệp nhạy cảm, chẳng hạn như `/etc/passwd`,`/etc/shadow` hoặc `/etc/hosts`.Thông tin này có thể được sử dụng để có được quyền truy cập trái phép vào máy chủ hoặc để đánh cắp thông tin nhạy cảm.
*** Thực hiện mã tùy ý: ** Kẻ tấn công có thể sử dụng lỗ hổng LFI để thực thi mã tùy ý trên máy chủ.Điều này có thể được sử dụng để cài đặt phần mềm độc hại, đánh cắp dữ liệu hoặc kiểm soát máy chủ.
*** Bỏ qua các hạn chế bảo mật: ** Lỗ hổng LFI có thể được sử dụng để vượt qua các hạn chế bảo mật, chẳng hạn như tường lửa hoặc danh sách kiểm soát truy cập.Điều này có thể cho phép kẻ tấn công truy cập các tài nguyên mà thông thường họ thường không thể truy cập.

** Làm thế nào các lỗ hổng LFI có thể được ngăn chặn? **

Có một số cách để ngăn chặn các lỗ hổng LFI.Dưới đây là một vài lời khuyên:

*** Sử dụng danh sách trắng của các tệp được phép: ** Chỉ cho phép ứng dụng web bao gồm các tệp từ một danh sách cụ thể các vị trí đáng tin cậy.Điều này sẽ ngăn chặn những kẻ tấn công bao gồm các tệp từ các vị trí khác, chẳng hạn như thư mục `/etc`.
*** Sử dụng quyền của tệp một cách chính xác: ** Đảm bảo rằng các tệp được bao gồm bởi ứng dụng web không thể đọc được thế giới.Điều này sẽ ngăn những kẻ tấn công đọc nội dung của các tệp đó.
*** Sử dụng xác thực đầu vào: ** Vệ sinh tất cả các đầu vào của người dùng trước khi bao gồm nó trong một tệp.Điều này sẽ ngăn những kẻ tấn công tiêm mã độc vào tệp.

**Phần kết luận**

Lỗ hổng LFI là một rủi ro an ninh nghiêm trọng.Bằng cách làm theo các mẹo trong bài viết này, bạn có thể giúp ngăn chặn các lỗ hổng này được khai thác trên các ứng dụng web của riêng bạn.

### Mẹo để hoàn thành các thử thách

Một số máy chủ web sẽ coi tất cả các dấu gạch chéo chuyển tiếp (`/`) là đường dẫn đến trang mới, nhưng nếu chúng ta muốn khai thác một tệp như `/etc/shadow` thì sao?

`` `
Ví dụ.com/notes/?include=/etc/shadow
`` `

Máy chủ sẽ nghĩ rằng nó sẽ truy cập `/ghi chú/bao gồm/etc/shadow`.Vì vậy, bạn không thể thêm các dấu gạch chéo về phía trước vào URL vì máy chủ web sẽ nghĩ rằng nó đang truy cập vào một thư mục khác.

Giải pháp là sử dụng mã hóa URL.Mã hóa URL thay thế các ký tự ASCII không an toàn bằng `%` theo sau là hai chữ số thập lục phân.Các dấu gạch chéo phía trước (`/`) có thể được mã hóa là `%2f`.Do đó, chúng ta có thể thay đổi đường dẫn thành:

`` `
Ví dụ.com/notes/?include=%2FETC%2FSHADOW
`` `

Yêu cầu mới này sẽ truy cập `/notes/` và sau đó chuyển đổi `2f` để chuyển tiếp cắt.À chính nó đấy!

**Tài nguyên**

* [Bảng gian lận OWASP LFI] (https://www.owasp.org/index.php/local_file_inclusion_ (lfi) _cheat_sheet)
* [Cách khai thác các lỗ hổng bao gồm tệp cục bộ (LFI)] (https://portswigger.net/blog/how-to- exploit-local-file-inclusion-lfi-vulnerability)
* [Ngăn chặn tệp cục bộ
=======================================
### How to exploit Local File Inclusion (LFI) vulnerabilities

**TL;DR**

Local File Inclusion (LFI) vulnerabilities occur when a web application includes the contents of a file from a remote or local source. This can be exploited by attackers to read sensitive files, execute arbitrary code, or even take control of the server.

**What is Local File Inclusion?**

Local File Inclusion (LFI) vulnerabilities occur when a web application includes the contents of a file from a remote or local source. This can happen when a web application uses a vulnerable function, such as `include()` or `require()`, to include the contents of a file.

For example, consider the following code:

```
$file = "/etc/passwd";
include($file);
```

This code will include the contents of the `/etc/passwd` file on the server. If the `/etc/passwd` file contains sensitive information, such as usernames and passwords, then an attacker could use this vulnerability to read that information.

**How can LFI vulnerabilities be exploited?**

LFI vulnerabilities can be exploited in a variety of ways. Here are a few examples:

* **Reading sensitive files:** An attacker can use an LFI vulnerability to read sensitive files, such as `/etc/passwd`, `/etc/shadow`, or `/etc/hosts`. This information can be used to gain unauthorized access to the server or to steal sensitive information.
* **Executing arbitrary code:** An attacker can use an LFI vulnerability to execute arbitrary code on the server. This can be used to install malware, steal data, or take control of the server.
* **Bypassing security restrictions:** An LFI vulnerability can be used to bypass security restrictions, such as firewalls or access control lists. This can allow an attacker to access resources that they would not normally be able to access.

**How can LFI vulnerabilities be prevented?**

There are a number of ways to prevent LFI vulnerabilities. Here are a few tips:

* **Use a whitelist of allowed files:** Only allow the web application to include files from a specific list of trusted locations. This will prevent attackers from including files from other locations, such as the `/etc` directory.
* **Use file permissions correctly:** Make sure that files that are included by the web application are not world-readable. This will prevent attackers from reading the contents of those files.
* **Use input validation:** Sanitize all user input before including it in a file. This will prevent attackers from injecting malicious code into the file.

**Conclusion**

LFI vulnerabilities are a serious security risk. By following the tips in this article, you can help to prevent these vulnerabilities from being exploited on your own web applications.

### Tips to complete challenges

Some web servers will consider all forward slashes (`/`) as the path to the new page, but what if we want to exploit a file like `/etc/shadow`?

```
Example.com/notes/?include=/etc/shadow
```

The server will think that it will access `/notes/include/etc/shadow`. So you cannot add forward slashes in the URL because the web server will think that it is accessing another folder.

The solution is to use URL encoding. URL encoding replaces the unsafe ASCII characters with `%` followed by two hexadecimal digits. Forward slashes (`/`) can be encoded as `%2F`. Therefore we can change the path to:

```
Example.com/notes/?include=%2fetc%2fshadow
```

This new request will access `/notes/` and then convert `2F` to forward slashes. So that's it!

**Resources**

* [OWASP LFI Cheat Sheet](https://www.owasp.org/index.php/Local_File_Inclusion_(LFI)_Cheat_Sheet)
* [How to Exploit Local File Inclusion (LFI) Vulnerabilities](https://portswigger.net/blog/how-to-exploit-local-file-inclusion-lfi-vulnerabilities)
* [Preventing Local File
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top