Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <errno.h>
  4.  
  5. #if defined(__linux__)
  6.  
  7. #include <unistd.h>
  8. #define GETPID 39
  9.  
  10. pid_t
  11. getpid_syscall (void)
  12. {
  13. pid_t pid = syscall(GETPID);
  14. return pid;
  15. }
  16.  
  17. #elif defined(_WIN32)
  18.  
  19. #include <windows.h>
  20. #include <winternl.h>
  21. #include <conio.h>
  22.  
  23. /* we define a pid integer to be of DWORD size */
  24. typedef DWORD pid_t;
  25.  
  26. __declspec(noinline)
  27. __declspec(naked)
  28. NTSTATUS
  29. CallNtQueryInformationProcess(HANDLE ProcessHandle, PROCESSINFOCLASS ProcessInformationClass, PVOID ProcessInformation, ULONG ProcessInformationLength, PULONG ReturnLength)
  30. {
  31. __asm {
  32. mov eax, 0x00a1 /* TODO: syscall nummer hier */
  33. mov edx, 0x7FFE0300 /* KUSER_SHARED_DATA syscall stub */
  34. call dword ptr [edx] /* call the stub code */
  35.  
  36. ret
  37. }
  38. }
  39.  
  40. pid_t
  41. getpid_syscall (void)
  42. {
  43. /* TODO: call CallNtQueryInformationProcess with the right parameters and
  44. * extract and return the process id
  45. *
  46. * see: https://msdn.microsoft.com/en-us/library/windows/desktop/ms684280(v=vs.85).aspx
  47. */
  48. return CallNtQueryInformationProcess();
  49. }
  50.  
  51. #else
  52. /* if neither linux, nor windows, we can't proceed. */
  53. #error neither windows nor linux?
  54. #endif
  55.  
  56. int
  57. main (void)
  58. {
  59. /* produce user mode API pid */
  60. pid_t pid_getpid = getpid();
  61. /* produce syscall API pid */
  62. pid_t pid_syscall = getpid_syscall();
  63.  
  64. /* error handling is always useful */
  65. if (pid_syscall <= 0)
  66. {
  67. perror("getpid_syscall");
  68. return 1;
  69. }
  70.  
  71. /* print both and exit */
  72. printf("%d\n%d\n", pid_getpid, pid_syscall);
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement