I'm using VTK to show textactor with info 'ID' added to the actors. When my mouse is over an actor, the MouseEvent will call an 'add_text' function. The function was called and printed the correct 'ID' but there's no textactor adding to the window, is there any problem with my program?
class MouseInteractorHighLightActor(vtk.vtkInteractorStyleTrackballCamera):
def __init__(self, parent=None):
self.AddObserver("MouseMoveEvent", self.MouseMove)
self.LastPickedActor = None
self.LastPickedProperty = vtk.vtkProperty()
self.new = vtkTextActor()
self.old = vtkTextActor()
self.on_actor = False
def add_text(self,renderer,ID):
self.new.SetInput(ID)
txtprop = self.new.GetTextProperty()
txtprop.SetFontSize(36)
self.new.SetDisplayPosition(0,0)
# renderer.RemoveActor(self.old)
self.old = self.new
if self.on_actor:
renderer.AddActor(self.new)
print("add_text called , ID : ",ID)
else:
pass
# renderer.RemoveActor(self.old)
# renderer.RemoveActor(self.new)
def MouseMove(self,obj,event):
Mousepos = self.GetInteractor().GetEventPosition()
picker = vtk.vtkPropPicker()
picker.Pick(Mousepos[0], Mousepos[1], 0, self.GetDefaultRenderer())
self.NewPickedActor = picker.GetActor()
if self.NewPickedActor:
if not self.on_actor:
self.on_actor = True
print("on actor")
info = self.NewPickedActor.GetProperty().GetInformation()
info = str(info)
pattern_1 = re.compile(r"ID.*\d+")
pattern_2 = re.compile(r"\d+")
string = pattern_1.findall(info)[0]
ID = pattern_2.findall(string)[0]
# print("ID : ",ID)
self.add_text(self.GetDefaultRenderer() , ID)
if self.LastPickedActor:
self.LastPickedActor.GetProperty().DeepCopy(self.LastPickedProperty)
self.LastPickedProperty.DeepCopy(self.NewPickedActor.GetProperty())
self.NewPickedActor.GetProperty().SetDiffuse(1.0)
self.NewPickedActor.GetProperty().SetSpecular(0.0)
self.LastPickedActor = self.NewPickedActor
else:
pass
else:
print("not on actor")
self.on_actor = False
Update , the problem is solved, source code :
class MouseInteractorHighLightActor(vtk.vtkInteractorStyleTrackballCamera):
def __init__(self, parent=None):
self.AddObserver("LeftButtonPressEvent", self.leftButtonPressEvent)
self.AddObserver("LeftButtonReleaseEvent", self.leftButtonReleaseEvent)
self.AddObserver("MouseMoveEvent", self.MouseMove)
self.LastPickedActor = None
self.LastPickedProperty = vtk.vtkProperty()
self.new = vtkTextActor()
self.old = vtkTextActor()
self.on_actor = False
self.picker = vtk.vtkPropPicker()
def add_text(self,renderer,ID):
self.new.SetInput(ID)
txtprop = self.new.GetTextProperty()
txtprop.SetFontSize(72)
self.new.SetDisplayPosition(0,0)
self.old = self.new
if self.on_actor:
renderer.AddActor(self.new)
# print("ID : ",ID)
def MouseMove(self,obj,event):
Mousepos = self.GetInteractor().GetEventPosition()
picker = vtk.vtkPropPicker()
picker.Pick(Mousepos[0], Mousepos[1], 0, self.GetDefaultRenderer())
self.NewPickedActor = picker.GetActor()
if self.NewPickedActor:
self.on_actor = True
# print("on actor")
info = self.NewPickedActor.GetProperty().GetInformation()
info = str(info)
pattern_1 = re.compile(r"ID.*\d+")
pattern_2 = re.compile(r"\d+")
string = pattern_1.findall(info)[0]
ID = pattern_2.findall(string)[0]
# print("ID : ",ID)
self.add_text(self.GetDefaultRenderer() , ID)
if self.LastPickedActor:
self.LastPickedActor.GetProperty().DeepCopy(self.LastPickedProperty)
self.LastPickedProperty.DeepCopy(self.NewPickedActor.GetProperty())
self.NewPickedActor.GetProperty().SetDiffuse(1.0)
self.NewPickedActor.GetProperty().SetSpecular(0.0)
self.LastPickedActor = self.NewPickedActor
self.OnLeftButtonDown()
self.OnLeftButtonUp()
else:
if self.LastPickedActor:
self.LastPickedActor.GetProperty().DeepCopy(self.LastPickedProperty)
self.on_actor = False
self.GetDefaultRenderer().RemoveActor(self.new)
self.GetDefaultRenderer().RemoveActor(self.old)
self.OnLeftButtonDown()
self.OnLeftButtonUp()
def leftButtonPressEvent(self, obj, event):
self.RemoveObservers("MouseMoveEvent")
self.OnLeftButtonDown()
def leftButtonReleaseEvent(self, obj, event):
self.AddObserver("MouseMoveEvent",self.MouseMove)
self.OnLeftButtonUp()
I have a situation here. Using Python, I have a process been created (running as SYSTEM or Administrator user) and I need to call an application using a different username without password. So reading this link bellow, the conclusion is that could be possible, Impersonating another user without password, using Kerberos... but I am not finding any solution written in Python and actually I have no idea if there is a better approach for this problem.
https://blogs.msdn.microsoft.com/winsdk/2015/08/28/logon-as-a-user-without-a-password/
Python 3.4.1 x64, Windows 7.
Anyone can help me on this?
Thanks very much!
PS: This could be possible using "win32" modules, however I do not have any process running for this target_user to catch a process token.
Here's a rough draft of a ctypes wrapper to call LsaLogonUser for an MsV1 interactive logon, MsV1 S4U logon (service for user, batch), or Kerberos S4U domain logon.
import os
import ctypes
import collections
from ctypes import wintypes
ntdll = ctypes.WinDLL('ntdll')
secur32 = ctypes.WinDLL('secur32')
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
advapi32 = ctypes.WinDLL('advapi32', use_last_error=True)
MAX_COMPUTER_NAME_LENGTH = 15
SECURITY_LOGON_TYPE = wintypes.ULONG
Interactive = 2
Network = 3
Batch = 4
Service = 5
LOGON_SUBMIT_TYPE = wintypes.ULONG
PROFILE_BUFFER_TYPE = wintypes.ULONG
MsV1_0InteractiveLogon = 2
MsV1_0Lm20Logon = 3
MsV1_0NetworkLogon = 4
MsV1_0WorkstationUnlockLogon = 7
MsV1_0S4ULogon = 12
MsV1_0NoElevationLogon = 82
KerbInteractiveLogon = 2
KerbWorkstationUnlockLogon = 7
KerbS4ULogon = 12
MSV1_0_S4U_LOGON_FLAG_CHECK_LOGONHOURS = 0x2
KERB_S4U_LOGON_FLAG_CHECK_LOGONHOURS = 0x2
KERB_S4U_LOGON_FLAG_IDENTITY = 0x8
TOKEN_SOURCE_LENGTH = 8
NEGOTIATE_PACKAGE_NAME = b'Negotiate'
MICROSOFT_KERBEROS_NAME = b'Kerberos'
MSV1_0_PACKAGE_NAME = b'MICROSOFT_AUTHENTICATION_PACKAGE_V1_0'
DELETE = 0x00010000
READ_CONTROL = 0x00020000
WRITE_DAC = 0x00040000
WRITE_OWNER = 0x00080000
STANDARD_RIGHTS_REQUIRED = (DELETE |
READ_CONTROL |
WRITE_DAC |
WRITE_OWNER)
TOKEN_ASSIGN_PRIMARY = 0x0001
TOKEN_DUPLICATE = 0x0002
TOKEN_IMPERSONATE = 0x0004
TOKEN_QUERY = 0x0008
TOKEN_QUERY_SOURCE = 0x0010
TOKEN_ADJUST_PRIVILEGES = 0x0020
TOKEN_ADJUST_GROUPS = 0x0040
TOKEN_ADJUST_DEFAULT = 0x0080
TOKEN_ADJUST_SESSIONID = 0x0100
TOKEN_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED |
TOKEN_ASSIGN_PRIMARY |
TOKEN_DUPLICATE |
TOKEN_IMPERSONATE |
TOKEN_QUERY |
TOKEN_QUERY_SOURCE |
TOKEN_ADJUST_PRIVILEGES |
TOKEN_ADJUST_GROUPS |
TOKEN_ADJUST_DEFAULT |
TOKEN_ADJUST_SESSIONID)
DUPLICATE_CLOSE_SOURCE = 0x00000001
DUPLICATE_SAME_ACCESS = 0x00000002
TOKEN_TYPE = wintypes.ULONG
TokenPrimary = 1
TokenImpersonation = 2
SECURITY_IMPERSONATION_LEVEL = wintypes.ULONG
SecurityAnonymous = 0
SecurityIdentification = 1
SecurityImpersonation = 2
SecurityDelegation = 3
class NTSTATUS(wintypes.LONG):
def to_error(self):
return ntdll.RtlNtStatusToDosError(self)
def __repr__(self):
name = self.__class__.__name__
status = wintypes.ULONG.from_buffer(self)
return '%s(%#010x)' % (name, status.value)
PNTSTATUS = ctypes.POINTER(NTSTATUS)
class BOOL(wintypes.BOOL):
def __repr__(self):
name = self.__class__.__name__
return '%s(%s)' % (name, bool(self))
class HANDLE(wintypes.HANDLE):
__slots__ = 'closed',
def __int__(self):
return self.value or 0
def Detach(self):
if not getattr(self, 'closed', False):
self.closed = True
value = int(self)
self.value = None
return value
raise ValueError("already closed")
def Close(self, CloseHandle=kernel32.CloseHandle):
if self and not getattr(self, 'closed', False):
CloseHandle(self.Detach())
__del__ = Close
def __repr__(self):
return "%s(%d)" % (self.__class__.__name__, int(self))
class LARGE_INTEGER(wintypes.LARGE_INTEGER):
# https://msdn.microsoft.com/en-us/library/ff553204
ntdll.RtlSecondsSince1970ToTime.restype = None
_unix_epoch = wintypes.LARGE_INTEGER()
ntdll.RtlSecondsSince1970ToTime(0, ctypes.byref(_unix_epoch))
_unix_epoch = _unix_epoch.value
def __int__(self):
return self.value
def __repr__(self):
name = self.__class__.__name__
return '%s(%d)' % (name, self.value)
def as_time(self):
time100ns = self.value - self._unix_epoch
if time100ns >= 0:
return time100ns / 1e7
raise ValueError('value predates the Unix epoch')
#classmethod
def from_time(cls, t):
time100ns = int(t * 10**7)
return cls(time100ns + cls._unix_epoch)
CHAR = ctypes.c_char
WCHAR = ctypes.c_wchar
PCHAR = ctypes.POINTER(CHAR)
PWCHAR = ctypes.POINTER(WCHAR)
class STRING(ctypes.Structure):
_fields_ = (('Length', wintypes.USHORT),
('MaximumLength', wintypes.USHORT),
('Buffer', PCHAR))
PSTRING = ctypes.POINTER(STRING)
class UNICODE_STRING(ctypes.Structure):
_fields_ = (('Length', wintypes.USHORT),
('MaximumLength', wintypes.USHORT),
('Buffer', PWCHAR))
PUNICODE_STRING = ctypes.POINTER(UNICODE_STRING)
class LUID(ctypes.Structure):
_fields_ = (('LowPart', wintypes.DWORD),
('HighPart', wintypes.LONG))
def __new__(cls, value=0):
return cls.from_buffer_copy(ctypes.c_ulonglong(value))
def __int__(self):
return ctypes.c_ulonglong.from_buffer(self).value
def __repr__(self):
name = self.__class__.__name__
return '%s(%#x)' % (name, int(self))
PLUID = ctypes.POINTER(LUID)
PSID = wintypes.LPVOID
class SID_AND_ATTRIBUTES(ctypes.Structure):
_fields_ = (('Sid', PSID),
('Attributes', wintypes.DWORD))
PSID_AND_ATTRIBUTES = ctypes.POINTER(SID_AND_ATTRIBUTES)
class TOKEN_GROUPS(ctypes.Structure):
_fields_ = (('GroupCount', wintypes.DWORD),
('Groups', SID_AND_ATTRIBUTES * 1))
PTOKEN_GROUPS = ctypes.POINTER(TOKEN_GROUPS)
class TOKEN_SOURCE(ctypes.Structure):
_fields_ = (('SourceName', CHAR * TOKEN_SOURCE_LENGTH),
('SourceIdentifier', LUID))
def __init__(self, SourceName=None, SourceIdentifier=None):
if SourceName is not None:
if not isinstance(SourceName, bytes):
SourceName = SourceName.encode('mbcs')
self.SourceName = SourceName
if SourceIdentifier is None:
luid = self.SourceIdentifier
ntdll.NtAllocateLocallyUniqueId(ctypes.byref(luid))
else:
self.SourceIdentifier = SourceIdentifier
PTOKEN_SOURCE = ctypes.POINTER(TOKEN_SOURCE)
py_source_context = TOKEN_SOURCE(b"PYTHON ")
py_origin_name = b"Python-%d" % os.getpid()
py_logon_process_name = b"PythonLogonProcess-%d" % os.getpid()
SIZE_T = ctypes.c_size_t
class QUOTA_LIMITS(ctypes.Structure):
_fields_ = (('PagedPoolLimit', SIZE_T),
('NonPagedPoolLimit', SIZE_T),
('MinimumWorkingSetSize', SIZE_T),
('MaximumWorkingSetSize', SIZE_T),
('PagefileLimit', SIZE_T),
('TimeLimit', wintypes.LARGE_INTEGER))
PQUOTA_LIMITS = ctypes.POINTER(QUOTA_LIMITS)
PULONG = ctypes.POINTER(wintypes.ULONG)
LSA_OPERATIONAL_MODE = wintypes.ULONG
PLSA_OPERATIONAL_MODE = PULONG
PHANDLE = ctypes.POINTER(wintypes.HANDLE)
PLPVOID = ctypes.POINTER(wintypes.LPVOID)
LPDWORD = ctypes.POINTER(wintypes.DWORD)
class ContiguousUnicode(ctypes.Structure):
# _string_names_: sequence matched to underscore-prefixed fields
def _get_unicode_string(self, name):
wchar_size = ctypes.sizeof(WCHAR)
s = getattr(self, '_%s' % name)
length = s.Length // wchar_size
buf = s.Buffer
if buf:
return buf[:length]
return None
def _set_unicode_buffer(self, value):
cls = type(self)
wchar_size = ctypes.sizeof(WCHAR)
bufsize = (len(value) + 1) * wchar_size
ctypes.resize(self, ctypes.sizeof(cls) + bufsize)
addr = ctypes.addressof(self) + ctypes.sizeof(cls)
ctypes.memmove(addr, value, bufsize)
def _set_unicode_string(self, name, value):
values = []
for n in self._string_names_:
if n == name:
values.append(value or u'')
else:
values.append(getattr(self, n) or u'')
self._set_unicode_buffer(u'\x00'.join(values))
cls = type(self)
wchar_size = ctypes.sizeof(WCHAR)
addr = ctypes.addressof(self) + ctypes.sizeof(cls)
for n, v in zip(self._string_names_, values):
ptr = ctypes.cast(addr, PWCHAR)
ustr = getattr(self, '_%s' % n)
length = ustr.Length = len(v) * wchar_size
full_length = length + wchar_size
if ((n == name and value is None) or
(n != name and not (length or ustr.Buffer))):
ustr.Buffer = None
ustr.MaximumLength = 0
else:
ustr.Buffer = ptr
ustr.MaximumLength = full_length
addr += full_length
def __getattr__(self, name):
if name not in self._string_names_:
raise AttributeError
return self._get_unicode_string(name)
def __setattr__(self, name, value):
if name in self._string_names_:
self._set_unicode_string(name, value)
else:
super(ContiguousUnicode, self).__setattr__(name, value)
#classmethod
def from_address_copy(cls, address, size=None):
x = ctypes.Structure.__new__(cls)
if size is not None:
ctypes.resize(x, size)
ctypes.memmove(ctypes.byref(x), address, ctypes.sizeof(x))
delta = ctypes.addressof(x) - address
for n in cls._string_names_:
ustr = getattr(x, '_%s' % n)
addr = ctypes.c_void_p.from_buffer(ustr.Buffer)
if addr:
addr.value += delta
return x
class AuthInfo(ContiguousUnicode):
# _message_type_: from a logon-submit-type enumeration
def __init__(self):
self.MessageType = self._message_type_
class MSV1_0_INTERACTIVE_LOGON(AuthInfo):
_message_type_ = MsV1_0InteractiveLogon
_string_names_ = 'LogonDomainName', 'UserName', 'Password'
_fields_ = (('MessageType', LOGON_SUBMIT_TYPE),
('_LogonDomainName', UNICODE_STRING),
('_UserName', UNICODE_STRING),
('_Password', UNICODE_STRING))
def __init__(self, UserName=None, Password=None, LogonDomainName=None):
super(MSV1_0_INTERACTIVE_LOGON, self).__init__()
if LogonDomainName is not None:
self.LogonDomainName = LogonDomainName
if UserName is not None:
self.UserName = UserName
if Password is not None:
self.Password = Password
class S4ULogon(AuthInfo):
_string_names_ = 'UserPrincipalName', 'DomainName'
_fields_ = (('MessageType', LOGON_SUBMIT_TYPE),
('Flags', wintypes.ULONG),
('_UserPrincipalName', UNICODE_STRING),
('_DomainName', UNICODE_STRING))
def __init__(self, UserPrincipalName=None, DomainName=None, Flags=0):
super(S4ULogon, self).__init__()
self.Flags = Flags
if UserPrincipalName is not None:
self.UserPrincipalName = UserPrincipalName
if DomainName is not None:
self.DomainName = DomainName
class MSV1_0_S4U_LOGON(S4ULogon):
_message_type_ = MsV1_0S4ULogon
class KERB_S4U_LOGON(S4ULogon):
_message_type_ = KerbS4ULogon
PMSV1_0_S4U_LOGON = ctypes.POINTER(MSV1_0_S4U_LOGON)
PKERB_S4U_LOGON = ctypes.POINTER(KERB_S4U_LOGON)
class ProfileBuffer(ContiguousUnicode):
# _message_type_
def __init__(self):
self.MessageType = self._message_type_
class MSV1_0_INTERACTIVE_PROFILE(ProfileBuffer):
_message_type_ = MsV1_0InteractiveLogon
_string_names_ = ('LogonScript', 'HomeDirectory', 'FullName',
'ProfilePath', 'HomeDirectoryDrive', 'LogonServer')
_fields_ = (('MessageType', PROFILE_BUFFER_TYPE),
('LogonCount', wintypes.USHORT),
('BadPasswordCount', wintypes.USHORT),
('LogonTime', LARGE_INTEGER),
('LogoffTime', LARGE_INTEGER),
('KickOffTime', LARGE_INTEGER),
('PasswordLastSet', LARGE_INTEGER),
('PasswordCanChange', LARGE_INTEGER),
('PasswordMustChange', LARGE_INTEGER),
('_LogonScript', UNICODE_STRING),
('_HomeDirectory', UNICODE_STRING),
('_FullName', UNICODE_STRING),
('_ProfilePath', UNICODE_STRING),
('_HomeDirectoryDrive', UNICODE_STRING),
('_LogonServer', UNICODE_STRING),
('UserFlags', wintypes.ULONG))
class SECURITY_ATTRIBUTES(ctypes.Structure):
_fields_ = (('nLength', wintypes.DWORD),
('lpSecurityDescriptor', wintypes.LPVOID),
('bInheritHandle', wintypes.BOOL))
def __init__(self, **kwds):
self.nLength = ctypes.sizeof(self)
super(SECURITY_ATTRIBUTES, self).__init__(**kwds)
LPSECURITY_ATTRIBUTES = ctypes.POINTER(SECURITY_ATTRIBUTES)
def _check_status(result, func, args):
if result.value < 0:
raise ctypes.WinError(result.to_error())
return args
def _check_bool(result, func, args):
if not result:
raise ctypes.WinError(ctypes.get_last_error())
return args
def WIN(func, restype, *argtypes):
func.restype = restype
func.argtypes = argtypes
if issubclass(restype, NTSTATUS):
func.errcheck = _check_status
elif issubclass(restype, BOOL):
func.errcheck = _check_bool
# https://msdn.microsoft.com/en-us/library/ms683179
WIN(kernel32.GetCurrentProcess, wintypes.HANDLE)
# https://msdn.microsoft.com/en-us/library/ms724251
WIN(kernel32.DuplicateHandle, BOOL,
wintypes.HANDLE, # _In_ hSourceProcessHandle
wintypes.HANDLE, # _In_ hSourceHandle
wintypes.HANDLE, # _In_ hTargetProcessHandle
PHANDLE, # _Out_ lpTargetHandle
wintypes.DWORD, # _In_ dwDesiredAccess
wintypes.BOOL, # _In_ bInheritHandle
wintypes.DWORD) # _In_ dwOptions
# https://msdn.microsoft.com/en-us/library/ms724295
WIN(kernel32.GetComputerNameW, BOOL,
wintypes.LPWSTR, # _Out_ lpBuffer
LPDWORD) # _Inout_ lpnSize
# https://msdn.microsoft.com/en-us/library/aa379295
WIN(advapi32.OpenProcessToken, BOOL,
wintypes.HANDLE, # _In_ ProcessHandle
wintypes.DWORD, # _In_ DesiredAccess
PHANDLE) # _Out_ TokenHandle
# https://msdn.microsoft.com/en-us/library/aa446617
WIN(advapi32.DuplicateTokenEx, BOOL,
wintypes.HANDLE, # _In_ hExistingToken
wintypes.DWORD, # _In_ dwDesiredAccess
LPSECURITY_ATTRIBUTES, # _In_opt_ lpTokenAttributes
SECURITY_IMPERSONATION_LEVEL, # _In_ ImpersonationLevel
TOKEN_TYPE, # _In_ TokenType
PHANDLE) # _Out_ phNewToken
# https://msdn.microsoft.com/en-us/library/ff566415
WIN(ntdll.NtAllocateLocallyUniqueId, NTSTATUS,
PLUID) # _Out_ LUID
# https://msdn.microsoft.com/en-us/library/aa378279
WIN(secur32.LsaFreeReturnBuffer, NTSTATUS,
wintypes.LPVOID,) # _In_ Buffer
# https://msdn.microsoft.com/en-us/library/aa378265
WIN(secur32.LsaConnectUntrusted, NTSTATUS,
PHANDLE,) # _Out_ LsaHandle
#https://msdn.microsoft.com/en-us/library/aa378318
WIN(secur32.LsaRegisterLogonProcess, NTSTATUS,
PSTRING, # _In_ LogonProcessName
PHANDLE, # _Out_ LsaHandle
PLSA_OPERATIONAL_MODE) # _Out_ SecurityMode
# https://msdn.microsoft.com/en-us/library/aa378269
WIN(secur32.LsaDeregisterLogonProcess, NTSTATUS,
wintypes.HANDLE) # _In_ LsaHandle
# https://msdn.microsoft.com/en-us/library/aa378297
WIN(secur32.LsaLookupAuthenticationPackage, NTSTATUS,
wintypes.HANDLE, # _In_ LsaHandle
PSTRING, # _In_ PackageName
PULONG) # _Out_ AuthenticationPackage
# https://msdn.microsoft.com/en-us/library/aa378292
WIN(secur32.LsaLogonUser, NTSTATUS,
wintypes.HANDLE, # _In_ LsaHandle
PSTRING, # _In_ OriginName
SECURITY_LOGON_TYPE, # _In_ LogonType
wintypes.ULONG, # _In_ AuthenticationPackage
wintypes.LPVOID, # _In_ AuthenticationInformation
wintypes.ULONG, # _In_ AuthenticationInformationLength
PTOKEN_GROUPS, # _In_opt_ LocalGroups
PTOKEN_SOURCE, # _In_ SourceContext
PLPVOID, # _Out_ ProfileBuffer
PULONG, # _Out_ ProfileBufferLength
PLUID, # _Out_ LogonId
PHANDLE, # _Out_ Token
PQUOTA_LIMITS, # _Out_ Quotas
PNTSTATUS) # _Out_ SubStatus
Helpers
def duplicate_token(source_token=None, access=TOKEN_ALL_ACCESS,
impersonation_level=SecurityImpersonation,
token_type=TokenPrimary, attributes=None):
close_source = False
if source_token is None:
close_source = True
source_token = HANDLE()
advapi32.OpenProcessToken(kernel32.GetCurrentProcess(),
TOKEN_ALL_ACCESS, ctypes.byref(source_token))
token = HANDLE()
try:
advapi32.DuplicateTokenEx(source_token, access, attributes,
impersonation_level, token_type, ctypes.byref(token))
finally:
if close_source:
source_token.Close()
return token
def lsa_connect_untrusted():
handle = wintypes.HANDLE()
secur32.LsaConnectUntrusted(ctypes.byref(handle))
return handle.value
def lsa_register_logon_process(logon_process_name):
if not isinstance(logon_process_name, bytes):
logon_process_name = logon_process_name.encode('mbcs')
logon_process_name = logon_process_name[:127]
buf = ctypes.create_string_buffer(logon_process_name, 128)
name = STRING(len(logon_process_name), len(buf), buf)
handle = wintypes.HANDLE()
mode = LSA_OPERATIONAL_MODE()
secur32.LsaRegisterLogonProcess(ctypes.byref(name),
ctypes.byref(handle), ctypes.byref(mode))
return handle.value
def lsa_lookup_authentication_package(lsa_handle, package_name):
if not isinstance(package_name, bytes):
package_name = package_name.encode('mbcs')
package_name = package_name[:127]
buf = ctypes.create_string_buffer(package_name)
name = STRING(len(package_name), len(buf), buf)
package = wintypes.ULONG()
secur32.LsaLookupAuthenticationPackage(lsa_handle, ctypes.byref(name),
ctypes.byref(package))
return package.value
API
# Low-level LSA logon
LOGONINFO = collections.namedtuple('LOGONINFO', ('Token', 'LogonId',
'Profile', 'Quotas'))
def lsa_logon_user(auth_info, local_groups=None, origin_name=py_origin_name,
source_context=None, auth_package=None, logon_type=None,
lsa_handle=None):
if local_groups is None:
plocal_groups = PTOKEN_GROUPS()
else:
plocal_groups = ctypes.byref(local_groups)
if source_context is None:
source_context = py_source_context
if not isinstance(origin_name, bytes):
origin_name = origin_name.encode('mbcs')
buf = ctypes.create_string_buffer(origin_name)
origin_name = STRING(len(origin_name), len(buf), buf)
if auth_package is None:
if isinstance(auth_info, MSV1_0_S4U_LOGON):
auth_package = NEGOTIATE_PACKAGE_NAME
elif isinstance(auth_info, KERB_S4U_LOGON):
auth_package = MICROSOFT_KERBEROS_NAME
else:
auth_package = MSV1_0_PACKAGE_NAME
if logon_type is None:
if isinstance(auth_info, S4ULogon):
logon_type = Batch
else:
logon_type = Interactive
profile_buffer = wintypes.LPVOID()
profile_buffer_length = wintypes.ULONG()
profile = None
logonid = LUID()
htoken = HANDLE()
quotas = QUOTA_LIMITS()
substatus = NTSTATUS()
deregister = False
if lsa_handle is None:
lsa_handle = lsa_connect_untrusted()
deregister = True
try:
if isinstance(auth_package, (str, bytes)):
auth_package = lsa_lookup_authentication_package(lsa_handle,
auth_package)
try:
secur32.LsaLogonUser(lsa_handle, ctypes.byref(origin_name),
logon_type, auth_package, ctypes.byref(auth_info),
ctypes.sizeof(auth_info), plocal_groups,
ctypes.byref(source_context), ctypes.byref(profile_buffer),
ctypes.byref(profile_buffer_length), ctypes.byref(logonid),
ctypes.byref(htoken), ctypes.byref(quotas),
ctypes.byref(substatus))
except WindowsError as e:
if substatus.value:
raise ctypes.WinError(substatus.to_error())
raise
finally:
if profile_buffer:
address = profile_buffer.value
buftype = PROFILE_BUFFER_TYPE.from_address(address).value
if buftype == MsV1_0InteractiveLogon:
profile = MSV1_0_INTERACTIVE_PROFILE.from_address_copy(
address, profile_buffer_length.value)
secur32.LsaFreeReturnBuffer(address)
finally:
if deregister:
secur32.LsaDeregisterLogonProcess(lsa_handle)
return LOGONINFO(htoken, logonid, profile, quotas)
# High-level LSA logons
def logon_msv1(name, password, domain=None, local_groups=None,
origin_name=py_origin_name, source_context=None):
return lsa_logon_user(MSV1_0_INTERACTIVE_LOGON(name, password, domain),
local_groups, origin_name, source_context)
def logon_msv1_s4u(name, local_groups=None, origin_name=py_origin_name,
source_context=None):
domain = ctypes.create_unicode_buffer(MAX_COMPUTER_NAME_LENGTH + 1)
length = wintypes.DWORD(len(domain))
kernel32.GetComputerNameW(domain, ctypes.byref(length))
return lsa_logon_user(MSV1_0_S4U_LOGON(name, domain.value),
local_groups, origin_name, source_context)
def logon_kerb_s4u(name, realm=None, local_groups=None,
origin_name=py_origin_name,
source_context=None,
logon_process_name=py_logon_process_name):
lsa_handle = lsa_register_logon_process(logon_process_name)
try:
return lsa_logon_user(KERB_S4U_LOGON(name, realm),
local_groups, origin_name, source_context,
lsa_handle=lsa_handle)
finally:
secur32.LsaDeregisterLogonProcess(lsa_handle)
I am very new to Python, however I am in need of using Python for my project, I have been learning on the go. There is a point I don't understand with the piece of code below:
result['data'] = pool.map(image_processor, im_info)
So, I figured out pool.map(somefunc,[list of params]) is akin to parfor of Matlab. What I don't understand is the part relating image_processor. I am assuming it should be a function, however it is not defined as one using def. The code is working perfectly fine and I tried to trace it with pdb and it seems to process the images as intended in batches, for caffe input. This might be a very trivial question, I just am new to Python. So what is the order of execution that image_processor just works and how can I make changes in this function (I will be passing multiple parameters, I just need to know where I can make those changes as the function is not defined using a def) Thx.
#Data layer for video. Change flow_frames and RGB_frames to be the path to the flow and RGB frames.
import sys
sys.path.append('../../python')
import caffe
import io
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import scipy.misc
import time
import pdb
import glob
import pickle as pkl
import random
import h5py
from multiprocessing import Pool
from threading import Thread
import skimage.io
import copy
flow_frames = 'flow_images/'
RGB_frames = 'frames/'
test_frames = 16
train_frames = 16
test_buffer = 3
train_buffer = 24
def processImageCrop(im_info, transformer, flow):
im_path = im_info[0]
im_crop = im_info[1]
im_reshape = im_info[2]
im_flip = im_info[3]
data_in = caffe.io.load_image(im_path)
if (data_in.shape[0] < im_reshape[0]) | (data_in.shape[1] < im_reshape[1]):
data_in = caffe.io.resize_image(data_in, im_reshape)
if im_flip:
data_in = caffe.io.flip_image(data_in, 1, flow)
data_in = data_in[im_crop[0]:im_crop[2], im_crop[1]:im_crop[3], :]
processed_image = transformer.preprocess('data_in',data_in)
return processed_image
class ImageProcessorCrop(object):
def __init__(self, transformer, flow):
self.transformer = transformer
self.flow = flow
def __call__(self, im_info):
return processImageCrop(im_info, self.transformer, self.flow)
class sequenceGeneratorVideo(object):
def __init__(self, buffer_size, clip_length, num_videos, video_dict, video_order):
self.buffer_size = buffer_size
self.clip_length = clip_length
self.N = self.buffer_size*self.clip_length
self.num_videos = num_videos
self.video_dict = video_dict
self.video_order = video_order
self.idx = 0
def __call__(self):
label_r = []
im_paths = []
im_crop = []
im_reshape = []
im_flip = []
if self.idx + self.buffer_size >= self.num_videos:
idx_list = range(self.idx, self.num_videos)
idx_list.extend(range(0, self.buffer_size-(self.num_videos-self.idx)))
else:
idx_list = range(self.idx, self.idx+self.buffer_size)
for i in idx_list:
key = self.video_order[i]
label = self.video_dict[key]['label']
video_reshape = self.video_dict[key]['reshape']
video_crop = self.video_dict[key]['crop']
label_r.extend([label]*self.clip_length)
im_reshape.extend([(video_reshape)]*self.clip_length)
r0 = int(random.random()*(video_reshape[0] - video_crop[0]))
r1 = int(random.random()*(video_reshape[1] - video_crop[1]))
im_crop.extend([(r0, r1, r0+video_crop[0], r1+video_crop[1])]*self.clip_length)
f = random.randint(0,1)
im_flip.extend([f]*self.clip_length)
rand_frame = int(random.random()*(self.video_dict[key]['num_frames']-self.clip_length)+1+1)
frames = []
for i in range(rand_frame,rand_frame+self.clip_length):
frames.append(self.video_dict[key]['frames'] %i)
im_paths.extend(frames)
im_info = zip(im_paths,im_crop, im_reshape, im_flip)
self.idx += self.buffer_size
if self.idx >= self.num_videos:
self.idx = self.idx - self.num_videos
return label_r, im_info
def advance_batch(result, sequence_generator, image_processor, pool):
label_r, im_info = sequence_generator()
#tmp = image_processor(im_info[0])
result['data'] = pool.map(image_processor, im_info)
result['label'] = label_r
cm = np.ones(len(label_r))
cm[0::16] = 0
result['clip_markers'] = cm
class BatchAdvancer():
def __init__(self, result, sequence_generator, image_processor, pool):
self.result = result
self.sequence_generator = sequence_generator
self.image_processor = image_processor
self.pool = pool
def __call__(self):
return advance_batch(self.result, self.sequence_generator, self.image_processor, self.pool)
class videoRead(caffe.Layer):
def initialize(self):
self.train_or_test = 'test'
self.flow = False
self.buffer_size = test_buffer #num videos processed per batch
self.frames = test_frames #length of processed clip
self.N = self.buffer_size*self.frames
self.idx = 0
self.channels = 3
self.height = 227
self.width = 227
self.path_to_images = RGB_frames
self.video_list = 'ucf101_split1_testVideos.txt'
def setup(self, bottom, top):
random.seed(10)
self.initialize()
f = open(self.video_list, 'r')
f_lines = f.readlines()
f.close()
video_dict = {}
current_line = 0
self.video_order = []
for ix, line in enumerate(f_lines):
video = line.split(' ')[0].split('/')[1]
l = int(line.split(' ')[1])
frames = glob.glob('%s%s/*.jpg' %(self.path_to_images, video))
num_frames = len(frames)
video_dict[video] = {}
video_dict[video]['frames'] = frames[0].split('.')[0] + '.%04d.jpg'
video_dict[video]['reshape'] = (240,320)
video_dict[video]['crop'] = (227, 227)
video_dict[video]['num_frames'] = num_frames
video_dict[video]['label'] = l
self.video_order.append(video)
self.video_dict = video_dict
self.num_videos = len(video_dict.keys())
#set up data transformer
shape = (self.N, self.channels, self.height, self.width)
self.transformer = caffe.io.Transformer({'data_in': shape})
self.transformer.set_raw_scale('data_in', 255)
if self.flow:
image_mean = [128, 128, 128]
self.transformer.set_is_flow('data_in', True)
else:
image_mean = [103.939, 116.779, 128.68]
self.transformer.set_is_flow('data_in', False)
channel_mean = np.zeros((3,227,227))
for channel_index, mean_val in enumerate(image_mean):
channel_mean[channel_index, ...] = mean_val
self.transformer.set_mean('data_in', channel_mean)
self.transformer.set_channel_swap('data_in', (2, 1, 0))
self.transformer.set_transpose('data_in', (2, 0, 1))
self.thread_result = {}
self.thread = None
pool_size = 24
self.image_processor = ImageProcessorCrop(self.transformer, self.flow)
self.sequence_generator = sequenceGeneratorVideo(self.buffer_size, self.frames, self.num_videos, self.video_dict, self.video_order)
self.pool = Pool(processes=pool_size)
self.batch_advancer = BatchAdvancer(self.thread_result, self.sequence_generator, self.image_processor, self.pool)
self.dispatch_worker()
self.top_names = ['data', 'label','clip_markers']
print 'Outputs:', self.top_names
if len(top) != len(self.top_names):
raise Exception('Incorrect number of outputs (expected %d, got %d)' %
(len(self.top_names), len(top)))
self.join_worker()
for top_index, name in enumerate(self.top_names):
if name == 'data':
shape = (self.N, self.channels, self.height, self.width)
elif name == 'label':
shape = (self.N,)
elif name == 'clip_markers':
shape = (self.N,)
top[top_index].reshape(*shape)
def reshape(self, bottom, top):
pass
def forward(self, bottom, top):
if self.thread is not None:
self.join_worker()
#rearrange the data: The LSTM takes inputs as [video0_frame0, video1_frame0,...] but the data is currently arranged as [video0_frame0, video0_frame1, ...]
new_result_data = [None]*len(self.thread_result['data'])
new_result_label = [None]*len(self.thread_result['label'])
new_result_cm = [None]*len(self.thread_result['clip_markers'])
for i in range(self.frames):
for ii in range(self.buffer_size):
old_idx = ii*self.frames + i
new_idx = i*self.buffer_size + ii
new_result_data[new_idx] = self.thread_result['data'][old_idx]
new_result_label[new_idx] = self.thread_result['label'][old_idx]
new_result_cm[new_idx] = self.thread_result['clip_markers'][old_idx]
for top_index, name in zip(range(len(top)), self.top_names):
if name == 'data':
for i in range(self.N):
top[top_index].data[i, ...] = new_result_data[i]
elif name == 'label':
top[top_index].data[...] = new_result_label
elif name == 'clip_markers':
top[top_index].data[...] = new_result_cm
self.dispatch_worker()
def dispatch_worker(self):
assert self.thread is None
self.thread = Thread(target=self.batch_advancer)
self.thread.start()
def join_worker(self):
assert self.thread is not None
self.thread.join()
self.thread = None
def backward(self, top, propagate_down, bottom):
pass
class videoReadTrain_flow(videoRead):
def initialize(self):
self.train_or_test = 'train'
self.flow = True
self.buffer_size = train_buffer #num videos processed per batch
self.frames = train_frames #length of processed clip
self.N = self.buffer_size*self.frames
self.idx = 0
self.channels = 3
self.height = 227
self.width = 227
self.path_to_images = flow_frames
self.video_list = 'ucf101_split1_trainVideos.txt'
class videoReadTest_flow(videoRead):
def initialize(self):
self.train_or_test = 'test'
self.flow = True
self.buffer_size = test_buffer #num videos processed per batch
self.frames = test_frames #length of processed clip
self.N = self.buffer_size*self.frames
self.idx = 0
self.channels = 3
self.height = 227
self.width = 227
self.path_to_images = flow_frames
self.video_list = 'ucf101_split1_testVideos.txt'
class videoReadTrain_RGB(videoRead):
def initialize(self):
self.train_or_test = 'train'
self.flow = False
self.buffer_size = train_buffer #num videos processed per batch
self.frames = train_frames #length of processed clip
self.N = self.buffer_size*self.frames
self.idx = 0
self.channels = 3
self.height = 227
self.width = 227
self.path_to_images = RGB_frames
self.video_list = 'ucf101_split1_trainVideos.txt'
class videoReadTest_RGB(videoRead):
def initialize(self):
self.train_or_test = 'test'
self.flow = False
self.buffer_size = test_buffer #num videos processed per batch
self.frames = test_frames #length of processed clip
self.N = self.buffer_size*self.frames
self.idx = 0
self.channels = 3
self.height = 227
self.width = 227
self.path_to_images = RGB_frames
self.video_list = 'ucf101_split1_testVideos.txt'
The code is from https://github.com/LisaAnne/lisa-caffe-public/blob/lstm_video_deploy/examples/LRCN_activity_recognition/sequence_input_layer.py
See this line:
self.image_processor = ImageProcessorCrop(self.transformer, self.flow)
The ImageProcessorCrop class has a __call__ method, which allows these objects to be used as if they're functions. When you call them, it calls that method.
Consider this program:
import win32file
src_file = win32file.CreateFile(
r'C:\test.rar',
win32file.GENERIC_READ,
win32file.FILE_SHARE_READ,
None,
win32file.OPEN_EXISTING,
win32file.FILE_FLAG_NO_BUFFERING,
None
)
dst_file = win32file.CreateFile(
r'D:\test.rar',
win32file.GENERIC_WRITE,
win32file.FILE_SHARE_WRITE,
None,
win32file.CREATE_ALWAYS,
win32file.FILE_FLAG_NO_BUFFERING,
None
)
while True:
rc, data = win32file.ReadFile(src_file, 4096)
if not data:
break
else:
win32file.WriteFile(dst_file, data)
src_file.close()
dst_file.close()
When writing the latest data file fails:
pywintypes.error: (87, 'WriteFile', 'The parameter is incorrect.')
This is the behavior expected with the FILE_FLAG_NO_BUFFERING flag, but I do not know how to solve it in python.
You can use ctypes. Note that FILE_FLAG_NO_BUFFERING requires writing in sector-size multiples, so unless you want the destination file to be larger than the source file, you probably want to use a different setting for writing. I used FILE_FLAG_WRITE_THROUGH:
import ctypes
import ctypes.wintypes
import sys
src_filename = sys.argv[0]
dst_filename = sys.argv[0] + ".test.backup"
CloseHandle = ctypes.windll.kernel32.CloseHandle
CreateFile = ctypes.windll.kernel32.CreateFileA
ReadFile = ctypes.windll.kernel32.ReadFile
WriteFile = ctypes.windll.kernel32.WriteFile
FILE_SHARE_READ = 1
FILE_SHARE_WRITE = 2
FILE_FLAG_NO_BUFFERING = 0x20000000
CREATE_ALWAYS = 2
OPEN_EXISTING = 3
OPEN_ALWAYS = 4
GENERIC_READ = 0x80000000
GENERIC_WRITE = 0x40000000
INVALID_HANDLE_VALUE = ctypes.wintypes.HANDLE(-1).value
INVALID_FILE_ATTRIBUTES = 0xFFFFFFFF
FILE_FLAG_WRITE_THROUGH = 0x80000000
hSrc = CreateFile(
src_filename,
GENERIC_READ,
FILE_SHARE_READ,
None,
OPEN_EXISTING,
FILE_FLAG_NO_BUFFERING,
None
)
hDest = CreateFile(
dst_filename,
GENERIC_WRITE,
FILE_SHARE_WRITE,
None,
OPEN_ALWAYS,
FILE_FLAG_WRITE_THROUGH,
None
)
def ctypes_alloc_aligned(size, alignment):
"""
http://stackoverflow.com/questions/8658813/control-memory-alignment-in-python-ctypes
"""
bufSize = size+(alignment-1)
raw_memory = bytearray(bufSize)
ctypes_raw_type = (ctypes.c_char * bufSize)
ctypes_raw_memory = ctypes_raw_type.from_buffer(raw_memory)
raw_address = ctypes.addressof(ctypes_raw_memory)
offset = raw_address % alignment
offset_to_aligned = (alignment - offset) % alignment
ctypes_aligned_type = (ctypes.c_char * (bufSize-offset_to_aligned))
ctypes_aligned_memory = ctypes_aligned_type.from_buffer(raw_memory, offset_to_aligned)
return ctypes_aligned_memory
sectorSize = 4096
bufSize = 256*sectorSize # 1MB
lpBuffer = ctypes_alloc_aligned(bufSize, sectorSize)
numBytes = ctypes.wintypes.DWORD()
while True:
bSuccess = ctypes.windll.kernel32.ReadFile(
hSrc, lpBuffer, bufSize, ctypes.byref(numBytes), None)
if not bSuccess:
print "ReadFile FAILED:", hSrc, numBytes
raise ctypes.WinError()
if numBytes.value == 0:
break
bSuccess = ctypes.windll.kernel32.WriteFile(
hDest, lpBuffer, numBytes, ctypes.byref(numBytes), None)
if not bSuccess:
print "WriteFile FAILED:", hDest, numBytes
raise ctypes.WinError()
CloseHandle(hSrc)
CloseHandle(hDest)