DEV Community

rashid Blox
rashid Blox

Posted on

this code in lua but l dont add GUI in lua what should do next to get work in anygame and this code from deep seej working code

-- ============================================================
-- INFINITE MONEY BUG: DEMO, EXPLOIT, AND FIX
-- Run with: lua money_bug.lua
-- ============================================================

-- ============================================================
-- PART 1: VULNERABLE PLAYER (the bug)
-- ============================================================
local VulnerablePlayer = {}
VulnerablePlayer.__index = VulnerablePlayer

function VulnerablePlayer.new(startingMoney)
return setmetatable({
money = startingMoney or 100,
lastTransaction = 0
}, VulnerablePlayer)
end

-- BUG: accepts negative amounts, no lock, no cap
function VulnerablePlayer:transfer(amount, target)
if self.money >= amount then
self.money = self.money - amount
target.money = target.money + amount
return true
end
return false
end

-- BUG: no cooldown, spammable
function VulnerablePlayer:claimDailyBonus(bonus)
self.money = self.money + bonus
return bonus
end

-- ============================================================
-- PART 2: FIXED PLAYER
-- ============================================================
local Player = {}
Player.__index = Player

local MAX_MONEY = 1e9 -- cap to prevent overflow
local BONUS_COOLDOWN = 86400 -- 24h in seconds
local MIN_TRANSFER = 1
local MAX_TRANSFER = 1e6

function Player.new(startingMoney)
return setmetatable({
money = math.max(0, math.min(startingMoney or 100, MAX_MONEY)),
lastBonusClaim = 0,
isProcessing = false, -- re-entrancy lock
}, Player)
end

-- FIX 1: validate amount (positive, in range, not NaN)
-- FIX 2: re-entrancy lock (blocks race conditions)
-- FIX 3: block self-transfer
-- FIX 4: atomic apply via pcall
function Player:transfer(amount, target)
if type(amount) ~= "number" or amount ~= amount then
return false, "Invalid amount"
end
if amount < MIN_TRANSFER or amount > MAX_TRANSFER then
return false, "Amount out of range"
end
if type(target) ~= "table" or type(target.money) ~= "number" then
return false, "Invalid target"
end
if self == target then
return false, "Cannot transfer to self"
end
if self.isProcessing or target.isProcessing then
return false, "Transaction in progress"
end

self.isProcessing   = true
target.isProcessing = true

local ok, err = pcall(function()
    if self.money < amount then
        error("Insufficient funds")
    end
    self.money   = self.money - amount
    target.money = math.min(target.money + amount, MAX_MONEY)
end)

self.isProcessing   = false
target.isProcessing = false

if not ok then return false, err end
return true
Enter fullscreen mode Exit fullscreen mode

end

-- FIX 5: server-authoritative cooldown + cap
function Player:claimDailyBonus(bonus, currentTime)
currentTime = currentTime or os.time()

if currentTime - self.lastBonusClaim < BONUS_COOLDOWN then
    return false, "Bonus already claimed"
end

self.money          = math.min(self.money + bonus, MAX_MONEY)
self.lastBonusClaim = currentTime
return true, bonus
Enter fullscreen mode Exit fullscreen mode

end

-- ============================================================
-- PART 3: TEST HARNESS
-- ============================================================
local function banner(title)
print("\n" .. string.rep("=", 60))
print(title)
print(string.rep("=", 60))
end

local function check(label, got, expected)
local pass = got == expected
print(string.format(" [%s] %-35s got=%s expected=%s",
pass and "PASS" or "FAIL", label, tostring(got), tostring(expected)))
return pass
end

-- ============================================================
-- PART 4: EXPLOIT THE VULNERABLE VERSION
-- ============================================================
banner("EXPLOITING VULNERABLE VERSION")

local v1 = VulnerablePlayer.new(100)
local v2 = VulnerablePlayer.new(100)

-- Exploit A: negative transfer drains the target
v1:transfer(-1000, v2)
print((" Exploit A (negative transfer): v1=%d v2=%d <-- BUG"):format(v1.money, v2.money))

-- Exploit B: spam the bonus (simulating 1000 calls in a single tick)
for i = 1, 1000 do
v1:claimDailyBonus(50)
end
print((" Exploit B (bonus spam): v1=%d <-- BUG"):format(v1.money))

-- Exploit C: integer overflow (huge values)
v1.money = MAX_MONEY
v1:claimDailyBonus(MAX_MONEY)
print((" Exploit C (overflow): v1=%s <-- BUG"):format(tostring(v1.money)))

-- ============================================================
-- PART 5: VERIFY THE FIX
-- ============================================================
banner("VERIFYING FIXED VERSION")

local p1 = Player.new(100)
local p2 = Player.new(100)
local now = os.time()
local allPass = true

-- Test 1: negative transfer rejected
local ok, err = p1:transfer(-1000, p2)
allPass = check("negative transfer rejected", ok, false) and allPass
allPass = check(" error message", err, "Amount out of range") and allPass

-- Test 2: zero transfer rejected
ok, err = p1:transfer(0, p2)
allPass = check("zero transfer rejected", ok, false) and allPass

-- Test 3: self-transfer rejected
ok, err = p1:transfer(50, p1)
allPass = check("self-transfer rejected", ok, false) and allPass

-- Test 4: valid transfer works
ok = p1:transfer(50, p2)
allPass = check("valid transfer succeeds", ok, true) and allPass
allPass = check(" p1 balance", p1.money, 50) and allPass
allPass = check(" p2 balance", p2.money, 150) and allPass

-- Test 5: insufficient funds rejected
ok, err = p1:transfer(999, p2)
allPass = check("insufficient funds rejected", ok, false) and allPass

-- Test 6: bonus spam — only 1 should succeed
local count = 0
for i = 1, 1000 do
if p1:claimDailyBonus(50, now) then count = count + 1 end
end
allPass = check("bonus claimed exactly once", count, 1) and allPass
allPass = check(" p1 balance after bonus", p1.money, 100) and allPass

-- Test 7: overflow capped
p1.money = MAX_MONEY
p1.lastBonusClaim = 0
p1:claimDailyBonus(MAX_MONEY, now + BONUS_COOLDOWN + 1)
allPass = check("overflow capped at MAX_MONEY", p1.money, MAX_MONEY) and allPass

-- Test 8: NaN rejected
ok, err = p1:transfer(0/0, p2)
allPass = check("NaN amount rejected", ok, false) and allPass

-- ============================================================
-- PART 6: SUMMARY
-- ============================================================

Top comments (0)