HyperDbg Debugger
Loading...
Searching...
No Matches
transparency.cpp File Reference

Measurements for debugger transparency. More...

#include "pch.h"

Functions

UINT64 TransparentModeRdtscDiffVmexit ()
 get the difference clock cycles between two rdtsc(s)
UINT64 TransparentModeRdtscVmexitTracing ()
 get the difference clock cycles between rdtsc+cpuid+rdtsc
INT TransparentModeCpuidTimeStampCounter (UINT64 *Average, UINT64 *StandardDeviation, UINT64 *Median)
 compute the average, standard deviation and median if rdtsc+cpuid+rdtsc
INT TransparentModeRdtscEmulationDetection (UINT64 *Average, UINT64 *StandardDeviation, UINT64 *Median)
 compute the average, standard deviation and median if rdtsc+rdtsc
BOOLEAN TransparentModeCheckHypervisorPresence (UINT64 *Average, UINT64 *StandardDeviation, UINT64 *Median)
 compute the average, standard deviation and median if rdtsc+cpuid+rdtsc
BOOLEAN TransparentModeCheckRdtscpVmexit (UINT64 *Average, UINT64 *StandardDeviation, UINT64 *Median)
 compute the average, standard deviation and median if rdtsc+rdtsc

Detailed Description

Measurements for debugger transparency.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.1
Date
2020-07-07

Function Documentation

◆ TransparentModeCheckHypervisorPresence()

BOOLEAN TransparentModeCheckHypervisorPresence ( UINT64 * Average,
UINT64 * StandardDeviation,
UINT64 * Median )

compute the average, standard deviation and median if rdtsc+cpuid+rdtsc

detects the presence of hypervisor

Parameters
Averagea pointer to save average on it
StandardDeviationa pointer to standard deviation average on it
Mediana pointer to save median on it
Returns
int
185{
186 //
187 // Check whether the hypervisor is detected or not
188 //
189 if (TransparentModeCpuidTimeStampCounter(Average, StandardDeviation, Median))
190 {
191 ShowMessages("hypervisor detected\n");
192 return TRUE;
193 }
194 else
195 {
196 ShowMessages("hypervisor not detected\n");
197 return FALSE;
198 }
199}
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
double Median(vector< double > Cases)
get the median of a vector
Definition gaussian-rng.cpp:22
T Average(const vector< T > &vec)
get the average of a vector
Definition gaussian-rng.cpp:53
INT TransparentModeCpuidTimeStampCounter(UINT64 *Average, UINT64 *StandardDeviation, UINT64 *Median)
compute the average, standard deviation and median if rdtsc+cpuid+rdtsc
Definition transparency.cpp:97

◆ TransparentModeCheckRdtscpVmexit()

BOOLEAN TransparentModeCheckRdtscpVmexit ( UINT64 * Average,
UINT64 * StandardDeviation,
UINT64 * Median )

compute the average, standard deviation and median if rdtsc+rdtsc

detects the presence of rdtsc/p vm-exits

Parameters
Averagea pointer to save average on it
StandardDeviationa pointer to standard deviation average on it
Mediana pointer to save median on it
Returns
int
215{
216 //
217 // Check whether the system emulating rdtsc/p or not
218 //
220 {
221 ShowMessages("rdtsc/p emulation detected\n");
222 return TRUE;
223 }
224 else
225 {
226 ShowMessages("rdtsc/p emulation not detected\n");
227 return FALSE;
228 }
229}
INT TransparentModeRdtscEmulationDetection(UINT64 *Average, UINT64 *StandardDeviation, UINT64 *Median)
compute the average, standard deviation and median if rdtsc+rdtsc
Definition transparency.cpp:139

◆ TransparentModeCpuidTimeStampCounter()

INT TransparentModeCpuidTimeStampCounter ( UINT64 * Average,
UINT64 * StandardDeviation,
UINT64 * Median )

compute the average, standard deviation and median if rdtsc+cpuid+rdtsc

Parameters
Averagea pointer to save average on it
StandardDeviationa pointer to standard deviation average on it
Mediana pointer to save median on it
Returns
INT
100{
101 double Avg = 0;
102 double MeasuredTime = 0;
103 vector<double> Results;
104
105 for (int i = 0; i < TestCount; i++)
106 {
107 MeasuredTime = (double)TransparentModeRdtscDiffVmexit();
108 Avg = Avg + MeasuredTime;
109
110 Results.push_back(MeasuredTime);
111
112 /*
113 ShowMessages("(%d) Measured time : %d\n", i, MeasuredTime);
114 */
115 }
116
117 if (Average != NULL && StandardDeviation != NULL && Median != NULL)
118 {
119 //
120 // Compute the average and variance
121 //
122 GuassianGenerateRandom(Results, Average, StandardDeviation, Median);
123 }
124
125 Avg = Avg / TestCount;
126 return (Avg < 1000 && Avg > 0) ? FALSE : TRUE;
127}
VOID GuassianGenerateRandom(vector< double > Data, UINT64 *AverageOfData, UINT64 *StandardDeviationOfData, UINT64 *MedianOfData)
Calculate and generate random gaussian number.
Definition gaussian-rng.cpp:158
#define TestCount
Number of tests for each instruction sets.
Definition transparency.h:23
UINT64 TransparentModeRdtscDiffVmexit()
get the difference clock cycles between two rdtsc(s)
Definition transparency.cpp:22

◆ TransparentModeRdtscDiffVmexit()

UINT64 TransparentModeRdtscDiffVmexit ( )

get the difference clock cycles between two rdtsc(s)

Returns
UINT64
23{
24 UINT64 Ret, Ret2;
25 INT CpuidResult[4] = {0};
26
27 //
28 // GCC
29 //
30 // __asm__ volatile("rdtsc" : "=a" (eax), "=d" (edx));
31 // ret = ((unsigned long long)eax) | (((unsigned long long)edx) << 32);
32
33 //
34 // Win32
35 //
36 Ret = CpuReadTsc();
37
38 /* vm exit forced here. it uses: eax = 0; cpuid; */
39
40 //
41 // GCC
42 //
43 //__asm__ volatile("cpuid" : /* no output */ : "a"(0x00));
44
45 //
46 // WIN32
47 //
48 CpuCpuId(CpuidResult, 0);
49
50 //
51 // GCC
52 //
53 // __asm__ volatile("rdtsc" : "=a" (eax), "=d" (edx));
54 // ret2 = ((unsigned long long)eax) | (((unsigned long long)edx) << 32);
55
56 //
57 // WIN32
58 //
59 Ret2 = CpuReadTsc();
60
61 return Ret2 - Ret;
62}
UINT64 CpuReadTsc(VOID)
Read Time-Stamp Counter.
Definition PlatformIntrinsics.c:295
VOID CpuCpuId(INT32 *CpuInfo, INT32 FunctionId)
Execute CPUID.
Definition PlatformIntrinsics.c:255
int INT
Definition BasicTypes.h:43

◆ TransparentModeRdtscEmulationDetection()

INT TransparentModeRdtscEmulationDetection ( UINT64 * Average,
UINT64 * StandardDeviation,
UINT64 * Median )

compute the average, standard deviation and median if rdtsc+rdtsc

Parameters
Averagea pointer to save average on it
StandardDeviationa pointer to standard deviation average on it
Mediana pointer to save median on it
Returns
INT
142{
143 double Avg = 0;
144 double MeasuredTime = 0;
145 vector<double> Results;
146
147 for (int i = 0; i < TestCount; i++)
148 {
149 MeasuredTime = (double)TransparentModeRdtscVmexitTracing();
150 Avg = Avg + MeasuredTime;
151
152 Results.push_back(MeasuredTime);
153
154 /*
155 ShowMessages("(%d) Measured time : %d\n", i, MeasuredTime);
156 */
157 }
158
159 if (Average != NULL && StandardDeviation != NULL && Median != NULL)
160 {
161 //
162 // Compute the average and variance
163 //
164 GuassianGenerateRandom(Results, Average, StandardDeviation, Median);
165 }
166
167 Avg = Avg / TestCount;
168 return (Avg < 750 && Avg > 0) ? FALSE : TRUE;
169}
UINT64 TransparentModeRdtscVmexitTracing()
get the difference clock cycles between rdtsc+cpuid+rdtsc
Definition transparency.cpp:70

◆ TransparentModeRdtscVmexitTracing()

UINT64 TransparentModeRdtscVmexitTracing ( )

get the difference clock cycles between rdtsc+cpuid+rdtsc

Returns
UINT64
71{
72 UINT64 Ret, Ret2;
73
74 //
75 // WIN32
76 //
77 Ret = CpuReadTsc();
78
79 //
80 // WIN32
81 //
82 Ret2 = CpuReadTsc();
83
84 return Ret2 - Ret;
85}