Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


aob manipulating lua function

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Tutorials -> LUA Tutorials
View previous topic :: View next topic  
Author Message
panraven
Grandmaster Cheater
Reputation: 54

Joined: 01 Oct 2008
Posts: 941

PostPosted: Wed Apr 08, 2015 3:11 am    Post subject: aob manipulating lua function This post has 1 review(s) Reply with quote

Some home-made aob manipulating lua function:
Code:
--

function byte2aob(b) return type(b)=='number' and b<256 and b>=0 and string.format('%02X',b) or '??' end
function aob2byte(a) a = tonumber(a,16) return type(a)=='number' and a <256 and a>=0 and a or -1 end

function imap(t,f) local s={} for i=1,#t do s[i]=f(t[i]) end return s end
function n2bt(n,t) t=type(t)=='string' and t or 'dword'  return rawget(_G,t..'ToByteTable')(n) end
function t2aob(t,sep) return table.concat(imap(t,byte2aob),type(sep)=='string' and sep or ' ') end
function n2aob(n,t) return t2aob(n2bt(n,t)) end
function s1aob(s) return t2aob(n2bt(s,'string')) end
function s2aob(s) return t2aob(n2bt(s,'wideString')) end
function aob2bt(a)
  local s = a:gsub('%S+',function(r) -- check parts
    local t,l = {},string.len(r)
    assert(string.len(r) % 2 == 0, 'some aob part has odds number of hex digits:'..l..'-'..r)
    for i=1,l/2 do t[i] = byte2aob(aob2byte(string.sub(r,i*2-1,i*2))) end-- extract byte from each 2 hex digits
    return table.concat(t)
  end):gsub('%s+','') -- trim all spaces
  local t,l = {},string.len(s)
  assert(l % 2 == 0, 'the aob has odds number of hex digits:'..l..'-'..s)
  for i=1,l/2 do t[i] = aob2byte(string.sub(s,i*2-1,i*2)) end-- extract byte from each 2 hex digits
  return t
end
function normalize(a,sep) return t2aob(aob2bt(a),type(sep)=='string' and sep or '') end -- check even hex digits, default no space form

function replaceBytes(a,p,r,m)
  assert(p>0,'replace position must be positive')
  local t = aob2bt(a)
  local isTable = type(r)=='table'
  local l = isTable and #r or r
  assert(type(l)=='number','replace table or length not valid')
  l = math.min(l,type(m)=='number' and m or l)
  for i=#t+1,p+l-1 do t[i]=0 end
  for i=1,l do t[p+i-1] = isTable and r[i] or -1 end
  return t2aob(t)
end

function join(sep,...) return table.concat(imap({...},tostring),type(sep)=='string' and sep or " ") end
function ajoin(...)
  return join(" ",unpack(imap({...},function(a)
    if type(a) == 'number' then a = byte2aob(a) end
    if type(a) == 'table' then a = t2aob(a) end
    return a
  end)))
end

-- application
function unityString(s,wide)
  local stoaob = wide == true and s2aob or s1aob
  return ajoin(n2aob(string.len(s)),stoaob(s))
end

-- test
print(n2aob(999,'double'))
local u = unityString('Hello CheatEngine')
local w = unityString('Hello CheatEngine',true)
print(u)
print(normalize(w))

-- r = aobscan(u) --
local r = 0x451200 - 8  -- unity string struct address at -8 offset of pattern [str-len:4bytes][str-chars]
local c = ajoin(0x68,n2aob(r),'50 ?? ?? e8') -- push [string addr] ; push some-eax ; call some-where
print(c)
local d,e = replaceBytes(c,2,4),replaceBytes(c,10,n2bt(1000000),2)
print(d) -- replace with how many wildcard, here 4, at position 2
print(e) -- replace/insert 1st 2 bytes of aob(1000000):0f4240 after e8, give ... e8 40 42

--[[ output
00 00 00 00 00 38 8F 40
11 00 00 00 48 65 6C 6C 6F 20 43 68 65 61 74 45 6E 67 69 6E 65
11000000480065006C006C006F0020004300680065006100740045006E00670069006E006500
68 F8 11 45 00 50 ?? ?? e8
68 ?? ?? ?? ?? 50 ?? ?? E8
68 F8 11 45 00 50 ?? ?? E8 40 42
--]]


ADDED:
acceptable 2nd parameter for n2aob & n2bt is string 'word','dword'...etc. from these ce function:
Code:
wordToByteTable(number),
dwordToByteTable(number),
qwordToByteTable(number),
floatToByteTable(number),
doubleToByteTable(number),
stringToByteTable(string),
wideStringToByteTable(string)


default 'dword'
Back to top
View user's profile Send private message
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Sat Apr 03, 2021 12:45 pm    Post subject: Reply with quote

Code:
function byteTableToAobString(t)
  for k,v in ipairs(t) do
    t[k] = ('%02X'):format(v)
  end
  return table.concat(t, ' ')
end

print(byteTableToAobString(wordToByteTable("65535"))) --65536 = 00 00 01
print(byteTableToAobString(dwordToByteTable("1000000")))
print(byteTableToAobString(qwordToByteTable("1000000")))
print(byteTableToAobString(floatToByteTable("1000000")))
print(byteTableToAobString(doubleToByteTable("1000000")))
print(byteTableToAobString(stringToByteTable("cheat 123")))
print(byteTableToAobString(wideStringToByteTable("cheat engine 123")))


result:
Code:
FF FF
40 42 0F 00
40 42 0F 00 00 00 00 00
00 24 74 49
00 00 00 00 80 84 2E 41
63 68 65 61 74 20 31 32 33
63 00 68 00 65 00 61 00 74 00 20 00 65 00 6E 00 67 00 69 00 6E 00 65 00 20 00 31 00 32 00 33 00


What is the way to reverse these transactions?
Thanks ..
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Tutorials -> LUA Tutorials All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites