VMP3 - Lua support

Issues related to VMProtect
Post Reply
ChMo
Posts: 21
Joined: Mon Aug 26, 2013 11:54 am

VMP3 - Lua support

Post by ChMo »

First of all, I really like the new Lua scripting support. It opens up a lot of interesting usage scenarios. There are a few minor things that could still be improved.

C is 0-based, while Lua is 1-based. Thus if I do "for i = 1, obj:size() do obj:item(i)..." in Lua, then I'll access an invalid index.
Accessing an invalid index crashes VMP, so e.g. vmprotect.core():inputFile():item(100):functions()
I think there should be a check there that throws a Lua error in that case.
Aside from that it'd be really convenient to access everything 1-based, like in other Lua scripts.

The next thing is that the overall data access is a bit inconvenient. So to print the name of the input file I have to write

Code: Select all

print(vmprotect.core():inputFile():name())
This could easily be converted to plain table data access, so it works like this:

Code: Select all

print(vmprotect.core.inputFile.name)
Here is a quick & dirty utility function that wraps the vmprotect object to work that way:

Code: Select all

local WrapUserdata
local Meta = {
	__index = function(_t, _k)
		local func = _t.__UD[_k]
		assert(func, k)
		local result = func(_t.__UD)
		if type(result) == "userdata" then
			return WrapUserdata(result)
		else
			return result
		end
	end,
	__newindex = function()
		error("Please don't write to the API tables")
	end

}
function WrapUserdata(_UD)
	local t = {__UD = _UD}
	setmetatable(t, Meta)
	return t
end

local _vmprotect = WrapUserdata(vmprotect)
print(_vmprotect.core.inputFile.name)
Of course it'd be way more efficient to have this in the official API, than to add custom wrappers.
This wrapper i.e. doesn't work with array access, so I can't do

Code: Select all

vmprotect.core.inputFile.item[1].functions
let alone

Code: Select all

for i, file in ipairs(vmprotect.core.inputFile.item) do
However if the official API already returned everything with an __index metatable, potentially even with __pairs and __ipairs, then everything would be really convenient and feel like "native" Lua.
Currently the returned userdata only has a metatable that supports calling functions.

Aside from that I also have a general usage question. I don't need code for that, but would just like to know if this is the way to do it.
Let's assume I want to automatically add all functions that have "_secure" in their name and have a length >= 5 byte.
Everything that can be accessed via mapFunctions has a size of 0, at least if it's a function.
So I'll have to add all functions in the mapFile to the project (if they fit the name pattern), and then traverse the list of added functions. Only then I can get the size of the function, xrefs, etc. So if it doesn't fit I'll have to remove it. I guess there's some overhead in case I intend to do something like that with 10000+ functions that need to be checked?
Btw: Where did VMProtector.AddByAddress() go?

Last but not least I have a suggestion which might be a really nice killer feature. It requires some time to be added, even though some features of the existing mutation engine can probably be re-used: The list of instructions in the added function list is currently read only. It'd be great to be able to modify those instructions and to even insert new instructions (without breaking relative jumps, jump tables, etc of course). That way it'd be possible to have custom mutation add-ins that are executed as a pre-pass.
Admin
Site Admin
Posts: 2584
Joined: Mon Aug 21, 2006 8:19 pm
Location: Russia, E-burg
Contact:

Re: VMP3 - Lua support

Post by Admin »

C is 0-based, while Lua is 1-based. Thus if I do "for i = 1, obj:size() do obj:item(i)..." in Lua, then I'll access an invalid index.
In our case Lua provides C-style functions that are 0-based. Here is the correct code:

Code: Select all

for i = 0, obj:size() - 1 do obj:item(i)
Let's assume I want to automatically add all functions that have "_secure" in their name and have a length >= 5 byte.
I can offer to use the following example:

Code: Select all

file = core:inputArchitecture()
for i = 0, file:mapFunctions():count() - 1 do
	map_function = file:mapFunctions():item(i)
	if (map_function:type() == ObjectType.Code and map_function:name():sub(1, 8) == "_secure") then
		file:functions():addByAddress(map_function:address(), CompilationType.Virtualization)
	end
end
About "length >= 5" - you need yourself to calculate the length like this:

Code: Select all

func = file:functions():addByAddress(map_function:address(), CompilationType.Virtualization)
if (func) then
	length = 0
	for j = 0, func:count() - 1 do
		length = length + func:item(j):size()
	end
	if length < 5 then
		func:destroy()
	end
end
ChMo
Posts: 21
Joined: Mon Aug 26, 2013 11:54 am

Re: VMP3 - Lua support

Post by ChMo »

Thanks for the code snippets. So addByAddress is in file:functions() :)

VMP crashes for me though whenever I attempt to delete a function in my small sample project. I'm using the latest build 269.
I've tried 3 different ways of removing added functions:

Code: Select all

local file = vmprotect.core():inputFile():item(0)
local map_function = file:mapFunctions():item(0)
local added_func = file:functions():addByAddress(map_function:address(), CompilationType.None)
assert(added_func)
print(added_func:name())
added_func:destroy()

Code: Select all

vmprotect.core():inputFile():item(0):functions():clear()

Code: Select all

-- At least one item present in list of protected functions
vmprotect.core():inputFile():item(0):functions():delete(0)
Regarding the 1/0 thing: Yes, I'm aware that your Lua API is "C-style". I'm just saying that if someone wants to use your embedded Lua "like Lua", then the API should convert to Lua style. I've worked with C and Lua for many years, and I can say that: a) I don't like 0-based indexing in Lua, b) I don't like 1-based indexing in C. It's often a cause for errors and inconvenience. I just wanted to mention this, as the API isn't used in production yet - thus not set in stone. Of course it's your decision how to design your API. I'm already happy that you've chosen Lua :-)
Admin
Site Admin
Posts: 2584
Joined: Mon Aug 21, 2006 8:19 pm
Location: Russia, E-burg
Contact:

Re: VMP3 - Lua support

Post by Admin »

Thank you for your report. We are going to change our functions to LUA-style (first item will be has index 1 instead of 0).
Admin
Site Admin
Posts: 2584
Joined: Mon Aug 21, 2006 8:19 pm
Location: Russia, E-burg
Contact:

Re: VMP3 - Lua support

Post by Admin »

Post Reply