简体   繁体   English

os.path 可以返回需要 root 访问权限的文件吗?

[英]Can os.path return a file that needs root access?

I am trying to test if python can access a core file on a Mac.我正在尝试测试 python 是否可以访问 Mac 上的核心文件。 The file I have used as an example is one of the bpf file's (the reason I use a plural files is because it will take on the form bpf0, bpf1, bpf2, etc..).我用作示例的文件是 bpf 文件之一(我使用复数文件的原因是因为它将采用 bpf0、bpf1、bpf2 等形式)。 I know for a fact that the file exists.我知道该文件存在的事实。 In fact, I know a program that accesses it and opens it, the airport file.事实上,我知道一个访问它并打开它的程序,机场文件。 Which is basically a program that will test wifi channels and puts your wifi card into monitor mode.这基本上是一个测试 wifi 通道并将您的 wifi 卡置于监控模式的程序。 When I run it along dtrace, to see what happens I get this as a return:当我沿着 dtrace 运行它时,为了看看会发生什么,我得到了这个作为回报:

 8747/0x8431b:  open("/dev/bpf\\0", 0x2, 0xFFFFFFFFFFFFFFFF)         = -1 Err#2\
 8747/0x8431b:  open("/dev/bpf\\0", 0x0, 0xFFFFFFFFFFFFFFFF)         = -1 Err#2\
 8747/0x8431b:  open("/dev/bpf0\\0", 0x2, 0x2)       = -1 Err#16\
 8747/0x8431b:  open("/dev/bpf1\\0", 0x2, 0x2)       = -1 Err#16\
 8747/0x8431b:  open("/dev/bpf2\\0", 0x2, 0x2)       = 4 0\

So bpf was tried to be opened twice, both returned -1 with an error code of #2 bpf0 was tried to be opened but returned an error -1 with an error code #16 bpf1 was tried to be opened but returned an error -1 with an error code #16 bpf2 was successfully opened, and was assigned the value of 4, therefore the offset was 0x4所以bpf被尝试打开两次,都返回-1,错误代码为#2 bpf0 was tried to be opened but returned an error -1 with error code #16 bpf1 was tried to be opened but returned an error -1错误代码#16 bpf2 已成功打开,并被分配了值 4,因此偏移量为 0x4

Things that were then done with this file:然后用这个文件完成的事情:

 8747/0x8431b:  ioctl(0x4, 0x40044271, 0x7FFEEE9DAF10)       = 0 0\
 8747/0x8431b:  ioctl(0x4, 0xC004427F, 0x7FEEAD008590)       = 0 0\
 8747/0x8431b:  ioctl(0x4, 0x40044266, 0x7FFEEE9DAF1C)       = 0 0\
 8747/0x8431b:  ioctl(0x4, 0xC0044266, 0x7FFEEE9DAF1C)       = 0 0\
 8747/0x8431b:  ioctl(0x4, 0x8020426C, 0x7FFEEE9DB420)       = 0 0\
 8747/0x8431b:  ioctl(0x4, 0x4004426A, 0x7FFEEE9DAF1C)       = 0 0\
 8747/0x8431b:  ioctl(0x4, 0xC00C4279, 0x7FFEEE9DAEF0)       = 0 0\
 8747/0x8431b:  ioctl(0x4, 0xC00C4279, 0x7FFEEE9DAEF0)       = 0 0\
 8747/0x8431b:  ioctl(0x4, 0x80044275, 0x7FFEEE9DAF08)       = 0 0\
 8747/0x8431b:  ioctl(0x4, 0x20004269, 0x0)      = 0 0\
 8747/0x8431b:  ioctl(0x4, 0x40044266, 0x7FFEEE9DAF1C)       = 0 0\
 8747/0x8431b:  ioctl(0x4, 0x80104267, 0x7FFEEE9DAEE0)       = 0 0\
 8747/0x8431b:  ioctl(0x4, 0x80044278, 0x7FFEEE9DB4AC)       = 0 0\

Which basically means with being in a sudo "state", the file is accessable.这基本上意味着处于 sudo “状态”时,该文件是可访问的。 When I run the os.webcrawler through the /dev/ folder, it also returns that the file is there:当我通过 /dev/ 文件夹运行 os.webcrawler 时,它还会返回文件在那里:

for root, dirs, files in os.walk("/dev"):
     for filename in files:
         if filename.startswith('bpf'):
              print('there')
              print(filename)

This code will then outputs this:此代码将输出以下内容:

there
bpf0
there
bpf1
there
bpf2
there
bpf3
there
bpf4
there
bpf5

However, if i run the command os.path.isfile('/dev/bpf3') or any of the numbers it always comes up false.但是,如果我运行命令os.path.isfile('/dev/bpf3')或任何数字,它总是出现错误。 I even went to the dev location in the command line and used the ls command and it came up.我什至去了命令行中的开发位置并使用了ls命令,它出现了。 Even if I start python in the command line at /dev as sudo with the command sudo python3 and type os.path.isfile('/dev/bpf3') it comes up false.即使我在 /dev 的命令行中使用命令sudo python3作为 sudo 启动 python 并键入os.path.isfile('/dev/bpf3')它也会出现错误。 Or, if I try to copy the file with the module shutil.copyfile('/dev/bpf4', 'bpf4') it comes up with the error OSError: [Errno 22] Invalid argument .或者,如果我尝试使用模块shutil.copyfile('/dev/bpf4', 'bpf4')复制文件,它会出现错误OSError: [Errno 22] Invalid argument What am I doing wrong?我究竟做错了什么? Am I supposed to try and access the bpf file without any numbers first like airport does, but that would just make no sense.我是否应该像机场那样首先尝试访问不带任何数字的 bpf 文件,但这没有任何意义。

As one of the comments mentioned, This Link worked for me正如提到的评论之一, 此链接对我有用

Specifically this function within the post:特别是这个 function 在帖子中:

def exists(path):
    """Test whether a path exists.  Returns False for broken symbolic links"""
    try:
        os.stat(path)
    except OSError:
        return False
    return True

~from JVDM ~来自 JVDM

I am still unsure how I would copy over the file and I would be able to examine it.我仍然不确定如何复制该文件并能够对其进行检查。 If someone knows please let me know.如果有人知道请告诉我。 If I find that answer I will post it.如果我找到那个答案,我会发布它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM