X-Git-Url: https://git.sven.stormbind.net/?p=sven%2Fjattach.git;a=blobdiff_plain;f=src%2Fwindows%2Fjattach.c;fp=src%2Fwindows%2Fjattach.c;h=1318a94541a2ef6706bde4d361cc81c4d28385f3;hp=b43e2f875e2aeaef9f692c4282859ee7e0ce5652;hb=98ce3928a99273b631c525a81df022e6f8ec46a7;hpb=91eb05a0b9cfabc7670a4f0f1e2b8083307a2fca diff --git a/src/windows/jattach.c b/src/windows/jattach.c index b43e2f8..1318a94 100644 --- a/src/windows/jattach.c +++ b/src/windows/jattach.c @@ -16,7 +16,8 @@ #include #include -#include +#include +#include typedef HMODULE (WINAPI *GetModuleHandle_t)(LPCTSTR lpModuleName); typedef FARPROC (WINAPI *GetProcAddress_t)(HMODULE hModule, LPCSTR lpProcName); @@ -223,32 +224,27 @@ static int read_response(HANDLE hPipe) { return result; } -int main(int argc, char** argv) { - if (argc < 3) { - printf("jattach " JATTACH_VERSION " built on " __DATE__ "\n" - "Copyright 2021 Andrei Pangin\n" - "\n" - "Usage: jattach [args ...]\n" - "\n" - "Commands:\n" - " load threaddump dumpheap setflag properties\n" - " jcmd inspectheap datadump printflag agentProperties\n" - ); - return 1; - } - - int pid = atoi(argv[1]); +int jattach(int pid, int argc, char** argv) { + // When attaching as an Administrator, make sure the target process can connect to our pipe, + // i.e. allow read-write access to everyone. For the complete format description, see + // https://docs.microsoft.com/en-us/windows/win32/secauthz/security-descriptor-string-format + SECURITY_ATTRIBUTES sec = {sizeof(SECURITY_ATTRIBUTES), NULL, FALSE}; + ConvertStringSecurityDescriptorToSecurityDescriptor("D:(A;;GRGW;;;WD)", SDDL_REVISION_1, + &sec.lpSecurityDescriptor, NULL); char pipeName[MAX_PATH]; sprintf(pipeName, "\\\\.\\pipe\\javatool%d", GetTickCount()); HANDLE hPipe = CreateNamedPipe(pipeName, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, - 1, 4096, 8192, NMPWAIT_USE_DEFAULT_WAIT, NULL); + 1, 4096, 8192, NMPWAIT_USE_DEFAULT_WAIT, &sec); if (hPipe == NULL) { print_error("Could not create pipe", GetLastError()); + LocalFree(sec.lpSecurityDescriptor); return 1; } - if (!inject_thread(pid, pipeName, argc - 2, argv + 2)) { + LocalFree(sec.lpSecurityDescriptor); + + if (!inject_thread(pid, pipeName, argc, argv)) { CloseHandle(hPipe); return 1; } @@ -262,3 +258,30 @@ int main(int argc, char** argv) { return result; } + +#ifdef JATTACH_VERSION + +int main(int argc, char** argv) { + if (argc < 3) { + printf("jattach " JATTACH_VERSION " built on " __DATE__ "\n" + "Copyright 2021 Andrei Pangin\n" + "\n" + "Usage: jattach [args ...]\n" + "\n" + "Commands:\n" + " load threaddump dumpheap setflag properties\n" + " jcmd inspectheap datadump printflag agentProperties\n" + ); + return 1; + } + + int pid = atoi(argv[1]); + if (pid <= 0) { + fprintf(stderr, "%s is not a valid process ID\n", argv[1]); + return 1; + } + + return jattach(pid, argc - 2, argv + 2); +} + +#endif // JATTACH_VERSION