Click here to download this article with executable program, shellcode file
Source: Moeomu’s blog
Variable code
Caution
- When picking encoding byte, it can’t be the same as existing byte, otherwise there will be 0
- It is possible to encode different areas with multiple different encoding bytes, but it will increase the complexity
- Multiple rounds of encoding of shellcode are possible
Implementation code (ExpStd0401)
|
|
Decoding code (ExpStd0402)
- Decoder is executed jointly with shellcode
- Default EAX is aligned to the shellcode start position at the beginning of the shellcode
- The last byte of shellcode is 0x90
|
|
ShellCode to reduce the size
Methods
- Pick short instruction
xchg eax, reg
;swap the values ofeax
and other registerslodsb
;load adword
pointed to byesi
intoeax
and addesi
lodsd
;load abyte
pointed to byesi
intoal
and incrementesi
stosd
;copy the contents ofeax
to the memory address ofedi
, adding0x4
toedi
for every four bytes copied, andecx
for the sizestosb
; copy the content ofeax
to the memory address ofedi
, for every byte copied,edi
adds0x4
,ecx
is the sizepushad/popad
;store/restore all register values from the stackcdq
;useedx
to expandeax
into four words, can be used asmov edx, 0
wheneax<0x80000000
- Compound instructions, combined use instructions
- API parameter stacking before a piece of the stack space to 0, the stack can be pressed into the non-0 parameters
- Code is used as data, data is used as code
- If the data on top of the stack is useful, raise the top of the stack to protect it for later use
- Some registers are always stored on the stack when the API is called, but most functions do not use EBP when they run, so you can use EBP to store data.
- HASH algorithm for storing APIs
Select the appropriate HASH algorithm
- 8bit represents up to 256 different characters, there will inevitably be collisions, but if the desired function is located first in the collision, then it can be used
- i.e. collisions are partially tolerable