]> git.sven.stormbind.net Git - sven/jattach.git/blobdiff - src/windows/jattach.c
New upstream version 2.1
[sven/jattach.git] / src / windows / jattach.c
index b43e2f875e2aeaef9f692c4282859ee7e0ce5652..1318a94541a2ef6706bde4d361cc81c4d28385f3 100644 (file)
@@ -16,7 +16,8 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <Windows.h>
+#include <windows.h>
+#include <sddl.h>
 
 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 <pid> <cmd> [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 <pid> <cmd> [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