Repository navigation
Expand file tree
/
Copy pathpayload_prepper.py
More file actions
executable file
·187 lines (155 loc) · 4.95 KB
/
Copy pathpayload_prepper.py
File metadata and controls
executable file
·187 lines (155 loc) · 4.95 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
#!/usr/bin/env python3
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
import sys
import argparse
def main():
parser = argparse.ArgumentParser(description="Generate + encrypt shellcode")
parser.add_argument(
"-f", "--file", required=True, help="Path to raw shellcode file"
)
parser.add_argument(
"--alg",
required=True,
help="Select an algorithm. Options are 'rc4', 'aes', and 'xor'.",
)
parser.add_argument(
"--key", help="Specify encryption key. Default is hardcoded testing key."
)
parser.add_argument(
"--iv", help="Specify IV key for AES. Default is hardcoded testing IV."
)
parser.add_argument(
"--outfile",
help="Output file name for encrypted shellcode (default: test_implant.bin)",
)
args = parser.parse_args()
# Input file handling
match args.file:
case "" | None:
print("[!] No input file specified.")
sys.exit(1)
case _:
input_file = args.file
# Algorithm selection
match args.alg:
case "rc4":
alg = args.alg
case "aes":
alg = args.alg
iv = args.iv.encode() if args.iv else b"1234567890123456"
case "xor":
alg = args.alg
case _:
print("[!] Invalid algorithm specified. Use 'rc4', 'aes', or 'xor'.")
sys.exit(1)
# Output file handling
match args.outfile:
case "" | None:
output_file = "cipher.bin"
case _:
output_file = args.outfile
# Key handling
match args.key:
case "" | None:
key = bytes(
[
0x00,
0x01,
0x02,
0x03,
0x04,
0x05,
0x06,
0x07,
0x08,
0x09,
0x0A,
0x0B,
0x0C,
0x0D,
0x0E,
0x0F,
]
)
case _:
key = args.key.encode()
# -------------------------------------------------------
# 1. Ecrypt shellcode
# -------------------------------------------------------
print("[+] Loading raw shellcode from disk...")
try:
with open(input_file, "rb") as f:
shellcode = f.read()
if alg == "rc4":
ciphertext = rc4(key, shellcode)
plaintext = rc4(key, ciphertext)
if plaintext != shellcode:
print("Round trip test failed")
if alg == "aes":
ciphertext = aes(key, iv, shellcode)
if alg == "xor":
xored = bytes(b ^ key[i % len(key)] for i, b in enumerate(shellcode))
ciphertext = xored
with open(output_file, "wb") as f:
f.write(ciphertext)
print(
f"[+] {alg.upper()} encryption complete. Ciphertext written to {output_file}"
)
except Exception as e:
print(f"[!] Failed to read raw shellcode: {e}")
sys.exit(1)
def rc4(key: bytes, data: bytes) -> bytes:
"""Encrypts or decrypts data using the RC4 algorithm.
Args:
key (bytes): The encryption/decryption key.
data (bytes): The data to be encrypted or decrypted.
Returns:
bytes: The resulting encrypted or decrypted data.
"""
S = list(range(256))
j = 0
# Key scheduling algorithm (KSA)
for i in range(256):
j = (j + S[i] + key[i % len(key)]) % 256
S[i], S[j] = S[j], S[i]
# Pseudo-random generation algorithm (PRGA)
i = j = 0
out = bytearray()
for byte in data:
i = (i + 1) % 256
j = (j + S[i]) % 256
S[i], S[j] = S[j], S[i]
k = S[(S[i] + S[j]) % 256]
out.append(byte ^ k)
return bytes(out)
def rc4_debug_verify(key: bytes, original: bytes) -> bool:
"""
Encrypts then decrypts data and verifies byte-for-byte equality.
Returns True if identical, False otherwise.
"""
encrypted = rc4_crypt(key, original)
decrypted = rc4_crypt(key, encrypted)
if decrypted != original:
for i, (a, b) in enumerate(zip(original, decrypted)):
if a != b:
print(f"[!] RC4 mismatch at offset {i}: {a:#02x} != {b:#02x}")
break
return False
print("[+] RC4 debug OK: decrypted data matches original")
return True
def aes(key, iv, shellcode):
"""Encrypts shellcode using AES encryption in CBC mode.
Args:
key (bytes): The AES encryption key.
iv (bytes): The initialization vector.
shellcode (bytes): The shellcode to be encrypted.
Returns:
bytes: The encrypted shellcode.
"""
cipher = AES.new(key, AES.MODE_CBC, iv)
padded = pad(shellcode, AES.block_size)
encrypted = cipher.encrypt(padded)
return encrypted
if __name__ == "__main__":
main()