I've found a few related posts about this on the site but non of them explained my problem. The code is described here: Link
My issue is the following:
The author tries to rewrite the return address of the strcpy() function in the notesearch.c program with one in the buffer so that the shellcode (which I understand is implanted machine code in the memory) will be executed.
For that to work he creates a buffer that looks like: nop nop nop ... | shell code .. | return address
From what I understand after the call to the strcpy() function the buffer is put on the stack and overwrites the return value to notesearch.c's main. He does that so when the strcpy() finishes running and wants to return to the main he'll call the return value from the buffer.
What I don't understand are the following:
It seems to me like the injected return address is always perfectly aligned with the original. How can that be?
It said in the book that the injected return address points to the shellcode in the same buffer. Why would we want to put the shellcode in the same buffer and not in another part of the memory? Couldn't that cause an infinite loop?
Why can't we set the return address to that of the shellcode on the buffer? Or to the variable shellcode for that matter?
Can anyone elaborate on how the return address was calculated and why we need the offset?