Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Invoking JIT Methods in .NET (Not Mono) Games.

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
ApacheTech
Newbie cheater
Reputation: 0

Joined: 26 Jun 2020
Posts: 14

PostPosted: Tue Feb 23, 2021 8:20 pm    Post subject: Invoking JIT Methods in .NET (Not Mono) Games. Reply with quote

With Unity games, I'm able to use `mono_findMethod` and `mono_invoke_method` to invoke JIT methods within the codebase. This is easy when using dnSpy or similar to trace back the methods you need to run.

I'm now writing scripts for a game that is written purely in .NET, without Unity. The .NET Info screen allows me to crawl the codebase, but are there methods similar to `mono_invoke_method` that don't rely on Mono?
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25287
Location: The netherlands

PostPosted: Tue Feb 23, 2021 8:29 pm    Post subject: Reply with quote

you can use the dotnetdatacollector in conjunction with
Code:

LaunchDotNetInterface()

which injects a c# helper assembly into the target process


and then you can use
dotnet_getMethodEntryPoint(moduleid, methoddef)

here's a helper function to use this (modulename is optional, but speeds things up)
Code:

function dotnet_findDotNetMethodAddress(namespace, classname, methodname, modulename)
  print(string.format("dotnet_findDotNetMethodAddress('%s','%s','%s','%s')",namespace,classname, methodname, modulename))

  local fcn

  if namespace==nil then namespace='' end
  if modulename then modulename=modulename:lower() end

  if namespace~='' then
    fcn=namespace..'.'..classname
  else
    fcn=classname
  end

  local dc=getDotNetDataCollector()

  local moduleid


  local domains=dc.enumDomains()
  local i
  for i=1,#domains do
    local modules=dc.enumModuleList(domains[i].DomainHandle)
    local j
    for j=1,#modules do
      if (modulename==nil) or (modulename==extractFileName(modules[j].Name):lower()) then
        local classes=dc.enumTypeDefs(modules[j].ModuleHandle)
        local k
        t={}

        for k=1,#classes do
          --printf("%d: %s (%d) <=>%s (%d)",k, classes[k].Name, #classes[k].Name, fcn, #fcn)
          t[k]=classes[k].Name

          if classes[k].Name==fcn then
            --found the class
            --print("yes")
            local ml=dc.getTypeDefMethods(modules[j].ModuleHandle, classes[k].TypeDefToken)
            local l
            for l=1,#ml do
              if ml[l].Name==methodname then
                print("Found method. Calling dotnet_getMethodEntryPoint")
                local r=dotnet_getMethodEntryPoint(dotnet_getModuleID(extractFileName(modules[j].Name)), ml[l].MethodToken)
               
                if r then
                  printf("%s at address %8x", methodname, r)
                  return r
                else
                  print("failure")
                end
              end
            end
          end
        end
      end
    end
  end
end

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
ApacheTech
Newbie cheater
Reputation: 0

Joined: 26 Jun 2020
Posts: 14

PostPosted: Thu Mar 18, 2021 10:18 am    Post subject: Reply with quote

Thank you. What's the .NET equivalent to "mono_invoke_method".

I have a helper method I've been using for that. Is there something similar for .NET?

Code:

    --- <summary>
    ---     Wrapper to safely invoke mono methods within Cheat Engine.
    --- </summary>
    function InvokeMethod(domain, method, args, instanceAddress)
      local c=mono_method_getClass(method);
      local instance;
      if (instanceAddress == nil) then
        instance = mono_class_findInstancesOfClassListOnly(domain,c);
        instance = instance[1];
      else
        instance = instanceAddress;
      end

      local params = string.split(mono_method_getSignature(method),',');
      if #args ~= #params then
        print('ERROR:InvokeMethod : Wrong length of args');
        print(string.format('ERROR:InvokeMethod : Expected: %d', #params));
        print(string.format('ERROR:InvokeMethod : Actual: %d', #args));
        return
      end

      local i;
      local args_t={};
      for i=1, #params do
        args_t[i] = {};
        args_t[i].type = monoTypeToVartypeLookup[params[i].type];
       args_t[i].value = args[i];
      end

      if method==nil or method==0 then
        print('ERROR:InvokeMethod : method==0');
       return;
      end

      if instance==nil or instance==0 then
        print('ERROR:InvokeMethod : instance==0');
       return;
      end

      local r=mono_invoke_method(domain, method, instance, args_t);
      return r;
    end
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25287
Location: The netherlands

PostPosted: Thu Mar 18, 2021 10:24 am    Post subject: Reply with quote

not right now. with .net i'd just create a thread and call the function with the proper parameters (ecx/rcx the this instance, and the other parameters according to the calling convention)

Does .net have a way to invoke a method with variable parameters?

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8517
Location: 127.0.0.1

PostPosted: Thu Mar 18, 2021 2:27 pm    Post subject: Reply with quote

.NET's MethodBase has Invoke(...) which takes a param array for variable parameters.
https://docs.microsoft.com/en-us/dotnet/api/system.reflection.methodbase.invoke?view=net-5.0

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
ApacheTech
Newbie cheater
Reputation: 0

Joined: 26 Jun 2020
Posts: 14

PostPosted: Sat Mar 27, 2021 3:32 pm    Post subject: Reply with quote

Considering that mono is .NET, I assumed there would be a "dotnet_invoke_method" LUA function I could call, in the same way as "mono_invoke_method". That's what I'm after.
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25287
Location: The netherlands

PostPosted: Sun Mar 28, 2021 4:50 am    Post subject: Reply with quote

mono is .NET with exposed functions like mono_invoke_method

.NET on windows is a mess with of COM's, Interfaces, undocumented stuff, etc...

But next version will allow you to invoke method as well. (The object types are different though, so the code has to be adjusted)

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites