44con 2014 - meterpreter internals, oj reeves

93
Meterpreter Internals OJ Reeves - @TheColonial 44con 2014

Upload: 44con

Post on 22-Apr-2015

1.273 views

Category:

Technology


1 download

DESCRIPTION

44CON 2014 - Meterpreter Internals, OJ Reeves Everyone has heard of Metasploit, the Open Source exploitation framework, and most have probably come into contact with it on the attacking and/or receiving end. Meterpreter, Metasploit’s most frequently used payload for Windows systems, enables a tester who has gained control of one machine to perform further exploitation, pivoting and penetration with relative ease. But how does Meterpreter work? What goes on ‘under the hood’ when certain commands are executed? How does it avoid touching the disk and survive happily in memory? How does it hide from the operating system, and how could you locate it if it’s running? Let’s dive into the plumbing that makes Meterpreter tick. I will explain in relative detail its lifecycle, along with some of the ins and outs of topics such as Reflective DLL Injection and Migration. Bring your low-level knowledge and interest in technical details as we pop the hood of one of the most loved parts of Metasploit.

TRANSCRIPT

Page 1: 44CON 2014 - Meterpreter Internals, OJ Reeves

Meterpreter Internals

OJ Reeves - @TheColonial44con 2014

Page 2: 44CON 2014 - Meterpreter Internals, OJ Reeves

GoalsDispel some Meterpreter myths …

… expose the innards …

… encourage you to dive in!

Page 3: 44CON 2014 - Meterpreter Internals, OJ Reeves

Agenda• What is Meterpreter?

o VERY brief overview and history

• What is it made of?o Components, code, communications

• How does migration work?• Questions

Page 4: 44CON 2014 - Meterpreter Internals, OJ Reeves

What is Meterpreter?Shells are great, but we need more …

… enter the “Meta-Interpreter” …

… a payload, RAT, and post-exploitation tool.

Page 5: 44CON 2014 - Meterpreter Internals, OJ Reeves

What is Meterpreter?• Multi-platform

o POSIX, Win32, Win64, Python, PHP, Java,Android … OSX!

• Forensics “friendly”o In memoryo Encrypted communications

• Much more controlo Stacks of commandso Dynamically loadable extensionso Post modules

Page 6: 44CON 2014 - Meterpreter Internals, OJ Reeves

It’s huge!Can’t possibly cover it all …

… implementations very across platforms …

… we’ll focus on Windows x86 Native Meterpreter.

Page 7: 44CON 2014 - Meterpreter Internals, OJ Reeves

What is it made of?Large amounts of C and C++

Page 8: 44CON 2014 - Meterpreter Internals, OJ Reeves

What is it made of?Sprinklings of assembly

Page 9: 44CON 2014 - Meterpreter Internals, OJ Reeves

What is it made of?A good dose of Ruby

Page 10: 44CON 2014 - Meterpreter Internals, OJ Reeves

Sample ScenarioGetting a

Meterpreter session

via reverse_tcp

Page 11: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 12: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 13: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 14: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 15: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 16: 44CON 2014 - Meterpreter Internals, OJ Reeves

ExploitationTarget Machine

(SMB)

445

Attacking Machine (MSF)

Listener - 4444

ms08_067_netapi

BufferRET

Shellcode addr

Page 17: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 18: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 19: 44CON 2014 - Meterpreter Internals, OJ Reeves

Stage Construction

• Load metsrv.x86.dll from disk• Generate a bootstrapper• Patch metsrv:

o Bootstrapper DOS headero Comms config (for http/https)

Page 20: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 21: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 22: 44CON 2014 - Meterpreter Internals, OJ Reeves

Reflective DLL Injection

• Stephen Fewer (legend!)o Harmony Security

• Mini PE loader• No host process registration

o Sorry sysinternals!

• Doesn’t touch disk• Slightly adjusted in MSF

o “Asks” not to paged to disko Extra attach/detach

Page 23: 44CON 2014 - Meterpreter Internals, OJ Reeves

RDI Steps1. Locate the image in memory2. Find helpful libraries/functions

o Needed to do more work

3. Prepare memory for new image4. Process sections5. Process imported libs/functions6. Process relocations7. Call DllMain()

Page 24: 44CON 2014 - Meterpreter Internals, OJ Reeves

RDI WalkthroughTime to look at the guts of

ReflectiveLoader

Here comes the C

Page 25: 44CON 2014 - Meterpreter Internals, OJ Reeves

Step 0

Find the image’s location

Page 26: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 27: 44CON 2014 - Meterpreter Internals, OJ Reeves

Step 1

Find loaded modules andfunction pointers

Page 28: 44CON 2014 - Meterpreter Internals, OJ Reeves

… to be continued …

Page 29: 44CON 2014 - Meterpreter Internals, OJ Reeves

… to be continued …

Page 30: 44CON 2014 - Meterpreter Internals, OJ Reeves

Why hash?• Can’t put strings in PIC

o We don’t know where we are, we don’twhere the strings are either

• Strings bloat payload sizeo Not as important here, but it is elsewhere

• Contain NULLso Not important here, but important elsewhere

• Consistent with block_api (later)

Page 31: 44CON 2014 - Meterpreter Internals, OJ Reeves

… to be continued …

Page 32: 44CON 2014 - Meterpreter Internals, OJ Reeves

… etc …

Page 33: 44CON 2014 - Meterpreter Internals, OJ Reeves

Step 2

Prepare a new memory locationto host the image

Page 34: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 35: 44CON 2014 - Meterpreter Internals, OJ Reeves

Step 3

Copy and prepare sections

Page 36: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 37: 44CON 2014 - Meterpreter Internals, OJ Reeves

Step 4

Manually wire up thefunction imports

Page 38: 44CON 2014 - Meterpreter Internals, OJ Reeves

… to be continued …

Page 39: 44CON 2014 - Meterpreter Internals, OJ Reeves

… to be continued …

Page 40: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 41: 44CON 2014 - Meterpreter Internals, OJ Reeves

Step 5

Handle the lack of PIC supportand support relocations

Page 42: 44CON 2014 - Meterpreter Internals, OJ Reeves

Relocation• For each relocation block entry …• … for each relocation entry in the block …• … figure out the relocation offset …• … patch in the library address value:

o Add DWORDo Add HIWORDo Add LOWORD

Page 43: 44CON 2014 - Meterpreter Internals, OJ Reeves

Step 6

Finally… DllMain!

Page 44: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 45: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 46: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 47: 44CON 2014 - Meterpreter Internals, OJ Reeves

Metsrv StartupFinally… DllMain!

• Server thread created• Comms taken over & encrypted• Scheduler initialised• Dispatch loop executes

Page 48: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 49: 44CON 2014 - Meterpreter Internals, OJ Reeves

Not quite!

But we’re really close!

Metsrv is running, but wehave no commands!

Page 50: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 51: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 52: 44CON 2014 - Meterpreter Internals, OJ Reeves

stdapi and priv• Extensions to meterpreter• Stdapi provides the “guts”

o Execution, shells, uploads/downloads, etc

• Priv gives us the ability to elevateo Getsystem

• Both immediately uploaded & reflectively loaded

Page 53: 44CON 2014 - Meterpreter Internals, OJ Reeves

Command Definition

Page 54: 44CON 2014 - Meterpreter Internals, OJ Reeves

Command Registration

Page 55: 44CON 2014 - Meterpreter Internals, OJ Reeves

ExploitationTarget Machine

(SMB)

445

Attacking Machine (MSF)

Listener - 4444

ms08_067_netapi

BufferRET

Shellcode addr

privstdapimetsrvpriv

stdapi

metsrvmetsrv

stdapi

priv

mimikatzkiwi

incognito

sniffer

Page 56: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 57: 44CON 2014 - Meterpreter Internals, OJ Reeves

Yes!

We have a fully functionalMeterpreter session!

Page 58: 44CON 2014 - Meterpreter Internals, OJ Reeves

How does it feel?

http://securityreactions.tumblr.com/post/93792005074/how-i-felt-when-i-got-my-first-meterpreter-session

Page 59: 44CON 2014 - Meterpreter Internals, OJ Reeves

Migration• My favourite feature• “Jumping” across process boundaries• Doesn’t drop connectivity• Helps avoid process that:

o Are likely to crasho Are likely to be closed

• Helps maintain sessions!

Page 60: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 61: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 62: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 63: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 64: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 65: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 66: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 67: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 68: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 69: 44CON 2014 - Meterpreter Internals, OJ Reeves

Migration in Metasploit

1. Check process exists, isn’t “me” and we have permissions to touch

2. Get target process architecture3. Generate a new migration payload4. Send command to Meterpreter5. Wait for migration to finish6. Reload previously loaded extensions

Page 70: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 71: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 72: 44CON 2014 - Meterpreter Internals, OJ Reeves

… but what’s a TLV?

Page 73: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 74: 44CON 2014 - Meterpreter Internals, OJ Reeves

Type, Length, Value• Type – actually both type and identifier

o String, integer, binary, etco ID which says “which integer” (eg. PID)

• Length – size of the datao Integer – 4 byteso String – ASCII string length

• Value – the data itselfo Byte blog of “Length” bytes

• Packet = Header + TLV + TLV + TLV …

Page 75: 44CON 2014 - Meterpreter Internals, OJ Reeves

Migration TLVs

Page 76: 44CON 2014 - Meterpreter Internals, OJ Reeves

Back to Ruby …

Page 77: 44CON 2014 - Meterpreter Internals, OJ Reeves

Migration in Meterpeter

1. Read all the data from the TLVs2. Create synchronisation primitive3. Prepare the target process memory4. Hand over control

o Thread creation/hijacking and RDI

5. Shut down current Meterpreter

Page 78: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 79: 44CON 2014 - Meterpreter Internals, OJ Reeves

Migrate Context

Force 8-byte size

Used for synchronisation

Pointer to metsrv payload

Duplicated socket info

Page 80: 44CON 2014 - Meterpreter Internals, OJ Reeves

Migration in Meterpreter

Page 81: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 82: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 83: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 84: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 85: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 86: 44CON 2014 - Meterpreter Internals, OJ Reeves

Migrated Payload Exec

Page 87: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 88: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 89: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 90: 44CON 2014 - Meterpreter Internals, OJ Reeves
Page 91: 44CON 2014 - Meterpreter Internals, OJ Reeves

Migration Completes!

• The RDI stub is invoked• Metsrv is reflectively loaded• The rest is history …

Page 92: 44CON 2014 - Meterpreter Internals, OJ Reeves

The “links” Slide• https://github.com/rapid7/meterpreter• https://github.com/rapid7/metasploit-

framework• http://buffered.io/• #metasploit on Freenode• http://rapid7.com/ (No, I don’t work for them!)• http://beyondbinary.io/

I look forward to your PRs!

Page 93: 44CON 2014 - Meterpreter Internals, OJ Reeves

Thank you!

OJ Reeves - @TheColonial44con 2014