Log in

View Full Version : Changing text constants


madiyaan
December 17th, 2009, 19:06
I have a binary that refers to a text string, for example:

MOV DWORD PTR SS:[ESP], 0x1000000 ; ASCII "Bla"
CALL MY_PRINTF ; Prints whatever was written to ESP before this instruction

MY_PRINTF might print the string whose pointer gets written to the top of stack (this is just a made up example)

I want to change this so that it prints a much longer string (say, "Hello World". The problem is that the bytes of "Bla" are located in relocatable segment of the executable and are immediately followed by another constant string that I don't want to trample over.

Basically I want to add a custom (perhaps long) text string to the constant section of the binary and add the address of that to the instruction before printf. The address should be such that when the loader patches the exe after relocating the text section, it should grab the correct value.

Can anyone help me with this? I haven't been able to find an online resource/tutorial that describes adding a new text (I know how to change instructions or add NOPs, etc., but don't know how to add relocatable strings to the binary).

Thanks in advance,

FrankRizzo
December 17th, 2009, 21:33
Well, if there's room in that section, just add, or change a string that you DON'T need, and adjust the value that gets pushed onto the stack before PRINTF is called. If there is no room to do it, it's POSSIBLE, but much more involved.

madiyaan
December 17th, 2009, 21:56
OK. I actually got it working by adding to the end of the section and changing the pointer that is pushed. I guess Olly automatically makes it such that the code is relocatable (what I am saying is that the value I modify for the push parameter might have to be different if that section is loaded in a different area of memory, and I was asking for advice on how to make it such that it survives the loader fix-up. Currently it seems like it is working for my executable without the fix-up).

Another question I have is the following: what if I need to add CODE to a place in Olly but I have no space. For example, between the PUSH and the CALL, what if I wanted to add 10-20 more instructions. Is that possible? Can you point me to a tutorial that does that?

Thanks,

FrankRizzo
December 17th, 2009, 22:40
Yes, my favorite way to do that, is to find a "code cave" (a search will do wonders for you with that search item), and in the mainline code, jump to the code cave, do what you need to do, and then jump back. (Don't forget to include the instructions that you wrote over with the jump!).