post exploit goodness on a mainframe - pwn2own exploit goodness... · 2018-03-29 · post exploit...

60
Shuffling zOS memory blocks Ayoub ELAASSAL @ayoul3__ Github.com/ayoul3 Post exploit goodness on a Mainframe SPECIAL is the new root

Upload: others

Post on 05-Apr-2020

28 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Ayoub ELAASSAL

@ayoul3__

Github.com/ayoul3

Post exploit goodness on a Mainframe

SPECIAL is the new root

Page 2: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

What I picture when talking about Mainframes

Page 3: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

What people picture when I talk about Mainframes

Page 4: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

In 2017…IBM z14

• 32 TB of RAM

• 170 processors,5.2 GHz

• Encryption at a rate of 312 Go/second

• Dedicated processors for JAVA, XML and

UNIX

• Dedicated processors for I/O

Page 5: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Page 6: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

6

Page 7: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

About me

Pentester at PwC France, mainly hacking Windows and Unix stuff

First got my hands on a mainframe in 2014…Hooked ever since

When not hacking stuff: Metal and wine

• github.com/ayoul3

• ayoul3__

Page 8: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Why we should care about mainframes

Quick recap on how to execute code on z/OS

Playing with z/OS internals

This talk

Page 9: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

The main protocole to interact with a Mainframe is

called TN3270

TN3270 is simply a rebranded Telnet

…Clear text by default

X3270 emulator if

you don’t have the

real thing

The wonders of TN3270

Page 10: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Ettercap dissector by @Mainframed767

ETTERCAP DEMO

Page 11: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Sniffing credentials

Good ol’ bruteforce

Go through the middleware

And many more (FTP, NJE, etc.)

Quick recap on how to execute code on z/OS

Page 12: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocksTsk tsk tsk… too friendly!

TSO is the /bin/bash on z/OS

Time Sharing Option (TSO)

Page 13: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Nmap script by @Mainframed767

https://nmap.org/nsedoc/scripts/tso-enum.html

Bruteforcing TSO

TSO: the command line interpreter

Page 14: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Bruteforce is still surprisingly effective

Passwords derived from the login name*

Windows : 5% Mainframe : 27%

*Stats of cracking ~1000 accounts on Windows vs Mainframe

in the same engagement

Page 15: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Sniffing credentials

Good ol’ bruteforce

Go through the middleware

And many more (FTP, NJE, etc.)

Quick recap on how to execute code on z/OS

Page 16: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Page 17: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Page 18: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Page 19: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

CICS is a combination Drupal and Apache Tomcat… before

it was cool (around 1968)

Current version is CICS TS 5.4

Most interactive applications on z/OS rely on a middleware

called CICS

Interactive apps

Page 20: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

If we manage to “exit” the application, we can instruct CICS to

execute default admin programs (CECI, CEMT, etc.) => rarely

secured

As usual, some API functions are particularly interesting!

CECI offers to execute API functions

CICS: a middleware full of secrets

Page 21: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

CICS ESCAPE DEMO

Page 22: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

https://github.com/ayoul3/cicspwn/https://github.com/ayoul3/cicspwn

CICSPWN DEMO

Page 23: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Sniffing credentials

Good ol’ bruteforce

Go through the middleware

And many more (FTP, NJE, etc.)

Check out @Mainframed767, @BigEndianSmalls & @singe’s talks!

Quick recap on how to execute code on z/OS

Page 24: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

There are three main security attributes on RACF :

• Special : can alter RACF rules and access any resource

• Operations : access all files unless being forbidden from doing so

• Auditor : access audit trails and manage logging classes

The most widespread security product on z/OS is RACF. It

performs authentication, access control, etc.

Shell on z/OS, now what?

Page 25: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

LISTUSER command on TSO

TSO: command line interpreter

RACF: security product. Enforces ACL and authentication

Page 26: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Why we should care about mainframes

Quick recap on how to execute code on z/OS

Playing with z/OS internals

Page 27: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Z architecture

Each instruction has many variants: memory-memory, memory-

register, register-register, register-immediate, etc.

16 general purpose registers (0 – 0xF) (+ other registers)

The PSW register holds control flags and the address of the

next instruction

Proprietary CPU (CISC – Big Endian)

Page 28: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Security context in memory

z/OS, like any other OS, relies on control blocks: data structures

describing the current state of the system

RACF stores the current user’s privileges in the ACEE control

block

RACF: security product. Enforces ACL and authentication

Page 29: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Security context in memory

PSA

PSAAOLD

0

548

Always

starts at

virtu. addr

0

ASCB

Address Space

Control Block

ASCBASXB108

200

ASXB

Address Space

Extension Block

ASXBSENV

ACEE

USER FLAGS

0... .... SPECIAL

..0. .... OPERATIONS

…0 .... AUDITOR

38

Page 30: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Patch ACEE bit 38 (x26) in memory

Attack plot

Page 31: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Program State Word (PSW)

ABEND S0C4, code 4: Protection exception.

Page 32: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Memory protection

Each page frame (4k) is allocated a 4-bit Storage key + Fetch

Protection bit at the CPU level

16 possible Storage key values

0 – 7 : system and middleware. 0 is the master key

8 : mostly for users

9 – 15 : used by programs that require virtual = real memory

The storage key of a memory page is compared with the storage

key in the PSW register

PSW: register holding next instruction address and control flags describing system state

Page 33: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Program State Word (PSW)

8 - 11 bit : current storage key, 8 in this case

Next instructionControl flags

Page 34: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Storage keys matchStorage don't match

& Fetch bit ON

Storage don't match

& Fetch bit OFF

PSW key is zero Full Full Full

PSW key is not zero Full None Read

Memory protection

Page 35: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Patch ACEE bit 38 (x26) in memory

Attack plot

Switch storage key in PSW : MODESET macro

• ACEE: data structure holding current privileges of a user/task

• PSW: register holding next instruction address and control

flags describing system state

Page 36: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Some instructions are only available in Supervisor state (kernel

mode) :

• Cross memory operations

• Direct Storage Access

• Changing storage keys

• Exit routines

• Listening/editing/filtering system events

• Etc.

Problem State Vs Supervisor State

Page 37: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

APF libraries are extensions of the zOS kernel

How do we get into Supervisor state

Any program present in an APF library can request supervisor

mode

Obviously…these libraries are very well protected ! (irony)

Page 38: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Patch ACEE bit 38 (x26) in memory

Attack plot

Switch storage key in PSW : MODESET macro

Find APF library with ALTER access

• ACEE: data structure holding current privileges of a user/task

• PSW: register holding next instruction address and control

flags describing system state

Page 39: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Hunting APF on z/OS... Diving into virtual memory

PSA

FLCCVT

0

16

Always

starts at

virtu. addr

0

CVT

EAECVT

References

all major

structures

140 ECVT

Extended CVT

ECVTCSVT228

CSVT

Content

Supervisor

Table

APFA12

APFA

APF

8

12FIRST

LAST

APF

APF

APF

CSVAPF command on z/OS 2.1 and higher

Page 40: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Patching the ACEE

Page 41: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

APF

@ayoul3__

Page 42: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

https://github.com/ayoul3/Privesc/blob/master/ELV.APF

ELV.APF DEMO

Page 43: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Mark Wilson @ich408i discussed a similar abuse of privilege using

SVC

Henri Kuiper @HenriKuiper has similar code (though more admin

oriented) published on http://zdevops.github.io/zdosu/

Stu Henderson alluded to critical risks of having APF with ALTER

access

The theory behind this trick is not new

Page 44: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Supervisor Call ~ Syscalls on Linux: APIs to hand over control to

Supervisor mode

Supervisor Call

Table of 255 SVC. 0 to 200 are IBM reserved. 201 – 255 are

user defined

Some admins/products register an authorized SVC that switches

the AUTH bit and goes into Kernel mode

Page 45: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

« Magic » SVC code

Page 46: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Forget APF, anyone can

get into Supervisor mode

Call SVC to get into Supervisor mode

APF: Libraries or folders to go into Kernel mode

Page 47: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

PSA

FLCCVT

0

16

Always starts at

virtu. addr0

CVT

CVTABEND

Referencesall major structures

200 SCVT

Secondary CVT

SVCTABLE132

SVC 3

SVC 2

SVC 1

SVC 255

Hunting SVC on z/OS... Diving into virtual memory

Page 48: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

We browse the SVC table

looking for these

instructions (and other

possible variations)

Looking for « magic » SVC

Page 49: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks https://github.com/ayoul3/Privesc/blob/master/ELV.SVC

ELV.SVC DEMO

Page 50: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks https://github.com/mainframed/logica/blob/master/Tfy.source.backdoor

Excerpts from the Logica attack

Page 51: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

The user’s attribute are modified => RACF rules are altered

You can be Special, that does not mean you can access any app!

=> Need to figure out the right class/resource to add

RACF rules (not easy)

A few problems though

RACF: enforces ACL and authentication

Page 52: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Impersonating users

Page 53: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Interesting stuff in the ACEEForeign ACEE

UserID

Group Name

User Flags

Privileged flag

Terminal information

Terminal ID

@ List of groups

….

….

Duplicate fields

Our own ACEE

Page 54: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Each user or program is allocated a virtual address space (same as

in Windows/Linux)

Not so fast…

Private areas can only be addressed from within the address space

All addresses spaces share some common regions that contain

system data & code: PSA, CVT, etc.

Each address space is identified by a 2-byte number : ASID (~ PID

on Linux)

Page 55: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Virtual address space layout

Shared Area

Low User Private

Extended Private

Extended Common

Common region

User region

System region

PSA

Private

8K

Virtual Address Space

16 MB

2 G

2 T

512 T

16 EB

ACEE

Private region24K

Page 56: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Interesting stuff in the ACEE

Target program or

user

….

Our own user

session

Evil program

Virtual address space A Virtual address space B Virtual address space C

1. Copy ACEE 2. Paste ACEE

Page 57: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

https://github.com/ayoul3/Privesc/blob/master/ELV.SELF

ELV.SELF DEMO

Page 58: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

Summary

Mainframes are *not* legacy

There is a huge gap in security awareness between the MF world

and other platforms

There are tools now to pentest the MF, but we need more people

to look into this stuff!

Page 59: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

Shuffling zOS memory blocks

@Mainframed767

@BigEndianSmalls

Mark Wilson & RSM Partners

Henri Kuiper

Stu Henderson

CBT TAPE

IBM

Wavestone

Thank you

Page 60: Post exploit goodness on a Mainframe - Pwn2Own exploit goodness... · 2018-03-29 · Post exploit goodness on a Mainframe SPECIAL is the new root. Shuffling zOS memory blocks What

• github.com/ayoul3

• ayoul3__