forked from iilegacyyii/Shellcrypt
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathshellcrypt.py
More file actions
290 lines (227 loc) · 9.54 KB
/
shellcrypt.py
File metadata and controls
290 lines (227 loc) · 9.54 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
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
"""
~ @0xLegacyy (Jordan Jay)
Shellcrypt: Quality of Life Shellcode Obfuscation Tool
/ - Shellcrypt.py : Main Program
/utils/
- /crypters.py : Encryption, Encoding & Compression Toolkit
- /logging.py : Logging Helper
"""
import argparse
import logging
import pyfiglet
from rich.console import Console
from rich.theme import Theme
from binascii import hexlify
from os import urandom
from os.path import isfile
from string import hexdigits
from utils.crypters import Encode, Encrypt, Compress, ShellcodeFormatter
from utils.logging import Log
theme = Theme({
"success" : "spring_green3",
"info" : "cornflower_blue",
"error" : "red",
"exception" : "red",
})
console = Console(theme=theme, color_system="auto")
# global vars
OUTPUT_FORMATS = [
"c",
"csharp",
"nim",
"go",
"py",
"ps1",
"vba",
"vbscript",
"raw",
"rust",
"js",
"zig"
]
CIPHERS = [
"aes_128",
"aes_ecb",
"aes_cbc",
"chacha20",
"rc4",
"salsa20",
"xor",
"xor_complex"
]
ENCODING = [
"alpha32",
"ascii85",
"base64",
"words256"
]
COMPRESSION = [
"lznt",
"rle",
]
VERSION = "v2.0 - Release"
def show_banner():
banner = pyfiglet.figlet_format("Shellcrypt", font="slant", justify="left")
console.print(f"[bold yellow]{banner}{VERSION}\n[/bold yellow]")
console.print("By: @0xLegacyy (Jordan Jay)\n", style="green4")
def parse_args():
# Parse arguments with additional features
# TODO: Add decryption routines in the future
argparser = argparse.ArgumentParser(prog="shellcrypt")
# Required argument: Input file
argparser.add_argument("-i", "--input", help="Path to file to be encrypted.", required=True)
# Encryption related options
argparser.add_argument("-e", "--encrypt", default=None, help="Encryption method to use, default None.")
argparser.add_argument("--decrypt", action="store_true", help="Enable decryption functionality (not yet implemented).")
# Encoding related options
argparser.add_argument("-d", "--encode", default=None, help="Encoding method to use, default None.")
# Compression related options
argparser.add_argument("-c", "--compress", default=None, help="Compression method to use.")
# Key and nonce options
argparser.add_argument("-k", "--key", help="Encryption key in hex format, default (random 16 bytes).")
argparser.add_argument("-n", "--nonce", help="Encryption nonce in hex format, default (random 16 bytes).")
# Format related options
argparser.add_argument("-f", "--format", help="Output format, specify --formats for a list of formats.")
# Info-related arguments
argparser.add_argument("--formats", action="store_true", help="Show a list of valid formats")
argparser.add_argument("--ciphers", action="store_true", help="Show a list of valid ciphers")
argparser.add_argument("--encoders", action="store_true", help="Show a list of valid encoders")
argparser.add_argument("--compressors", action="store_true", help="Show a list of valid compressors")
# Output file and version
argparser.add_argument("-o", "--output", help="Path to output file")
argparser.add_argument("-a", "--array", default="sh3llc0d3", help="Array Name, default sh3llc0d3")
argparser.add_argument("-v", "--version", action="store_true", help="Shows the version and exits")
# Additional Features
# Preserve null bytes during XOR encryption
argparser.add_argument("--preserve-null", action="store_true", help="Avoid XORing null bytes during XOR encryption.")
# Specify key length (if greater than 16)
argparser.add_argument("--key-length", type=int, default=16, help="Specify the key length in bytes (default is 16).")
return argparser.parse_args()
def print_available_options(option_type, options, exit_on_print=True):
print(f"The following {option_type} are available:")
for option in options:
print(f" - {option}")
if exit_on_print:
exit()
def validate_input_file(input_file):
if input_file is None:
Log.logError("""Must specify an input file e.g.
-i shellcode.bin (specify --help for more info)""")
exit()
if not isfile(input_file):
Log.logError(f"Input file '{input_file}' does not exist.")
exit()
Log.logSuccess(f"Input file: '{input_file}'")
def validate_and_get_key(key, encrypt_type):
if key is None:
return urandom(32)
if len(key) < 2 or len(key) % 2 == 1 or any(i not in hexdigits for i in key):
Log.logError("Key must be valid byte(s) in hex format (e.g. 4141).")
exit()
if encrypt_type == "aes" and len(key) != 32:
Log.logError("AES-128 key must be exactly 16 bytes long.")
exit()
return bytearray.fromhex(key)
def validate_and_get_nonce(nonce):
if nonce is None:
return urandom(16)
if len(nonce) != 32 or any(i not in hexdigits for i in nonce):
Log.logError("Nonce must be 16 valid bytes in hex format (e.g. 7468697369736d616c6963696f757321)")
exit()
return bytearray.fromhex(nonce)
def process_encoding(input_bytes, args, encoder):
if args.encode:
input_bytes = encoder.encode(args.encode, input_bytes)
return input_bytes
def process_compression(input_bytes, args, compressor):
if args.compress:
input_bytes = compressor.compress(args.compress, input_bytes)
return input_bytes
def process_encryption(input_bytes, args, cryptor, key, nonce):
if args.encrypt:
input_bytes = cryptor.encrypt(args.encrypt, input_bytes, key, nonce)
return input_bytes
def main():
try:
# Show banner and parse arguments
show_banner()
args = parse_args()
# --------- Info-only arguments ---------
if args.formats:
print_available_options("formats", OUTPUT_FORMATS)
if args.ciphers:
print_available_options("ciphers", CIPHERS)
if args.encoders:
print_available_options("encoders", ENCODING)
if args.compressors:
print_available_options("compressors", COMPRESSION)
if args.version:
print(VERSION)
exit()
# --------- Argument Validation ---------
Log.logDebug(msg="Validating arguments")
validate_input_file(args.input)
if args.format not in OUTPUT_FORMATS:
Log.logError("""Invalid format specified, please specify a valid format e.g.
-f c (--formats gives a list of valid formats)""")
exit()
Log.logSuccess(f"Output format: {args.format}")
if args.encrypt and args.encrypt not in CIPHERS:
Log.logError("""Invalid cipher specified, please specify a valid cipher e.g.
-e xor (--ciphers gives a list of valid ciphers)""")
exit()
if args.encode and args.encode not in ENCODING:
Log.logError("""Invalid encoder specified, please specify a valid encoder e.g.
-d ascii85 (--encoders gives a list of valid encoders)""")
exit()
if args.compress and args.compress not in COMPRESSION:
Log.logError("""Invalid compression specified, please specify a valid compression e.g.
-c lznt (--compressors gives a list of valid compressors)""")
exit()
Log.logSuccess(f"Output Compression: {args.compress}")
Log.logSuccess(f"Output Encryption: {args.encrypt}")
Log.logSuccess(f"Output Encoding: {args.encode}")
key = validate_and_get_key(args.key, args.encrypt)
Log.logSuccess(f"Using key: {hexlify(key).decode()}")
nonce = validate_and_get_nonce(args.nonce)
if args.encrypt == "aes":
Log.logSuccess(f"Using nonce: {hexlify(nonce).decode()}")
Log.logDebug("Arguments validated")
# --------- Read Input File ---------
with open(args.input, "rb") as input_handle:
input_bytes = input_handle.read()
# --------- Input File Processing ---------
cryptor = Encrypt()
compressor = Compress()
encoder = Encode()
if args.compress:
logging.info("Compressing Shellcode")
input_bytes = process_compression(input_bytes, args, compressor)
if args.encrypt:
logging.info("Encrypting Shellcode")
input_bytes = process_encryption(input_bytes, args, cryptor, key, nonce)
if args.encode:
logging.info("Encoding Shellcode")
input_bytes = process_encoding(input_bytes, args, encoder)
Log.logSuccess(f"Successfully processed input file ({len(input_bytes)} bytes)")
Log.logInfo("Deobfuscation Routine: Decode -> Decrypt -> Decompress\n")
# --------- Output Generation ---------
arrays = {"key": key}
if args.encrypt and args.encrypt in ["aes_128", "aes_ecb", "aes_cbc"]:
arrays["nonce"] = nonce
arrays[args.array] = input_bytes
# Generate formatted output
shellcode_formatter = ShellcodeFormatter()
output = shellcode_formatter.generate(args.format, arrays)
# --------- Output ---------
if args.output is None:
console.print(output.decode("latin1") if isinstance(output, bytearray) else output)
exit()
write_mode = "wb" if isinstance(output, bytearray) else "w"
with open(args.output, write_mode) as file_handle:
file_handle.write(output)
Log.logSuccess(f"Output written to '{args.output}'")
except Exception as e:
Log.LogException(f"Exception Caught: {e}")
if __name__ == "__main__":
main()