SHOW:
|
|
- or go back to the newest paste.
1 | --[[ | |
2 | - | RCEoR: a secure protocol for remote code execution over rednet (ComputerCraft) |
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 | - | RCEoR v1.1 |
29 | + | RCEoS v1.0 |
30 | - | Protocol: rednet protocol "rceor", messages of the format: |
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 | - | rednet = rednet, |
38 | + | |
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 | - | _HOST = _HOST .. " (RCEoR Sandbox)", |
47 | + | |
48 | - | string = string |
48 | + | |
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 | - | elseif ty == "function" then |
58 | + | |
59 | - | return function(...) return x(...) end |
59 | + | |
60 | for k, v in pairs(x) do | |
61 | new[k] = clone(v) | |
62 | end | |
63 | return new | |
64 | else | |
65 | - | secureEnv.rednet.close = nil |
65 | + | |
66 | end | |
67 | end | |
68 | ||
69 | local argv = {...} | |
70 | local secure = true | |
71 | ||
72 | if argv[1] == "insecure" then | |
73 | secure = false | |
74 | - | for _, n in pairs(peripheral.getNames()) do -- open modems |
74 | + | |
75 | - | if pcall(rednet.open, n) then |
75 | + | |
76 | - | print("Opened a modem") |
76 | + | |
77 | ||
78 | function run(code) | |
79 | print("Executing", code) | |
80 | ||
81 | if secure then | |
82 | - | local function safeish_serialize(x) |
82 | + | local f = load(code, "@<RCEoS input>", "t", clone(secureEnv)) |
83 | - | local ok, res = pcall(textutils.serialize, x) |
83 | + | |
84 | - | if ok then return res |
84 | + | |
85 | - | else return tostring(x) end |
85 | + | local _, result = pcall(f) |
86 | print(textutils.serialise(result)) | |
87 | end)) | |
88 | else | |
89 | print "Invalid code: EVIL?!!?!?!?!?!" | |
90 | end | |
91 | else | |
92 | - | local f = load(code, "@<RCEoR input>", "t", clone(secureEnv)) |
92 | + | print(textutils.serialise(loadstring(code)())) |
93 | end | |
94 | end | |
95 | - | local result = {pcall(f)} |
95 | + | |
96 | - | print(safeish_serialise(result)) |
96 | + | |
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(safeish_serialise(loadstring(code)())) |
102 | + | |
103 | else | |
104 | print "Running securely" | |
105 | run(message.code) | |
106 | end | |
107 | else | |
108 | - | local sender, message = rednet.receive "rceor" |
108 | + | |
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 |