开发者

Check if a file is setuid root in Python

开发者 https://www.devze.com 2022-12-18 17:32 出处:网络
I\'m trying to check if a file has the setuid bit in Python. The stat doc mentions a S_ISUID function but it only works with os.chmod(), not to actually read th开发者_如何学运维e setuid bit. It also

I'm trying to check if a file has the setuid bit in Python.

The stat doc mentions a S_ISUID function but it only works with os.chmod(), not to actually read th开发者_如何学运维e setuid bit. It also lists S_IMODE, but I have no idea how to interpret it.

How can I easily check if a file as the setuid root bit set?


stat.S_ISUID is the mode bit for 'setuid'. You compare the stat result's mode to see if it contains that bit:

>>> ping = os.stat('/bin/ping')
>>> ping.st_mode & stat.S_ISUID
2048
>>> echo = os.stat('/bin/echo')
>>> echo.st_mode & stat.S_ISUID
0
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号