这简直不是一件小事,可惜答案并不像我希望的那样简单。
如果以后有人会被这个同样的问题困扰,我就把这个答案贴出来,但希望也许有人能早点贴出更好的解决方案来
为了找到主人,我用 本库及其实例:from smb.SMBConnection import SMBConnection
conn = SMBConnection(username='', password='', domain=', my_name='', remote_name='')
conn.connect('')
sec_att = conn.getSecurity('', r'\some\file\path')
owner_sid = sec_att.owner
问题是 pysmb 包只能给你所有者的SID,而不是他的名字。为了得到他的名字,你需要 做一个ldap查询,就像这个答案一样。 (转贴代码)。from ldap3 import Server, Connection, ALL
from ldap3.utils.conv import escape_bytes
s = Server('my_server', get_info=ALL)
c = Connection(s, 'my_user', 'my_password')
c.bind()
binary_sid = b'....' # your sid must be in binary format
c.search('my_base', '(objectsid=' + escape_bytes(binary_sid) + ')', attributes=['objectsid', 'samaccountname'])
print(c.entries)
但当然没有什么是容易的,我花了好几个小时才找到一种方法 在python中把字符串SID转换为二进制SID。最后这就解决了:# posting the needed functions and omitting the class part
def byte(strsid):
'''
Convert a SID into bytes
strdsid - SID to convert into bytes
'''
sid = str.split(strsid, '-')
ret = bytearray()
sid.remove('S')
for i in range(len(sid)):
sid[i] = int(sid[i])
sid.insert(1, len(sid)-2)
ret += longToByte(sid[0], size=1)
ret += longToByte(sid[1], size=1)
ret += longToByte(sid[2], False, 6)
for i in range(3, len(sid)):
ret += cls.longToByte(sid[i])
return ret
def byteToLong(byte, little_endian=True):
'''
Convert bytes into a Python integer
byte - bytes to convert
little_endian - True (default) or False for little or big endian
'''
if len(byte) > 8:
raise Exception('Bytes too long. Needs to be <= 8 or 64bit')
else:
if little_endian:
a = byte.ljust(8, b'\x00')
return struct.unpack('
else:
a = byte.rjust(8, b'\x00')
return struct.unpack('>q', a)[0]
... 最后你有完整的解决方案!享受:(