#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);
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;
}
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