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 


Analyzing A Piece of Assembly Code
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sat Feb 14, 2009 3:13 pm    Post subject: Analyzing A Piece of Assembly Code Reply with quote

I'm trying to analyze the logic behind a piece of assembly code from Iczelion's PE Tutorial 6: Import Table. I understand the assembly code, just not so much the logic behind it. Here's the function I'm analyzing (RVAToOffset):

Code:

RVAToOffset PROC uses edi esi edx ecx pFileMap:DWORD,RVA:DWORD
   mov esi,pFileMap
   assume esi:ptr IMAGE_DOS_HEADER
   add esi,[esi].e_lfanew
   assume esi:ptr IMAGE_NT_HEADERS
   mov edi,RVA ; edi == RVA
   mov edx,esi
   add edx,sizeof IMAGE_NT_HEADERS
   mov cx,[esi].FileHeader.NumberOfSections
   movzx ecx,cx
   assume edx:ptr IMAGE_SECTION_HEADER
   .while ecx>0 ; check all sections
     .if edi>=[edx].VirtualAddress
       mov eax,[edx].VirtualAddress
       add eax,[edx].SizeOfRawData
       .if edi<eax ; The address is in this section
         mov eax,[edx].VirtualAddress
         sub edi,eax
         mov eax,[edx].PointerToRawData
         add eax,edi ; eax == file offset
         ret
       .endif
     .endif
     add edx,sizeof IMAGE_SECTION_HEADER
     dec ecx
   .endw
   assume edx:nothing
   assume esi:nothing
   mov eax,edi
   ret
RVAToOffset endp


I get this is the basis of what it's doing:

Quote:

esi = pFileMap (address of file in memory)
esi = start of PE Header (IMAGE_NT_HEADERS)
edi = RVA
edx = esi (start of PE Header (IMAGE_NT_HEADERS))
edx = Start of Section Table (edx += sizeof(IMAGE_NT_HEADERS), IMAGE_SECTION_HEADER)
ecx = [esi].FileHeader.NumberOfSections (IMAGE_NT_HEADERS.FileHeader.NumberOfSections)
while loop all sections (ecx>0)
check if edi (the RVA) is more than or equal to [edx].VirtualAddress (IMAGE_SECTION_HEADER.VirtualAddress)
{
if so move [edx.VirtualAddress] (IMAGE_SECTION_HEADER.VirtualAddress) into eax
add SizeOfRawData to eax
check if edi (the RVA) is less than eax
if so move [edx].VirtualAddress (IMAGE_SECTION_HEADER.VirtualAddress) into eax
subtract eax from edi (the RVA)
move [edx].PointerToRawData (IMAGE_SECTION_HEADER.PointerToRawData) into eax
add edi (the RVA) to eax
return with the correct offset in eax
}

if not add sizeof(IMAGE_SECTION_HEADER) to edx (move on to next section)
decrease the value of ecx (number of sections) by one


My problem is with the whole bolded part. I just don't get the logic behind it all.

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
tony2108
Advanced Cheater
Reputation: 0

Joined: 26 Nov 2008
Posts: 63
Location: Hacking Battlefield

PostPosted: Sat Feb 14, 2009 4:04 pm    Post subject: Reply with quote

well it's the computer's language what can you say about it?
You created it O,o
Though what was the reason you created it for?

_________________
"Dark Angel is watching you"
Back to top
View user's profile Send private message MSN Messenger
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sat Feb 14, 2009 4:14 pm    Post subject: Reply with quote

That was totally irrelevant. Perhaps you should re-read the post or not spam?
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
tony2108
Advanced Cheater
Reputation: 0

Joined: 26 Nov 2008
Posts: 63
Location: Hacking Battlefield

PostPosted: Sat Feb 14, 2009 4:32 pm    Post subject: Reply with quote

well sorry i didn't know that was spam i just want to know the reason but yeah i didn't read it right
read the tutorial it's huge xD
it sais:
Code:
check if edi (the RVA) is more than or equal to [edx].VirtualAddress


it has to do something with converting RVA to file offset and IMAGE_SECTION_HEADER is the structure to do it.

Quote:
check if edi (the RVA) is more than or equal to [edx].VirtualAddress (IMAGE_SECTION_HEADER.VirtualAddress)
{
if so move [edx.VirtualAddress] (IMAGE_SECTION_HEADER.VirtualAddress) into eax
add SizeOfRawData to eax
check if edi (the RVA) is less than eax
if so move [edx].VirtualAddress (IMAGE_SECTION_HEADER.VirtualAddress) into eax
subtract eax from edi (the RVA)
move [edx].PointerToRawData (IMAGE_SECTION_HEADER.PointerToRawData) into eax
add edi (the RVA) to eax
return with the correct offset in eax
}


it gives you the results of converting ?
it's a discussion btw not spamming. I just read this and it's really confusing Sad

_________________
"Dark Angel is watching you"
Back to top
View user's profile Send private message MSN Messenger
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sat Feb 14, 2009 4:37 pm    Post subject: Reply with quote

I know what it does tony lol. I just don't get the logic behind the section in bold:

Quote:


check if edi (the RVA) is more than or equal to [edx].VirtualAddress (IMAGE_SECTION_HEADER.VirtualAddress)
{
if so move [edx.VirtualAddress] (IMAGE_SECTION_HEADER.VirtualAddress) into eax
add SizeOfRawData to eax
check if edi (the RVA) is less than eax
if so move [edx].VirtualAddress (IMAGE_SECTION_HEADER.VirtualAddress) into eax
subtract eax from edi (the RVA)
move [edx].PointerToRawData (IMAGE_SECTION_HEADER.PointerToRawData) into eax
add edi (the RVA) to eax
return with the correct offset in eax
}


_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
tony2108
Advanced Cheater
Reputation: 0

Joined: 26 Nov 2008
Posts: 63
Location: Hacking Battlefield

PostPosted: Sat Feb 14, 2009 4:50 pm    Post subject: Reply with quote

oh i thought that the logic was all about converting T_T
_________________
"Dark Angel is watching you"
Back to top
View user's profile Send private message MSN Messenger
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sun Feb 15, 2009 12:33 pm    Post subject: Reply with quote

What I don't get is that the RVAs are relative to the image base. But when you load the file into memory, wouldn't the RVAs become relative to the address in memory where it got loaded.
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
BanMe
Master Cheater
Reputation: 0

Joined: 29 Nov 2005
Posts: 375
Location: Farmington NH, USA

PostPosted: Sun Feb 15, 2009 12:51 pm    Post subject: Reply with quote

More coherent Explanation..

Code:

RVAToOffset PROC uses edi esi edx ecx pFileMap:DWORD,RVA:DWORD
   mov esi,pFileMap
   assume esi:ptr IMAGE_DOS_HEADER
   add esi,[esi].e_lfanew
   assume esi:ptr IMAGE_NT_HEADERS
   mov edi,RVA ; edi == RVA
   mov edx,esi
   add edx,sizeof IMAGE_NT_HEADERS
   mov cx,[esi].FileHeader.NumberOfSections
   movzx ecx,cx
   assume edx:ptr IMAGE_SECTION_HEADER
   .while ecx>0 ; check all sections
     ;RVA's being offsets into the Dll
     ;if RVA Greater then or equal to the virtual address (OFFSET>=Current Virtual Address
;this means that if OFFSet is biggger or equal to current Section
      ;int 3 step here to see good stuff and understand better
       
     .if edi>=[edx].VirtualAddress
       ;image section header virtual address
       ;VirtualAddress
       ;The address of the first byte of the section when loaded into
       ;memory, relative to the image base.
       ;VA to eax
       mov eax,[edx].VirtualAddress 
       ;add the size of the rawdata in that section
       ;VA+Sizeof(Data)
       add eax,[edx].SizeOfRawData
       ;if edi is <
       ;VA+SizeOfRawData
       .if edi<eax ; Then The address is in this section
         ;we found our  RVA is within the current section
         ;current VA to EAX
         mov eax,[edx].VirtualAddress
         ;this is where i get a lil fuzzy why subtract eax, from edi..
         sub edi,eax
         ; file pointer to the first page within the COFF file
         ;they mean to say its a pointer to the image Base Address
         mov eax,[edx].PointerToRawData
         ;add image Base + RVA  and get our current Virtual Address(not to be confused with Offset)..
         add eax,edi ;
         ret
       .endif
     .endif
     add edx,sizeof IMAGE_SECTION_HEADER
     dec ecx
   .endw
   assume edx:nothing
   assume esi:nothing
   mov eax,edi
   ret
RVAToOffset endp


regards BanMe

_________________
don't +rep me..i do not wish to have "status" or "recognition" from you or anyone.. thank you.


Last edited by BanMe on Mon Feb 16, 2009 11:26 am; edited 2 times in total
Back to top
View user's profile Send private message MSN Messenger
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sun Feb 15, 2009 2:14 pm    Post subject: Reply with quote

That didn't really help. I can figure out what the assembly is doing fine, I just don't get the logic behind it. And as I said before, can't you just use the same RVAs but relative to the address where the file was l oaded in memory?
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
BanMe
Master Cheater
Reputation: 0

Joined: 29 Nov 2005
Posts: 375
Location: Farmington NH, USA

PostPosted: Sun Feb 15, 2009 3:06 pm    Post subject: Reply with quote

ok here a lamens break down

we have a RVA
we have a module Base
we look at module and find the headers
we store module base into some var
add the size of the headers to modulebase var
we add sizeofrawdata to the var
then we chack if the RVA falls into the area of var
[modulebase+sizeHeaders+SizeofRawDate >= var]
if it doesn fall into that area then we add the next section and check again Wink

regards BanMe

_________________
don't +rep me..i do not wish to have "status" or "recognition" from you or anyone.. thank you.
Back to top
View user's profile Send private message MSN Messenger
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sun Feb 15, 2009 4:02 pm    Post subject: Reply with quote

Oh, I get it now. But still, why can't we just use the same RVAs but relative to the address where the file got loaded in memory?
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
BanMe
Master Cheater
Reputation: 0

Joined: 29 Nov 2005
Posts: 375
Location: Farmington NH, USA

PostPosted: Sun Feb 15, 2009 4:22 pm    Post subject: Reply with quote

I Meant to say you just take the RVA and add GetModuleHandle("ModuleName") to it to locate that data ..
regards BanMe

_________________
don't +rep me..i do not wish to have "status" or "recognition" from you or anyone.. thank you.


Last edited by BanMe on Sun Feb 15, 2009 9:26 pm; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Sun Feb 15, 2009 4:23 pm    Post subject: Reply with quote

oib111 wrote:
Oh, I get it now. But still, why can't we just use the same RVAs but relative to the address where the file got loaded in memory?

Because of sections. The data is mapped to memory based on sections. (The section header describes where the section starts and ends etc). So it's not the same as it is in the file.
For example:
data section A: 1234
data section B: 5678

Then in the file, it's stored as: [section A header],1234,[maybe some padding 0's],[section B header],5678,[maybe some padding 0's]

But in memory, it gets stored differently, so instead of the data of each section being right after each other, they are on different pages.
So for example:
04001000: 1234
04002000: 5678
So in memory, there's almost 1000 bytes in between the data, while in the file there are only a few bytes in between.

I hope that explains it.
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sun Feb 15, 2009 7:58 pm    Post subject: Reply with quote

Thanks tombana that did. Question for BanMe though. What is "OldModuleBase" and "NewModuleBase"?
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
BanMe
Master Cheater
Reputation: 0

Joined: 29 Nov 2005
Posts: 375
Location: Farmington NH, USA

PostPosted: Sun Feb 15, 2009 8:22 pm    Post subject: Reply with quote

i made a mistake in my thought process on that one..i admit it..
i forgot the RVA was already the Offset MINUS The BaseAddress of the Module in memory..

quoted wikipedia..
Quote:

Relative Virtual Addresses (RVAs) are not to be confused with standard virtual addresses. A relative virtual address is the virtual address of an object from the file once it is loaded into memory, minus the base address of the file image. If the file were to be mapped literally from disk to memory, the RVA would be the same as that of the offset into the file, but this is actually quite unusual.

Note that the RVA term is only used with objects in the image file. Once loaded into memory, the image base address is added, and ordinary VAs are used.



sadly i made that mistake xD

regards BanMe

_________________
don't +rep me..i do not wish to have "status" or "recognition" from you or anyone.. thank you.
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
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 can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites