安全矩阵

 找回密码
 立即注册
搜索
查看: 2153|回复: 0

如何编写一段内存蠕虫?

[复制链接]

7

主题

23

帖子

111

积分

注册会员

Rank: 2

积分
111
发表于 2020-4-21 10:21:10 | 显示全部楼层 |阅读模式
我们怎么写一段代码,能够在程序内存里面不停移动?就是让shellcode代码能在内存中不停的复制自己,并且一直执行下去,也就是内存蠕虫。我们要把shellcode代码偏移出蠕虫长度再复制到蠕虫后面的内存中,然后执行。我们在实现过程中同时把前面同长度代码变成\x90,那个就是虫子走过的路,最终吃掉所有的内存。实现这个我们要知道shellcode长度,并且计算好shellcode每次移动的位置是多少。我们的shllcode以调用printf函数为例。
0×01 写出printf程序
  1. #include "stdio.h"
  2. int main()
  3. {
  4.         printf("begin\n");
  5.         char *str="a=%d\n";
  6.        
  7.         __asm{
  8.                 mov eax,5
  9.                 push eax
  10.                 push str
  11.                 mov eax,0x00401070  
  12.                 call eax
  13.                 add esp,8
  14.                 ret
  15.         }
  16.                 return 0;
  17. }
复制代码

0×00401070 是我机子上printf的地址,将自己机子上的printf地址更换一下就行,还要在最后加一个ret,因为执行完shellcode还要回到复制shellcode的代码执行。

上面汇编转成shellcode形式,shellcode为:

  1. char shellcode[]="\xB8\x05\x00\x00\x00\x50\xFF\x75\xFC\xB8\x70\x10\x40\x00\xFF\xD0\x83\x**\x08\xc3";
复制代码
0×02 编写蠕虫代码
  1. insect:mov bl,byte ptr ds:[eax+edx]
  2.            mov byte ptr ds:[eax+edx+20],bl
  3.            mov byte ptr ds:[eax+edx],0x90
  4.            inc edx
  5.            cmp edx,20
  6.            je ee
  7.            jmp insect
  8.           
  9. ee:                add eax,20
  10.                 push eax
  11.                 call eax
  12.                 pop eax
  13.                 xor edx,edx
  14.                 jmp insect
复制代码

shellcode长度是20,假设数据的地址是s,我们把数据复制到地址为s+20处,原来的数据变为0×90,表示数据曾经来过这里,insect段是用来复制数据用到,复制了20次,刚刚好把shellcode复制完。

因为shellcode相当于向下移动20位,所以我们要把eax加上20,还要把edx恢复成0,方便下次接着复制,然后去执行我们的shellcode,接着跳转到insect段继续执行,这是ee段干的事。

inscet和ee段加起来是复制我们的shellcode到其他地方,然后去执行shellcode,然后再复制,循环下去。

0×03 最终程序
  1. #include "stdio.h"

  2. char shellcode[]="\xB8\x05\x00\x00\x00\x50\xFF\x75\xFC\xB8\x70\x10\x40\x00\xFF\xD0\x83\x**\x08\xc3";
  3. int main()
  4. {
  5.         printf("begin\n");
  6.         char *str="a=%d\n";
  7.        
  8.        
  9.         __asm{
  10.                
  11.                 lea eax,shellcode
  12.                 push eax
  13.                 call eax
  14.                 pop eax
  15.                 xor edx,edx
  16.                        
  17. insect:mov bl,byte ptr ds:[eax+edx]
  18.            mov byte ptr ds:[eax+edx+20],bl
  19.            mov byte ptr ds:[eax+edx],0x90
  20.            inc edx
  21.            cmp edx,20
  22.            je ee
  23.            jmp insect
  24.           
  25. ee:                add eax,20
  26.                 push eax
  27.                 call eax
  28.                 pop eax
  29.                 xor edx,edx
  30.                 jmp insect


  31.                
  32.         }
  33.        
  34.        
  35.         return 0;
  36. }
复制代码

调试的时候找到shellcode位置,一步步调试能看见shellcode被复制,原来的转成0×90,并且printf还被执行

没有复制前:


复制后:

0×04 总结

我们要先计算出shellcode的长度,计算好shellcode每次移动的位置是多少,然后写出复制程序,并且还要有调转到复制后的shellcode首地址程序,执行复制后的shellcode,接着在复制再执行,循环下去,当然在一段内存里循环执行也可以,只要找到位置,跳转过去就行







回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|安全矩阵

GMT+8, 2024-3-29 03:06 , Processed in 0.013604 second(s), 18 queries .

Powered by Discuz! X4.0

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表