--x86 and x64 compatible
--this script was intended to be private, where the fuck did you get it?
--well, I guess you one of the guys whom I trust, don't fail me

unhookarray={};

fast_processing=false;
oepbreaks=1;
lethalbreaks=5;

function UnhookArrayList()
	local z=1;
	while true do
		if unhookarray[z]==nil then
			break;
		end;
		AddModuleToUnhookList(unhookarray[z]);
		z=z+1;
	end;
end;

function StartProcess()
	local oepcounter=0;
	while true do
		Start(false);
		local breaksatoep=0;
		if fast_processing==true then
			unhook_inaction=true;
		end;
		UnhookArrayList();
		AddBreak(jmp_to_oep+victim_base,3,0);
		EnableBreak(jmp_to_oep+victim_base);
		while true do
			Continue(true);
			if break_where==0xf40 then
				breaksatoep=breaksatoep+1;
				if breaksatoep==oepbreaks then
					break;
				else
					TraceAndReplace(EIP);
				end;
			elseif break_where==0xf00 then
				oepcounter=oepcounter+1;
				WriteEx(string.format("Died while tracing to OEP %u",oepcounter),true,true,0x0000ff);
				break;
			end;
		end;
		if break_where==0xf00 then
			if oepcounter==lethalbreaks then
				WriteEx("Lethally died while tracing to OEP",true,true,0x0000ff);
				return -1;
			end;
		else
			break;
		end;
	end;
	if fast_processing==true then
		unhook_inaction=false;
	end;
	SuspendAllOther();
	return 0;
end;

if StartProcess()==-1 then
	return -1;
end;
DeleteBreak(OEP);
temp=NextInstr(NextInstr(EIP));
AddBreak(temp,1,0);
EnableBreak(temp);
oldbytes=ReadMem(EIP,2);
oldeip=EIP;

temp=0xFEEB;
WriteMem(EIP,temp,2);
WriteLog("Eee, attach to me, baby!");

Continue(true);
Pause("Detach in debugger and press OK");

WriteMem(oldeip,oldbytes,2);
EIP=oldeip;

Dump();
import_meth=1;
if cut_sections==true then
	ProcessResources();
	CutSections();
	ProcessExport();
	ProcessTLS();
end;
RestoreImportRelocs();
if cut_sections==true then
	SaveResources();
end;
SaveFile();
Terminate();