• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Falcon22

Problem with loading a DLL through a code cave

20 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

@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
0

Share this post


Link to post
Share on other sites
Are you sure the bottom is actually some sort of executable region of code instead of say, the bss section?
0

Share this post


Link to post
Share on other sites

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

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

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0