Advertisement
GopherAtl

lua2bin (cc lua)

Jul 25th, 2013
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.38 KB | None | 0 0
  1. --[[
  2.  
  3. lua2bin
  4.  
  5. loads a lua program, compiles it to bytecode, then outputs a new program
  6. that contains that bytecode, in base64, and when run, decodes it and
  7. launches the original program.
  8.  
  9. used for code obfuscation in lua.
  10. --]]
  11.  
  12. local tArgs={...}
  13.  
  14. local function printUsage()
  15.   print([[Usage:
  16. lua2bin <luafile> <output>
  17. <luafile> should be the name of the lua program to encrypt
  18. <output> should be the name of the output file.
  19. Note this process is NOT REVERSABLE, so output must be differnt from input!]])
  20.   error()
  21. end
  22.  
  23. if #tArgs~=2 then
  24.   printUsage()
  25. end
  26.  
  27. if not fs.exists(tArgs[1]) then
  28.   error("Input file \""..tArgs[1].."\" does not exist!")
  29. end
  30. if fs.exists(tArgs[2]) then
  31.   error("output file \""..tArgs[2].."\" already exists!")
  32. end
  33.  
  34. local base64="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"
  35. local base64r={}
  36. for i=1,64 do
  37.   base64r[base64:sub(i,i)]=i-1
  38. end
  39.  
  40. local function _8to6(src)
  41.   local out=""
  42.   local bits=0
  43.   local numBits=0
  44.   for i=1,#src do
  45.     bits=bits+bit.blshift(src:byte(i),numBits)
  46.     numBits=numBits+8
  47.     while numBits>=6 do
  48.       local byte=bit.band(bits,0x3f)
  49.       bits=bit.brshift(bits,6)
  50.       numBits=numBits-6
  51.       out=out..base64:sub(byte+1,byte+1)
  52.     end
  53.   end
  54.   --append last bits
  55.   if numBits>0 then
  56.     out=out..base64:sub(bits+1,bits+1)
  57.   end
  58.   return out
  59. end
  60.  
  61.  
  62. local file1=[[
  63. --file autogenrated by lua2bin, by GopherAtl.
  64.  
  65. --look-up table for decoding the base64
  66. local base64r=]]
  67.  
  68. local file2=[[
  69.  
  70.  
  71. --this function converts the encoded base64 back into binary
  72. local function _6to8(src)
  73.   local out=""
  74.   local bits=0
  75.   local numBits=0
  76.   for i=1,#src do
  77.     bits=bits+bit.blshift(base64r[string.char(src:byte(i))],numBits)
  78.     numBits=numBits+6
  79.     while numBits>=8 do
  80.       local byte=bit.band(bits,0xff)
  81.       out=out..string.char(byte)
  82.       bits=bit.brshift(bits,8)
  83.       numBits=numBits-8
  84.     end
  85.   end
  86.   return out
  87. end
  88.  
  89. local code64="]]
  90.  
  91.  
  92.  
  93. local file3=[["
  94.  
  95.  
  96. local srcBin=_6to8(code64)
  97. local func=loadstring(srcBin)
  98. func(...)]]
  99.  
  100.  
  101. local file=fs.open(tArgs[1],"r")
  102. local text=file.readAll()
  103. file.close()
  104.  
  105. local func=loadstring(text)
  106. local binText=string.dump(func)
  107. local text64=_8to6(binText)
  108.  
  109. file=fs.open(tArgs[2],"w")
  110. file.write(file1)
  111. file.write(textutils.serialize(base64r))
  112. file.write(file2)
  113. file.write(text64)
  114. file.write(file3)
  115. file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement