Skip to content

Commit bad0aee

Browse files
authored
anti-evasion system
fak u srlion
1 parent 1418f94 commit bad0aee

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

lua/badcoderz/sh_code_smells.lua

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local reports = {}
2+
23
local function do_report(func, hookname, lines, parentFuncs)
34
reports[func] = reports[func] or {}
45
reports[func][hookname] = reports[func][hookname] or {}
@@ -21,67 +22,68 @@ end
2122

2223
local color_red = Color(255, 50, 50)
2324

24-
2525
local function fixGMAPath(path)
2626
if file.Exists("garrysmod/" .. path, "BASE_PATH") then return path end
2727

2828
for k, v in pairs(BadCoderz.GMA_DB) do
2929
if table.HasValue(v, path) then
3030
path = k .. "/" .. path
31+
3132
return path
3233
end
3334
end
35+
3436
return path
3537
end
3638

37-
3839
--[[
3940
Some libs like ULib overwrite the hooks functions, so to detect it by locations we need to manually run a hook
4041
then detect the top function in the call stack and decide it's the file where the call comes from
4142
]]
4243
local function initScan()
4344
local debugTable
4445
local level = 1
46+
4547
while true do
4648
local _debugTable = debug.getinfo(level, "S")
4749
if not _debugTable then break end
4850
debugTable = _debugTable
4951
level = level + 1
5052
end
53+
5154
assert(debugTable, "What the fuck did you do to the hook system ?")
5255
BadCoderz.potentialsHooksFiles[debugTable.source:sub(2)] = true
5356
hook.Remove("Think", "badCoderzTrapHook")
5457
end
5558

59+
local hookNames = {
60+
name = true,
61+
event_name = true
62+
}
63+
5664
local function _hook()
5765
local curStackLevel = 2
5866
local calledFunc = debug.getinfo(curStackLevel, "f").func
59-
60-
if not BadCoderz.heavy_funcs[calledFunc] then
61-
return
62-
end
63-
67+
if not BadCoderz.heavy_funcs[calledFunc] then return end
6468
--threats functions like Color/Angle/Vector in a different way since the way it's called matters
6569
local heavyObject = BadCoderz.heavy_funcs_objects[calledFunc]
70+
6671
if heavyObject then
6772
local callingContext = debug.getinfo(curStackLevel + 1, "fSl")
6873
if callingContext.what == "C" then return end -- C could be calling it for some reason
6974
local callingContextFunc = callingContext.func
70-
7175
local found = BadCoderz.find_call_static_args(callingContextFunc, heavyObject, callingContext.currentline)
72-
if not found then
73-
return
74-
end
75-
76+
if not found then return end
7677
end
7778

78-
7979
local level = 0
8080
traceTable = {}
81+
8182
while true do
8283
local debugTable = debug.getinfo(level, "Sl")
8384
if not debugTable then break end
8485
level = level + 1
86+
8587
-- already got level++'ed, don't do it again here
8688
traceTable[level] = {
8789
location = debugTable.source:sub(2),
@@ -98,8 +100,8 @@ local function _hook()
98100
local foundName
99101

100102
if name == "self" then
101-
102103
local func = debug.getinfo(topStack - 1, "f").func
104+
103105
if value == gmod.GetGamemode() then
104106
for k, v in pairs(value) do
105107
if not isfunction(v) then continue end
@@ -109,6 +111,7 @@ local function _hook()
109111
break
110112
end
111113
end
114+
112115
if foundName and BadCoderz.dangerous_hooks[foundName] then
113116
foundHookContext = "GM:" .. foundName
114117
foundhookLevel = topStack - 1
@@ -149,18 +152,18 @@ local function _hook()
149152
end
150153
else
151154
print("Var type " .. type(value) .. " is not implemented in BadCoderz, pls tell the dev")
155+
152156
return
153157
end
154158
elseif BadCoderz.potentialsHooksFiles[stackData.location] then
155159
local hookStackLevel = topStack - 1
156160
local i = 1
157161

158-
159162
while (true) do
160163
local _name, _value = debug.getlocal(hookStackLevel, i)
161164
if (_name == nil) then break end
162165

163-
if _name == "name" then
166+
if hookNames[_name] == true then
164167
foundName = _value
165168
break
166169
end
@@ -188,11 +191,17 @@ local function _hook()
188191

189192
while (foundhookLevel >= targetStackLevel) do
190193
local data = debug.getinfo(foundhookLevel, "lSf")
194+
191195
if data.currentline == -1 then
192196
foundhookLevel = foundhookLevel - 1
193197
continue
194198
end
195-
local infoline = {location = fixGMAPath(data.source:gsub("^@", "")), line = data.currentline}
199+
200+
local infoline = {
201+
location = fixGMAPath(data.source:gsub("^@", "")),
202+
line = data.currentline
203+
}
204+
196205
table.insert(lines, infoline)
197206
table.insert(functions, data.func)
198207
foundhookLevel = foundhookLevel - 1
@@ -201,19 +210,19 @@ local function _hook()
201210
do_report(BadCoderz.heavy_funcs[calledFunc], foundHookContext, lines, functions)
202211
end
203212

204-
205213
local function start_scan()
214+
print("started scan")
206215
jit.off()
207216
-- you also need to flush the jit cache because it may miss something if one day LuaJIT stitching gets jitted with black magic
208217
jit.flush()
209-
210218
hook.Add("Think", "badCoderzTrapHook", initScan)
211219
BadCoderz.scanningCodeSmells = true
212220
reports = {}
213221

214222
if CLIENT and gui.IsConsoleVisible() then
215223
MsgC(color_red, "PLEASE CLOSE THE CONSOLE TO RUN ALL CLIENTS CHECKS\n")
216224
end
225+
217226
debug.sethook(_hook, "c") -- hook functions calls
218227
end
219228

@@ -230,5 +239,3 @@ function BadCoderz.toggleCodeSmellsScan()
230239
stop_scan()
231240
end
232241
end
233-
234-

0 commit comments

Comments
 (0)