-
Notifications
You must be signed in to change notification settings - Fork 8
/
kuymak.c
298 lines (273 loc) · 8.97 KB
/
kuymak.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
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
/*
* Kuymak
* Copyright (C) 2022 Blue DeviL<bluedevil.SCT@gmail.com>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* KUYMAK
* A tasty cross-platform tool from Blacksea to debug shellcode!
*
* How to compile:
* If you have installed GCC or MingW on your host platform, you can compile
* as simple as with this command:
*
* $ gcc -Wall kuymak.c -o kuymak
*
* You can compile for Windows if you installed cross-compilers.
* Windows x86_64:
* $ x86_64-w64-mingw32-gcc -Wall kuymak.c -o kuymak.exe
* Windows x86_32:
* $ i686-w64-mingw32-gcc -Wall kuymak.c -o kuymak.exe
*
* How to run:
* Save your shellcode to a binary file and run as easily as:
* $ ./kuymak -b shellcode.bin
*
* You can run your shellcode with "\x" specifier directly from command line
* $ ./kuymak -c "\x48\x83\xEC\x28\x48 <snipped> \x00\x48\x8D"
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h> // getopt defined here
#ifdef __linux__
#include <sys/mman.h> // mmap defined here
#elif _WIN64
#include <windows.h>
// #include <WinBase.h>
#elif _WIN32
#include <windows.h>
#elif __APPLE__
#include <sys/mman.h>
#endif
const char *ver = "0.4.0";
const char *date = "06/05/2023";
unsigned char *shellcode;
void usage()
{
fprintf(stdout, "KUYMAK v%s\n\n", ver);
fprintf(stdout, "[*] USAGE:\n");
fprintf(stdout, "\tkuymak -b shellcode.bin\n");
fprintf(stdout, "\tkuymak -c \"\\x0f\\x01\\xf8\\xe8\\x05\\x00\\x00\\x00"
"\\x0f\\x01\\xf8\\x48\\xcf\"\n\n");
fprintf(stdout, "[*] Options:\n");
fprintf(stdout, "\t-b\tshellcode as a binary\n");
fprintf(stdout, "\t-c\tshellcode as char array\n");
fprintf(stdout, "\t-h\tprints this help\n");
return;
}
void print_banner(void)
{
char *bannerfmt =
" __________________________________________________________________\n"
" | ...-... .'\\\\ _ __ _ |\n"
" | ./sh0mmm0hs+\\ o | | / / | | |\n"
" | ./hmPS1SSSSSSm0y/: | |/ / _ _ _ _ _ __ ___ __ _| | __ |\n"
" | +mq00000000000pm+ | \\| | | | | | | '_ ` _ \\ / _` | |/ / |\n"
" | .mq0001010011000pm. | |\\ \\ |_| | |_| | | | | | | (_| | < |\n"
" +=| .mq0001000011000pm. \\_| \\_/\\__,_|\\__, |_| |_| |_|\\__,_|_|\\_\\ |=+\n"
" | | +mq00101010000pm+ __/ | | |\n"
" | | //yomooooooooom0+. |___/ github.com/blue-devil/ | |\n"
" | | \\ y+sh0mmm0hs/. | |\n"
" | | `-' '''-''' To kuymak or not to kuymak, that's the question! | |\n"
" | |__________________________________________________________________| |\n"
" | ____________________________________________________ |\n"
" | -+| @author : Blue DeviL <bluedevil.SCT@gmail.com> |+- |\n"
" | | | @version : %s | | |\n"
" +<===>+--| @date : %s |--+<===>+\n"
" | | @license : GPLv3 | |\n"
" -+| @info : Cross-platform shellcode runner |+-\n"
" |____________________________________________________|\n\n";
char banner[2048];
sprintf(banner, bannerfmt, ver, date);
fprintf(stdout, "%s", banner);
}
/**
* map_shellcode - maps shellcode into the memory's executable space
* @file_size: size of shellcode
*
* Return value: None
*/
void *map_shellcode(ssize_t file_size)
{
#ifdef __linux__
// allocate an executable/readable/writable area inside memory
void *pSC = mmap(0, sizeof(shellcode),
PROT_EXEC | PROT_WRITE | PROT_READ,
MAP_ANONYMOUS | MAP_PRIVATE,
-1, 0);
if (pSC == MAP_FAILED)
{
perror("mmap");
free(shellcode);
exit(EXIT_FAILURE);
}
#elif _WIN64
void *pSC = VirtualAlloc(NULL, (size_t)file_size,
MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READWRITE);
if (pSC == NULL)
{
fprintf(stdout, "ERROR: VirtualAlloc\n");
free(shellcode);
exit(EXIT_FAILURE);
}
#elif _WIN32
void *pSC = VirtualAlloc(NULL, (size_t)file_size,
MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READWRITE);
if (pSC == NULL)
{
fprintf(stdout, "ERROR: VirtualAlloc\n");
free(shellcode);
exit(EXIT_FAILURE);
}
#elif __APPLE__
void *pSC = mmap(0, sizeof(shellcode),
PROT_EXEC | PROT_WRITE | PROT_READ,
MAP_ANONYMOUS | MAP_PRIVATE,
-1, 0);
if (pSC == MAP_FAILED)
{
perror("mmap");
free(shellcode);
exit(EXIT_FAILURE);
}
#endif
// copy our shellcode to allocated mem space
memcpy(pSC, shellcode, file_size);
fprintf(stdout, "[*] Shellcode address: 0x%016lX\n",
(unsigned long)((uintptr_t)pSC));
//((void (*)(void))pSC)();
return pSC;
}
/**
* get_shellcode - gets shellcode from its path and returns as char array
* @shellcode_path: path of shellcode as a binary
*
* Return value: SUCC file size
* FAIL exit process
*/
ssize_t get_shellcode(char *shellcode_path)
{
FILE *fp;
static ssize_t file_size = 0;
fp = fopen(shellcode_path, "rb");
if (fp == NULL)
{
perror("fopen");
exit(EXIT_FAILURE);
}
// get size of shellcode file
fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
// allocate space in heap as much shellcode size
shellcode = (unsigned char *)malloc(file_size + 1);
if (shellcode == NULL)
{
perror("malloc");
fclose(fp);
exit(EXIT_FAILURE);
}
// copy shellcode to heap
fread(shellcode, file_size, 1, fp);
// close file
fclose(fp);
return file_size;
}
/**
* get_shellcode2 - gets shellcode from commandline
* @sc: shellcode from commandline as "\x53\x43\X54"
*
* Return value: SUCC file size
* FAIL exit process
*/
ssize_t get_shellcode2(char *sc)
{
static ssize_t file_size = 0;
file_size = strlen(sc) / 4 + 1;
// allocate space in heap as much shellcode size
shellcode = (unsigned char *)malloc(file_size + 1);
if (shellcode == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
// lowercase the sc bytes if user uses \X instead of \x
char c;
for (int ix = 0; ix < strlen(sc); ix++)
{
c = sc[ix];
sc[ix] = tolower(c);
}
size_t i = 0;
for (char *tok = strtok(sc, "\\x"); tok; tok = strtok(NULL, "\\x"))
{
sscanf(tok, "%02hhx", shellcode + i);
i++;
}
return file_size;
}
/**
* run_shellcode - takes shellcode's pointer as a parameter; jumps that
* memory address and executes shellcode
*
* @pSC: pointer of shellcode
*
* Return value : None
*/
void run_shellcode(void *pSC)
{
printf("[+] Do you wanna run shellcode? (y/n) ");
char r = getchar();
if (r == 'y' || r == 'Y')
{
printf("\n[+] Executing...\n");
((void (*)(void))pSC)();
}
else
{
printf("[+] https://github.com/blue-devil/kuymak\n");
}
}
int main(int argc, char **argv)
{
int opt;
print_banner();
if (argc < 2)
{
usage();
}
while ((opt = getopt(argc, argv, "hb:c:")) != -1)
{
switch (opt)
{
case 'h':
usage();
exit(EXIT_SUCCESS);
case 'b':
fprintf(stdout, "[*] Shellcode file : %s\n", optarg);
run_shellcode(map_shellcode(get_shellcode(optarg)));
break;
case 'c':
fprintf(stdout, "[*] Shellcode as chars : %s\n", optarg);
run_shellcode(map_shellcode(get_shellcode2(optarg)));
break;
default:
// usage();
exit(EXIT_FAILURE);
}
}
return EXIT_SUCCESS;
}