Andy73

scan

Jul 19th, 2015
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.64 KB | None | 0 0
  1. local args={...}
  2. local fbuffer={} -- File buffer for memoization
  3. local chunks={} -- Chunk count
  4.  
  5. local w,h=term.getSize()
  6. local CSIZE=500 -- Base chunk size, in bytes
  7. local setUp=false
  8. local trailingSpace=string.rep(" ",w)
  9.  
  10. local getSize=fs.getSize
  11. local readFile,loadTable,scanFile,markFile,dirtree,fastSleep,printThreat,makeChunk,nextChunk,printGUI,setUpGUI
  12. local known,scanPath
  13.  
  14. function setUpGUI()
  15.     term.setBackgroundColour(colours.white)
  16.     term.setTextColour(colours.grey)
  17.  
  18.     if setUp then return end
  19.  
  20.     term.clear()
  21.  
  22.     term.setTextColour(colours.black)
  23.     term.setCursorPos(3,2)
  24.     term.write("Files scanned: ")
  25.     term.setCursorPos(3,3)
  26.     term.write("Threats found: ")
  27.  
  28.     term.setCursorPos(2,h-1)
  29.     term.setBackgroundColour(colours.grey)
  30.     term.write(string.rep(" ",w-2))
  31.  
  32.     term.setBackgroundColour(colours.white)
  33.     term.setTextColour(colours.grey)
  34.  
  35.     setUp=true
  36. end
  37.  
  38. --TODO: Rewrite this! Looks horrible :(
  39. function printGUI(path, percentage, scanned, found)
  40.     setUpGUI()
  41.  
  42.     term.setCursorPos(2,5)
  43.     term.write(path..trailingSpace)
  44.  
  45.     term.setCursorPos(w/4,h*2/3)
  46.     term.write(tostring(percentage).."%"..trailingSpace)
  47.  
  48.     term.setTextColour(colours.black)
  49.     term.setCursorPos(18,2)
  50.     term.write(scanned)
  51.     term.setCursorPos(18,3)
  52.     term.setTextColour(found==0 and colours.black or colours.red)
  53.     term.write(found)
  54.  
  55.     -- Print progress bar
  56.     term.setCursorPos(2,h-1)
  57.     term.setBackgroundColour(colours.green)
  58.     term.write(string.rep(" ",(w-2)*percentage/100))
  59. end
  60.  
  61. function printThreat(...)
  62.     local args={...}
  63.  
  64.     if #args<1 then return print() end
  65.  
  66.     local _,_,family=tostring(args[1]):gsub("db%.",""):find("(%a+)%.")
  67.     if family=="Suspicious" then
  68.         term.setTextColour(colours.orange)
  69.     else
  70.         term.setTextColour(colours.red)
  71.     end
  72.  
  73.     return print(...)
  74. end
  75.  
  76. function fastSleep()
  77.     os.queueEvent("fastSleep")
  78.     return coroutine.yield("fastSleep")
  79. end
  80.  
  81. function dirtree(dir, includeDirectories)
  82.     assert(dir and dir ~= "", "directory parameter is missing or empty")
  83.     if string.sub(dir, -1) == "/" then
  84.         dir=string.sub(dir, 1, -2)
  85.     end
  86.  
  87.     local function yieldtree(dir)
  88.         for i,entry in ipairs(fs.list(dir)) do
  89.             if entry ~= "." and entry ~= ".." then
  90.                 entry=dir.."/"..entry
  91.  
  92.                 if includeDirectories or not fs.isDir(entry) then
  93.                     coroutine.yield(entry)
  94.                 end
  95.  
  96.                 if fs.isDir(entry) then
  97.                     yieldtree(entry)
  98.                 end
  99.             end
  100.         end
  101.     end
  102.  
  103.     return coroutine.wrap(function() yieldtree(dir) end)
  104. end
  105.  
  106. function readFile(path, absolute, force)
  107.     path=(absolute and "" or shell.resolve(".").."/")..path
  108.  
  109.     if fbuffer[path] and not force then
  110.         return fbuffer[path]
  111.     end
  112.  
  113.     local f,err=fs.open(path,"r")
  114.     if not f then error("Could not open file '"..tostring(path).."': "..tostring(err),0) end
  115.     local content=f.readAll()
  116.    
  117.     f.close()
  118.  
  119.     fbuffer[path]=content
  120.  
  121.     return content
  122. end
  123.  
  124. --WARN: Consecutive chunk has the last character of the previous chunk, therefore its max size is CSIZE+1
  125.  
  126. function makeChunk(str, offset)
  127.     if offset>#str then
  128.         return
  129.     end
  130.  
  131.     local chunkSize=math.min(#str-offset, CSIZE)
  132.  
  133.     return str:sub(offset, offset+chunkSize)
  134. end
  135.  
  136. function nextChunk(path)
  137.     if not chunks[path] then
  138.         chunks[path]=-1
  139.     end
  140.     chunks[path]=chunks[path]+1
  141.  
  142.     local minSize=math.min(CSIZE,getSize(path))
  143.  
  144.     if chunks[path]*minSize>getSize(path)*2+CSIZE/2+1 then
  145.         return nil
  146.     end
  147.  
  148.     local offset=(chunks[path]*minSize)>getSize(path) and minSize/2 or 0
  149.     return makeChunk(readFile(path), CSIZE*chunks[path]+offset)
  150. end
  151.  
  152. function loadTable(path, absolute)
  153.     return textutils.unserialize(readFile(path,absolute))
  154. end
  155.  
  156. function markFile(path, threats)
  157.     --TODO: calculate a checksum and save to threat database
  158. end
  159.  
  160. function scanFile(path, db)
  161.     local iter,scan
  162.     local position="db"
  163.     local res={}
  164.  
  165.     function iter(tbl)
  166.         for k,v in pairs(tbl) do
  167.             local myPos=position
  168.  
  169.             position=position.."."..k
  170.  
  171.             if type(v)=="table" and not v[0] then
  172.                 iter(v)
  173.             else
  174.                 scan(v)
  175.             end
  176.             position=myPos
  177.         end
  178.     end
  179.  
  180.     function scan(tbl)
  181.         for i,pattern in ipairs(tbl) do
  182.             --TODO: rewrite nextChunk to iterator
  183.             local chunk=nextChunk(path)
  184.  
  185.             while chunk do
  186.                 if chunk:find(pattern) then
  187.                     table.insert(res,position.."."..i)
  188.                 end
  189.  
  190.                 fastSleep()
  191.                 chunk=nextChunk(path)              
  192.             end
  193.  
  194.             chunks[path]=nil
  195.         end
  196.     end
  197.  
  198.     iter(db)
  199.  
  200.     return res
  201. end
  202.  
  203.  
  204. -- Load known threats
  205. known,err=loadTable("threatDatabase.ltbl")
  206. if not known then error("Failed to load threat database: "..tostring(err))end
  207.  
  208.  
  209. local results={}
  210. local totalFound=0
  211. local totalScanned=0
  212. local totalBytes=0
  213. local currentBytes=0
  214.  
  215. scanPath="/"
  216.  
  217. if args[1] and fs.exists(args[1]) and fs.isDir(args[1]) then
  218.     scanPath=args[1]
  219. end
  220.  
  221. for path in dirtree(scanPath) do
  222.     totalBytes=totalBytes+getSize(path)
  223. end
  224.  
  225. -- Test
  226. for path in dirtree(scanPath) do
  227.     currentBytes=currentBytes+getSize(path)
  228.     local percentage=currentBytes/totalBytes*100
  229.  
  230.     printGUI(path,percentage,totalScanned,totalFound)
  231.    
  232.     results[path]=scanFile(path,known)
  233.     totalFound=totalFound+#results[path]
  234.    
  235.     totalScanned=totalScanned+1
  236. end
  237.  
  238.  
  239. term.setBackgroundColour(colours.white)
  240. term.setTextColour(colours.black)
  241. term.clear()
  242. term.setCursorPos(1,1)
  243.  
  244. print("Scan results:")
  245. print("  Files scanned: "..totalScanned)
  246. term.write("  Threats found: ")
  247. term.setTextColour(totalFound==0 and colours.black or colours.red)
  248. print(totalFound)
  249. term.setTextColour(colours.lightGrey)
  250. print("  (Click to scroll)")
  251. print()
  252.  
  253. for file,threats in pairs(results) do
  254.     if #threats>0 then
  255.         term.setTextColour(colours.black)
  256.         print("Infected file: "..file)
  257.         for i,threat in ipairs(threats) do
  258.             printThreat("  "..threat)
  259.         end
  260.         os.pullEvent("mouse_click")
  261.     end
  262. end
Advertisement
Add Comment
Please, Sign In to add comment