技术讨论 | 如何编写一段内存蠕虫?
2015 年 12 月 11 日
我们怎么写一段代码,能够在程序内存里面不停移动?就是让shellcode代码能在内存中不停的复制自己,并且一直执行下去,也就是内存蠕虫。我们要把shellcode代码偏移出蠕虫长度再复制到蠕虫后面的内存中,然后执行。我们在实现过程中同时把前面同长度代码变成\x90,那个就是虫子走过的路,最终吃掉所有的内存。实现这个我们要知道shellcode长度,并且计算好shellcode每次移动的位置是多少。我们的shllcode以调用printf函数为例。
0×01 写出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; }
0×00401070 是我机子上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";
0×02 编写蠕虫代码
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处,原来的数据变为0×90,表示数据曾经来过这里,insect段是用来复制数据用到,复制了20次,刚刚好把shellcode复制完。
因为shellcode相当于向下移动20位,所以我们要把eax加上20,还要把edx恢复成0,方便下次接着复制,然后去执行我们的shellcode,接着跳转到insect段继续执行,这是ee段干的事。
inscet和ee段加起来是复制我们的shellcode到其他地方,然后去执行shellcode,然后再复制,循环下去。
0×03 最终程序
#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被复制,原来的转成0×90,并且printf还被执行
没有复制前:
复制后: