+ Reply to Thread
Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Arunpreet Singh

Thread: Size of Function in MSVC Share/Save - My123World.Com!

  1. #1
    Garage Newcomer Arunpreet Singh is on a distinguished road
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    0
    Thanked 5 Times in 3 Posts

    Size of Function in MSVC



    I got this problem when i was making a little packer . The Idea is to Copy the Loader in Seprate Section and then Redirect the OEP to Section Containing Loader.
    I write a very simple Loader using C and some basic inline Asm .. Now Prob is How to COpy a Function to a Section ..

    Simple Answer is to make a Function Pointer and get function Address and copy the Function into Section

    But Real Thing is how to get Size of Function ..

    So Here i am Writing a the Sample Code i Written to Solve this ,May be Someone Find this useful
    Code:
    First Make a Function Pointer 
    
    int (*LP)(void);
    
    LP=&Loader ;         //Name of My function is "Loader"..Here We putthing function address into our function pointer LP
      
    __asm
    {
        push ebx          //Save registers
        push edx
        mov  ebx,LP     //Now Ebx = Function Address
    	
    	
    //Here Important thing to Note is  When we get a Function  Address ...We Do not Get Pointer to Exact Code ..First We have A JUMP which Jumps
           to orginal code of function	(i am using MSVC ,IDE VS2010)
    	
    //   So we have to Understand the JUMP   to get Address of Actual Code 
    // JUMP   INSTRUCTION   Actually Contains the Relative Address ...The Real JUMP Val is Calculated as  EIP (Current Instruction Address)+ Relative Address  in Jump+Size of JMP instruction 
    	
    
    										 
    
    	 Example From Ollydbg 
                        					 
    	 Instruction address       JUMP OPCODE        Equvalent Dissambly  
    	       0129119A                  E9 71020000        JMP 01291410 
    	                  
    					  
       So How it Become JMP 01291410 ... 0129119A(insturction address)+0000271(Realtive Address in JUMP)+5 (Jump insturction size) 
    	
    	  *Relative Jump  address is in reverse Byte order....thats how Data is Stored in memory      
    						   
    	inc  ebx                    // SKIP OPCODE E9  NOW EBX Point to Relative address conatined in Jump
    	mov  edx,[ebx]        //Get Relative Address in Jump
    	add  ebx,edx          // EBX =Address of Jump Instruction + Relative Address in JUMP
            add  ebx,4             // To get Orginal Address of Function we have to add size of JUMP instruction that is 5..But We Incremented ebx in
    	                               in starting ..So add 4 here
    	
    	
    	mov LP,ebx         //Now We have Exact Address in LP
    	pop edx            //POP Out Registers
    	pop ebx
    }
    
    tt=(DWORD)LP;         
    
    while(1)                 //Infinte Loop
    {
    
    	__asm
    	{
    		push ebx              //Save Registers Before using it
    		push edx
                    xor edx,edx         //Zero Out Registers
    		xor ebx,ebx
    		
    		mov  edx,tt                  // Now Actual Address of Function in edx
    		mov  ebx,[edx]            //Get  4 Bytes From address pointed by Edx into Ebx
    		mov  opcode,ebx         //Move into Opcode
    		pop edx                     //Restore orginal Values
            pop ebx
    	}
    	
    	
    	/*So how to Find End of Function ..Commonly Functions  ends with  instruction
    	
        01291513   |.  8BE5               MOV ESP,EBP
        01291515   |.  5D                 POP EBP                                                   
        01291516   \.  C3                 RETN
    
       I Just Write the Opcodes in Reverse Byte Order and it make 0xC35DE58B
    	
      *One May think  it can RETN X ...Loader Function Normally Needs No Argument...Also by default function uses "cdecl" calling covention 
        so no need  to wrroy  ...mostly it will be RETN   */
    	
    	
    	if(opcode==0xC35DE58B)   	
    	{
    	                        
    		count=count+4;        // When Match Ocurs the C3 will be at end due to reverse byte order ...so add  to counter to get exact loc of C3 
    		break;                //out of Loop when Match Occurs
    	}
    
    	count=count+1;         // count No. of Bytes 
    	tt=tt+1;              //Next Address
    }
    
    So At the End  Count Will contain Size of Function
    Last edited by Arunpreet Singh; 07-15-2012 at 06:05 AM.

  2. The Following 3 Users Say Thank You to Arunpreet Singh For This Useful Post:

    "vinnu" (07-18-2012), 41.w4r10r (07-16-2012), b0nd (07-16-2012)

  3. #2
    Garage Newcomer subrat.sarkar is on a distinguished road
    Join Date
    Aug 2012
    Location
    Bangalore
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    Hi Arunpreet,

    First thing I want to know is why "LP=&Loader " ?
    Second point is with your comment -
    " Here Important thing to Note is When we get a Function Address ...We Do not Get Pointer to Exact Code ..First We have A JUMP which Jumps to orginal code of function (i am using MSVC ,IDE VS2010)" ?
    - So the point is where I am confused is when you have address of function(Loader) then why are you again calculating its address with jump calculation ?

    Thanks for nice post...

    ~Subrat

  4. #3
    Garage Newcomer Arunpreet Singh is on a distinguished road
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    0
    Thanked 5 Times in 3 Posts
    Let me try to Answer it

    1) Why LP=&Loader

    Loader is Name of Function which i want to Copy to diffrent section (ofcourse it contains loader code).

    So idea is to get the address of function ...So to Hold Address of Function and then copy bytes from that address i made a function Pointer LP

    I Declare it as

    int (*LP)(void);

    Hope i am Clear with it ...Yeah you can do it with other ways ...but i find this one more simpler

    2)In MSVC 10 .. this is How the funtion are represented ..Let Say My Function Name is "FunA"
    You must be thinking (or may be not)that when u get the address of FunA .Then You get the a Address of actual Function Code
    But in actual First We have Jump which contains the actual code Address ..thats Why i Take RVA from JMP instruction andd Add with Address of FunA .
    to get real function code ...
    u can check this in ollydbg...make a simple program in VC++ 10 .and check how function are working,..
    I am positng a Screenshot ,,,may be this makes things clear

    Name:  Loader.jpg
Views: 81
Size:  21.1 KB







    For Better Quality Check this

    http://oi50.tinypic.com/30u7evq.jpg
    Last edited by Arunpreet Singh; 08-13-2012 at 11:05 PM.
    "vinnu" likes this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts