Leaked source code of windows server 2003
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.

290 lines
7.1 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <stdio.h>
  4. ULONG ProcessNumber = 1;
  5. #define DbgPrint printf
  6. ULONG
  7. fork ();
  8. __cdecl
  9. main(
  10. )
  11. {
  12. LONG i, j;
  13. PULONG p4, p3, p2, p1, Ro3, Noaccess;
  14. SIZE_T Size1, Size2, Size3, SizeRo3, SizeNoaccess;
  15. NTSTATUS status;
  16. HANDLE CurrentProcessHandle;
  17. ULONG id = 0;
  18. ULONG OldProtect;
  19. CurrentProcessHandle = NtCurrentProcess();
  20. for(i=0;i<3;i++){
  21. DbgPrint("Hello World...\n\n");
  22. }
  23. DbgPrint("allocating virtual memory\n");
  24. p1 = (PULONG)NULL;
  25. Size1 = 30 * 4096;
  26. status = NtAllocateVirtualMemory (CurrentProcessHandle, (PVOID *)&p1,
  27. 0, &Size1, MEM_COMMIT, PAGE_READWRITE);
  28. DbgPrint("created vm1 status %X start %p size %lx\n",
  29. status, p1, Size1);
  30. p2 = (PULONG)NULL;
  31. Size2 = 16 * 4096;
  32. status = NtAllocateVirtualMemory (CurrentProcessHandle, (PVOID *)&p2,
  33. 0, &Size2, MEM_COMMIT, PAGE_READWRITE);
  34. DbgPrint("created vm2 status %X start %p size %lx\n",
  35. status, p2, Size2);
  36. id = fork () + id;
  37. DbgPrint("fork complete id %lx\n",id);
  38. p3 = p1 + 8 * 1024;
  39. Size3 = 16 * 4096;
  40. DbgPrint("deleting va from %p for %lx bytes\n",p3, Size3);
  41. status = NtFreeVirtualMemory (CurrentProcessHandle,(PVOID *)&p3, &Size3,
  42. MEM_RELEASE);
  43. DbgPrint("free vm status %X start %p size %lx\n",
  44. status, p3, Size3);
  45. p3 = (PULONG)NULL;
  46. Size3 = 50 * 4096;
  47. DbgPrint("allocating 50 pages of vm\n");
  48. status = NtAllocateVirtualMemory (CurrentProcessHandle, (PVOID *)&p3,
  49. 0, &Size3, MEM_COMMIT, PAGE_READWRITE);
  50. DbgPrint("created vm3 status %X start %p size %lx\n",
  51. status, p3, Size3);
  52. Ro3 = (PULONG)NULL;
  53. SizeRo3 = 393933;
  54. status = NtAllocateVirtualMemory (CurrentProcessHandle, (PVOID *)&Ro3,
  55. 0, &SizeRo3, MEM_COMMIT, PAGE_READONLY);
  56. DbgPrint("created vm4 status %X start %p size %lx\n",
  57. status, Ro3, SizeRo3);
  58. *p3 = *Ro3;
  59. p1 = p3;
  60. p2 = ((PULONG)((PUCHAR)p3 + Size3));
  61. p4 = p1;
  62. j = 0;
  63. while (p3 < p2) {
  64. j += 1;
  65. if (j % 8 == 0) {
  66. if (*p4 != (ULONG)((ULONG_PTR)p4)) {
  67. DbgPrint("bad value in cell %p value is %lx\n",p4, *p4);
  68. }
  69. p4 += 1;
  70. *p4 = (ULONG)((ULONG_PTR)p4);
  71. p4 = p4 + 1026;
  72. }
  73. *p3 = (ULONG)((ULONG_PTR)p3);
  74. p3 += 1027;
  75. }
  76. p3 = p1;
  77. //
  78. // Protect page as no access.
  79. //
  80. Noaccess = NULL;
  81. SizeNoaccess = 200*4096;
  82. status = NtAllocateVirtualMemory (CurrentProcessHandle, (PVOID *)&Noaccess,
  83. 0, &SizeNoaccess, MEM_COMMIT, PAGE_READWRITE);
  84. DbgPrint("created vm5 status %X start %p size %lx\n",
  85. status, Ro3, SizeRo3);
  86. //
  87. // Touch all the pages.
  88. //
  89. RtlZeroMemory(Noaccess, SizeNoaccess);
  90. *Noaccess = 91;
  91. Size1 = 30 * 4097;
  92. status = NtProtectVirtualMemory (CurrentProcessHandle, (PVOID *)&Noaccess,
  93. &Size1, PAGE_NOACCESS,
  94. &OldProtect);
  95. DbgPrint("protected VM1 status %X, base %p, size %lx, old protect %lx\n",
  96. status, p1, Size1, OldProtect);
  97. DbgPrint("forking a second time\n");
  98. id = fork () + id;
  99. DbgPrint("fork2 complete id %lx\n",id);
  100. DbgPrint("changing page protection\n");
  101. Size1 = 9000;
  102. OldProtect = *p3;
  103. status = NtProtectVirtualMemory (CurrentProcessHandle, (PVOID *)&p3,
  104. &Size1, PAGE_EXECUTE_READWRITE | PAGE_NOCACHE,
  105. &OldProtect);
  106. DbgPrint("protected VM2 status %X, base %p, size %lx, old protect %lx\n",
  107. status, p1, Size1, OldProtect);
  108. status = NtProtectVirtualMemory (CurrentProcessHandle, (PVOID *)&Ro3,
  109. &Size1, PAGE_READONLY | PAGE_NOCACHE, &OldProtect);
  110. DbgPrint("protected VM3 status %X, base %p, size %lx, old protect %lx\n",
  111. status, Ro3, Size1, OldProtect);
  112. p1 += 1;
  113. while (p3 < p2) {
  114. *p1 = (ULONG)((ULONG_PTR)p1);
  115. if (*p3 != (ULONG)((ULONG_PTR)p3)) {
  116. DbgPrint("bad value in cell %p value is %lx\n",p3, *p3);
  117. }
  118. p3 += 1027;
  119. p1 += 1027;
  120. }
  121. DbgPrint("trying noaccess test\n");
  122. try {
  123. if (*Noaccess != 91) {
  124. DbgPrint("*************** FAILED NOACCESS TEST 1 *************\n");
  125. }
  126. } except (EXCEPTION_EXECUTE_HANDLER) {
  127. if (GetExceptionCode() != STATUS_ACCESS_VIOLATION) {
  128. DbgPrint("*************** FAILED NOACCESS TEST 2 *************\n");
  129. }
  130. }
  131. //
  132. // Make no access page accessable.
  133. //
  134. status = NtProtectVirtualMemory (CurrentProcessHandle, (PVOID *)&Noaccess,
  135. &Size1, PAGE_READWRITE,
  136. &OldProtect);
  137. if (*Noaccess != 91) {
  138. DbgPrint("*************** FAILED NOACCESS TEST 3 *************\n");
  139. }
  140. DbgPrint("that's all process %lx\n", id);
  141. NtTerminateProcess(NtCurrentProcess(),STATUS_SUCCESS);
  142. return 0;
  143. }
  144. ULONG
  145. fork ()
  146. {
  147. LONG i;
  148. PULONG Foo;
  149. NTSTATUS status;
  150. HANDLE CurrentProcessHandle;
  151. HANDLE ProcessHandle;
  152. CONTEXT ThreadContext;
  153. CLIENT_ID Cid1;
  154. HANDLE Thread1;
  155. LARGE_INTEGER DelayTime;
  156. PINITIAL_TEB Teb;
  157. CurrentProcessHandle = NtCurrentProcess();
  158. DbgPrint("creating new process\n");
  159. status = NtCreateProcess(
  160. &ProcessHandle,
  161. PROCESS_ALL_ACCESS, //DesiredAccess,
  162. NULL, //ObjectAttributes,
  163. CurrentProcessHandle, //ParentProcess
  164. TRUE, //InheritObjectTable,
  165. NULL, //SectionHandle
  166. NULL, //DebugPort OPTIONAL,
  167. NULL //ExceptionPort OPTIONAL
  168. );
  169. DbgPrint("status from create process %lx\n",status);
  170. if (!NT_SUCCESS(status)) {
  171. return 0;
  172. }
  173. ThreadContext.ContextFlags = CONTEXT_FULL;
  174. status = NtGetContextThread (NtCurrentThread(), &ThreadContext);
  175. DbgPrint("status from get context %lx\n",status);
  176. if (status == 0) {
  177. #ifdef i386
  178. ThreadContext.Eax = 0x7777;
  179. ThreadContext.Esp -= 0x18;
  180. Foo = (PULONG)ThreadContext.Esp;
  181. DbgPrint("stack value is %lx\n",*Foo);
  182. #endif
  183. Teb= (PINITIAL_TEB) NtCurrentTeb ();
  184. status = NtCreateThread(
  185. &Thread1,
  186. THREAD_ALL_ACCESS,
  187. NULL,
  188. ProcessHandle,
  189. &Cid1,
  190. &ThreadContext,
  191. Teb,
  192. FALSE
  193. );
  194. // DelayTime.HighPart = -1;
  195. // DelayTime.LowPart = 0;
  196. // NtDelayExecution (FALSE, &DelayTime);
  197. return 0;
  198. } else {
  199. ProcessNumber += ProcessNumber;
  200. return ProcessNumber;
  201. }
  202. }