Advertisement
nostopgmaming17

Roblox luaC executor

Jul 21st, 2020
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --"Lua C" emulator in Lua by 3dsboy08
  2. --Credits to Josh for his amazing Lua API wrapper (https://github.com/NewVoids/LuaAPI/)
  3. local unpack = unpack or table.unpack
  4. local cache = {}
  5. local memory, address = {states = {}, threads = {}}, 0x0000001
  6. local types = {
  7.    LUA_TNONE = -1,
  8.    LUA_TNIL = 0,
  9.    LUA_TBOOLEAN = 1,
  10.    LUA_TLIGHTUSERDATA = 2,
  11.    LUA_TNUMBER = 3,
  12.    LUA_TSTRING = 4,
  13.    LUA_TTABLE = 5,
  14.    LUA_TFUNCTION = 6,
  15.    LUA_TUSERDATA = 7,
  16.    LUA_TTHREAD = 8
  17. }
  18. local gcactions = {
  19.    'LUA_GCSTOP',
  20.    'LUA_GCRESTART',
  21.    'LUA_GCCOLLECT',
  22.    'LUA_GCCOUNT',
  23.    'LUA_GCCOUNTB',
  24.    'LUA_GCSTEP',
  25.    'LUA_GCSETPAUSE',
  26.    'LUA_GCSETSTEPMUL'
  27. }
  28. local sign = function(n)
  29.    return n < 0 and -1 or n > 0 and 1 or 0
  30. end
  31. local gettype = function(...)
  32.    if select('#', ...) == 0 then
  33.       return types.LUA_TNONE
  34.    elseif type(...) == 'nil' then
  35.       return types.LUA_TNIL
  36.    elseif type(...) == 'boolean' then
  37.       return types.LUA_TBOOLEAN
  38.    elseif type(...) == 'number' then
  39.       return types.LUA_TNUMBER
  40.    elseif type(...) == 'string' then
  41.       return types.LUA_TSTRING
  42.    elseif type(...) == 'table' then
  43.       return types.LUA_TTABLE
  44.    elseif type(...) == 'function' then
  45.       return types.LUA_TFUNCTION
  46.    elseif type(...) == 'userdata' and #... ~= 'fthread' then
  47.       return types.LUA_TUSERDATA
  48.    elseif type(...) == 'thread' or #... == 'fthread' then
  49.       return types.LUA_TTHREAD
  50.    end
  51.    return types.LUA_TLIGHTUSERDATA
  52. end
  53. local push = function(L, v)
  54.    L.stacksize = L.stacksize + 1
  55.    L.stack[L.stacksize] = v
  56. end
  57. local getaddress = function()
  58.    address = address + 0x01
  59.    return string.format('0x%07x', address)
  60. end
  61. local stackKey = function(L, i)
  62.    return (sign(i) == -1 and (L.stacksize + 1) + i) or i
  63. end
  64. local get = function(L, i)
  65.    return L.stack[(sign(i) == -1 and (L.stacksize + 1) + i) or i]
  66. end
  67. local set = function(L, i, v)
  68.    L.stack[(sign(i) == -1 and (L.stacksize + 1) + i) or i] = v
  69. end
  70. local shift = function(L, s, n)
  71.    if sign(s) == -1 then
  72.       for i = ((sign(n) == -1 and (L.stacksize + 1) + n) or n) + 1, L.stacksize do
  73.          L.stack[i - 1] = L.stack[i]
  74.       end
  75.       set(L,  L.stacksize, nil)
  76.       L.stacksize = L.stacksize - 1
  77.    else
  78.       local new = {}
  79.       for i = (sign(n) == -1 and L.stacksize - n or n), L.stacksize do
  80.          new[i + 1] = L.stack[i]
  81.       end
  82.       for k, v in next, new do
  83.          L.stack[k] = v
  84.       end
  85.       set(L, n, nil)
  86.       L.stacksize = L.stacksize + 1
  87.    end
  88. end
  89. local pop = function(L, i)
  90.    local val = get(L, i)
  91.    set(L, i, nil)
  92.    shift(L, -1, i)
  93.    return val
  94. end
  95. local lua_functions = {
  96.    LUA_MULTRET = -math.huge,
  97.    luaL_newstate = function()
  98.       local state = newproxy(true)
  99.       getmetatable(state).__tostring = function() return 'state: ' .. memory[state] end
  100.       getmetatable(state).__index = function(...) error('Lua state libraries not open (luaL_openlibs(L))', 0) end
  101.       memory[state] = state
  102.       return state
  103.    end,
  104.    luaL_openlibs = function(L)
  105.       getmetatable(L).__index = {stack = {}, stacksize = 0, memory = {}}
  106.       getmetatable(L).__newindex = getmetatable(L).__index
  107.       return L
  108.    end,
  109.    luaL_loadstring = function(L, s)
  110.       return loadstring(s)
  111.    end,
  112.    lua_close = function(L)
  113.       memory[L] = nil
  114.    end,
  115.    lua_getglobal = function(L, g)
  116.       push(L, getfenv(0)[g] or cache[g])
  117.    end,
  118.    lua_setglobal = function(L, n)
  119.       cache[n] = get(L, -1)
  120.    end,
  121.    lua_getfenv = function(L, n)
  122.       push(L, setmetatable(cache, {__index = getfenv(n)}))
  123.    end,
  124.    lua_setfenv = function(L, n)
  125.       cache = {}
  126.       local instr = pop(L, -1)
  127.       setfenv(n, instr)
  128.       push(L, (type(instr) ~= 'function' and type(instr) ~= 'thread' and type(instr) ~= 'userdata' and 0) or 1)
  129.    end,
  130.    lua_getfield = function(L, n, k)
  131.       push(L, get(L, n)[k])
  132.    end,
  133.    lua_gettable = function(L, i)
  134.       push(L, get(L, i)[pop(L, -1)])
  135.    end,
  136.    lua_getmetatable = function(L, n)
  137.       if getmetatable(get(L, n)) and get(L, n) then
  138.          push(L, getmetatable(get(L, n)))
  139.       else
  140.          return 0
  141.       end
  142.    end,
  143.    lua_setmetatable = function(L, n)
  144.       set(L, n, setmetatable(get(L, n), pop(L, -1)))
  145.    end,
  146.    lua_createtable = function(L, n)
  147.       push(L, {})
  148.    end,
  149.    lua_settable = function(L, n)
  150.       get(L, n)[pop(L, -2)] = pop(L, -1)
  151.    end,
  152.    lua_settop = function(L, n)
  153.       if stackKey(L, n) < L.stacksize then
  154.          for i = stackKey(L, n) + 1, L.stacksize do
  155.             shift(L, -1, -1)
  156.          end
  157.       else
  158.          for i = L.stacksize, stackKey(L, n) do
  159.             push(L, nil)
  160.          end
  161.       end
  162.    end,
  163.    lua_pop = function(L, n)
  164.       for i = 1, n do
  165.          shift(L, -1, -1)
  166.       end
  167.    end,
  168.    lua_setfield = function(L, n, k)
  169.       get(L, n)[k] = pop(L, -1)
  170.    end,
  171.    lua_gettop = function(L)
  172.       return L.stacksize
  173.    end,
  174.    lua_toboolean = function(L, n)
  175.       return get(L, n) and 1 or 0
  176.    end,
  177.    lua_tointeger = function(L, n)
  178.       return tonumber(get(L, n))
  179.    end,
  180.    lua_tonumber = function(L, n)
  181.       return tonumber(get(L, n))
  182.    end,
  183.    lua_tostring = function(L, n)
  184.       return tostring(get(L, n))
  185.    end,
  186.    lua_tolstring = function(L, n, len)
  187.       return len and string.sub(get(L, n), 1, len) or tostring(get(L, n))
  188.    end,
  189.    lua_iscfunction = function(L, n)
  190.       return pcall(string.dump, get(L, n)) and 1 or 0
  191.    end,
  192.    lua_isfunction = function(L, n)
  193.       return L, type(get(L, n)) == 'function' and 1 or 0
  194.    end,
  195.    lua_isnil = function(L, n)
  196.       return get(L, n) == nil and 1 or 0
  197.    end,
  198.    lua_isnoneornil = function(L, n)
  199.       return get(L, n) == nil and 1 or 0
  200.    end,
  201.    lua_isthread = function(L, n)
  202.       return type(get(L, n)) == 'thread' and 1 or 0
  203.    end,
  204.    lua_istable = function(L, n)
  205.       return type(get(L, n)) == 'table' and 1 or 0
  206.    end,
  207.    lua_isuserdata = function(L, n)
  208.       return type(get(L, n)) == 'userdata' and 1 or 0
  209.    end,
  210.    lua_isstring = function(L, n)
  211.       return type(get(L, n)) == 'string' and 1 or 0
  212.    end,
  213.    lua_isnumber = function(L, n)
  214.       return type(get(L, n)) == 'number' and 1 or 0
  215.    end,
  216.    lua_isboolean = function(L, n)
  217.       return type(get(L, n)) == 'boolean' and 1 or 0
  218.    end,
  219.    lua_lessthan = function(L, n1, n2)
  220.       return get(L, n1) < get(L, n2) and 1 or 0
  221.    end,
  222.    lua_equal = function(L, n1, n2)
  223.       return get(L, n1) == get(L, n2) and 1 or 0
  224.    end,
  225.    lua_rawequal = function(L, n1, n2)
  226.       return rawequal(n1, n2) and 1 or 0
  227.    end,
  228.    lua_rawgeti = function(L, i, n)
  229.       return rawget(get(L, i), n)
  230.    end,
  231.    lua_rawseti = function(L, i, n)
  232.       return rawset(get(L, i)), n, pop(L, -1)
  233.    end,
  234.    lua_gettop = function(L, i)
  235.       return L.stacksize
  236.    end,
  237.    lua_next = function(L, i)
  238.       local res = next(get(L, -1))
  239.       push(L, res == nil and types.LUA_TNIL or res, i)
  240.    end,
  241.    lua_type = function(L, ...)
  242.       return gettype(...)
  243.    end,
  244.    lua_typename = function(L, n)
  245.       local tvalue = gettype(get(L, n))
  246.       for typename, value in next, types do
  247.          if tvalue == value then
  248.             return typename
  249.          end
  250.       end
  251.       return 'LUA_TNONE'
  252.    end,
  253.    lua_newthread = function(L, nresults)
  254.       local thread = newproxy(true)
  255.       local cache = {}
  256.       local f = {}
  257.       memory[L][thread] = getaddress()
  258.       getmetatable(thread).__tostring = function() return 'fthread: ' .. memory[L][thread] end
  259.       getmetatable(thread).__index = cache
  260.       getmetatable(thread).__len = function(self) return 'fthread' end
  261.       getmetatable(thread).__call = function(self, c)
  262.          if not cache[c] then
  263.             local rthread = coroutine.create(c)
  264.             cache[rthread] = getaddress()
  265.             cache[c] = rthread
  266.             return rthread
  267.          else
  268.             return cache[c]
  269.          end
  270.       end
  271.       push(L, thread)
  272.    end,
  273.    lua_yield = function(L, nresults)
  274.       return select(coroutine.yield(), 1, nresults)
  275.    end,
  276.    lua_resume = function(L, narg)
  277.       local f = L.stack[L.stacksize - narg]
  278.       local t = L.stack[L.stacksize - narg - 1]
  279.       local args, n = {}, 0
  280.       for i = (L.stacksize + 1) - narg, L.stacksize do
  281.          n = n + 1
  282.          args[n] = L.stack[i]
  283.       end
  284.       for i = (L.stacksize + 1) - narg, L.stacksize do
  285.          shift(L, -1, i)
  286.       end
  287.       local val = {pcall(coroutine.resume, t(f), unpack(args, 1, n))}
  288.       local s, m = val[1], {select(2, unpack(val))}
  289.       shift(L, -1, -1)
  290.       shift(L, -1, -1)
  291.       push(L, select(2, unpack(val)))
  292.    end,
  293.    lua_pushboolean = function(L, b)
  294.       assert(type(b) == 'boolean', 'Argument type "' .. type(b) .. '" is incompatible with parameter of type boolean')
  295.       push(L, b)
  296.    end,
  297.    lua_pushnil = function(L)
  298.       push(L, nil)
  299.    end,
  300.    lua_pushnumber = function(L, n)
  301.       assert(type(n) == 'number', 'Argument type "' .. type(n) .. '" is incompatible with parameter of type number')
  302.       push(L, n)
  303.    end,
  304.    lua_pushstring = function(L, s)
  305.       assert(type(s) == 'string', 'Argument type "' .. type(s) .. '" is incompatible with parameter of type string')
  306.       push(L, s)
  307.    end,
  308.    lua_pushtable = function(L, t)
  309.       assert(type(t) == 'table', 'Argument type "' .. type(t) .. '" is incompatible with parameter of type table')
  310.       push(L, t)
  311.    end,
  312.    lua_pushvalue = function(L, n)
  313.       push(L, get(L, n))
  314.    end,
  315.    lua_pushclosure = function(L, c)
  316.       assert(type(c) == 'function', 'Argument type "' .. type(c) .. '" is incompatible with parameter of type function')
  317.       push(L, c)
  318.    end,
  319.    lua_remove = function(L, n)
  320.       pop(L, n)
  321.       shift(L, -1, n)
  322.    end,
  323.    lua_insert = function(L, n)
  324.       local element = get(L, -1)
  325.       shift(L, 1, n)
  326.       set(L, n, element)
  327.    end,
  328.    lua_pcall = function(L, nargs, nresults, errfunc)
  329.       local f = L.stack[L.stacksize - nargs]
  330.       local args, n = {}, 0
  331.       for i = (L.stacksize + 1) - nargs, L.stacksize do
  332.          n = n + 1
  333.          args[n] = L.stack[i]
  334.       end
  335.       for i = (L.stacksize + 1) - nargs, L.stacksize do
  336.          shift(L, -1, i)
  337.       end
  338.       local val = {pcall(f, unpack(args, 1, n))}
  339.       local s, m = val[1], {select(2, unpack(val))}
  340.       local r = {unpack(m, 1, nresults == -math.huge and #m or nresults)}
  341.       shift(L, -1, -1)
  342.       if not s and errfunc ~= 0 then
  343.          push(L, get(L, errfunc)(m))
  344.       else
  345.          push(L, select(2, unpack(val)))
  346.       end
  347.    end,
  348.    lua_call = function(L, nargs, nresults)
  349.       local f = L.stack[L.stacksize - nargs]
  350.       assert(type(f) == 'function', 'Unprotected error in call to Lua API (attempt to call a ' .. type(f) .. ' value)')
  351.       local args, n = {}, 0
  352.       for i = (L.stacksize + 1) - nargs, L.stacksize do
  353.          n = n + 1
  354.          args[n] = L.stack[i]
  355.       end
  356.       for i = (L.stacksize + 1) - nargs, L.stacksize do
  357.          shift(L, -1, i)
  358.       end
  359.       local val = {f(unpack(args, 1, n))}
  360.       local r = {unpack(val, 1, nresults == -math.huge and #val or nresults)}
  361.       shift(L, -1, -1)
  362.       push(L, unpack(r))
  363.    end,
  364.    emptystack = function(L)
  365.       L.stack = {}
  366.       L.stacksize = 0
  367.    end
  368. }
  369.  
  370. local function emu(scr)
  371.    local L = luaL_newstate()
  372.    luaL_openlibs(L)
  373.  
  374.    local function reconstruct_string(t, idx)
  375.     local ret = ""
  376.     for i=idx,#t do
  377.         ret = ret .. t[i] .. " "
  378.     end
  379.     return ret:sub(1, -2)
  380.    end
  381.  
  382.    local function convert_number(str, pc)
  383.     local res = tonumber(str)
  384.     assert(type(res) == "number", "invalid number (pc = " .. tostring(pc) .. ")")
  385.     return res
  386.    end
  387.  
  388.    local pc = 1
  389.    for line in scr:gmatch("([^\n]*)\n?") do
  390.     local args = {}
  391.     for arg in string.gmatch(line, "%S+") do table.insert(args, arg) end
  392.     if #args >= 1 then
  393.         if args[1] == "getglobal" then
  394.             assert(#args >= 2, "invalid amount of arguments (getglobal, pc = " .. tostring(pc) .. ")")
  395.             lua_getglobal(L, reconstruct_string(args, 2))
  396.         elseif args[1] == "getfield" then
  397.             assert(#args >= 3, "invalid amount of arguments (getfield, pc = " .. tostring(pc) .. ")")
  398.             lua_getfield(L, convert_number(args[2], pc), reconstruct_string(args, 3))
  399.         elseif args[1] == "setfield" then
  400.             assert(#args >= 3, "invalid amount of arguments (setfield, pc = " .. tostring(pc) .. ")")
  401.             lua_setfield(L, convert_number(args[2], pc), reconstruct_string(args, 3))
  402.         elseif args[1] == "pushvalue" then
  403.             assert(#args == 2, "invalid amount of arguments (pushvalue, pc = " .. tostring(pc) .. ")")
  404.             lua_pushvalue(L, convert_number(args[2], pc))
  405.         elseif args[1] == "pcall" then
  406.             assert(#args == 4, "invalid amount of arguments (pcall, pc = " .. tostring(pc) .. ")")
  407.             lua_pcall(L, convert_number(args[2], pc), convert_number(args[3], pc), convert_number(args[4], pc))
  408.         elseif args[1] == "call" then
  409.             assert(#args == 3, "invalid amount of arguments (call, pc = " .. tostring(pc) .. ")")
  410.             lua_pcall(L, convert_number(args[2], pc), convert_number(args[3], pc))
  411.         elseif args[1] == "pushnumber" then
  412.             assert(#args == 2, "invalid amount of arguments (pushnumber, pc = " .. tostring(pc) .. ")")
  413.             lua_pushnumber(L, convert_number(args[2], pc))
  414.         elseif args[1] == "pushboolean" or args[1] == "pushbool" then
  415.             assert(#args == 2, "invalid amount of arguments (pushboolean, pc = " .. tostring(pc) .. ")")
  416.             if args[2] == "true" then
  417.                 lua_pushboolean(L, true)
  418.             elseif args[2] == "false" then
  419.                 lua_pushboolean(L, false)
  420.             else
  421.                 error("invalid boolean, pc = " .. tostring(pc))
  422.             end
  423.         elseif args[1] == "pushnil" then
  424.             lua_pushnil(L)
  425.         elseif args[1] == "pushstring" then
  426.             assert(#args >= 2, "invalid amount of arguments (pushstring, pc = " .. tostring(pc) .. ")")
  427.             lua_pushstring(L, reconstruct_string(args, 2))
  428.         elseif args[1] == "settop" then
  429.             assert(#args == 2, "invalid amount of arguments (settop, pc = " .. tostring(pc) .. ")")
  430.             lua_settop(L, convert_number(args[2], pc))
  431.         elseif args[1] == "remove" then
  432.             assert(#args == 2, "invalid amount of arguments (remove, pc = " .. tostring(pc) .. ")")
  433.             lua_remove(L, convert_number(args[2], pc))
  434.         elseif args[1] == "pop" then
  435.             assert(#args == 2, "invalid amount of arguments (pop, pc = " .. tostring(pc) .. ")")
  436.             lua_pop(L, convert_number(args[2], pc))
  437.         elseif args[1] == "emptystack" then
  438.             emptystack(L)
  439.         end
  440.         pc = pc + 1
  441.     end
  442.    end
  443.  
  444.    lua_close(L)
  445. end
  446.  
  447. local femu = setfenv(emu, setmetatable(lua_functions, {__index = getfenv(1)}))
  448.  
  449. local function create_gui()
  450.     local luac_emu = Instance.new("ScreenGui")
  451.     local Frame = Instance.new("Frame")
  452.     local TopLabel = Instance.new("TextLabel")
  453.     local ScriptBox = Instance.new("TextBox")
  454.     local ExecuteButton = Instance.new("TextButton")
  455.    
  456.     luac_emu.Name = "luac_emu"
  457.     luac_emu.Parent = game.Players.LocalPlayer.PlayerGui
  458.    
  459.     Frame.Parent = luac_emu
  460.     Frame.Active = true
  461.     Frame.BackgroundColor3 = Color3.new(0.176471, 0.176471, 0.176471)
  462.     Frame.BorderColor3 = Color3.new(0.176471, 0.176471, 0.176471)
  463.     Frame.Draggable = true
  464.     Frame.Position = UDim2.new(0, 400, 0, 230)
  465.     Frame.Size = UDim2.new(0, 440, 0, 330)
  466.    
  467.     TopLabel.Name = "TopLabel"
  468.     TopLabel.Parent = Frame
  469.     TopLabel.Active = true
  470.     TopLabel.BackgroundColor3 = Color3.new(0.109804, 0.109804, 0.109804)
  471.     TopLabel.BorderColor3 = Color3.new(0.109804, 0.109804, 0.109804)
  472.     TopLabel.Size = UDim2.new(0, 440, 0, 40)
  473.     TopLabel.Font = Enum.Font.SourceSansSemibold
  474.     TopLabel.Text = "Lua C Emulator"
  475.     TopLabel.TextColor3 = Color3.new(1, 1, 1)
  476.     TopLabel.TextSize = 16
  477.    
  478.     ScriptBox.Name = "ScriptBox"
  479.     ScriptBox.Parent = Frame
  480.     ScriptBox.BackgroundColor3 = Color3.new(0.109804, 0.109804, 0.109804)
  481.     ScriptBox.BorderColor3 = Color3.new(0.109804, 0.109804, 0.109804)
  482.     ScriptBox.Position = UDim2.new(0, 10, 0, 50)
  483.     ScriptBox.Size = UDim2.new(0, 420, 0, 220)
  484.     ScriptBox.Font = Enum.Font.SourceSansSemibold
  485.     ScriptBox.Text = ""
  486.     ScriptBox.TextColor3 = Color3.new(1, 1, 1)
  487.     ScriptBox.TextSize = 16
  488.     ScriptBox.TextXAlignment = Enum.TextXAlignment.Left
  489.     ScriptBox.TextYAlignment = Enum.TextYAlignment.Top
  490.     ScriptBox.MultiLine = true
  491.     ScriptBox.ClearTextOnFocus = false
  492.    
  493.     ExecuteButton.Name = "ExecuteButton"
  494.     ExecuteButton.Parent = Frame
  495.     ExecuteButton.BackgroundColor3 = Color3.new(0.109804, 0.109804, 0.109804)
  496.     ExecuteButton.BorderColor3 = Color3.new(0.109804, 0.109804, 0.109804)
  497.     ExecuteButton.Position = UDim2.new(0, 10, 0, 280)
  498.     ExecuteButton.Size = UDim2.new(0, 420, 0, 40)
  499.     ExecuteButton.Font = Enum.Font.SourceSansBold
  500.     ExecuteButton.Text = "Execute"
  501.     ExecuteButton.TextColor3 = Color3.new(1, 1, 1)
  502.     ExecuteButton.TextSize = 16
  503.  
  504.     return Frame
  505. end
  506.  
  507. local GUI = create_gui()
  508.  
  509. GUI.ExecuteButton.MouseButton1Click:connect(function()
  510.     if GUI.ScriptBox.Text ~= "" then
  511.         local check, message = pcall(function() femu(GUI.ScriptBox.Text) end)
  512.         if check then
  513.             GUI.ExecuteButton.Text = "Executed!"
  514.             wait(3)
  515.             GUI.ExecuteButton.Text = "Execute"
  516.         else
  517.             GUI.ExecuteButton.Text = "Failed to execute. Check devconsole."
  518.             spawn(function()
  519.                 error(message)
  520.             end)
  521.             wait(3)
  522.             GUI.ExecuteButton.Text = "Execute"
  523.         end
  524.     end
  525. end)
  526.  
  527. local function gui_toggle(_, state, __)
  528.     if state == Enum.UserInputState.Begin then
  529.         if not GUI.Visible then GUI.Visible = true else GUI.Visible = false end
  530.     end
  531. end
  532.  
  533. game.ContextActionService:BindAction("luac_exec_o", gui_toggle, false, Enum.KeyCode.O)
Advertisement
RAW Paste Data Copied
Advertisement