t8rt0t

deobfuscator.lua

Jun 1st, 2024 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.04 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local function split(input,sep)
  4.     local t = {}
  5.     for str in input:gmatch("([^"..sep.."]+)") do
  6.         table.insert(t,str)
  7.     end
  8.     return t
  9. end
  10.  
  11. --Known issue, double square brackets are not supported
  12. local function popStrings(input)
  13.     local newStr,quotes = "",{}
  14.     local quoteType = "none" --'"',"'"
  15.     local tempStr = ""
  16.     local backSlash = false
  17.     for i=1,#input do
  18.         local char = input:sub(i,i)
  19.         if backSlash then
  20.             tempStr = tempStr .. char
  21.             backSlash = false --consume backslash
  22.         elseif char == quoteType or (char == "\n" and quoteType ~= "none") then --If char is end quote or newLine
  23.             table.insert(quotes,quoteType .. tempStr .. quoteType)
  24.             tempStr = ""
  25.             newStr = newStr .. '"s' .. #quotes .. '"'
  26.             quoteType = "none"
  27.         elseif char == "\\" then
  28.             tempStr = tempStr .. char
  29.             backSlash = not backSlash
  30.         elseif quoteType == "none" and (char == '"' or char == "'") then
  31.             newStr = newStr .. tempStr
  32.             tempStr = ""
  33.             quoteType = char
  34.         else
  35.             tempStr = tempStr .. char
  36.         end
  37.     end
  38.     newStr = newStr .. tempStr
  39.     return newStr,quotes
  40. end
  41. --Last step, insert the quotes back into the original data
  42. local function insertStrings(input,quotes,num)
  43.     if tonumber(num) and quotes[num] then
  44.         return input:gsub('"s'..num .. '"',quotes[num])
  45.     end
  46.     return input:gsub('"s%d+"',function(str) return quotes[tonumber(str:match("%d+"))] end)
  47. end
  48.  
  49. --REMOVE
  50.  
  51.  
  52. -- local function removeSpaceAroundChar(input,char)
  53. --     return input:gsub("%s*%".. char .. "%s*",char)
  54. -- end
  55.  
  56. -- --Input must not contain sub-strings
  57. -- --Known issue, double square brackets are popped off here instead
  58. -- local function deFluff(input)
  59. --     input = input:gsub("%-%-.-\n","\n") --pops off comments (format "-- ... newline")
  60. --     input = input:gsub("\\[%[%]]","") --remove stuff
  61. --     input = input:gsub("%[%[.-%]%]","") --Remove multiline comments
  62. --     input = input:gsub("%s+"," ") --Remove excessive whitespace
  63. --     for _,v in ipairs({",","(",")","[","]","{","}","*","/","=","#",".",":"}) do
  64. --         input = removeSpaceAroundChar(input,v)
  65. --     end
  66. --     return input
  67. -- end
  68.  
  69. -- local function getNameGen(mode)
  70. --     local num = 0
  71. --     if mode == "short" then
  72. --         return function() num = num + 1 return string.format("_%x",num) end
  73. --     else
  74. --         return function() num = num + 1 return string.rep("_",num) end
  75. --     end
  76. -- end
  77.  
  78. -- local function isValidVar(str)
  79. --     return str:match("[_%a][_%w]*") == str
  80. -- end
  81.  
  82. -- local function createBlankMap()
  83. --     local t = {
  84. --         ["toSlot"] = "toSlot",
  85. --         ["fromSlot"] = "fromSlot",
  86. --         ["compact"] = "compact",
  87. --         ["name"] = "name",
  88. --         ["count"] = "count",
  89. --         ["allow_repetitions"] = "allow_repetitions",
  90. --         ["in"] = "in"
  91. --     }
  92. --     for k,v in pairs(_G) do
  93. --         t[k] = k
  94. --     end
  95. --     return t
  96. -- end
  97.  
  98. -- --Input should be defluffed
  99. -- local function createVarRemap(input,quotes,mode)
  100. --     local nextName = getNameGen(mode)
  101. --     local map = createBlankMap()
  102. --     for args in input:gmatch("([_%a][_,%.%w]*)=[^=]") do --Normal declarations, like `local a = ...`. Avoids boolean expressions like ==
  103. --         args = split(args,",")
  104. --         for k,v in ipairs(args) do
  105. --             v = v:match("[^%.]*$")
  106. --             if isValidVar(v) and not map[v] then
  107. --                 map[v] = nextName()
  108. --             end
  109. --         end
  110. --     end
  111. --     for str in input:gmatch("%[.-%]=[^=]") do --`tab[key]=...` or `tab["key"]=...`
  112. --         str = str:sub(2,-4)
  113. --         if type(str) == "string" and not map[str] then --If normal number key, leave it alone
  114. --             if str:match('^"s%d+"$') then --If its a string "s#" key then add mappings to both the string and its replacement
  115. --                 map[str] = nextName()
  116. --                 local num = tonumber(str:match("%d+"))
  117. --                 if num and type(quotes[num]) == "string" and isValidVar(quotes[num]:gsub("'\"","")) then --If a valid variable
  118. --                     map[quotes[num]:gsub("'\"","")] = nextName()
  119. --                 end
  120. --             elseif isValidVar(str) then --Otherwise, if its a valid variable name
  121. --                 map[str] = nextName()
  122. --             end
  123. --         end
  124. --     end
  125. --     for fName,args in input:gmatch("function ([_%a][_%.%w]*[_%w]?)%((.-)%)") do --function name(var,...)
  126. --         fName = fName:match("[^%.]+$")
  127. --         if fName and not map[fName] then map[fName] = nextName() end
  128. --         args = split(args,",")
  129. --         for k,v in ipairs(args) do
  130. --             if isValidVar(v) and not map[v] then
  131. --                 map[v] = nextName()
  132. --             end
  133. --         end
  134. --     end
  135. --     for str in input:gmatch("function%((.-)%)") do --args of directly called functions
  136. --         args = split(str,",")
  137. --         for k,v in ipairs(args) do
  138. --             if isValidVar(v) and not map[v] then
  139. --                 map[v] = nextName()
  140. --             end
  141. --         end
  142. --     end
  143. --     for fName,args in input:gmatch("function [_%a][_%.%w]*:([_%a][_%w]*)%((.-)%)") do --The weird colon functions
  144. --         if not map[fName] then map[fName] = nextName() end
  145. --         args = split(args,",")
  146. --         for k,v in ipairs(args) do
  147. --             if isValidVar(v) and not map[v] then
  148. --                 map[v] = nextName()
  149. --             end
  150. --         end
  151. --     end
  152. --     for args in input:gmatch("for (.-)do") do --for loop
  153. --         args = split(args,", =")
  154. --         for k,v in ipairs(args) do
  155. --             if isValidVar(v) and not map[v] then
  156. --                 map[v] = nextName()
  157. --             end
  158. --         end
  159. --     end
  160. --     return map
  161. -- end
  162.  
  163. -- local function replaceVars(input,quotes,mode)
  164. --     local map = createVarRemap(input,quotes,mode)
  165. --     return input:gsub("[_%a][_%w]*",function(str) return map[str] or str end)
  166. -- end
  167.  
  168. -- local function obfuscate(input,mode)
  169. --     nostr,quotes = popStrings(input)
  170. --     compactstr = deFluff(nostr)
  171. --     newvars = replaceVars(compactstr,quotes,mode)
  172. --     output = insertStrings(newvars,quotes)
  173. --     return output
  174. -- end
  175.  
  176. --REMOVE
  177. local reservedWords = {["if"]=true,["for"]=true,["while"]=true,["true"]=true,["false"]=true,["nil"]=true,["and"]=true,["or"]=true,["not"]=true,["local"]=true,["return"]=true,["function"]=true,["require"]=true}
  178.  
  179. local function reFluff(nostr)
  180.     for k,v in pairs({"if","local","while","for","return","require"}) do
  181.         nostr = nostr:gsub("[^\n]"..v,function(nostr) return nostr:sub(1,1) .. "\n"..v end)
  182.     end
  183.     local str = ""
  184.     local line = ""
  185.     local lastWord = ""
  186.     local tabAmt = 0
  187.     for i=1,#nostr do
  188.         local char = nostr:sub(i,i)
  189.         if char:match("%a") then lastWord = lastWord .. char elseif char ~= " " then lastWord = "" end
  190.         if lastWord == "then" or lastWord == "do" or lastWord == "repeat" or line:match("function.+%)") then
  191.             tabAmt = tabAmt + 1
  192.             str = str .. line .. "\n"
  193.             line = string.rep("\t",tabAmt) or ""
  194.         elseif lastWord == "elseif" or lastWord == "end" or lastWord == "until" then
  195.             line = line:gsub("%a+$","",1) --remove lastword
  196.             str = str .. line .. "\n"
  197.             tabAmt = tabAmt - 1
  198.             line = string.rep("\t",tabAmt) or ""
  199.             line = line .. lastWord
  200.         elseif lastWord == "else" and not char:match("%a") then
  201.             line = line:gsub("%a+$","",1) --remove lastword
  202.             str = str .. line .. "\n"
  203.             line = string.rep("\t",math.max(tabAmt - 1,0)) or ""
  204.             str = str .. line .. "\n"
  205.             line = string.rep("\t",tabAmt) or ""
  206.             line = line .. lastWord
  207.         elseif char == "\n" then
  208.             str = str .. line .. "\n"
  209.             line = string.rep("\t",tabAmt) or ""
  210.         else
  211.             line = line .. char
  212.         end
  213.         -- elseif char == " " and not reservedWords[lastWord] and not line:match("for") and not line:match("if") and not line:match("while") then
  214.         --     str = str .. line .. "\n"
  215.         --     line = string.rep("\t",tabAmt) or ""
  216.         -- end
  217.     end
  218.     str = str .. line
  219.     str = str:gsub("\n%s+\n","\n")
  220.     return str
  221. end    
  222.  
  223. local function format(str)
  224.     nostr,quotes = popStrings(str)
  225.     fluff = reFluff(nostr)
  226.     output = insertStrings(fluff,quotes)
  227.     return output
  228. end
  229.  
  230. if type(args[1]) == "string" and fs.exists(args[1]) and not fs.isDir(args[1]) then
  231.     local file = fs.open(args[1],"r")
  232.     local data = file.readAll()
  233.     file.close()
  234.     local newData = format(data,args[2])
  235.     print(newData)
  236.     local key
  237.     repeat
  238.         local _,key = os.pullEvent("key")
  239.         if key == keys.q then return end
  240.     until key == keys.space or key == keys.enter
  241.     -- fs.delete(args[1])
  242.     -- local file = fs.open(args[1],"w+")
  243.     -- file.write(newData)
  244.     -- file.close()
  245.     print("success")
  246. end
Advertisement
Add Comment
Please, Sign In to add comment