Yo!
In this code, we use the same security a Car uses to protect its car keys for generating dynamic and secure random numbers in Roblox, which can be particularly useful for applications like Anti-Tampers and Anti-Cheats.
What are Rolling Codes?
Rolling codes are a type of security mechanism used in various systems, such as garage door openers, keyless entry systems, and remote controls. They rely on a constantly changing code that is synchronized between the transmitter (e.g., a remote control) and the receiver (e.g., a garage door opener). Each time a new code is generated, it becomes the next valid code in the sequence, preventing replay attacks and ensuring that the same code is never used twice.
Generating Dynamic Random Numbers
To implement rolling codes or similar security mechanisms in Roblox, we need a way to generate random numbers that change over time in a predictable and synchronized manner that cannot be replicated by an attacker. Hereâs an example image on what the code does
-- ReplicatedFirst.Client
local salt = "Hello, World!"
local bytesOfString = 0
for i = 1, #salt do
local byte = string.byte(salt, i)
bytesOfString = bytesOfString + byte
end
--H (72) + e (101) + l (108) + l (108) + o (111) + , (44) + (32) + W (87) + o (111) + r (114) + l (108) + d (100) + ! (33) = 1191
local increment = 0
while wait(1) do
increment+=1
local unixTime = os.time()
local minutesSinceEpoch = math.floor(unixTime / 60)
local randomSeed = minutesSinceEpoch+increment+bytesOfString
local random = Random.new(randomSeed)
local randomNumber = random:NextNumber() -- Random number between 0 and 1
remote:FireServer(randomNumber)
end
Using the Random Number Generator
We can use this code to perform a handshake between the client and the server. This handshake is useful to determine if the client has been tampered in any way and if a remote doesnât come within a specified period of time, kick the player.
-- ServerScriptService.Server
local Remote = game.ReplicatedStorage.RemoteEvent
local salt = "Hello, World!"
local bytesOfString = 0
for i = 1, #salt do
local byte = string.byte(salt, i)
bytesOfString = bytesOfString + byte
end
game.Players.PlayerAdded:Connect(function(player)
local increment = 0
local responseWaitTime = 10
local lastRespondedTime = 0
local playerDisconnected = false
local connection = game.Players.PlayerRemoving:Connect(function(player)
playerDisconnected = true
end)
local connection2 = Remote.OnServerEvent:Connect(function(player, playerNumber)
increment += 1
local unixTime = os.time()
local minutesSinceEpoch = math.floor(unixTime / 60)
local randomSeed = minutesSinceEpoch + increment + bytesOfString
local random = Random.new(randomSeed)
local randomNumber = random:NextNumber() -- Random number between 0 and 1
print("Servers random number: " .. randomNumber)
print("Player " .. player.Name .. "'s sent number: " .. playerNumber)
print("Match: " .. tostring(randomNumber == playerNumber))
if randomNumber==playerNumber then
lastRespondedTime = 0
end
end)
repeat
task.wait(1)
lastRespondedTime +=1
until playerDisconnected or lastRespondedTime>responseWaitTime
connection:Disconnect()
connection2:Disconnect()
if not playerDisconnected then
player:Kick("")
end
end)
Securing the client-side
Well, obviously. If you have the code, the key, and the algorithm, you could just remove the parts of the code that do stuff like anti-cheats, and just have the handshake. This can be prevented by obfuscating the client script.
You can obfuscate LUA scripts with this website:
LuaObfuscator - Playground
Using obfuscation, the attacker wonât have any knowledge of the insides of the contents, leaving everything safe.
Full Code:
RollingCodes.rbxm (3.4 KB)
Feedback is greatly appreciated!