This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KronixGames's profile picture

Help with scanning files(for anti virus)

Started by KronixGames, 09 January 2013 - 06:45 PM
KronixGames #1
Posted 09 January 2013 - 07:45 PM
hello friends, I'm working on OS and was thinking of putting an antivirus in your script, for example, the OS scan the files in the root folder and CC rom, to see if there is any virus, it would be less useless in a server with CC. the only problem is that I can not make it scan the folders, for example:
Code:


print("Scanning…")
local file = textutils.serialize(fs.list("/"))
file1 = fs.getName([file(#1)])
is fs.isDir(file1) == true then
else
local h = fs.open(file1, "r")
scr = h.readAll()
if scr == "os.shutdown()" or scr == "os.reboot()" then
print("The file " .. file1 .. " is infected")
else
print("Nothing infected")
end

sorry for my bad english(using Google Translator)
theoriginalbit #2
Posted 09 January 2013 - 07:56 PM
firstly this forum has the ability to use code tags[code] [/code] its always good to have them, nicer to read :)/> for long code pastebin is good too! :)/>

now there are lots of issues to tackle to get it working.

Firstly here are changes to your code just to get it to work (untested, but it should work)

print( "Scanning..." )
local files = fs.list("/")
local file1 = shell.resolve( file[1] )
if fs.isDir( file1 ) then

else
  local h = fs.open( file1, "r" )
  local scr = h.readAll()
  h.close()
  local sStart = scr:find( "os.shutdown()" , 1 )
  local rStart = scr:find( "os.reboot()" , 1 )
  if sStart ~= nil or rStart ~= nil then
	print( "The file "..file1.." is infected")
  else
	print( "Nothing infected" )
  end
end

However this only scans one file. And would only scan one layer of folders. There are a few ways I can think of to do this, however I feel the easiest may be to use recursion.
anonimo182 #3
Posted 10 January 2013 - 04:27 AM
You could add the folders in a table, and then scan for each folder in the table… and so on
theoriginalbit #4
Posted 10 January 2013 - 04:34 AM
You could add the folders in a table, and then scan for each folder in the table… and so on
And to do this recursion is the easiest way. believe me its so much easier, I've done it a few times now. :)/>