forked from TheWover/donut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
donutmodule.c
201 lines (171 loc) · 6.97 KB
/
donutmodule.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
/**
BSD 3-Clause License
Copyright (c) 2019, TheWover, Odzhan. All rights reserved.
Python C Extension by @byt3bl33d3r
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <Python.h>
#include "donut.h"
static PyObject *Donut_Create(PyObject *self, PyObject *args, PyObject *keywds) {
char *input = NULL; // input file to execute in-memory
int arch = 0; // target CPU architecture or mode
int bypass = 0; // AMSI/WDLP bypassing behavior
int compress = 0; // compress input file
int entropy = 0; // whether to randomize API hashes and use encryption
int format = 0; // output format
int exit_opt = 0; // exit process or exit thread
int thread = 0; // run unmanaged entrypoint as a thread
char *oep = NULL; // creates new thread for loader and continues execution at specified address provided in hexadecimal format
char *output = NULL; // name of loader stored on disk
char *runtime = NULL; // runtime version
char *domain = NULL; // app domain name to use
char *cls = NULL; // class name
char *method = NULL; // method name
char *params = NULL; // parameters for method
int unicode = 0; // param is converted to unicode before being passed to unmanaged DLL function
char *server = NULL; // HTTP server to download module from
char *modname = NULL; // name of module stored on HTTP server
static char *kwlist[] = {
"file", "arch", "bypass", "compress", "entropy",
"format", "exit_opt", "thread", "oep", "output",
"runtime", "appdomain", "cls", "method", "params",
"unicode", "server", "url", "modname", NULL};
if (!PyArg_ParseTupleAndKeywords(
args, keywds, "s|iiiiiiisssssssisss", kwlist, &input, &arch,
&bypass, &compress, &entropy, &format, &exit_opt, &thread,
&oep, &output, &runtime, &domain, &cls, &method, ¶ms,
&unicode, &server, &server, &modname))
{
return NULL;
}
DONUT_CONFIG c;
// zero initialize configuration
memset(&c, 0, sizeof(c));
// default settings
c.inst_type = DONUT_INSTANCE_EMBED; // file is embedded
c.arch = DONUT_ARCH_X84; // dual-mode (x86+amd64)
c.bypass = DONUT_BYPASS_CONTINUE; // continues loading even if disabling AMSI/WLDP fails
c.format = DONUT_FORMAT_BINARY; // default output format
c.compress = DONUT_COMPRESS_NONE; // compression is disabled by default
c.entropy = DONUT_ENTROPY_DEFAULT; // enable random names + symmetric encryption by default
c.exit_opt = DONUT_OPT_EXIT_THREAD; // default behaviour is to exit the thread
c.unicode = 0; // command line will not be converted to unicode for unmanaged DLL function
// input file
if(input != NULL) {
strncpy(c.input, input, DONUT_MAX_NAME - 1);
}
// target cpu architecture
if(arch != 0) {
c.arch = arch;
}
// bypass options
if(bypass != 0) {
c.bypass = bypass;
}
// class of .NET assembly
if(cls != NULL) {
strncpy(c.cls, cls, DONUT_MAX_NAME - 1);
}
// name of domain to use for .NET assembly
if(domain != NULL) {
strncpy(c.domain, domain, DONUT_MAX_NAME - 1);
}
// encryption options
if(entropy != 0) {
c.entropy = entropy;
}
// output format
if(format != 0) {
c.format = format;
}
// method of .NET assembly
if(method != NULL) {
strncpy(c.method, method, DONUT_MAX_NAME - 1);
}
// module name
if(modname != NULL) {
strncpy(c.modname, modname, DONUT_MAX_NAME - 1);
}
// output file for loader
if(output != NULL) {
strncpy(c.output, output, DONUT_MAX_NAME - 1);
}
// parameters to method, DLL function or command line for unmanaged EXE
if(params != NULL) {
strncpy(c.param, params, DONUT_MAX_NAME - 1);
}
// runtime version to use for .NET DLL / EXE
if(runtime != NULL) {
strncpy(c.runtime, runtime, DONUT_MAX_NAME - 1);
}
// run entrypoint of unmanaged EXE as a thread
if(thread != 0) {
c.thread = 1;
}
// server
if(server != NULL) {
strncpy(c.server, server, DONUT_MAX_NAME - 2);
c.inst_type = DONUT_INSTANCE_HTTP;
}
// convert param to unicode? only applies to unmanaged DLL function
if(unicode != 0) {
c.unicode = 1;
}
// call RtlExitUserProcess to terminate host process
if(exit_opt != 0) {
c.exit_opt = exit_opt;
}
// fork a new thread and execute address of original entry point
if(oep != NULL) {
c.oep = strtoull(oep, NULL, 16);
}
// pack/compress input file
if(compress != 0) {
c.compress = compress;
}
int err = DonutCreate(&c);
// printf("Error : %i\n", err);
PyObject *shellcode = Py_BuildValue("y#", c.pic, c.pic_len);
DonutDelete(&c);
return shellcode;
}
// module's function table
static PyMethodDef Donut_FunctionsTable[] = {
{
"create", // name exposed to Python
Donut_Create, // C wrapper function
METH_VARARGS|METH_KEYWORDS,
"Calls DonutCreate to generate shellcode for a .NET assembly" // documentation
},
{NULL, NULL, 0, NULL}
};
// modules definition
static struct PyModuleDef Donut_Module = {
PyModuleDef_HEAD_INIT,
"donut", // name of module exposed to Python
"Donut Python C extension", // module documentation
-1,
Donut_FunctionsTable
};
PyMODINIT_FUNC PyInit_donut(void) {
return PyModule_Create(&Donut_Module);
}