Advertisement
GravityScore

Antivirus v1.0

Sep 30th, 2012
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.86 KB | None | 0 0
  1.  
  2. --  -------- Rescue Disk
  3. --  -------- Made by 1lann, GravityScore
  4. --  -------- With Thanks To this_is_1984
  5.  
  6.  
  7. --  -------- Variables
  8.  
  9. -- Version
  10. local antivirusVersion = "1.0"
  11.  
  12. -- Dropbox URLs
  13. local antivirusURL = "http://dl.dropbox.com/u/97263369/antivirus/antivirus-stable.lua"
  14. local databaseURL = "http://dl.dropbox.com/u/97263369/antivirus/database.txt"
  15.  
  16. -- Locations
  17. local antivirusLocation = "/" .. shell.getRunningProgram()
  18. local mountPath = "/disk"
  19. local databaseLocation = "/.rescue-disk-database"
  20.  
  21. -- Clock Variables
  22. local clockRunning = true
  23. local clockVisible = true
  24. local toTerminal = false
  25.  
  26.  
  27. --  -------- Prompt Software
  28.  
  29. function prompt(list, dir)
  30.     -- Functions
  31.     local function drawArrows(word, x, y)
  32.         term.setCursorPos(x, y)
  33.         write("[")
  34.         term.setCursorPos(x + 1 + string.len(word), y)
  35.         write("]")
  36.     end
  37.    
  38.     local function removeArrows(word, x, y)
  39.         term.setCursorPos(x, y)
  40.         write(" ")
  41.         term.setCursorPos(x + 1 + string.len(word), y)
  42.         write(" ")
  43.     end
  44.  
  45.     -- Variables
  46.     local curSel = 1
  47.     local c1 = 200
  48.     local c2 = 208
  49.     if dir == "horizontal" then c1 = 203 c2 = 205 end
  50.  
  51.     -- Draw
  52.     for i = 1, #list do
  53.         if list[i][2] == -1 then
  54.             local w, h = term.getSize()
  55.             list[i][2] = math.ceil(w/2 - list[i][1]:len()/2)
  56.         end
  57.         term.setCursorPos(list[i][2], list[i][3])
  58.         write(list[i][1])
  59.     end
  60.     drawArrows(list[curSel][1], list[curSel][2] - 1, list[curSel][3])
  61.  
  62.     -- Selection
  63.     while not(exitApp) do
  64.         local event, key = os.pullEvent("key")
  65.         removeArrows(list[curSel][1], list[curSel][2] - 1, list[curSel][3])
  66.         if key == c1 then
  67.             if curSel ~= 1 then curSel = curSel - 1 end
  68.         elseif key == c2 then
  69.             if curSel ~= #list then curSel = curSel + 1 end
  70.         elseif key == 28 then
  71.             return list[curSel][1]
  72.         end
  73.         drawArrows(list[curSel][1], list[curSel][2] - 1, list[curSel][3])
  74.     end
  75. end
  76.  
  77.  
  78. --  -------- Drawing
  79.  
  80. local function centerPrint(text)
  81.     local w, h = term.getSize()
  82.     local x, y = term.getCursorPos()
  83.     term.setCursorPos(math.ceil((w + 1)/2 - text:len()/2), y)
  84.     print(text)
  85. end
  86.  
  87. local function centerWrite(text)
  88.     local w, h = term.getSize()
  89.     local x, y = term.getCursorPos()
  90.     term.setCursorPos(math.ceil((w + 1)/2 - text:len()/2), y)
  91.     write(text)
  92. end
  93.  
  94. local function clearPage()
  95.     local title = "Rescue Disk " .. antivirusVersion
  96.  
  97.     -- Title
  98.     term.clear()
  99.     term.setCursorPos(2, 1)
  100.     write(title)
  101.  
  102.     -- Line
  103.     local w, h = term.getSize()
  104.     term.setCursorPos(1, 1)
  105.     write(string.rep("-", w))
  106.     print(" ")
  107. end
  108.  
  109.  
  110. --  -------- Updating
  111.  
  112. local database = {}
  113.  
  114. local function getDropbox(url, loc)
  115.     sleep(0.01)
  116.     while not(exitApp) do
  117.         http.request(url)
  118.         sleep(0.0000000001)
  119.         while not(exitApp) do
  120.             local event, _, response = os.pullEvent()
  121.             if event == "http_failure" then
  122.                 return "false"
  123.             elseif event == "http_success" and response ~= nil then
  124.                 local text = response:readAll()
  125.                 local f = io.open(loc, "w")
  126.                 sleep(0.01)
  127.                 f:write(text)
  128.                 sleep(0.01)
  129.  
  130.                 f:close()
  131.                 response:close()
  132.                 return "true"
  133.             end
  134.         end
  135.     end
  136. end
  137.  
  138. local function updateClient()
  139.     local updateLocation =  "/.rescue-disk-update"
  140.     fs.delete(updateLocation)
  141.  
  142.     getDropbox(antivirusURL, updateLocation)
  143.     local f1 = io.open(updateLocation, "r")
  144.     local f2 = io.open(antivirusLocation, "r")
  145.     local update = f1:read("*a")
  146.     local current = f2:read("*a")
  147.     f1:close()
  148.     f2:close()
  149.  
  150.     if current ~= update then
  151.         fs.delete(antivirusLocation)
  152.         fs.move(updateLocation, antivirusLocation)
  153.         shell.run(antivirusLocation)
  154.         error()
  155.     else
  156.         fs.delete(updateLocation)
  157.     end
  158. end
  159.  
  160. local function getDatabase()
  161.     fs.delete(databaseLocation)
  162.     getDropbox(databaseURL, databaseLocation)
  163.     local f = io.open(databaseLocation)
  164.     local lines = {}
  165.     local a = f:read("*l")
  166.     while a ~= nil do
  167.         table.insert(lines, a)
  168.         a = f:read("*l")
  169.     end
  170.     f:close()
  171.  
  172.     database = {}
  173.     local curItem = {}
  174.     local t = "name"
  175.     for k, v in pairs(lines) do
  176.         if t == "name" then
  177.             curItem.name = v:gsub("^%s*(.-)%s*$", "%1")
  178.             t = "definition"
  179.         elseif t == "definition" then
  180.             curItem.definition = v
  181.             table.insert(database, curItem)
  182.             t = "name"
  183.         end
  184.     end
  185.     fs.delete(databaseLocation)
  186. end
  187.  
  188.  
  189. --  -------- Validation
  190.  
  191. local function runOnStartup()
  192.     -- Check for startup
  193.     local name = fs.getName(antivirusLocation)
  194.     if name ~= "startup" then
  195.         clearPage()
  196.         print(" ")
  197.         centerPrint("Uh Oh!")
  198.         print("\n\n")
  199.         centerPrint("The Rescue Disk Wasn't Run On Startup!")
  200.         print(" ")
  201.         centerPrint("Please Rename The Rescue Disk To 'startup'")
  202.         centerPrint("And Make Sure It's Run From A Disk")
  203.  
  204.         local opt = prompt({{"Shutdown", -1, 14}, {"Exit To Terminal", -1, 16}}, "vertical")
  205.         if opt == "Exit To Terminal" then
  206.             toTerminal = true
  207.         end
  208.         return false
  209.     end
  210.  
  211.     -- Check Run On Disk
  212.     local mPaths = {}
  213.     for _, v in pairs(rs.getSides()) do
  214.         if peripheral.getType(v) == "drive" then
  215.             table.insert(mPaths, disk.getMountPath(v))
  216.         end
  217.     end
  218.     local onDisk = false
  219.     for _, v in pairs(mPaths) do
  220.         if antivirusLocation == "/" .. v .. "/startup" then
  221.             onDisk = true
  222.             mountPath = "/" .. v
  223.         end
  224.     end
  225.  
  226.     if not(onDisk) then
  227.         clearPage()
  228.         print(" ")
  229.         centerPrint("Uh Oh!")
  230.         print("\n\n")
  231.         centerPrint("The Rescue Disk Wasn't Run From A Disk!")
  232.         print(" ")
  233.         centerPrint("Please Make Sure The Rescue Disk Is Named")
  234.         centerPrint("'startup', And Is Run From A Disk")
  235.  
  236.         local opt = prompt({{"Shutdown", -1, 14}, {"Exit To Terminal", -1, 16}}, "vertical")
  237.         if opt == "Exit To Terminal" then
  238.             toTerminal = true
  239.         end
  240.         return false
  241.     end
  242.  
  243.     return true
  244. end
  245.  
  246.  
  247. --  -------- Clock
  248.  
  249. local function runClock()
  250.     while clockRunning do
  251.         if clockVisible then
  252.             local prevx, prevy = term.getCursorPos()
  253.             local w, h = term.getSize()
  254.             local time = textutils.formatTime(os.time())
  255.  
  256.             term.setCursorPos(w - 10, 1)
  257.             write(string.rep(" ", 10))
  258.             term.setCursorPos(w - time:len() - 1, 1)
  259.             write(time)
  260.  
  261.             term.setCursorPos(prevx, prevy)
  262.         end
  263.         sleep(0.06)
  264.     end
  265. end
  266.  
  267.  
  268. --  -------- Scanning
  269.  
  270. local viruses = {}
  271.  
  272. local function log(...)
  273.     local a = {...}
  274.     for k, v in pairs(a) do
  275.         term.setCursorPos(1, 9 + k)
  276.         term.clearLine()
  277.         centerWrite(v)
  278.     end
  279.     sleep(0.05)
  280. end
  281.  
  282. local function scanRomPrograms()
  283.     local root = fs.list("/")
  284.     local sys = fs.list("/rom/programs")
  285.     for a = 1, #root do
  286.         for b = 1, #sys do
  287.             if root[a] == sys[b] then
  288.                 log("General.Malware Found In", "/" .. root[a])
  289.  
  290.                 local c = {}
  291.                 c.type = "General.Malware"
  292.                 c.location = "/" .. root[a]
  293.                 table.insert(viruses, c)
  294.             end
  295.         end
  296.     end
  297. end
  298.  
  299. local function scanRomAPIs()
  300.     local root = fs.list("/")
  301.     local sys = {"cp", "dir", "ls", "mv", "rm", "computer", "http", "secret", "turtle", "bit",
  302.     "colors", "colours", "gps", "help", "io", "parallel", "rednet", "term", "textutils", "vector",
  303.     "os", "math", "fs", "coroutine", "parallel", "peripheral", "rs", "redstone", "shell", "string",
  304.     "table", "term", "adventure", "hello", "worm", "pastebin", "alongtimeago", "dance", "excavate",
  305.     "go", "tunnel", "turn"}
  306.     for a = 1, #root do
  307.         for b = 1, #sys do
  308.             if root[a] == sys[b] then
  309.                 log("General.Malware Found In", "/" .. root[a])
  310.  
  311.                 local c = {}
  312.                 c.type = "General.Malware"
  313.                 c.location = "/" .. root[a]
  314.                 table.insert(viruses, c)
  315.             end
  316.         end
  317.     end
  318. end
  319.  
  320. local function scanDirectory(dir)
  321.     local curDir = dir
  322.     local cont = fs.list(curDir)
  323.     if cont[1] == nil then
  324.         return
  325.     end
  326.  
  327.     local skip = false
  328.     for i = 1, #cont do
  329.         sleep(0.000001)
  330.         if (cont[i] == "disk" and curDir == "/") or (cont[i] == "rom" and curDir == "/") then
  331.             skip = true
  332.         else
  333.             skip = false
  334.         end
  335.  
  336.         if not(fs.isDir(curDir .. cont[i])) then
  337.             local f = io.open(curDir .. cont[i], "r")
  338.             local data = f:read("*a")
  339.             f:close()
  340.  
  341.             for x = 1, #database do
  342.                 if string.find(data, database[x].definition) ~= nil then
  343.                     log(database[x].name .. " Found In", curDir .. cont[i])
  344.                     break
  345.                 end
  346.             end
  347.         elseif skip == false then
  348.             scanDirectory(curDir .. cont[i] .. "/")
  349.         end
  350.     end
  351. end
  352.  
  353.  
  354. --  -------- Main
  355.  
  356. local function performScan()
  357.     clearPage()
  358.     print("\n")
  359.     centerPrint("Complete Virus Scan")
  360.     print(" ")
  361.     centerPrint("This Will Search Through")
  362.     centerPrint("All Your Computer's Files,")
  363.     centerPrint("And Check If Any Are Viruses.")
  364.     print(" ")
  365.     centerPrint("Beware! Most Viruses Will Have")
  366.     centerPrint("Already Deleted Most Of Your")
  367.     centerPrint("Computer's Files.")
  368.     local w, h = term.getSize()
  369.     local opt = prompt({{"Continue", math.floor(w/4 - string.len("Continue")/2), 16},
  370.             {"Exit To Terminal", math.ceil(w/4 - string.len("Exit To Terminal")/2 + w/2), 16}},
  371.             "horizontal")
  372.     if opt == "Continue" then
  373.         clearPage()
  374.         print("\n\n\n\n")
  375.         centerPrint("Scanning For Viruses...")
  376.         sleep(0.25)
  377.         scanRomPrograms()
  378.         scanRomAPIs()
  379.         scanDirectory("/")
  380.  
  381.         clearPage()
  382.         print("\n")
  383.         centerPrint("Done!")
  384.         if #viruses == 0 then
  385.             print("\n\n\n")
  386.             centerPrint("No Viruses Were Found!")
  387.             prompt({{"Continue", -1, 14}}, "vertical")
  388.         else
  389.             local opt1 = prompt({{"Delete All Viruses", -1, 9}, {"Move Viruses To Disk", -1, 11}},
  390.                 "vertical")
  391.             if opt1 == "Delete All Viruses" then
  392.                 for _, v in pairs(viruses) do
  393.                     fs.delete(v.location)
  394.                 end
  395.             elseif opt1 == "Move Viruses To Disk" then
  396.                 if not(fs.exists(mountPath .. "/viruses")) then fs.makeDir(mountPath .. "/viruses") end
  397.                 for _, v in pairs(viruses) do
  398.                     fs.delete(mountPath .. "/viruses/" .. fs.getName(v.location))
  399.                     fs.move(v.location, mountPath .. "/viruses/" .. fs.getName(v.location))
  400.                 end
  401.             end
  402.         end
  403.  
  404.         clearPage()
  405.         print("\n")
  406.         centerPrint("Horrah!")
  407.         print(" ")
  408.         centerPrint("Your Computer Is Now Completely")
  409.         centerPrint("Free Of Viruses!")
  410.         print(" ")
  411.         centerPrint("Enjoy!")
  412.         prompt({{"Shutdown", -1, 15}}, "vertical")
  413.         return nil
  414.     elseif opt == "Exit To Terminal" then
  415.         toTerminal = true
  416.         return
  417.     end
  418. end
  419.  
  420. local function main()
  421.     clearPage()
  422.     print("\n")
  423.     centerPrint("Welcome To Rescue Disk " .. antivirusVersion)
  424.     print(" ")
  425.     centerPrint("Made by 1lann and GravityScore")
  426.     local opt = prompt({{"Continue To Terminal", -1, 12}, {"Perform Virus Scan", -1, 14}}, "vertical")
  427.  
  428.     if opt == "Continue To Terminal" then
  429.         toTerminal = true
  430.         return
  431.     elseif opt == "Perform Virus Scan" then
  432.         performScan()
  433.     end
  434. end
  435.  
  436.  
  437. --  -------- Startup
  438.  
  439. local function startup()
  440.     -- Splash screen
  441.     term.clear()
  442.     term.setCursorPos(1, 4)
  443.     centerPrint("       ____                               ")
  444.     centerPrint("      / __ \\ ___   _____ _____ __  __ ___ ")
  445.     centerPrint("     / /_/ // _ \\ / ___// ___// / / // _ \\")
  446.     centerPrint("    / _, _//  __//__  // /__ / /_/ //  __/")
  447.     centerPrint("   /_/ |_| \\___//____/ \\___/ \\__,_/ \\___/ ")
  448.     centerPrint("              ____            __          ")
  449.     centerPrint("             / __ \\ __ _____ / /__        ")
  450.     centerPrint("            / / / // // ___// //_/        ")
  451.     centerPrint("           / /_/ // //__  // ,<           ")
  452.     centerPrint("          /_____//_//____//_/|_|          ")
  453.  
  454.     -- Updating
  455.     term.setCursorPos(1, 18)
  456.     term.clearLine()
  457.     centerWrite("Updating Rescue Disk...")
  458.     updateClient()
  459.  
  460.     -- Run on startup
  461.     local cont = runOnStartup()
  462.     if not(cont) then
  463.         if toTerminal == false then
  464.             term.clear()
  465.             term.setCursorPos(1, 1)
  466.             os.shutdown()
  467.         else
  468.             term.clear()
  469.             term.setCursorPos(1, 1)
  470.             centerPrint("Thank You For Using Rescue Disk v" .. antivirusVersion)
  471.             centerPrint("Made By GravityScore and 1lann")
  472.             error()
  473.         end
  474.     end
  475.  
  476.     -- Database
  477.     term.clearLine()
  478.     centerWrite("Loading Database...")
  479.     getDatabase()
  480.  
  481.     -- Start
  482.     parallel.waitForAny(main, runClock)
  483.  
  484.     -- Exit
  485.     if toTerminal == true then
  486.         term.clear()
  487.         term.setCursorPos(1, 1)
  488.         centerPrint("Thank You For Using Rescue Disk v" .. antivirusVersion)
  489.         centerPrint("Made By GravityScore and 1lann")
  490.     else
  491.         term.clear()
  492.         term.setCursorPos(1, 1)
  493.         os.shutdown()
  494.     end
  495. end
  496.  
  497. -- Run
  498. startup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement