// main cure file (debug version, includes comments)
// Written by Vitaliy Kamlyuk, Kaspersky Lab, 2007
// for DefCon 15 presentation
// Vitaliy.Kamlyuk@gmail.com

//Filling work area with zeroes
f 100 900 0
f 1000 1B00 0

//Import libraries
e 1300 "KERNEL32.dll"

//Import names
e 1402 "ExitProcess"
e 1412 "CreateFileA"
e 1422 "SetFilePointer"
e 1432 "ReadFile"
e 1442 "WriteFile"
e 1452 "CloseHandle"
e 1462 "LocalAlloc"
e 1472 "LocalLock"
e 1482 "LocalUnlock"
e 1492 "LocalFree"

//Import Lookup Table
e 1200 00,14,00,00 ,10,14,00,00 ,20,14,00,00 ,30,14,00,00
e 1210 40,14,00,00 ,50,14,00,00 ,60,14,00,00 ,70,14,00,00
e 1220 80,14,00,00 ,90,14,00,00 ,00,00,00,00

//Import Address Table
e 1000 00,14,00,00 ,10,14,00,00 ,20,14,00,00 ,30,14,00,00
e 1010 40,14,00,00 ,50,14,00,00 ,60,14,00,00 ,70,14,00,00
e 1020 80,14,00,00 ,90,14,00,00 ,00,00,00,00

//Import Directory Table
e 1100 00,12,0,0 ,0,0,0,0 ,0,0,0,0 ,00,13,0,0 ,00,10,0,0
//+1 Zero filled field is kept in mind ;)

//1500-1600: user variables
//filein
e 1500 "file.in",0
//fileout
e 1510 "tmp_part1.cab",0
//hfilein
e 1520 0
//hfileout
e 1530 0
//Var1
e 1540 0
//Var2
e 1550 0
//hGlobMem
e 1560 0
//pMem
e 1570 0
//fileout2
e 1580 "tmp_part2.bin",0
//hfileout
e 1590 0
//dwCleanImgSize
e 15A0 0
//dwInfectionSize
e 15B0 0



//User code (aligned by instructions with trailing nops by 16 bytes)
f 1600 1B00 90

//push 0 ,push 80, push 3
e 1600 68,00,00,00,00 ,68,80,00,00,00 ,68,03,00,00,00 ,90
//push 0 ,push 3 ,push C0000000
e 1610 68,00,00,00,00 ,68,03,00,00,00 ,68,00,00,00,c0 ,90
//push 401500 ,call CreateFileA ,mov 401520,eax
e 1620 68,00,15,40,00 ,ff,15,04,10,40,00 ,a3,20,15,40,00
//push 2 ,push 0 ,push -8 ,push eax
e 1630 68,02,00,00,00 ,68,00,00,00,00 ,68,f8,ff,ff,ff ,50
//call SetFilePointer ,push 0 ,push 401550 (var2)
e 1640 ff,15,08,10,40,00 ,68,00,00,00,00 ,68,50,15,40,00
//push 4 ,push 401540 (Var1) , mov eax,[401520](hfilein), push eax
e 1650 68,04,00,00,00 ,68,40,15,40,00 ,a1,20,15,40,00 ,50
//call ReadFile ,mov eax,var1 ,cmp eax,2c8c
e 1660 ff,15,0C,10,40,00 ,a1,40,15,40,00 ,3d,8c,2c,00,00 

//jne 47A ,xor eax,eax ,sub eax,[401540](var1)
e 1670 0f,85,7A,04,00,00 ,66,33,c0 ,2b,05,40,15,40,00 
//sub eax,-0x10, push 2
e 1680 83,e8,10 ,68,02,00,00,00
//push 0, push eax, mov eax,[401520](hfilein) ,push eax
e 1690 68,00,00,00,00 ,50 ,a1,20,15,40,00 ,50
//call SetFilePointer ,push 0 ,push 401550(var2)
e 16A0 ff,15,08,10,40,00 ,68,00,00,00,00  ,68,50,15,40,00
//push 4, push 401540(Var1) ,mov eax,[401520](hfilein) ,push eax
e 16B0 68,04,00,00,00 ,68,40,15,40,00 ,a1,20,15,40,00 ,50
//call ReadFile
e 16C0 ff,15,0C,10,40,00

//push 0 ,push 401550(var2)
e 16D0 68,00,00,00,00  ,68,50,15,40,00
//push 4, push 4015B0(dwInfectionSize) ,mov eax,[401520](hfilein) ,push eax
e 16E0 68,04,00,00,00 ,68,B0,15,40,00 ,a1,20,15,40,00 ,50
//call ReadFile
e 16F0 ff,15,0C,10,40,00

//xor eax,eax ,sub eax,[401540](var1) ,sub eax,8
e 1700 66,33,c0 ,2b,05,40,15,40,00 ,83,e8,08
e 1710 68,01,00,00,00
//push 0, push eax, mov eax,[401520] (hfilein) ,push eax
e 1720 68,00,00,00,00 ,50 ,a1,20,15,40,00 ,50
//call SetFilePointer ,mov dwCleanImgSize,eax ,mov eax,Var1
e 1730 ff,15,08,10,40,00 ,a3,a0,15,40,00 ,a1,40,15,40,00 
//push eax, push 0
e 1740 50 ,68,00,00,00,00
//call LocalAlloc ,mov 401560(hGlobMem),eax ,push eax
e 1750 ff,15,18,10,40,00 ,a3,60,15,40,00 ,50
//call LocalLock ,mov 401570(pMem),eax ,push 0
e 1760 ff,15,1C,10,40,00 ,a3,70,15,40,00 ,68,00,00,00,00
//push 401550(var2) ,mov eax,Var1 ,push eax
e 1770 68,50,15,40,00 ,a1,40,15,40,00 ,50 
//mov eax,pMem ,push eax
e 1780 a1,70,15,40,00 ,50
//mov eax,[401520](hfilein) ,push eax ,call ReadFile
e 1790 a1,20,15,40,00 ,50 ,ff,15,0C,10,40,00
//mov eax,pMem ,mov d,[eax],"MSCF"
e 17A0 a1,70,15,40,00 ,C7,00,4D,53,43,46

//push 0 ,push 80, push 2
e 17B0 68,00,00,00,00 ,68,80,00,00,00 ,68,02,00,00,00 ,90
//push 0 ,push 3 ,push 40000000
e 17C0 68,00,00,00,00 ,68,03,00,00,00 ,68,00,00,00,40 ,90
//push 401510 ,call CreateFileA ,mov 401530(hfileout),eax
e 17D0 68,10,15,40,00 ,ff,15,04,10,40,00 ,a3,30,15,40,00

//push 0 ,push var2 ,mov eax,[401540](var1), push eax
e 17E0 68,00,00,00,00 ,68,50,15,40,00 ,a1,40,15,40,00 ,50
//mov eax,pMem, push eax ,mov eax,hfileout ,push eax
e 17F0 a1,70,15,40,00 ,50 ,a1,30,15,40,00 ,50
//call WriteFile
e 1800 ff,15,10,10,40,00

//mov eax,hfileout ,push eax ,call CloseHandle
e 1810 a1,30,15,40,00 ,50 ,ff,15,14,10,40,00 

//mov eax,hGlobMem, push eax, call LocalUnlock
e 1820 a1,60,15,40,00 ,50 ,ff,15,20,10,40,00
//mov eax,hGlobMem, push eax, call LocalFree
e 1830 a1,60,15,40,00 ,50 ,ff,15,24,10,40,00

//push 0, push 0 ,mov eax,dwInfectionSize ,push eax
e 1840 68,00,00,00,00 ,68,00,00,00,00 ,a1,B0,15,40,00 ,50
//mov eax,hfilein ,push eax ,call SetFilePointer
e 1850 a1,20,15,40,00 ,50 ,ff,15,08,10,40,00

//mov ebx,dwInfectionSize ,mov eax,dwCleanImgSize ,sub eax,abx
e 1860 8b,1D,B0,15,40,00 ,a1,A0,15,40,00 ,2b,c3

//mov dwCleanImgSize,eax  ,push eax ,push 0
e 1870 a3,a0,15,40,00 ,50 ,68,00,00,00,00

//call LocalAlloc ,mov 401560(hGlobMem),eax ,push eax
e 1880 ff,15,18,10,40,00 ,a3,60,15,40,00 ,50
//call LocalLock ,mov 401570(pMem),eax ,push 0
e 1890 ff,15,1C,10,40,00 ,a3,70,15,40,00 ,68,00,00,00,00
//push 401550(var2) ,mov eax,dwCleanImgSize
e 18A0 68,50,15,40,00 ,a1,A0,15,40,00 ,50 
//mov eax,pMem ,push eax
e 18B0 a1,70,15,40,00 ,50
//mov eax,[401520](hfilein) ,push eax ,call ReadFile
e 18C0 a1,20,15,40,00 ,50 ,ff,15,0C,10,40,00

//push 0 ,push 80, push 2
e 18D0 68,00,00,00,00 ,68,80,00,00,00 ,68,02,00,00,00 ,90
//push 0 ,push 3 ,push 40000000
e 18E0 68,00,00,00,00 ,68,03,00,00,00 ,68,00,00,00,40 ,90
//push 401510 ,call CreateFileA ,mov 401530(hfileout),eax
e 18F0 68,80,15,40,00 ,ff,15,04,10,40,00 ,a3,30,15,40,00

//push 0 ,push var2 ,mov eax,dwCleanImgSize, push eax
e 1910 68,00,00,00,00 ,68,50,15,40,00 ,a1,A0,15,40,00 ,50
//mov eax,pMem, push eax ,mov eax,hfileout ,push eax
e 1920 a1,70,15,40,00 ,50 ,a1,30,15,40,00 ,50
//call WriteFile
e 1930 ff,15,10,10,40,00

//mov eax,hfileout ,push eax ,call CloseHandle
e 1940 a1,30,15,40,00 ,50 ,ff,15,14,10,40,00

//mov eax,hGlobMem, push eax, call LocalUnlock
e 1950 a1,60,15,40,00 ,50 ,ff,15,20,10,40,00
//mov eax,hGlobMem, push eax, call LocalFree
e 1960 a1,60,15,40,00 ,50 ,ff,15,24,10,40,00

//mov eax,hfilein ,push eax ,call CloseHandle
e 1970 a1,20,15,40,00 ,50 ,ff,15,14,10,40,00 

//xor eax,eax ,push eax, call ExitProcess
e 1AF0 33,c0 ,50 ,ff,15,00,10,40,00

//Moving section right after the header
m 1000 1B00 300

//EXE header
e 100 4D,5A
e 13C 40
e 140 50,45,0,0 ,4c,01 ,01
e 154 e0,00 ,0f,01 ,0b,01
e 168 00,16
e 174 00,00,40,00 ,00,10,00,00 ,00,02,00,00 ,04,00 ,0,0 ,0,0 ,0,0 ,04,00 
e 190 00,20,00,00 ,00,02,00,00 ,0,0 ,0,0 ,02,00
e 1B4 10,00
e 1C0 00,11,00,00 ,3c,00,00,00
e 240 00,10,00,00 ,00,10,00,00 ,00,0B,00,00 ,00,02,00,00
e 25C 20,00,00,e0

//Saving to file
n cure.bin
rcx
D00
w

//Quiting
q
