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

headers for test functions More...

Go to the source code of this file.

Macros

#define TestCount   1000
 Number of tests for each instruction sets.

Functions

void GuassianGenerateRandom (vector< double > Data, UINT64 *AverageOfData, UINT64 *StandardDeviationOfData, UINT64 *MedianOfData)
 Calculate and generate random gaussian number.
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
double Randn (double mu, double sigma)
 random generator based on calculations
double Median (vector< double > Cases)
 get the median of a vector
unsigned long long TransparentModeRdtscDiffVmexit ()
 get the difference clock cycles between two rdtsc(s)
unsigned long long TransparentModeRdtscVmexitTracing ()
 get the difference clock cycles between rdtsc+cpuid+rdtsc

Detailed Description

headers for test functions

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

Macro Definition Documentation

◆ TestCount

#define TestCount   1000

Number of tests for each instruction sets.

used to generate test cases for rdts+cpuid+rdtsc and rdtsc+rdtsc commands

Function Documentation

◆ GuassianGenerateRandom()

void GuassianGenerateRandom ( vector< double > Data,
UINT64 * AverageOfData,
UINT64 * StandardDeviationOfData,
UINT64 * MedianOfData )

Calculate and generate random gaussian number.

Parameters
Data
AverageOfData
StandardDeviationOfData
MedianOfData
159{
160 vector<double> FinalData;
161 INT CountOfOutliers = 0;
162 double Medians;
163 double Mad;
164 double StandardDeviation;
165 double DataAverage;
166 double DataMedian;
167
168 vector<double> OriginalData = Data;
169 vector<double> ChangableData = std::move(Data);
170
171 Mad = MedianAbsoluteDeviationTest(ChangableData);
172 Medians = Median(OriginalData);
173
174 for (auto item : OriginalData)
175 {
176 if (item > (3 * Mad) + Medians || item < -(3 * Mad) + Medians)
177 {
178 CountOfOutliers++;
179 }
180 else
181 {
182 FinalData.push_back(item);
183 }
184 }
185
186 StandardDeviation = CalculateStandardDeviation(FinalData);
187 DataAverage = Average(FinalData);
188 DataMedian = Median(FinalData);
189
190 //
191 // Set the values to return
192 //
193 *AverageOfData = (UINT64)DataAverage;
194
195 //
196 // We add 5 to the standard deviation because this value might be
197 // 0 or 1 so we need more variance
198 //
199 *StandardDeviationOfData = (UINT64)StandardDeviation + 5;
200 *MedianOfData = (UINT64)DataMedian;
201
202 //
203 // ShowMessages("variance : %f\n", StandardDeviation);
204 // ShowMessages("mean : %f\n", DataAverage);
205 // ShowMessages("count of outliers : %d\n", CountOfOutliers);
206 //
207 //
208 // for (int i = 0; i < 10000; i++)
209 // {
210 // ShowMessages("final Random Time Stamp : %d\n", (int) Randn(DataAverage,
211 // StandardDeviation));
212 // _getch();
213 // }
214 //
215}
int INT
Definition BasicTypes.h:43
T CalculateStandardDeviation(const std::vector< T > &v)
get the standard deviation of elements
Definition gaussian-rng.cpp:78
double Median(vector< double > Cases)
get the median of a vector
Definition gaussian-rng.cpp:22
double MedianAbsoluteDeviationTest(vector< double > Data)
get the Median Absolute Deviation (MAD) Test
Definition gaussian-rng.cpp:97
T Average(const vector< T > &vec)
get the average of a vector
Definition gaussian-rng.cpp:53
Start of Optional Data
Definition script_buffer.hex.txt:8

◆ Median()

double Median ( vector< double > Cases)

get the median of a vector

Parameters
Casesall the elements
Returns
double median of elements
23{
24 SIZE_T Size = Cases.size();
25
26 if (Size == 0)
27 {
28 return 0; // Undefined, really
29 }
30 else
31 {
32 sort(Cases.begin(), Cases.end());
33 if (Size % 2 == 0)
34 {
35 return (Cases[Size / 2 - 1] + Cases[Size / 2]) / 2;
36 }
37 else
38 {
39 return Cases[Size / 2];
40 }
41 }
42}

◆ Randn()

double Randn ( double mu,
double sigma )

random generator based on calculations

Parameters
mu
sigma
Returns
double random number in the range of gaussian curve
122{
123 double U1, U2, W, mult;
124 static double X1, X2;
125 static INT Call = 0;
126
127 if (Call == 1)
128 {
129 Call = !Call;
130 return (mu + sigma * (double)X2);
131 }
132
133 do
134 {
135 U1 = -1 + ((double)rand() / RAND_MAX) * 2;
136 U2 = -1 + ((double)rand() / RAND_MAX) * 2;
137 W = pow(U1, 2) + pow(U2, 2);
138 } while (W >= 1 || W == 0);
139
140 mult = sqrt((-2 * log(W)) / W);
141 X1 = U1 * mult;
142 X2 = U2 * mult;
143
144 Call = !Call;
145
146 return (mu + sigma * (double)X1);
147}

◆ 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
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

◆ TransparentModeRdtscDiffVmexit()

unsigned long long 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

◆ TransparentModeRdtscVmexitTracing()

unsigned long long 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}