0% found this document useful (0 votes)
8 views

Table Of Content - Gray Hat Hacking

The document outlines the contents of a comprehensive guide on Gray Hat Hacking, covering various aspects of ethical hacking, exploit development, and system vulnerabilities. It is divided into six parts, including preparation, ethical hacking, hacking systems, IoT, hypervisors, and cloud hacking, with detailed chapters on specific topics. Each section provides foundational knowledge and practical skills necessary for ethical hacking and cybersecurity practices.

Uploaded by

rereva7383
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Table Of Content - Gray Hat Hacking

The document outlines the contents of a comprehensive guide on Gray Hat Hacking, covering various aspects of ethical hacking, exploit development, and system vulnerabilities. It is divided into six parts, including preparation, ethical hacking, hacking systems, IoT, hypervisors, and cloud hacking, with detailed chapters on specific topics. Each section provides foundational knowledge and practical skills necessary for ethical hacking and cybersecurity practices.

Uploaded by

rereva7383
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

CONTENTS AT A GLANCE

Part I Preparation
Chapter 1 Gray Hat Hacking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Chapter 2 Programming Survival Skills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Chapter 3 Linux Exploit Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Chapter 4 Introduction to Ghidra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Chapter 5 IDA Pro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

Part II Ethical Hacking


Chapter 6 Red and Purple Teams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Chapter 7 Command and Control (C2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Chapter 8 Building a Threat Hunting Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Chapter 9 Introduction to Threat Hunting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

Part III Hacking Systems


Chapter 10 Basic Linux Exploits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Chapter 11 Advanced Linux Exploits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Chapter 12 Linux Kernel Exploits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Chapter 13 Basic Windows Exploitation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Chapter 14 Windows Kernel Exploitation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Chapter 15 PowerShell Exploitation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Chapter 16 Getting Shells Without Exploits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Chapter 17 Post-Exploitation in Modern Windows Environments . . . . . . . . . . . . . . . 343
Chapter 18 Next-Generation Patch Exploitation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369

xi
Gray Hat Hacking: The Ethical Hacker’s Handbook
xii
Part IV Hacking IoT
Chapter 19 Internet o Things to Be Hacked . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Chapter 20 Dissecting Embedded Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Chapter 21 Exploiting Embedded Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Chapter 22 Sotware-Defned Radio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447

Part V Hacking Hypervisors


Chapter 23 Hypervisors 101 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Chapter 24 Creating a Research Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Chapter 25 Inside Hyper-V . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Chapter 26 Hacking Hypervisors Case Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541

Part VI Hacking the Cloud


Chapter 27 Hacking in Amazon Web Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Chapter 28 Hacking in Azure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Chapter 29 Hacking Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
Chapter 30 Hacking on Kubernetes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623

Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
CONTENTS

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi

Part I Preparation
Chapter 1 Gray Hat Hacking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Gray Hat Hacking Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
History of Hacking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Ethics and Hacking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Definition of Gray Hat Hacking . . . . . . . . . . . . . . . . . . . . . . 5
History of Ethical Hacking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
History of Vulnerability Disclosure . . . . . . . . . . . . . . . . . . . . 6
Bug Bounty Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Know the Enemy: Black Hat Hacking . . . . . . . . . . . . . . . . . . . . . . 11
Advanced Persistent Threats . . . . . . . . . . . . . . . . . . . . . . . . . 11
Lockheed Martin Cyber Kill Chain . . . . . . . . . . . . . . . . . . . . 11
Courses of Action for the Cyber Kill Chain . . . . . . . . . . . . . . 13
MITRE ATT&CK Framework . . . . . . . . . . . . . . . . . . . . . . . 14
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Chapter 2 Programming Survival Skills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
C Programming Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Basic C Language Constructs . . . . . . . . . . . . . . . . . . . . . . . . 21
Lab 2-1: Format Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Lab 2-2: Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Lab 2-3: if/else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Sample Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Lab 2-4: hello.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Lab 2-5: meet.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Compiling with gcc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Lab 2-6: Compiling meet.c . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Computer Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Random Access Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Endian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Segmentation of Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

xiii
Gray Hat Hacking: The Ethical Hacker’s Handbook
xiv
Programs in Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Strings in Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Putting the Pieces of Memory Together . . . . . . . . . . . . . . . . . 35
Lab 2-7: memory.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Intel Processors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Assembly Language Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Machine vs. Assembly vs. C . . . . . . . . . . . . . . . . . . . . . . . . . . 37
AT&T vs. NASM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Addressing Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Assembly File Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Lab 2-8: Simple Assembly Program . . . . . . . . . . . . . . . . . . . . 41
Debugging with gdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
gdb Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Lab 2-9: Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Lab 2-10: Disassembly with gdb . . . . . . . . . . . . . . . . . . . . . . 44
Python Survival Skills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Getting Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Lab 2-11: Launching Python . . . . . . . . . . . . . . . . . . . . . . . . 46
Lab 2-12: “Hello, World!” in Python . . . . . . . . . . . . . . . . . . 46
Python Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Lab 2-13: Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Lab 2-14: Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Lab 2-15: Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Lab 2-16: Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Lab 2-17: Files with Python . . . . . . . . . . . . . . . . . . . . . . . . . 51
Lab 2-18: Sockets with Python . . . . . . . . . . . . . . . . . . . . . . . 53
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Chapter 3 Linux Exploit Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Binary, Dynamic Information-Gathering Tools . . . . . . . . . . . . . . . . 55
Lab 3-1: Hello.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Lab 3-2: ldd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Lab 3-3: objdump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Lab 3-4: strace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Lab 3-5: ltrace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Lab 3-6: checksec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Lab 3-7: libc-database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Lab 3-8: patchelf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Lab 3-9: one_gadget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Lab 3-10: Ropper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Contents
xv
Extending gdb with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Pwntools CTF Framework and Exploit Development Library . . . . . 64
Summary of Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Lab 3-11: leak-bof.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
HeapME (Heap Made Easy) Heap Analysis
and Collaboration Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Installing HeapME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Lab 3-12: heapme_demo.c . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Chapter 4 Introduction to Ghidra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Creating Our First Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Installation and QuickStart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Setting the Project Workspace . . . . . . . . . . . . . . . . . . . . . . . . 72
Functionality Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Lab 4-1: Improving Readability with Annotations . . . . . . . . 80
Lab 4-2: Binary Diffing and Patch Analysis . . . . . . . . . . . . . . 83
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Chapter 5 IDA Pro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Introduction to IDA Pro for Reverse Engineering . . . . . . . . . . . . . . 89
What Is Disassembly? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Navigating IDA Pro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
IDA Pro Features and Functionality . . . . . . . . . . . . . . . . . . . . . . . . 96
Cross-References (Xrefs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Proximity Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Opcodes and Addressing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Debugging with IDA Pro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

Part II Ethical Hacking


Chapter 6 Red and Purple Teams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Introduction to Red Teams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Vulnerability Scanning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Validated Vulnerability Scanning . . . . . . . . . . . . . . . . . . . . . . 113
Gray Hat Hacking: The Ethical Hacker’s Handbook
xvi
Penetration Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Threat Simulation and Emulation . . . . . . . . . . . . . . . . . . . . . 118
Purple Team . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Making Money with Red Teaming . . . . . . . . . . . . . . . . . . . . . . . . . 121
Corporate Red Teaming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Consultant Red Teaming . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Purple Team Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Purple Team Skills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Purple Team Activities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Chapter 7 Command and Control (C2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Command and Control Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Metasploit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Lab 7-1: Creating a Shell with Metasploit . . . . . . . . . . . . . . . 129
PowerShell Empire . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Covenant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Lab 7-2: Using Covenant C2 . . . . . . . . . . . . . . . . . . . . . . . . 132
Payload Obfuscation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
msfvenom and Obfuscation . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Lab 7-3: Obfuscating Payloads with msfvenom . . . . . . . . . . . 136
Creating C# Launchers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Lab 7-4: Compiling and Testing C# Launchers . . . . . . . . . . . 141
Creating Go Launchers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Lab 7-5: Compiling and Testing Go Launchers . . . . . . . . . . . 143
Creating Nim Launchers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Lab 7-6: Compiling and Testing Nim Launchers . . . . . . . . . 145
Network Evasion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Alternate Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
C2 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
EDR Evasion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Killing EDR Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Bypassing Hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Chapter 8 Building a Threat Hunting Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Threat Hunting and Labs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Options of Threat Hunting Labs . . . . . . . . . . . . . . . . . . . . . . 153
Method for the Rest of this Chapter . . . . . . . . . . . . . . . . . . . 154
Basic Threat Hunting Lab: DetectionLab . . . . . . . . . . . . . . . . . . . . 154
Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Contents
xvii
Lab 8-1: Install the Lab on Your Host . . . . . . . . . . . . . . . . . . 155
Lab 8-2: Install the Lab in the Cloud . . . . . . . . . . . . . . . . . . 157
Lab 8-3: Looking Around the Lab . . . . . . . . . . . . . . . . . . . . 160
Extending Your Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
HELK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Lab 8-4: Install HELK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Lab 8-5: Install Winlogbeat . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Lab 8-6: Kibana Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Lab 8-7: Mordor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Chapter 9 Introduction to Threat Hunting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Threat Hunting Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Types of Threat Hunting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Workflow of a Threat Hunt . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Normalizing Data Sources with OSSEM . . . . . . . . . . . . . . . . . . . . . 175
Data Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
OSSEM to the Rescue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Data-Driven Hunts Using OSSEM . . . . . . . . . . . . . . . . . . . . . . . . . 177
MITRE ATT&CK Framework Refresher: T1003.002 . . . . . 177
Lab 9-1: Visualizing Data Sources with OSSEM . . . . . . . . . . 177
Lab 9-2: AtomicRedTeam Attacker Emulation . . . . . . . . . . . 181
Exploring Hypothesis-Driven Hunts . . . . . . . . . . . . . . . . . . . . . . . . 183
Lab 9-3: Hypothesis that Someone Copied a SAM File . . . . . 183
Crawl, Walk, Run . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Enter Mordor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Lab 9-4: Hypothesis that Someone Other
than an Admin Launched PowerShell . . . . . . . . . . . . . . . . 186
Threat Hunter Playbook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Departure from HELK for Now . . . . . . . . . . . . . . . . . . . . . . 190
Spark and Jupyter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Lab 9-5: Automated Playbooks and Sharing of Analytics . . . 191
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

Part III Hacking Systems


Chapter 10 Basic Linux Exploits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Stack Operations and Function-Calling Procedures . . . . . . . . . . . . . 199
Buffer Overflows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Lab 10-1: Overflowing meet.c . . . . . . . . . . . . . . . . . . . . . . . 203
Ramifications of Buffer Overflows . . . . . . . . . . . . . . . . . . . . . 206
Gray Hat Hacking: The Ethical Hacker’s Handbook
xviii
Local Buffer Overflow Exploits . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Lab 10-2: Components of the Exploit . . . . . . . . . . . . . . . . . . 207
Lab 10-3: Exploiting Stack Overflows
from the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . 208
Lab 10-4: Writing the Exploit with Pwntools . . . . . . . . . . . . 209
Lab 10-5: Exploiting Small Buffers . . . . . . . . . . . . . . . . . . . . 210
Exploit Development Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Lab 10-6: Building Custom Exploits . . . . . . . . . . . . . . . . . . . 212
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Chapter 11 Advanced Linux Exploits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Lab 11-1: Vulnerable Program and Environment Setup . . . . 219
Lab 11-2: Bypassing Non-Executable Stack (NX)
with Return-Oriented Programming (ROP) . . . . . . . . . . . 222
Lab 11-3: Defeating Stack Canaries . . . . . . . . . . . . . . . . . . . 225
Lab 11-4: ASLR Bypass with an Information Leak . . . . . . . . 228
Lab 11-5: PIE Bypass with an Information Leak . . . . . . . . . . 230
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Chapter 12 Linux Kernel Exploits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Lab 12-1: Environment Setup
and Vulnerable procfs Module . . . . . . . . . . . . . . . . . . . . . 233
Lab 12-2: ret2usr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Lab 12-3: Defeating Stack Canaries . . . . . . . . . . . . . . . . . . . 238
Lab 12-4: Bypassing Supervisor Mode Execution Protection
(SMEP) and Kernel Page-Table Isolation (KPTI) . . . . . . . 241
Lab 12-5: Bypassing Supervisor Mode Access
Prevention (SMAP) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Lab 12-6: Defeating Kernel Address Space
Layout Randomization (KASLR) . . . . . . . . . . . . . . . . . . . 246
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Chapter 13 Basic Windows Exploitation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Compiling and Debugging Windows Programs . . . . . . . . . . . . . . . 250
Lab 13-1: Compiling on Windows . . . . . . . . . . . . . . . . . . . . 250
Debugging on Windows with Immunity Debugger . . . . . . . . 252
Lab 13-2: Crashing the Program . . . . . . . . . . . . . . . . . . . . . . 255
Writing Windows Exploits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Exploit Development Process Review . . . . . . . . . . . . . . . . . . 258
Lab 13-3: Exploiting ProSSHD Server . . . . . . . . . . . . . . . . . 258
Contents
xix
Understanding Structured Exception Handling . . . . . . . . . . . . . . . . 270
Understanding and Bypassing Common
Windows Memory Protections . . . . . . . . . . . . . . . . . . . . . 271
Safe Structured Exception Handling . . . . . . . . . . . . . . . . . . . 272
Bypassing SafeSEH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Data Execution Prevention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Return-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . 274
Gadgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Building the ROP Chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Chapter 14 Windows Kernel Exploitation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
The Windows Kernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Kernel Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Kernel Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Lab 14-1: Setting Up Kernel Debugging . . . . . . . . . . . . . . . . 284
Picking a Target . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Lab 14-2: Obtaining the Target Driver . . . . . . . . . . . . . . . . . 286
Lab 14-3: Reverse Engineering the Driver . . . . . . . . . . . . . . . 287
Lab 14-4: Interacting with the Driver . . . . . . . . . . . . . . . . . . 292
Token Stealing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Lab 14-5: Arbitrary Pointer Read/Write . . . . . . . . . . . . . . . . 297
Lab 14-6: Writing a Kernel Exploit . . . . . . . . . . . . . . . . . . . . 298
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Chapter 15 PowerShell Exploitation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Why PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Living off the Land . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
PowerShell Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
PowerShell Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Loading PowerShell Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Lab 15-1: The Failure Condition . . . . . . . . . . . . . . . . . . . . . 307
Lab 15-2: Passing Commands on the Command Line . . . . . 309
Lab 15-3: Encoded Commands . . . . . . . . . . . . . . . . . . . . . . 310
Lab 15-4: Bootstrapping via the Web . . . . . . . . . . . . . . . . . . 311
Exploitation and Post-Exploitation with PowerSploit . . . . . . . . . . . 313
Lab 15-5: Setting Up PowerSploit . . . . . . . . . . . . . . . . . . . . . 313
Lab 15-6: Running Mimikatz Through PowerShell . . . . . . . . 315
Using PowerShell Empire for C2 . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Lab 15-7: Setting Up Empire . . . . . . . . . . . . . . . . . . . . . . . . 316
Lab 15-8: Staging an Empire C2 . . . . . . . . . . . . . . . . . . . . . . 317
Lab 15-9: Using Empire to Own the System . . . . . . . . . . . . . 318
Lab 15-10: Using WinRM to Launch Empire . . . . . . . . . . . . 320
Gray Hat Hacking: The Ethical Hacker’s Handbook
xx
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Chapter 16 Getting Shells Without Exploits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Capturing Password Hashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Understanding LLMNR and NBNS . . . . . . . . . . . . . . . . . . . 325
Understanding Windows NTLMv1
and NTLMv2 Authentication . . . . . . . . . . . . . . . . . . . . . . 326
Using Responder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Lab 16-1: Getting Passwords with Responder . . . . . . . . . . . . 329
Using Winexe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Lab 16-2: Using Winexe to Access Remote Systems . . . . . . . 332
Lab 16-3: Using Winexe to Gain Elevated Privileges . . . . . . . 333
Using WMI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Lab 16-4: Querying System Information with WMI . . . . . . . 334
Lab 16-5: Executing Commands with WMI . . . . . . . . . . . . . 337
Taking Advantage of WinRM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Lab 16-6: Executing Commands with WinRM . . . . . . . . . . 338
Lab 16-7: Using Evil-WinRM to Execute Code . . . . . . . . . . 339
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Chapter 17 Post-Exploitation in Modern Windows Environments . . . . . . . . . . 343
Post-Exploitation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Host Recon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Lab 17-1: Using whoami to Identify Privileges . . . . . . . . . . . 344
Lab 17-2: Using Seatbelt to Find User Information . . . . . . . 346
Lab 17-3: System Recon with PowerShell . . . . . . . . . . . . . . . 348
Lab 17-4: System Recon with Seatbelt . . . . . . . . . . . . . . . . . 350
Lab 17-5: Getting Domain Information with PowerShell . . . 352
Lab 17-6: Using PowerView for AD Recon . . . . . . . . . . . . . . 354
Lab 17-7: Gathering AD Data with SharpHound . . . . . . . . . 356
Escalation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Lab 17-8: Profiling Systems with winPEAS . . . . . . . . . . . . . . 357
Lab 17-9: Using SharpUp to Escalate Privileges . . . . . . . . . . 358
Lab 17-10: Searching for Passwords in User Objects . . . . . . . 359
Lab 17-11: Abusing Kerberos to Gather Credentials . . . . . . . 360
Lab 17-12: Abusing Kerberos to Escalate Privileges . . . . . . . . 361
Active Directory Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Lab 17-13: Abusing AdminSDHolder . . . . . . . . . . . . . . . . . 364
Lab 17-14: Abusing SIDHistory . . . . . . . . . . . . . . . . . . . . . . 366
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Contents
xxi
Chapter 18 Next-Generation Patch Exploitation . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Introduction to Binary Diffing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Application Diffing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Patch Diffing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Binary Diffing Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
BinDiff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
turbodiff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Lab 18-1: Our First Diff . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Patch Management Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Microsoft Patch Tuesday . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Obtaining and Extracting Microsoft Patches . . . . . . . . . . . . . 380
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389

Part IV Hacking IoT


Chapter 19 Internet o Things to Be Hacked . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Internet of Things (IoT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Types of Connected Things . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Wireless Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Communication Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Security Concerns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Shodan IoT Search Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Web Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Shodan Command-Line Interface . . . . . . . . . . . . . . . . . . . . . 400
Lab 19-1: Using the Shodan Command Line . . . . . . . . . . . . 400
Shodan API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Lab 19-2: Testing the Shodan API . . . . . . . . . . . . . . . . . . . . 402
Lab 19-3: Playing with MQTT . . . . . . . . . . . . . . . . . . . . . . 403
Implications of this Unauthenticated Access to MQTT . . . . . 404
IoT Worms: It Was a Matter of Time . . . . . . . . . . . . . . . . . . . . . . . 404
Prevention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Chapter 20 Dissecting Embedded Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
CPU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Microprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
System on Chip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Common Processor Architectures . . . . . . . . . . . . . . . . . . . . . 410
Gray Hat Hacking: The Ethical Hacker’s Handbook
xxii
Serial Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
UART . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
I2C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Debug Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
JTAG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
SWD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Bootloader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
No Operating System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Real-Time Operating System . . . . . . . . . . . . . . . . . . . . . . . . . 423
General Operating System . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Chapter 21 Exploiting Embedded Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Static Analysis of Vulnerabilities in Embedded Devices . . . . . . . . . . 427
Lab 21-1: Analyzing the Update Package . . . . . . . . . . . . . . . 427
Lab 21-2: Performing Vulnerability Analysis . . . . . . . . . . . . . 432
Dynamic Analysis with Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . 436
The Test Environment Setup . . . . . . . . . . . . . . . . . . . . . . . . . 436
Ettercap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Dynamic Analysis with Emulation . . . . . . . . . . . . . . . . . . . . . . . . . 440
FirmAE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Lab 21-3: Setting Up FirmAE . . . . . . . . . . . . . . . . . . . . . . . . 440
Lab 21-4: Emulating Firmware . . . . . . . . . . . . . . . . . . . . . . . 441
Lab 21-5: Exploiting Firmware . . . . . . . . . . . . . . . . . . . . . . . 444
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Chapter 22 Sotware-Defned Radio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Getting Started with SDR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
What to Buy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Not So Quick: Know the Rules . . . . . . . . . . . . . . . . . . . . . . . 449
Learn by Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Replay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Analyze . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Preview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Execute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Contents
xxiii
Part V Hacking Hypervisors
Chapter 23 Hypervisors 101 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
What Is a Hypervisor? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Popek and Goldberg Virtualization Theorems . . . . . . . . . . . . 470
Goldberg’s Hardware Virtualizer . . . . . . . . . . . . . . . . . . . . . . 472
Type-1 and Type-2 VMMs . . . . . . . . . . . . . . . . . . . . . . . . . . 474
x86 Virtualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Dynamic Binary Translation . . . . . . . . . . . . . . . . . . . . . . . . . 476
Ring Compression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Shadow Paging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Paravirtualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Hardware Assisted Virtualization . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
VMX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
EPT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Chapter 24 Creating a Research Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Hypervisor Attack Surface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
The Unikernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Lab 24-1: Booting and Communication . . . . . . . . . . . . . . . . 489
Lab 24-2: Communication Protocol . . . . . . . . . . . . . . . . . . . 492
Boot Message Implementation . . . . . . . . . . . . . . . . . . . . . . . 497
Handling Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
The Client (Python) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Communication Protocol (Python) . . . . . . . . . . . . . . . . . . . . 500
Lab 24-3: Running the Guest (Python) . . . . . . . . . . . . . . . . 504
Lab 24-4: Code Injection (Python) . . . . . . . . . . . . . . . . . . . . 505
Fuzzing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
The Fuzzer Base Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
Lab 24-5: IO-Ports Fuzzer . . . . . . . . . . . . . . . . . . . . . . . . . . 510
Lab 24-6: MSR Fuzzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Lab 24-7: Exception Handling . . . . . . . . . . . . . . . . . . . . . . . 513
Fuzzing Tips and Improvements . . . . . . . . . . . . . . . . . . . . . . 514
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
Chapter 25 Inside Hyper-V . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Environment Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Hyper-V Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Hyper-V Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Virtual Trust Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
Generation-1 VMs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
Gray Hat Hacking: The Ethical Hacker’s Handbook
xxiv
Lab 25-1: Scanning PCI Devices in a Generation-1 VM . . . . 522
Generation 2 VMs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Lab 25-2: Scanning PCI Devices in a Generation-2 VM . . . . 523
Hyper-V Synthetic Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Synthetic MSRs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Lab 25-3: Setting Up the Hypercall Page
and Dumping Its Contents . . . . . . . . . . . . . . . . . . . . . . . . 525
Hypercalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
VMBus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
Lab 25-4: Listing VMBus Devices . . . . . . . . . . . . . . . . . . . . 534
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Chapter 26 Hacking Hypervisors Case Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
Bug Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
USB Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Lab 26-1: Patch Analysis Using GitHub API . . . . . . . . . . . . 544
Developing a Trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Setting Up the Target . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Lab 26-2: Scanning the PCI Bus . . . . . . . . . . . . . . . . . . . . . . 547
The EHCI Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
Triggering the Bug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Lab 26-3: Running the Trigger . . . . . . . . . . . . . . . . . . . . . . . 551
Exploitation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
Relative Write Primitive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
Relative Read Primitive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Lab 26-4: Debugging the Relative Read Primitive . . . . . . . . . 554
Arbitrary Read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
Full Address-Space Leak Primitive . . . . . . . . . . . . . . . . . . . . . 556
Module Base Leak . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
RET2LIB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
Lab 26-5: Finding Function Pointers with GDB . . . . . . . . . . 559
Lab 26-6: Displaying IRQState with GDB . . . . . . . . . . . . . . 561
Lab 26-7: Launching the Exploit . . . . . . . . . . . . . . . . . . . . . 562
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563

Part VI Hacking the Cloud


Chapter 27 Hacking in Amazon Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Amazon Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Services, Locations, and Infrastructure . . . . . . . . . . . . . . . . . . 568
How Authorization Works in AWS . . . . . . . . . . . . . . . . . . . . 568
Contents
xxv
Abusing AWS Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . 570
Lab 27-1: Environment Setup . . . . . . . . . . . . . . . . . . . . . . . . 571
Abusing Authentication Controls . . . . . . . . . . . . . . . . . . . . . . . . . . 571
Types of Keys and Key Material . . . . . . . . . . . . . . . . . . . . . . . 572
Lab 27-2: Finding AWS Keys . . . . . . . . . . . . . . . . . . . . . . . . 572
Attacker Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Lab 27-3: Enumerating Permissions . . . . . . . . . . . . . . . . . . . 577
Lab 27-4: Leveraging Access to Perform
Unauthorized Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
Lab 27-5: Persistence Through System Internals . . . . . . . . . . 580
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
Chapter 28 Hacking in Azure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Microsoft Azure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Differences Between Azure and AWS . . . . . . . . . . . . . . . . . . . 586
Lab 28-1: Setup of Our Labs . . . . . . . . . . . . . . . . . . . . . . . . 587
Lab 28-2: Additional User Steps . . . . . . . . . . . . . . . . . . . . . . 589
Lab 28-3: Validating Access . . . . . . . . . . . . . . . . . . . . . . . . . 590
Microsoft Azure AD Overview . . . . . . . . . . . . . . . . . . . . . . . 591
Azure Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
Constructing an Attack on Azure-Hosted Systems . . . . . . . . . . . . . 593
Lab 28-4: Azure AD User Lookups . . . . . . . . . . . . . . . . . . . . 594
Lab 28-5: Azure AD Password Spraying . . . . . . . . . . . . . . . . 596
Lab 28-6: Getting onto Azure . . . . . . . . . . . . . . . . . . . . . . . . 597
Control Plane and Managed Identities . . . . . . . . . . . . . . . . . . . . . . 598
Lab 28-7: System Assigned Identities . . . . . . . . . . . . . . . . . . 598
Lab 28-8: Getting a Backdoor on a Node . . . . . . . . . . . . . . . 600
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
Chapter 29 Hacking Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
Linux Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
Container Internals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
Cgroups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
Lab 29-1: Setup of our Environment . . . . . . . . . . . . . . . . . . 604
Lab 29-2: Looking at Cgroups . . . . . . . . . . . . . . . . . . . . . . . 605
Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
Lab 29-3: Container Storage . . . . . . . . . . . . . . . . . . . . . . . . . 609
Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
What Is Docker? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
Lab 29-4: Looking for Docker Daemons . . . . . . . . . . . . . . . 612
Gray Hat Hacking: The Ethical Hacker’s Handbook
xxvi
Container Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
Lab 29-5: Interacting with the Docker API . . . . . . . . . . . . . . 614
Lab 29-6: Executing Commands Remotely . . . . . . . . . . . . . . 614
Lab 29-7: Pivots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
Breaking Out of Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
Lab 29-8: Privileged Pods . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
Lab 29-9: Abusing Cgroups . . . . . . . . . . . . . . . . . . . . . . . . . 618
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Chapter 30 Hacking on Kubernetes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
Kubernetes Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
Fingerprinting Kubernetes API Servers . . . . . . . . . . . . . . . . . . . . . . 625
Lab 30-1: Cluster Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Finding Kubernetes API Servers . . . . . . . . . . . . . . . . . . . . . . . 627
Lab 30-2: Fingerprinting Kubernetes Servers . . . . . . . . . . . . 628
Hacking Kubernetes from Within . . . . . . . . . . . . . . . . . . . . . . . . . . 630
Lab 30-3: Kubestriker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Lab 30-4: Attacking from Within . . . . . . . . . . . . . . . . . . . . . 633
Lab 30-5: Attacking the API Server . . . . . . . . . . . . . . . . . . . . 636
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641

You might also like