shellforth

40
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. ShellForth Shell-code cross-compiler for Forth Dave Weinstein / ToorCon 16

Upload: daniel-sanders

Post on 31-Dec-2015

22 views

Category:

Documents


5 download

DESCRIPTION

ShellForth. Shell-code cross-compiler for Forth Dave Weinstein / ToorCon 16. Agenda. ShellForth in Practice. Code Generation. Forth Overview. Intro. Who am I? Forth? Seriously?. Intro. Who am I? Dave Weinstein Long-time software developer - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

ShellForthShell-code cross-compiler for ForthDave Weinstein / ToorCon 16

Page 2: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.2

Agenda

ShellForth in Practice

Code Generation

Forth OverviewIntro

• Who am I?

• Forth? Seriously?

Page 3: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.3

Intro

Who am I?Dave Weinstein• Long-time software developer • Moved into the security space (as other than “the problem”) about a decade ago• Currently a Security Researcher at the Zero Day Initiative in HP Security Research

Forth? Shell-code in Forth? Seriously?Yes. Ok, no. Well, yes and no.• It started out as a joke concept (code that never came from a standard compiler for RE

questions)• It turned out to be interesting in and of itself• Also, back in the 1980s, I burned out my “technical religious fanatic” circuits as a Forth

ideologue

Page 4: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.4

Agenda

ShellForth in Practice

Code Generation

Forth Overview

• History

• Basics

• Key Features

• Use cases

Intro

• Who am I?

• Forth? Seriously?

Page 5: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.5

Forth Overview

History of Forth

• Created by Charles Moore– Original development work dates back to the 1950s– Key language concepts show up by the late 1960s, with a recognizably Forth language

by 1970– Language as “usable by people other than Charles Moore” dates back to the 1970s– ANSI Standard defined in 1994

• ShellForth is not an ANSI-compliant dialect• In fact, the ANSI-compliant dialects won’t even talk to it at parties

Page 6: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.6

Forth Overview

Forth Basics

• Stack based language– All Forth subroutines (“words”) consume arguments from the stack, and leave results

on the stack• Returning multiple values has always been trivial in Forth

– One stack isn’t enough• Forth has a separate “return stack”, which holds the return address for ending a

subroutine, but can also be used to hold information inside of a subroutine• Syntax is almost always “Reverse-Polish Notation”

– i.e. “5 4 +” instead of “5 + 4” or “(+ 5 4)”– No, my fondness for Forth and RPN is not why I ended up at Hewlett-Packard

Page 7: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.7

Forth Overview

Forth Basics

• Based around a dictionary of “words”– Each word has an action associated with it

• Variables put their address on the data stack• Constants put their value on the data stack• Executable words (i.e. subroutines) execute their body

– Forth intrinsics are not privileged• New “defining words” (words that create new types of words) can be defined• Existing words (including Forth core language features) can be overridden for future

compilation (already-compiled uses are unaffected)

Page 8: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.8

Forth Overview

Programming in Forth

• Often derided as a “write-only language”• Idiomatic Forth programming consists of effectively extending the language until it

includes your application, and then stopping– Forth syntax leads to a large number of small functions with few side effects

• All of the needed linguistic features for Function Programming have always been part of it

– The drawback is that much like having to work in someone else’s Emacs environment, the customizations are more complicated than the base

• Very easy to experiment with new language features using Forth

Page 9: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.9

Forth Overview

Forth Example Code5 CONSTANT FLUID-LEVEL-PORT 7 CONSTANT FLUID-INPUT-VALVE

0 CONSTANT ON 1 CONSTANT OFF 63 CONSTANT FULL

: WAIT-WHILE-FULL ( -- )

BEGIN FLUID-LEVEL-PORT IN FULL <> UNTIL ;

: WAIT-UNTIL-FULL ( -- )

BEGIN FLUID-LEVEL-PORT IN FULL = UNTIL ;

: KEEP-FULL ( -- )

BEGIN

WAIT-WHILE-FULL

ON FLUID-INPUT-VALVE OUT

WAIT-UNTIL-FULL

OFF FLUID-INPUT-VALVE OUT

REPEAT ;

Page 10: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.10

Forth Overview

Forth in Use

• Astronomy (where it began)• Embedded systems• OpenBoot• Bitcoin script is a Forth derivative

Page 11: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.11

Agenda

ShellForth in Practice

Code Generation

• Register Usage

• Compilation Modes

• x64 Differences

• Differences from Standard Forth

Forth Overview

• History

• Basics

• Key Features

• Use cases

Intro

• Who am I?

• Forth? Seriously?

Page 12: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.12

Code Generation

Register Usage and Convention (x86)

ESI Pointer to code base EAX

Scratch

EDI Pointer to data base ECX

Top of Data Stack

ESP Data Stack Pointer EDX

2nd of Data Stack

EBP Return Stack Pointer EBX

3rd of Data Stack

Page 13: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.13

Code Generation

Compilation Mode

Forth PrimitiveESP Data Stack (except for cached top elements)EBP Return Stack

Forth Function CallESP Return StackEBP Data Stack (except for cached top elements)

Page 14: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.14

Code Generation

Inline Primitives

The Forth primitive NIP removes the second element of the stack. In Forth standard comments, this would be described as ( n1 n2 – n2 ):

; NIP

mov edx,ebxpop ebx

Page 15: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.15

Code Generation

x64 Code Generation Differences

RSI Pointer to code base RAX

Scratch

RDI Pointer to data base RBX

Scratch

RSP Data Stack Pointer RCX

Top of Data Stack

RBP Return Stack Pointer RDX

2nd of Data Stack

R8 3rd of Data Stack

R9 4th of Data Stack

Page 16: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.16

Code Generation

Differences from Standard Forth

Compiler Features ShellForth does not have or has changed:• Immediate/Compiled Mode

– This is the ability to run Forth code as you’re compiling it; ShellForth has no interpreted mode

• The ability to create defining words– Defining words are all done at the F# level

• Constant definitions are changed to be entirely prefix notation– Unlike standard Forth, we don’t have a stack running while we compile

• Variable definitions are changed to be variable width– Standard Forth has variables as “cell width”, and ALLOT to increase that; we just have

declarations• C-style strings instead of Pascal-style (counted) strings

Page 17: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.17

Code Generation

Differences from Standard Forth

Forth primitives not implemented:• Floating point• I/O • Forth compilation words

– These are the things out of which new defining words are built• Forth dictionary manipulation

– This includes both the ability to create new constants on the fly, or to search/modify the dictionary at runtime

Page 18: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.18

Code Generation

Differences from Standard Forth

Functionality added:• Direct support for read/write to the TEB• STDCALL/STDCALLVOID words for Windows system calls• APIHASH/FINDAPICALL functionality

– APIHASH is a compile-time calculation of the Metasploit hash of a function by module and name

– FINDAPICALL is a built-in function that will put the address of the a function (given the hash) on the stack at runtime

Page 19: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.19

Agenda

ShellForth in Practice

• Demo

• Taking the Demo apart

• Was there ever any real point to this?

Code Generation

• Register Usage

• Compilation Modes

• x64 Differences

• Differences from Standard Forth

Forth Overview

• History

• Basics

• Key Features

• Use cases

Intro

• Who am I?

• Forth? Seriously?

Page 20: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.20

ShellForth in Practice

Demo!

Page 21: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.21

ShellForth in Practice

Taking Apart the Demo

Page 22: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.22

ShellForth in Practice

Taking Apart the Demo

Page 23: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.23

ShellForth in Practice

Taking Apart the Demo

Page 24: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.24

ShellForth in Practice

Taking Apart the Demo

Page 25: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.25

ShellForth in Practice

Taking Apart the Demo

Page 26: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.26

ShellForth in Practice

Taking Apart the Demo

Page 27: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.27

ShellForth in Practice

Taking Apart the Demo

Looking at the Forth source code:

: SHELL_CODE RESOLVE_FUNCTION_ADDRESSES BEGIN COMMAND CREATE_PROCESS WHILE WAIT_FOR_TERMINATION "Not so fast..." "This isn't over yet!" MESSAGE_BOX REPEAT ;

Page 28: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.28

ShellForth in Practice

Taking Apart the Demo

Let’s look at the implementation of CREATE_PROCESS:

Page 29: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.29

ShellForth in Practice

Taking Apart the Demo

Page 30: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.30

ShellForth in Practice

Taking Apart the Demo

Page 31: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.31

ShellForth in Practice

Taking Apart the Demo

Page 32: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.32

ShellForth in Practice

Taking Apart the Demo

Page 33: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.33

ShellForth in Practice

Taking Apart the Demo

Page 34: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.34

ShellForth in Practice

Taking Apart the Demo

Page 35: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.35

ShellForth in Practice

Taking Apart the Demo

Page 36: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.36

ShellForth in Practice

Taking Apart the Demo

Page 37: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.37

ShellForth in Practice

Taking Apart the Demo

Page 38: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.38

ShellForth in Practice

Taking Apart the Demo

Looking at the Forth source code:

VARIABLE CREATE_PROCESSA_FUNC CELLSIZE

: CREATE_PROCESSA ( create-processa-args -- retval ) 10 CREATE_PROCESSA_FUNC @ STDCALL ;

: CREATE_PROCESS ( commandline -- errorVal ) >R PROCESS_INFORMATION STARTUPINFO NULL NULL 0 FALSE NULL NULL R> NULL CREATE_PROCESSA ;

Page 39: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.39

Wrapping Things Up

Did this ever really have a use?

• Well, maybe…– The original genesis of this was “what happens when you put something that compiles

to a virtual machine that most people aren’t familiar with, and wasn’t ever C or C++, in front of a reverse engineer”?

– That still has some use.

• Otherwise…– No, not really. But it was fun to write.

• Any questions?

Page 40: ShellForth

© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

Thank you