Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local loadPool = {
- function()
- os.run( {}, "rom/programs/lua" )
- end
- }
- -- Feel free to set this to false if you don't want the text printed
- local showMessages = true
- if os.myEnv then
- if showMessages then
- print"Injection completed successfully"
- end
- -- Remove myEnv to prevent detection
- os.myEnv = nil
- return
- end
- local prevState = {}
- table.size = function(tbl)
- local count = 0
- for _,__ in pairs(tbl) do
- count = count + 1
- end
- return count
- end
- local function getParentShell()
- local at=0
- while true do
- at=at+1
- local ok,env = pcall(function() return getfenv(at) end)
- if not ok then break end
- if table.size(env) == 1 then
- local i,v = next(env) -- Grab first
- if i == "shell" then
- return v
- end
- end
- end
- return nil
- end
- local function recover()
- -- Set flag
- os.myEnv = true
- -- Put back the trampled environment
- os.startTimer = prevState.startTimer
- os.shutdown = prevState.shutdown
- os.pullEvent = prevState.pullEvent
- -- Launch shell like nothing happened
- prevState = nil
- term.setCursorPos(1,1)
- term.clear()
- if showMessages then
- -- Feel free to remove this line, It's just a proof thing
- print"Look at me, I load before shell does"
- end
- -- Because we killed rednet, we have to remove it and then reload it
- _G['rednet'] = nil
- os.loadAPI('/rom/apis/rednet')
- local ok, err = pcall( function()
- parallel.waitForAny(
- unpack(loadPool)
- )
- end )
- end
- os.stage = {}
- -- Stages:
- -- injected: Overriding os functions
- -- shutdown: Shutdown program has been called
- -- jstincse: Shell is doing it's just-in-case
- -- bioswait: Bios is waiting for a key press
- -- biosexit: Bios has quit
- -- complete: fully injected
- local function setStage(stage)
- os.stage.currentStage = stage
- end
- local function getStage()
- return os.stage.currentStage
- end
- local function _os_pullEvent(_filter)
- _filter = _filter or ""
- if _filter == "key" then
- setStage("bioswait")
- return "key", 0
- elseif _filter == "timer" then
- setStage("shutdown")
- return "timer", 0
- end
- end
- local function _replSleep(dur)
- local timer = prevState.startTimer( dur )
- repeat
- local sEvent, param = prevState.pullEvent( "timer" )
- until param == timer
- end
- local function _os_shutdown()
- if getStage() == "shutdown" then
- setStage("jstincse")
- elseif getStage() == "bioswait" then
- setStage("biosexit")
- end
- if getStage() == "biosexit" then
- recover()
- end
- end
- local function _os_startTimer(seconds)
- return 0
- end
- local function inject()
- prevState.startTimer = os.startTimer
- prevState.shutdown = os.shutdown
- prevState.pullEvent = os.pullEvent
- os.shutdown = _os_shutdown
- os.pullEvent = _os_pullEvent
- os.startTimer = _os_startTimer
- setStage("injected")
- local shellImpl = getParentShell()
- shellImpl.exit()
- end
- -- Start everything
- inject()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement