winver.py 2.85 KB
import os
from runcmd import runProcess
import re
import pefile
import findfile

_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

DETECTED_WINDOWS_PATH = {}

def getWindowsVersion(path):
    if(os.path.isfile(getWindowsDirectory(path) + "/System32/ntdll.dll")):
        version = ""
        pe = pefile.PE(getWindowsDirectory(path) + "/System32/ntdll.dll")
        version = str(pe.FileInfo[0].StringTable[0].entries[b'ProductVersion'],'utf-8')
        pe.close()
        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):
    if path not in DETECTED_WINDOWS_PATH:
      result = findfile.find_pattern("explorer.exe",path,False)
      #result,code = runProcess(["find",path,"-xdev","-iname","explorer.exe","-print","-quit"])
      #if(result.decode("utf-8") == ""):
      if result is None:
          return None
      #matches = result.decode("utf-8").splitlines()
      #WARNING: ONLY CONSIDERING FIRST MATCH
      #TODO: Improve
      #return os.path.dirname(os.path.realpath(matches[0]))
      DETECTED_WINDOWS_PATH[path] = os.path.dirname(os.path.realpath(result))
    return DETECTED_WINDOWS_PATH[path]

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