Module 07b JMP
Module 07b JMP
Exploitation
SEC-300-01/CSI-301-02
Ali Hadi
@binaryz0ne
Jumping Strategies…
www.ashemery.com 3
JMP (or CALL) Cited [1]
www.ashemery.com 4
POP RET Cited [1]
pop return
• If none of the registers point directly to the shellcode, but you
can see an address on the stack (first, second, … address on
the stack) that points to the shellcode
• You can load that value into EIP by first putting a pointer to
pop ret, or pop pop ret, or pop pop pop ret (all depending on
the location of where the address is found on the stack) into
EIP.
www.ashemery.com 5
PUSH RET Cited [1]
push return
• this method is only slightly different than the “call register”
technique
• If you cannot find a <jump register> or <call register> opcode
anywhere, you could simply put the address on the stack and
then do a ret
• So you basically try to find a push <register>, followed by a ret
• Find the opcode for this sequence, find an address that
performs this sequence, and overwrite EIP with this address
www.ashemery.com 6
JMP [reg + offset] Cited [1]
www.ashemery.com 7
Blind Return Cited [1]
blind return
• We know that ESP points to the current stack position (by
definition)
• A RET instruction will ‘pop’ the last value (4bytes) from the
stack and will put that address in ESP
• So if you overwrite EIP with the address that will perform a
RET instruction, you will load the value stored at ESP into EIP
• If you are faced with the fact that the available space in the
buffer (after the EIP overwrite) is limited, but you have plenty
of space before overwriting EIP, then you could use jump code
in the smaller buffer to jump to the main shellcode in the first
part of the buffer
www.ashemery.com 8
SEH Cited [1]
www.ashemery.com 9
SEH – Cont. Cited [1]
www.ashemery.com 10
POPAD Cited [1]
popad (pop all double) will pop double words from the stack
(ESP) into the general-purpose registers, in one action (opcode
= 0x61)
• Loaded order: EDI, ESI, EBP, EBX, EDX, ECX and EAX
• As a result, the ESP register is incremented after each register
is loaded (triggered by the popad)
• One popad will thus take 32 bytes from ESP and pops them in
the registers in an orderly fashion
• So suppose you need to jump 40 bytes, and you only have a
couple of bytes to make the jump, you can issue 2 popad’s to
point ESP to the shellcode
– Don’t forget to place NOPs at the beginning
www.ashemery.com 11
Short Jumps Cited [1]
• In the event you need to jump over just a few bytes, then you
can use a couple ‘short jump’ technique to accomplish this
• Short jump (jmp) opcode is 0xeb
• All you need to do is jmp followed by the number of bytes
• So if you want to jump 30 bytes, the opcode is 0xeb,0x1e
www.ashemery.com 12
Conditional Jumps Cited [1]
www.ashemery.com 13
Conditional Jumps – Cont. Cited [1]
Example:
• Suppose you want to jump 6 bytes
• Have a look at the flags, and depending on the flag status, you
can use one of the opcodes below
• Let’s say the Zero flag is 1, then you can use opcode 0x74,
followed by the number of bytes you want to jump (0x06 for
this case)
www.ashemery.com 14
Backward Jumps Cited [1]
www.ashemery.com 15
Backward Jumps: Example #1
You want to jump backwards 7 bytes
• Assembly instruction is: JMP -7
• OPCode/bytecode (5 bytes): E9F4FFFFFF
• Result would be: "\xE9\xF4\xFF\xFF\xFF“
www.ashemery.com 16
Backward Jumps: Example #2
But what if you only want to jump to a near location with less
bytes? Then you can use the following syntax:
• Assembly instruction is: JMP short -7
• OPCode/bytecode (2 bytes): EBF7
• Results would be: "\xEB\xF7“
– When using JMP this way, you can only jump backwards and forwards
in a limited number of bytes. That is why sometimes even if you type
JMP -400 in NASM, it will be converted to a long or far distance jump
as if you typed JMP LONG -400.
www.ashemery.com 17
Backward Jumps: Example #3
I guess you know this by now! Jumping backwards 400 bytes:
• You cannot do this with a short jump, so you will either type:
JMP -400
Or
JMP LONG -400
• Result = "\xE9\x6B\xFE\xFF\xFF”
www.ashemery.com 18
Weird Relative Backward Jump Cited [1]
"\x59\xFE\xCD\xFE\xCD\xFE\xCD\xFF\xE1\xE8\xF2\xFF\xFF\xFF“
• Explanation
\x59 POP ECX
\xFE\xCD DEC CH
\xFE\xCD DEC CH
\xFE\xCD DEC CH
\xFF\xE1 JMP ECX
\xE8\xF2\xFF\xFF\xFF CALL [relative -0D]
• Could be adjusted to fit your needs
www.ashemery.com 19
Summary
• Explained different jumping strategies
www.ashemery.com 20
References
• Peter “Corelanc0d3r”, Exploit Writing (Jumping to
Shellcode), http://www.corelan.be/,
• Memory Corruption 101, NYU Poly, Dino Dai Zovi
• Grayhat Hacking: The Ethical Hacker’s Handbook, 3rd Edition
• The Shellcoders Handbook
www.ashemery.com 21