Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.1 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define STACKSIZE 32768
  5. typedef BOOL (* LPDEBUG_BREAK_PROCESS_ROUTINE) (
  6. HANDLE Process
  7. );
  8. VOID
  9. DebugPriv(
  10. VOID
  11. )
  12. {
  13. HANDLE Token ;
  14. UCHAR Buf[ sizeof( TOKEN_PRIVILEGES ) + sizeof( LUID_AND_ATTRIBUTES ) ];
  15. UCHAR Buf2[ sizeof( Buf ) ];
  16. PTOKEN_PRIVILEGES Privs ;
  17. PTOKEN_PRIVILEGES NewPrivs ;
  18. DWORD size ;
  19. if (OpenProcessToken( GetCurrentProcess(),
  20. MAXIMUM_ALLOWED,
  21. &Token )) {
  22. Privs = (PTOKEN_PRIVILEGES) Buf ;
  23. Privs->PrivilegeCount = 1 ;
  24. Privs->Privileges[0].Luid.LowPart = 20L ;
  25. Privs->Privileges[0].Luid.HighPart = 0 ;
  26. Privs->Privileges[0].Attributes = SE_PRIVILEGE_ENABLED ;
  27. NewPrivs = (PTOKEN_PRIVILEGES) Buf2 ;
  28. AdjustTokenPrivileges( Token,
  29. FALSE,
  30. Privs,
  31. sizeof( Buf2 ),
  32. NewPrivs,
  33. &size );
  34. CloseHandle( Token );
  35. }
  36. }
  37. int
  38. __cdecl
  39. main(
  40. int argc,
  41. char **argv
  42. )
  43. {
  44. LPTHREAD_START_ROUTINE DbgBreakPoint;
  45. LPDEBUG_BREAK_PROCESS_ROUTINE DebugBreakProcessRoutine;
  46. HANDLE ntdll, kernel32;
  47. ULONG ProcessId;
  48. ULONG ThreadId;
  49. HANDLE Process;
  50. HANDLE Thread;
  51. if (argc != 2) {
  52. usage:
  53. fprintf(stderr, "usage: breakin <pid>\n");
  54. exit(1);
  55. }
  56. ProcessId = atoi(argv[1]);
  57. if (ProcessId == 0) {
  58. goto usage;
  59. }
  60. DebugPriv();
  61. Process = OpenProcess(
  62. PROCESS_ALL_ACCESS,
  63. FALSE,
  64. ProcessId
  65. );
  66. if (Process) {
  67. kernel32 = GetModuleHandle("kernel32.dll");
  68. if (kernel32) {
  69. DebugBreakProcessRoutine = (LPDEBUG_BREAK_PROCESS_ROUTINE)GetProcAddress(kernel32, "DebugBreakProcess");
  70. if (DebugBreakProcessRoutine) {
  71. if (!(*DebugBreakProcessRoutine)(Process)) {
  72. printf("DebugBreakProcess failed %d\n", GetLastError());
  73. }
  74. CloseHandle(Process);
  75. return 0;
  76. }
  77. }
  78. ntdll = GetModuleHandle("ntdll.dll");
  79. if (ntdll) {
  80. DbgBreakPoint = (LPTHREAD_START_ROUTINE)GetProcAddress(ntdll, "DbgBreakPoint");
  81. if (DbgBreakPoint) {
  82. Thread = CreateRemoteThread(
  83. Process,
  84. NULL,
  85. STACKSIZE,
  86. DbgBreakPoint,
  87. NULL,
  88. 0,
  89. &ThreadId
  90. );
  91. if (Thread){
  92. CloseHandle(Thread);
  93. }
  94. }
  95. }
  96. CloseHandle(Process);
  97. } else {
  98. printf("Open process failed %d\n", GetLastError());
  99. }
  100. return 0;
  101. }