-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.py
executable file
·220 lines (195 loc) · 6.95 KB
/
main.py
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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import logging
import argparse
from sysplant.utils.loggerSingleton import LoggerSingleton
from sysplant.constants import DESC_HEADER, FANCY_HEADER
from sysplant.sysplant import Sysplant
if __name__ == "__main__":
# Set default level
log_level = logging.INFO
# Init logger Singleton
logger = LoggerSingleton(log_level)
parser = argparse.ArgumentParser(description=DESC_HEADER)
##################### VERBOSITY OPTIONS #####################
output_options = parser.add_argument_group("Output options")
shout_level = output_options.add_mutually_exclusive_group()
shout_level.add_argument(
"--debug", action="store_true", help="Display all DEBUG messages upon execution"
)
shout_level.add_argument(
"--verbose",
action="store_true",
help="Display all INFO messages upon execution",
)
shout_level.add_argument(
"--quiet",
action="store_true",
help="Remove all messages upon execution",
)
##################### ACTIONS OPTIONS #####################
actions = parser.add_subparsers(dest="action")
actions.required = True
action_list = actions.add_parser("list")
action_generate = actions.add_parser("generate")
################## LIST ACTION ########################
action_list.add_argument(
"path", help="Path to search for NtFunction, could be a file or a directory"
)
################## GENERATE ACTION ########################
subparsers = action_generate.add_subparsers(dest="generation")
subparsers.required = True
parser_hell = subparsers.add_parser("hell")
parser_halo = subparsers.add_parser("halo")
parser_tartarus = subparsers.add_parser("tartarus")
parser_freshy = subparsers.add_parser("freshy")
parser_syswhispers = subparsers.add_parser("syswhispers")
parser_syswhispers3 = subparsers.add_parser("syswhispers3")
parser_canterlot = subparsers.add_parser("canterlot")
parser_custom = subparsers.add_parser("custom")
###################### ARCH OPTIONS #######################
arch_options = action_generate.add_argument_group("Architecture options")
arch = arch_options.add_mutually_exclusive_group()
arch.add_argument(
"-x86", action="store_true", help="Set mode to 32bits", default=False
)
arch.add_argument(
"-wow",
action="store_true",
help="Set mode to WoW64 (execution of 32bits on 64bits)",
default=False,
)
arch.add_argument(
"-x64",
action="store_true",
help="Set mode to 64bits (Default True)",
default=True,
)
###################### LANGUAGE OPTIONS #######################
lang_options = action_generate.add_argument_group("Language options")
language = lang_options.add_mutually_exclusive_group()
language.add_argument(
"-nim",
action="store_false",
help="Generate NIM code (Default: true)",
default=True,
)
language.add_argument(
"-c", action="store_true", help="Generate C code", default=False
)
##################### CUSTOM OPTIONS #####################
parser_custom.add_argument(
"-i",
"--iterator",
help="Select syscall iterator (Default: canterlot)",
choices=[
"hell",
"halo",
"tartarus",
"freshy",
"syswhispers",
"syswhispers3",
"canterlot",
],
default="canterlot",
)
parser_custom.add_argument(
"-m",
"--method",
help="Select syscall stub (Default: direct)",
choices=["direct", "indirect", "random"],
default="direct",
)
##################### SYSCALL OPTIONS #####################
syscalls_options = action_generate.add_argument_group("Syscall options")
syscalls = syscalls_options.add_mutually_exclusive_group()
syscalls.add_argument(
"-p",
"--preset",
help="Preset functions to generate (Default: common)",
choices=["all", "donut", "common"],
required=False,
default="common",
)
syscalls.add_argument(
"-f", "--functions", help="Comma-separated functions", required=False
)
##################### GENERATE OPTIONS #####################
action_generate.add_argument(
"-x",
"--scramble",
help="Randomize internal function names to evade static analysis",
action="store_true",
)
action_generate.add_argument(
"-o",
"--output",
help="Output path for NIM generated file",
required=True,
)
args = parser.parse_args()
if args.verbose:
log_level = logging.INFO
if args.debug:
log_level = logging.DEBUG
if args.quiet:
log_level = logging.CRITICAL
# Update log level if necessary
logger.log_level = log_level
logger.info(FANCY_HEADER, stripped=True)
if args.action == "generate":
# Set architecture
arch = "x86" if args.x86 else "x64"
# Preset infos when necessary
if args.generation == "hell":
iterator = "hell"
method = "direct"
elif args.generation == "halo":
iterator = "halo"
method = "direct"
elif args.generation == "tartarus":
iterator = "tartarus"
method = "direct"
elif args.generation == "freshy":
iterator = "freshy"
method = "direct"
elif args.generation == "syswhispers":
iterator = "syswhispers"
method = "indirect"
elif args.generation == "syswhispers3":
iterator = "syswhispers3"
method = "random"
elif args.generation == "canterlot":
iterator = "canterlot"
method = "random"
elif args.generation == "custom":
iterator = args.iterator
method = args.method
try:
# Override default condition (preset: common) if funcitons are set
if args.functions:
args.syscalls = args.functions.split(",")
# Set syscall to generate
else:
args.syscalls = args.preset
# Set language mode
lang_set = "nim" # Default option
if args.c:
lang_set = "c"
engine = Sysplant(arch=arch, language=lang_set)
engine.generate(iterator=iterator, method=method, syscalls=args.syscalls)
engine.scramble(args.scramble)
engine.output(args.output)
except Exception as err:
logger.critical(err)
else:
try:
engine = Sysplant()
found = engine.list(args.path)
if len(found) > 0:
logger.info(f"{len(found)} NtFunctions found:")
logger.output(",".join(found))
else:
logger.info("No NtFunctions found")
except Exception as err:
logger.critical(err)