Advertisement
osmarks

RCEoS Client

Oct 6th, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. RCEoR: a secure protocol for remote code execution over skynet (ComputerCraft)
  3.  
  4. This is free and unencumbered software released into the public domain.
  5.  
  6. Anyone is free to copy, modify, publish, use, compile, sell, or
  7. distribute this software, either in source code form or as a compiled
  8. binary, for any purpose, commercial or non-commercial, and by any
  9. means.
  10.  
  11. In jurisdictions that recognize copyright laws, the author or authors
  12. of this software dedicate any and all copyright interest in the
  13. software to the public domain. We make this dedication for the benefit
  14. of the public at large and to the detriment of our heirs and
  15. successors. We intend this dedication to be an overt act of
  16. relinquishment in perpetuity of all present and future rights to this
  17. software under copyright law.
  18.  
  19. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  22. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. OTHER DEALINGS IN THE SOFTWARE.
  26.  
  27. For more information, please refer to <http://unlicense.org/>
  28.  
  29. RCEoS v1.0
  30. Protocol: skynet channel "rceos", messages of the format:
  31. {code = [code to run], evil = [evilness of code]}
  32. live is also accepted as an alias for evil.
  33. ]]
  34.  
  35. local a=http.get"https://raw.githubusercontent.com/osmarks/skynet/master/client.lua"local b=fs.open("skynet","w")b.write(a.readAll())a.close()b.close()
  36. local skynet = dofile "./skynet"
  37.  
  38. local secureEnv = {
  39.     print = print,
  40.     http = http,
  41.     textutils = textutils,
  42.     bit = bit,
  43.     pcall = pcall,
  44.     xpcall = xpcall,
  45.     potatOS = potatOS,
  46.     colors = colors,
  47.     gps = gps,
  48.     pairs = pairs,
  49.     ipairs = ipairs,
  50.     math = math,
  51.     _HOST = _HOST .. " (RCEoS Sandbox)",
  52.     string = string,
  53.     skynet = skynet
  54. }
  55.  
  56. function clone(x)
  57.     local ty = type(x)
  58.     if ty == "table" then
  59.         local new = {}
  60.         for k, v in pairs(x) do
  61.             new[k] = clone(v)
  62.         end
  63.         return new
  64.     else
  65.         return x
  66.     end
  67. end
  68.  
  69. local argv = {...}
  70. local secure = true
  71.  
  72. if argv[1] == "insecure" then
  73.     secure = false
  74. end
  75.  
  76. local coros = {}
  77.  
  78. function run(code)
  79.     print("Executing", code)
  80.  
  81.     if secure then
  82.         local f = load(code, "@<RCEoS input>", "t", clone(secureEnv))
  83.         if f then
  84.             table.insert(coros, coroutine.create(function()
  85.                 local _, result = pcall(f)
  86.                 print(textutils.serialise(result))
  87.             end))
  88.         else
  89.             print "Invalid code: EVIL?!!?!?!?!?!"
  90.         end
  91.     else
  92.         print(textutils.serialise(loadstring(code)()))
  93.     end
  94. end
  95.  
  96. function receiver()
  97.     while true do
  98.         local sender, message = skynet.receive "rceos"
  99.         if message and message.code then
  100.             if secure then
  101.                 if message.evil or message.live then
  102.                     print "Evil message detected"
  103.                 else
  104.                     print "Running securely"
  105.                     run(message.code)
  106.                 end
  107.             else
  108.                 print "Running"
  109.                 run(message.code)
  110.             end
  111.         end
  112.     end
  113. end
  114.  
  115. if not secure then receiver() end
  116.  
  117. table.insert(coros, coroutine.create(receiver))
  118.  
  119. while true do
  120.     local ev = {os.pullEvent()}
  121.     for k, c in pairs(coros) do
  122.         if coroutine.status(c) == "dead" then coros[k] = nil end
  123.         coroutine.resume(c, table.unpack(ev))
  124.     end
  125. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement