Windows NT 4.0 source code leak
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.

147 lines
3.8 KiB

4 years ago
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. SYSTEM_INFO SystemInfo;
  5. BOOL fVerbose;
  6. BOOL fUnprotect;
  7. BOOL fShowUsage;
  8. LONG
  9. RobustpUnprotectMemory(
  10. LPVOID BaseAddress,
  11. struct _EXCEPTION_POINTERS *ExceptionInfo
  12. );
  13. int _CRTAPI1
  14. main(
  15. int argc,
  16. char *argv[],
  17. char *envp[]
  18. )
  19. {
  20. HANDLE hModule;
  21. LPVOID BaseAddress;
  22. LPVOID MaxAddress;
  23. LPSTR DllName;
  24. PIMAGE_NT_HEADERS NtHeaders;
  25. char c, *p;
  26. GetSystemInfo(&SystemInfo);
  27. DllName = "kernel32.dll";
  28. fVerbose = FALSE;
  29. fUnprotect = FALSE;
  30. fShowUsage = FALSE;
  31. while (--argc) {
  32. p = *++argv;
  33. if (*p == '/' || *p == '-') {
  34. while (c = *++p)
  35. switch (toupper( c )) {
  36. case '?':
  37. fShowUsage = TRUE;
  38. goto showUsage;
  39. break;
  40. case 'U':
  41. fUnprotect = TRUE;
  42. break;
  43. case 'V':
  44. fVerbose = TRUE;
  45. break;
  46. case 'D':
  47. if (!argc--) {
  48. fShowUsage = TRUE;
  49. goto showUsage;
  50. }
  51. argv++;
  52. DllName = *argv;
  53. break;
  54. default:
  55. printf("ROBUST: Invalid switch - /%c\n", c );
  56. fShowUsage = TRUE;
  57. goto showUsage;
  58. break;
  59. }
  60. }
  61. }
  62. showUsage:
  63. if ( fShowUsage ) {
  64. fprintf(stderr,"usage: ROBUST\n" );
  65. fprintf(stderr," [-?] display this message\n" );
  66. fprintf(stderr," [-v] verbose messages)\n" );
  67. fprintf(stderr," [-u] attempt unprotect on access violation\n" );
  68. fprintf(stderr," [-d dllname] use dllname as target dll\n" );
  69. ExitProcess(1);
  70. }
  71. hModule = LoadLibrary(DllName);
  72. if ( !hModule ) {
  73. printf("ROBUST: Unable to load %s (%d)\n",DllName,GetLastError());
  74. ExitProcess(1);
  75. }
  76. BaseAddress = (LPVOID)hModule;
  77. NtHeaders = (PIMAGE_NT_HEADERS)((LPSTR)BaseAddress + ((PIMAGE_DOS_HEADER)BaseAddress)->e_lfanew);
  78. MaxAddress = (LPVOID)((LPSTR)BaseAddress + NtHeaders->OptionalHeader.SizeOfImage);
  79. printf("ROBUST: %s spans 0x%08x -> 0x%08x\n",DllName,BaseAddress,MaxAddress);
  80. while ( BaseAddress < MaxAddress ) {
  81. try {
  82. FillMemory(BaseAddress,SystemInfo.dwPageSize,0xfe);
  83. }
  84. except (RobustpUnprotectMemory(BaseAddress,GetExceptionInformation())) {
  85. ;
  86. }
  87. BaseAddress = (LPVOID)((LPSTR)BaseAddress + SystemInfo.dwPageSize);
  88. }
  89. TerminateProcess(GetCurrentProcess(),1);
  90. return 1;
  91. }
  92. LONG
  93. RobustpUnprotectMemory(
  94. LPVOID BaseAddress,
  95. struct _EXCEPTION_POINTERS *ExceptionInfo
  96. )
  97. {
  98. LPVOID FaultAddress;
  99. DWORD OldProtect;
  100. BOOL b;
  101. //
  102. // Attempt to make the page writable
  103. //
  104. b = FALSE;
  105. if ( fUnprotect ) {
  106. FaultAddress = (PVOID)(ExceptionInfo->ExceptionRecord->ExceptionInformation[1] & ~0x3);
  107. if ( ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION &&
  108. ExceptionInfo->ExceptionRecord->ExceptionInformation[0]
  109. ) {
  110. b = VirtualProtect(FaultAddress,SystemInfo.dwPageSize,PAGE_READWRITE,&OldProtect);
  111. if ( !b ) {
  112. b = VirtualProtect(FaultAddress,SystemInfo.dwPageSize,PAGE_EXECUTE_READWRITE,&OldProtect);
  113. }
  114. if ( fVerbose ) {
  115. printf("ROBUST: Write Fault at %x. %s (%d)\n",FaultAddress, b ? "Protection changed to writable." : "Unable to change protection.",GetLastError());
  116. }
  117. }
  118. }
  119. if ( b ) {
  120. return EXCEPTION_CONTINUE_EXECUTION;
  121. }
  122. else {
  123. return EXCEPTION_EXECUTE_HANDLER;
  124. }
  125. }