def toRaw(self): if type(self.name) is STRINGTYPE: self.name = pywintypes.Unicode(self.name) if type(self.val) is STRINGTYPE: self.val = pywintypes.Unicode(self.val) vallen = len(self.val) + 1 typ = 1 sublen = 6 + 2*len(self.name) + 2 pad = '' if sublen % 4: pad = '\000\000' sublen = sublen + len(pad) + 2*vallen return struct.pack('hhh', sublen, vallen, typ) + getRaw(self.name) + '\000\000' + pad + getRaw(self.val) + '\000\000'
def __init__(self): super(RweHelper, self).__init__() import platform self.os_system = platform.system() self.os_release = platform.release() self.os_version = platform.version() self.os_machine = platform.machine() self.os_uname = platform.uname() if "windows" == self.os_system.lower(): win_ver = "win7_" + self.os_machine.lower() if ("5" == self.os_release): win_ver = "winxp" if logger().HAL: logger().log( "[helper] OS: %s %s %s" % (self.os_system, self.os_release, self.os_version)) if logger().HAL: logger().log("[helper] Using 'helper/win/%s' path for driver" % win_ver) self.use_existing_service = False self.driver_path = None self.win_ver = win_ver self.driver_handle = None self.device_file = pywintypes.Unicode(DEVICE_FILE) c_int_p = POINTER(c_int) # enable required SeSystemEnvironmentPrivilege privilege privilege = win32security.LookupPrivilegeValue( None, 'SeSystemEnvironmentPrivilege') token = win32security.OpenProcessToken( win32process.GetCurrentProcess(), win32security.TOKEN_READ | win32security.TOKEN_ADJUST_PRIVILEGES) win32security.AdjustTokenPrivileges( token, False, [(privilege, win32security.SE_PRIVILEGE_ENABLED)]) win32api.CloseHandle(token) # import firmware variable API try: self.GetFirmwareEnvironmentVariable = kernel32.GetFirmwareEnvironmentVariableW self.GetFirmwareEnvironmentVariable.restype = c_int self.GetFirmwareEnvironmentVariable.argtypes = [ c_wchar_p, c_wchar_p, c_void_p, c_int ] self.SetFirmwareEnvironmentVariable = kernel32.SetFirmwareEnvironmentVariableW self.SetFirmwareEnvironmentVariable.restype = c_int self.SetFirmwareEnvironmentVariable.argtypes = [ c_wchar_p, c_wchar_p, c_void_p, c_int ] except AttributeError, msg: logger().warn( "G[S]etFirmwareEnvironmentVariableW function doesn't seem to exist" ) pass
def GetScheduleShelvePath(config): # In some rare cases (clamwin plugin to BartPE <http://oss.netfarm.it/winpe/>) # path to Shelve may be not in the current dir shelvePath = config.Get('Schedule', 'Path') if not len(shelvePath): shelvePath = GetProfileDir(True) else: if sys.platform.startswith("win"): shelvePath = pywintypes.Unicode(shelvePath) shelvePath = SafeExpandEnvironmentStrings(shelvePath) return shelvePath
def toRaw(self): self.vallen = 0 self.wType = 1 self.name = pywintypes.Unicode('VarFileInfo') sublen = 6 + 2 * len(self.name) + 2 pad = '' if sublen % 4: pad = '\000\000' tmp = ''.join([kid.toRaw() for kid in self.kids]) self.sublen = sublen + len(pad) + len(tmp) return (struct.pack('hhh', self.sublen, self.vallen, self.wType) + getRaw(self.name) + '\000\000' + pad + tmp)
def toRaw(self): self.wValueLength = len(self.kids) * 2 self.wType = 0 if type(self.name) is STRINGTYPE: self.name = pywintypes.Unicode(self.name) sublen = 6 + 2*len(self.name) + 2 pad = '' if sublen % 4: pad = '\000\000' self.sublen = sublen + len(pad) + self.wValueLength tmp = ''.join([struct.pack('h', kid) for kid in self.kids]) return (struct.pack('hhh', self.sublen, self.wValueLength, self.wType) + getRaw(self.name) + '\000\000' + pad + tmp)
def _invokeex_(self, dispid, lcid, wFlags, args, kwargs, serviceProvider): if dispid == 0: # item l = len(args) if l < 1: raise COMException(desc="not enough parameters", scode=winerror.DISP_E_BADPARAMCOUNT) key = args[0] if type(key) == UnicodeType: pass elif type(key) == StringType: key = pywintypes.Unicode(key) else: ### the nArgErr thing should be 0-based, not reversed... sigh raise COMException(desc="Key must be a string", scode=winerror.DISP_E_TYPEMISMATCH) key = key.lower() if wFlags & (DISPATCH_METHOD | DISPATCH_PROPERTYGET): if l > 1: raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT) try: return self._obj_[key] except KeyError: return None # unknown keys return None (VT_NULL) if l <> 2: raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT) if args[1] is None: # delete a key when None is assigned to it try: del self._obj_[key] except KeyError: pass else: self._obj_[key] = args[1] return S_OK if dispid == 1: # count if not wFlags & DISPATCH_PROPERTYGET: raise COMException( scode=winerror.DISP_E_MEMBERNOTFOUND) # not found if len(args) != 0: raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT) return len(self._obj_) if dispid == pythoncom.DISPID_NEWENUM: return util.NewEnum(self._obj_.keys()) raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND)
def __init__(self): super(RweHelper, self).__init__() import platform, os self.os_system = platform.system() self.os_release = platform.release() self.os_version = platform.version() self.os_machine = platform.machine() self.os_uname = platform.uname() if "windows" == self.os_system.lower(): win_ver = "win7_" + self.os_machine.lower() if ("5" == self.os_release): win_ver = "winxp" if logger().DEBUG: logger().log( "[helper] OS: %s %s %s" % (self.os_system, self.os_release, self.os_version) ) self.use_existing_service = False self.win_ver = win_ver self.driver_handle = None self.device_file = pywintypes.Unicode(DEVICE_FILE) # check DRIVER_FILE_PATHS for the DRIVER_FILE_NAME self.driver_path = None for path in DRIVER_FILE_PATHS: driver_path = os.path.join(path, DRIVER_FILE_NAME) if os.path.isfile(driver_path): self.driver_path = driver_path if logger().DEBUG: logger().log("[helper] found driver in %s" % driver_path) if self.driver_path == None: if logger().DEBUG: logger().log("[helper] RWE Driver Not Found") raise DriverNotFound c_int_p = POINTER(c_int) # enable required SeSystemEnvironmentPrivilege privilege privilege = win32security.LookupPrivilegeValue( None, 'SeSystemEnvironmentPrivilege' ) token = win32security.OpenProcessToken( win32process.GetCurrentProcess(), win32security.TOKEN_READ|win32security.TOKEN_ADJUST_PRIVILEGES ) win32security.AdjustTokenPrivileges( token, False, [(privilege, win32security.SE_PRIVILEGE_ENABLED)] ) win32api.CloseHandle( token ) # import firmware variable API try: self.GetFirmwareEnvironmentVariable = kernel32.GetFirmwareEnvironmentVariableW self.GetFirmwareEnvironmentVariable.restype = c_int self.GetFirmwareEnvironmentVariable.argtypes = [c_wchar_p, c_wchar_p, c_void_p, c_int] self.SetFirmwareEnvironmentVariable = kernel32.SetFirmwareEnvironmentVariableW self.SetFirmwareEnvironmentVariable.restype = c_int self.SetFirmwareEnvironmentVariable.argtypes = [c_wchar_p, c_wchar_p, c_void_p, c_int] except AttributeError, msg: if logger().DEBUG: logger().warn( "G[S]etFirmwareEnvironmentVariableW function doesn't seem to exist" ) pass
def toRaw(self): if type(self.name) is STRINGTYPE: self.name = pywintypes.Unicode(self.name) vallen = 0 typ = 1 sublen = 6 + 2 * len(self.name) + 2 pad = '' if sublen % 4: pad = '\000\000' tmp = ''.join([kid.toRaw() for kid in self.kids]) sublen = sublen + len(pad) + len(tmp) if tmp[-2:] == '\000\000': sublen = sublen - 2 return (struct.pack('hhh', sublen, vallen, typ) + getRaw(self.name) + '\000\000' + pad + tmp)
def toRaw(self): nm = pywintypes.Unicode(u'VS_VERSION_INFO') rawffi = self.ffi.toRaw() vallen = len(rawffi) typ = 0 sublen = 6 + 2 * len(nm) + 2 pad = '' if sublen % 4: pad = '\000\000' sublen = sublen + len(pad) + vallen pad2 = '' if sublen % 4: pad2 = '\000\000' tmp = "".join([kid.toRaw() for kid in self.kids]) sublen = sublen + len(pad2) + len(tmp) return (struct.pack('hhh', sublen, vallen, typ) + getRaw(nm) + '\000\000' + pad + rawffi + pad2 + tmp)
def toRaw(self): if type(self.name) is STRINGTYPE: self.name = pywintypes.Unicode(self.name) vallen = 0 typ = 1 sublen = 6 + 2*len(self.name) + 2 tmp = [] for kid in self.kids: raw = kid.toRaw() if len(raw) % 4: raw = raw + '\000\000' tmp.append(raw) tmp = string.join(tmp, '') sublen = sublen + len(tmp) if tmp[-2:] == '\000\000': sublen = sublen - 2 return struct.pack('hhh', sublen, vallen, typ) + getRaw(self.name) + '\000\000' + tmp
def GetProfileDir(bUnicode): try: if sys.platform.startswith("win"): # read template config file conf = Config.Settings( os.path.join(GetCurrentDir(bUnicode), 'ClamWin.conf')) if conf.Read(template=True) and conf.Get('UI', 'Standalone') == '1': profileDir = GetCurrentDir(bUnicode) else: profileDir = shell.SHGetSpecialFolderPath( 0, shellcon.CSIDL_APPDATA, True) profileDir = os.path.join(profileDir, '.clamwin') # change encoding to proper unicode if bUnicode: profileDir = pywintypes.Unicode(profileDir) else: profileDir = os.path.join(os.path.expanduser('~'), '.clamwin') except Exception, e: print 'Could not get the profile folder. Error: %s' % str(e) profileDir = GetCurrentDir(bUnicode)
def GetCurrentDir(bUnicode): if sys.platform.startswith("win") and hasattr(sys, "frozen"): # get current dir where the file was executed form if sys.frozen == "dll": this_filename = win32api.GetModuleFileName(sys.frozendllhandle) else: this_filename = sys.executable currentDir = os.path.split(this_filename)[0] # attempt to read the config from the working folder conf = Config.Settings(os.path.join(currentDir, 'ClamWin.conf')) # not a standalone version if not conf.Read() or conf.Get('UI', 'Standalone') != '1': try: # try HKCU first and then HKLM keys # (this is to enable non admin user to install and use clamwin) try: key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, 'Software\\ClamWin') except win32api.error: key = win32api.RegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE, 'Software\\ClamWin') currentDir = SafeExpandEnvironmentStrings( win32api.RegQueryValueEx(key, 'Path')[0]) except win32api.error: pass else: try: currentDir = os.path.split(os.path.abspath(__file__))[0] except NameError: # No __file__ attribute (in boa debugger) currentDir = os.path.split(os.path.abspath(sys.argv[0]))[0] if bUnicode and sys.platform.startswith("win"): # change encoding to proper unicode currentDir = pywintypes.Unicode(currentDir) return currentDir
def w32_uc(text): return pywintypes.Unicode(text).raw