SuperAPI AI 工具集

roblox-security

安全与审计

查看项目

更新于 10-14 07:28

技能介绍

用途

Roblox Security 用于指导 Roblox 游戏脚本的安全编写与审查,重点防止客户端作弊,确保所有权威逻辑在服务端执行。它适用于编写处理玩家操作、货币、属性、伤害或任何 RemoteEvent/RemoteFunction 通信的脚本,也适用于审查现有代码找出可被利用的模式。

核心原则

永远不要信任客户端。每个 LocalScript 都在玩家机器上运行,且可被修改。所有权威逻辑——伤害、货币、属性、位置变更——都必须位于服务端。

现代 Roblox 中 FilteringEnabled 始终开启。除非服务端显式应用,客户端改动不会同步到服务端或其他客户端。

安全与不安全模式对照

  • 造成伤害:不安全做法是 LocalScript 直接设置 Humanoid.Health;安全做法是服务端验证后扣血。
  • 发放货币:不安全做法是 LocalScript 增加 leaderstats;安全做法是服务端验证动作后再增加。
  • Leaderstats 所有权:不安全做法是 LocalScript 拥有 IntValue;安全做法是服务端创建并拥有所有 leaderstats。
  • 位置变更:不安全做法是 LocalScript 传送角色;安全做法是服务端验证后再移动角色。
  • 工具使用:不安全做法是客户端命中即触发伤害;安全做法是服务端射线检测并施加伤害。
  • 冷却:不安全做法是客户端本地记录冷却;安全做法是服务端按玩家记录冷却。

主要能力与代码示例

安全的 Leaderstats 设置

应放在 ServerScriptService 的 Script 中,而非 LocalScript:

-- Script in ServerScriptService — never LocalScript
game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Value = 0
    coins.Parent = leaderstats
end)

服务端合理性检查

距离检查:

local MAX_INTERACT_DISTANCE = 10

InteractRemote.OnServerEvent:Connect(function(player, targetPart)
    if typeof(targetPart) ~= "Instance" or not targetPart:IsA("BasePart") then return end

    local root = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
    if not root then return end

    if (root.Position - targetPart.Position).Magnitude > MAX_INTERACT_DISTANCE then
        warn(player.Name .. " sent interaction from invalid distance")
        return
    end

    processInteraction(player, targetPart)
end)

冷却验证:

local ABILITY_COOLDOWN = 5
local lastUsed = {}

UseAbilityRemote.OnServerEvent:Connect(function(player)
    local now = os.clock()
    if now - (lastUsed[player] or 0) < ABILITY_COOLDOWN then return end
    lastUsed[player] = now
    applyAbility(player)
end)

game.Players.PlayerRemoving:Connect(function(player)
    lastUsed[player] = nil
end)

属性边界检查:

local MAX_QUANTITY = 99
local ITEM_COST = 50

BuyItemRemote.OnServerEvent:Connect(function(player, quantity)
    if type(quantity) ~= "number" then return end
    quantity = math.clamp(math.floor(quantity), 1, MAX_QUANTITY)

    local coins = player.leaderstats.Coins
    if coins.Value < ITEM_COST * quantity then return end

    coins.Value = coins.Value - (ITEM_COST * quantity)
    -- award items server-side
end)

速率限制:

local RATE_LIMIT = 10   -- max calls
local RATE_WINDOW = 1   -- per second
local callLog = {}

local function isRateLimited(player)
    local now = os.clock()
    local log = callLog[player] or {}
    local pruned = {}
    for _, t in ipairs(log) do
        if now - t < RATE_WINDOW then table.insert(pruned, t) end
    end
    if #pruned >= RATE_LIMIT then
        callLog[player] = pruned
        return true
    end
    table.insert(pruned, now)
    callLog[player] = pruned
    return false
end

ActionRemote.OnServerEvent:Connect(function(player)
    if isRateLimited(player) then return end
    handleAction(player)
end)

game.Players.PlayerRemoving:Connect(function(player)
    callLog[player] = nil
end)

参数验证工具:

-- ServerScriptService/Modules/Validate.lua
local Validate = {}

function Validate.number(value, min, max)
    if type(value) ~= "number" then return false end
    if value ~= value then return false end -- NaN check
    if min and value < min then return false end
    if max and value > max then return false end
    return true
end

function Validate.instance(value, className)
    if typeof(value) ~= "Instance" then return false end
    if className and not value:IsA(className) then return false end
    return true
end

function Validate.string(value, maxLength)
    if type(value) ~= "string" then return false end
    if maxLength and #value > maxLength then return false end
    return true
end

return Validate

-- Usage
local Validate = require(script.Parent.Modules.Validate)

remote.OnServerEvent:Connect(function(player, amount, targetPart)
    if not Validate.number(amount, 1, 100) then return end
    if not Validate.instance(targetPart, "BasePart") then return end
    -- safe to proceed
end)

速度 / 反作弊检测:

local SPEED_LIMIT = 32
local violations = {}

task.spawn(function()
    while true do
        task.wait(2)
        for _, player in ipairs(game.Players:GetPlayers()) do
            local root = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
            if root and root.AssemblyLinearVelocity.Magnitude > SPEED_LIMIT then
                violations[player] = (violations[player] or 0) + 1
                if violations[player] >= 3 then
                    player:Kick("Cheating detected.")
                end
            else
                violations[player] = math.max(0, (violations[player] or 0) - 1)
            end
        end
    end
end)

ModuleScript 放置

ServerScriptService/
  Modules/
    DamageCalculator.lua   -- server-only, never exposed to client
    EconomyManager.lua     -- server-only

ReplicatedStorage/
  Remotes/                 -- RemoteEvent/RemoteFunction instances only
  SharedModules/           -- non-sensitive utilities only

绝不要把货币、伤害或 DataStore 逻辑放在 ReplicatedStorage 模块中——客户端可以 require() 它们。

常见错误

  • FireServer(damage) 且服务端信任它:客户端可发送任意值;修复方式是服务端根据自身工具数据计算伤害。
  • 货币存储在 LocalScript 变量:客户端可修改内存;修复方式是仅由服务端拥有。
  • 触发前做客户端距离检查:检查可被绕过;修复方式是服务端收到事件后重新检查。
  • RemoteEvent 处理器无冷却:刷屏即无限资源;修复方式是服务端按玩家冷却。
  • 信任客户端设置的 WalkSpeed:客户端可设为任意高;修复方式是服务端拥有并限制 WalkSpeed。
  • 敏感逻辑放在 ReplicatedStorage 模块:客户端可 require;修复方式是移到 ServerScriptService。

适用场景

适用于所有涉及玩家操作、经济系统、战斗伤害、属性管理、交互事件和移动控制的 Roblox 游戏开发,尤其适合多人游戏和带经济系统的体验。

限制

本资料仅提供服务端安全验证的模式与示例代码,未提供具体游戏的完整架构方案、未涉及 DataStore 的具体安全配置细节,也未说明不同 Roblox 版本间的兼容性差异。