Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte...

27
By Alon Weinberg Please inject-me, a x64 code injection August 2019

Transcript of Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte...

Page 1: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

By Alon Weinberg

Please inject-me, a x64 code injection

August 2019

Page 2: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

I’m a Security researcher!I’ve been working at Deep Instinct, Since 2017And I was in the IDF Cyber Unit for 4.5 years

Alon Weinberg

Page 3: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

Please inject-mea x64 code injection

Intro

▪ Code injection and its importance

▪ Introducing Inject-Me

Technical background

▪ ReadProcessMemory▪ X64 WinAPI calling

convention

Inject-Me - Detailed flow

▪ Abusing ReadProcessMemory

▪ Copying data on the target

process▪ Finalizing the injection

▪ Infinite running thread

▪ Execution▪ Demo

4

Page 4: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

IntroPlease inject-me, a x64 code injection

5

Page 5: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

Code injection is the general term of introducing (or "injecting") code into a process

and executing it from the process context.

Injecting process target process

Inject code

MessageBoxcode

Execute code

What is code injection

6

Page 6: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

Why is code injectionimportant?

Benign use of code injection

▪ Security solutions

▪ Adding functionality

▪ Monitoring, Analysis and Research

7

Malicious use of code injection:

▪ Stealth - Hiding malware presence

▪ Evasion - Bypassing security solutions

▪ Stealing information from another process

Page 7: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

Introducing inject-me

▪ How it all started

▪ A new code injection for x64

▪ The idea behind Inject-Me

▪ “Injection-less” code injection

8

Page 8: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

Technical backgroundPlease inject-me, a x64 code injection

9

Page 9: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

▪ Reads memory from a process

By running the function remotely in a target process, and controlling the parameters passed using SetThreadContext one can read\inject a shellcode into the target process.

ReadProcessMemory function

10

Page 10: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

▪ Integer arguments passed in registers RCX, RDX, R8, and R9

▪ Arguments after the fourth argument passed on the stack

▪ Function can be set with four or less arguments remotely using SetThreadContext

hProcess

lpBaseAddress

lpBuffer

nSize

lpNumberOfBytesRead

ReadProcessMemory

RCX =

RDX =

R8 =

R9 =

RtlExitUserThread

0x00000000

0x00000000

0x00000000

0x00000000

Stack

lpNumberOfBytesRead

Return Address=

Ignored

Ignored

Ignored

Ignored

On stack

X64 WinAPI calling convention

11

Page 11: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

Details and flow of the Injection-less code injection

Setting up ReadProcessMemory

First problemAccess violation

Creating an infinite running thread

Executing code injection

12

Page 12: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

▪ ReadProcessMemory gets 5 arguments

▪ Only 4 arguments can be passed through registers

▪ Fifth parameter can be NULL

▪ Creating a dummy stack - VirtualAllocExallocates memory in a process and zeroes it

▪ Dummy stack will be used later as the stack when calling ReadProcessMemory

Setting up ReadProcessMemory

First problemAccess violation

Creating an infinite running thread

Executing code injection

Setting up ReadProcessMemoryfor abuse

13

Page 13: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

▪ Using DuplicateHandle to duplicate injecting process handle to the target process

▪ Setting hProcess to Injecting process duplicated handle

▪ Allocating memory for the shellcode in the target process using VirtualAllocEx

Setting up ReadProcessMemoryfor abuse

14

Setting up ReadProcessMemory

First problemAccess violation

Creating an infinite running thread

Executing code injection

Page 14: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

Access violation return address is 0

0x00000000

Process stackProcess flow

0x00000000

0x00000000

0x00000000

0x00000000

Call ReadProcessMemory

Memory is read to buffer

Return to address on stack

15

Setting up ReadProcessMemory

First problemAccess violation

Creating an infinite running thread

Executing code injection

Page 15: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

Access violation return address is RtlExitUserThread

0x00000000

Process stackProcess flow

0x00000000

0x00000000

RtlExitUserThread

0x00000000

Call ReadProcessMemory

Memory is read to buffer

Return to address on stack

16

Setting up ReadProcessMemory

First problemAccess violation

Creating an infinite running thread

Executing code injection

Page 16: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

Copying RtlExitUserThreadto the dummy stack

▪ Kernel32.dll imports RtlExitUserThread from ntdll.dll

▪ RtlExitUserThread address should exist in kernel32.dll IAT (Import Address Table)

▪ kernel32.dll base address and IAT address are identical between processes

▪ Finding RtlExitUserThread in injecting process and copying it in the target process

17

Setting up ReadProcessMemory

First problemAccess violation

Creating an infinite running thread

Executing code injection

Page 17: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

▪ NtQueueApcThread calls a function in a process and passes 3 parameters to it

▪ RtlCopyMemory gets 3 parameters

▪ Copying data using NtQueueApcThread and RtlCopyMemory

How to copy data on the target process

Destination

Source

Length

Injecting process

NtQueueApcThread

RtlCopyMemory

Target process thread handle Destination

Source

Length

Target process thread

RtlCopyMemory

18

Page 18: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

Side note –Recreating shellcode in a target process

▪ The method described earlier can be used to recreate a shellcode in the target process:

▪ Finding each byte of the shellcode in the target process

▪ Copying the shellcode byte by byte in the target process

▪ We’ve found a way to recreate shellcode in a target process!

19

Page 19: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

Finalizing the code injectionPlease inject-me, a x64 code injection

20

Page 20: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

An Infinite running thread is neededDescribing a new problem

▪ Set RIP register to ReadProcessMemory

▪ Really?

▪ Setting the RIP register of a thread created suspended causes exception

▪ Exception 0xC000000D, STATUS_INVALID_PARAMETER

▪ The thread needs to initialize before it is manipulated

▪ A thread created in the target process will terminate before it can be manipulated

▪ Running an infinitely running thread will allow it to initialize

21

Setting up ReadProcessMemory

First problemAccess violation

Creating an infinite running thread

Executing code injection

Page 21: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

An Infinite running thread is neededLooking at RtlUserThreadStart

22

Page 22: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

An Infinite running thread is neededLooking at RtlUserThreadStart

23

Page 23: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

An Infinite running thread is neededRunning the infinitely running thread

▪ Allocating RWX memory for jmp RBX opcode using VirtualAllocEx

▪ Looking for jump RBX opcode in our version of ntdll (opcode: 0xffe3)

▪ Copying jump RBX opcode in the target process using method described earlier

▪ Creating suspended thread using CreateRemoteThreadfunction starting at jmp RBX opcode

▪ Setting RBX to point to jmp RBX opcode using SetThreadContext

▪ Resuming the thread

24

Setting up ReadProcessMemory

First problemAccess violation

Creating an infinite running thread

Executing code injection

Page 24: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

Executing the code injection

▪ Suspend the thread and check if RIP is at jmp RBX opcode address

▪ Setting the thread context using SetThreadContext

▪ Resuming the thread and waiting for the injection to occur

▪ Using WaitForSingleObject to wait until the thread is done

▪ Executing the shellcode!

25

Setting up ReadProcessMemory

First problemAccess violation

Creating an infinite running thread

Executing code injection

Page 25: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

DemoPlease inject-me, a x64 code injection

27

Page 26: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

28

Page 27: Please inject-me, a x64 code injection - DEF CON CON 27/DEF CON 27... · Copying the shellcode byte by byte in the target process We’ve found a way to recreate shellcode in a target

Thank you!For the full research paper visit this link | http://bit.ly/MeX64

29