local f = fs.open("/file","r")
if not f then
return
end
local fn = loadstring(f.readAll())
f.close()
fn() -- the error is here
[bytecode][fs][error] how to load a dumped function from a file?
Started by tesla1889, Oct 27 2012 06:27 PM
11 replies to this topic
#1
Posted 27 October 2012 - 06:27 PM
I used string.dump on a function and wrote that dump into a file. When i use loadstring on the contents of the file, it gives me a function. The problem occurs when i try to use that function. I get the error "invalid object." How do you read bytecode from a file and turn it into a function?
#2
Posted 27 October 2012 - 06:33 PM
If you're using string.dump, you really should be using binary mode file operations (wb and rb for write and read, respectively). It's possible that non-binary mode usage is screwing up your function string.
#3
Posted 27 October 2012 - 08:01 PM
thanks, i'll try that and post the results
#4
Posted 27 October 2012 - 09:14 PM
if a file is opened in binary read mode, it only reads one byte at a time and returns the integer, so how do i reconstruct the dump of the function?
#5
Posted 27 October 2012 - 09:50 PM
You can read a byte and concatenate it to the string with string.char:
local file = fs.open("path/to/file", "rb")
if file then
local s = ""
local b = file.read()
while b do
s = s..string.char(:D/>/>
b = file.read()
end
file.close()
local f = loadstring(s)
if f then
f()
end
end
#6
Posted 27 October 2012 - 10:18 PM
still didnt work. the function comes up nil.
#7
Posted 27 October 2012 - 10:24 PM
Are you writing it to the file as binary?
Like this:
Like this:
local s = string.dump(someFunction)
local file = fs.open("path", "wb")
if file then
for i = 1, #s do
file.write(string.byte(i))
end
file.close()
end
#8
Posted 27 October 2012 - 10:29 PM
Okay, why are you doing this? I mean, you already have the working function, right?
#9
Posted 28 October 2012 - 05:22 AM
its closed source. i wanted a way to hide security features in my work, and the only way to do that is closed source
#10
Posted 28 October 2012 - 05:48 AM
You should google 'Security by obscurity'
/> if it's secure, then it shouldn't matter that people can see the code. also there are some decompilers for Lua 5.1 . So they can see it anyways.
(off-topic: in your signature: rs.setRedstone( ) is no valid CC function? )
(off-topic: in your signature: rs.setRedstone( ) is no valid CC function? )
#12
Posted 28 October 2012 - 06:30 AM
Like others say, if you can use loadstring on it, then others can see the code in a form that they'll be able to figure out how it works and what the vulnerabilities are. loadstring is not the right tool for this job.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users












