asteroidsteam icon

Antivirus

asteroidsteam | PRO | 10/23/18 10:31:53 PM UTC | 0 ⭐ | 607 👁️ | Never ⏰ | []
Lua |

9.68 KB

|

None

|

0 👍

/

0 👎

--Define Variables
local argv = {...}
local argc = #argv
local vDefs = nil
local fDef = nil
local oldDefs = false
local machineInf = false
local viruscount = 0
local critical = false
--Define Functions
function ord(char)
  return string.byte(char)
end
function ordA1(char)
  return string.byte(char)-string.byte("a")+1
end
function chr(num)
  return string.char(num)
end
function chrA1(num)
  return string.char(string.byte("a")+num-1)
end
function checkSum(str)
  local checkSum = 0
  for i=1, #str do
    local c = string.sub(str,i,i)
    checkSum = checkSum + ord(c)
  end
  return checkSum
end
function update()
  local url = http.get("https://pastebin.com/raw/wjYsbRPC")
  if url ~= nil then
    fs.delete(shell.getRunningProgram())
    writeFile(shell.getRunningProgram(),url.readAll())
    return true
  else   
    return false
  end
end
function hash(str)
  local hash = ""
  for i=1, #str do
    local c = string.sub(str,i,i)
    hash = hash..chr(ord(c)+checkSum(c))
  end
  return hash
end
function removeStr(oldstr,remove)
        return string.gsub(oldstr,remove,"")
end
function getDefs()
  local virDefs = http.get("https://pastebin.com/raw/v3iDY3Qb")
  local defs = nil
  if virDefs == nil then
    defs = false
  else
    defs = textutils.unserialise(virDefs.readAll())
    virDefs.close()
  end
  return defs
end
function exists(fName)
  return fs.exists(fName)
end
function readFile(fName)
  if exists(fName) then
    local f = fs.open(fName,"r")
    local fRead = f.readAll()
    f.close()
    return fRead
  else
    return false
  end
end
function writeFile(fName,fData)
  local f = fs.open(fName,"w")
  if f == nil then
    return false
  else
    f.write(fData)
    f.close()
  end
end
function strFind(original,target)
  return string.find(original,target)
end
function setCritical()
  critical = true
end
function isCritical()
  return critical
end
function scanFile(fName,defs)
  local fData = readFile(fName)
  local isInf = false
  print("Scanning File: "..fName)
  if fData ~= false then
    for i=1,#defs do
      if strFind(fData,defs[i][1]) then
        if defs[i][2] == 1 then
          print("High Security risk file found: "..fName.." Attempting to remove file...")
          isInf = true
          writeFile(fName..".backup",removeStr(fData,defs[i][1]))
          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".."\")")
          setCritical()
          break
        end
        print("Virus Found in file "..fName.." Attempting to remove virus code...")
        writeFile(fName,removeStr(fData,defs[i][1]))
        isInf = true
      end
    end
    return 0,isInf
  else
    return 1
  end
end
function wipe()
  local files = fs.list("/")
  for i=1,#files do
    if files[i] ~= "rom" then
      fs.delete(files[i])
    end
  end
end
function scanDir(Directory,def)
    print("Scannng Directory: "..Directory)
    local sDir = shell.dir()
    sDir = shell.resolve( Directory )
    local tAll = fs.list( sDir )
    local tFiles = {}
    local tDirs = {}
    for n, sItem in pairs( tAll ) do
        if string.sub( sItem, 1, 1 ) ~= "." then
            local sPath = fs.combine( sDir, sItem )
            if fs.isDir( sPath ) then
                table.insert( tDirs, sItem )
            else
                table.insert( tFiles, sItem )
            end
        end
    end
    table.sort( tDirs )
    table.sort( tFiles )
   
    for i=1,table.getn(tFiles) do
        local code,x = scanFile(Directory.."/"..tFiles[i],def)
        if code == 1 then
            printError("Error scanning file: "..Directory.."/"..tFiles[i]) 
        else
          if x then
            machineInf = true
            viruscount = viruscount + 1
          end
        end
    end
    for i=1,table.getn(tDirs) do
        if tDirs[i] ~= "rom" then
            scanDir(Directory.."/"..tDirs[i],def)
        end
    end
   
end
function vFullScan()
scanDir("/",vDefs)
  if machineInf then
    print("Alert! "..viruscount.." Virus(es) Found!")
  else
    print("Machine is Virus Free!")
  end
  if isCritical() then
    local msg = [[
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Alert!
System Status Critical: One or more files infected with a destructive virus.
System wipe may be necessary.
Would you like to wipe the system?
[Y]es
[N]o
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Awaiting Input...
    ]]
    print(msg)
    while true do
      local event,key1 = os.pullEvent()
      if event == "key" then
        if key1 == keys.y then
          wipe()
          print("System Wiped, Have a good day.")
          break
        elseif key1 == keys.n then
          break
        end
      else
        print(msg)
      end
    end
  end
end
function fFullScan()
scanDir("/",fDef)
    if machineInf then
      print("Alert! "..viruscount.." Virus(es) Found!")
    else
      print("Machine is Virus Free!")
    end
    if isCritical() then
    local msg = [[
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Alert!
System Status Critical: One or more files infected with a destructive virus.
System wipe may be necessary.
Would you like to wipe the system?
[Y]es
[N]o
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Awaiting Input...
    ]]
    print(msg)
    while true do
      local event,key1 = os.pullEvent()
      if event == "key" then
        if key1 == keys.y then
          wipe()
          print("System Wiped, Have a good day.")
          break
        elseif key1 == keys.n then
          break
        end
      else
        print(msg)
      end
    end
  end
end
function setTermBg(color)
  term.setBackgroundColor(color)
  term.clear()
end
function cPos(x,y)
  term.setCursorPos(x,y)
end
function setLineBg(y,color)
  cPos(1,y)
  term.setBackgroundColor(color)
  term.clearLine()
end
function wColor(txt,color)
  term.setTextColor(color)
  write(txt)
end 
function setTxtC(color)
  term.setTextColor(color)
end
function setTxtBC(color)
  term.setBackgroundColor(color)
end
function clearScreen()
  term.clear()
  term.setCursorPos(1,1)
end
function gui(v)
  local tx,ty = term.getSize()
  local btn_scan = "Scan"
  local btn_update = "Update"
  local btn_u_x,btn_u_y = tx-btn_update:len(),ty
  local btn_s_x,btn_s_y = 1,ty
  local txtbgcolor = nil
  local msg = [[
J-Os Antivirus-0.1-Dev is a sophisticated antivirus that protects your system as well as removes active viruses.
  ]]
  if advanced then
    local bgcolor = colors.aqua
    txtbgcolor = colors.aqua
    setTermBg(bgcolor)
    cPos(1,1)
    setLineBg(1,colors.gray)
    cPos(1,1)
    wColor("[Close]   J-Os Antivirus-0.1-DEV",colors.red)
  else
    local bgcolor = colors.gray
    txtbgcolor = colors.gray
    setTermBg(bgcolor)
    cPos(1,1)
    setLineBg(1,colors.gray)
    cPos(1,1)
    wColor("[Close]   J-Os Antivirus-0.1-DEV",colors.white)
  end
  cPos(1,3)
  wColor(msg,colors.white)
  cPos(btn_s_x,btn_s_y)
  setTxtBC(txtbgcolor)
  wColor(btn_scan,colors.white)
  cPos(btn_u_x,btn_u_y)
  setTxtBC(txtbgcolor)
  wColor(btn_update,colors.white)
  local timer1 = nil
  while true do
    local event,but,cx,cy = os.pullEvent()
    if event == "mouse_click" then 
      if cx >= btn_s_x and cx <= btn_s_x + btn_scan:len() and cy == btn_s_y then
        if v then
          clearScreen()
          vFullScan()
          setLineBg(1,colors.gray)
          cPos(1,1)
          wColor("[Close]   J-Os Antivirus-0.1-DEV",colors.white)
          setLineBg(2,colors.gray)
          cPos(1,2)
          timer1 = os.startTimer(10)
        else
          clearScreen()
          fFullScan()
          setLineBg(1,colors.gray)
          cPos(1,1)
          wColor("[Close]   J-Os Antivirus-0.1-DEV",colors.white)
          setLineBg(2,colors.gray)
          cPos(1,2)
          timer1 = os.startTimer(10)
        end
      elseif cx >= btn_u_x and cx <= btn_u_x + btn_update:len() and cy == btn_u_y then
        if update() then
          cPos(1,3)
          setLineBg(3,colors.gray)
          cPos(1,3)
          wColor("System updated, Restarting in 5 seconds",colors.white)
          os.sleep(5)
          setTxtBC(colors.black)
          setTxtC(colors.white)
          clearScreen()
          break
        else
          cPos(1,3)
          setLineBg(3,colors.gray)
          cPos(1,3)
          wColor("Error updating! (Is there wifi?)",colors.white)
        end
      elseif cx >= 1 and cx <= 7 and cy == 1 then
        setTxtBC(colors.black)
        setTxtC(colors.white)
        clearScreen()
        break
      end
    elseif event == "timer" and but == timer1 then
      gui(v)
      break
    end
  end
end
--Set Variables
vDefs = getDefs()
--Get the definitons
if vDefs == false then
  printError("Error grabbing definitions! Reverting to old defs.")
  oldDefs = true
  if oldDefs then
    print("Grabbing Old Virus Definitions...")
    fDef = readFile(".fdef")
    fDef_hash = readFile(".fdef_hash")
    if fDef == false then
      printError("Error opening Virus Definitions file! (Is it up to date?)")
    else
      if fDef_hash == false then
        printError("Fatal Error! Cannot read hash! Aborting!")
        fDef = false
      else
        if fDef_hash == hash(fDef) then
          fDef = textutils.unserialise(fDef)
        else
          printError("Fatal Error! Hash is invalid! Aborting!")
          fDef = false
        end
      end
    end
  end
  if fDef ~= false then
    --Run antivirus with fDef as definitions
    gui(false)
  end
else
  writeFile(".fdef",textutils.serialise(vDefs))
  writeFile(".fdef_hash",hash(textutils.serialise(vDefs)))
  --Run antivirus with vDef as definitions
  gui(true)
end

Comments