Thread: Patching doesn't work?

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    60

    Question Patching doesn't work?

    Hi there

    I'm currently doing practices under Linux w/ blah crackmes:
    Link: crackmes.de

    This is the dump result:

    Code:
    blah:     file format elf32-i386
    
    
    Disassembly of section .text:
    
    08048094 <.text>:
     8048094:	31 c0                	xor    %eax,%eax
     8048096:	b8 2f 00 00 00       	mov    $0x2f,%eax
     804809b:	cd 80                	int    $0x80
     804809d:	3d ad de 00 00       	cmp    $0xdead,%eax
     80480a2:	75 16                	jne    0x80480ba <-- I want to NOP this line
     80480a4:	b8 04 00 00 00       	mov    $0x4,%eax
     80480a9:	bb 01 00 00 00       	mov    $0x1,%ebx
     80480ae:	b9 c4 90 04 08       	mov    $0x80490c4,%ecx
     80480b3:	ba 06 00 00 00       	mov    $0x6,%edx
     80480b8:	cd 80                	int    $0x80
     80480ba:	31 c0                	xor    %eax,%eax
     80480bc:	40                   	inc    %eax
     80480bd:	31 db                	xor    %ebx,%ebx
     80480bf:	cd 80                	int    $0x80
    As I comment above, I want to patch that line by 2 - NOP bytes.
    I wrote this code:

    Code:
    #include <stdio.h>
    
    int
    main( int argc, char *argv[] )
    {
    	int offset[2] = { 0x75, 0x16 }; /* origin */
    	char patch[2] = { 0x90, 0x90 }; /* nop */
    	FILE *file;
    	int i;
    	
    	file = fopen( "blah", "rb+" );
    	if( file != NULL ) {
    		for( i = 0; i < 2; ++i ) {
    			fseek( file, offset[i], SEEK_SET ); /* search */
    			fprintf( file, "%c", patch[i] ); /* patch */			
    		}
    		printf("Patched Done.\n");
    	} else {
    		printf("[Error]: file not found. \n");
    	}		
    	fclose( file );
    	return 0;
    }
    However, I don't know why it doesn't work???
    [Note: if I change different offsets, source works; only the above offset occurs error]
    May some1 help me this?

  2. #2
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    [Note: if I change different offsets, source works; only the above offset occurs error]
    Sounds mysteriously. Maybe it's all because you are using hex representation as offset? Seems like you want to patch 117th and 22th bytes.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    60
    @GL.Sam: event I change to decimal, it stays the same. =_+

  4. #4
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    Sorry, I've expressed myself improperly. I meant that you are trying to patch 117th and 22th bytes, counting from the beginning. 75 16 is not the offset, it is what actually stands for your jne command. fseek's second argument is not the first occurence of a value, it is address from the beginning. You should probably open the file in hex editor and look for actual offset, or organize a loop with checking for what you want and use fwrite() for patching two bytes.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Try using fwrite intead of fprintf:

    Code:
          fwrite( &patch[i], 1, 1,  file);
    Last edited by Scarlet7; 08-05-2009 at 07:18 AM.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    60
    ohoh...my bad )
    I'm crazyyyyy

    this is my solution anyways, for any1 who want to reference.

    Code:
    #include <stdio.h>
    
    int
    main( int argc, char *argv[] )
    {
    	FILE *file;
    	const char *file_name 	  = "blah";
    	const char *file_mode 	  = "rb+";
    	const int	patch_offset  = 0xa2;
    	const char	patch_value[] = { 0x90, 0x90 };
    	
    	file = fopen( file_name, file_mode );
    	if( file != NULL ) {		
    		fseek( file, patch_offset, SEEK_SET ); /* search */
    		fwrite( patch_value, sizeof( char ), 2, file );	
    		printf("Patched Done.\n");
    	} else {
    		printf("[Error]: file not found. \n");
    	}		
    	
    	fclose( file );
    	return 0;
    }
    Thanks guys !

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    You have another problem. Where you have
    Code:
    	if( file != NULL ) {		
    		fseek( file, patch_offset, SEEK_SET ); /* search */
    		fwrite( patch_value, sizeof( char ), 2, file );	
    		printf("Patched Done.\n");
    	} else {
    		printf("[Error]: file not found. \n");
    	}		
    	
    	fclose( file );
    I think that fclose would seg fault, though it has been so long since I've worked with files I don't remember what happens when one closes an unopened file handle.

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by Kennedy View Post
    You have another problem. Where you have
    Code:
    	if( file != NULL ) {		
    		fseek( file, patch_offset, SEEK_SET ); /* search */
    		fwrite( patch_value, sizeof( char ), 2, file );	
    		printf("Patched Done.\n");
    	} else {
    		printf("[Error]: file not found. \n");
    	}		
    	
    	fclose( file );
    I think that fclose would seg fault, though it has been so long since I've worked with files I don't remember what happens when one closes an unopened file handle.

    This should satisfy the fclose()

    Code:
       file = fopen( file_name, file_mode );
    Is that not enough?

  9. #9
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    Uh, yeah, when it ends up to fclose(NULL) you are getting totally screwed.

    Should be moved to be after the first printf.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  10. #10
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by GL.Sam View Post
    Uh, yeah, when it ends up to fclose(NULL) you are getting totally screwed.

    Should be moved to be after the first printf.

    Oh, yes very true!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM