hecker7735

cc global antivirus

Oct 21st, 2023 (edited)
711
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.01 KB | None | 1 0
  1. -- Good AV+ Improved
  2. -- 2025 No Rights Reserved
  3. -- Version 2 (improved)
  4. -- Made By A and hecker
  5. local safe = shell.getRunningProgram()
  6. local detectedPatternsString = ""
  7. local quarantineDir = ".av_quarantine"
  8. local logFile = ".avlog"
  9. local detectionThreshold = 3
  10.  
  11. local function isPathRestricted (path)
  12.     return (string.find(path, "av_quarantine", 1, true) or string.find(path, safe, 1, true))
  13. end
  14.  
  15. local _shell_original_resolveProgram = shell.resolveProgram
  16. shell.resolveProgram = function(name)
  17.     local resolvedPath = _shell_original_resolveProgram(name)
  18.  
  19.     if resolvedPath and string.find(resolvedPath, "av_quarantine", 1, true) then
  20.         return nil
  21.     end
  22.  
  23.     return resolvedPath
  24. end
  25.  
  26. local _shell_original_run = shell.run
  27. shell.run = function ( name )
  28.     SCAN()
  29.     return _shell_original_run(name)
  30. end
  31.  
  32.  
  33. if not fs.exists(quarantineDir) then
  34.     local ok, err = fs.makeDir(quarantineDir)
  35.     if not ok then print("Warning: could not create quarantine dir:", err) end
  36. end
  37.  
  38.  
  39. local maliciousPatterns = {
  40.     "%(shell%.getRunningProgram%(%),%s*\"r\"%)", -- attempted self-identification pattern
  41.     "payload", -- suspicious marker (broad)
  42.     "os%.pullEvent%s*=%s*os%.pullEventRaw", -- event hijack / cannot be terminated
  43.     "in%s+ipairs%(%s*fs%.list%(%w*%)%s*%)", -- scanning directories
  44.     "[%a_][%w_]*:%s*byte%s*%(%s*%)%s*%+.-%%.-", -- weird byte ops (cheap heuristic)
  45.     "%x%x%x%x%x%x%x%x", -- hex sequences (possible obfuscation)
  46.     "fs%.open%(%s*[\"']%.startup", -- writing startup
  47.     "fs%.open%(%s*[\"']%.startup%.luax", -- writing .startup.luax
  48.     "shell%.setAlias%s*%(", -- alias changes
  49.     "_G%.[%a_][%w_]*%s*=", -- direct global table assignments
  50.     "^%s*(local%s*)?%w+%s*=%s*_G%.[%a_][%w_]*", -- wrapping globals into locals
  51.     "for%s+[%w_,%s]+in%s+pairs%(%w+%)%s+do%s+_G%.fs%[k%]%s*=%s*v%s+end", -- wrapper loop
  52.     "http%.get%s*%(", -- remote fetch
  53.     "load%s*%(", -- dynamic execution
  54.     "loadstring%s*%(", -- dynamic execution
  55.     "os%.reboot%s*%(", -- reboot calls
  56.     "while%s+true%s+do%s-.-os%.reboot" -- infinite reboot loop
  57. }
  58.  
  59. local maliciousPatternsExplain = {
  60.     "Self-Replicating", "Interfering", "Cannot Be Terminated",
  61.     "Scanning for files", "Encryption-ish / Byte ops", "Obfuscation / Hex",
  62.     "Startup modification", "Startup modification (.luax)", "Changing Aliases",
  63.     "Global Table Manipulation", "Global Table Wrapping",
  64.     "Global Table Wrapping (loop)", "Remote Fetch", "Remote/Dynamic Execution",
  65.     "Remote/Dynamic Execution", "Reboot calls", "Forced Reboot Loop"
  66. }
  67.  
  68. local function dedupe(list)
  69.     local seen = {}
  70.     local out = {}
  71.     for _, v in ipairs(list) do
  72.         if not seen[v] then
  73.             table.insert(out, v)
  74.             seen[v] = true
  75.         end
  76.     end
  77.     return out
  78. end
  79.  
  80. local function isMalicious(contents)
  81.     local matches = 0
  82.     local detected = {}
  83.     local seen = {}
  84.     if not contents or #contents == 0 then return false, {} end
  85.  
  86.     for i, pat in ipairs(maliciousPatterns) do
  87.  
  88.         local ok, found = pcall(function() return contents:find(pat) end)
  89.         if ok and found then
  90.             local expl = maliciousPatternsExplain[i] or ("Pattern #" .. i)
  91.             if not seen[expl] then
  92.                 matches = matches + 1
  93.                 table.insert(detected, expl)
  94.                 seen[expl] = true
  95.             end
  96.         end
  97.     end
  98.  
  99.     detected = dedupe(detected)
  100.     return matches >= detectionThreshold, detected
  101. end
  102.  
  103. local function typeofInfection(detectedPatterns)
  104.     local selfReplicating, scanningForFiles, cannotBeTerminated = false, false,
  105.                                                                   false
  106.     local interfering, Encryption, SuspiciousActivity, nolua = false, false,
  107.                                                                false, false
  108.  
  109.     for _, p in ipairs(detectedPatterns) do
  110.         if p == "Self-Replicating" then
  111.             selfReplicating = true
  112.         elseif p == "Scanning for files" then
  113.             scanningForFiles = true
  114.         elseif p == "Cannot Be Terminated" then
  115.             cannotBeTerminated = true
  116.         elseif p == "Interfering" then
  117.             interfering = true
  118.         elseif p == "Encryption-ish / Byte ops" then
  119.             Encryption = true
  120.         elseif p == "Changing Aliases" then
  121.             interfering = true;
  122.             nolua = true
  123.         elseif p == "Global Table Manipulation" then
  124.             nolua = true
  125.         end
  126.     end
  127.  
  128.     local endType = "Unknown"
  129.     if selfReplicating and (scanningForFiles or interfering) then
  130.         endType = "Quine"
  131.         if nolua then endType = "Quine (Evading)" end
  132.     elseif Encryption and cannotBeTerminated then
  133.         endType = "Ransomware"
  134.     elseif cannotBeTerminated and interfering then
  135.         endType = "Malware"
  136.     elseif scanningForFiles then
  137.         endType = "Spyware"
  138.     elseif Encryption then
  139.         endType = "Ransomware"
  140.     elseif nolua then
  141.         endType = "Malware (Global mods)"
  142.     end
  143.  
  144.     return endType
  145. end
  146.  
  147. local function logEntry(text)
  148.     local ok, f = pcall(fs.open, logFile, "a")
  149.     if not ok or not f then
  150.         print("Could not write to log file:", logFile)
  151.         print(text)
  152.         return
  153.     end
  154.     local ts = "UNKNOWN TIME"
  155.     if os and os.date then
  156.         pcall(function() ts = os.date("%Y-%m-%d %H:%M:%S") end)
  157.     end
  158.     f.write(("[%s] %s\n\n"):format(ts, text))
  159.     f.close()
  160. end
  161.  
  162. local function quarantineFile(fullPath)
  163.  
  164.     if not fs.exists(quarantineDir) then pcall(fs.makeDir, quarantineDir) end
  165.  
  166.     local base = fs.getName(fullPath)
  167.     local dest = fs.combine(quarantineDir, base)
  168.  
  169.     local counter = 1
  170.     while fs.exists(dest) do
  171.         dest = fs.combine(quarantineDir, base .. "_" .. tostring(counter))
  172.         counter = counter + 1
  173.     end
  174.  
  175.     local ok, err = pcall(fs.move, fullPath, dest)
  176.     if not ok or not fs.exists(dest) then
  177.         -- try copy then delete
  178.         local copyOk, copyErr = pcall(fs.copy, fullPath, dest)
  179.         if not copyOk then
  180.             return false,
  181.                    "quarantine move/copy failed: " .. tostring(copyErr or err)
  182.         end
  183.         pcall(fs.delete, fullPath)
  184.     end
  185.     return true, dest
  186. end
  187.  
  188. -- Recursively scan directory safely
  189. local function scanAndQuarantine(directory)
  190.     local listOk, entries = pcall(function() return fs.list(directory) end)
  191.     if not listOk or not entries then return end
  192.  
  193.     for _, name in ipairs(entries) do
  194.         local fullPath = fs.combine(directory, name)
  195.         -- skip rom and system mounts
  196.         if fullPath:sub(1, 4) ~= "rom/" and fullPath ~= safe then
  197.             local ok, isDir = pcall(function()
  198.                 return fs.isDir(fullPath)
  199.             end)
  200.             if ok and isDir then
  201.  
  202.                 if fullPath ~= quarantineDir then
  203.                     pcall(scanAndQuarantine, fullPath)
  204.                 end
  205.             else
  206.  
  207.                 local handleOk, handle = pcall(fs.open, fullPath, "r")
  208.                 if handleOk and handle then
  209.                     local contents = handle.readAll()
  210.                     handle.close()
  211.                     local flagged, patterns = isMalicious(contents)
  212.                     if flagged then
  213.                         -- move to quarantine
  214.                         local quarantined, destOrErr = quarantineFile(fullPath)
  215.                         local fileName = fs.getName(fullPath)
  216.                         local typ = typeofInfection(patterns)
  217.                         local text =
  218.                             ("Removed malicious file: %s\nFamily Type: %s\nDetected Patterns:\n%s\nQuarantined to: %s"):format(
  219.                                 fileName, typ, table.concat(patterns, "\n- "),
  220.                                 tostring(destOrErr))
  221.                         print(text)
  222.                         logEntry(text)
  223.                     end
  224.                 end
  225.             end
  226.         end
  227.     end
  228. end
  229.  
  230. print("AV: Starting scan. This may take a while...")
  231.  
  232.  
  233. function SCAN()
  234.  
  235.     pcall(function() scanAndQuarantine("") end)
  236.  
  237.     for i = 0, 9 do
  238.         local mount = (i == 0) and "disk" or ("disk" .. tostring(i))
  239.         if fs.exists(mount) and fs.isDir(mount) then
  240.             pcall(function() scanAndQuarantine(mount) end)
  241.         end
  242.     end
  243.  
  244.     local topListOk, topEntries = pcall(function() return fs.list("/") end)
  245.     if topListOk and topEntries then
  246.         for _, entry in ipairs(topEntries) do
  247.             local entryPath = "/" .. entry
  248.             -- skip rom and quarantine to avoid recursion
  249.             if entryPath:sub(1, 4) ~= "/rom" and entry ~= quarantineDir then
  250.                 pcall(function() scanAndQuarantine(entry) end)
  251.             end
  252.         end
  253.     end
  254. end
  255. SCAN()
  256.  
  257. print("AV: Scan complete. Check " .. logFile .. " and " .. quarantineDir ..
  258.           " for details.")
  259. print("Use `edit " .. logFile .. "` to view logs. Quarantined files are in " ..
  260.           quarantineDir .. ".")
  261.  
  262. -- anti virus protection:
  263. local fs_wrapper = {}
  264. for k, v in pairs(_G.fs) do
  265.     fs_wrapper[k] = v -- backup originals
  266. end
  267.  
  268. _G.fs.exists = function(path)
  269.     if isPathRestricted(path) then return false end
  270.     return fs_wrapper.exists(path) -- call original
  271. end
  272.  
  273. _G.fs.makeDir = function(path)
  274.     if isPathRestricted(path) then
  275.         return false, "Access denied: Cannot create restricted directory."
  276.     end
  277.     return fs_wrapper.makeDir(path)
  278. end
  279.  
  280. _G.fs.move = function(from, to)
  281.     if isPathRestricted(from) then
  282.         return false, "Access denied: Cannot move restricted files/directories."
  283.     end
  284.     return fs_wrapper.move(from, to) -- fixed args
  285. end
  286.  
  287. _G.fs.copy = function(from, to)
  288.     if isPathRestricted(from) or isPathRestricted(to) then
  289.         return false, "Access denied: Cannot copy restricted files/directories."
  290.     end
  291.     return fs_wrapper.copy(from, to) -- fixed args
  292. end
  293.  
  294. _G.fs.delete = function(path)
  295.     if isPathRestricted(path) then
  296.         return false, "Access denied: Cannot delete restricted files/directories."
  297.     end
  298.     return fs_wrapper.delete(path)
  299. end
Advertisement
Add Comment
Please, Sign In to add comment