KevY007

DiskRecover.lua

Jul 25th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.28 KB | None | 0 0
  1. ------ Credits to Avarin for SHA-256 in LUA API (Without him this = impossible)
  2. --- Please rush down this is just SHA-256 code
  3. local mod32 = 2^32
  4. local sha_hashlen = 32
  5. local sha_blocksize = 64
  6.  
  7. local band    = bit32 and bit32.band or bit.band
  8. local bnot    = bit32 and bit32.bnot or bit.bnot
  9. local bxor    = bit32 and bit32.bxor or bit.bxor
  10. local blshift = bit32 and bit32.lshift or bit.blshift
  11. local upack   = unpack
  12.  
  13. local function rrotate(n, b)
  14.     local s = n/(2^b)
  15.     local f = s%1
  16.     return (s-f) + f*mod32
  17. end
  18. local function brshift(int, by) -- Thanks bit32 for bad rshift
  19.     local s = int / (2^by)
  20.     return s - s%1
  21. end
  22.  
  23. local H = {
  24. 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  25. 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
  26. }
  27.  
  28. local K = {
  29. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  30. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  31. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  32. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  33. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  34. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  35. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  36. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  37. }
  38.  
  39. local function incr(t, incr)
  40.     if 0xFFFFFFFF - t[1] < incr then
  41.         t[2] = t[2] + 1
  42.         t[1] = incr - (0xFFFFFFFF - t[1]) - 1      
  43.         else t[1] = t[1] + incr
  44.         end
  45.         return t
  46.     end
  47.  
  48.     local function preprocess(data)
  49.         local len = #data
  50.  
  51.         data[#data+1] = 0x80
  52.         while #data%64~=56 do data[#data+1] = 0 end
  53.         local l = incr({0,0}, len*8)
  54.         for i = 2, 1, -1 do
  55.             data[#data+1] = band(brshift(band(l[i], 0xFF000000), 24), 0xFF)
  56.             data[#data+1] = band(brshift(band(l[i], 0xFF0000), 16), 0xFF)
  57.             data[#data+1] = band(brshift(band(l[i], 0xFF00), 8), 0xFF)
  58.             data[#data+1] = band(l[i], 0xFF)
  59.         end
  60.         return data
  61.     end
  62.  
  63.     local function BE_toInt(bs, i)
  64.         return blshift((bs[i] or 0), 24) + blshift((bs[i+1] or 0), 16) + blshift((bs[i+2] or 0), 8) + (bs[i+3] or 0)
  65.     end
  66.  
  67.     local function digestblock(data, i, C)
  68.         local w = {}
  69.         for j = 1, 16 do w[j] = BE_toInt(data, i+(j-1)*4) end
  70.         for j = 17, 64 do
  71.             local v = w[j-15]
  72.             local s0 = bxor(bxor(rrotate(w[j-15], 7), rrotate(w[j-15], 18)), brshift(w[j-15], 3))
  73.             local s1 = bxor(bxor(rrotate(w[j-2], 17), rrotate(w[j-2], 19)), brshift(w[j-2], 10))
  74.             w[j] = (w[j-16] + s0 + w[j-7] + s1)%mod32
  75.         end
  76.         local a, b, c, d, e, f, g, h = upack(C)
  77.         for j = 1, 64 do
  78.             local S1 = bxor(bxor(rrotate(e, 6), rrotate(e, 11)), rrotate(e, 25))
  79.             local ch = bxor(band(e, f), band(bnot(e), g))
  80.             local temp1 = (h + S1 + ch + K[j] + w[j])%mod32
  81.             local S0 = bxor(bxor(rrotate(a, 2), rrotate(a, 13)), rrotate(a, 22))
  82.             local maj = bxor(bxor(band(a, b), band(a, c)), band(b, c))
  83.             local temp2 = (S0 + maj)%mod32
  84.             h, g, f, e, d, c, b, a = g, f, e, (d+temp1)%mod32, c, b, a, (temp1+temp2)%mod32
  85.         end
  86.         C[1] = (C[1] + a)%mod32
  87.         C[2] = (C[2] + b)%mod32
  88.         C[3] = (C[3] + c)%mod32
  89.         C[4] = (C[4] + d)%mod32
  90.         C[5] = (C[5] + e)%mod32
  91.         C[6] = (C[6] + f)%mod32
  92.         C[7] = (C[7] + g)%mod32
  93.         C[8] = (C[8] + h)%mod32
  94.         return C
  95.     end
  96.  
  97.     local mt = {
  98.     __tostring = function(a) return string.char(unpack(a)) end,
  99.     __index = {
  100.     toHex = function(self, s) return ("%02x"):rep(#self):format(unpack(self)) end,
  101.     isEqual = function(self, t)
  102.         if type(t) ~= "table" then return false end
  103.         if #self ~= #t then return false end
  104.         local ret = 0
  105.         for i = 1, #self do
  106.             ret = bit32.bor(ret, bxor(self[i], t[i]))
  107.         end
  108.         return ret == 0
  109.     end
  110. }
  111. }
  112.  
  113. local function toBytes(t, n)
  114.     local b = {}
  115.     for i = 1, n do
  116.         b[(i-1)*4+1] = band(brshift(band(t[i], 0xFF000000), 24), 0xFF)
  117.         b[(i-1)*4+2] = band(brshift(band(t[i], 0xFF0000), 16), 0xFF)
  118.         b[(i-1)*4+3] = band(brshift(band(t[i], 0xFF00), 8), 0xFF)
  119.         b[(i-1)*4+4] = band(t[i], 0xFF)
  120.     end
  121.     return setmetatable(b, mt)
  122. end
  123.  
  124. function digest(data)
  125.     data = data or ""
  126.     data = type(data) == "string" and {data:byte(1,-1)} or data
  127.  
  128.     data = preprocess(data)
  129.     local C = {upack(H)}
  130.     for i = 1, #data, 64 do C = digestblock(data, i, C) end
  131.     return toBytes(C, 8)
  132. end
  133.  
  134. function hmac(data, key)
  135.     local data = type(data) == "table" and {upack(data)} or {tostring(data):byte(1,-1)}
  136.     local key = type(key) == "table" and {upack(key)} or {tostring(key):byte(1,-1)}
  137.  
  138.     local blocksize = sha_blocksize
  139.  
  140.     key = #key > blocksize and digest(key) or key
  141.  
  142.     local ipad = {}
  143.     local opad = {}
  144.     local padded_key = {}
  145.  
  146.     for i = 1, blocksize do
  147.         ipad[i] = bxor(0x36, key[i] or 0)
  148.         opad[i] = bxor(0x5C, key[i] or 0)
  149.     end
  150.  
  151.     for i = 1, #data do
  152.         ipad[blocksize+i] = data[i]
  153.     end
  154.  
  155.     ipad = digest(ipad)
  156.  
  157.     for i = 1, blocksize do
  158.         padded_key[i] = opad[i]
  159.         padded_key[blocksize+i] = ipad[i]
  160.     end
  161.  
  162.     return digest(padded_key)
  163. end
  164.  
  165. function pbkdf2(pass, salt, iter, dklen)
  166.     local out = {}
  167.     local hashlen = sha_hashlen
  168.     local block = 1
  169.     dklen = dklen or 32
  170.  
  171.     while dklen > 0 do
  172.         local ikey = {}
  173.         local isalt = type(salt) == "table" and {upack(salt)} or {tostring(salt):byte(1,-1)}
  174.         local clen = dklen > hashlen and hashlen or dklen
  175.  
  176.         isalt[#isalt+1] = band(brshift(band(block, 0xFF000000), 24), 0xFF)
  177.         isalt[#isalt+1] = band(brshift(band(block, 0xFF0000), 16), 0xFF)
  178.         isalt[#isalt+1] = band(brshift(band(block, 0xFF00), 8), 0xFF)
  179.         isalt[#isalt+1] = band(block, 0xFF)
  180.  
  181.         for j = 1, iter do
  182.             isalt = hmac(isalt, pass)
  183.             for k = 1, clen do ikey[k] = bxor(isalt[k], ikey[k] or 0) end
  184.             if j % 200 == 0 then os.queueEvent("PBKDF2", j) coroutine.yield("PBKDF2") end
  185.         end
  186.         dklen = dklen - clen
  187.         block = block+1
  188.         for k = 1, clen do out[#out+1] = ikey[k] end
  189.     end
  190.  
  191.     return setmetatable(out, mt)
  192. end
  193. --[[----------------------------------------------------------
  194.                  Main Functions Begin
  195.                   SHA-256 Code END
  196. -----------------------------------------------------------]]--
  197. token = "def_tk_010sk25jsmiq25jgsm23915asddrasdf" -- Edit this only if you need FULL Protection and you had changed the token in your disk install file(If you dont remember your doomed, Bricked)
  198.  
  199. function encrypt(str, saltt)
  200.   return pbkdf2(str, saltt, 128):toHex()
  201. end
  202.  
  203. function Start()
  204.     term.clear()
  205.     term.setCursorPos(1, 1)
  206.     if not fs.exists("/disk/") then
  207.         oldCol = term.getTextColor()
  208.         if term.isColor() then
  209.             term.setTextColor(colors.red)
  210.         end
  211.         print("You dont have any disk inserted!")
  212.         print("Press ENTER once you have inserted the disk...")
  213.         if term.isColor() then
  214.             term.setTextColor(colors.black)
  215.         end
  216.         inx = read()
  217.         if term.isColor() then
  218.             term.setTextColor(oldCol)
  219.         end
  220.         Start()
  221.     end
  222.     oldCol = term.getTextColor()
  223.     if term.isColor() then
  224.         term.setTextColor(colors.lightBlue)
  225.     end
  226.     term.clear()
  227.     term.setCursorPos(1, 1)
  228.     print("Insert your Computer ID(The ID for which you are creating the disk)")
  229.     if term.isColor() then
  230.         term.setTextColor(colors.yellow)
  231.     end
  232.     compId = read()
  233.     if tonumber(compId) == nil then
  234.         term.clear()
  235.         term.setCursorPos(1, 1)
  236.         sleep(0.2)
  237.         if term.isColor() then
  238.             term.setTextColor(colors.red)
  239.         end
  240.         print("Its not a valid number!")
  241.         if term.isColor() then
  242.             term.setTextColor(oldCol)
  243.         end
  244.         sleep(1)
  245.         Start()
  246.     end
  247.     if term.isColor() then
  248.         term.setTextColor(colors.lightBlue)
  249.     end
  250.     term.clear()
  251.     term.setCursorPos(1, 1)
  252.     print("ID: " ..compId .." - Now type your password...")
  253.     if term.isColor() then
  254.         term.setTextColor(colors.yellow)
  255.     end
  256.     passWd = read()
  257.     if term.isColor() then
  258.         term.setTextColor(colors.lime)
  259.     end
  260.     term.clear()
  261.     term.setCursorPos(1, 1)
  262.     print("ID: " ..compId .." - Password: " ..passWd)
  263.     if term.isColor() then
  264.         term.setTextColor(colors.brown)
  265.     end
  266.     print("Is this the correct information? Type no to restart or anything to continue")
  267.     if term.isColor() then
  268.         term.setTextColor(colors.yellow)
  269.     end
  270.     correct = read()
  271.     if correct == "no" or correct == "No" or correct == "NO" or correct == "nO" or correct == "oN" then
  272.         if term.isColor() then
  273.             term.setTextColor(colors.red)
  274.         end
  275.         term.clear()
  276.         term.setCursorPos(1, 1)
  277.         print("You inserted invalid information, Restarting...")
  278.         if term.isColor() then
  279.             term.setTextColor(oldCol)
  280.         end
  281.         sleep(1)
  282.         Start()
  283.     else
  284.         term.clear()
  285.         term.setCursorPos(1, 1)
  286.         if term.isColor() then
  287.             term.setTextColor(colors.lime)
  288.         end
  289.         print("Creating the password in the disk file (Will overwrite if you have DiskLock.shc in disk)")
  290.         if fs.exists("/disk/DiskLock.shc") then
  291.             if term.isColor() then
  292.                 term.setTextColor(colors.blue)
  293.             end
  294.             print("Found DiskLock.shc, Deleting it..")
  295.             sleep(1)
  296.             fs.delete("/disk/DiskLock.shc")
  297.             term.clear()
  298.             term.setCursorPos(1, 1)
  299.         end
  300.         term.clear()
  301.         if term.isColor() then
  302.             oco2 = term.getTextColor()
  303.             term.setTextColor(colors.yellow)
  304.         end
  305.         print("Encrypting Now, It will take some time but you will get better results too!")
  306.         if term.isColor() then
  307.             term.setTextColor(oco2)
  308.         end
  309.         idwpass = compId ..":" ..token
  310.         enc_pass = encrypt(passWd, idwpass)
  311.         filex = fs.open("/disk/DiskLock.shc", "w")
  312.         if filex then
  313.             filex.write(enc_pass)
  314.             filex.close()
  315.             if term.isColor() then
  316.                 term.setTextColor(colors.green)
  317.             end
  318.             term.clear()
  319.             shell.run("eject", "left")
  320.             shell.run("eject", "right")
  321.             shell.run("eject", "top")
  322.             shell.run("eject", "bottom")
  323.             term.clear()
  324.             if term.isColor() then
  325.                 term.setTextColor(colors.green)
  326.             end
  327.             term.setCursorPos(1, 1)
  328.             print("Successfully created the password file...")
  329.             if term.isColor() then
  330.                 term.setTextColor(oldCol)
  331.             end
  332.             sleep(1)
  333.             error()
  334.         else
  335.             if term.isColor() then
  336.                 term.setTextColor(colors.red)
  337.             end
  338.             term.clear()
  339.             term.setCursorPos(1, 1)
  340.             print("Failed to create and write in the password file...")
  341.             if term.isColor() then
  342.                 term.setTextColor(oldCol)
  343.             end
  344.             Sleep(1)
  345.             error()
  346.         end
  347.         if term.isColor() then
  348.             term.setTextColor(colors.red)
  349.         end
  350.         print("An unexpected error has occured!")
  351.         if term.isColor() then
  352.             term.setTextColor(oldCol)
  353.         end
  354.         error()
  355.     end
  356. end
  357. Start()
Add Comment
Please, Sign In to add comment