jborean93 / smbprotocol

Python SMBv2 and v3 Client
MIT License
323 stars 73 forks source link

fd.writable misreporting #302

Open yellephen opened 4 days ago

yellephen commented 4 days ago

I noticed that fd.writable was returning true when a file was opened with 'rb'.

with smbclient.open_file(file, 'rb') as fd:
    if fd.writable:
        print("     [x] The above file is writable by the smbuser passed.")
        fd.write("test")

The print line was printed and fd.write resulted in an exception.

The following works for the purpose.

try:
    with smbclient.open_file(file,'w') as fdw:
        print("     [x] The above file is writable by the smbuser passed.")
except:
    print("     [ ] No write access to above file.")
jborean93 commented 2 days ago

The writable member is not an attribute but rather a method https://docs.python.org/3/library/io.html#io.IOBase.writable. You can see this when you compare it to the builtin open method where writeable is also a function.

with open("/tmp/test.txt, mode="wb") as fd:
    print(fd.writable)  # <built-in method writable of _io.BufferedWriter object at 0xffffa23f4f60>
    print(fd.writeable()) # True

You need to do

with smbclient.open_file(file, 'wb') as fd:
    if fd.writable():
        print("     [x] The above file is writable by the smbuser passed.")
        fd.write("test")

Keep in mind it doesn't do an access check, it just tells you whether the opened IO object is opened as a writable stream.