Advertisement
osmarks

PotatOS Obfuscation Tool

Jun 15th, 2020
1,591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.75 KB | None | 0 0
  1. local xoshiro128, xoshiro128genstate
  2.  
  3. do
  4.     -- http://prng.di.unimi.it/xoshiro128plusplus.c port
  5.  
  6.     local function normalize(x)
  7.         return x % 0x80000000
  8.     end
  9.  
  10.     local rotl = bit32.lrotate
  11.     local bxor = bit.bxor
  12.     local lshift = bit32.lshift
  13.  
  14.     local function statexor(s, i1, i2)
  15.         s[i1] = bxor(s[i1], s[i2])
  16.     end
  17.  
  18.     xoshiro128 = function(state)
  19.         local result = normalize(rotl(state[1] + state[4], 7) + state[1])
  20.         local t = lshift(state[2], 9)
  21.         statexor(state, 3, 1)
  22.         statexor(state, 4, 2)
  23.         statexor(state, 2, 3)
  24.         statexor(state, 1, 4)
  25.         state[3] = bxor(state[3], t)
  26.         state[4] = rotl(state[4], 11)
  27.         return result
  28.     end
  29.  
  30.     xoshiro128genstate = function()
  31.         local s = {normalize(os.epoch "utc"), math.random(0x7FFFFFFF), os.getComputerID(), math.random(0x7FFFFFFF)}
  32.         xoshiro128(s)
  33.         return s
  34.     end
  35. end
  36.  
  37. local oetemplate = [[local bitbxor, stringchar, tableconcat, tableinsert, bitband, LO, lrotate, lshift = bit.bxor, string.char, table.concat, table.insert, bit.band, 0x0F, bit32.lrotate, bit.blshift
  38.  
  39. local function statexor(s, i1, i2)
  40.     s[i1] = bitbxor(s[i1], s[i2])
  41. end
  42.  
  43. local function rand(s)
  44.     local result = (lrotate(s[1] + s[4], 7) + s[1]) % 0x80000000
  45.     local t = lshift(s[2], 9)
  46.     statexor(s, 3, 1)
  47.     statexor(s, 4, 2)
  48.     statexor(s, 2, 3)
  49.     statexor(s, 1, 4)
  50.     s[3] = bitbxor(s[3], t)
  51.     s[4] = lrotate(s[4], 11)
  52.     return result
  53. end
  54.  
  55. local function a(x)
  56.     local b = {}
  57.     for i = 1, #x, 2 do
  58.         local h = bitband(x:byte(i) - 33, LO)
  59.         local l = bitband(x:byte(i + 1) - 81, LO)
  60.         local s = (h * 0x10) + l
  61.         tableinsert(b, stringchar(bitbxor(rand(k) % 256, s)))
  62.     end
  63.     return tableconcat(b)
  64. end]]
  65.  
  66. local miniobftemplate = [[local k=%s;local a,b,c,d,e,f,g,A=bit.bxor,string.char,table.concat,table.insert,bit.band,0x0F,bit32.lrotate,bit.blshift;local function h(i,j,m)i[j]=a(i[j],i[m])end;local function n(i)local o=(g(i[1]+i[4],7)+i[1])%%0x80000000;local p=A(i[2],9)h(i,3,1)h(i,4,2)h(i,2,3)h(i,1,4)i[3]=a(i[3],p)i[4]=g(i[4],11)return o end;local function q(r)local s={}for t=1,#r,2 do local u=e(r:byte(t)-33,f)local l=e(r:byte(t+1)-81,f)local i=u*0x10+l;d(s,b(a(n(k)%%256,i)))end;return c(s)end]]
  67.  
  68. local function obfstrt(x)
  69.     local state = xoshiro128genstate()
  70.     local function encode(d)
  71.         local out = {}
  72.         for i = 1, #d do
  73.             local byte = bit.bxor(xoshiro128(state) % 256, d:byte(i))
  74.             local hi = bit.brshift(byte, 4) + bit.blshift(bit.band(0x02, byte), 3)
  75.             local lo = bit.band(0x0F, byte) + bit.band(0x10, byte)
  76.             table.insert(out, string.char(hi + 33))
  77.             table.insert(out, string.char(lo + 81))
  78.         end
  79.         return table.concat(out)
  80.     end
  81.     return miniobftemplate:format(textutils.serialise(state)) .. "\n" .. x:gsub(
  82.         "{{([^}]+)}}",
  83.         function(q) return ("(q%q)"):format(encode(q)) end
  84.     )
  85. end
  86.  
  87. local function bydump(code)
  88.     return string.dump(load(code, "@æ"))
  89. end
  90.  
  91. local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  92. local sgsub, sbyte, ssub, schar, sfind = string.gsub, string.byte, string.sub, string.char, string.find
  93.  
  94. -- encoding
  95. local function b64enc(data)
  96.     return ((sgsub(data, '.', function(x)
  97.         local r,b='',sbyte(x)
  98.         for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
  99.         return r;
  100.     end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
  101.         if (#x < 6) then return '' end
  102.         local c=0
  103.         for i=1,6 do c=c+(ssub(x,i,i)=='1' and 2^(6-i) or 0) end
  104.         return ssub(b, c+1,c+1)
  105.     end)..({ '', '==', '=' })[#data%3+1])
  106. end
  107.  
  108. local b64code = [[local a='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'local b,c,d,e,f=string.gsub,string.byte,string.sub,string.char,string.find;local function b64dec(g)g=b(g,'[^'..a..'=]','')return b(g,'.',function(h)if h=='='then return''end;local i,j='',f(a,h)-1;for k=6,1,-1 do i=i..(j%2^k-j%2^(k-1)>0 and'1'or'0')end;return i end):gsub('%d%d%d?%d?%d?%d?%d?%d?',function(h)if#h~=8 then return''end;local l=0;for k=1,8 do l=l+(d(h,k,k)=='1'and 2^(8-k)or 0)end;return e(l)end)end]]
  109. local maincode = [[load(potatOS.decompress(b64dec(%q)), "@a", "b")]]
  110.  
  111. local function bycomp(code)
  112.     local dumped = bydump(code)
  113.     local comp = potatOS.compress(dumped)
  114.     return b64code .. "\n" .. maincode:format(b64enc(comp))
  115. end
  116.  
  117. return {
  118.     obfstrt = obfstrt,
  119.     bydump = bydump,
  120.     nulzcod = nulzcod,
  121.     bycomp = bycomp
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement