unit unrandomizer; //Todo, update with new tech {$MODE Delphi} interface uses windows, CEFuncProc,dialogs,classes,comctrls,LCLIntf,sysutils,formsettingsunit, NewKernelHandler, commonTypeDefs, MemFuncs; type Tunrandomize=class(tthread) private processid,processhandle: dword; originalcode: array of record address: ptruint; code: array of byte; end; threaddone: boolean; procedure unrandomize64bit; procedure save(address:ptruint; buf: pointer; size: integer); procedure done; public progressbar: tprogressbar; procedure execute; override; procedure restore; procedure showaddresses; destructor destroy; override; end; implementation uses MainUnit, ProcessHandlerUnit, symbolhandler, autoassembler; resourcestring rsTheFollowingAddressesGotChanged = 'The following addresses got changed'; rsTheUnrandomizerWillCurrentlyNotWorkOn64BitApplicat = 'The unrandomizer will currently not work on 64-bit applications'; type tcodereplace=array of byte; destructor TUnrandomize.destroy; begin if (processid=processhandlerunit.ProcessID) and (processhandle=processhandlerunit.ProcessHandle) then restore; inherited destroy; end; procedure TUnrandomize.done; begin try if mainform<>nil then begin progressbar.Hide; progressbar.Free; mainform.cbUnrandomizer.Enabled:=true; end; except end; threaddone:=true; end; procedure TUnrandomize.showaddresses; var s: string; i: integer; e: integer; begin if threaddone then begin s:=rsTheFollowingAddressesGotChanged+':'; for i:=0 to length(originalcode)-1 do s:=s+#13#10+inttohex(originalcode[i].address,8); showmessage(s); end; end; procedure TUnrandomize.restore; var i: integer; l: dword; begin if (processhandle<>processhandlerunit.ProcessHandle) and (processid=processhandlerunit.ProcessID) then processhandle:=processhandlerunit.ProcessHandle; //e.g debugger //restore the replaced code with the original for i:=length(originalcode)-1 downto 0 do begin l:=length(originalcode[i].code); rewritecode(processhandle,originalcode[i].address,originalcode[i].code,l); setlength(originalcode[i].code,0); end; setlength(originalcode,0); end; procedure TUnrandomize.save(address:ptruint; buf: pointer; size: integer); var i: integer; begin i:=length(originalcode); setlength(originalcode,i+1); originalcode[i].address:=address; setlength(originalcode[i].code,size); copymemory(originalcode[i].code,buf,size); end; procedure TUnrandomize.unrandomize64bit; var memoryregion: tmemoryregions; i,j: integer; totalmemory: dword; ar,aw: ptrUint; buffer: array of byte; totalread: dword; defaultreturn: integer; incremental: boolean; counter: pointer; genericreplace: tcodereplace; l: dword; c: tstringlist; a: ptruint; e: boolean; begin processid:=processhandlerunit.ProcessID; processhandle:=processhandlerunit.ProcessHandle; c:=tstringlist.Create; c.add('globalalloc(randomvalue,8)'); try autoassemble(c,false); except end; setlength(genericreplace,10); //mov eax,defaultvalue genericreplace[0]:=$a1; pqword(@genericreplace[1])^:=symhandler.getAddressFromName('randomvalue'); genericreplace[9]:=$c3; setlength(buffer,length(genericreplace)); a:=symhandler.getAddressFromName('msvcrt120.rand',true, e); if not e then begin readprocessmemory(processhandle, pointer(a), @buffer[0], length(genericreplace), ar); save(a,@buffer[j],length(genericreplace)); l:=length(genericreplace); rewritecode(processhandle,a,@genericreplace[0],l); end; a:=symhandler.getAddressFromName('ntdll.rtlrandom',true, e); if not e then begin readprocessmemory(processhandle, pointer(a), @buffer[0], length(genericreplace), ar); save(a,@buffer[j],length(genericreplace)); l:=length(genericreplace); rewritecode(processhandle,a,@genericreplace[0],l); end; a:=symhandler.getAddressFromName('rand',true, e); if not e then begin readprocessmemory(processhandle, pointer(a), @buffer[0], length(genericreplace), ar); save(a,@buffer[j],length(genericreplace)); l:=length(genericreplace); rewritecode(processhandle,a,@genericreplace[0],l); end; { c.clear; c.Add('msvr120.rand:'); c.add('mov rax,[randomvalue]'); c.add('ret'); try autoassemble(c,false); except end; c.clear; c.Add('ntdll.rtlrandom:'); c.add('mov rax,[randomvalue]'); c.add('ret'); try autoassemble(c,false); except end; c.clear; c.Add('rand:'); c.add('mov rax,[randomvalue]'); c.add('ret'); try autoassemble(c,false); except end; } totalmemory:=0; ar:=0; getexecutablememoryregionsfromregion(0,qword($7fffffffffffffff),memoryregion); for i:=0 to length(memoryregion)-1 do begin totalmemory:=totalmemory+memoryregion[i].MemorySize; if ar