Python pth文件写入getshell
2023-4-17 09:37:55 Author: y4er.com(查看原文) 阅读量:26 收藏

https://www.sonarsource.com/blog/pretalx-vulnerabilities-how-to-get-accepted-at-every-conference/

在这篇文章中学到的,记一下。

python的site模块支持"Site-specific configuration hook"的功能,这个功能点本身是用来将特定路径加入模块搜索路径。该模块在初始化期间自动导入。

https://y4er.com/img/uploads/Python-pth-file-write-getshell/1.png

sys.prefixsys.exec_prefixC:/Python,那么创建路径为C:\Python\Lib\site-packages\1.pth的pth文件,然后其内容为

那么在新的python进程中,temp路径将被添加到sys.path中

https://y4er.com/img/uploads/Python-pth-file-write-getshell/2.png

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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文件内容为

1
import os;os.system("calc")

新启动python进程则会执行calc命令。

https://y4er.com/img/uploads/Python-pth-file-write-getshell/3.png

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

文笔垃圾,措辞轻浮,内容浅显,操作生疏。不足之处欢迎大师傅们指点和纠正,感激不尽。


文章来源: https://y4er.com/posts/python-pth-file-write-getshell/
如有侵权请联系:admin#unsafe.sh