Guest User

Untitled

a guest
Mar 25th, 2019
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.40 KB | None | 0 0
  1. -- XFU5K470R - Advanced Lua Obfuscator
  2. -- Copyright (C) 2012 LoDC
  3. -- I lost the actual source code when my file server hard drive corrupted.
  4. -- This is the best backup i could find
  5.  
  6. function obfuscate(code, level, mxLevel)
  7.     local function print(...) end
  8.     local concat = function(...) return table.concat({...}, "") end
  9.     math.randomseed(os and os.time() or tick())
  10.     level = level or 1
  11.     mxLevel = mxLevel or 3
  12.    
  13.     local a = ""
  14.     -- STRIP COMMENTS
  15.     code = code:gsub("(%-%-%[(=*)%[.-%]%2%])", "")
  16.     code = code:gsub("(%-%-[^\r\n]*)", "")
  17.    
  18.     -- RIP ALL CONSTANT STRINGS OUT
  19.     local function dumpString(x) return concat("\"", x:gsub(".", function(d) return "\\" .. string.byte(d) end), "\"") end
  20.     local function dumpString2(x)
  21.         local x2 = "\""
  22.         local x3 = ""
  23.         for _,__ in x:gmatch("%[(=*)%[(.-)%]%1%]") do
  24.             x3 = __:gsub(".", function(d) return "\\" .. string.byte(d) end)
  25.         end
  26.         return concat(x2, x3, x2)
  27.     end
  28.     local function GenerateSomeFluff()
  29.         local randomTable = { "N00BING N00B TABLE", "game.Workspace:ClearAllChildren()", "?????????", "game", "Workspace", "wait", "loadstring", "Lighting", "TeleportService", "error", "crash__", "_", "____", "\\\"FOOLED YA?!?!\\\"", "\\\"MWAHAHA H4X0RZ\\\"", "string", "table", "\\\"KR3D17 70 XFU5K470R\\\"", "string", "os", "tick", "\"system\"" }
  30.         --for i = 1, 100 do print(math.random(1, #randomTable)) end
  31.         local x = math.random(1, #randomTable)
  32.         if x > (#randomTable / 2) then
  33.             local randomName = randomTable[x]
  34.             return concat("local ", string.rep("_", math.random(5, 10)), " = ", "____[#____ - 9](", dumpString("loadstring(\"return " .. randomName .. "\")()"), ")\n")
  35.         elseif x > 3 then
  36.             return concat("local ", string.rep("_", math.random(5, 10)), " = ____[", math.random(1, 31), "]\n")
  37.         else -- x == 3, 2, or 1
  38.             return concat("local ", ("_"):rep(100), " = ", dumpString("XFU5K470R R00LZ"), "\n")
  39.         end
  40.     end
  41.     local function GenerateFluff()
  42.         --local x = { } for i = 1, math.random(2, 10) do table.insert(x, GenerateSomeFluff()) end return table.concat(x)
  43.         return GenerateSomeFluff()
  44.     end
  45.  
  46.     --a = "local _ = function(x) return string.char(x) end "
  47.     a = a .. "local CONSTANT_POOL = { "
  48.     local CONSTANT_POOL = { }
  49.     local i = 0
  50.     -- "
  51.     local last = ""
  52.     local instr = false
  53.     local foundOne = true
  54.     while foundOne do
  55.         foundOne = false
  56.         for i2 = 1, code:len() do
  57.             local c = code:sub(i2, i2)
  58.             if c == "\"" then
  59.                 if code:sub(i2 - 1, i2 - 1) == "\\" then
  60.                     if instr then
  61.                         last = last .. "\""
  62.                     end
  63.                 else
  64.                     instr = not instr
  65.                     if not instr then
  66.                         if not CONSTANT_POOL[last] then
  67.                             CONSTANT_POOL[last] = i
  68.                             a = a .. "[" .. i .. "]" .. " = " .. dumpString(last) .. ", "
  69.                             code = code:gsub("\"" .. last .. "\"", "(CONSTANT_POOL[" .. CONSTANT_POOL[last] .. "])")
  70.                             i = i + 1
  71.                         else
  72.                             code = code:gsub("\"" .. last .. "\"", "(CONSTANT_POOL[" .. CONSTANT_POOL[last] .. "])")
  73.                         end
  74.                         last = ""
  75.                         foundOne = true
  76.                         break
  77.                     end
  78.                 end
  79.             else
  80.                 if instr then
  81.                     last = last .. c
  82.                 end
  83.             end
  84.         end
  85.     end
  86.     -- '
  87.     local last = ""
  88.     local instr = false
  89.     local foundOne = true
  90.     while foundOne do
  91.         foundOne = false
  92.         for i2 = 1, code:len() do
  93.             local c = code:sub(i2, i2)
  94.             if c == "'" then
  95.                 if code:sub(i2 - 1, i2 - 1) == "\\" then
  96.                     if instr then
  97.                         last = last .. "'"
  98.                     end
  99.                 else
  100.                     instr = not instr
  101.                     if not instr then
  102.                         if not CONSTANT_POOL[last] then
  103.                             CONSTANT_POOL[last] = i
  104.                             a = a .. "[" .. i .. "]" .. " = " .. dumpString(last) .. ", "
  105.                             code = code:gsub("'" .. last .. "'", "(CONSTANT_POOL[" .. CONSTANT_POOL[last] .. "])")
  106.                             i = i + 1
  107.                         else
  108.                             code = code:gsub("'" .. last .. "'", "(CONSTANT_POOL[" .. CONSTANT_POOL[last] .. "])")
  109.                         end
  110.                         last = ""
  111.                         foundOne = true
  112.                         break
  113.                     end
  114.                 end
  115.             else
  116.                 if instr then
  117.                     last = last .. c
  118.                 end
  119.             end
  120.         end
  121.     end
  122.     -- [(=*)[
  123.     for var in code:gmatch("(%[(=*)%[.*%]%2%])") do
  124.     --for var in code:gmatch("%[(=*)%[.-%]%1%]") do
  125.         if not CONSTANT_POOL[var] then
  126.             a = a .. "[" .. i .. "]" .. " = " .. dumpString2(var) .. ", "
  127.             CONSTANT_POOL[var] = i
  128.             for i2 = 1, code:len() do
  129.                 if code:sub(i2, i2 + var:len() - 1) == var then
  130.                     code = code:sub(1, i2 - 1) .. "(CONSTANT_POOL[" .. i .. "])" .. code:sub(i2 + var:len())
  131.                 end
  132.             end
  133.             --code = code:gsub(var, "(CONSTANT_POOL[" .. CONSTANT_POOL[var] .. "])")
  134.             i = i + 1
  135.         end
  136.     end
  137.     a = a .. concat("[", i, "] = \"\88\70\85\53\75\52\55\48\82\32\49\53\32\52\87\51\53\48\77\51\46\32\75\82\51\68\49\55\32\55\48\32\88\70\85\53\75\52\55\48\82\33\"")
  138.     a = a .. " }\n"
  139. --print(code)
  140.  
  141. if level == 1 then
  142.     -- OBFUSCATES LOCAL FUNCTIONS AND VARIABLES
  143.     local chars = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuioplkjhgfdsazxcvbnm_"
  144.     local chars2 = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuioplkjhgfdsazxcvbnm_1234567890"
  145.     local taken = { }
  146.     taken[""] = true
  147.     local function GetReplacement()
  148.         local s = ""
  149.         while taken[s] do
  150.             local n = math.random(1, #chars)
  151.             s = s .. chars:sub(n, n)
  152.             for i = 1, math.random(6,20) do
  153.                 local n = math.random(1, #chars2)
  154.                 s = s .. chars2:sub(n, n)
  155.             end
  156.         end
  157.         taken[s] = true
  158.         return s
  159.     end
  160.     local library = {}
  161.    
  162.     -- functions
  163.     for fType in code:gmatch("local%s*function%s*([%w_]+)%(") do -- for fType in x:gmatch("(function %a+%(%))") do
  164.         local replacement = GetReplacement()
  165.         --print(fType)
  166.         if #fType > 5 then
  167.             library[fType] = replacement
  168.             code = code:gsub("function " .. fType, "function " .. replacement)
  169.         end
  170.     end
  171.    
  172.     for fCall in code:gmatch("([%w_]+)%s*%(") do -- for fCall in x:gmatch("(%a+)%(%)") do
  173.         if library[fCall] then
  174.             code = code:gsub(fCall .. '%(', library[fCall] .. '%(')
  175.         end
  176.     end
  177.    
  178.     local function isKeyword(s)
  179.         local s2 = "and break do else elseif end false for function if in local nil not or repeat return then true until"
  180.         for w in s2:gmatch("(%w+)") do
  181.             --if w:find(s) ~= nil then
  182.             --    return true
  183.             --end
  184.             --for i = 1, w:len() do
  185.             --    if s:sub(1, i) == w:sub(1, i) then
  186.             --        return true
  187.             --    end
  188.             --end
  189.             if w == s then return true end
  190.         end
  191.         return false
  192.     end
  193.    
  194.     for each in code:gmatch("local%s*([%w_]*)%s*=") do
  195.         if #each > 3 and not isKeyword(each) then
  196.             local varName = GetReplacement()
  197.             --code = code .. ">>"..varName.."<<"
  198.             code = code:gsub(each, varName)
  199.         end
  200.     end
  201. end
  202.  
  203.     -- STRIP ALL WHITESPACE (AT THIS POINT IT DOESN'T MATTER FOR VARIABLES, THEY'RE GONE)
  204.     --code = code:gsub("[\n]+", " ")
  205.     --code = code:gsub("[\r]+", " ")
  206.     --code = code:gsub("[\t]+", " ")
  207.     --code = code:gsub("[ ]+", " ") -- '  ' becomes ' '
  208.     code = code:gsub("(%s+)", " ")
  209.     a = a .. code
  210.    
  211.     -- CONVERT TO CHAR/TABLE/LOADSTRING THING
  212.     math.randomseed(os and os.time() or tick())
  213.     local __X = math.random()
  214.     local a2 = [[ math.randomseed(]] .. __X .. [[)
  215. local ____
  216. ____ = { function(...) local t = { ...} return ____[8](t) end, print, game, math.frexp, math.random(1, 1100), string.dump, string.sub, table.concat, wait, tick, loadstring, "t", function(x) local x2 = loadstring(x) if x2 then return ____[tonumber("\50\48")](function() x2() end) else return nil end end, "InsertService", 1234567890, getfenv, "", "wai", 7.2, pcall, math.pi, ""}
  217. ]] .. GenerateFluff() .. [[local ___ = ____[5]
  218. ]] .. GenerateFluff() .. [[local _ = function(x) return string.char(x / ___) end
  219. ]] .. GenerateFluff() .. [[local __ = {]]
  220.     math.randomseed(__X)
  221.     local ___X = math.random(1, 1100)
  222.     local a3 = { }
  223.     for i = 1, a:len() do
  224.         table.insert(a3, concat("_(", (string.byte(a:sub(i, i)) * ___X), "), "))
  225.     end
  226.     a2 = a2 .. table.concat(a3, "")
  227.     a2 = a2 .. " } \n"
  228.     a2 = a2 .. GenerateFluff()
  229.     a2 = a2 .. "return ____[11]((____[8](__)), ____[#____])()\n"
  230.     --a2 = a2 .. GenerateFluff() TODO
  231.     if level < mxLevel then -- more than 3 is over 1MB (for the simple test). WAY to long to use in a project.
  232.         print(concat("OBFUSCATED AT LEVEL ", level, " OUT OF ", mxLevel, " (" .. a:len() .. " Obfuscated characters)"))
  233.         return obfuscate(a2, level + 1, mxLevel)
  234.     else
  235.         print(concat("OBFUSCATED AT LEVEL ", level, " OUT OF ", mxLevel, " (", a:len(), " Obfuscated Characters) [Done]"))
  236.         a2 = a2:gsub("[\n]+", " ")
  237.         a2 = a2:gsub("[\r]+", " ")
  238.         a2 = a2:gsub("[\t]+", " ")
  239.         a2 = a2:gsub("[ ]+", " ") -- '  ' becomes ' '
  240.         return a2
  241.     end
  242. end
  243.  
  244. xfuscate = function(code)
  245.     return obfuscate(code, 1, 1)
  246. end
  247.  
  248. code = [[
  249. local function printhi()
  250.     local msg_to_print = 'hello world'
  251.     print(msg_to_print)
  252. end
  253.  
  254. printhi()
  255.  
  256. --print(CONSTANT_POOL[0])
  257. ]]
  258.  
  259. if arg and arg[1] then
  260.     code = io.open(arg[1], 'rb'):read'*a'
  261. end
  262.  
  263. local t1 = os and os.time() or tick()
  264. result = obfuscate(code, 1, 3)
  265. local t2 = os and os.time() or tick()
  266. print(result)
  267. a, b = loadstring(result)
  268. if a then
  269.     print"--Successful!"
  270.     a()
  271. else
  272.     print("--Failed: " .. b)
  273. end
  274. print("Time taken:", t2 - t1)
Add Comment
Please, Sign In to add comment