Beyond r57

38
Beyond r57 [email protected]

description

Presented at BSides Las Vegas, 2011

Transcript of Beyond r57

Page 1: Beyond r57

Beyond r57

[email protected]

Page 2: Beyond r57

Outline

Page 3: Beyond r57

PHP Background

• It’s terrible

• It’s always been terrible

• Objects are an afterthought

• Function names aren’t consistent

Page 4: Beyond r57
Page 5: Beyond r57
Page 6: Beyond r57
Page 7: Beyond r57

Why bother?

• They have to be uploaded and configured

– Leaves logs, files on disk

• Some call home to the authors

• They all focus on the server

– Maybe the database, too

• Nothing looks beyond, to the network

Page 8: Beyond r57
Page 9: Beyond r57

Rome Wasn’t Sacked in a Day

• Build payloads from simple -> complex

Page 10: Beyond r57

Essence of Payloads

• Create form of communication

• Do your bidding

Page 11: Beyond r57

Simplest: Exec

• Just run a system command

• Don’t care about input/output

• php/exec

Page 12: Beyond r57

Slightly Less Simple: download-exec

• Go grab an executable from a listening webserver

• Save it to disk and run it

• Still don’t care about input/output

• php/download_exec

Page 13: Beyond r57

More Complex: shell

• Need to have a comm channel

• Listen for input and send back output

• php/reverse_tcp

• php/bind_tcp

Page 14: Beyond r57

Meterpreter

• Flexible, extensible, capable

• php/meterpreter/reverse_tcp

• php/meterpreter/bind_tcp

Page 15: Beyond r57

Meterpreter for Pwned Home Pages

• Doesn’t have to be on disk

• Uses the same protocol and extension system

– The existing client works just fine

• Does as much as possible w/o using a shell

– Works in a chroot, doesn’t require /bin/sh

• Platform independent; works anywhere PHP works

Page 16: Beyond r57

Meterpreter Required Reading

• “Beyond EIP” 2006

– skape and spoonm, Blackhat Federal

• “Hacking Macs for Fun and Profit” 2009

– Dino dai Zovi and Charlie Miller, CanSecWest

Page 17: Beyond r57

Why is Meterpreter cool?

• Works even in restrictive environments

• Not limited to installed commands

• If it has more access, it can do cooler stuff

• Programmatically automatable

Page 18: Beyond r57

Meterpreter screenie

Page 19: Beyond r57

Meterpreter

• Flexible extension system

• Uses a (mostly) binary protocol

– TLV (Type, Length, Value)

– Designed for extensibility

Page 20: Beyond r57

Meterpreter Protocol

Length Type Value …..

4 bytes 4 bytes ($length - 8) bytes

Length Type Value …..

Page 21: Beyond r57

Meterpreter Protocol

• Packets are themselves TLVs

• TLVs make parsing simple and flexible

– No formatting knowledge is required outside of the TLV structure

– Allows a core packet parsing engine without any knowledge of extensions or their protocols

Page 22: Beyond r57

Meterpreter Ruby API

• Powerful and flexible scripting capabilities

• Extensions create an attribute under the main client object

• Various bits of info in each extension

– client.sys.config.sysinfo

– client.net.socket.create_tcp_client_channel

Page 23: Beyond r57

Challenges of doing all this in PHP

Page 24: Beyond r57

Liabilities

• Magic Quotes

• Size restrictions

• Safe mode

• disable_functions setting in php.ini

• PHP Quirks

Page 25: Beyond r57

We Don’t Need No Stinking Quotes

Page 26: Beyond r57

Size Restrictions

• URL length is limited to 4000 bytes on Apache

• Total length of an HTTP header value is 8190

• Solution is the same as for other kinds of shellcode: stagers

– Setup some kind of communication with the attacker, read in more code, eval

Page 27: Beyond r57

Safe Mode

• Kind of a bummer for some things

• Restricts files and command exec

• Doesn’t limit sockets in any way

Page 28: Beyond r57

disable_functions setting

• Sucks

• Can try a bunch of different functions with similar purposes until one works

– shell_exec -> passthru -> system -> popen …

• Esser’s memory corruption fu

Page 29: Beyond r57

PHP Quirks

• Stream vs Socket Resources

– stream_select() vs socket_select()

• Operator precedence

– $var & CONST == CONST

– $var & (CONST == CONST)

– $var & 1

• Can’t assume to have > version 4.3

Page 30: Beyond r57

Assets

• Many ways of doing the same thing

– System Commands, Sockets

• Your brains, his strength, my steel.

Page 31: Beyond r57

Running System Commands

• system, exec, popen, pctl_open, shell_exec, passthru, proc_open

• A few non-default extensions: perl, win32std, win32services, almost certainly others

Page 32: Beyond r57

Communications

• Use the webserver

– Simple, effective. Most existing payloads do this

– Leaves logs =(

• Programs on the system: nc, bash, ftp, …

– No guarantee they’ll be there or work

• Sockets

Page 33: Beyond r57

Sockets

• fsockopen, pfsockopen, socket_create, stream_socket, fopen

• Extensions: curl, perl (wtf?)

Page 34: Beyond r57

Files

• fopen is usually enough

– Nobody disables it because it would break everything

Page 35: Beyond r57

Future

• Javaterpreter, JSPterpreter

– Already in the works, written by mihi

• ASPterpreter?

• Macterpreter/POSIX Meterpreter

– Most of the code is there but is not really usable

Page 36: Beyond r57

What should I call it?

• PHP Meterpreter, php-terpreter

• meterphpreter (pronounced “meterfpreter”)

• phpterpreter (pronounced “fapterpreter”)

• phpsucksmyballsterpreter

Page 37: Beyond r57

Demos

Page 38: Beyond r57

Questions

8=====D