Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a code cave, and is there any legitimate use for one?

I encountered this word for the first time in the StackOverflow question "C# Theoretical: Write a JMP to a codecave in asm." I see that according to Wiktionary, a code cave is:

an unused block of memory that someone, typically a software cracker, can use to inject custom programming code to modify the behavior of a program.

Did I find the correct definition? If so, is there any legitimate use for a code cave?

like image 561
Eddie Avatar asked Apr 24 '09 18:04

Eddie


2 Answers

One might wish to intentionally create a code cave as a part of using self-modifying code.

Assuming, of course, that one is insane.

like image 120
chaos Avatar answered Sep 24 '22 16:09

chaos


I've used them, although I'd never heard the term code cave until today. The Wiktionary definition suggests that a code cave is something the cracker finds in the executable he or she is attempting to crack. The question you cite doesn't use it that way. Instead, it suggests the code cave is being allocated with VirtualAllocEx to create a brand new block of memory in the target process. That removes the need to search for unused space in the target, and it guarantees you'll have enough space to put all your new code.

Ultimately, I think a "code cave" is just a place to store run-time-generated code. There doesn't have to be any nefarious purpose to that code. And at that point, the question of what a code cave is becomes entirely uninteresting. The interesting parts are what reasons there are for generating code at run time, and what techniques there are for making sure that new code gets run when you want it.

like image 24
Rob Kennedy Avatar answered Sep 25 '22 16:09

Rob Kennedy