gtxyzz

如何编写一段内存蠕虫?

gtxyzz 安全防护 2022-12-02 280浏览 0

我们怎么写一段代码,能够在程序内存里面不停移动?就是让shellcode代码能在内存中不停的复制自己,并且一直执行下去,也就是内存蠕虫。我们要把shellcode代码偏移出蠕虫长度再复制到蠕虫后面的内存中,然后执行。

如何编写一段内存蠕虫?

我们在实现过程中同时把前面同长度代码变成\x90,那个就是虫子走过的路,最终吃掉所有的内存。实现这个我们要知道shellcode长度,并且计算好shellcode每次移动的位置是多少。我们的shllcode以调用printf函数为例。

1. 写出printf程序

#include"stdio.h"
intmain()
{
printf("begin\n");
char*str="a=%d\n";

__asm{
moveax,5
pusheax
pushstr
moveax,0x00401070
calleax
addesp,8
ret
}
return0;
}

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

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

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

2. 编写蠕虫代码

insect:movbl,byteptrds:[eax+edx]
movbyteptrds:[eax+edx+20],bl
movbyteptrds:[eax+edx],0x90
incedx
cmpedx,20
jeee
jmpinsect

ee:addeax,20
pusheax
calleax
popeax
xoredx,edx
jmpinsect

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

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

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

3. 最终程序

#include"stdio.h"

charshellcode[]="\xB8\x05\x00\x00\x00\x50\xFF\x75\xFC\xB8\x70\x10\x40\x00\xFF\xD0\x83\x**\x08\xc3";
intmain()
{
printf("begin\n");
char*str="a=%d\n";


__asm{

leaeax,shellcode
pusheax
calleax
popeax
xoredx,edx

insect:movbl,byteptrds:[eax+edx]
movbyteptrds:[eax+edx+20],bl
movbyteptrds:[eax+edx],0x90
incedx
cmpedx,20
jeee
jmpinsect

ee:addeax,20
pusheax
calleax
popeax
xoredx,edx
jmpinsect



}


return0;
}

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

没有复制前:

如何编写一段内存蠕虫?

复制后:

如何编写一段内存蠕虫?

4. 总结

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

继续浏览有关 安全 的文章
发表评论