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

unhookarray={};

oepbreaks=1;
lethalbreaks=5;

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

function StartProcessInternal()
	UnhookArrayList();
	Start(false);
	local breaksatoep=0;
	unhook_inaction=true;
	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
			break;
		end;
	end;
	unhook_inaction=false;
end;

function StartProcess()
	local oepcounter=0;
	while true do
		StartProcessInternal();
		if break_where==0xf00 then
			oepcounter=oepcounter+1;
			WriteEx(string.format("Died while starting process %u of %u",oepcounter,lethalbreaks),true,true,0x0000ff);
			if oepcounter==lethalbreaks then
				WriteEx("Lethally died while starting process",true,true,0x0000ff);
				return -1;
			end;
		else
			break;
		end;
	end;
	SuspendAllOther();
--	WriteMem(0x7c97c0dc,0xffffffff,4);
	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;
ProcessDelphiInit();
RestoreImportRelocs();
if cut_sections==true then
	SaveResources();
end;
ProcessOverlay();
SaveFile();
Terminate();