winver.py 3.05 KB
import os
from runcmd import runProcess
import re
import pefile

_WIN_9x     = 0
_WIN_ME     = 1
_WIN_NT     = 2
_WIN_2k     = 3
_WIN_XP     = 4
_WIN_2k3    = 5
_WIN_VISTA  = 6
_WIN_2k8    = 6
_WIN_7      = 7
_WIN_2k8R2  = 7
_WIN_8      = 8
_WIN_2k12   = 8
_WIN_81     = 9
_WIN_2k12R2 = 9
_WIN_10     = 10



def getWindowsVersion(path):
    if(os.path.isfile(getWindowsDirectory(path) + "/System32/ntdll.dll")):
        version = ""
        pe = pefile.PE(getWindowsDirectory(path) + "/System32/ntdll.dll")
        for entry in pe.FileInfo:
            if hasattr(entry, 'StringTable'):
                for st in entry.StringTable:
                    for k, v in st.entries.items():
                        if k == "ProductVersion":
                            version = v
                            if re.match("(3|4)\.",version) != None:
                                return _WIN_NT
                            elif re.match("5\.0", version) != None:
                                return _WIN_2k
                            elif re.match("5\.1", version) != None:
                                return _WIN_XP
                            elif re.match("5\.2", version) != None:
                                return _WIN_2k3
                            elif re.match("6\.0", version) != None:
                                return _WIN_VISTA
                            elif re.match("6\.1", version) != None:
                                return _WIN_7
                            elif re.match("6\.2", version) != None:
                                return _WIN_8
                            elif re.match("6\.3", version) != None:
                                return _WIN_81
                            elif re.match("10\.", version) != None:
                                return _WIN_10
    else:
        if(os.path.isfile(getWindowsDirectory(path) + "/CLASSES.DAT")):
            return _WIN_ME
        return _WIN_9x

def getWindowsDirectory(path):
    result,code = runProcess(["find",path,"-xdev","-iname","explorer.exe","-print","-quit"])
    if(result.decode("utf-8") == ""):
        return None
    matches = result.decode("utf-8").splitlines()
    #WARNING: ONLY CONSIDERING FIRST MATCH
    #TODO: Improve
    return os.path.dirname(os.path.realpath(matches[0]))

def getUserProfiles(path):
    version = getWindowsVersion(path)
    if version < _WIN_XP:
        profilepath = getWindowsDirectory(path) + "/Profiles"
        if (os.path.exists(profilepath)):
            return [profilepath + prof for prof in os.listdir(profilepath)]
        return []
    elif version == _WIN_XP:
        profilepath = path + "/Documents and Settings"
        if (os.path.exists(profilepath)):
            return [profilepath + prof for prof in os.listdir(profilepath)]
        return []
    else:
        profiles = []
        profilepath = path + "/Users"
        if (os.path.exists(profilepath)):
            for elem in os.listdir(profilepath):
                if(os.path.isdir(profilepath + "/" + elem)):
                    profiles.append(profilepath + "/" + elem)

        return profiles
    pass