/** * Creates a new instance of the process. * * @param domain the account domain * @param username the account username * @param password the account password */ public WindowsProcess(final String domain, final String username, final String password) { this.domain = domain; this.user = username; this.password = password; this.inRead = new HANDLEByReference(); this.inWrite = new HANDLEByReference(); this.outRead = new HANDLEByReference(); this.outWrite = new HANDLEByReference(); this.errRead = new HANDLEByReference(); this.errWrite = new HANDLEByReference(); this.in_fd = new FileDescriptor(); this.out_fd = new FileDescriptor(); this.err_fd = new FileDescriptor(); this.pid = -1; }
/** * Safely closes a handle. * @param ref handle by reference */ private static void closeSafely(final HANDLEByReference ref) { final HANDLE handle = ref.getValue(); if (handle != null) { Kernel32.INSTANCE.CloseHandle(handle); } }
/** * The DuplicateTokenEx function creates a new access token that duplicates * an existing token. This function can create either a primary token or an * impersonation token. * * @param hExistingToken A handle to an access token opened with * TOKEN_DUPLICATE access. * @param dwDesiredAccess Specifies the requested access rights for the new * token. * @param lpTokenAttributes A pointer to a SECURITY_ATTRIBUTES structure * that specifies a security descriptor for the new token and determines * whether child processes can inherit the token. * @param ImpersonationLevel Specifies a value from the * SECURITY_IMPERSONATION_LEVEL enumeration that indicates the impersonation * level of the new token. * @param TokenType Specifies one of the following values from the * TOKEN_TYPE enumeration. * @param phNewToken A pointer to a HANDLE variable that receives the new * token. * @return If the function succeeds, the function returns a nonzero value. * If the function fails, it returns zero. To get extended error * information, call GetLastError. */ public boolean DuplicateTokenEx( HANDLE hExistingToken, int dwDesiredAccess, WinBase.SECURITY_ATTRIBUTES lpTokenAttributes, int ImpersonationLevel, int TokenType, HANDLEByReference phNewToken);
/** * The OpenThreadToken function opens the access token associated with a * thread. * * @param ThreadHandle * Handle to the thread whose access token is opened. * @param DesiredAccess * Specifies an access mask that specifies the requested types of * access to the access token. These requested access types are * reconciled against the token's discretionary access control * list (DACL) to determine which accesses are granted or denied. * @param OpenAsSelf * Indicates whether the access check is to be made against the * security context of the thread calling the OpenThreadToken * function or against the security context of the process for * the calling thread. * @param TokenHandle * Pointer to a variable that receives the handle to the newly * opened access token. * @return If the function succeeds, the return value is nonzero. If the * function fails, the return value is zero. To get extended error * information, call GetLastError. */ public boolean OpenThreadToken(HANDLE ThreadHandle, int DesiredAccess, boolean OpenAsSelf, HANDLEByReference TokenHandle);
/** * The OpenProcessToken function opens the access token associated with a * process. * * @param ProcessHandle * Handle to the process whose access token is opened. The * process must have the PROCESS_QUERY_INFORMATION access * permission. * @param DesiredAccess * Specifies an access mask that specifies the requested types of * access to the access token. These requested access types are * compared with the discretionary access control list (DACL) of * the token to determine which accesses are granted or denied. * @param TokenHandle * Pointer to a handle that identifies the newly opened access * token when the function returns. * @return If the function succeeds, the return value is nonzero. If the * function fails, the return value is zero. To get extended error * information, call GetLastError. */ public boolean OpenProcessToken(HANDLE ProcessHandle, int DesiredAccess, HANDLEByReference TokenHandle);
/** * The DuplicateToken function creates a new access token that duplicates * one already in existence. * * @param ExistingTokenHandle * Handle to an access token opened with TOKEN_DUPLICATE access. * @param ImpersonationLevel * Specifies a SECURITY_IMPERSONATION_LEVEL enumerated type that * supplies the impersonation level of the new token. * @param DuplicateTokenHandle * Pointer to a variable that receives a handle to the duplicate * token. This handle has TOKEN_IMPERSONATE and TOKEN_QUERY * access to the new token. * @return If the function succeeds, the return value is nonzero. If the * function fails, the return value is zero. To get extended error * information, call GetLastError. */ public boolean DuplicateToken(HANDLE ExistingTokenHandle, int ImpersonationLevel, HANDLEByReference DuplicateTokenHandle);
/** * The DuplicateTokenEx function creates a new access token that duplicates * an existing token. This function can create either a primary token or an * impersonation token. * * @param hExistingToken * A handle to an access token opened with TOKEN_DUPLICATE * access. * @param dwDesiredAccess * Specifies the requested access rights for the new token. * @param lpTokenAttributes * A pointer to a SECURITY_ATTRIBUTES structure that specifies a * security descriptor for the new token and determines whether * child processes can inherit the token. * @param ImpersonationLevel * Specifies a value from the SECURITY_IMPERSONATION_LEVEL * enumeration that indicates the impersonation level of the new * token. * @param TokenType * Specifies one of the following values from the TOKEN_TYPE * enumeration. * @param phNewToken * A pointer to a HANDLE variable that receives the new token. * @return If the function succeeds, the function returns a nonzero value. * If the function fails, it returns zero. To get extended error * information, call GetLastError. */ public boolean DuplicateTokenEx(HANDLE hExistingToken, int dwDesiredAccess, WinBase.SECURITY_ATTRIBUTES lpTokenAttributes, int ImpersonationLevel, int TokenType, HANDLEByReference phNewToken);
/** * The OpenThreadToken function opens the access token associated with a * thread. * * @param ThreadHandle Handle to the thread whose access token is opened. * @param DesiredAccess Specifies an access mask that specifies the * requested types of access to the access token. These requested access * types are reconciled against the token's discretionary access control * list (DACL) to determine which accesses are granted or denied. * @param OpenAsSelf Indicates whether the access check is to be made * against the security context of the thread calling the OpenThreadToken * function or against the security context of the process for the calling * thread. * @param TokenHandle Pointer to a variable that receives the handle to the * newly opened access token. * @return If the function succeeds, the return value is nonzero. If the * function fails, the return value is zero. To get extended error * information, call GetLastError. */ public boolean OpenThreadToken( HANDLE ThreadHandle, int DesiredAccess, boolean OpenAsSelf, HANDLEByReference TokenHandle);
/** * The OpenProcessToken function opens the access token associated with a * process. * * @param ProcessHandle Handle to the process whose access token is opened. * The process must have the PROCESS_QUERY_INFORMATION access permission. * @param DesiredAccess Specifies an access mask that specifies the * requested types of access to the access token. These requested access * types are compared with the discretionary access control list (DACL) of * the token to determine which accesses are granted or denied. * @param TokenHandle Pointer to a handle that identifies the newly opened * access token when the function returns. * @return If the function succeeds, the return value is nonzero. If the * function fails, the return value is zero. To get extended error * information, call GetLastError. */ public boolean OpenProcessToken( HANDLE ProcessHandle, int DesiredAccess, HANDLEByReference TokenHandle);
/** * The DuplicateToken function creates a new access token that duplicates * one already in existence. * * @param ExistingTokenHandle Handle to an access token opened with * TOKEN_DUPLICATE access. * @param ImpersonationLevel Specifies a SECURITY_IMPERSONATION_LEVEL * enumerated type that supplies the impersonation level of the new token. * @param DuplicateTokenHandle Pointer to a variable that receives a handle * to the duplicate token. This handle has TOKEN_IMPERSONATE and TOKEN_QUERY * access to the new token. * @return If the function succeeds, the return value is nonzero. If the * function fails, the return value is zero. To get extended error * information, call GetLastError. */ public boolean DuplicateToken( HANDLE ExistingTokenHandle, int ImpersonationLevel, HANDLEByReference DuplicateTokenHandle);