ReFirmLabs / binwalk

Firmware Analysis Tool
MIT License
10.75k stars 1.54k forks source link

Would it be possible to use the built-in python module 'getpass' as a somewhat OS agnostic way to get the username? #657

Closed JeffreyO closed 1 week ago

JeffreyO commented 1 year ago

As opposed to the 'pwd' module which is Unix specific and/or needing to litter the Python code with conditional statements about the OS.

JeffreyO commented 1 year ago

As for the test for Admin or root, I haven't found an OS agnostic way yet. The closest I could find was this answer from grigoryvp on this question at Stack Overflow: https://stackoverflow.com/questions/1026431/cross-platform-way-to-check-admin-rights-in-a-python-script-under-windows. The same solution appears to have been taken without credit by the creator of the following website, where they have discovered that it doesn't work on Windows XP: https://raccoon.ninja/en/dev/using-python-to-check-if-the-application-is-running-as-an-administrator/

I'm still looking.

mzpqnxow commented 8 months ago

Well, the purpose of checking if binwalk is running as root/administrator, why not theow away the idea that this should be done based on a username and instead use pseido-ish code like:

def can_write(path):
    if windows:
        ... windows equivalent to access()
        return bool(result)
    return os.access(path, os.W_OK)

def rootdir():
    if windows: return “c:\\”
    return “/“
if can_write(os.path.join(rootdir(), “testfile”)):
    raise RuntimeError(“too dangerous”)

Checking for the username “Administrator” or “root” is not really a sufficient check to determine safety anyway

I’m not much of a Windows user, but I can speak to Linux and say that there are ways to allow any user to bypass filesystem access controls so (e.g. CAP_* capabilities that override kernel filesystem permission checking logic)

Having logic like the above to actually test if the dangerous behavior is effectively permitted using a real test of the functionality can simplify the check and make it more comprehensive on all of Windows, Linux and MacOS

EDIT: I realize there’s a better way to get the Windows installation drive rather than assuming it’s C: but I can’t be bothered to look it up 😁