Skip to content

Commit a2c22e0

Browse files
committed
add option to templates to generate 14 byte jmp scripts
1 parent e6d6078 commit a2c22e0

File tree

3 files changed

+75
-32
lines changed

3 files changed

+75
-32
lines changed

Cheat Engine/LuaHandler.pas

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12636,6 +12636,8 @@ function lua_GenerateCodeInjectionScript(L: PLua_state): integer; cdecl;
1263612636
var
1263712637
script: TStrings;
1263812638
address: string;
12639+
12640+
farjmp: boolean;
1263912641
begin
1264012642
result:=0;
1264112643
if lua_gettop(L)>=1 then
@@ -12647,8 +12649,13 @@ function lua_GenerateCodeInjectionScript(L: PLua_state): integer; cdecl;
1264712649
else
1264812650
address:=inttohex(MemoryBrowser.disassemblerview.SelectedAddress,8);
1264912651

12652+
if lua_gettop(L)>=3 then
12653+
farjmp:=lua_toboolean(L,3)
12654+
else
12655+
farjmp:=false;
12656+
1265012657
try
12651-
GenerateCodeInjectionScript(script, address);
12658+
GenerateCodeInjectionScript(script, address,farjmp);
1265212659
lua_pushboolean(L,true);
1265312660
result:=1;
1265412661
except
@@ -12661,6 +12668,7 @@ function lua_GenerateAOBInjectionScript(L: PLua_state): integer; cdecl;
1266112668
script: TStrings;
1266212669
address, symbolname: string;
1266312670
lineCountToCopy: integer;
12671+
farjmp: boolean;
1266412672
begin
1266512673
result:=0;
1266612674
if lua_gettop(L)>=2 then
@@ -12678,6 +12686,11 @@ function lua_GenerateAOBInjectionScript(L: PLua_state): integer; cdecl;
1267812686
else
1267912687
linecountToCopy:=20;
1268012688

12689+
if lua_gettop(L)>=5 then
12690+
farjmp:=lua_toboolean(L,5)
12691+
else
12692+
farjmp:=false;
12693+
1268112694
try
1268212695
GenerateAOBInjectionScript(script, address, symbolname, lineCountToCopy);
1268312696
lua_pushboolean(L,true);
@@ -12692,6 +12705,7 @@ function lua_GenerateFullInjectionScript(L: PLua_state): integer; cdecl;
1269212705
script: TStrings;
1269312706
address: string;
1269412707
lineCountToCopy: integer;
12708+
farjmp: boolean;
1269512709
begin
1269612710
result:=0;
1269712711
if lua_gettop(L)>=1 then
@@ -12708,6 +12722,11 @@ function lua_GenerateFullInjectionScript(L: PLua_state): integer; cdecl;
1270812722
else
1270912723
linecountToCopy:=20;
1271012724

12725+
if lua_gettop(L)>=4 then
12726+
farjmp:=lua_toboolean(L,4)
12727+
else
12728+
farjmp:=false;
12729+
1271112730
try
1271212731
GenerateFullInjectionScript(script, address, lineCountToCopy);
1271312732
lua_pushboolean(L,true);

Cheat Engine/bin/celua.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,9 @@ registerAutoAssemblerTemplate(name, function(script: TStrings; sender: TFrmAutoI
320320
unregisterAutoAssemblerTemplate(ID)
321321

322322

323-
generateCodeInjectionScript(script: Tstrings, address: string) - Adds a default codeinjection script to the given script
324-
generateAOBInjectionScript(script: Tstrings, symbolname: string, address: string, commentradius(default 10)) - Adds an AOB injection script to the given script
325-
generateFullInjectionScript(script: Tstrings, address: string, commentradius(default 10)) - Adds a Full Injection script to the given script
323+
generateCodeInjectionScript(script: Tstrings, address: string, farjmp: boolean) - Adds a default codeinjection script to the given script
324+
generateAOBInjectionScript(script: Tstrings, symbolname: string, address: string, commentradius(default 10), farjmp: boolean) - Adds an AOB injection script to the given script
325+
generateFullInjectionScript(script: Tstrings, address: string, commentradius(default 10), farjmp: boolean) - Adds a Full Injection script to the given script
326326

327327
getNextAllocNumber(script: TStrings): integer - scans the given script for alloc(newmem## and returns the next unused newmem number)
328328
addSnapshotAsComment(script: TStrings, address: integer, radius(Default 10)) - creates a comment section for AA scripts that contains a snapshot of the original code

Cheat Engine/frmautoinjectunit.pas

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,9 @@ TfrmAutoInject = class(TForm)
384384

385385
procedure Getjumpandoverwrittenbytes(address,addressto: ptrUINT; jumppart,originalcodepart: tstrings);
386386
procedure generateAPIHookScript(script: tstrings; address: string; addresstogoto: string; addresstostoreneworiginalfunction: string=''; nameextension:string='0'; targetself: boolean=false);
387-
procedure GenerateCodeInjectionScript(script: tstrings; addressstring: string);
388-
procedure GenerateAOBInjectionScript(script: TStrings; address: string; symbolname: string; commentradius: integer=10);
389-
procedure GenerateFullInjectionScript(Script: tstrings; address: string; commentradius: integer=10);
387+
procedure GenerateCodeInjectionScript(script: tstrings; addressstring: string; farjmp: boolean=false);
388+
procedure GenerateAOBInjectionScript(script: TStrings; address: string; symbolname: string; commentradius: integer=10; farjmp: boolean=false);
389+
procedure GenerateFullInjectionScript(Script: tstrings; address: string; commentradius: integer=10; farjmp: boolean=false);
390390

391391
function registerAutoAssemblerTemplate(name: string; m: TAutoAssemblerTemplateCallback; shortcut: TShortCut=0): integer;
392392
procedure unregisterAutoAssemblerTemplate(id: integer);
@@ -984,7 +984,7 @@ procedure TfrmAutoInject.FormClose(Sender: TObject;
984984
{$endif}
985985
end;
986986

987-
procedure GenerateCodeInjectionScript(script: tstrings; addressstring: string);
987+
procedure GenerateCodeInjectionScript(script: tstrings; addressstring: string; farjmp: boolean=false);
988988
function inttostr(i:int64):string;
989989
begin
990990
if i=0 then result:='' else result:=sysutils.IntToStr(i);
@@ -1005,7 +1005,13 @@ function inttostr(i:int64):string;
10051005
disablepos: integer;
10061006
enablecode: tstringlist;
10071007
disablecode: tstringlist;
1008+
jmpsize: integer;
10081009
begin
1010+
if not processhandler.is64Bit then
1011+
farjmp:=false;
1012+
1013+
jmpsize:=ifthen(farjmp, 14, 5);
1014+
10091015
try
10101016
a:=StrToQWordEx('$'+addressstring);
10111017
except
@@ -1020,7 +1026,7 @@ function inttostr(i:int64):string;
10201026
setlength(originalcode,0);
10211027
codesize:=0;
10221028

1023-
while codesize<5 do
1029+
while codesize<jmpsize do
10241030
begin
10251031
setlength(originalcode,length(originalcode)+1);
10261032
originalcode[length(originalcode)-1]:=disassemble(c,x);
@@ -1038,7 +1044,7 @@ function inttostr(i:int64):string;
10381044
try
10391045
with enablecode do
10401046
begin
1041-
if processhandler.is64bit then
1047+
if processhandler.is64bit and (not farjmp) then
10421048
add('alloc(newmem'+inttostr(injectnr)+',2048,'+addressstring+') ')
10431049
else
10441050
add('alloc(newmem'+inttostr(injectnr)+',2048)');
@@ -1059,11 +1065,14 @@ function inttostr(i:int64):string;
10591065

10601066
add('');
10611067
add(addressstring+':');
1062-
add('jmp newmem'+inttostr(injectnr)+'');
1063-
if codesize>5 then
1068+
if farjmp then
1069+
add('jmp far newmem'+inttostr(injectnr)+'')
1070+
else
1071+
add('jmp newmem'+inttostr(injectnr)+'');
1072+
if codesize>jmpsize then
10641073
begin
1065-
if codesize-5>1 then
1066-
add('nop '+inttohex(codesize-5,1))
1074+
if codesize-jmpsize>1 then
1075+
add('nop '+inttohex(codesize-jmpsize,1))
10671076
else
10681077
add('nop');
10691078
end;
@@ -1134,7 +1143,7 @@ procedure TfrmAutoInject.Codeinjection1Click(Sender: TObject);
11341143
address:=symhandler.getNameFromAddress(a);
11351144

11361145
if inputquery(rsCodeInjectTemplate, rsOnWhatAddressDoYouWantTheJump, address) then
1137-
GenerateCodeInjectionScript(assemblescreen.lines, address);
1146+
GenerateCodeInjectionScript(assemblescreen.lines, address, ssCtrl in GetKeyShiftState);
11381147
end;
11391148

11401149
procedure TfrmAutoInject.Panel1Resize(Sender: TObject);
@@ -2839,7 +2848,7 @@ function GetNextAllocNumber(Script: TStrings): integer;
28392848
end;
28402849

28412850
// \/ http://forum.cheatengine.org/viewtopic.php?t=566415 (jgoemat and some mods by db)
2842-
procedure GenerateFullInjectionScript(Script: tstrings; address: string; commentRadius: integer=10);
2851+
procedure GenerateFullInjectionScript(Script: tstrings; address: string; commentRadius: integer=10; farjmp: boolean=false);
28432852
var
28442853
originalcode: array of string;
28452854
originalbytes: array of byte;
@@ -2874,7 +2883,12 @@ procedure GenerateFullInjectionScript(Script: tstrings; address: string; comment
28742883
ddBytes: string;
28752884

28762885
mi: TModuleInfo;
2886+
jmpsize: integer;
28772887
begin
2888+
if not processhandler.is64Bit then
2889+
farjmp:=false;
2890+
2891+
jmpsize:=ifthen(farjmp, 14, 5);
28782892

28792893
try
28802894
a:=StrToQWordEx('$'+address);
@@ -2896,7 +2910,7 @@ procedure GenerateFullInjectionScript(Script: tstrings; address: string; comment
28962910
setlength(originalcode,0);
28972911
codesize:=0;
28982912

2899-
while codesize<5 do
2913+
while codesize<jmpsize do
29002914
begin
29012915
setlength(originalcode,length(originalcode)+1);
29022916
originalcode[length(originalcode)-1]:=disassemble(c,x);
@@ -2954,7 +2968,7 @@ procedure GenerateFullInjectionScript(Script: tstrings; address: string; comment
29542968
with enablecode do
29552969
begin
29562970
add('assert(address'+nr+',bytes'+nr+')');
2957-
if processhandler.is64bit then
2971+
if processhandler.is64bit and (not farjmp) then
29582972
add('alloc(newmem' + nr + ',$1000,' + address + ')')
29592973
else
29602974
add('alloc(newmem' + nr + ',$1000)');
@@ -2972,11 +2986,14 @@ procedure GenerateFullInjectionScript(Script: tstrings; address: string; comment
29722986

29732987
add('');
29742988
add('address'+nr+':');
2975-
add(' jmp newmem'+nr+'');
2976-
if codesize>5 then
2989+
if farjmp then
2990+
add(' jmp far newmem'+nr+'')
2991+
else
2992+
add(' jmp newmem'+nr+'');
2993+
if codesize>jmpsize then
29772994
begin
2978-
if codesize-5>1 then
2979-
add(' nop '+inttohex(codesize-5,1))
2995+
if codesize-jmpsize>1 then
2996+
add(' nop '+inttohex(codesize-jmpsize,1))
29802997
else
29812998
add(' nop');
29822999
end;
@@ -3058,7 +3075,7 @@ procedure TfrmAutoInject.menuFullInjectionClick(Sender: TObject);
30583075
address:=inttohex(a,8);
30593076

30603077
if inputquery(rsCodeInjectTemplate, rsOnWhatAddressDoYouWantTheJump, address) then
3061-
generateFullInjectionScript(assemblescreen.Lines, address);
3078+
generateFullInjectionScript(assemblescreen.Lines, address, 10, ssCtrl in GetKeyShiftState);
30623079
end;
30633080

30643081

@@ -3127,7 +3144,7 @@ procedure TfrmAutoInject.MenuItem1Click(Sender: TObject);
31273144
frmHighlighterEditor.free;
31283145
end;
31293146

3130-
procedure GenerateAOBInjectionScript(script: TStrings; address: string; symbolname: string; commentradius: integer=10);
3147+
procedure GenerateAOBInjectionScript(script: TStrings; address: string; symbolname: string; commentradius: integer=10; farjmp: boolean=false);
31313148
var
31323149
a,a2: ptrUint; // pointer to injection point
31333150
originalcode: array of string; // disassembled code we're replacing
@@ -3168,8 +3185,12 @@ procedure GenerateAOBInjectionScript(script: TStrings; address: string; symbolna
31683185
resultAOB: String;
31693186
resultOffset: Integer;
31703187
symbolNameWithOffset: String;
3188+
jmpsize: integer;
31713189
begin
3172-
// now heavily modified code from "Code injection" menu
3190+
if not processhandler.is64Bit then
3191+
farjmp:=false;
3192+
3193+
jmpsize:=ifthen(farjmp, 14, 5);
31733194

31743195
try
31753196
a:=StrToQWordEx('$'+address);
@@ -3190,7 +3211,7 @@ procedure GenerateAOBInjectionScript(script: TStrings; address: string; symbolna
31903211
setlength(originalcode,0);
31913212
codesize:=0;
31923213

3193-
while codesize<5 do
3214+
while codesize<jmpsize do
31943215
begin
31953216
setlength(originalcode,length(originalcode)+1);
31963217
originalcode[length(originalcode)-1]:=disassemble(c,x);
@@ -3253,7 +3274,7 @@ procedure GenerateAOBInjectionScript(script: TStrings; address: string; symbolna
32533274
else
32543275
add('aobscan(' + symbolName + ',' + resultAOB + ') // should be unique');
32553276

3256-
if processhandler.is64bit then
3277+
if processhandler.is64bit and (not farjmp) then
32573278
add('alloc(newmem' + nr + ',$1000,' + symbolname + ')')
32583279
else
32593280
add('alloc(newmem' + nr + ',$1000)');
@@ -3271,11 +3292,14 @@ procedure GenerateAOBInjectionScript(script: TStrings; address: string; symbolna
32713292

32723293
add('');
32733294
add(symbolNameWithOffset + ':');
3274-
add(' jmp newmem' + nr + '');
3275-
if codesize>5 then
3295+
if farjmp then
3296+
add(' jmp far newmem' + nr + '')
3297+
else
3298+
add(' jmp newmem' + nr + '');
3299+
if codesize>jmpsize then
32763300
begin
3277-
if codesize-5>1 then
3278-
add(' nop '+inttohex(codesize-5,1))
3301+
if codesize-jmpsize>1 then
3302+
add(' nop '+inttohex(codesize-jmpsize,1))
32793303
else
32803304
add(' nop');
32813305
end;
@@ -3370,7 +3394,7 @@ procedure TfrmAutoInject.menuAOBInjectionClick(Sender: TObject);
33703394
symbolname:='INJECT'+nr;
33713395

33723396
if inputquery(rsCodeInjectTemplate, rsWhatIdentifierDoYouWantToUse, symbolName) then
3373-
GenerateAOBInjectionScript(assemblescreen.Lines, address, symbolname);
3397+
GenerateAOBInjectionScript(assemblescreen.Lines, address, symbolname, 10, ssCtrl in GetKeyShiftState);
33743398
end;
33753399
end;
33763400

0 commit comments

Comments
 (0)