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 


getPreviousOpcode and some other lua questions
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Chris12
Expert Cheater
Reputation: 1

Joined: 27 Apr 2012
Posts: 103

PostPosted: Sun Feb 10, 2013 12:11 pm    Post subject: getPreviousOpcode and some other lua questions Reply with quote

1:
Quote:
"getPreviousOpcode(address): Returns the address of the previous opcode (this is just an estimated guess)"

Assume the pe is not encrypted / obfuscated etc.
Only x86 and some mmx.
CE's disassembler seems to do a good job most of the time.

How reliable is it in this case?


2:
How do I reliably detect the start of a function with Lua?
Some (most) functions setup a stackframe at the beginning.
But there are also some that aren't stdcall/thiscall/cdelc, but userpurge /usercall.
Align-Regions (regions between functions) can be filled with 0x0, ret or int3.

But I don't know if thats enough to reliably detect the start of a function. Also what about cases where two functions are just by chance perfectly aligned.

3:
How do I find all references to a function (with lua)?
Is there a way to also find "call <register>" calls referencing a function?


CE is a aweseome, super-powerful tool, but somehow I have the feeling that I should be using IDA for those 3 things? The problem is I don't know how to make ida plugins and Lua seems to be so nice and easy Sad
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

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

PostPosted: Sun Feb 10, 2013 2:57 pm    Post subject: This post has 1 review(s) Reply with quote

1:It's just an estimate based on disassembling several bytes in front and see which distances result in the current address being one of them without any invalid/abnormal instruction inbetween

2: No, a function does not HAVE to start at a 16 byte alignment and if it uses pascal/fastcall without stackframe there is no way to find out it's a function start.
At best you could check if there is a pointer (unaligned) to that function and disassemble all the code and look for a reference to that function

3: in ce you'd do the code dissect, but that's not in lua yet. It's similar to disassembling all the code and look if it references the function. Check answer2

_________________
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
Chris12
Expert Cheater
Reputation: 1

Joined: 27 Apr 2012
Posts: 103

PostPosted: Sun Feb 10, 2013 3:54 pm    Post subject: Reply with quote

1:
Thats what I thought Sad
But why does this function work like this when CE already has a better algorithm to detect a very similar case.
I mean, when you set the disasm view to a "wrong" address inside a function it will still show some (garbage) disassembled code.
But as soon as you turn the mouse-wheel it will "snap" to the nearest correct address and then display the correct disassembly.

You could also see if it's possible to disassemble the second-previous instruction. If not, then the current offset to the previous instruction is most likely a few bytes off. That should improve the heuristic a bit at least. (Maybe thats how CE snaps to the correct offsets when scrolling??)


2&3:
Disassemble all the code and look for references.
I'd rather use the dissect-code feature. It seems to be really well done and bug-free.
Besides I don't really know how to do the same thing myself properly in Lua.
And won't it be slow and use way to much memory when done in lua? (70mb game dll)

Do you have any plans on making the dissect code features accessible trough Lua?
It would be enough to have: dissectCode(moduleName), getXrefs(address), getReferencedStrings(moduleName)
It's all stuff that's already there, but I don't know how difficult / timeconsuming it is to expose those functions.


4:
Is there a way to get / set the comment of a line in the dissassembler?


5:
Is there a way to access the exposed Lua functions from a plugin? (like getPreviousOpcode and disassemblerview_getSelectedAddress)
I can understand that you don't want to mess with the "old" plugin system now that we have the new lua engine, but you can just add a "string ExecuteLuaScript(string)" (same as in the "LuaEngine" window) and automatically make all of CE's Lua functions available for plugin writers too Very Happy
Because I'd rather program with a C-like language than Lua Twisted Evil

Thank you so much for your support db <3
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

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

PostPosted: Mon Feb 11, 2013 4:22 am    Post subject: Reply with quote

1: It is the same algorithm
When you turn the mouse wheel it uses getpreviousopcode to find the best possible previous opcode according to those rules (disassembling multiple offsets and see which result has the most normal code)
Because the start opcode is now different the subsequent opcodes might be different as well.

2&3: I was going to add he disassembler class which provides specific data like "Lastinstruction.IsCall IsJump , ReferencesX" but I guess I can also add the dissected code class


4: Not yet, but I guess I could add functions for it

5:
Yes, the lua state is exposed to the plugin. Check out the forcedinjection plugin on the main site where I make use of lua

_________________
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
Chris12
Expert Cheater
Reputation: 1

Joined: 27 Apr 2012
Posts: 103

PostPosted: Mon Feb 11, 2013 10:21 am    Post subject: Reply with quote

IsCall, IsJump, flags would be cool to have in addition to ReferencedBy, ReferencingX. Very Happy

Yeah mofifying comments is really needed. It would allow providing the results of custom code-dissection passes over the modules.

Wouldn't it be better to let dissect code Do its job and then "fix" the instructions when they're not changed instead of trying to snap to a correct offset every time the asmView is scrolled? or let's the user manually lock/unlock the assembler view? It feels like a rather unsafe way to handle the code.

Well whatever it seems to be good enough for now Very Happy

Any idea when the disassembler flags and/or code dissect features and comment editting functions are exposed ?
I can't wait to code some awesome IDA style code-analysis plugins Very Happy

edit: Can't find where lua_dostring is defined in the forcedinjection plugin :/
I can only spot this at the end of the CESdk.h file:

Code:

//V5: Todo, implement function declarations
  VOID *ExecuteKernelCode;
  VOID *UserdefinedInterruptHook;
  VOID *GetLuaState;
  VOID *MainThreadCall;

But no lua_dostring in sight ...
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

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

PostPosted: Mon Feb 11, 2013 12:00 pm    Post subject: Reply with quote

You can press the left and right buttons to change the address by 1 in the disassembler, this is useful for joined instruction tricks

It will probably be in next version, but not sure when it gets implemented in the svn.


lua_dostring is an export of the lua 5.1 dll, just get the module handle to that dll, get the proc address, and call it with the lua state pointer

_________________
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
Chris12
Expert Cheater
Reputation: 1

Joined: 27 Apr 2012
Posts: 103

PostPosted: Mon Feb 11, 2013 1:39 pm    Post subject: Reply with quote

lua5.1-32.dll in the ce directory doesn't export lua_dostring.
do I have to use luaL_loadstring and lua_call ?
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

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

PostPosted: Mon Feb 11, 2013 1:49 pm    Post subject: Reply with quote

Ah yes, lua_dostring is just a macro/wrapper for loadstring/pcall. So yes, you'll need to call those two exports. (Just define that function yourself)
_________________
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
Chris12
Expert Cheater
Reputation: 1

Joined: 27 Apr 2012
Posts: 103

PostPosted: Tue Feb 12, 2013 1:05 am    Post subject: Reply with quote

Nice! I got most of the important Lua functions imported.
When I'm finished we will have a solid .Net plugin sdk.

Now I'll just have to wait until those features are exposed to Lua / the plugin system. If I can help somewhere please tell me Smile

Btw did you get a chance to checkout why my plugin might crash on startup but not when loaded from the ce settings (I tried delaying and loading with a Lua script too but it crashes too)

An Thank you for adding those requested features. I have a ton of ideas for awesome plugins which will make CE much more powerful Very Happy
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

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

PostPosted: Tue Feb 12, 2013 1:44 am    Post subject: Reply with quote

No, i can't really see what goes wrong, and i only have vs2008 which refuses to open the project
Perhaps the application. methods cause a problem when there hasn't been any window created yet.
Just comment out almost everything in initialize and see what causes it

Also, not that it matters too much, you're telling ce your plugin uses sdk version 1. That will cause ce to treat your plugin slightly different

_________________
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
Chris12
Expert Cheater
Reputation: 1

Joined: 27 Apr 2012
Posts: 103

PostPosted: Tue Feb 12, 2013 12:21 pm    Post subject: Reply with quote

I can't attach it here because it says I exceeded my storage space. So here's the link.
http://www.mediafire.com/?gm81g7pn6vgbh2n

It's now set to .net framework 3.0.
Also I removed nearly all code.
On vs2008 you can just open the project by going to file->new->project from available code.

Then add a reference to the dll in: packages/UnmanagedExports.1.2.3-Beta/lib/net/

You don't really have to compile the plugin yourself, it is included. You can also run ce in a debugger and see where it crashes.

Maybe changing it so CE uses the same loading technique at the beginning (and with lua: loadPlugin()), as it does when loading from the settings dialog will help?
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

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

PostPosted: Tue Feb 12, 2013 8:30 pm    Post subject: Reply with quote

Do you have a 64-bit dll I can test ? I currently don't have any debug environment for 32-bit ce. (Only at the very end when porting ce to 32-bit)

Does it still crash if you remove that showMessage line?

_________________
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
Chris12
Expert Cheater
Reputation: 1

Joined: 27 Apr 2012
Posts: 103

PostPosted: Tue Feb 12, 2013 9:53 pm    Post subject: Reply with quote

Yes it still crashes when I'm removing the showmessage. As I said, it crashes before any code is executed.
But I found a workaround. (more on that later)

I suspect it's because the dll isn't a normal dll with normal exports.
Loading it causes a real avalance of LoadLibrary calls because the CLR is loading itself.
The exported functions are actually only long-jumps to the CLR-JitCompiler.
When CE calls exports of the dll, the function compiles "itself" and runs.

Maybe CE is loading the plugins in an unusual way (not LoadLibrary)?
Or it's somehow calling the exports before the library is completly loaded (but I don't beleive that's possible since LoadLibrary is synchronous and holds a "loader lock").

For my workaround I took some time to coded an extra dll in c++ that spins up the CLR.
It transfers control to a managed "meta plugin" which in turn loads all managed plugins it can find in CEs plugin directory.

I planned to load each plugin into its own AppDomain but because of speed and complexity I trashed that idea (for now).

Works really well so far, so you can move this issue to low priority for now Smile
For now the proxy c++ dll is only 32bit, but it's on my list to also provide a 64 bit version.
Does CE already know if a dll is x86/x64? Luckily .NET supports "AnyCPU" so I only have to write my "real" plugins once. Smile

If you are still interested in fixing this bug I can compile a 64bit version of the old plugin for you though, just tell me.

By the way. Do you think it would be possible to also add functions (lua or not) to get and set those "exta-comments" or "annotations" at the top?


edit:
Everything's running smoothly now.
But there's definitly some race-condition with RegisterFunction(...)
It won't work or sometimes crashes when the main window isn't created yet.
I suggest delaying plugin loading until CEs mainwindow is fully shown. Even when GetMainWindowHandle() isn't 0 it sometimes doesnt work.
However a Sleep(50) fixes it.


edit2:
I don't know how exactly CE saves the information about xrefs it gathered by "code dissect", but it would be important to be able to add to this data.
If CE only saves this information as comments to functions (as seen in the pic i included here) and not inside some additional data structure then
"change comment" would be enough. Otherwise I'll need something like "addReference(address)" in addition to "getReferences(address)".
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

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

PostPosted: Wed Feb 13, 2013 4:47 am    Post subject: Reply with quote

ce uses the same code to load the plugin at startup as it does when using the settings. (pluginhandler.LoadPlugin() followed by pluginhandler.EnablePlugin(), which calls LoadLibrary and GetProcAddress)

The only difference is that it loads the plugin when the settings get loaded(After the window has been created, inside the onShow code), and at that time there is no multithreading going on, so I don't know why that sleep() would make any difference. (Unless one of the injected dll's has spawned a thread and is doing something to ce's memory, like initializing something your dll needs?)


(And yes, I noticed that the symbolhandler doesn't deal with this dll properly)

Quote:

Do you think it would be possible to also add functions (lua or not) to get and set those "exta-comments" or "annotations" at the top?

Do you mean the list of cross references to the specific address or the comments like "->kernel32.ReadProcessMemory"

For comments like "->kernel32.ReadProcessMemory" there will be DisassemblerObject.decodeLastParametersToString() to get the unmodified text, and getComment(address)/setComment(address,text) to set/get userdefined comments (Use %s to also display the old one)

For the cross references there will be addReference(fromAddress, toAddress, referencetype) and deleteReference(fromAddress, toAddress)
I'll probably have to rewrite the whole scanner though as this was implemented back when there was no Map or Tree support in the provided library (Current implementation does not allow deleting)

_________________
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
Chris12
Expert Cheater
Reputation: 1

Joined: 27 Apr 2012
Posts: 103

PostPosted: Wed Feb 13, 2013 2:19 pm    Post subject: Reply with quote

Dark Byte wrote:

The only difference is that it loads the plugin when the settings get loaded(After the window has been created, inside the onShow code), and at that time there is no multithreading going on, so I don't know why that sleep() would make any difference. (Unless one of the injected dll's has spawned a thread and is doing something to ce's memory, like initializing something your dll needs?)

I'm enabling VisualStyles, and spawning my own window. But I don't access any of CE's exported functions in the initializer or thread. I only use exported functions/lua when buttons are pressed.
Whatever, it works perfectly with a Sleep().

Quote:
(And yes, I noticed that the symbolhandler doesn't deal with this dll properly)

Not that it's really that important now that I have a workaround, but do you know how to fix it?
Because in theory there shouldn't be any difference between a native dll and one that has been created with [DllExport] attributes.
The only thing I can think of is that CE somehow calls functions before the CLR is completely initialized. But I don't know how that's possible since it's called in DllMain...

Quote:

Do you mean the list of cross references to the specific address or the comments like "->kernel32.ReadProcessMemory"

For comments like "->kernel32.ReadProcessMemory" there will be DisassemblerObject.decodeLastParametersToString() to get the unmodified text, and getComment(address)/setComment(address,text) to set/get userdefined comments (Use %s to also display the old one)

For the cross references there will be addReference(fromAddress, toAddress, referencetype) and deleteReference(fromAddress, toAddress)
I'll probably have to rewrite the whole scanner though as this was implemented back when there was no Map or Tree support in the provided library (Current implementation does not allow deleting)


Actually I need to be able to modify both types of comments / annotations. (red and blue)

By the way, the Italic font is a bit hard to read. I have a few low priority suggestions:
- Remove the italic
- Make a new line for each xref (IDA Style, referer green text in the pic below)
- Allow for custom colors or even HTML style color formating.

The display style here is already possible in CE. (When it shows the italic font, that line has double height)
It should also be used for multiline comments, which are not possible currently.
Like this:


This whole comment display stuff, colors, formatting isn't that important for now.
But I plan to add a ton of gathered information to the comments (comment column on the right) and the annotations (double line height stuff example: "0x1234 (Call)")

A quick question:
Is the Lua engine thread safe (especially the lua stack)?
Can it happen that after I call a function I get wrong return values because some other thread popped stuff of or on the stack??
Or does each thread have it's own threadlocal lua stack??

Thanks for the progress on those features.
I'll release a big plugin bundle when I'm done Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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