-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindcave.h
82 lines (68 loc) · 1.72 KB
/
findcave.h
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
/*
* =====================================================================================
*
* Filename: findcave.h
*
* Description: a header file to find the code cave
*
* Version: 1.0
* Created: 11/27/2020 09:59:22 PM
* Revision: none
* Compiler: gcc
*
* Author: SPOOKY (@spooky_sec), https://discord.gg/UTVBvJs
* Organization:
*
* =====================================================================================
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string.h>
#include <time.h>
#include <errno.h>
typedef struct {
long file_size;
int size;
int start;
int end;
int bytes_written;
} code_cave_t;
int find_code_cave(char *bin_path, code_cave_t *cc) {
int fd = open(bin_path, O_RDONLY, S_IRUSR | S_IWUSR);
if (fd == -1) {
printf("File: %s not found!\n", bin_path);
return -1;
}
int current_cave_size = 0, byte = 0, currentByte = 0;
cc->file_size = 0;
cc->size = 0;
cc->bytes_written = 0;
char *fileMem = NULL;
struct stat sb;
if (fstat(fd, &sb) == -1) {
printf("Error with fstat\n");
return -1;
}
cc->file_size = sb.st_size;
fileMem = (char *)mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
for (byte = 0; byte < cc->file_size; byte++) {
currentByte = fileMem[byte];
if ((int)currentByte == 0x00) {
current_cave_size++;
} else {
if (current_cave_size > 0) {
if (current_cave_size > cc->size) {
cc->size = current_cave_size;
cc->end = byte;
cc->start = byte - current_cave_size;
}
current_cave_size = 0;
}
}
}
return 0;
}