forked from r3dy-malz/XOR_CUDA_Unpacking_C
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.cu
More file actions
134 lines (107 loc) · 3.89 KB
/
Copy pathkernel.cu
File metadata and controls
134 lines (107 loc) · 3.89 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
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#define MAX_SHELLCODE 10000000 // EDIT WITH THE SIZE OF YOUR SHELLCODE
unsigned char file_data[MAX_SHELLCODE] = "ST_ART_HE_R_E";
unsigned char* readFile(const char* file_path, long* file_size) {
FILE* file = fopen(file_path, "r");
if (file == NULL) {
perror("Error opening file");
return NULL;
}
fseek(file, 0, SEEK_END);
*file_size = ftell(file);
fseek(file, 0, SEEK_SET);
unsigned char* file_data = (unsigned char*)malloc(*file_size + 1);
if (file_data == NULL) {
perror("Error allocating memory");
fclose(file);
return NULL;
}
fread(file_data, 1, *file_size, file);
file_data[*file_size] = '\0';
fclose(file);
return file_data;
}
__global__ void xor (char* file_data_xored, char* file_data) {
int x = threadIdx.x + blockIdx.x * blockDim.x;
file_data_xored[x] = file_data[x] ^ 0x43;
}
cudaError_t xor_with_cuda(unsigned char* file_data_xored, unsigned char* file_data, long file_size) {
cudaError_t cudaStatus;
char* dev_file_data;
char* dev_file_data_xored;
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed!");
goto Error;
}
cudaStatus = cudaMalloc((void**)&dev_file_data, file_size * sizeof(char));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}
cudaStatus = cudaMalloc((void**)&dev_file_data_xored, file_size * sizeof(char));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}
cudaStatus = cudaMemcpy(dev_file_data, file_data, file_size * sizeof(char), cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}
int blockSize = 256;
int numBlocks = (file_size + blockSize - 1) / blockSize;
xor << <numBlocks, blockSize >> > (dev_file_data_xored, dev_file_data);
cudaStatus = cudaMemcpy(file_data_xored, dev_file_data_xored, file_size * sizeof(char), cudaMemcpyDeviceToHost);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
goto Error;
}
Error:
printf("test");
cudaFree(dev_file_data);
cudaFree(dev_file_data_xored);
return cudaStatus;
}
void xor_with_cpu(unsigned char* file_data, long file_size) {
if (file_data != NULL) {
for (int x = 0; x < file_size; x++) {
file_data[x] = file_data[x] ^ 0x43;
}
}
}
int main() {
long file_size = MAX_SHELLCODE;
unsigned char* file_data = (unsigned char*)malloc(file_size + 1);
unsigned char* file_data_xored = (unsigned char*)malloc(file_size + 1);
// CPU
auto start_time_cpu = std::chrono::high_resolution_clock::now();
xor_with_cpu(file_data, file_size); ///
auto end_time_cpu = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed_time_cpu = end_time_cpu - start_time_cpu;
printf("Temps d'exécution CPU : %f millisecondes\n", elapsed_time_cpu.count());
// GPU
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start);
xor_with_cuda(file_data_xored, file_data, file_size); ///
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float elapsed_time_gpu;
cudaEventElapsedTime(&elapsed_time_gpu, start, stop);
printf("Temps d'exécution GPU : %f millisecondes\n", elapsed_time_gpu);
cudaEventDestroy(start);
cudaEventDestroy(stop);
free(file_data_xored);
return 0;
}