SlideShare a Scribd company logo
Malicious Hypervisor
Virtualization in Shellcodes
Adhokshaj Mishra
Who am I?
 A security fella
 I am fascinated by malware, and cryptography
 I work on offensive side of research
 Get in touch: me@adhokshajmishraonline.in
Agenda
 Hypervisor: what, how and why?
 Hypervisor in Linux
 Capsule course on hypervisor (Intel VT-x, AMD-V, KVM)
 Spawning a bare-bone VM
 Injecting code in VM
 I/O Between Host and Guest
 Converting C Code to Shellcode
Hypervisor: What?
 A hypervisor is computer software, firmware, or hardware, that creates and
runs virtual machines. A computer on which a hypervisor runs one or more
virtual machines is called a host machine, and each virtual machine is
called a guest machine.
 Type-1 hypervisor: runs directly on bare metal. Example: ESXi
 Type-2 hypervisor: runs on a conventional OS. Example: KVM
Hypervisor: How?
 Software emulation: Hypervisor emulates instructions of guest machine
using instructions of host machine. These are slow, but can handle arbitrary
architecture for guest machine.
 Hardware-assisted: Hypervisor runs the instructions of guest machine directly
on physical CPU using virtualization instructions provided by chip (vmenter,
vmexit etc). Naturally, these are much faster than emulation based; but
cannot handle architectures not supported by underlying chip.
Hypervisor: Why?
Hypervisor: Why?
Hypervisor: Why?
 Hypervisor makes it harder to debug the code running inside it.
 Debugging is even harder if guest architecture is custom and
undocumented.
Capsule Course on Hypervisor
 A virtual CPU starts up just like an actual CPU, i.e., it will start in 8086 aka real
mode. You need to switch it to protected mode (x86), and then to long
mode (x64). Mode switch is not mandatory if you keep yourself limited to
instruction set of the mode you are going to use.
 At startup, you need to allocate memory which will be used by guest
machine. Guest machine will see it as ram, while host will see it as a
memory buffer.
 Registers must be set properly on startup, otherwise guest machine will fail.
You need to load code in guest memory, set registers (at least instruction
pointer) before running the guest.
Capsule Course on Hypervisor
 During execution, guest machine will raise events, which must be handled
by the host.
 For any I/O, virtual ports will be used by guest machine. Whenever guest
machine attempts a write to port, or read from port, it triggers an event,
which is passed to host. Host must handle the data transfer if any.
Spawning a Bare-bone VM
 int kvm = open(“/dev/kvm”, O_RDWR | O_CLOEXEC);
int vmfd = ioctl(kvm, KVM_CREATE_VM, (unsigned long)0);
int vcpufd = ioctl(vmfd, KVM_CREATE_CPU, (unsigned long)0);
 All communication and configuration is done by IOCTL calls. These calls
follow the following pattern:
int ioctl(file_descriptor, command, parameter);
Spawning a Bare-bone VM
 Setting up memory:
uint8_t *mem = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
The above call allocates a buffer of length 4KB, with read and write
permissions. The allocated page will be anonymous (as in, not backed by
any file).
Spawning a Bare-bone VM
 Setting up guest memory:
struct kvm_userspace_memory_region region =
{
.slot = 0,
.guest_phys_addr = 0x1000,
.memory_size = 0x1000,
.userspace_addr = (uint64_t)mem;
};
int ret = ioctl(vmfd, KVM_SET_USER_MEMORY_REGION, &region);
Why was guest physical address set to
0x1000, and not at, say, 0x10 or 0x1?
Injecting Code in VM
 It is as simple as copying code bytes into memory buffer before giving it to
guest machine.
Uint8_t code[] = {/*code bytes here*/};
memcpy(mem, code, sizeof(code));
 NOTE: The code must be for 8086 CPU, otherwise guest machine will fail with
illegal instruction fault and/or with bizarre results. If you want to switch
modes, it must happen through 8086 code.
Injecting Code in VM
 Setting up segment registers:
struct kvm_sregs sregs;
ret = ioctl(vcpufd, KVM_GET_SREGS, &sregs);
sregs.cs.base = 0;
sregs.cs.selector = 0;
ret = ioctl(vcpufd, KVM_SET_SREGS, &sregs);
Injecting Code in VM
 Setting up registers:
struct kvm_regs regs =
{
.rip = 0x1000,
.rax = 4,
.rbx = 5,
.rflags = 0x2,
};
ret = ioctl(vcpufd, KVM_SET_REGS, &regs);
Kicking VM to Life
size_t mmap_size = ioctl(kvm, KVM_GET_CPU_MMAP_SIZE, NULL);
kvm_run *run = mmap(NULL, mmap_size, PROT_READ | PRO_WRITE,
MAP_SHARED, vcpufd, 0);
while(1){
ret = ioctl(vcpufd, KVM_RUN, NULL);
switch (run->exit_reason) {
/*handle events here*/
}
}
I/O Between Host and Guest
switch (run->exit_reason) {
case KVM_EXIT_IO:
if (run->io.direction == KVM_EXIT_IO_OUT && io.port == 0xabc)
{
char ch = *(((char*)run) + run->io.data_offset);
}
/*handle more cases here*/
}
Demo of KVM Powered Hypervisor
Conversion to Shellcode
 gcc kvm.c –o kvm –save-temps –masm=intel
 kvm.s contains assembly listing.
 Remove all the extra clutter generated by compiler
 Resolve addresses of all hardcoded values using JMP-CALL-POP method
 Profit!
Demo of shellcode
Got any questions?

More Related Content

PDF
Implements BIOS emulation support for BHyVe: A BSD Hypervisor
PDF
Implements BIOS emulation support for BHyVe
PDF
Don't Tell Joanna the Virtualized Rootkit is Dead (Blackhat 2007)
PPT
[HackInTheBox] Breaking virtualization by any means
PDF
D1 t2 jonathan brossard - breaking virtualization by switching to virtual 8...
PPTX
ARMvisor @ Linux Symposium 2012
PDF
ARMvisor, more details
PDF
ARMvisor @ COSCUP2012
Implements BIOS emulation support for BHyVe: A BSD Hypervisor
Implements BIOS emulation support for BHyVe
Don't Tell Joanna the Virtualized Rootkit is Dead (Blackhat 2007)
[HackInTheBox] Breaking virtualization by any means
D1 t2 jonathan brossard - breaking virtualization by switching to virtual 8...
ARMvisor @ Linux Symposium 2012
ARMvisor, more details
ARMvisor @ COSCUP2012

What's hot (19)

PPT
PDF
QEMU in Cross building
PDF
[Ruxcon 2011] Post Memory Corruption Memory Analysis
PDF
Embedded Systems Conference 2014 Presentation
PDF
[Ruxcon] Breaking virtualization by switching the cpu to virtual 8086 mode
PDF
Linux: the first second
PDF
ACRN vMeet-Up EU 2021 - shared memory based inter-vm communication introduction
PDF
Tegra 186のu-boot & Linux
PDF
sponsorAVAST-VB2014
PDF
VM - Talk
PDF
Dave Gilbert - KVM and QEMU
PDF
[CB19] Attacking DRM subsystem to gain kernel privilege on Chromebooks by Di ...
PDF
XS Boston 2008 Cache
PDF
Kernel Recipes 2014 - Writing Code: Keep It Short, Stupid!
PDF
Project ACRN GVT-d introduction and tutorial
PDF
The entropic principle: /dev/u?random and NetBSD by Taylor R Campbell
PDF
Exploit access root to kernel 2.6.32 2.6.36 privilege escalation exploit
PDF
Project ACRN CPU sharing BVT scheduler in ACRN hypervisor
QEMU in Cross building
[Ruxcon 2011] Post Memory Corruption Memory Analysis
Embedded Systems Conference 2014 Presentation
[Ruxcon] Breaking virtualization by switching the cpu to virtual 8086 mode
Linux: the first second
ACRN vMeet-Up EU 2021 - shared memory based inter-vm communication introduction
Tegra 186のu-boot & Linux
sponsorAVAST-VB2014
VM - Talk
Dave Gilbert - KVM and QEMU
[CB19] Attacking DRM subsystem to gain kernel privilege on Chromebooks by Di ...
XS Boston 2008 Cache
Kernel Recipes 2014 - Writing Code: Keep It Short, Stupid!
Project ACRN GVT-d introduction and tutorial
The entropic principle: /dev/u?random and NetBSD by Taylor R Campbell
Exploit access root to kernel 2.6.32 2.6.36 privilege escalation exploit
Project ACRN CPU sharing BVT scheduler in ACRN hypervisor
Ad

Similar to Malicious Hypervisor - Virtualization in Shellcodes by Adhokshaj Mishra (20)

ODP
Kvm and libvirt
PPTX
Virtualization concept slideshare
PPTX
Server virtualization
PPT
Virtual Pc Seminar
PPTX
Hypervisors
PPTX
Virtualization-Presentation-with-History
PPTX
Virtualization of computing and servers
PDF
Rmll Virtualization As Is Tool 20090707 V1.0
PDF
RMLL / LSM 2009
PPTX
Building a KVM-based Hypervisor for a Heterogeneous System Architecture Compl...
PDF
BitVisor Summit 11「2. BitVisor on Aarch64」
PPTX
Operating system Virtualization_NEW.pptx
PPTX
3. CPU virtualization and scheduling
ODP
S4 xen hypervisor_20080622
PPTX
Hardware support for efficient virtualization
PDF
Rootlinux17: Hypervisors on ARM - Overview and Design Choices by Julien Grall...
PPT
Unit II.ppt
PPT
Virtualization
PDF
AsiaBSDCon2023 - Hardening Emulated Devices in OpenBSD’s vmd(8) Hypervisor
PDF
The kvm virtualization way
Kvm and libvirt
Virtualization concept slideshare
Server virtualization
Virtual Pc Seminar
Hypervisors
Virtualization-Presentation-with-History
Virtualization of computing and servers
Rmll Virtualization As Is Tool 20090707 V1.0
RMLL / LSM 2009
Building a KVM-based Hypervisor for a Heterogeneous System Architecture Compl...
BitVisor Summit 11「2. BitVisor on Aarch64」
Operating system Virtualization_NEW.pptx
3. CPU virtualization and scheduling
S4 xen hypervisor_20080622
Hardware support for efficient virtualization
Rootlinux17: Hypervisors on ARM - Overview and Design Choices by Julien Grall...
Unit II.ppt
Virtualization
AsiaBSDCon2023 - Hardening Emulated Devices in OpenBSD’s vmd(8) Hypervisor
The kvm virtualization way
Ad

More from OWASP Delhi (20)

PDF
Getting Started With Hacking Android & iOS Apps? Tools, Techniques and resources
PDF
Securing dns records from subdomain takeover
PDF
Effective Cyber Security Report Writing
PPTX
Data sniffing over Air Gap
PPTX
UDP Hunter
PDF
Demystifying Container Escapes
PPTX
Automating WAF using Terraform
PPTX
Actionable Threat Intelligence
PDF
Threat hunting 101 by Sandeep Singh
PPTX
Owasp top 10 vulnerabilities
PPTX
Recon with Nmap
PPTX
Securing AWS environments by Ankit Giri
PDF
DMARC Overview
PDF
Cloud assessments by :- Aakash Goel
PDF
Pentesting Rest API's by :- Gaurang Bhatnagar
ODP
Wireless security beyond password cracking by Mohit Ranjan
PDF
IETF's Role and Mandate in Internet Governance by Mohit Batra
PPTX
ICS Security 101 by Sandeep Singh
PDF
Thwarting The Surveillance in Online Communication by Adhokshaj Mishra
ODP
Hostile Subdomain Takeover by Ankit Prateek
Getting Started With Hacking Android & iOS Apps? Tools, Techniques and resources
Securing dns records from subdomain takeover
Effective Cyber Security Report Writing
Data sniffing over Air Gap
UDP Hunter
Demystifying Container Escapes
Automating WAF using Terraform
Actionable Threat Intelligence
Threat hunting 101 by Sandeep Singh
Owasp top 10 vulnerabilities
Recon with Nmap
Securing AWS environments by Ankit Giri
DMARC Overview
Cloud assessments by :- Aakash Goel
Pentesting Rest API's by :- Gaurang Bhatnagar
Wireless security beyond password cracking by Mohit Ranjan
IETF's Role and Mandate in Internet Governance by Mohit Batra
ICS Security 101 by Sandeep Singh
Thwarting The Surveillance in Online Communication by Adhokshaj Mishra
Hostile Subdomain Takeover by Ankit Prateek

Recently uploaded (20)

PPTX
Funds Management Learning Material for Beg
PPTX
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
PPTX
QR Codes Qr codecodecodecodecocodedecodecode
PDF
Paper PDF World Game (s) Great Redesign.pdf
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PDF
The Internet -By the Numbers, Sri Lanka Edition
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
PDF
WebRTC in SignalWire - troubleshooting media negotiation
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
DOCX
Unit-3 cyber security network security of internet system
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
PPTX
Digital Literacy And Online Safety on internet
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
Funds Management Learning Material for Beg
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
QR Codes Qr codecodecodecodecocodedecodecode
Paper PDF World Game (s) Great Redesign.pdf
Introuction about ICD -10 and ICD-11 PPT.pptx
The Internet -By the Numbers, Sri Lanka Edition
Design_with_Watersergyerge45hrbgre4top (1).ppt
An introduction to the IFRS (ISSB) Stndards.pdf
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
Module 1 - Cyber Law and Ethics 101.pptx
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
WebRTC in SignalWire - troubleshooting media negotiation
Job_Card_System_Styled_lorem_ipsum_.pptx
Unit-3 cyber security network security of internet system
522797556-Unit-2-Temperature-measurement-1-1.pptx
Introuction about WHO-FIC in ICD-10.pptx
introduction about ICD -10 & ICD-11 ppt.pptx
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
Digital Literacy And Online Safety on internet
Tenda Login Guide: Access Your Router in 5 Easy Steps

Malicious Hypervisor - Virtualization in Shellcodes by Adhokshaj Mishra

  • 1. Malicious Hypervisor Virtualization in Shellcodes Adhokshaj Mishra
  • 2. Who am I?  A security fella  I am fascinated by malware, and cryptography  I work on offensive side of research  Get in touch: me@adhokshajmishraonline.in
  • 3. Agenda  Hypervisor: what, how and why?  Hypervisor in Linux  Capsule course on hypervisor (Intel VT-x, AMD-V, KVM)  Spawning a bare-bone VM  Injecting code in VM  I/O Between Host and Guest  Converting C Code to Shellcode
  • 4. Hypervisor: What?  A hypervisor is computer software, firmware, or hardware, that creates and runs virtual machines. A computer on which a hypervisor runs one or more virtual machines is called a host machine, and each virtual machine is called a guest machine.  Type-1 hypervisor: runs directly on bare metal. Example: ESXi  Type-2 hypervisor: runs on a conventional OS. Example: KVM
  • 5. Hypervisor: How?  Software emulation: Hypervisor emulates instructions of guest machine using instructions of host machine. These are slow, but can handle arbitrary architecture for guest machine.  Hardware-assisted: Hypervisor runs the instructions of guest machine directly on physical CPU using virtualization instructions provided by chip (vmenter, vmexit etc). Naturally, these are much faster than emulation based; but cannot handle architectures not supported by underlying chip.
  • 8. Hypervisor: Why?  Hypervisor makes it harder to debug the code running inside it.  Debugging is even harder if guest architecture is custom and undocumented.
  • 9. Capsule Course on Hypervisor  A virtual CPU starts up just like an actual CPU, i.e., it will start in 8086 aka real mode. You need to switch it to protected mode (x86), and then to long mode (x64). Mode switch is not mandatory if you keep yourself limited to instruction set of the mode you are going to use.  At startup, you need to allocate memory which will be used by guest machine. Guest machine will see it as ram, while host will see it as a memory buffer.  Registers must be set properly on startup, otherwise guest machine will fail. You need to load code in guest memory, set registers (at least instruction pointer) before running the guest.
  • 10. Capsule Course on Hypervisor  During execution, guest machine will raise events, which must be handled by the host.  For any I/O, virtual ports will be used by guest machine. Whenever guest machine attempts a write to port, or read from port, it triggers an event, which is passed to host. Host must handle the data transfer if any.
  • 11. Spawning a Bare-bone VM  int kvm = open(“/dev/kvm”, O_RDWR | O_CLOEXEC); int vmfd = ioctl(kvm, KVM_CREATE_VM, (unsigned long)0); int vcpufd = ioctl(vmfd, KVM_CREATE_CPU, (unsigned long)0);  All communication and configuration is done by IOCTL calls. These calls follow the following pattern: int ioctl(file_descriptor, command, parameter);
  • 12. Spawning a Bare-bone VM  Setting up memory: uint8_t *mem = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); The above call allocates a buffer of length 4KB, with read and write permissions. The allocated page will be anonymous (as in, not backed by any file).
  • 13. Spawning a Bare-bone VM  Setting up guest memory: struct kvm_userspace_memory_region region = { .slot = 0, .guest_phys_addr = 0x1000, .memory_size = 0x1000, .userspace_addr = (uint64_t)mem; }; int ret = ioctl(vmfd, KVM_SET_USER_MEMORY_REGION, &region);
  • 14. Why was guest physical address set to 0x1000, and not at, say, 0x10 or 0x1?
  • 15. Injecting Code in VM  It is as simple as copying code bytes into memory buffer before giving it to guest machine. Uint8_t code[] = {/*code bytes here*/}; memcpy(mem, code, sizeof(code));  NOTE: The code must be for 8086 CPU, otherwise guest machine will fail with illegal instruction fault and/or with bizarre results. If you want to switch modes, it must happen through 8086 code.
  • 16. Injecting Code in VM  Setting up segment registers: struct kvm_sregs sregs; ret = ioctl(vcpufd, KVM_GET_SREGS, &sregs); sregs.cs.base = 0; sregs.cs.selector = 0; ret = ioctl(vcpufd, KVM_SET_SREGS, &sregs);
  • 17. Injecting Code in VM  Setting up registers: struct kvm_regs regs = { .rip = 0x1000, .rax = 4, .rbx = 5, .rflags = 0x2, }; ret = ioctl(vcpufd, KVM_SET_REGS, &regs);
  • 18. Kicking VM to Life size_t mmap_size = ioctl(kvm, KVM_GET_CPU_MMAP_SIZE, NULL); kvm_run *run = mmap(NULL, mmap_size, PROT_READ | PRO_WRITE, MAP_SHARED, vcpufd, 0); while(1){ ret = ioctl(vcpufd, KVM_RUN, NULL); switch (run->exit_reason) { /*handle events here*/ } }
  • 19. I/O Between Host and Guest switch (run->exit_reason) { case KVM_EXIT_IO: if (run->io.direction == KVM_EXIT_IO_OUT && io.port == 0xabc) { char ch = *(((char*)run) + run->io.data_offset); } /*handle more cases here*/ }
  • 20. Demo of KVM Powered Hypervisor
  • 21. Conversion to Shellcode  gcc kvm.c –o kvm –save-temps –masm=intel  kvm.s contains assembly listing.  Remove all the extra clutter generated by compiler  Resolve addresses of all hardcoded values using JMP-CALL-POP method  Profit!