Advertisement
Guest User

customAOBInjectionTemplates.lua

a guest
Apr 12th, 2023
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.42 KB | None | 0 0
  1. myAOBInjectionTemplates = {}
  2.  
  3. myAOBInjectionTemplates.Templates = {
  4.  
  5. {
  6. displayName="Regular AOB",
  7. submenu='AOB Injection Templates',
  8. templateSections=
  9. [==[
  10. <<INFO>>
  11. // Game Executable   : %processName%
  12. // Author            : %authorName%
  13. // Executable Version: %exeVersion%
  14. // MD5 Signature     : %md5Signature%
  15. // EXE Compile Date  : %compileDate%
  16. // Script Date       : %scriptDate%
  17. <<INFO_END>>
  18.  
  19. <<ENABLE>>
  20. aobscan%isModuleScan%(aob_%cheatName%,%moduleNameC%%searchPattern%)
  21. registersymbol(aob_%cheatName%)
  22. alloc(newmem_%cheatName%,1024%CmoduleName%)
  23. label(return_%cheatName%)
  24.  
  25. newmem_%cheatName%:
  26. %_originalCodeLines%
  27.   jmp return_%cheatName%
  28.  
  29. aob_%cheatName%%aobAdjust%:
  30.   jmp newmem_%cheatName%
  31. %_nopLines%
  32. return_%cheatName%:
  33.  
  34. <<ENABLE_END>>
  35.  
  36. <<DISABLE>>
  37. aob_%cheatName%%aobAdjust%:
  38.   db %originalBytes%
  39.  
  40. unregistersymbol(aob_%cheatName%)
  41. dealloc(newmem_%cheatName%)
  42.  
  43. %additionalInfo%
  44. <<DISABLE_END>>
  45. ]==]
  46. },
  47.  
  48. {
  49. displayName="AOB Replace Jump",
  50. submenu='AOB Injection Templates',
  51. templateSections=
  52. [==[
  53. <<INFO>>
  54. // Game Executable   : %processName%
  55. // Author            : %authorName%
  56. // Executable Version: %exeVersion%
  57. // MD5 Signature     : %md5Signature%
  58. // EXE Compile Date  : %compileDate%
  59. // Script Date       : %scriptDate%
  60. <<INFO_END>>
  61.  
  62. <<ENABLE>>
  63. aobscan%isModuleScan%(aob_%cheatName%,%moduleNameC%%searchPattern%)
  64. registersymbol(aob_%cheatName%)
  65.  
  66. aob_%cheatName%%aobAdjust%:
  67.   db EB
  68.  
  69. <<ENABLE_END>>
  70.  
  71. <<DISABLE>>
  72. aob_%cheatName%%aobAdjust%:
  73.   db %originalBytes%
  74.  
  75. unregistersymbol(aob_%cheatName%)
  76.  
  77. %additionalInfo%
  78. <<DISABLE_END>>
  79. ]==]
  80. },
  81.  
  82.  
  83. {
  84. displayName="AOB set pointer at address",
  85. submenu='AOB Injection Templates',
  86. templateSections=
  87. [==[
  88. <<INFO>>
  89. // Game Executable   : %processName%
  90. // Author            : %authorName%
  91. // Executable Version: %exeVersion%
  92. // MD5 Signature     : %md5Signature%
  93. // EXE Compile Date  : %compileDate%
  94. // Script Date       : %scriptDate%
  95. <<INFO_END>>
  96.  
  97. <<ENABLE>>
  98. aobscan%isModuleScan%(aob_%cheatName%,%moduleNameC%%searchPattern%)
  99. registersymbol(aob_%cheatName%)
  100. alloc(newmem_%cheatName%,1024%CmoduleName%)
  101. label(return_%cheatName%)
  102. label(set_%cheatName%)
  103. label(quit_%cheatName%)
  104.  
  105. newmem_%cheatName%:
  106.   jmp quit_%cheatName%
  107.  
  108. set_%cheatName%:
  109.   mov %bracketsRegsOffset%,0
  110.   //jmp quit_%cheatName%
  111.  
  112. quit_%cheatName%:
  113. %_originalCodeLines%
  114.   jmp return_%cheatName%
  115.  
  116. aob_%cheatName%%aobAdjust%:
  117.   jmp newmem_%cheatName%
  118. %_nopLines%
  119. return_%cheatName%:
  120.  
  121. <<ENABLE_END>>
  122.  
  123. <<DISABLE>>
  124. aob_%cheatName%%aobAdjust%:
  125.   db %originalBytes%
  126.  
  127. unregistersymbol(aob_%cheatName%)
  128. dealloc(newmem_%cheatName%)
  129.  
  130. %additionalInfo%
  131. <<DISABLE_END>>
  132. ]==]
  133. }
  134.  
  135. }
  136.  
  137. local extensionVersion = '1.3.6'
  138.  
  139. function myAOBInjectionTemplates.formCreateNotify(form)
  140.   if form.ClassName~="TfrmAutoInject" then return end
  141.  
  142.   local timer=createTimer()
  143.   timer.Interval=100
  144.   timer.OnTimer = function (t)
  145.                     if (form.Menu==nil) then return end
  146.                     t.destroy()
  147.                     myAOBInjectionTemplates.addMenuEntries(form)
  148.                   end
  149. end
  150.  
  151. function myAOBInjectionTemplates.addMenuEntries(form)
  152.   local m,mi,sm=form.emplate1,nil,nil
  153.   local createdSubmenus={}
  154.   local lastMenuItemFromGroup={}
  155.   local smIndex = 1
  156.  
  157.   mi = createMenuItem(m); m.add(mi); mi.Caption = '-' -- separator
  158.  
  159.   for i=1,#myAOBInjectionTemplates.Templates do
  160.     local submenu = myAOBInjectionTemplates.Templates[i].submenu
  161.     local group = myAOBInjectionTemplates.Templates[i].group
  162.     local groupname=(submenu or '')..(group or '')
  163.  
  164.     if submenu~=nil then
  165.       if createdSubmenus[submenu] then
  166.         sm=createdSubmenus[submenu]
  167.       else
  168.         sm = createMenuItem(m); m.add(sm)
  169.         sm.Caption = submenu
  170.         sm.Name = 'miAlternativeAOBtemplateSubmenu'..smIndex; smIndex=smIndex+1
  171.         createdSubmenus[submenu]=sm
  172.       end
  173.     else
  174.       sm=m
  175.     end
  176.  
  177.     if lastMenuItemFromGroup[groupname]==nil then
  178.       if sm.Count>0 then
  179.         mi = createMenuItem(m); sm.add(mi); mi.Caption = '-' -- separator
  180.       end
  181.       mi = createMenuItem(m); sm.add(mi)
  182.       lastMenuItemFromGroup[groupname]=mi
  183.     else
  184.       mi = createMenuItem(m); sm.insert(lastMenuItemFromGroup[groupname].MenuIndex+1, mi)
  185.       lastMenuItemFromGroup[groupname]=mi
  186.     end
  187.  
  188.     mi.OnClick = function (sender)
  189.                   myAOBInjectionTemplates.generate(sender,myAOBInjectionTemplates.Templates[i])
  190.                  end
  191.     mi.Caption = myAOBInjectionTemplates.Templates[i].displayName
  192.     mi.Name = 'miAlternativeAOBtemplate'..i
  193.   end
  194. end
  195.  
  196. registerFormAddNotification(myAOBInjectionTemplates.formCreateNotify)
  197.  
  198.  
  199.  
  200. function myAOBInjectionTemplates.generate(sender,chosenTemplate)
  201.  
  202.   local displayName = chosenTemplate.displayName
  203.   local cheatName = chosenTemplate.defaultSymbolName or 'example'
  204.   local template = chosenTemplate.templateSections
  205.   local form=sender.Owner.Owner
  206.   local origScript = form.Assemblescreen.Lines.Text
  207.  
  208.   --gather existing names from origScript from registersymbol
  209.   local existingNames = {}
  210.   for existingName in origScript:gmatch('registersymbol%(%s*(.-)%s*%)') do
  211.     existingNames[1+#existingNames] = existingName
  212.   end
  213.   -- also from define
  214.   for existingName in origScript:gmatch('define%(%s*(.-)%s*,') do
  215.     existingNames[1+#existingNames] = existingName
  216.   end
  217.  
  218.   local function checkForCollides(str)
  219.     for i,v in ipairs(existingNames) do
  220.       if v:find(str, 1, true)~=nil then return 'Name "'..str..'" collides with existing "'..v..'"' end
  221.       if str:find(v, 1, true)~=nil then return 'Existing "'..v..'" collides with name "'..str..'"' end
  222.     end
  223.     return nil
  224.   end
  225.  
  226.   local address = 0
  227.   if form.owner.DisassemblerView then
  228.     address = form.owner.DisassemblerView.SelectedAddress
  229.   else
  230.     address = getMemoryViewForm().DisassemblerView.SelectedAddress
  231.   end
  232.  
  233.   --try to find module name+offset
  234.   local selectedAddress = getNameFromAddress(address)
  235.   local modulesTable,size = enumModules(),0
  236.   local exeVersionStr = ''
  237.  
  238.   local _,exeVersionTbl = getFileVersion(modulesTable[1].PathToFile)
  239.   local exeVersionStr = exeVersionTbl==nil and '' or (exeVersionTbl.major..'.'..exeVersionTbl.minor..'.'..
  240.                                                       exeVersionTbl.release..'.'..exeVersionTbl.build)
  241.                                                      
  242.   local md5Hash = string.upper(tostring(md5file(modulesTable[1].PathToFile)))
  243.  
  244.   local base = enumModules()[1].Address
  245.   local e_lfanew = readInteger(base + 0x3c)
  246.   local time_date_stamp = readInteger(base + e_lfanew + 0x08)
  247.  
  248.   selectedAddress=inputQuery(displayName,'On what address do you want the jump?', selectedAddress)
  249.   if selectedAddress==nil then return end
  250.  
  251.   cheatName=inputQuery(displayName,'What do you want to name the symbol for the injection point?', cheatName)
  252.   if cheatName==nil then return end
  253.  
  254.   ::setValidname:: --do not allow default name or those already existing/colliding or empty
  255.   while cheatName:lower()=='inject' or
  256.         cheatName=='' do
  257.     cheatName=inputQuery('Caution!', 'Ugly name. Change it.', cheatName) or ''
  258.     cheatName=cheatName:gsub('%s','') -- remove spaces
  259.   end
  260.  
  261.   -- check if already exist or collides with each other
  262.   local collides = checkForCollides(cheatName)
  263.   if collides~=nil then
  264.     cheatName=inputQuery('Caution!', collides..'. Change it.', cheatName) or ''
  265.     cheatName=cheatName:gsub('%s','') -- remove spaces
  266.     goto setValidname
  267.   end
  268.  
  269.   local newScript_stringlist = createStringlist()
  270.   local gaobisResult = generateAOBInjectionScript(newScript_stringlist, cheatName, selectedAddress)
  271.   local newScript = newScript_stringlist.Text
  272.   newScript_stringlist.destroy()
  273.  
  274.   -- is process opened?
  275.   if newScript:match('No Process Selected') then showMessage("No process selected!") return end
  276.  
  277.   -- is aob unique?
  278.   if newScript:match('Could not find unique AOB') then
  279.     showMessage("Warning. Could not find unique AOB.")
  280.     newScript = newScript:gsub('ERROR: Could not find unique AOB,','ERROR: Could not find unique AOB;')
  281.   end
  282.  
  283.   if not gaobisResult
  284.   then showMessage("generateAOBInjectionScript raised exception!") return end
  285.  
  286.  
  287.   -- note: 'origScript' and 'newScript' will have "carriage return & line feed" at the end of each line
  288.   --       because it is taken from TStrings object.
  289.   --       'template' has only "line feed"
  290.  
  291.  
  292.   local authorName    = newScript:match('Author : (.-)\r\n')
  293.   local processName   = newScript:match('Game   : (.-)\r\n')
  294.   local scriptDate   = newScript:match('Date   : (.-)\r\n')
  295.   local isModuleScan  = newScript:match('aobscan(module)') or ''
  296.   local searchPattern = newScript:match('aobscan.-%(.*,(.-)%) // should be unique')
  297.  
  298.   local moduleName, moduleName_comma, comma_moduleName
  299.  
  300.   if isModuleScan=='module' then
  301.     moduleName = newScript:match('aobscan.-%(.-,(.-),.-%)')
  302.     moduleName_comma = moduleName..','
  303.     comma_moduleName = ','..moduleName
  304.   else
  305.     moduleName = ''
  306.     moduleName_comma = ''
  307.     comma_moduleName = ''
  308.   end
  309.  
  310.   local _originalCodeLines = newScript:match('code:\r\n(.-)\r\n  jmp return')
  311.   local aobAdjust          = newScript:match('code:.-jmp return\r\n\r\n'..cheatName..'(.-):')
  312.   local _nopLines          = newScript:match('  jmp code\r\n(.-)\r\nreturn:') or ''
  313.  
  314.   if _nopLines=='' then  -- other case
  315.         _nopLines          = newScript:match('  jmp newmem\r\n(.-)\r\nreturn:') or ''
  316.   end
  317.  
  318.   local originalBytes      = newScript:match('  db (.-)\r\n')
  319.   local additionalInfo     = newScript:match('{\r\n// ORIGINAL CODE %- INJECTION POINT.*')
  320.  
  321.   local origfirstLine = (_originalCodeLines..'\r\n'):match( "(.-)\r\n" )
  322.   local bracketsRegsOffset  = origfirstLine:match('[dq]?word ptr %[.-%]')
  323.                            or origfirstLine:match('byte ptr %[.-%]')
  324.                            or origfirstLine:match('%[.-%]')
  325.                            or ''
  326.   local regsOffset = origfirstLine:match('%[(.-)%]') or ''
  327.  
  328.   local originalCodeLines = _originalCodeLines:sub(3):gsub('\r\n  ','\r\n')       -- indent less version
  329.   local nopLines = _nopLines=='' and '' or _nopLines:sub(3):gsub('\r\n  ','\r\n') -- indent less version
  330.   local CoriginalCodeLines = '//Alt: '.._originalCodeLines:sub(3):gsub('\r\n  ','\r\n//Alt: ')-- commented version
  331.  
  332.   local db90s=''
  333.   if _nopLines~='' then
  334.     if _nopLines:match('  nop (%x+)') then --CE7.0 and above has '  nop '+inttohex(NopCount)
  335.       local nopNumber = tonumber(_nopLines:match('  nop (%x+)'),16)
  336.       db90s = 'db'..string.rep(" 90", nopNumber)
  337.     else --up to CE6.8.3
  338.       db90s = 'db'..(nopLines..'\r\n'):gsub('nop\r\n',' 90')
  339.     end
  340.   end
  341.  
  342.   local _,replacedInstructionsSize = originalBytes:gsub('%x%x','')
  343.   local replacedInstructionsSizeHex = string.format('%X',replacedInstructionsSize)
  344.  
  345.  
  346.   --Mono & Hook Address
  347.   local injectAddress = newScript:match('INJECTING HERE %-%-%-%-%-%-%-%-%-%-\r\n(.-):')
  348.   local injectAddressNum = getAddress(injectAddress)
  349.   local monoAddress = ''
  350.   if template:find('%%monoAddress%%') then -- remove lag for templates without mono
  351.     if LaunchMonoDataCollector~=nil and LaunchMonoDataCollector()~=0 then
  352.       monoAddress = mono_addressLookupCallback(injectAddressNum) or ''
  353.     end
  354.   end
  355.  
  356.   --reassembleReplacedInstructions
  357.   local tmp = getInstructionSize(injectAddressNum)
  358.   local reassembleReplacedInstructions = 'reassemble(~)'
  359.   while tmp < replacedInstructionsSize do
  360.     reassembleReplacedInstructions = reassembleReplacedInstructions .. '\nreassemble(~+'..string.format('%X',tmp)..')'
  361.     tmp = tmp + getInstructionSize(injectAddressNum+tmp)
  362.   end
  363.  
  364.   -- use the template
  365.   template = template:gsub('%%cheatName%%', cheatName)
  366.   template = template:gsub('%%authorName%%', "gir489")
  367.   template = template:gsub('%%processName%%', processName)
  368.   template = template:gsub('%%isModuleScan%%', isModuleScan)
  369.   template = template:gsub('%%searchPattern%%', searchPattern)
  370.   template = template:gsub('%%CmoduleName%%', comma_moduleName)
  371.   template = template:gsub('%%moduleNameC%%', moduleName_comma)
  372.   template = template:gsub('%%compileDate%%', os.date("%B %d, %Y %I:%M %p", time_date_stamp))
  373.   template = template:gsub('%%moduleName%%', moduleName)
  374.   template = template:gsub('%%replacedInstructionsSize%%', replacedInstructionsSize)
  375.   template = template:gsub('%%replacedInstructionsSizeHex%%', replacedInstructionsSizeHex)
  376.   template = template:gsub('%%_originalCodeLines%%', _originalCodeLines)
  377.   template = template:gsub('%%originalCodeLines%%', originalCodeLines)
  378.   template = template:gsub('%%CoriginalCodeLines%%', CoriginalCodeLines)
  379.   template = template:gsub('%%originalBytes%%', originalBytes)
  380.   template = template:gsub('%%aobAdjust%%', aobAdjust)
  381.   template = template:gsub('%%additionalInfo%%', additionalInfo)
  382.   template = template:gsub('%%bracketsRegsOffset%%', bracketsRegsOffset)
  383.   template = template:gsub('%%regsOffset%%', regsOffset)
  384.   template = template:gsub('%%injectAddress%%', injectAddress)
  385.   template = template:gsub('%%monoAddress%%', monoAddress)
  386.   template = template:gsub('%%reassembleReplacedInstructions%((.-)%)%%', function (a)
  387.     return reassembleReplacedInstructions:gsub('~',a)
  388.   end)
  389.  
  390.   template = template:gsub('%%scriptDate%%', os.date("%B %d, %Y %I:%M %p", os.time()))
  391.   template = template:gsub('%%exeVersion%%', exeVersionStr)
  392.   template = template:gsub('%%md5Signature%%', md5Hash)
  393.  
  394.   if db90s~='' then
  395.    template = template:gsub('%%nopLines%%', nopLines)
  396.    template = template:gsub('%%_nopLines%%', _nopLines)
  397.    template = template:gsub('%%db90s%%', db90s)
  398.   else
  399.    -- remove whole line when NOP'ing is not needed
  400.    template = template:gsub('%%nopLines%%.-\n', '')
  401.    template = template:gsub('%%_nopLines%%.-\n', '')
  402.    template = template:gsub('%%db90s%%.-\n', '')
  403.   end
  404.  
  405.   template = template:gsub('%%extensionVersion%%', extensionVersion)
  406.  
  407.   local enablePart  = template:match('<<ENABLE>>.(.*).<<ENABLE_END>>')
  408.   local disablePart = template:match('<<DISABLE>>.(.*).<<DISABLE_END>>')
  409.   local infoPart = template:match('<<INFO>>.(.*).<<INFO_END>>')
  410.  
  411.   if origScript=='\r\n' then origScript='' end --after manually deleting all lines, there's always one empty line
  412.  
  413.   local pos=origScript:find('%[DISABLE]')
  414.   if pos then newScript=origScript:sub(1,pos-1)..'\r\n'..enablePart..'\r\n'..origScript:sub(pos)..'\r\n'..disablePart
  415.          else newScript=origScript..'[ENABLE]\r\n'..enablePart..'\r\n[DISABLE]\r\n'..disablePart
  416.   end
  417.  
  418.   if pos==nil and infoPart~=nil then newScript=infoPart..'\r\n'..newScript end
  419.  
  420.   form.Assemblescreen.Lines.Text = newScript -- update
  421. end
  422.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement