Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.82 KB | None | 0 0
  1. /*
  2.  
  3.  
  4. */
  5. /*
  6. This program accepts commands that cause it to perform virtual memory
  7. operations. The commands are read from standard input, but it is better
  8. to put the commands in a "script file" and use the operating system's
  9. command line to redirect the script file to this program's standard input
  10. (as in "c:\VMdriver < VMcmds.txt").
  11.  
  12. The commands that this program accepts are of the form
  13.  
  14. time, vmOp, vmAddress, units, access
  15.  
  16. The five parameters have the following meaning:
  17.  
  18. time - Seconds to wait after reading the command before performing the VM operation.
  19. vmOp - Code that represents the VM operation to perform.
  20. vmAddress - virtual memory address (in hex) where the VM operation is to be performed
  21. units - The number of units to use in the VM operation.
  22. For reserving memory, each unit represents 65536 bytes of memory.
  23. For committing memory, each unit represents 4096 bytes of memory.
  24. access - Code that represents the access protection.
  25.  
  26. The vmOp codes and their meanings are:
  27. 1 - Reserve a region of virtual memory.
  28. 2 - Commit a block of pages.
  29. 3 - Touch pages in a block.
  30. 4 - Lock a block of pages.
  31. 5 - Unlock a block of pages.
  32. 6 - Create a guard page.
  33. 7 - Decommit a block of pages.
  34. 8 - Release a region.
  35.  
  36. The access codes and their meaning are:
  37. 1 - PAGE_READONLY
  38. 2 - PAGE_READWRITE
  39. 3 - PAGE_EXECUTE
  40. 4 - PAGE_EXECUTE_READ
  41. 5 - PAGE_EXECUTE_READWRITE
  42. 6 - PAGE_NOACCESS
  43.  
  44. Most of the commands are described in the file
  45. "Virtual Memory from 'Beginning Windows NT Programming' by Julian Templeman.pdf".
  46. The only command not mentioned there is the "Touch pages in a block" command. This means
  47. that you should access (read) a memory location from each page in a specified block.
  48.  
  49. Be absolutely sure that you check for any errors created by the VM operations
  50. since you will be trying to cause errors.
  51.  
  52. This program should create a process that runs the program VMmapper.exe so that
  53. you can observe the memory operations as they happen. The program VMmapper takes
  54. a PID on its command line and then it repeatedly maps and displays (once a second)
  55. the virtual memory space of the process with that PID. This program should pass on
  56. the command line its own PID to the VMmapper program.
  57.  
  58. When this program has completed all of its operations, it goes into an infinite
  59. loop.
  60. */
  61. #include <windows.h>
  62. #include <stdio.h>
  63. #include <string.h>
  64.  
  65. // prototype for the function, defined below, that prints err messages
  66. void printError(char* functionName);
  67.  
  68. int main(int argc, char *argv[])
  69. {
  70. int time, vmOp, units, access;
  71. unsigned int vmAddress;
  72.  
  73. PROCESS_INFORMATION processInfo;
  74.  
  75. STARTUPINFO startInfo;
  76. ZeroMemory(&startInfo, sizeof(startInfo));
  77. startInfo.cb = sizeof(startInfo);
  78.  
  79. DWORD cPID = GetCurrentProcessId();
  80.  
  81. //Create the process
  82. char args[256];
  83. sprintf(args, " %d", cPID);
  84.  
  85. if( !CreateProcess("VMmapper.exe",args, NULL, NULL, FALSE,
  86. HIGH_PRIORITY_CLASS | CREATE_NEW_CONSOLE,
  87. NULL, NULL, &startInfo, &processInfo) )
  88. {
  89. printError("VMmapper.exe");
  90. }
  91.  
  92. Sleep(5000); // give VMmapper.exe time to start
  93.  
  94. // Process loop
  95. printf("next VM command: ");
  96. while(fscanf(stdin,"%d%d%x%d%d", &time, &vmOp, &vmAddress, &units, &access) != EOF)
  97. {
  98. switch(access)
  99. {
  100. case 1:
  101. access = PAGE_READONLY;
  102. break;
  103. case 2:
  104. access = PAGE_READWRITE;
  105. break;
  106. case 3:
  107. access = PAGE_EXECUTE;
  108. break;
  109. case 4:
  110. access = PAGE_EXECUTE_READ;
  111. break;
  112. case 5:
  113. access = PAGE_EXECUTE_READWRITE;
  114. break;
  115. case 6:
  116. access = PAGE_NOACCESS;
  117. break;
  118. }
  119. // wait until it is time to execute the command
  120. Sleep(time*1000);
  121.  
  122. // Parse the command and execute it
  123. switch (vmOp)
  124. {
  125. case 1: // Reserve a region
  126. if(VirtualAlloc((LPVOID)vmAddress, units*65536, MEM_RESERVE, access))
  127. {
  128. printf("Processed %d %d %x %d %d\n", time, vmOp, vmAddress, units, access);
  129. }
  130. else
  131. {
  132. printError("VirtualAlloc Memory Reserve failed.");
  133. }
  134.  
  135. break;
  136. case 2: // Commit a block of pages
  137. if(VirtualAlloc((LPVOID)vmAddress, units*4096, MEM_COMMIT, access))
  138. {
  139. printf("Processed %d %d %x %d %d\n", time, vmOp, vmAddress, units*4096, access);
  140. }
  141. else
  142. {
  143. printError("VirtualAlloc Memory Commit failed.");
  144. }
  145.  
  146. break;
  147. case 3: // Touch pages in a block
  148. *((int*)vmAddress) = 3;
  149. printf("%d",*((int*)vmAddress));
  150. printf("Processed %d %d %x %d %d\n", time, vmOp, vmAddress, units, access);
  151. break;
  152. case 4: // Lock a block of pages
  153. if(VirtualLock((LPVOID)vmAddress, units*4096))
  154. {
  155. printf("Processed %d %d %x %d %d\n", time, vmOp, vmAddress, units*4096, access);
  156. }
  157. else
  158. {
  159. printError("Virtual Lock failed");
  160. }
  161.  
  162. break;
  163. case 5: // Unlock a block of pages
  164. if(VirtualUnlock((LPVOID)vmAddress, units*4096))
  165. {
  166. printf("Processed %d %d %x %d %d\n", time, vmOp, vmAddress, units*4096, access);
  167. }
  168. else
  169. {
  170. printError("Virtual Unlock failed.");
  171. }
  172.  
  173. break;
  174. case 6: // Create a guard page
  175. if(VirtualAlloc((LPVOID)vmAddress, units*4096, MEM_RESERVE | MEM_COMMIT, access | PAGE_GUARD))
  176. {
  177. printf("Processed %d %d %x %d %d\n", time, vmOp, vmAddress, units*4096, access);
  178. }
  179. else
  180. {
  181. printError("VirtualAlloc Page Guard failed.");
  182. }
  183.  
  184. break;
  185. case 7: // Decommit a block of pages
  186. if(VirtualFree((LPVOID)vmAddress, units*4096, MEM_DECOMMIT))
  187. {
  188. printf("Processed %d %d %x %d %d\n", time, vmOp, vmAddress, units*4096, access);
  189. }
  190. else
  191. {
  192. printError("VirtualFree Memory Decommit failed.");
  193. }
  194.  
  195. break;
  196. case 8: // Release a region
  197. if(VirtualFree((LPVOID)vmAddress, units*65536, MEM_RELEASE))
  198. {
  199. printf("Processed %d %d %x %d %d\n", time, vmOp, vmAddress, units*65536, access);
  200. }
  201. else
  202. {
  203. printError("VirtualFree Memory Release failed.");
  204. }
  205.  
  206. break;
  207. }//switch
  208. printf("next VM command: ");
  209. }//while
  210.  
  211. while (1) Sleep(1000); // spin until killed
  212.  
  213. return 0;
  214. }//main
  215.  
  216.  
  217.  
  218. void printError(char* functionName)
  219. { LPVOID lpMsgBuf;
  220. int error_no;
  221. error_no = GetLastError();
  222. FormatMessage(
  223. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  224. NULL,
  225. error_no,
  226. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  227. (LPTSTR) &lpMsgBuf,
  228. 0,
  229. NULL
  230. );
  231. // Display the string.
  232. fprintf(stderr, "\n%s failed on error %d: ", functionName, error_no);
  233. fprintf(stderr, lpMsgBuf);
  234. MessageBox(NULL, lpMsgBuf, "Error", MB_OK);
  235. // Free the buffer.
  236. LocalFree( lpMsgBuf );
  237. }//printError
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement