Advertisement
asteroidsteam

Antivirus

Oct 23rd, 2018
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.68 KB | None | 0 0
  1. --Define Variables
  2. local argv = {...}
  3. local argc = #argv
  4. local vDefs = nil
  5. local fDef = nil
  6. local oldDefs = false
  7. local machineInf = false
  8. local viruscount = 0
  9. local critical = false
  10. --Define Functions
  11. function ord(char)
  12.   return string.byte(char)
  13. end
  14. function ordA1(char)
  15.   return string.byte(char)-string.byte("a")+1
  16. end
  17. function chr(num)
  18.   return string.char(num)
  19. end
  20. function chrA1(num)
  21.   return string.char(string.byte("a")+num-1)
  22. end
  23. function checkSum(str)
  24.   local checkSum = 0
  25.   for i=1, #str do
  26.     local c = string.sub(str,i,i)
  27.     checkSum = checkSum + ord(c)
  28.   end
  29.   return checkSum
  30. end
  31. function update()
  32.   local url = http.get("https://pastebin.com/raw/wjYsbRPC")
  33.   if url ~= nil then
  34.     fs.delete(shell.getRunningProgram())
  35.     writeFile(shell.getRunningProgram(),url.readAll())
  36.     return true
  37.   else  
  38.     return false
  39.   end
  40. end
  41. function hash(str)
  42.   local hash = ""
  43.   for i=1, #str do
  44.     local c = string.sub(str,i,i)
  45.     hash = hash..chr(ord(c)+checkSum(c))
  46.   end
  47.   return hash
  48. end
  49. function removeStr(oldstr,remove)
  50.         return string.gsub(oldstr,remove,"")
  51. end
  52. function getDefs()
  53.   local virDefs = http.get("https://pastebin.com/raw/v3iDY3Qb")
  54.   local defs = nil
  55.   if virDefs == nil then
  56.     defs = false
  57.   else
  58.     defs = textutils.unserialise(virDefs.readAll())
  59.     virDefs.close()
  60.   end
  61.   return defs
  62. end
  63. function exists(fName)
  64.   return fs.exists(fName)
  65. end
  66. function readFile(fName)
  67.   if exists(fName) then
  68.     local f = fs.open(fName,"r")
  69.     local fRead = f.readAll()
  70.     f.close()
  71.     return fRead
  72.   else
  73.     return false
  74.   end
  75. end
  76. function writeFile(fName,fData)
  77.   local f = fs.open(fName,"w")
  78.   if f == nil then
  79.     return false
  80.   else
  81.     f.write(fData)
  82.     f.close()
  83.   end
  84. end
  85. function strFind(original,target)
  86.   return string.find(original,target)
  87. end
  88. function setCritical()
  89.   critical = true
  90. end
  91. function isCritical()
  92.   return critical
  93. end
  94. function scanFile(fName,defs)
  95.   local fData = readFile(fName)
  96.   local isInf = false
  97.   print("Scanning File: "..fName)
  98.   if fData ~= false then
  99.     for i=1,#defs do
  100.       if strFind(fData,defs[i][1]) then
  101.         if defs[i][2] == 1 then
  102.           print("High Security risk file found: "..fName.." Attempting to remove file...")
  103.           isInf = true
  104.           writeFile(fName..".backup",removeStr(fData,defs[i][1]))
  105.           writeFile(fName,"printError(\"This file has been overwritten due to compromisation of this file. If you need to restore this file, there is a backup saved to "..fName..".backup".."\")")
  106.           setCritical()
  107.           break
  108.         end
  109.         print("Virus Found in file "..fName.." Attempting to remove virus code...")
  110.         writeFile(fName,removeStr(fData,defs[i][1]))
  111.         isInf = true
  112.       end
  113.     end
  114.     return 0,isInf
  115.   else
  116.     return 1
  117.   end
  118. end
  119. function wipe()
  120.   local files = fs.list("/")
  121.   for i=1,#files do
  122.     if files[i] ~= "rom" then
  123.       fs.delete(files[i])
  124.     end
  125.   end
  126. end
  127. function scanDir(Directory,def)
  128.     print("Scannng Directory: "..Directory)
  129.     local sDir = shell.dir()
  130.     sDir = shell.resolve( Directory )
  131.     local tAll = fs.list( sDir )
  132.     local tFiles = {}
  133.     local tDirs = {}
  134.     for n, sItem in pairs( tAll ) do
  135.         if string.sub( sItem, 1, 1 ) ~= "." then
  136.             local sPath = fs.combine( sDir, sItem )
  137.             if fs.isDir( sPath ) then
  138.                 table.insert( tDirs, sItem )
  139.             else
  140.                 table.insert( tFiles, sItem )
  141.             end
  142.         end
  143.     end
  144.     table.sort( tDirs )
  145.     table.sort( tFiles )
  146.    
  147.     for i=1,table.getn(tFiles) do
  148.         local code,x = scanFile(Directory.."/"..tFiles[i],def)
  149.         if code == 1 then
  150.             printError("Error scanning file: "..Directory.."/"..tFiles[i])
  151.         else
  152.           if x then
  153.             machineInf = true
  154.             viruscount = viruscount + 1
  155.           end
  156.         end
  157.     end
  158.     for i=1,table.getn(tDirs) do
  159.         if tDirs[i] ~= "rom" then
  160.             scanDir(Directory.."/"..tDirs[i],def)
  161.         end
  162.     end
  163.    
  164. end
  165. function vFullScan()
  166. scanDir("/",vDefs)
  167.   if machineInf then
  168.     print("Alert! "..viruscount.." Virus(es) Found!")
  169.   else
  170.     print("Machine is Virus Free!")
  171.   end
  172.   if isCritical() then
  173.     local msg = [[
  174.  
  175. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  176. Alert!
  177. System Status Critical: One or more files infected with a destructive virus.
  178. System wipe may be necessary.
  179. Would you like to wipe the system?
  180. [Y]es
  181. [N]o
  182. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  183. Awaiting Input...
  184.     ]]
  185.     print(msg)
  186.     while true do
  187.       local event,key1 = os.pullEvent()
  188.       if event == "key" then
  189.         if key1 == keys.y then
  190.           wipe()
  191.           print("System Wiped, Have a good day.")
  192.           break
  193.         elseif key1 == keys.n then
  194.           break
  195.         end
  196.       else
  197.         print(msg)
  198.       end
  199.     end
  200.   end
  201. end
  202. function fFullScan()
  203. scanDir("/",fDef)
  204.     if machineInf then
  205.       print("Alert! "..viruscount.." Virus(es) Found!")
  206.     else
  207.       print("Machine is Virus Free!")
  208.     end
  209.     if isCritical() then
  210.     local msg = [[
  211.  
  212. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  213. Alert!
  214. System Status Critical: One or more files infected with a destructive virus.
  215. System wipe may be necessary.
  216. Would you like to wipe the system?
  217. [Y]es
  218. [N]o
  219. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  220. Awaiting Input...
  221.     ]]
  222.     print(msg)
  223.     while true do
  224.       local event,key1 = os.pullEvent()
  225.       if event == "key" then
  226.         if key1 == keys.y then
  227.           wipe()
  228.           print("System Wiped, Have a good day.")
  229.           break
  230.         elseif key1 == keys.n then
  231.           break
  232.         end
  233.       else
  234.         print(msg)
  235.       end
  236.     end
  237.   end
  238. end
  239. function setTermBg(color)
  240.   term.setBackgroundColor(color)
  241.   term.clear()
  242. end
  243. function cPos(x,y)
  244.   term.setCursorPos(x,y)
  245. end
  246. function setLineBg(y,color)
  247.   cPos(1,y)
  248.   term.setBackgroundColor(color)
  249.   term.clearLine()
  250. end
  251. function wColor(txt,color)
  252.   term.setTextColor(color)
  253.   write(txt)
  254. end
  255. function setTxtC(color)
  256.   term.setTextColor(color)
  257. end
  258. function setTxtBC(color)
  259.   term.setBackgroundColor(color)
  260. end
  261. function clearScreen()
  262.   term.clear()
  263.   term.setCursorPos(1,1)
  264. end
  265. function gui(v)
  266.   local tx,ty = term.getSize()
  267.   local btn_scan = "Scan"
  268.   local btn_update = "Update"
  269.   local btn_u_x,btn_u_y = tx-btn_update:len(),ty
  270.   local btn_s_x,btn_s_y = 1,ty
  271.   local txtbgcolor = nil
  272.   local msg = [[
  273. J-Os Antivirus-0.1-Dev is a sophisticated antivirus that protects your system as well as removes active viruses.
  274.   ]]
  275.   if advanced then
  276.     local bgcolor = colors.aqua
  277.     txtbgcolor = colors.aqua
  278.     setTermBg(bgcolor)
  279.     cPos(1,1)
  280.     setLineBg(1,colors.gray)
  281.     cPos(1,1)
  282.     wColor("[Close]   J-Os Antivirus-0.1-DEV",colors.red)
  283.   else
  284.     local bgcolor = colors.gray
  285.     txtbgcolor = colors.gray
  286.     setTermBg(bgcolor)
  287.     cPos(1,1)
  288.     setLineBg(1,colors.gray)
  289.     cPos(1,1)
  290.     wColor("[Close]   J-Os Antivirus-0.1-DEV",colors.white)
  291.   end
  292.   cPos(1,3)
  293.   wColor(msg,colors.white)
  294.   cPos(btn_s_x,btn_s_y)
  295.   setTxtBC(txtbgcolor)
  296.   wColor(btn_scan,colors.white)
  297.   cPos(btn_u_x,btn_u_y)
  298.   setTxtBC(txtbgcolor)
  299.   wColor(btn_update,colors.white)
  300.   local timer1 = nil
  301.   while true do
  302.     local event,but,cx,cy = os.pullEvent()
  303.     if event == "mouse_click" then
  304.       if cx >= btn_s_x and cx <= btn_s_x + btn_scan:len() and cy == btn_s_y then
  305.         if v then
  306.           clearScreen()
  307.           vFullScan()
  308.           setLineBg(1,colors.gray)
  309.           cPos(1,1)
  310.           wColor("[Close]   J-Os Antivirus-0.1-DEV",colors.white)
  311.           setLineBg(2,colors.gray)
  312.           cPos(1,2)
  313.           timer1 = os.startTimer(10)
  314.         else
  315.           clearScreen()
  316.           fFullScan()
  317.           setLineBg(1,colors.gray)
  318.           cPos(1,1)
  319.           wColor("[Close]   J-Os Antivirus-0.1-DEV",colors.white)
  320.           setLineBg(2,colors.gray)
  321.           cPos(1,2)
  322.           timer1 = os.startTimer(10)
  323.         end
  324.       elseif cx >= btn_u_x and cx <= btn_u_x + btn_update:len() and cy == btn_u_y then
  325.         if update() then
  326.           cPos(1,3)
  327.           setLineBg(3,colors.gray)
  328.           cPos(1,3)
  329.           wColor("System updated, Restarting in 5 seconds",colors.white)
  330.           os.sleep(5)
  331.           setTxtBC(colors.black)
  332.           setTxtC(colors.white)
  333.           clearScreen()
  334.           break
  335.         else
  336.           cPos(1,3)
  337.           setLineBg(3,colors.gray)
  338.           cPos(1,3)
  339.           wColor("Error updating! (Is there wifi?)",colors.white)
  340.         end
  341.       elseif cx >= 1 and cx <= 7 and cy == 1 then
  342.         setTxtBC(colors.black)
  343.         setTxtC(colors.white)
  344.         clearScreen()
  345.         break
  346.       end
  347.     elseif event == "timer" and but == timer1 then
  348.       gui(v)
  349.       break
  350.     end
  351.   end
  352. end
  353. --Set Variables
  354. vDefs = getDefs()
  355. --Get the definitons
  356. if vDefs == false then
  357.   printError("Error grabbing definitions! Reverting to old defs.")
  358.   oldDefs = true
  359.   if oldDefs then
  360.     print("Grabbing Old Virus Definitions...")
  361.     fDef = readFile(".fdef")
  362.     fDef_hash = readFile(".fdef_hash")
  363.     if fDef == false then
  364.       printError("Error opening Virus Definitions file! (Is it up to date?)")
  365.     else
  366.       if fDef_hash == false then
  367.         printError("Fatal Error! Cannot read hash! Aborting!")
  368.         fDef = false
  369.       else
  370.         if fDef_hash == hash(fDef) then
  371.           fDef = textutils.unserialise(fDef)
  372.         else
  373.           printError("Fatal Error! Hash is invalid! Aborting!")
  374.           fDef = false
  375.         end
  376.       end
  377.     end
  378.   end
  379.   if fDef ~= false then
  380.     --Run antivirus with fDef as definitions
  381.     gui(false)
  382.   end
  383. else
  384.   writeFile(".fdef",textutils.serialise(vDefs))
  385.   writeFile(".fdef_hash",hash(textutils.serialise(vDefs)))
  386.   --Run antivirus with vDef as definitions
  387.   gui(true)
  388. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement