Jump to content

  • Log In with Google      Sign In   
  • Create Account

Problem with loading a DLL through a code cave


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
20 replies to this topic

#1 Falcon22   Members   -  Reputation: 110

Like
0Likes
Like

Posted 29 January 2014 - 02:51 PM

Hello,

 

I am trying to load a certain dll from inside a certain .exe of which I don't have the source code.

So I tried to do it with a code cave which seemed like a good option, but I am experiencing some trouble.

 

This is what I have so far:

 

I first load the .exe in ollydebugger then I select the exe module with ALT + E. 

 

then I make sure I have at least 5 bytes of free space somewhere in the stack, in this case I did that by replacing the following asm commands with NOP's:

004BF23B                                 8BF2             MOV ESI,EDX
004BF23D                                 8BD8             MOV EBX,EAX
004BF23F                                 94               XCHG EAX,ESP

After that I go to the bottom to find some free space and add this code:

0052152A                                    90               NOP
0052152B                                    68 87155200      PUSH some.00521587 ; ASCII "some.dll"
00521530                                    E8 42349B76      CALL kernel32.LoadLibraryA
00521535                                    90               NOP
.....					    8BF2             MOV ESI,EDX
.....         	                            8BD8             MOV EBX,EAX 
.....	                                    94               XCHG EAX,ESP
00521536                                    68 FDF04B00      PUSH <some.codecave.Return>
0052153B                                    C3               RETN

I declared the ASCII somewhere above this code in the stack, also in some free space. The "codecave.Return" label is attached to the command right below the free space I made, so it would normally come after "XCHG EAX, ESP".

 

Finally I replaced the 5 bytes of free space with this command:

004BF23B                                   E9 E7220600      JMP <some.codecave.MyCodeCave>

which makes it jump straight to the beginning of the code you see in the second code window.

 

then I right click and do "copy all modifications to executable", when another window which shows the selection of what I modified appears,  I right click again and do "save" and I enter another name than the original.

 

So when I have the dll in the same folder as the edited executable it still doesnt load it, the executable doesnt even start.

 

I hope someone can tell me what I did wrong.

 

Thanks alot in advance!


Edited by Falcon22, 29 January 2014 - 02:53 PM.


Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 13576

Like
0Likes
Like

Posted 29 January 2014 - 03:26 PM

If you loaded it in a debugger to see why it fails to start it would be helpful in tracking down the error.

 

What stands out to me:

#1: 00521535 and then 00521536?  You moved 5 bytes to address 00521536.  Why aren’t they showing?  68 FDF04B00      PUSH <some.codecave.Return> should be on 0052153B.  Could be an OllyDbg patching error.

#2: Why the PUSH on 00521536 followed by a RETN?  Without seeing what was originally on 004BF240 this could be correct or wrong, but there is a safe way to not care either way: 00521536    JMP 004BF240.  When you inject code, you replace a part with a JMP, add the replaced part to your jumped-to location, and then JMP back to just after the part you replaced.  This is probably your error.

 

 

L. Spiro


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#3 Falcon22   Members   -  Reputation: 110

Like
0Likes
Like

Posted 29 January 2014 - 03:31 PM

Hi,

 

Thanks for the reply,

 

Dont mind the addresses they are not accurate.

I also tried it once with a JMP to the address after my JMP to the code cave and that also didn't work.



#4 ApochPiQ   Moderators   -  Reputation: 15698

Like
1Likes
Like

Posted 29 January 2014 - 04:00 PM

LoadLibrary returns its result in EAX. You're clobbering the register with your injected code. Try PUSHing EAX onto the stack prior to the LoadLibrary call and POPing it back off afterwards.



#5 Nypyren   Crossbones+   -  Reputation: 4293

Like
1Likes
Like

Posted 29 January 2014 - 04:08 PM

I believe Windows calling convention allows EAX, ECX and EDX to be used as volatiles (i.e. the called function will not back them up, and the caller must assume they got clobbered and back them up if they need to be preserved).  You should save all three just in case.



#6 Falcon22   Members   -  Reputation: 110

Like
0Likes
Like

Posted 29 January 2014 - 05:24 PM

@ApochPiQ 

 

Still did the same thing, it just doesnt start. This is what I replaced this time:

004BF1F7                                        00CC             ADD AH,CL
004BF1F9                                        8BC8             MOV ECX,EAX
004BF1FB                                        85C9             TEST ECX,ECX

with this:

004BF1F7                                            E9 38230600      JMP <some.codecave.MyCodeCave>
004BF1FC                                            90               NOP
004BF1FD <codecave.ReturnFromCodeCave>              75 FF            JNZ SHORT some.004BF1FE

and the codecave:

00521534 <codecave.MyCodeCave>                      50               PUSH EAX
00521535                                            68 1B155200      PUSH some.0052151B
0052153A                                            E8 38345874      CALL kernel32.LoadLibraryA
0052153F                                            58               POP EAX
00521540                                            90               NOP
00521541                                            02E1             ADD AH,CL
00521543                                            8BC8             MOV ECX,EAX
00521545                                            85C9             TEST ECX,ECX
00521547                                            90               NOP
00521548                                           ^E9 B0DCF9FF      JMP <some.codecave.ReturnFromCodeCave>

Thanks in advance


Edited by Falcon22, 29 January 2014 - 05:25 PM.


#7 ApochPiQ   Moderators   -  Reputation: 15698

Like
0Likes
Like

Posted 29 January 2014 - 09:14 PM

Are you increasing the size of the .exe file when you modify it?

#8 Falcon22   Members   -  Reputation: 110

Like
0Likes
Like

Posted 30 January 2014 - 04:13 AM

No, the size of the modded program is 412,672 bytes and the original also 412,672 bytes..



#9 incertia   Crossbones+   -  Reputation: 779

Like
0Likes
Like

Posted 30 January 2014 - 10:03 AM

Are you sure the bottom is actually some sort of executable region of code instead of say, the bss section?
what

#10 Falcon22   Members   -  Reputation: 110

Like
0Likes
Like

Posted 30 January 2014 - 10:32 AM

The lines which I refer to as "the bottom" contain this command: 

0052151B   0000             ADD BYTE PTR DS:[EAX],AL


#11 ApochPiQ   Moderators   -  Reputation: 15698

Like
0Likes
Like

Posted 30 January 2014 - 03:47 PM

That's just zeroed bytes. It doesn't mean the memory it corresponds to is actually executable.



#12 Falcon22   Members   -  Reputation: 110

Like
0Likes
Like

Posted 30 January 2014 - 03:52 PM

Alright so where should I place my codecave then?



#13 ApochPiQ   Moderators   -  Reputation: 15698

Like
0Likes
Like

Posted 30 January 2014 - 04:16 PM

It needs to go somewhere where you know it will be loaded by the OS into an executable memory page. We can't tell without the EXE itself where that might be, so you'll have to look at how PE loading works as well as understand the structure of the actual binary you're trying to modify.

 

 

Or you could just do what was suggested earlier, and launch the process under a debugger to see why it won't run.



#14 Falcon22   Members   -  Reputation: 110

Like
0Likes
Like

Posted 30 January 2014 - 04:36 PM

But does the fact that the size of the modded exe isn't different from the original size, already tell me that the part I am using for my code cave isn't loaded by the OS?



#15 Nypyren   Crossbones+   -  Reputation: 4293

Like
0Likes
Like

Posted 30 January 2014 - 04:58 PM

But does the fact that the size of the modded exe isn't different from the original size, already tell me that the part I am using for my code cave isn't loaded by the OS?


When the program is actually loaded into RAM there will be padding at the end of the code section until the next page size boundary (Typically 4096 bytes for a 32-bit process, which it looks like yours is). The linker can optionally leave that same amount of padding in the EXE file itself, but it is not required to.

If you load the EXE in a hexeditor and see a bunch of zeros after the "end" of the code but before the start of the next section listed in the PE headers, that's padding, and you can add instructions there without editing the PE headers.

If there's no padding, then you'll have to rearrange the EXE or scavenge space from some unimportant existing code (perhaps a logging function if you can find any).

Edited by Nypyren, 30 January 2014 - 04:59 PM.


#16 Falcon22   Members   -  Reputation: 110

Like
0Likes
Like

Posted 02 February 2014 - 10:32 AM

Does anyone here know of any good tutorial which explains the process of fixing an executable with a debugger?



#17 Adam_42   Crossbones+   -  Reputation: 2505

Like
0Likes
Like

Posted 02 February 2014 - 02:53 PM

The first thing to do with a debugger is to work out why it's crashing. You can do that with visual studio in the same way you'd debug any other process without symbols.

 

If this is some sort of commercial product you're trying to adjust I wouldn't be surprised if simply noping out a single harmless instruction would stop it loading. They will generally have some kind of built in anti-cheating and copy protection code that go to a lot of effort to make it difficult to do much.



#18 ApochPiQ   Moderators   -  Reputation: 15698

Like
0Likes
Like

Posted 02 February 2014 - 03:11 PM

Perhaps a more useful question is, what are you trying to accomplish in the first place? If all you want is to get your code loaded into another process, there are far simpler and easier ways - look up DLL injection.

#19 Nypyren   Crossbones+   -  Reputation: 4293

Like
0Likes
Like

Posted 02 February 2014 - 03:53 PM

Agreed with Apoch.  It's easier to just CreateProcess(suspended), VirtualAllocEx, WriteProcessMemory and CreateRemoteThread to load a DLL into another process than it is to find space in the existing EXE and patch it.


Edited by Nypyren, 02 February 2014 - 03:58 PM.


#20 Falcon22   Members   -  Reputation: 110

Like
0Likes
Like

Posted 02 February 2014 - 04:14 PM

Thanks alot for the replies guys, I am aware of dll injection and how it works. But I really need to have this dll loaded when the exe, that I am modifying, starts. Without depending on other executables like dll injectors.

 

I think code caving is my best option.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS