forked from NytroRST/ShellcodeCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeystoneLib.cpp
More file actions
54 lines (37 loc) · 1.11 KB
/
KeystoneLib.cpp
File metadata and controls
54 lines (37 loc) · 1.11 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
#include "KeystoneLib.h"
// Function used to assemble code with keystone. Code taken from keystone sample and adapted
unsigned char* KeystoneLib::Assemble(size_t *p_pSize, string p_sASM)
{
ks_engine *ks;
ks_err err = KS_ERR_ARCH;
size_t count;
unsigned char *encode;
size_t size;
unsigned char *output = NULL;
// Open keystone engine
err = ks_open(KS_ARCH_X86, KS_MODE_32, &ks);
if (err != KS_ERR_OK) {
cout << "ERROR: Failed on ks_open()" << endl;
return NULL;
}
// Assemble the code
if (ks_asm(ks, p_sASM.c_str(), 0, &encode, &size, &count)) {
cout << "ERROR: Failed on ks_asm() with count = " << count << ", error code = " << ks_errno(ks) << endl;
return NULL;
}
// If everything was OK, set the size and output and return
output = new unsigned char[size];
if (!output)
{
cout << "ERROR: Cannot allocate memmory for output" << endl;
return NULL;
}
*p_pSize = size;
memcpy(output, encode, size);
// Assembly information
cout << "Assembled: " << size << " bytes, " << count << " statements" << endl;
// Cleanup allocated data
ks_free(encode);
ks_close(ks);
return output;
}