introduction to reverse engineering for penetration testers · –supports multiple debuggers and...

25
Introduction to Reverse Engineering for Penetration Testers Stephen Sims SANS Fellow @Steph3nSims

Upload: others

Post on 22-May-2020

20 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

Introduction to Reverse Engineering for Penetration

Testers

Stephen SimsSANS Fellow

@Steph3nSims

Page 2: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

…but I’m a pentester, why should I care about coding, reversing, and exploit dev??• A quick rant about the past and present…

• Back when I first started exploit dev there weren’t many jobs in the field

• The number of people who knew how to write exploits was relatively low

• Compared to today, exploit-writing was easy!

• The same applies to reversing malware

• Exploit dev is in the GXPN and OSCP…

Page 3: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

…but I’m a pentester, why should I care about coding, reversing, and exploit dev??• A quick rant about the past and present…

• Back when I first started exploit dev there weren’t many jobs in the field

• The number of people who knew how to write exploits was relatively low

• Compared to today, exploit-writing was easy!

• The same applies to reversing malware

• Exploit dev is in the GXPN and OSCP…

• You don’t want to be this

Most overused meme in security, but still funny…

Page 4: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

Windows XP shouldn’t be a thing anymore…

Windows XP

This is why we liked XP as

attackers…

Page 5: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

The attack surface has changed…Windows XP Windows 10

Win7

Page 6: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

Low Level vs. High Level Languages• There is no specific classification, but languages can be divided up as

such:• Low level languages include machine code and assembly• Medium level languages included C and C++• High level languages include C# and Java• Even higher level languages include PowerShell and Python

• Lower level languages offer more power as they sit closer to the physical hardware• Unmanaged (low level) languages can pose security concerns over that of

managed languages (high level)• There are various tools available to aid in the reverse engineering of each

language, such as disassemblers and decompilers

6

Easy

Med

Hard

Boss

Page 7: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

Disassembly• The process of taking machine code as input and converting it back to assembly, as originally

assembled by the compiler from source code

Example x86 instruction set input:

Dis

asse

mb

ler

Black: InstructionRed: Operand

Machine Code Disassembly

10

01

01

00

11

1

7

Page 8: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

There are two flavors of disassembly syntax…• Intel and ATT Intel

• Neither changes the code, only the way it is displayed

• Source and destination are switched

• ATT uses “$” & “%” for immediate and indirect operands, and () for pointers

• Intel uses [] for pointers and spells out size and such (DWORD PTR)

• Example• Intel: 89 04 24 mov DWORD PTR [esp], eax

• ATT: 89 04 24 movl %eax, (%esp)Syntax Opcodes Instruction Operands

Page 9: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

Recommended Resources• The IDA Pro Book

• The Unofficial Guide to the World’s Most Popular Disassembler by Chris Eagle• First Edition ISBN: 978-1-59327-178-7• Second Edition released: ISBN 13: 978-1-59327-289-0

• The Hex-Rays Forum - http://www.hex-rays.com/forum/• A great resource for research, questions, and answers• Must be a registered user (must have an IDA license)

• IDA Plugins• http://www.openrce.org/downloads/browse/IDA_Plugins

• IDA 7 is finally out• It’s now a 64-bit application with a completely redesigned API

9

Page 10: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

What is IDA• Recursive Descent Disassembler and Debugger

– Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc.

– Disassembles many processor architectures including ARM, x86, AMD, Motorola, etc.

– Provides many different graphical and structural views of disassembled code

– Reads symbol libraries and cross-references function calls

– Identifies jump tables, lists functions, exported and imported functions, conditional branches, etc.

– At tool that visually makes you look and feel smarter! ☺

10

Page 11: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

How I first saw IDA many years

ago…

Page 12: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

Disassembly Types• Linear Sweep Disassembly - gdb, WinDbg, objdump

• Easiest and most straightforward approach• Begin at Code Segment (CS) entry point & disassemble one instruction at a

time linearly until the end of the CS• Does not accommodate control flow such as branches

• Recursive Descent Disassembly - IDA• Much more complex and effective approach• Can tell instructions from data• Handles branches such as jumps and calls• Defers branch target instructions based on a condition

12

Page 13: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

Conditional Jump Example• Jump on Zero (JZ) and similar instructions

• Checks Zero Flag

Green Arrow

Jump

Red Arrow

Don’t Jump

13

Page 14: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

Primary Dashboard (1)

Function names

Overview Navigator

Graphical view of disassembled function

14

Page 15: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

Primary Dashboard (2)

Disassembly View

15

Page 16: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

Import and Export Address Tables• By clicking on the “Imports” or “Exports” pane you will get a listing of the

IAT/EAT or PLT/GOT for the file examined

• There are other panes and views that will be discussed when appropriate

16

Page 17: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

Debugging Symbols Resolved

Failed to load symbols

17

Page 18: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

IDA Alternatives• It is often asked as to what alternatives there are to IDA

• Radare2: http://www.radare.org

• A free reverse engineering framework

• Installed on Kali Linux by default

• Disassembler, debugger, diffing, extensible, etc.

• Hopper: http://www.hopperapp.com/

• Reverse engineering tool for Linux and OS X

• $89 Personal License & $169 Computer License

• Disassembler, decompiler, extensible, debugger, etc.

18

Page 19: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

Remote Debugging with IDA• IDA supports remote debugging, which allows you to use IDA’s

graphical front-end to various debuggers remotely• Mac OS X 32-bit & 64-bit

• Windows 32-bit & 64-bit

• Linux 32-bit & 64-bit

• Windows CE

• ARM application debugging

• Android application debugging

• Remote GDB

64-bit application debugging only supported with IDA Professional, formerly IDA Advanced.

19

Page 20: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

IDA SDK and Automation Overview• Overview of features:• The IDA SDK allows you to write your own plugins, primarily in C & C++

• Allows developers and users of IDA to expand IDA’s capabilities, automate analysis, etc.

• IDA offers scripting support to interact with the IDA API and practically all contents of the IDA database

• The IDA API allows for interaction via a C/C++ like language called the IDC scripting language

• Since IDA 5.4, Python scripting is supported through the use of IDAPython

• IDA 7 has a new API, so old plugins don’t work without porting over

20

Page 21: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

IDAPython• Plugin to IDA allowing Python scripting

• IDA Python is led by Gergely Erdelyi and available at http://code.google.com/p/idapython/

• More powerful than IDC with access to SDK

• We will focus more heavily on IDAPython due to ease of use, power, and community support

• Using the “ctypes” module in Python can help get around some SDK limitations

• Replaces the interactive box at the bottom of IDA

21

Quick Demo of a couple simple

scripts…

Page 22: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

Common Exploit Technique: Pivoting the Stack Pointer?• The stack pointer advances with pop’s and ret’s

• If we exchange it with another reg, we can yield its power

ESPEAX

I’ve never

been this far

from the stack.

It’s okay ESP,

it will be an

adventure…

Quick Demo…

Page 23: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

FLIRT and FLAIR• Fast Library Identification and Recognition Technology (FLIRT)

• Technology to look for patterns in common library functions

• Helps reduce time spent reversing statically compiled library functions• https://hex-rays.com/products/ida/tech/flirt/in_depth.shtml

• Fast Library Acquisition for Identification and Recognition (FLAIR)• A tool set that allows you to write your own FLIRT signatures

• Used commonly by CTF teams and malware analysts as library code is often statically compiled into the programs

23

Quick Demo…

Page 24: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

Summary

• Reversing may be intimidating at first, but you pick it up quickly and there are a lot of free resources online

• Practice makes perfect

• There’s just not enough time to get everything done without automating as much of your work as possible

Page 25: Introduction to Reverse Engineering for Penetration Testers · –Supports multiple debuggers and techniques, including WinDbg, GDB, Bochs emulator, etc. –Disassembles many processor

Thanks!Questions?

Stephen Sims

@Steph3nSims

25