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}
unsigned __int64 UINT64
Definition BasicTypes.h:21
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}
#define RAND_MAX
Maximum value that can be returned by the rand function.
Definition Transparency.h:35

◆ 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
197{
198 //
199 // Check whether the hypervisor is detected or not
200 //
201 if (TransparentModeCpuidTimeStampCounter(Average, StandardDeviation, Median))
202 {
203 ShowMessages("hypervisor detected\n");
204 return TRUE;
205 }
206 else
207 {
208 ShowMessages("hypervisor not detected\n");
209 return FALSE;
210 }
211}
#define TRUE
Definition BasicTypes.h:55
#define FALSE
Definition BasicTypes.h:54
VOID ShowMessages(const char *Fmt,...)
Show messages.
Definition libhyperdbg.cpp:96
int TransparentModeCpuidTimeStampCounter(UINT64 *Average, UINT64 *StandardDeviation, UINT64 *Median)
compute the average, standard deviation and median if rdtsc+cpuid+rdtsc
Definition transparency.cpp:109

◆ 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
227{
228 //
229 // Check whether the system emulating rdtsc/p or not
230 //
232 {
233 ShowMessages("rdtsc/p emulation detected\n");
234 return TRUE;
235 }
236 else
237 {
238 ShowMessages("rdtsc/p emulation not detected\n");
239 return FALSE;
240 }
241}
int TransparentModeRdtscEmulationDetection(UINT64 *Average, UINT64 *StandardDeviation, UINT64 *Median)
compute the average, standard deviation and median if rdtsc+rdtsc
Definition transparency.cpp:151

◆ TransparentModeRdtscDiffVmexit()

unsigned long long TransparentModeRdtscDiffVmexit ( )

get the difference clock cycles between two rdtsc(s)

Returns
unsigned long long
23{
24 unsigned long long ret, ret2;
25 int cpuid_result[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 = __rdtsc();
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 __cpuid(cpuid_result, 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 = __rdtsc();
60
61 return ret2 - ret;
62}

◆ TransparentModeRdtscVmexitTracing()

unsigned long long TransparentModeRdtscVmexitTracing ( )

get the difference clock cycles between rdtsc+cpuid+rdtsc

Returns
unsigned long long
71{
72 unsigned long long ret, ret2;
73
74 //
75 // GCC
76 //
77 // __asm__ volatile("rdtsc" : "=a" (eax), "=d" (edx));
78 // ret = ((unsigned long long)eax) | (((unsigned long long)edx) << 32);
79
80 //
81 // WIN32
82 //
83 ret = __rdtsc();
84
85 //
86 // GCC
87 //
88 // __asm__ volatile("rdtsc" : "=a"(eax), "=d"(edx));
89 // ret2 = ((unsigned long long)eax) | (((unsigned long long)edx) << 32);
90
91 //
92 // WIN32
93 //
94 ret2 = __rdtsc();
95
96 return ret2 - ret;
97}