Advertisement
Communeguy

CG Clear Serv

Nov 22nd, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.49 KB | None | 0 0
  1. --CGClearServ v.0.0.3
  2. --Credential Passing Server
  3. --Must be saved as startup
  4. --Full Documentation unavailable.
  5.  
  6. --Begin Variable Config Block
  7. local reqChan = 1 -- The channel the server will want to receive requests on.
  8. local userChan = 2 -- the channel the server will send username information on
  9. local passChan = 3 -- The channel the server uses to send password information
  10. local credChan = 4 -- the channel the server uses to send credential information
  11. local keyChan = 5 -- the channel that the administrator uses to send current tables to the server.
  12. local downChan = 6 -- the channel that the server uses to talk to the client.
  13. local saltChan = 7 -- the channel that the server uses to talk to the client.
  14. local trust = "anything" -- the trust variable that is expected for requests.
  15. local mside = "direction" -- the direction of the modem.
  16.  
  17. --SHA-256 Hashing Implementation via GravityScore
  18. local MOD = 2^32
  19. local MODM = MOD-1
  20.  
  21. local function memoize(f)
  22.         local mt = {}
  23.         local t = setmetatable({}, mt)
  24.         function mt:__index(k)
  25.                 local v = f(k)
  26.                 t[k] = v
  27.                 return v
  28.         end
  29.         return t
  30. end
  31.  
  32. local function make_bitop_uncached(t, m)
  33.         local function bitop(a, b)
  34.                 local res,p = 0,1
  35.                 while a ~= 0 and b ~= 0 do
  36.                         local am, bm = a % m, b % m
  37.                         res = res + t[am][bm] * p
  38.                         a = (a - am) / m
  39.                         b = (b - bm) / m
  40.                         p = p*m
  41.                 end
  42.                 res = res + (a + b) * p
  43.                 return res
  44.         end
  45.         return bitop
  46. end
  47.  
  48. local function make_bitop(t)
  49.         local op1 = make_bitop_uncached(t,2^1)
  50.         local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  51.         return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  52. end
  53.  
  54. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  55.  
  56. local function bxor(a, b, c, ...)
  57.         local z = nil
  58.         if b then
  59.                 a = a % MOD
  60.                 b = b % MOD
  61.                 z = bxor1(a, b)
  62.                 if c then z = bxor(z, c, ...) end
  63.                 return z
  64.         elseif a then return a % MOD
  65.         else return 0 end
  66. end
  67.  
  68. local function band(a, b, c, ...)
  69.         local z
  70.         if b then
  71.                 a = a % MOD
  72.                 b = b % MOD
  73.                 z = ((a + b) - bxor1(a,b)) / 2
  74.                 if c then z = bit32_band(z, c, ...) end
  75.                 return z
  76.         elseif a then return a % MOD
  77.         else return MODM end
  78. end
  79.  
  80. local function bnot(x) return (-1 - x) % MOD end
  81.  
  82. local function rshift1(a, disp)
  83.         if disp < 0 then return lshift(a,-disp) end
  84.         return math.floor(a % 2 ^ 32 / 2 ^ disp)
  85. end
  86.  
  87. local function rshift(x, disp)
  88.         if disp > 31 or disp < -31 then return 0 end
  89.         return rshift1(x % MOD, disp)
  90. end
  91.  
  92. local function lshift(a, disp)
  93.         if disp < 0 then return rshift(a,-disp) end
  94.         return (a * 2 ^ disp) % 2 ^ 32
  95. end
  96.  
  97. local function rrotate(x, disp)
  98.     x = x % MOD
  99.     disp = disp % 32
  100.     local low = band(x, 2 ^ disp - 1)
  101.     return rshift(x, disp) + lshift(low, 32 - disp)
  102. end
  103.  
  104. local k = {
  105.         0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  106.         0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  107.         0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  108.         0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  109.         0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  110.         0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  111.         0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  112.         0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  113.         0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  114.         0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  115.         0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  116.         0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  117.         0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  118.         0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  119.         0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  120.         0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  121. }
  122.  
  123. local function str2hexa(s)
  124.         return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  125. end
  126.  
  127. local function num2s(l, n)
  128.         local s = ""
  129.         for i = 1, n do
  130.                 local rem = l % 256
  131.                 s = string.char(rem) .. s
  132.                 l = (l - rem) / 256
  133.         end
  134.         return s
  135. end
  136.  
  137. local function s232num(s, i)
  138.         local n = 0
  139.         for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  140.         return n
  141. end
  142.  
  143. local function preproc(msg, len)
  144.         local extra = 64 - ((len + 9) % 64)
  145.         len = num2s(8 * len, 8)
  146.         msg = msg .. "\128" .. string.rep("\0", extra) .. len
  147.         assert(#msg % 64 == 0)
  148.         return msg
  149. end
  150.  
  151. local function initH256(H)
  152.         H[1] = 0x6a09e667
  153.         H[2] = 0xbb67ae85
  154.         H[3] = 0x3c6ef372
  155.         H[4] = 0xa54ff53a
  156.         H[5] = 0x510e527f
  157.         H[6] = 0x9b05688c
  158.         H[7] = 0x1f83d9ab
  159.         H[8] = 0x5be0cd19
  160.         return H
  161. end
  162.  
  163. local function digestblock(msg, i, H)
  164.         local w = {}
  165.         for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  166.         for j = 17, 64 do
  167.                 local v = w[j - 15]
  168.                 local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  169.                 v = w[j - 2]
  170.                 w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  171.         end
  172.  
  173.         local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]
  174.         for i = 1, 64 do
  175.                 local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  176.                 local maj = bxor(band(a, b), band(a, c), band(b, c))
  177.                 local t2 = s0 + maj
  178.                 local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  179.                 local ch = bxor (band(e, f), band(bnot(e), g))
  180.                 local t1 = h + s1 + ch + k[i] + w[i]
  181.                 h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  182.         end
  183.  
  184.         H[1] = band(H[1] + a)
  185.         H[2] = band(H[2] + b)
  186.         H[3] = band(H[3] + c)
  187.         H[4] = band(H[4] + d)
  188.         H[5] = band(H[5] + e)
  189.         H[6] = band(H[6] + f)
  190.         H[7] = band(H[7] + g)
  191.         H[8] = band(H[8] + h)
  192. end
  193.  
  194. local function sha256(msg)
  195.         msg = preproc(msg, #msg)
  196.         local H = initH256({})
  197.         for i = 1, #msg, 64 do digestblock(msg, i, H) end
  198.         return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  199.                 num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  200. end
  201.  
  202. --Table/File Handling Protocols
  203. local function load(name)
  204. local file = fs.open(name, "r")
  205. local data = file.readAll()
  206. file.close()
  207. return textutils.unserialize(data)
  208. end
  209.  
  210. local function save(table, name)
  211. local file = fs.open(name, "w")
  212. file.write(textutils.serialize(table))
  213. file.close()
  214. end
  215.  
  216. --First Boot//Install Block
  217. --Checks for an existing database. If it can't find one, it creates a random one.
  218. if fs.exists("userhash") == false then
  219.     userHash = {[1]=sha256(tostring(math.random(1, 2^16))), }
  220.     save(userHash, "userhash")
  221.     print("Intializing Users.")
  222. end
  223. if fs.exists("passhash") == false then
  224.     passHash = {[1]=sha256(tostring(math.random(1, 2^16))), }
  225.     save(passHash, "passhash")
  226.     print("Intializing Passwords.")
  227. end
  228. if fs.exists("credhash") == false then
  229.     credHash = {[1]=sha256(tostring(math.random(1, 2^16))), } -- Virtually ensures that the default password won't be credentialled for anything anyway.
  230.     save(credHash, "credhash")
  231.     print("Initializing Credentials")
  232. end
  233. if fs.exists("tablesalt") == false then
  234.     tableSalt = {[1]=sha256(tostring(math.random(1, 2^16))), }
  235.     save(tableSalt, "tablesalt")
  236.     print("Salting Database")
  237. end
  238.  
  239. --Initialization
  240. modem = peripheral.wrap(mside)
  241. modem.open(reqChan)
  242. modem.open(keyChan)
  243. modem.open(userChan)
  244. modem.open(passChan)
  245. modem.open(credChan)
  246. modem.open(saltChan)
  247. print("Credentials Server Ready.")
  248.  
  249. while true do
  250.         local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message") -- Waits for a message, in this case a request.
  251.         if senderChannel == reqChan then -- Request Handling Block
  252.                 if message == sha256(trust) then -- this verified that the requester is allowed to have the data.
  253.                         print("Recieved Request for Credentials from" ..tostring(senderDistance) .."Away") -- For detection of bad machines.
  254.                         modem.transmit(downChan, reqChan, sha256(trust)) -- tells the client it is a valid server.
  255.                         os.pullEvent("modem_message") -- waits to be told to continue.
  256.                         local userHash = load("userHash")
  257.                         modem.transmit(userChan, reqChan, userHash) -- sends the userHash file as a string.
  258.                         os.pullEvent("modem_message")
  259.                         local passHash = load("passHash")
  260.                         modem.transmit(passChan, reqChan, passHash) -- sends passHash
  261.                         os.pullEvent("modem_message")
  262.                         local credHash = load("credhash")
  263.                         modem.transmit(credChan, reqChan, credHash) -- sends credHash
  264.                         os.pullEvent("modem_message")
  265.                         local tableSalt = load("tableSalt")
  266.                         modem.transmit(saltChan, reqChan, tableSalt) -- sends the salt.
  267.                         print("Update sent to Client upon Request.")
  268.                 else
  269.                 print("Unauthorized Request.")
  270.                 end
  271.         elseif senderChannel == keyChan then
  272.         print("Receiving Credentials Update.")
  273.                 if message == sha256(trust) then -- validates the admin machine.
  274.                         modem.close(reqChan) -- stops other requests from interrupting the system.
  275.                         modem.transmit(downChan, keyChan, sha256(trust)) -- tells the admin machine this is a valid server.
  276.                         os.pullEvent("modem_message")
  277.                         local userUpdate = textutils.unserialize(message)
  278.                         save(userUpdate, "userHash")
  279.                         print("Usernames Updated")
  280.                         modem.transmit(downChan, downChan, "ping") -- asks for the next packet.
  281.                         os.pullEvent("modem_message")
  282.                         local passUpdate = textutils.unserialize(message)
  283.                         save(passUpdate, "passHash")
  284.                         print("Passwords Updated")
  285.                         modem.transmit(downChan, keyChan, "ping")
  286.                         os.pullEvent("modem_message")
  287.                         local credUpdate = textutils.unserialize(message)
  288.                         save(credUpdate, "credHash")
  289.                         print("Credentials List Updated")
  290.                         modem.transmit(downChan, keyChan, "ping")
  291.                         os.pullEvent("modem_message")
  292.                         local saltUpdate = textutils.unserialize(message)
  293.                         save(saltUpdate, "tableSalt")
  294.                         print("Salts Updated")
  295.                         modem.open(reqChan) -- because people are going to need the new files.
  296.                         end
  297.                 else
  298.                         print("Spoofing Attempt Detected.")
  299.                 end
  300.                 print("Credentials server ready.")
  301.         end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement