• Stars
    star
    122
  • Rank 292,031 (Top 6 %)
  • Language
  • Created over 4 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PoC TSC offsetting in KVM

BetterTiming

This is a small project of mine aiming to improve CPU timing in KVM SVM implementation to bypass certain anti-VM checks. It saves vm-exit times and then uses them to offest TSC.

screenshot Pafish passing CPU timing checks screenshot2 vmcheck kernel-mode driver passing checks

Disclaimer: Testing was done only in an isolated environment. Doing such a change might introduce unwanted side effects to the guest OS.

How to apply the patch

You will need to recompile Linux kernel from source. If you are on a distribution that has a custom build system, it might be easier to use it for building the kernel. The patch is currently for version 5.9.2, but you should be able to easily modify it for any new version of the kernel.

1.) Download and extract kernel source

wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.9.2.tar.xz
tar -xf linux-5.9.2.tar.xz
cd linux-5.9.2

2.) Download the patch

git clone https://github.com/SamuelTulach/BetterTiming
mv BetterTiming/timing.patch timing.patch

3.) Apply patch

patch -s -p0 < timing.patch

4.) Build and install the kernel

How it works

The concept of this is extremely simple. I have added additional variables to kvm_vcpu struct.

u64 last_exit_start;
u64 total_exit_time;

Then created a wrapper function around vcpu_enter_guest (which is where VM-exit is handled). This wrapper would then save the time it took for the vcpu to exit if the exit reason matches specified value.

static int vcpu_enter_guest(struct kvm_vcpu *vcpu) 
{	
	int result;
	u64 differece;
	
	vcpu->last_exit_start = rdtsc();

	result = vcpu_enter_guest_real(vcpu);

	if (vcpu->run->exit_reason == 123) 
	{
		differece = rdtsc() - vcpu->last_exit_start;
		vcpu->total_exit_time += differece;
	}

	return result;
}

Added a VM-exit handler for RDTSC instruction which takes those values.

static int handle_rdtsc_interception(struct vcpu_svm *svm) 
{
	u64 differece;
	u64 final_time;
	u64 data;
	
	differece = rdtsc() - svm->vcpu.last_exit_start;
	final_time = svm->vcpu.total_exit_time + differece;

	data = rdtsc() - final_time;

	svm->vcpu.run->exit_reason = 123;
	svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & -1u;
	svm->vcpu.arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;

	skip_emulated_instruction(&svm->vcpu);

	return 1;
}

And of course modified kvm_get_msr_common to also utilise this patch.

case MSR_IA32_TSC: {
	differece = rdtsc() - vcpu->last_exit_start;
	final_time = vcpu->total_exit_time + differece;

	msr_info->data = rdtsc() - final_time;

	vcpu->run->exit_reason = 123;
	break;
}

Then added custom exit reason to other instruction with unconditional exit reason (such as INVD and XSETBV).

Troubleshooting

  • Q: VM does not seem to boot after the patch!
  • A: Double check if the patch was applied correctly and that your CPU supports RDTSC instruction.
  • Q: I am still getting detected by some rootkits!
  • A: Check you are on AMD processor. Check that you have CPU pinning enabled and that you have free cores that are not pinned to the VM. In case CPU pinning is already in place, you can try increasing the difference in vcpu_enter_guest. Also you might want to specify exit reason for other instructions then just CPUID.
  • Q: My PC does not boot after applying the patch!
  • A: The patch does not change anything unless you boot a VM. Please check that you have compiled the kernel with correct settings.
  • Q: I installed the new kernel, but I can see only blinking cursor!
  • A: If you have NVIDIA GPU, make sure that the driver did not fuck itself after installation of a custom kernel. If you are on Arch Linux for example, you can use package nvidia-dkms to install the proprietary driver.

More Repositories

1

VirusTotalUploader

C# Open-Source Winforms application for uploading files to VirusTotal
C#
1,128
star
2

mutante

Kernel-mode Windows HWID spoofer
C++
524
star
3

efi-memory

PoC EFI runtime driver for memory r/w & kdmapper fork
C++
476
star
4

rainbow

Hide SMBIOS/disk/NIC serials from EFI bootkit
C
270
star
5

negativespoofer

PoC HWID spoofer that runs in EFI
C++
269
star
6

LightHook

Single-header, minimalistic, cross-platform hook library written in pure C
C++
246
star
7

RwxMeme

State of the art DLL injector that took 20 minutes to make
C++
192
star
8

tpm-spoofer

Simple proof of concept kernel mode driver hooking tpm.sys dispatch to randomize any public key reads
C
179
star
9

nullmap

Using CVE-2023-21768 to manual map kernel mode driver
C
171
star
10

memhv

Minimalistic AMD-V/SVM hypervisor with memory introspection capabilities
C++
152
star
11

meme-rw

Archive R/W into any protected process by changing the value of KTHREAD->PreviousMode
C++
146
star
12

OverlayCord

Simple proof of concept showing how you can abuse Discord's in-game internal module to draw on top of the game (even if the game is in fullscreen) from an external application without modifying any Discord files or loading its modules.
C++
139
star
13

SecureFakePkg

Simple EFI runtime driver that hooks GetVariable function and returns data expected by Windows to make it think that it's running with secure boot enabled (faking secure boot)
C
125
star
14

PwnedBoot

Using Windows' own bootloader as a shim to bypass Secure Boot
C
103
star
15

voidmap

Using CVE-2021-40449 to manual map kernel mode driver
C
97
star
16

CanetisRadar

Open-source application for detecting sound direction using 7.1 audio device in games
C#
93
star
17

PatchBoot

Guide for patching AMI Aptio V UEFI firmware to circumvent Secure Boot checks
87
star
18

windowhide

Hide external overlay by using SetWindowDisplayAffinity
C++
86
star
19

DirectPageManipulation

A basic demonstration of directly overwriting paging structures for physical memory r/w and interprocess memory copy
C++
75
star
20

ImGui-AppKit

Project template for single-window GUI apps using Dear ImGui
C++
44
star
21

eac_cr3_shuffle

C++
41
star
22

ida-unity-pdb-downloader

Simple IDA Pro plugin to download Unity debug symbols from their symbol server
C++
41
star
23

EfiDump

PoC EFI runtime driver for direct Windows interprocess memory copy
C
37
star
24

InjectMouseInputExample

C/C++ example of InjectMouseInput function
C++
30
star
25

MemoryGuard

Experiment with PAGE_GUARD protection to hide memory from other processes
C
29
star
26

human-mouse

Adopted SRL-6 mouse movement to C++
C++
29
star
27

memdrv

C++
26
star
28

GetDeviceInterfacesMemoryLeak

Small memory leak PoC that is happening in IopGetDeviceInterfaces
C++
24
star
29

SaberHighlight

Beat Saber mod enabling NVIDIA Highlights functionality
C#
24
star
30

be_shellcode_dump

December 2023 BattlEye shellcode dump
C
24
star
31

SoundReplacer

BSIPA mod for replacing hit sounds, menu music, click sounds and much more!
C#
19
star
32

EvilKaspersky

C++
19
star
33

vmcheck

C
18
star
34

OcuFix

Simple mod to automatically disable ASW and change services/runtime priority
C#
16
star
35

RecoilTime

A simple program made in C# for reducing recoil in games under Windows environment
C#
14
star
36

EasyUefi

Visual Studio template for GNU-EFI
C
14
star
37

eft-profile-api

Reversing the newly added EFT feature that allows you to view other players' stats
C#
13
star
38

EmuAuth

KeyAuth server emulator
C++
11
star
39

DiscordLeech

Read Discord user info from process memory
C#
10
star
40

esea-control

Simple program to temporarly disable ESEA anti-cheat
C#
9
star
41

lrdp

8
star
42

InactiveTitlebarPatch

Simple registry patch to turn inactive title bar color dark
8
star
43

ByeMouse

C#
5
star
44

obs-dda

C
3
star
45

EasyDump

C#
3
star
46

PowerControl

C#
3
star
47

MapPasswordTool

TM2020 tool to remove password lock for editing from maps
C#
2
star
48

virtmgr

C++
2
star
49

OneplusDebloat

Remove crappy useless apps in Oxygen OS (both 3rd party and Google)
Shell
2
star
50

NekoSwap

C++
1
star