AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Coding MM:S Plugins & SM Extensions (https://forums.alliedmods.net/forumdisplay.php?f=75)
-   -   Solved Calling constructors from server binary? (https://forums.alliedmods.net/showthread.php?t=306223)

nosoop 03-21-2018 09:53

Calling constructors from server binary?
 
Hey there.
I'm trying to experiment with hooking some NextBot-related function calls in TF2 (and figuring out C++ in the process).

Right now I have a hook on a function CTFBotScenarioMonitor::DesiredScenarioAndClassAction, which expects a return type Action<CTFBot>. I'd like to return an instance of one of the other actions present in the game.

Is there a way to manually get the size of a class, or to instantiate an instance of that class without an explicit size declaration? I'm able to allocate memory and call the ctor of something like CTFBotSeekAndDestroy, but so far I'm manually inputting the amount to allocate based on existing references. Was wondering if there were any other approaches (or something available to extensions that I didn't find).

Thanks!

Fyren 03-21-2018 12:57

Re: Calling constructors from server binary?
 
The compiler would have the definition of the class and so would know the size whenever you construct one. If you look at the disassembly for an object constructed on the heap, you'll see a call to new with its size (unless it's placement new, I guess).

I guess you could try to sigscan for a call to new that you know is for a class you want to get the size at runtime. I don't really have any other ideas besides maintaining your own definition of the class.

nosoop 03-22-2018 22:37

Re: Calling constructors from server binary?
 
Yeah, that makes sense.

IDA's free offering seems to be sufficient in finding cross references to constructor calls, and it looks like the call to new is being made. (I'm not too concerned about non-Linux platforms at the moment.)

Creating my own class definitions for these classes is a little too complicated for me at the moment (for a full recreation anyways, I have some classes declared with bytes padded), so this will do. Thanks for the help!

Fyren 03-23-2018 14:31

Re: Calling constructors from server binary?
 
Creating your own definition can make accessing members easier since you wouldn't have to do any pointer math/casting yourself. You would only need to fill it in so the size is correct and the placement of any members you care about are correct. class Foo { char pad[36]; int intICareAbout; char pad2[16]; } for example.

nosoop 03-24-2018 01:56

Re: Calling constructors from server binary?
 
Got it. At the moment I don't need access to member variables, but I'll probably take advantage of it if / when the project's more fleshed out (probably when I leverage sigsegv's reverse engineering work).


All times are GMT -4. The time now is 16:00.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.