-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsnet-bin-parser.c
150 lines (121 loc) · 3.86 KB
/
snet-bin-parser.c
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
/*
snet-extractor
-----------------------------------------
Anestis Bechtsoudis <[email protected]>
Copyright 2017 by CENSUS S.A. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <limits.h>
static char *kMetadataFile = "metadata_flags.txt";
static char *kPayloadFile = "payload.snet";
static uint8_t *mapFile(char *fileName, off_t *fileSz, int *fd, bool enableWrite) {
if ((*fd = open(fileName, enableWrite ? O_RDWR : O_RDONLY)) == -1) {
printf("[-] Couldn't open() '%s' file in R/O mode (%s)", fileName, strerror(errno));
return NULL;
}
struct stat st;
if (fstat(*fd, &st) == -1) {
printf("[-] Couldn't stat() the '%s' file (%s)", fileName, strerror(errno));
close(*fd);
return NULL;
}
uint8_t *buf;
int flags = PROT_READ;
if (enableWrite) {
flags |= PROT_WRITE;
}
if ((buf = mmap(NULL, st.st_size, flags, MAP_PRIVATE, *fd, 0)) == MAP_FAILED) {
printf("[-] Couldn't mmap() the '%s' file (%s)\n", fileName, strerror(errno));
close(*fd);
return NULL;
}
*fileSz = st.st_size;
return buf;
}
static bool writeToFd(int fd, const uint8_t * buf, size_t fileSz) {
size_t writtenSz = 0;
while (writtenSz < fileSz) {
ssize_t sz = write(fd, &buf[writtenSz], fileSz - writtenSz);
if (sz < 0 && errno == EINTR)
continue;
if (sz < 0)
return false;
writtenSz += sz;
}
return true;
}
static bool writeBufToFile(const char *fileName, const uint8_t * buf, size_t fileSz) {
int fd = open(fileName, O_CREAT | O_WRONLY, 0644);
if (fd == -1) {
printf("[-] Couldn't open '%s' for write (%s)\n", fileName, strerror(errno));
return false;
}
if (writeToFd(fd, buf, fileSz) == false) {
printf("[-] Couldn't write '%zu' bytes to file '%s' (fd='%d') (%s)\n",
fileSz, fileName, fd, strerror(errno));
unlink(fileName);
close(fd);
return false;
}
return true;
}
uint32_t read4Bytes(uint8_t *cursor) {
uint32_t x;
x = 0xFF & cursor[3];
x |= (0xFF & cursor[2]) << 8;
x |= (0xFF & cursor[1]) << 16;
x |= (uint32_t)(0xFF & cursor[0]) << 24;
return x;
}
int main(int argc, char **argv) {
int ret = EXIT_FAILURE;
if (argc != 3) {
printf("%s <input_snet.flags> <output_dir>\n", argv[0]);
return ret;
}
off_t fileSz = 0;
int srcfd = -1;
uint8_t *buf = NULL;
buf = mapFile(argv[1], &fileSz, &srcfd, false);
if (buf == NULL) {
printf("[-] Open & map failed\n");
return ret;
}
uint32_t sigBlobSize = read4Bytes(buf);
char outputFile[PATH_MAX] = { 0 };
snprintf(outputFile, sizeof(outputFile), "%s/%s", argv[2], kMetadataFile);
if (!writeBufToFile(outputFile, buf + 4, sigBlobSize)) {
printf("[-] Failed to write snet metadata file");
goto fini;
}
printf("[*] Successfully extracted '%s'\n", kMetadataFile);
memset(outputFile, 0, sizeof(outputFile));
snprintf(outputFile, sizeof(outputFile), "%s/%s", argv[2], kPayloadFile);
if (!writeBufToFile(outputFile, buf + 4 + sigBlobSize, fileSz - 4 - sigBlobSize)) {
printf("[-] Failed to write snet flags file");
goto fini;
}
printf("[*] Successfully extracted '%s'\n", kPayloadFile);
ret = EXIT_SUCCESS;
fini:
close(srcfd);
return ret;
}