【问题标题】:make a python program have my UID to modificate a file让 python 程序有我的 UID 来修改文件
【发布时间】:2020-04-14 16:12:29
【问题描述】:

因此,简而言之,我创建了一个脚本(名为 main.py),其中有一段时间我在文件中写入内容。它运作良好。但是,该文件的权限必须是 rwxrwxrw-,因此任何人都可以修改服务器上的文件。那不是我想要的。于是我把权限改成了 rwxrwxr-- 然后我改了 main.py 的代码:

#!/usr/bin/python
import subprocess
text = "I want this text to appear in my file"
command = subprocess.Popen(["python", "modificateFile.py", str('"')+text+str('"')], stderr=subprocess.PIPE, stdout=subprocess.PIPE) #I run another file that will do the task
for i in command.stderr:
    print(i.decode("utf-8")) #check if there is any error

modificateFile.py 的代码

#!/usr/bin/python
import platform
import os
import sys

UID = 1080 #my UID.. I don't really know if it's the right way to program this

if __name__ == "__main__":
    system = platform.system()
    if system == "Linux": #ok it may be useless
        os.setuid(UID) #            /!\ I THINK THAT'S WHERE THE PROBLEM IS /!\
        if len(sys.argv) > 1:
            with open("file.txt", "w", encoding="utf-8") as f:
                f.write(sys.argv[1])
        else:
            sys.stderr.write("not enough parameters to work") #didn't know which error I could raise.. 
#so as I already imported sys, I used this function
            exit(-1)
    else:
        sys.stderr.write("wrong OS : program only work on linux")
        exit(-1)

当我创建这个时,老实说,我真的不知道自己在做什么……我正在学习编程。 错误信息:

Traceback(最近一次调用最后一次):文件“modificateFile.py”,第 11 行, 在 os.setuid(UID) PermissionError: [Errno 1] Operation not allowed

我听说过 SUID.. 但我没有 root 权限。

有人可以解释什么是错的,我能做些什么吗? (如果您需要更多元素,请告诉我)

【问题讨论】:

    标签: python python-3.x linux permissions


    【解决方案1】:

    好的……我以为 SUID 只能用于根程序……我已经为这个程序测试过它,但它不起作用。但是,后来我了解到 SUID 不适用于开头带有 shebang 的程序……所以,我用 C 语言创建了一个代码来做到这一点。

    【讨论】:

      猜你喜欢
      • 2010-10-30
      • 1970-01-01
      • 2010-10-14
      • 1970-01-01
      • 2021-04-22
      • 2022-11-02
      • 1970-01-01
      • 2022-06-25
      相关资源
      最近更新 更多