Jump to content




[bytecode][fs][error] how to load a dumped function from a file?


  • You cannot reply to this topic
11 replies to this topic

#1 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

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?

local f = fs.open("/file","r")
if not f then
return
end
local fn = loadstring(f.readAll())
f.close()
fn() -- the error is here


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

Posted 27 October 2012 - 08:01 PM

thanks, i'll try that and post the results

#4 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

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 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

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 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

Posted 27 October 2012 - 10:18 PM

still didnt work. the function comes up nil.

#7 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 27 October 2012 - 10:24 PM

Are you writing it to the file as binary?
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 ChunLing

  • Members
  • 2,027 posts

Posted 27 October 2012 - 10:29 PM

Okay, why are you doing this? I mean, you already have the working function, right?

#9 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

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 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 28 October 2012 - 05:48 AM

You should google 'Security by obscurity' :D/> 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? )

#11 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 28 October 2012 - 06:13 AM

View PostOrwell, on 28 October 2012 - 05:48 AM, said:

(off-topic: in your signature: rs.setRedstone( ) is no valid CC function? )

I think he meant rs.setOutput()

#12 ChunLing

  • Members
  • 2,027 posts

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.





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users