The unhackable Integer

User projects written in or related to FreeBASIC.
mrminecrafttnt
Posts: 86
Joined: Feb 11, 2013 12:23

The unhackable Integer

Postby mrminecrafttnt » Mar 27, 2019 22:55

Hi guys!
I found a way to defend an Integer from all hacks with direct memorywriting etc.
Can anyone try it for me? Simply use CheatEngine or other programms to change the value, but you will fail..
Its just an just4fun project. :)

Code: Select all

type anticheat
    dim as ubyte seed
    dim as ubyte key
    dim as ubyte mem(any)
    declare sub dumpmem(adress as any ptr,length as uinteger)
    declare function compare(adress as any ptr) as integer
end type

sub anticheat.dumpmem(adress as any ptr,length as uinteger)
    do
    seed = int(rnd*256)
    loop until seed <> 0
    length-=1
    if length = 0 then print "ZERO LENGTH" : EXIT SUB
    if adress = 0 then print "ZERO ADRESS" : EXIT SUB
    if length <> ubound(mem) then
   
        redim preserve mem(length)
    end if
   
    for i as integer = 0 to length-1
        mem(i)=peek (adress+i) xor seed
    next
end sub

function anticheat.compare(adress as any ptr ) as integer
   
    if adress = 0 then print "ZERO POINTER@COMPARE": return 0
    dim as ubyte v1,v2
    if ubound(mem) > 0 then
        for i as integer = 0 to ubound(mem)-1
            v1 = mem(i) xor seed
            v2 = peek(adress+i)
           
            if v1 <> v2 then 'cheat detection is here               
                static counter as integer = 3
                counter -=1
                if counter = 0 then return 0
                poke adress+i,mem(i) xor seed
            end if
        next
        return 1
    else
        print "UNALLOCATED MEM!! DUMP FIRST!"
    end if
end function

type ash
    value as integer
    declare constructor
end type

constructor ash
    value = -2
end constructor
   

dim shared instruction as ubyte 'for checking the amount of instructions
type anticheat_main
    T as integer
    errorcompension as integer 'if have one group (value and key) have bad values
                               'so try next (up to errorcompension 2)
                               'if this fails so is the thread trapped in an loop now :)
    value(2) as integer
    key(2) as integer
    hash as integer
    dim as integer cheatdetection 'cheatid code
    anti_selftesthack as ash
    declare sub set (v as integer)
    declare function get as integer
    declare function rnd32 as integer
    declare constructor
end type

constructor anticheat_main
    cheatdetection = anti_selftesthack.value   
    dim as integer v1 = rnd32
    dim as integer v2 = rnd32
    if v1 = v2 then cheatdetection = 8
    dim as integer v = 123456
    if v = 0 then cheatdetection = 7
    color 15
    Print "Selftest results: ";
    Print "RANDOMIZER..";
   
    if rnd32 > 0 and (rnd32 <> rnd32) and (v1 <> v2-1) then
        color 10 :
        pRINT "PASSED";
        cheatdetection+=1
    else
        color 12
        print "FAILED";
        cheatdetection = 5
    end if
    color 15
    PRINT "..SABOTAGE DETECTION..";
   
    set v
    if get <> v then cheatdetection = 1
    set v + 1
    if get <> v + 1 then cheatdetection = 2
    set get - 2
    if get <> v - 1 then cheatdetection = 3
    set 0
    if get <> 0 then cheatdetection = 4
    cheatdetection+=1
    if cheatdetection = 0 then
        color 10
        print "PASSED"
       
    ELSE
       
        color 12
        PRINT "FAILED"
    end if
    color 7
end constructor

function anticheat_main.rnd32 as integer
  static x as integer
  x+=1
  return int(rnd * 2^31)
end function
   
   
sub anticheat_main.set (v as integer)
    if cheatdetection > 0 then exit sub
    instruction+=1
    dim as integer trys = 10
    do
        key(0) = rnd32
        key(1) = rnd32
        trys-=1
        if trys = 0 then exit do
    loop until key(0) > 0 and key(1) > 0
    do
        if errorcompension = 0 then
            value(0) = v xor key(0)
            if value(0) <> (v xor key(0)) then
                errorcompension = 1
                end if
            else
                value(1) = v xor key(1)
                errorcompension = 1
                if value(1) <> (v xor key(1)) then
                    errorcompension = 2
                key(2)= rnd32
                value(2) = v xor key(2)
            end if
        end if
    trys-=1
    if errorcompension > 0 then print "WARNING! TRYING TO CHEAT WAS DETECTED! - THIS WILL NOT BE TOLERATED!"
loop until value(errorcompension) = (v xor key(errorcompension)) or trys <= 0
    if errorcompension > ubound(value) then set v
    if (value(errorcompension) xor key(errorcompension)) <> v then errorcompension = 1 : set v
    hash = key(errorcompension) + value(errorcompension)
    if hash = 0 or key(errorcompension) = 0 then set v
    T =CINT(TIMER)
   
end sub

function anticheat_main.get as integer
    dim as anticheat dump
    dump.dumpmem (@this,len(this))
   
    if cheatdetection > 0 then return rnd32
    static oi as integer
    oi = instruction
   instruction+=1
    if hash = 0 and key(errorcompension) = 0 then return rnd32: errorcompension = 1
    if hash = key(errorcompension) + value(errorcompension) then
        if value(errorcompension) = (value(errorcompension) xor key(errorcompension)) then
            return rnd32
        end if
        if cint(timer)-t > 1 then set rnd32 'anti debugging.. ;)
        if dump.compare (@this) = 0 THEN SET 0
        set value(errorcompension) xor key(errorcompension)
        dump.dumpmem(@this,len(this))
        if instruction-oi <> 2 then cheatdetection = 6
        instruction = 0
        if dump.compare (@this) = 0 THEN SET 0
        return value(errorcompension) xor key(errorcompension)
    else
        return rnd32
    end if
end function


'###example starts here###
locate ,,0
dim as anticheat_main score
dim as anticheat protection
dim as integer protectionvalue
score.set 10000
protectionvalue = score.get
protection.dumpmem(@protectionvalue,len(protectionvalue))
Print "CHEAT ME! INCRASE WITH + / DECRASE WITH -, ESC TO EXIT"

do
    locate 3
    print hex(@score)
   
    print score.get
    if protection.compare(@protectionvalue) = 0 then print "CHEAT DETECTED!!!":EXIT do
    select case inkey
    case "+"
        score.set score.get + 1
        protectionvalue = score.get
        protection.dumpmem(@protectionvalue,len(protectionvalue))
    case "-"
        score.set score.get - 1
        protectionvalue = score.get
        protection.dumpmem(@protectionvalue,len(protectionvalue))
    case chr(27)
        exit do
    end select
loop

Return to “Projects”

Who is online

Users browsing this forum: No registered users and 56 guests