• 技术文章 >后端开发 >Python教程

    如何使用fdopen实现对Python进程产生的文件进行权限最小化配置

    王林王林2023-04-28 22:22:05转载65

    需求背景

    用python进行文件的创建和读写操作时,我们很少关注所创建的文件的权限配置。对于一些安全性较高的系统,如果我们创建的文件权限其他用户或者同一用户组里的其他用户有可读权限的话,有可能导致不必要的信息泄漏的风险。因此,除了创建一个更加安全和隐私的个人环境之外(如容器环境等),我们还可以对生成的文件的配置进行权限最小化处理。

    常用方法及其缺陷分析

    常用的python文件创建和读写方法,是直接通过内置的open函数创建一个文件。这里如果是使用with语法来创建的,结束语句后会自动关闭被打开的对象。而如果是直接使用open函数来定义一个对象,则需要在任务结束时手动的执行close操作。以下演示内置函数open的用法及其文件操作属性,首先创建一个名为file-test.py的文件:

    # file-test.py
     
    with open('test1.txt', 'w') as file:
        file.write('hello world!')

    该任务的内容为:在当前目录下创建一个名为test1.txt的文件,清空该文件的内容后,在文件中写入hello world!这个字符串。接下来用python3执行该文件:

    [dechin@dechin-manjaro os_security]$ python3 file-test.py
    [dechin@dechin-manjaro os_security]$ ll
    总用量 8
    -rw-r--r-- 1 dechin dechin 83 1月 25 13:43 file-test.py
    -rw-r--r-- 1 dechin dechin 12 1月 25 13:43 test1.txt

    这里我们发现,在执行之后成功产生了test1.txt这个文件,其权限配置为644,与前面创建的file-test.py保持一致。在不清楚内置函数open的实现原理时,原本以为这个产生的文件权限配置是与当前的py文件保持一致的。然而经过进一步的测试,将py文件的权限配置为440之后再重新执行该文件:

    [dechin@dechin-manjaro os_security]$ chmod 440 file-test.py
    [dechin@dechin-manjaro os_security]$ ll
    总用量 8
    -r--r----- 1 dechin dechin 83 1月 25 13:43 file-test.py
    -rw-r--r-- 1 dechin dechin 12 1月 25 13:43 test1.txt
    [dechin@dechin-manjaro os_security]$ rm test1.txt
    [dechin@dechin-manjaro os_security]$ python3 file-test.py
    [dechin@dechin-manjaro os_security]$ ll
    总用量 8
    -r--r----- 1 dechin dechin 83 1月 25 13:43 file-test.py
    -rw-r--r-- 1 dechin dechin 12 1月 25 13:44 test1.txt

    这里从测试结果我们可以看出,python的内置函数open产生的文件类型是与源py文件无关的。关于这里py文件的执行是否需要可执行权限,可以参考这篇博客。

    改进后的python文件创建方法

    通过fdopen这个库以及特殊的权限指定,我们可以设置生成文件的访问权限,以下直接展示一个python代码案例:

    # fdopen-test.py
     
    import os
    import stat
     
    file_name = 'test2.txt'
    flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
    mode = stat.S_IRUSR | stat.S_IWUSR
     
    with os.fdopen(os.open(file_name, flags, mode), 'w') as file:
        file.write('hello world!')

    执行之后我们可以发现,当前目录下生成了一个名为test2.txt的文件,其权限配置为600, 对照于我们在代码中设置的mode = stat.S_IRUSR | stat.S_IWUSR。这里我们先对其中的一些参数作一个解释:os.O_WRONLY表示以只写的方式打开,os.O_CREAT表示创建并打开一个新文件,os.O_EXCL表示如果文件已存在则报错。而mode中所配置的权限分别对应rwx配置,其中USR,GRP,OTH又分别对用户、用户组、其他用户进行了细分的配置,从而我们就可以通过改变mode参数来实现所有种类的权限配置。

    我们可以尝试将上述用例中的mode作一个调整,比如添加一个可执行权限变为700:

    # fdopen-test.py
     
    import os
    import stat
     
    file_name = 'test3.txt'
    flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
    mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
     
    with os.fdopen(os.open(file_name, flags, mode), 'w') as file:
        file.write('hello world!')

    又或者,我们需要为用户组里的其他用户添加可访问权限,比如640权限:

    # fdopen-test.py
     
    import os
    import stat
     
    file_name = 'test4.txt'
    flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
    mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP
     
    with os.fdopen(os.open(file_name, flags, mode), 'w') as file:
        file.write('hello world!')

    甚至我们也可以写出系统原生的644文件权限:

    # fdopen-test.py
     
    import os
    import stat
     
    file_name = 'test5.txt'
    flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
    mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH
     
    with os.fdopen(os.open(file_name, flags, mode), 'w') as file:
        file.write('hello world!')

    最后,让我们一起看下上面这些python示例执行后得到的结果:

    [dechin@dechin-manjaro os_security]$ ll
    总用量 28
    -rw-r--r-- 1 dechin dechin 269 1月 25 14:58 fdopen-test.py
    -r--r----- 1 dechin dechin 84 1月 25 14:11 file-test.py
    -rw-r--r-- 1 dechin dechin 12 1月 25 13:44 test1.txt
    -rw------- 1 dechin dechin 12 1月 25 14:44 test2.txt
    -rwx------ 1 dechin dechin 12 1月 25 14:48 test3.txt
    -rw-r----- 1 dechin dechin 12 1月 25 14:56 test4.txt
    -rw-r--r-- 1 dechin dechin 12 1月 25 14:58 test5.txt

    从结果中我们可以看出,所有产生的文件test*.txt都按照我们预期的文件权限配置生成,到这里我们就完成了所有预期的目标。

    以上就是如何使用fdopen实现对Python进程产生的文件进行权限最小化配置的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:亿速云,如有侵犯,请联系admin@php.cn删除
    专题推荐:Python
    上一篇:Python怎么使用树状图实现可视化聚类 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • Python中的Array模块怎么使用• Python中数据类型如何转换• 怎么用Python展示全国高校的分布情况• python apscheduler cron定时任务触发接口自动化巡检怎么实现• python如何去除字符串最后的换行符‘\n’
    1/1

    PHP中文网