View difference between Paste ID: QSmSr3zP and E90VdmHH
SHOW: | | - or go back to the newest paste.
1
local function drawScreen()
2
  term.setBackgroundColor(colors.blue)
3
  term.setTextColor(colors.white)
4
  term.clear()
5
  term.setCursorPos(1, 1)
6
  print("LuaGuard by SxwCorp CyberSecurity")
7
  term.setBackgroundColor(colors.lime)
8
  term.clearLine()
9
  term.setCursorPos(1, 3)
10
  term.setBackgroundColor(colors.blue)
11
end
12
13
local definitions = http.get("http://files.fluidnode.com/public/computercraft/LuaGuard/definitions.lua")
14
15
local oldLoadstring = loadstring
16
17
local function scanString(str)
18
  local hasFound = nil
19
  for k, v in pairs(definitions) do
20
    local found = str:find(v.fragment, 1, true)
21
    if found then
22
      hasFound = v.name
23
    end
24
  end
25
26
  if hasFound then
27
    drawScreen()
28
    print("Close call!")
29
    print("Your computer was almost infected with:")
30
    print(hasFound)
31
    print("LuaGuard has nullified the threat.")
32
    print("You're safe.")
33
    local random = tostring(math.random())
34
    os.queueEvent("luaguard_" .. random)
35
    os.pullEvent("luaguard_" .. random)
36
    print("")
37
    print("[Press any key to continue]")
38
    os.pullEvent("key")
39
  else
40
    return true
41
  end
42
end
43
44
local function fakeLoadstring(theCode, stringName)
45
  local run = scanString(theCode)
46
  if run then
47
    return oldLoadstring(theCode, stringName)
48
  else
49
    return nil, "LuaGuard virus prevention triggered"
50
  end
51
end
52
53
local function fakeLoadfile(filename)
54
  local fh = fs.open(filename, "r")
55
  if fh then
56
    local func = fakeLoadstring(fh.readAll())
57
    return func
58
  end
59
end
60
61
local function fakeDofile(filename)
62
  local f = assert(fakeLoadfile(filename))
63
  return f()
64
end
65
66
local function fakeOSrun( _tEnv, _sPath, ... )
67
    local tArgs = { ... }
68
    local fnFile, err = fakeLoadfile( _sPath )
69
    if fnFile then
70
        local tEnv = _tEnv
71
        --setmetatable( tEnv, { __index = function(t,k) return _G[k] end } )
72
        setmetatable( tEnv, { __index = _G } )
73
        setfenv( fnFile, tEnv )
74
        local ok, err = pcall( function()
75
            fnFile( unpack( tArgs ) )
76
        end )
77
        if not ok then
78
            if err and err ~= "" then
79
                printError( err )
80
            end
81
            return false
82
        end
83
        return true
84
    end
85
    if err and err ~= "" then
86
        printError( err )
87
    end
88
    return false
89
end
90
91
if definitions then
92
  definitions = loadstring(definitions.readAll())()
93
  loadstring = fakeLoadstring
94
  loadfile = fakeLoadfile
95
  dofile = fakeDofile
96
  os.run = fakeOSrun
97
else
98
  drawScreen()
99
  print("Hello! LuaGuard was unable to fetch virus definitions.")
100
  print("Your computer will boot in unsafe mode")
101
  print("")
102
  print("[Press any key to continue, or ctrl-R to reboot]")
103
  os.pullEvent("key")
104
end
105
106
local function transparent()
107
  local oldfs = {}
108
  oldfs.open = fs.open
109
  oldfs.delete = fs.delete
110
  oldfs.move = fs.move
111
  oldfs.copy = fs.copy
112
  oldfs.exists = fs.exists
113
  oldfs.combine = fs.combine
114
  oldfs.list = fs.list
115
  local oldos = {}
116
  oldos.pullEventRaw = os.pullEventRaw
117
118
119
  local function redirect(path)
120
      if oldfs.combine("/", path) == "startup" then
121
          return "/.luaguardstartup"
122
      else
123
          return path
124
      end
125
  end
126
127
  fs.open = function(path, mode)
128
      return oldfs.open(redirect(path), mode)
129
  end
130
131
  fs.delete = function(path)
132
      return oldfs.delete(redirect(path))
133
  end
134
135
  fs.move = function(path, dest)
136
      return oldfs.move(redirect(path), redirect(dest))
137
  end
138
139
  fs.copy = function(path, dest)
140
      return oldfs.copy(redirect(path), redirect(dest))
141
  end
142
143
  fs.exists = function(path)
144
      return oldfs.exists(redirect(path))
145
  end
146
147
  fs.list = function(path)
148
      if oldfs.combine("/", path) == "" then
149
          if oldfs.exists("/.luaguardstartup") then
150
            local list = oldfs.list("/")
151
            local newlist = {}
152
            for k, v in pairs(list) do
153
                if v ~= oldfs.combine("/.luaguardstartup", "/") then
154
                    table.insert(newlist, v)
155
                end
156
            end
157
            return newlist
158
          else
159
              local list = oldfs.list("/")
160
              local newlist = {}
161
              for k, v in pairs(list) do
162
                  if v ~= "startup" then
163
                      table.insert(newlist, v)
164
                  end
165
              end
166
              return newlist
167
          end
168
      else
169
          return oldfs.list(path)
170
      end
171
  end
172
end
173
174
transparent()
175
print("LuaGuard active")
176
177
if fs.exists("/startup") then
178
  term.setTextColor(colors.white)
179
  shell.run("/startup")
180
end