/* This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. *
* This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ package org.rzo.yajsw.os.ms.win.w32; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import jnacontrib.jna.Advapi32; import jnacontrib.jna.Options; import org.apache.commons.collections.MultiHashMap; import org.rzo.yajsw.io.CyclicBufferFileInputStream; import org.rzo.yajsw.io.CyclicBufferFilePrintStream; import org.rzo.yajsw.os.AbstractProcess; import org.rzo.yajsw.os.Process; import org.rzo.yajsw.os.ms.win.w32.WindowsXPProcess.MyAdvapi.TOKEN_PRIVILEGES; import org.rzo.yajsw.os.ms.win.w32.WindowsXPProcess.MyKernel32.MEMORY_BASIC_INFORMATION; import org.rzo.yajsw.os.ms.win.w32.WindowsXPProcess.MyKernel32.PROCESSENTRY32; import org.rzo.yajsw.os.ms.win.w32.WindowsXPProcess.MyKernel32.PROCESS_INFORMATION; import org.rzo.yajsw.os.ms.win.w32.WindowsXPProcess.MyKernel32.SECURITY_ATTRIBUTES; import org.rzo.yajsw.os.ms.win.w32.WindowsXPProcess.MyKernel32.STARTUPINFO; import org.rzo.yajsw.os.ms.win.w32.WindowsXPProcess.Ntdll.PEB; import org.rzo.yajsw.os.ms.win.w32.WindowsXPProcess.Ntdll.PEB64; import org.rzo.yajsw.os.ms.win.w32.WindowsXPProcess.Ntdll.PROCESS_BASIC_INFORMATION; import org.rzo.yajsw.os.ms.win.w32.WindowsXPProcess.Ntdll.RTL_USER_PROCESS_PARAMETERS; import org.rzo.yajsw.os.ms.win.w32.WindowsXPProcess.Shell32.SHELLEXECUTEINFO; import com.sun.jna.Memory; import com.sun.jna.Native; import com.sun.jna.NativeLong; import com.sun.jna.PlatformEx; import com.sun.jna.Pointer; import com.sun.jna.StringBlock; import com.sun.jna.Structure; import com.sun.jna.WString; import com.sun.jna.platform.win32.Kernel32Util; import com.sun.jna.platform.win32.User32; import com.sun.jna.platform.win32.WinDef.HWND; import com.sun.jna.platform.win32.WinNT.HANDLE; import com.sun.jna.platform.win32.WinUser.WINDOWINFO; import com.sun.jna.ptr.IntByReference; import com.sun.jna.ptr.LongByReference; import com.sun.jna.ptr.NativeLongByReference; import com.sun.jna.ptr.PointerByReference; import com.sun.jna.win32.StdCallLibrary; // TODO: Auto-generated Javadoc /** * The Class WindowsXPProcess. */ public class WindowsXPProcess extends AbstractProcess { public interface Shell32 extends com.sun.jna.win32.StdCallLibrary { // Method declarations, constant and structure definitions go here /** The INSTANCE. */ Shell32 INSTANCE = (Shell32) Native.loadLibrary("Shell32", Shell32.class); /* * BOOL ShellExecuteEx( __inout LPSHELLEXECUTEINFO lpExecInfo ); */ boolean ShellExecuteEx( SHELLEXECUTEINFO lpExecInfo ); /* * typedef struct _SHELLEXECUTEINFO { DWORD cbSize; ULONG fMask; HWND hwnd; LPCTSTR lpVerb; LPCTSTR lpFile; LPCTSTR lpParameters; LPCTSTR lpDirectory; int nShow; HINSTANCE hInstApp; LPVOID lpIDList; LPCTSTR lpClass; HKEY hkeyClass; DWORD dwHotKey; union { HANDLE hIcon; HANDLE hMonitor; } DUMMYUNIONNAME; HANDLE hProcess; } SHELLEXECUTEINFO, *LPSHELLEXECUTEINFO; */ public static class SHELLEXECUTEINFO extends Structure { public int cbSize; public int fMask; public Pointer hwnd; public String lpVerb; public String lpFile; public String lpParameters; public String lpDirectory; public int nShow; public IntByReference hInstApp; public Pointer lpIDList; public String lpClass; public Pointer hkeyClass; public int dwHotKey; public Pointer hMonitor; public HANDLE hProcess; } public static final int SEE_MASK_DEFAULT = 0x00000000; public static final int SEE_MASK_CLASSNAME = 0x00000001; public static final int SEE_MASK_CLASSKEY = 0x00000003; public static final int SEE_MASK_IDLIST = 0x00000004; public static final int SEE_MASK_INVOKEIDLIST = 0x0000000C; public static final int SEE_MASK_ICON = 0x00000010; public static final int SEE_MASK_HOTKEY = 0x00000020; public static final int SEE_MASK_NOCLOSEPROCESS = 0x00000040; public static final int SEE_MASK_CONNECTNETDRV = 0x00000080; public static final int SEE_MASK_NOASYNC = 0x00000100; public static final int SEE_MASK_FLAG_DDEWAIT = 0x00000100; public static final int SEE_MASK_DOENVSUBST = 0x00000200; public static final int SEE_MASK_FLAG_NO_UI = 0x00000400; public static final int SEE_MASK_UNICODE = 0x00004000; public static final int SEE_MASK_NO_CONSOLE = 0x00008000; public static final int SEE_MASK_ASYNCOK = 0x00100000; public static final int SEE_MASK_NOQUERYCLASSSTORE = 0x01000000; public static final int SEE_MASK_HMONITOR = 0x00200000; public static final int SEE_MASK_NOZONECHECKS = 0x00800000; public static final int SEE_MASK_WAITFORINPUTIDLE = 0x02000000; public static final int SEE_MASK_FLAG_LOG_USAGE = 0x04000000; public static final String VERB_EDIT = "edit"; public static final String VERB_EXPLORE = "explore"; public static final String VERB_FIND = "find"; public static final String VERB_OPEN = "open"; public static final String VERB_PRINT = "print"; public static final String VERB_PROPERTIES = "properties"; public static final String VERB_RUNAS = "runas"; public static final int SW_HIDE = 0; public static final int SW_MAXIMIZE = 3; public static final int SW_MINIMIZE = 6; public static final int SW_RESTORE = 9; public static final int SW_SHOW = 5; public static final int SW_SHOWDEFAULT = 10; public static final int SW_SHOWMAXIMIZED = 3; public static final int SW_SHOWMINIMIZED = 2; public static final int SW_SHOWMINNOACTIVE = 7; public static final int SW_SHOWNA = 8; public static final int SW_SHOWNOACTIVATE = 4; public static final int SW_SHOWNORMAL = 1; } /** * The Interface MyUser32. */ public interface MyUser32 extends User32 { // Method declarations, constant and structure definitions go here /** The INSTANCE. */ MyUser32 INSTANCE = (MyUser32) Native.loadLibrary("User32", MyUser32.class); /* * HWND GetForegroundWindow(VOID); */ /** * Gets the foreground window. * * @return the pointer */ HWND GetForegroundWindow(); /* * DWORD GetWindowThreadProcessId( HWND hWnd, LPDWORD lpdwProcessId ); */ /** * Gets the window thread process id. * * @param hWnd * the h wnd * @param lpdwProcessId * the lpdw process id * * @return the int */ int GetWindowThreadProcessId(Pointer hWnd, IntByReference lpdwProcessId); /** The W m_ close. */ int WM_CLOSE = 16; int WM_QUIT = 18; int WM_DESTROY = 2; int WM_KEYDOWN = 256; int WM_KEYUP = 257; int WM_CHAR = 258; int WM_SETFOCUS = 7; int VK_CONTROL = 17; int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount); /* * BOOL WINAPI GetWindowInfo( __in HWND hwnd, __inout PWINDOWINFO pwi ); */ boolean GetWindowInfo( HWND hwnd, WINDOWINFO pwi ); /* * BOOL PostThreadMessage( DWORD idThread, UINT Msg, WPARAM wParam, * LPARAM lParam ); */ /** * Post thread message a. * * @param idThread * the id thread * @param Msg * the msg * @param wParam * the w param * @param lParam * the l param * * @return true, if successful */ boolean PostThreadMessageA(int idThread, int Msg, int wParam, int lParam); /* * DWORD WINAPI WaitForInputIdle( __in HANDLE hProcess, __in DWORD * dwMilliseconds ); */ /** * Wait for input idle. * * @param hProcess * the h process * @param dwMilliseconds * the dw milliseconds * * @return the int */ int WaitForInputIdle(HANDLE hProcess, int dwMilliseconds); void PostMessageA(HWND hWnd, int msg, Pointer wParam, Pointer lParam); void SendMessageW(HWND hWnd, int msg, long wParam, int lParam); public interface WNDENUMPROC extends StdCallCallback { /** Return whether to continue enumeration. */ boolean callback(HWND hWnd, int data); } boolean EnumWindows(WNDENUMPROC lpEnumFunc, int data); } /** * The Interface MyKernel32. */ public interface MyKernel32 extends com.sun.jna.platform.win32.Kernel32 { // Method declarations, constant and structure definitions go here /** The INSTANCE. */ MyKernel32 INSTANCE = (MyKernel32) Native.loadLibrary("kernel32", MyKernel32.class); /* * BOOL WINAPI ReadFile( __in HANDLE hFile, __out LPVOID lpBuffer, __in * DWORD nNumberOfBytesToRead, __out_opt LPDWORD lpNumberOfBytesRead, * __inout_opt LPOVERLAPPED lpOverlapped ); */ boolean ReadFile(Pointer hFile, Memory lpBuffer, int nNumberOfBytesToRead, IntByReference lpNumberOfBytesRead, Structure lpOverlapped); /* * DWORD WINAPI GetCurrentProcessId(void); */ /* * (non-Javadoc) * * @see com.sun.jna.examples.win32.Kernel32#GetCurrentProcessId() */ int GetCurrentProcessId(); /* * DWORD WINAPI GetProcessIdOfThread( __in HANDLE Thread ); */ /** * Gets the process id of thread. * * @param Thread * the thread * * @return the int */ int GetProcessIdOfThread(Pointer Thread); /* * BOOL WINAPI CreateProcess( LPCTSTR lpApplicationName, LPTSTR * lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, * LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD * dwCreationFlags, LPVOID lpEnvironment, LPCTSTR lpCurrentDirectory, * LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMATION * lpProcessInformation ); */ /** * Creates the process a. * * @param lpApplicationName * the lp application name * @param lpCommandLine * the lp command line * @param lpProcessAttributes * the lp process attributes * @param lpThreadAttributes * the lp thread attributes * @param bInheritHandles * the b inherit handles * @param dwCreationFlags * the dw creation flags * @param lpEnvironment * the lp environment * @param lpCurrentDirectory * the lp current directory * @param lpStartupInfo * the lp startup info * @param lpProcessInformation * the lp process information * * @return true, if successful */ boolean CreateProcessA(String lpApplicationName, String lpCommandLine, Structure lpProcessAttributes, Structure lpThreadAttributes, boolean bInheritHandles, int dwCreationFlags, Structure lpEnvironment, String lpCurrentDirectory, Structure lpStartupInfo, Structure lpProcessInformation); boolean CreateProcessW(WString lpApplicationName, WString lpCommandLine, Structure lpProcessAttributes, Structure lpThreadAttributes, boolean bInheritHandles, int dwCreationFlags, Memory lpEnvironment, WString lpCurrentDirectory, Structure lpStartupInfo, Structure lpProcessInformation); /** The CREAT e_ n o_ window. */ int CREATE_NO_WINDOW = 0x08000000; /** The CREAT e_ unicod e_ environment. */ int CREATE_UNICODE_ENVIRONMENT = 0x00000400; /** The CREAT e_ ne w_ console. */ int CREATE_NEW_CONSOLE = 0x00000010; int DETACHED_PROCESS = 0x00000008; /* * typedef struct _PROCESS_INFORMATION { HANDLE hProcess; HANDLE * hThread; DWORD dwProcessId; DWORD dwThreadId; } */ /** * The Class PROCESS_INFORMATION. */ public static class PROCESS_INFORMATION extends Structure { /** The h process. */ public HANDLE hProcess = null; /** The h thread. */ public Pointer hThread = null; /** The dw process id. */ public int dwProcessId = -1; /** The dw thread id. */ public int dwThreadId = -1; @Override public void finalize() { try { super.finalize(); } catch (Throwable e) { e.printStackTrace(); } } } /* * typedef struct _STARTUPINFO { DWORD cb; LPTSTR lpReserved; LPTSTR * lpDesktop; LPTSTR lpTitle; DWORD dwX; DWORD dwY; DWORD dwXSize; DWORD * dwYSize; DWORD dwXCountChars; DWORD dwYCountChars; DWORD * dwFillAttribute; DWORD dwFlags; WORD wShowWindow; WORD cbReserved2; * LPBYTE lpReserved2; HANDLE hStdInput; HANDLE hStdOutput; HANDLE * hStdError; } */ /** * The Class STARTUPINFO. */ public static class STARTUPINFO extends Structure { /** The cb. */ public int cb; /** The lp reserved. */ public WString lpReserved; /** The lp desktop. */ public WString lpDesktop; /** The lp title. */ public WString lpTitle; /** The dw x. */ public int dwX; /** The dw y. */ public int dwY; /** The dw x size. */ public int dwXSize; /** The dw y size. */ public int dwYSize; /** The dw x count chars. */ public int dwXCountChars; /** The dw y count chars. */ public int dwYCountChars; /** The dw fill attribute. */ public int dwFillAttribute; /** The dw flags. */ public int dwFlags; /** The w show window. */ public short wShowWindow; /** The cb reserved2. */ public short cbReserved2; /** The lp reserved2. */ public Pointer lpReserved2; /** The h std input. */ public Pointer hStdInput; /** The h std output. */ public Pointer hStdOutput; /** The h std error. */ public Pointer hStdError; @Override public void finalize() { try { super.finalize(); } catch (Throwable e) { e.printStackTrace(); } } @Override public String toString() { return ""+hStdError+"/"+hStdOutput+"/"+hStdInput; } } int SW_SHOWMINIMIZED = 2; int SW_SHOWNOACTIVATE = 4; int STARTF_USESHOWWINDOW = 0x00000001; /** The START f_ usestdhandles. */ int STARTF_USESTDHANDLES = 256; /** The IDL e_ priorit y_ class. */ int IDLE_PRIORITY_CLASS = 0x00000040; /** The BELO w_ norma l_ priorit y_ class. */ int BELOW_NORMAL_PRIORITY_CLASS = 0x00004000; /** The NORMA l_ priorit y_ class. */ int NORMAL_PRIORITY_CLASS = 0x00000020; /** The ABOV e_ norma l_ priorit y_ class. */ int ABOVE_NORMAL_PRIORITY_CLASS = 0x00008000; /** The HIG h_ priorit y_ class. */ int HIGH_PRIORITY_CLASS = 0x00000080; /** The REALTIM e_ priorit y_ class. */ int REALTIME_PRIORITY_CLASS = 0x00000100; /* * DWORD WINAPI WaitForSingleObject( HANDLE hHandle, DWORD * dwMilliseconds ); */ /** * Wait for single object. * * @param handle * the handle * @param dwMilliseconds * the dw milliseconds * * @return the int */ int WaitForSingleObject(Pointer handle, int dwMilliseconds); /** The INFINITE. */ int INFINITE = 0xFFFFFFFF; /* * BOOL WINAPI GetExitCodeProcess( HANDLE hProcess, LPDWORD lpExitCode * ); */ /** * Gets the exit code process. * * @param handle * the h process * @param lpExitCode * the lp exit code * * @return true, if successful */ boolean GetExitCodeProcess(HANDLE handle, IntByReference lpExitCode); /** The STIL l_ active. */ int STILL_ACTIVE = 0x103; /* * BOOL WINAPI TerminateProcess( HANDLE hProcess, UINT uExitCode ); */ /** * Terminate process. * * @param handle * the h process * @param uExitCode * the u exit code * * @return true, if successful */ boolean TerminateProcess(HANDLE handle, int uExitCode); /* * BOOL WINAPI GetProcessAffinityMask( __in HANDLE hProcess, __out * PDWORD_PTR lpProcessAffinityMask, __out PDWORD_PTR * lpSystemAffinityMask ); */ /** * Gets the process affinity mask. * * @param handle * the h process * @param lpProcessAffinityMask * the lp process affinity mask * @param lpSystemAffinityMask * the lp system affinity mask * * @return true, if successful */ boolean GetProcessAffinityMask(HANDLE handle, IntByReference lpProcessAffinityMask, IntByReference lpSystemAffinityMask); /* * BOOL WINAPI SetProcessAffinityMask( __in HANDLE hProcess, __in * DWORD_PTR dwProcessAffinityMask ); */ /** * Sets the process affinity mask. * * @param handle * the h process * @param dwProcessAffinityMask * the dw process affinity mask * * @return true, if successful */ boolean SetProcessAffinityMask(HANDLE handle, int dwProcessAffinityMask); /* * BOOL WINAPI SetPriorityClass( * _In_ HANDLE hProcess, *_In_ DWORD dwPriorityClass * ); */ boolean SetPriorityClass(HANDLE handle, int dwPriorityClass); /* * BOOL WINAPI CloseHandle( HANDLE hObject ); */ /** * Close handle. * * @param hObject * the h object * * @return true, if successful */ public boolean CloseHandle(Pointer hObject); /* * HANDLE WINAPI CreateToolhelp32Snapshot( DWORD dwFlags, DWORD * th32ProcessID ); */ /** * Creates the toolhelp32 snapshot. * * @param dwFlags * the dw flags * @param th32ProcessID * the th32 process id * * @return the pointer */ Pointer CreateToolhelp32Snapshot(int dwFlags, int th32ProcessID); /** The T h32 c s_ snapprocess. */ int TH32CS_SNAPPROCESS = 0x2; int WAIT_FAILED = 0xFFFFFFFF; int WAIT_TIMEOUT = 0x00000102; int WAIT_OBJECT_0 = 0x00000000; int WAIT_ABANDONED = 0x00000080; /* * BOOL WINAPI Process32First( HANDLE hSnapshot, LPPROCESSENTRY32 lppe * ); */ /** * Process32 first. * * @param hSnapshot * the h snapshot * @param lppe * the lppe * * @return true, if successful */ boolean Process32First(Pointer hSnapshot, Structure lppe); /* * typedef struct tagPROCESSENTRY32 { DWORD dwSize; DWORD cntUsage; * DWORD th32ProcessID; ULONG_PTR th32DefaultHeapID; DWORD th32ModuleID; * DWORD cntThreads; DWORD th32ParentProcessID; LONG pcPriClassBase; * DWORD dwFlags; TCHAR szExeFile[MAX_PATH]; } PROCESSENTRY32, * PPROCESSENTRY32; */ /** * The Class PROCESSENTRY32. */ public static class PROCESSENTRY32 extends Structure { /** The dw size. */ public int dwSize; /** The cnt usage. */ public int cntUsage; /** The th32 process id. */ public int th32ProcessID; /** The th32 default heap id. */ public int th32DefaultHeapID; /** The th32 module id. */ public int th32ModuleID; /** The cnt threads. */ public int cntThreads; /** The th32 parent process id. */ public int th32ParentProcessID; /** The pc pri class base. */ public int pcPriClassBase; /** The dw flags. */ public int dwFlags; /** The sz exe file. */ public char[] szExeFile; } /** The MA x_ path. */ int MAX_PATH = 260; /* * BOOL WINAPI Process32Next( HANDLE hSnapshot, LPPROCESSENTRY32 lppe ); */ /** * Process32 next. * * @param hSnapshot * the h snapshot * @param lppe * the lppe * * @return true, if successful */ boolean Process32Next(Pointer hSnapshot, Structure lppe); /* * HANDLE WINAPI OpenProcess( DWORD dwDesiredAccess, BOOL * bInheritHandle, DWORD dwProcessId ); */ /** * Open process. * * @param dwDesiredAccess * the dw desired access * @param bInheritHandle * the b inherit handle * @param dwProcessId * the dw process id * * @return the pointer */ HANDLE OpenProcess(int dwDesiredAccess, boolean bInheritHandle, int dwProcessId); /** The PROCES s_ terminate. */ int PROCESS_TERMINATE = 1; /** The PROCES s_ quer y_ information. */ int PROCESS_QUERY_INFORMATION = 1024; /** The STANDAR d_ right s_ required. */ int STANDARD_RIGHTS_REQUIRED = 0xF0000; /** The SYNCHRONIZE. */ int SYNCHRONIZE = 0x100000; /** The PROCES s_ al l_ access. */ int PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xFFF; /* * BOOL WINAPI GetProcessTimes( __in HANDLE hProcess, __out LPFILETIME * lpCreationTime, __out LPFILETIME lpExitTime, __out LPFILETIME * lpKernelTime, __out LPFILETIME lpUserTime ); */ /** * Gets the process times. * * @param handle * the h process * @param lpCreationTime * the lp creation time * @param lpExitTime * the lp exit time * @param lpKernelTime * the lp kernel time * @param lpUserTime * the lp user time * * @return true, if successful */ boolean GetProcessTimes(HANDLE handle, LongByReference lpCreationTime, LongByReference lpExitTime, LongByReference lpKernelTime, LongByReference lpUserTime); /* * BOOL WINAPI CreatePipe( __out PHANDLE hReadPipe, __out PHANDLE * hWritePipe, __in LPSECURITY_ATTRIBUTES lpPipeAttributes, __in DWORD * nSize ); */ /** * Creates the pipe. * * @param hReadPipe * the h read pipe * @param hWritePipe * the h write pipe * @param lpPipeAttributes * the lp pipe attributes * @param nSize * the n size * * @return the int */ int CreatePipe(PointerByReference hReadPipe, PointerByReference hWritePipe, Structure lpPipeAttributes, int nSize); /* * typedef struct _SECURITY_ATTRIBUTES { DWORD nLength; LPVOID * lpSecurityDescriptor; BOOL bInheritHandle; } SECURITY_ATTRIBUTES, * PSECURITY_ATTRIBUTES, LPSECURITY_ATTRIBUTES; */ /** * The Class SECURITY_ATTRIBUTES. */ public static class SECURITY_ATTRIBUTES extends Structure { /** The n length. */ public int nLength; /** The lp security descriptor. */ public Pointer lpSecurityDescriptor; /** The b inherit handle. */ public boolean bInheritHandle; } /* * BOOL WINAPI SetHandleInformation( __in HANDLE hObject, __in DWORD * dwMask, __in DWORD dwFlags ); */ /** * Sets the handle information. * * @param hObject * the h object * @param dwMask * the dw mask * @param dwFlags * the dw flags * * @return true, if successful */ boolean SetHandleInformation(Pointer hObject, int dwMask, int dwFlags); /** The HANDL e_ fla g_ inherit. */ int HANDLE_FLAG_INHERIT = 0x00000001; int HANDLE_FLAG_PROTECT_FROM_CLOSE = 0x00000002; /* * HANDLE WINAPI CreateNamedPipe( __in LPCTSTR lpName, __in DWORD * dwOpenMode, __in DWORD dwPipeMode, __in DWORD nMaxInstances, __in * DWORD nOutBufferSize, __in DWORD nInBufferSize, __in DWORD * nDefaultTimeOut, __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes * ); */ /** * Creates the named pipe a. * * @param lpName * the lp name * @param dwOpenMode * the dw open mode * @param dwPipeMode * the dw pipe mode * @param nMaxInstances * the n max instances * @param nOutBufferSize * the n out buffer size * @param nInBufferSize * the n in buffer size * @param nDefaultTimeOut * the n default time out * @param lpSecurityAttributes * the lp security attributes * * @return the pointer */ Pointer CreateNamedPipeA(String lpName, int dwOpenMode, int dwPipeMode, int nMaxInstances, int nOutBufferSize, int nInBufferSize, int nDefaultTimeOut, SECURITY_ATTRIBUTES lpSecurityAttributes); /** The PIP e_ acces s_ outbound. */ int PIPE_ACCESS_OUTBOUND = 0x00000002; /** The PIP e_ acces s_ inbound. */ int PIPE_ACCESS_INBOUND = 0x00000001; /** The PIP e_ wait. */ int PIPE_WAIT = 0x00000000; /** The PIP e_ nowait. */ int PIPE_NOWAIT = 0x00000001; /** The GENERI c_ read. */ int GENERIC_READ = 0x80000000; /** * Creates the file a. * * @param lpFileName * the lp file name * @param dwDesiredAccess * the dw desired access * @param dwShareMode * the dw share mode * @param lpSecurityAttributes * the lp security attributes * @param dwCreationDisposition * the dw creation disposition * @param dwFlagsAndAttributes * the dw flags and attributes * @param hTemplateFile * the h template file * * @return the pointer */ Pointer CreateFileA(String lpFileName, int dwDesiredAccess, int dwShareMode, SECURITY_ATTRIBUTES lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, Pointer hTemplateFile); /* * BOOL WINAPI ConnectNamedPipe( __in HANDLE hNamedPipe, __inout_opt * LPOVERLAPPED lpOverlapped ); */ /** * Connect named pipe. * * @param hNamedPipe * the h named pipe * @param lpOverlapped * the lp overlapped * * @return true, if successful */ boolean ConnectNamedPipe(Pointer hNamedPipe, PointerByReference lpOverlapped); /** The INVALI d_ handl e_ value. */ Pointer INVALID_HANDLE_VALUE = Pointer.createConstant(-1); /* * BOOL WINAPI WaitNamedPipe( __in LPCTSTR lpNamedPipeName, __in DWORD * nTimeOut ); */ /** * Wait named pipe a. * * @param lpNamedPipeName * the lp named pipe name * @param nTimeOut * the n time out * * @return true, if successful */ boolean WaitNamedPipeA(String lpNamedPipeName, int nTimeOut); /** The NMPWAI t_ us e_ defaul t_ wait. */ int NMPWAIT_USE_DEFAULT_WAIT = 0; /** The NMPWAI t_ wai t_ forever. */ int NMPWAIT_WAIT_FOREVER = 0xffffffff; /* * BOOL WINAPI SetCurrentDirectory( __in LPCTSTR lpPathName ); */ boolean SetCurrentDirectoryA(String lpPathName); /* * typedef struct _MEMORY_BASIC_INFORMATION { PVOID BaseAddress; PVOID * AllocationBase; DWORD AllocationProtect; SIZE_T RegionSize; DWORD * State; DWORD Protect; DWORD Type; } MEMORY_BASIC_INFORMATION, * *PMEMORY_BASIC_INFORMATION; */ public static class MEMORY_BASIC_INFORMATION extends Structure { public Pointer BaseAddress; public Pointer AllocationBase; public int AllocationProtect; public Pointer RegionSize; public int State; public int Protect; public int Type; } public static int PAGE_NOACCESS = 0x01; public static int PAGE_EXECUTE = 0x10; /* * SIZE_T WINAPI VirtualQueryEx( __in HANDLE hProcess, __in_opt LPCVOID * lpAddress, __out PMEMORY_BASIC_INFORMATION lpBuffer, __in SIZE_T * dwLength ); */ int VirtualQueryEx(Pointer hProcess, Pointer lpAddress, Pointer lpBuffer, int dwLength); boolean ReadProcessMemory(Pointer hProcess, Pointer lpBaseAddress, Pointer lpBuffer, NativeLong nSize, NativeLongByReference lpNumberOfBytesRead); /* * DWORD WTSGetActiveConsoleSessionId(void); */ int WTSGetActiveConsoleSessionId(); } /** * The Interface Ntdll. */ public interface Ntdll extends com.sun.jna.win32.StdCallLibrary { /** The INSTANCE. */ Ntdll INSTANCE = (Ntdll) Native.loadLibrary("Ntdll", Ntdll.class); /* * NTOSAPI NTSTATUS NTAPI ZwReadVirtualMemory( /IN/ HANDLE * ProcessHandle, /IN/ PVOID BaseAddress, /OUT/ PVOID Buffer, /IN/ ULONG * BufferLength, /OUT/ PULONG ReturnLength /OPTIONAL/); */ /** * Zw read virtual memory. * * @param ProcessHandle * the process handle * @param BaseAddress * the base address * @param Buffer * the buffer * @param BufferLength * the buffer length * @param ReturnLength * the return length * * @return the int */ int ZwReadVirtualMemory(Pointer ProcessHandle, Pointer BaseAddress, Pointer Buffer, int BufferLength, IntByReference ReturnLength); /* * NTSTATUS WINAPI ZwQueryInformationProcess( __in HANDLE ProcessHandle, * __in PROCESSINFOCLASS ProcessInformationClass, __out PVOID * ProcessInformation, __in ULONG ProcessInformationLength, __out_opt * PULONG ReturnLength ); */ /** * Zw query information process. * * @param process * the process handle * @param ProcessInformationClass * the process information class * @param ProcessInformation * the process information * @param ProcessInformationLength * the process information length * @param ReturnLength * the return length * * @return the int */ int ZwQueryInformationProcess(HANDLE process, int ProcessInformationClass, Pointer ProcessInformation, int ProcessInformationLength, IntByReference ReturnLength); /* * typedef struct _PROCESS_BASIC_INFORMATION { PVOID Reserved1; PPEB * PebBaseAddress; PVOID Reserved2[2]; ULONG_PTR UniqueProcessId; PVOID * Reserved3; } PROCESS_BASIC_INFORMATION; */ /** * The Class PROCESS_BASIC_INFORMATION. */ class PROCESS_BASIC_INFORMATION extends Structure { /** The Reserved1. */ public Pointer Reserved1; /** The Peb base address. */ public Pointer PebBaseAddress; /** The Reserved2. */ public Pointer[] Reserved2 = new Pointer[2]; /** The Unique process id. */ public Pointer UniqueProcessId; /** The Reserved3. */ public Pointer Reserved3; } /* * typedef struct _PEB { BYTE Reserved1[2]; BYTE BeingDebugged; BYTE * Reserved2[1]; PVOID Reserved3[2]; PPEB_LDR_DATA Ldr; * PRTL_USER_PROCESS_PARAMETERS ProcessParameters; BYTE Reserved4[104]; * PVOID Reserved5[52]; PPS_POST_PROCESS_INIT_ROUTINE * PostProcessInitRoutine; BYTE Reserved6[128]; PVOID Reserved7[1]; * ULONG SessionId; } PEB, PPEB; */ /** * The Class PEB. */ class PEB extends Structure { /** The Reserved1. */ public byte[] Reserved1 = new byte[2]; /** The Being debugged. */ public byte BeingDebugged; /** The Reserved2. */ public byte Reserved2; /** The Reserved3. */ public Pointer[] Reserved3 = new Pointer[2]; /** The Ldr. */ public Pointer Ldr; /** The Process parameters. */ public Pointer ProcessParameters; /** The Reserved4. */ public byte[] Reserved4 = new byte[104]; /** The Reserved5. */ public Pointer[] Reserved5 = new Pointer[52]; /** The Post process init routine. */ public Pointer PostProcessInitRoutine; /** The Reserved6. */ public byte[] Reserved6 = new byte[128]; /** The Reserved7. */ public Pointer Reserved7; /** The Session id. */ public int SessionId; } /* * typedef struct _PEB { BYTE Reserved1[2]; BYTE BeingDebugged; BYTE * Reserved2[21]; PPEB_LDR_DATA LoaderData; PRTL_USER_PROCESS_PARAMETERS * ProcessParameters; BYTE Reserved3[520]; PPS_POST_PROCESS_INIT_ROUTINE * PostProcessInitRoutine; BYTE Reserved4[136]; ULONG SessionId; } PEB; */ class PEB64 extends Structure { public byte[] Reserved1 = new byte[2]; public byte BeingDebugged; public byte[] Reserved2 = new byte[21]; ; public Pointer Ldr; public Pointer ProcessParameters; public byte[] Reserved3 = new byte[520]; public Pointer PostProcessInitRoutine; public byte[] Reserved4 = new byte[136]; public int SessionId; } /* * typedef struct _RTL_USER_PROCESS_PARAMETERS { BYTE Reserved1[16]; 16 * PVOID Reserved2[10]; 40 UNICODE_STRING ImagePathName; UNICODE_STRING * CommandLine; } RTL_USER_PROCESS_PARAMETERS, * PRTL_USER_PROCESS_PARAMETERS; * * typedef struct _RTL_USER_PROCESS_PARAMETERS { ULONG MaximumLength; 4 * ULONG Length; 8 ULONG Flags; 12 ULONG DebugFlags; 16 PVOID * ConsoleHandle; 4 ULONG ConsoleFlags; 8 HANDLE StdInputHandle; 12 * HANDLE StdOutputHandle; 16 HANDLE StdErrorHandle; 20 UNICODE_STRING * CurrentDirectoryPath; 28 HANDLE CurrentDirectoryHandle; 32 * UNICODE_STRING DllPath; 40 UNICODE_STRING ImagePathName; 48 * UNICODE_STRING CommandLine; 56 PVOID Environment; 4 ULONG * StartingPositionLeft; 8 ULONG StartingPositionTop; 12 ULONG Width; 16 * ULONG Height; 20 ULONG CharWidth; 24 ULONG CharHeight; 28 ULONG * ConsoleTextAttributes; 32 ULONG WindowFlags; 36 ULONG * ShowWindowFlags; 40 UNICODE_STRING WindowTitle; 48 UNICODE_STRING * DesktopName; UNICODE_STRING ShellInfo; UNICODE_STRING RuntimeData; * RTL_DRIVE_LETTER_CURDIR DLCurrentDirectory[0x20]; } * RTL_USER_PROCESS_PARAMETERS,PRTL_USER_PROCESS_PARAMETERS; */ /** * The Class RTL_USER_PROCESS_PARAMETERS. */ class RTL_USER_PROCESS_PARAMETERS extends Structure { public int AllocationSize; public int Size; public int Flags; public int DebugFlags; public HANDLE hConsole; public int ProcessGroup; public HANDLE hStdInput; public HANDLE hStdOutput; public HANDLE hStdError; public UNICODE_STRING CurrentDirectoryPath; public Pointer CurrentDirectoryHandle; public UNICODE_STRING DllPath; /** The Image path name. */ public UNICODE_STRING ImagePathName; /** The Command line. */ public UNICODE_STRING CommandLine; public Pointer Environment; // new public int dwX; public int dwY; public int dwXSize; public int dwYSize; public int dwXCountChars; public int dwYCountChars; public int dwFillAttribute; public int dwFlags; public int wShowWindow; public UNICODE_STRING WindowTitle; public UNICODE_STRING Desktop; public UNICODE_STRING ShellInfo; public UNICODE_STRING RuntimeInfo; public RTL_DRIVE_LETTER_CURDIR[] DLCurrentDirectory = new RTL_DRIVE_LETTER_CURDIR[0x20]; } class RTL_DRIVE_LETTER_CURDIR extends Structure { public int Flags; public int Length; public int TimeStamp; public UNICODE_STRING DosPath; }; /* * typedef struct _LSA_UNICODE_STRING { USHORT Length; USHORT * MaximumLength; PWSTR Buffer; } LSA_UNICODE_STRING, * PLSA_UNICODE_STRING, UNICODE_STRING, PUNICODE_STRING; */ /** * The Class UNICODE_STRING. */ class UNICODE_STRING extends Structure { /** The Length. */ public short Length = 0; /** The Maximum length. */ public short MaximumLength; /** The Buffer. */ public Pointer Buffer; } } public interface MyAdvapi extends Advapi32 { MyAdvapi INSTANCE = (MyAdvapi) Native.loadLibrary("Advapi32", MyAdvapi.class, Options.UNICODE_OPTIONS); /* * BOOL WINAPI LookupAccountSid( __in_opt LPCTSTR lpSystemName, __in * PSID lpSid, __out_opt LPTSTR lpName, __inout LPDWORD cchName, * __out_opt LPTSTR lpReferencedDomainName, __inout LPDWORD * cchReferencedDomainName, __out PSID_NAME_USE peUse ); */ boolean LookupAccountSidW(String lpSystemName, Pointer lpSid, Memory lpName, IntByReference cchName, Memory lpReferencedDomainName, IntByReference cchReferencedDomainName, IntByReference peUse); /* * typedef struct _SID_AND_ATTRIBUTES { PSID Sid; DWORD Attributes; } * SID_AND_ATTRIBUTES,PSID_AND_ATTRIBUTES; */ static class SID_AND_ATTRIBUTES extends Structure { public Pointer Sid; public int Attributes; } /* * typedef struct _TOKEN_USER { SID_AND_ATTRIBUTES User; } TOKEN_USER, * PTOKEN_USER; */ static class TOKEN_USER extends Structure { public SID_AND_ATTRIBUTES User; public TOKEN_USER(Pointer p) { super(); this.useMemory(p); this.read(); } } public static final int TokenPrivileges = 3; public static final int TokenUser = 1; public static final int TokenElevation = 20; /* * typedef struct _TOKEN_ELEVATION { DWORD TokenIsElevated; } TOKEN_ELEVATION, *PTOKEN_ELEVATION; */ static class TOKEN_ELEVATION extends Structure { public int TokenIsElevated = 0; public TOKEN_ELEVATION(Pointer p) { super(); this.useMemory(p); this.read(); } public boolean isElevated() { return TokenIsElevated != 0; } } /* * BOOL WINAPI GetTokenInformation( __in HANDLE TokenHandle, __in * TOKEN_INFORMATION_CLASS TokenInformationClass, __out_opt LPVOID * TokenInformation, __in DWORD TokenInformationLength, __out PDWORD * ReturnLength ); */ boolean GetTokenInformation(Pointer TokenHandle, int TokenInformationClass, Memory TokenInformation, int TokenInformationLength, IntByReference ReturnLength); /* * BOOL WINAPI InitializeSecurityDescriptor( __out PSECURITY_DESCRIPTOR * pSecurityDescriptor, __in DWORD dwRevision ); */ boolean InitializeSecurityDescriptor(Memory pSecurityDescriptor, int dwRevision); public static final int SECURITY_DESCRIPTOR_MIN_LENGTH = 20; public static final int SECURITY_DESCRIPTOR_REVISION = 1; /* * BOOL WINAPI SetSecurityDescriptorSacl( __inout PSECURITY_DESCRIPTOR * pSecurityDescriptor, __in BOOL bSaclPresent, __in_opt PACL pSacl, * __in BOOL bSaclDefaulted ); */ boolean SetSecurityDescriptorDacl(Pointer pSecurityDescriptor, boolean bSaclPresent, Pointer pSacl, boolean bSaclDefaulted); public static int SE_PRIVILEGE_ENABLED = 2; /* * typedef struct _LUID { DWORD LowPart; LONG HighPart; } LUID,PLUID; */ static class LUID extends Structure { public int LowPart; public int HighPart; } /* * typedef struct _LUID_AND_ATTRIBUTES { LUID Luid; DWORD Attributes; } * LUID_AND_ATTRIBUTES,PLUID_AND_ATTRIBUTES; */ static class LUID_AND_ATTRIBUTES extends Structure { public LUID Luid; public int Attributes; } /* * typedef struct _TOKEN_PRIVILEGES { DWORD PrivilegeCount; * LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY]; } TOKEN_PRIVILEGES, * PTOKEN_PRIVILEGES; */ static class TOKEN_PRIVILEGES extends Structure { public int PrivilegeCount = 1; public LUID_AND_ATTRIBUTES[] Privileges = new LUID_AND_ATTRIBUTES[1]; public TOKEN_PRIVILEGES() { super(); Privileges[0] = new LUID_AND_ATTRIBUTES(); } public TOKEN_PRIVILEGES(Pointer p) { super(); PrivilegeCount = p.getInt(0); Privileges = new LUID_AND_ATTRIBUTES[PrivilegeCount]; this.useMemory(p); this.read(); } } /* * BOOL WINAPI AdjustTokenPrivileges( __in HANDLE TokenHandle, __in BOOL * DisableAllPrivileges, __in_opt PTOKEN_PRIVILEGES NewState, __in DWORD * BufferLength, __out_opt PTOKEN_PRIVILEGES PreviousState, __out_opt * PDWORD ReturnLength ); */ boolean AdjustTokenPrivileges(Pointer TokenHandle, boolean DisableAllPrivileges, TOKEN_PRIVILEGES NewState, int BufferLength, PointerByReference PreviousState, IntByReference ReturnLength); /* * BOOL WINAPI LookupPrivilegeValue( __in_opt LPCTSTR lpSystemName, __in * LPCTSTR lpName, __out PLUID lpLuid ); */ boolean LookupPrivilegeValueA(String lpSystemName, String lpName, LUID lpLuid); public static final String SE_ASSIGNPRIMARYTOKEN_NAME = "SeAssignPrimaryTokenPrivilege"; public static final String SE_INCREASE_QUOTA_NAME = "SeIncreaseQuotaPrivilege"; public static final String SE_DEBUG_NAME = "SeDebugPrivilege"; public static final String SE_TCB_NAME = "SeTcbPrivilege"; /* * BOOL WINAPI OpenProcessToken( __in HANDLE ProcessHandle, __in DWORD * DesiredAccess, __out PHANDLE TokenHandle ); */ boolean OpenProcessToken(HANDLE ProcessHandle, int DesiredAccess, PointerByReference TokenHandle); public static final int STANDARD_RIGHTS_READ = 0x20000; public static final int STANDARD_RIGHTS_WRITE = 0x20000; public static final int TOKEN_QUERY = 0x0008; public static final int TOKEN_ADJUST_PRIVILEGES = 0x0020; public static final int TOKEN_ADJUST_GROUPS = 0x0040; public static final int TOKEN_ADJUST_DEFAULT = 0x0080; public static final int TOKEN_DUPLICATE = 0x0002; public static final int TOKEN_IMPERSONATE = 0x0004; public static final int TOKEN_READ = STANDARD_RIGHTS_READ | TOKEN_QUERY; public static final int TOKEN_WRITE = STANDARD_RIGHTS_WRITE | TOKEN_ADJUST_PRIVILEGES | TOKEN_ADJUST_GROUPS | TOKEN_ADJUST_DEFAULT; /* * BOOL WINAPI CreateProcessWithLogonW( __in LPCWSTR lpUsername, * __in_opt LPCWSTR lpDomain, __in LPCWSTR lpPassword, __in DWORD * dwLogonFlags, __in_opt LPCWSTR lpApplicationName, __inout_opt LPWSTR * lpCommandLine, __in DWORD dwCreationFlags, __in_opt LPVOID * lpEnvironment, __in_opt LPCWSTR lpCurrentDirectory, __in * LPSTARTUPINFOW lpStartupInfo, __out LPPROCESS_INFORMATION * lpProcessInfo ); */ boolean CreateProcessWithLogonW(WString lpUsername, WString lpDomain, WString lpPassword, int dwLogonFlags, WString lpApplicationName, WString lpCommandLine, int dwCreationFlags, Pointer lpEnvironment, WString lpCurrentDirectory, Structure lpStartupInfo, Structure lpProcessInfo); public static final int LOGON_WITH_PROFILE = 0x00000001; public static final int LOGON_NETCREDENTIALS_ONLY = 0x00000002; /* * BOOL LogonUser( __in LPTSTR lpszUsername, __in_opt LPTSTR lpszDomain, * __in LPTSTR lpszPassword, __in DWORD dwLogonType, __in DWORD * dwLogonProvider, __out PHANDLE phToken ); */ boolean LogonUserA(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, PointerByReference phToken); boolean LogonUserW(WString lpszUsername, WString lpszDomain, WString lpszPassword, int dwLogonType, int dwLogonProvider, PointerByReference phToken); public static final int LOGON32_LOGON_INTERACTIVE = 2; public static final int LOGON32_LOGON_NETWORK = 3; public static final int LOGON32_LOGON_BATCH = 4; public static final int LOGON32_LOGON_SERVICE = 5; public static final int LOGON32_LOGON_UNLOCK = 7; public static final int LOGON32_LOGON_NETWORK_CLEARTEXT = 8; public static final int LOGON32_LOGON_NEW_CREDENTIALS = 9; public static final int LOGON32_PROVIDER_DEFAULT = 0; public static final int LOGON32_PROVIDER_WINNT35 = 1; public static final int LOGON32_PROVIDER_WINNT40 = 2; public static final int LOGON32_PROVIDER_WINNT50 = 3; /* * BOOL WINAPI ImpersonateLoggedOnUser( __in HANDLE hToken ); */ boolean ImpersonateLoggedOnUser(Pointer hToken); /* * BOOL WINAPI CreateProcessAsUser( __in_opt HANDLE hToken, __in_opt * LPCTSTR lpApplicationName, __inout_opt LPTSTR lpCommandLine, __in_opt * LPSECURITY_ATTRIBUTES lpProcessAttributes, __in_opt * LPSECURITY_ATTRIBUTES lpThreadAttributes, __in BOOL bInheritHandles, * __in DWORD dwCreationFlags, __in_opt LPVOID lpEnvironment, __in_opt * LPCTSTR lpCurrentDirectory, __in LPSTARTUPINFO lpStartupInfo, __out * LPPROCESS_INFORMATION lpProcessInformation ); */ boolean CreateProcessAsUserW(Pointer hToken, WString lpApplicationName, WString lpCommandLine, Structure lpProcessAttributes, Structure lpThreadAttributes, boolean bInheritHandles, int dwCreationFlags, Structure lpEnvironment, WString lpCurrentDirectory, Structure lpStartupInfo, Structure lpProcessInformation); static class SECURITY_ATTRIBUTES { public int nLength; public Pointer lpSecurityDescriptor; boolean bInheritHandle; } /* * BOOL WINAPI DuplicateTokenEx( __in HANDLE hExistingToken, __in DWORD * dwDesiredAccess, __in_opt LPSECURITY_ATTRIBUTES lpTokenAttributes, * __in SECURITY_IMPERSONATION_LEVEL ImpersonationLevel, __in TOKEN_TYPE * TokenType, __out PHANDLE phNewToken ); */ boolean DuplicateTokenEx(Pointer hExistingToken, int dwDesiredAccess, Pointer lpTokenAttributes, int ImpersonationLevel, int TokenType, PointerByReference phNewToken); } public interface Secur32 extends StdCallLibrary { /** The INSTANCE. */ Secur32 INSTANCE = (Secur32) Native.loadLibrary("Secur32", Secur32.class); /* * BOOLEAN WINAPI GetUserNameEx( __in EXTENDED_NAME_FORMAT NameFormat, * __out LPTSTR lpNameBuffer, __inout PULONG lpnSize ); */ boolean GetUserNameEx(int NameFormat, Memory lpNameBuffer, IntByReference lpnSize); } public interface MyWtsapi32 extends StdCallLibrary { // Method declarations, constant and structure definitions go here /** The INSTANCE. */ MyWtsapi32 INSTANCE = (MyWtsapi32) Native.loadLibrary("Wtsapi32", MyWtsapi32.class); /* * BOOL WTSQueryUserToken( __in ULONG SessionId, __out PHANDLE phToken); */ boolean WTSQueryUserToken(int SessionId, PointerByReference phToken); } /** The _startup info. */ volatile STARTUPINFO _startupInfo; /** The _process information. */ volatile PROCESS_INFORMATION _processInformation; /** The in read. */ volatile PointerByReference inRead = null; /** The in write. */ volatile PointerByReference inWrite = null; /** The out read. */ volatile PointerByReference outRead = null; /** The out write. */ volatile PointerByReference outWrite = null; /** The err read. */ volatile PointerByReference errRead = null; /** The err write. */ volatile PointerByReference errWrite = null; /** The sa. */ volatile SECURITY_ATTRIBUTES sa; /** The m_h out pipe. */ volatile Pointer m_hOutPipe = null; /** The m_h err pipe. */ volatile Pointer m_hErrPipe = null; /** The m_h in pipe. */ volatile Pointer m_hInPipe = null; /** The in write pipe. */ volatile Pointer inWritePipe = null; /** The out read pipe. */ volatile Pointer outReadPipe = null; /** The err read pipe. */ volatile Pointer errReadPipe = null; volatile int _isElevated = -1; // 1 = true, 0 = false; /** * Gets the process. * * @param pid * the pid * * @return the process */ public static Process getProcess(int pid) { WindowsXPProcess result = new WindowsXPProcess(); HANDLE hProcess = MyKernel32.INSTANCE.OpenProcess(MyKernel32.PROCESS_ALL_ACCESS, false, pid); if (hProcess == null) hProcess = MyKernel32.INSTANCE.OpenProcess(MyKernel32.PROCESS_QUERY_INFORMATION, false, pid); if (hProcess == null) return null; result._pid = pid; result._processInformation = new PROCESS_INFORMATION(); result._processInformation.dwProcessId = pid; result._processInformation.hProcess = hProcess; result._cmd = result.getCommandLineInternal(); // this does not always work (why ??), if so try again, then this // normally does // on win64 PEB of 64 bit cannot be accessed from wow -> use wmi if (result._cmd.equals("?")) result._cmd = result.getCommandLineInternalWMI(); if ("?".equals(result._cmd)) { System.err.println("Could not get commandline"); } //else // System.out.println("Command line of " + pid + ": " + result._cmd); PointerByReference hToken = new PointerByReference(); HANDLE hp = new HANDLE(); hp.setPointer(hProcess.getPointer()); if (MyAdvapi.INSTANCE.OpenProcessToken(hp, MyAdvapi.TOKEN_READ, hToken)) { IntByReference dwSize = new IntByReference(); MyAdvapi.INSTANCE.GetTokenInformation(hToken.getValue(), MyAdvapi.TokenUser, null, 0, dwSize); { Memory pTokenUser = new Memory(dwSize.getValue()); if (MyAdvapi.INSTANCE.GetTokenInformation(hToken.getValue(), MyAdvapi.TokenUser, pTokenUser, dwSize.getValue(), dwSize)) { MyAdvapi.TOKEN_USER tokenUser = new MyAdvapi.TOKEN_USER(pTokenUser); Pointer lpSid = tokenUser.User.Sid; Memory lpName = new Memory(256); IntByReference cchName = new IntByReference(); cchName.setValue(256); Memory lpReferencedDomainName = new Memory(256); IntByReference cchReferencedDomainName = new IntByReference(); cchReferencedDomainName.setValue(256); IntByReference peUse = new IntByReference(); if (MyAdvapi.INSTANCE.LookupAccountSidW(null, lpSid, lpName, cchName, lpReferencedDomainName, cchReferencedDomainName, peUse)) result._user = lpReferencedDomainName.getString(0, true) + "\\" + lpName.getString(0, true); ; // System.out.println(result._user); } } if (result._user == null) System.out.println("could not get user name OS error #" + MyKernel32.INSTANCE.GetLastError()); MyKernel32.INSTANCE.CloseHandle(hToken.getValue()); } return result; } private boolean setPrivilege(Pointer hToken, String lpszPrivilege, boolean bEnablePrivilege) { TOKEN_PRIVILEGES tp = new TOKEN_PRIVILEGES(); MyAdvapi.LUID luid = new MyAdvapi.LUID(); luid.size(); if (!MyAdvapi.INSTANCE.LookupPrivilegeValueA(null, lpszPrivilege, luid)) return false; tp.Privileges[0].Luid = luid; tp.write(); if (bEnablePrivilege) tp.Privileges[0].Attributes = MyAdvapi.SE_PRIVILEGE_ENABLED; else tp.Privileges[0].Attributes = 0; int size = tp.size(); boolean result = MyAdvapi.INSTANCE.AdjustTokenPrivileges(hToken, false, tp, 0, null, null); // return GetLastError() == ERROR_SUCCESS; if (!result) { int errNr = MyKernel32.INSTANCE.GetLastError(); log("error setting privliges OS error #" + errNr + "/" + Integer.toHexString(errNr)); } return result; } /* * (non-Javadoc) * * @see org.rzo.yajsw.os.Process#start() */ public boolean start() { boolean result = false; if (isRunning()) { log("process already running -> abort start"); return false; } else { setPid(-1); setExitCode(-1); } _started = false; int PIPE_SIZE = 1024; // buffer size for pipes int PIPE_TIMEOUT = 12000; // time to wait for pipe if (_arrCmd == null && _cmd == null) return false; if (_cmd == null) { _cmd = ""; for (String cmd : _arrCmd) { if (cmd == null || cmd.length() == 0) continue; if (cmd.contains(" ") && !cmd.endsWith("\"")) _cmd += '"' + cmd + "\" "; else _cmd += cmd + " "; } // _cmd += cmd + " "; } if (_debug) log("exec: " + _cmd); if (_processInformation != null) { log("process not correctly disposed -> abort start"); return false; } try { destroyed = false; _startupInfo = new STARTUPINFO(); _startupInfo.clear(); _processInformation = new PROCESS_INFORMATION(); _processInformation.clear(); if (_pipeStreams) { if (sa == null) { sa = new SECURITY_ATTRIBUTES(); sa.clear(); sa.nLength = sa.size(); sa.lpSecurityDescriptor = null; sa.bInheritHandle = true;// 1; // true otherwise streams are // not piped } inRead = new PointerByReference(); inWrite = new PointerByReference(); outRead = new PointerByReference(); outWrite = new PointerByReference(); errRead = new PointerByReference(); errWrite = new PointerByReference(); _startupInfo.dwFlags = MyKernel32.STARTF_USESTDHANDLES; if (MyKernel32.INSTANCE.CreatePipe(inRead, inWrite, sa, 0) == 0) { log("Error in CreatePipe inWrite " + Integer.toHexString(MyKernel32.INSTANCE.GetLastError())); return false; } if (!MyKernel32.INSTANCE.SetHandleInformation(inWrite.getValue(), MyKernel32.HANDLE_FLAG_INHERIT, 0)) { log("error in set handle inWrite -> abort start"); return false; } _startupInfo.hStdInput = inRead.getValue(); if (MyKernel32.INSTANCE.CreatePipe(outRead, outWrite, sa, 0) == 0) { log("Error in CreatePipe outWrite " + Integer.toHexString(MyKernel32.INSTANCE.GetLastError())); return false; } if (!MyKernel32.INSTANCE.SetHandleInformation(outRead.getValue(), MyKernel32.HANDLE_FLAG_INHERIT, 0)) { log("error in set handle outRead -> abort start"); return false; } _startupInfo.hStdOutput = outWrite.getValue(); if (MyKernel32.INSTANCE.CreatePipe(errRead, errWrite, sa, 0) == 0) { log("Error in CreatePipe errWrite " + Integer.toHexString(MyKernel32.INSTANCE.GetLastError())); return false; } if (!MyKernel32.INSTANCE.SetHandleInformation(errRead.getValue(), MyKernel32.HANDLE_FLAG_INHERIT, 0)) { log("error in set handle inWrite -> abort start"); return false; } _startupInfo.hStdError = errWrite.getValue(); // for some unknown reason: if we add the following // lines we do not get "operation on non socket" error // in mina /*|| !MyKernel32.INSTANCE.SetHandleInformation(inWrite.getValue(), MyKernel32.HANDLE_FLAG_PROTECT_FROM_CLOSE, MyKernel32.HANDLE_FLAG_PROTECT_FROM_CLOSE) || !MyKernel32.INSTANCE.SetHandleInformation(outRead.getValue(), MyKernel32.HANDLE_FLAG_PROTECT_FROM_CLOSE, MyKernel32.HANDLE_FLAG_PROTECT_FROM_CLOSE) || !MyKernel32.INSTANCE.SetHandleInformation(errRead.getValue(), MyKernel32.HANDLE_FLAG_PROTECT_FROM_CLOSE, MyKernel32.HANDLE_FLAG_PROTECT_FROM_CLOSE) */ if (this._redirectErrorStream) MyKernel32.INSTANCE.SetHandleInformation(errRead.getValue(), MyKernel32.HANDLE_FLAG_INHERIT, 0); } int creationFlag = 0; if (!_visible) { creationFlag |= MyKernel32.CREATE_NO_WINDOW | MyKernel32.CREATE_UNICODE_ENVIRONMENT; _startupInfo.lpTitle = null; } else { creationFlag |= MyKernel32.CREATE_NEW_CONSOLE | MyKernel32.CREATE_UNICODE_ENVIRONMENT; _startupInfo.lpTitle = new WString(_title); if (_minimized) { _startupInfo.wShowWindow |= MyKernel32.SW_SHOWMINIMIZED | MyKernel32.SW_SHOWNOACTIVATE; _startupInfo.dwFlags |= MyKernel32.STARTF_USESHOWWINDOW; } } creationFlag |= getPriorityFlag(_priority); // do not inherit handles. otherwise resources are not freed if // parent is killed // inherit only when we need to pipe the streams _startupInfo.write(); WString cmd = new WString(_cmd); WString wDir = getWorkingDir() == null ? null : new WString(getWorkingDir()); String stdUser = standardizeUser(_user); StringBlock environment = null; WString[] env = null; if (_environment.size() != 0) { env = new WString[_environment.size()]; int i = 0; for (String[] entry : _environment) { env[i++] = new WString(entry[0] + "=" + entry[1]); } environment = new StringBlock(env); } if (_desktop != null) { _startupInfo.lpDesktop = new WString(_desktop); log("setting desktop "+_desktop); } if (_logonActiveSession) { log("start process in active session"); int session = 0xFFFFFFFF; // wait until we have an active session while (session == 0xFFFFFFFF) { session = MyKernel32.INSTANCE.WTSGetActiveConsoleSessionId(); if (session == 0xFFFFFFFF) Thread.sleep(1000); log("active session: "+session); } PointerByReference phToken = new PointerByReference(); // wait for a user to log on to the session boolean userLoggedOn = false; int retries = 0; while (!userLoggedOn) { result = MyWtsapi32.INSTANCE.WTSQueryUserToken(session, phToken); userLoggedOn = result || MyKernel32.INSTANCE.GetLastError() != 1008; if (!userLoggedOn) { Thread.sleep(1000); retries++; } } // if user just logged on: wait for the desktop to get up. // TODO evntl. add a configuration property for the time to wait. if (retries > 0) Thread.sleep(10000); if (!doesUserHavePrivilege(MyAdvapi.SE_TCB_NAME)) log("WARNING: Process does not have the SE_TCB_NAME privilege !!"); // start the application if (result) { log("got session token: "+phToken.getValue()); PointerByReference phNewToken = new PointerByReference(); result = MyAdvapi.INSTANCE.DuplicateTokenEx(phToken.getValue(), 0x2000000, null, 0, 1, phNewToken); if (result) { log("duplicated token: "+phNewToken.getValue()); //_startupInfo.lpDesktop = new WString("winsta0\\default"); creationFlag = 0; creationFlag |= MyKernel32.CREATE_NO_WINDOW | MyKernel32.CREATE_UNICODE_ENVIRONMENT; creationFlag |= getPriorityFlag(_priority); result = MyAdvapi.INSTANCE.CreateProcessAsUserW(phNewToken.getValue(), null, cmd, null, null, _pipeStreams, creationFlag, null, new WString(getWorkingDir()), _startupInfo, _processInformation); log("started "+result); } } } else if (stdUser == null || stdUser.equals(currentUser())) { result = MyKernel32.INSTANCE.CreateProcessW(null, cmd, null, null, _pipeStreams, creationFlag, environment, wDir, _startupInfo, _processInformation); } else { WString user = null; WString domain = null; int i = _user.lastIndexOf("\\"); if (i > 0) { user = new WString(_user.substring(_user.lastIndexOf("\\") + 1)); domain = new WString(_user.substring(0, _user.lastIndexOf("\\"))); } else user = new WString(_user); WString password = null; if (_password != null) password = new WString(_password); log("current user :: requested user: " + currentUserName() + " :: " + stdUser); // in windows 2008: system user seems to be