2025年3月31日 星期一 乙巳(蛇)年 正月初一 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > 编程箴言

技术讨论 | 如何编写一段内存蠕虫?

时间:08-22来源:FreeBuf.COM作者:好好学习er点击数:78

我们怎么写一段代码,能够在程序内存里面不停移动?就是让shellcode代码能在内存中不停的复制自己,并且一直执行下去,也就是内存蠕虫。我们要把shellcode代码偏移出蠕虫长度再复制到蠕虫后面的内存中,然后执行。我们在实现过程中同时把前面同长度代码变成\x90,那个就是虫子走过的路,最终吃掉所有的内存。实现这个我们要知道shellcode长度,并且计算好shellcode每次移动的位置是多少。我们的shllcode以调用printf函数为例。

0x01 写出printf程序

  • #include "stdio.h"
  • int main()
  • {
  • printf("begin\n");
  • char *str="a=%d\n";
  • __asm{
  • mov eax,5
  • push eax
  • push str
  • mov eax,0x00401070
  • call eax
  • add esp,8
  • ret
  • }
  • return 0;
  • }

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

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

  • char shellcode[]="\xB8\x05\x00\x00\x00\x50\xFF\x75\xFC\xB8\x70\x10\x40\x00\xFF\xD0\x83\x**\x08\xc3";

0x02 编写蠕虫代码

  • insect:mov bl,byte ptr ds:[eax+edx]
  • mov byte ptr ds:[eax+edx+20],bl
  • mov byte ptr ds:[eax+edx],0x90
  • inc edx
  • cmp edx,20
  • je ee
  • jmp insect
  • ee: add eax,20
  • push eax
  • call eax
  • pop eax
  • xor edx,edx
  • jmp insect

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

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

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

0x03 最终程序

  • #include "stdio.h"
  • char shellcode[]="\xB8\x05\x00\x00\x00\x50\xFF\x75\xFC\xB8\x70\x10\x40\x00\xFF\xD0\x83\x**\x08\xc3";
  • int main()
  • {
  • printf("begin\n");
  • char *str="a=%d\n";
  • __asm{
  • lea eax,shellcode
  • push eax
  • call eax
  • pop eax
  • xor edx,edx
  • insect:mov bl,byte ptr ds:[eax+edx]
  • mov byte ptr ds:[eax+edx+20],bl
  • mov byte ptr ds:[eax+edx],0x90
  • inc edx
  • cmp edx,20
  • je ee
  • jmp insect
  • ee: add eax,20
  • push eax
  • call eax
  • pop eax
  • xor edx,edx
  • jmp insect
  • }
  • return 0;
  • }

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

没有复制前:

复制后:

0x04 总结

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

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门