Advertisement
PaymentOption

Light Shot Error Checker

Jan 23rd, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.08 KB | None | 0 0
  1. -- Creates a copy of the shell API in a separate table and returns said table.
  2. function getShellCopy()
  3.  
  4.     local shellCopy = {}
  5.    
  6.     for itemIndex, item in pairs(shell) do
  7.         shellCopy[itemIndex] = item
  8.     end
  9.    
  10.     return shellCopy
  11.  
  12. end
  13.  
  14. -- Creates a copy of the fs API and returns it.
  15. function getFsCopy()
  16.  
  17.     local fsCopy = {}
  18.    
  19.     for itemIndex, item in pairs( _G["fs"] ) do
  20.         fsCopy[itemIndex] = item
  21.     end
  22.    
  23.     return fsCopy
  24.  
  25. end
  26.  
  27. -- Creates a copy of the io API and returns it.
  28. function getIoCopy()
  29.  
  30.     local ioCopy = {}
  31.    
  32.     for itemIndex, item in pairs( _G["io"] ) do
  33.         ioCopy[itemIndex] = item
  34.     end
  35.    
  36.     return ioCopy
  37.  
  38. end
  39.  
  40. -- Creates a copy of the os API and returns it.
  41. function getOsCopy()
  42.  
  43.     local osCopy = {}
  44.    
  45.     for itemIndex, item in pairs( _G["os"] ) do
  46.         osCopy[itemIndex] = item
  47.     end
  48.    
  49.     return osCopy
  50.  
  51. end
  52.  
  53. -- Checks if a file that is a lightshot recording that is
  54. -- of a version before 1.2 is malicious or simply illegeal.
  55. -- Returns true if the given line contains malicious code,
  56. -- and false if not.
  57. function checkFileForVirus(filePath)
  58.  
  59.     -- Get a file handle and the contents of the file.
  60.     local fileHandle   = fs.open(filePath, 'r')
  61.     local fileContents = fileHandle.readAll()
  62.     fileHandle.close()
  63.    
  64.     -- Create a copy of the shell, fs, and io API's, plus the os.run function.
  65.     local old_shell = getShellCopy()
  66.     local old_fsAPI = getFsCopy()
  67.     local old_ioAPI = getIoCopy()
  68.     local old_osAPI = getOsCopy()
  69.     local old_sleep = _G["sleep"]
  70.     local old_print = _G["print"]
  71.     local old_write = _G["write"]
  72.    
  73.    
  74.     -- Replace shell API.
  75.     local dummy_shell = {}
  76.     for shellItemIndex, shellItem in pairs(old_shell) do
  77.         dummy_shell[shellItemIndex] = function( ... )
  78.             error("Unauthorized shell call: " .. shellItemIndex .. '!')
  79.         end
  80.     end
  81.     shell = dummy_shell
  82.    
  83.     -- Replace fs API.
  84.     for fsItemIndex, fsItem in pairs(old_fsAPI) do
  85.         _G["fs"][fsItemIndex] = function( ... )
  86.             error("Unauthorized fs call: " .. fsItemIndex .. '!')
  87.         end
  88.     end
  89.    
  90.     -- Replace io API.
  91.     for ioItemIndex, ioItem in pairs(old_ioAPI) do
  92.         _G["io"][ioItemIndex] = function( ... )
  93.             error("Unauthorized io call: " .. ioItemIndex .. '!')
  94.         end
  95.     end
  96.    
  97.     -- Replace os API.
  98.     for osItemIndex, osItem in pairs(old_osAPI) do
  99.         _G["os"][osItemIndex] = function( ... )
  100.             error("Unauthorized os call: " .. osItemIndex .. '!')
  101.         end
  102.     end
  103.     -- Replace the sleep function so that we can run through this recording much, much faster.
  104.     _G["sleep"] = function( ... )
  105.     end
  106.     -- Replace the print and write functions so that we don't get the end of recording message from the recording.
  107.     _G["write"] = function( ... )
  108.     end
  109.     _G["print"] = function( ... )
  110.     end
  111.    
  112.    
  113.    
  114.     -- Load the line into a function as a string, then run the function with
  115.     -- a limited environment. However, the terminal is available.
  116.     local recordingAsFunction, errorMessage = loadstring(fileContents)
  117.    
  118.     -- Check for errors in the line before continuing.
  119.     if errorMessage then
  120.         print("Error: " .. errorMessage .. " in file " .. filePath .. '.')
  121.         return true
  122.     end
  123.    
  124.     -- Now that the APIs are replaced, run the file with a protected call and capture the error.
  125.     local _, virusError = pcall(recordingAsFunction)
  126.    
  127.    
  128.     -- Restore all of the APIs back to their original state.
  129.     shell        = old_shell
  130.     _G["fs"]     = old_fsAPI
  131.     _G["io"]     = old_ioAPI
  132.     _G["os"]     = old_osAPI
  133.     _G["sleep"]  = old_sleep
  134.     _G["write"]  = old_write
  135.     _G["print"]  = old_print
  136.    
  137.    
  138.     -- If we captured an error, then return true and the error because this line is probably a virus.
  139.     if virusError then
  140.         return true, virusError
  141.     end
  142.    
  143.     -- If there was no error, then return false because this is probably not a virus.
  144.     return false
  145.  
  146. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement