-
Notifications
You must be signed in to change notification settings - Fork 1
/
lnktools.py
217 lines (166 loc) · 6.4 KB
/
lnktools.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
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
import binascii
import locale
from pathlib import Path, PureWindowsPath
# ref: https://github.com/bristi/swinlnk
# see also: https://github.com/strayge/pylnk , https://github.com/Matmaus/LnkParse3
class SWinLnk:
r"""
Note that all data is passed as strings of numbers (no preceding \x)
"""
def __init__(self):
###############################################################
# Variables from the official Microsoft documentation
###############################################################
# HeaderSize
self.HeaderSize = '4c000000'
# LinkCLSID
self.LinkCLSID = self.convert_clsid_to_data(
"00021401-0000-0000-c000-000000000046"
)
# HasLinkTargetIDList ForceNoLinkInfo IsUnicode
self.LinkFlags = '81010000'
# FILE_ATTRIBUTE_DIRECTORY
self.FileAttributes_Directory = '10000000'
# FILE_ATTRIBUTE_ARCHIVE
self.FileAttributes_File = '20000000'
self.CreationTime = '0000000000000000'
self.AccessTime = '0000000000000000'
self.WriteTime = '0000000000000000'
self.FileSize = '00000000'
self.IconIndex = '00000000'
# SW_SHOWNORMAL
self.ShowCommand = '01000000'
# No Hotkey
self.Hotkey = '0000'
self.Reserved = '0000' # Non-modifiable value
self.Reserved2 = '00000000' # Non-modifiable value
self.Reserved3 = '00000000' # Non-modifiable value
self.TerminalID = '0000' # Non-modifiable value
# Workplace
self.CLSID_Computer = "20d04fe0-3aea-1069-a2d8-08002b30309d"
###############################################################
# Constants found from file analysis lnk
###############################################################
# Local disk
self.PREFIX_LOCAL_ROOT = '2f'
# File folder
self.PREFIX_FOLDER = '310000000000000000000000'
# File
self.PREFIX_FILE = '320000000000000000000000'
# Root network file server
self.PREFIX_NETWORK_ROOT = 'c30181'
# Network printer
self.PREFIX_NETWORK_PRINTER = 'c302c1'
self.END_OF_STRING = '00'
self.cp = locale.getlocale()[1]
def bytes2hex(self, b):
return b.hex()
def gen_idlist(self, id_item):
# We double length since our input lacks '\x'
id_item_len = len(id_item) * 2
item_size = format(int(id_item_len / 4) + 2, '04x')
slices = [
(2, 2),
(0, 2),
]
data = [item_size[x:x + y] for x, y in slices]
datastring = ''.join(data) + id_item
# > format(int(72/4)+2, '04x')
# '0014'
# When length is used on hex strings like \x00 and we just
# have 00, then multiply by two ;)
return datastring
def convert_clsid_to_data(self, clsid):
slices = [
(6, 2),
(4, 2),
(2, 2),
(0, 2),
(11, 2),
(9, 2),
(16, 2),
(14, 2),
(19, 4),
(24, 12),
]
data = [clsid[x:x + y] for x, y in slices]
datastring = ''.join(data)
return datastring
def create_lnk(self, link_target, link_name):
"""
:param link_target: Eg 'C:\\foo\\bar'
:param link_name: Eg /home/john/dunno.lnk
:return:
"""
root_lnk = False
target_leaf = ''
p = PureWindowsPath(link_target)
prefix_root = self.PREFIX_LOCAL_ROOT
item_data = '1f50' + \
self.convert_clsid_to_data(self.CLSID_Computer)
idlist_items = self.gen_idlist(item_data) # Root Folder part
target_root = p.drive
if len(p.parts) > 1:
# Leaf is part without drive (and backslash)
# Eg for 'C:\\Foo\\Bar' we get 'Foo\\Bar'
target_leaf = str(p)[len(p.drive)+1:]
if not target_root.endswith('\\'):
# TODO: Not sure this is a good idea..?
# log.debug("target_root ends with '\\'")
target_root += '\\'
if len(target_leaf) == 0:
# log.debug("No target leaf so assuming root link")
root_lnk = True
# We select the prefix that will be used to display the shortcut icon
if p.suffix:
prefix_of_target = self.PREFIX_FILE
file_attributes = self.FileAttributes_File
else:
prefix_of_target = self.PREFIX_FOLDER
file_attributes = self.FileAttributes_Directory
# Convert target values to binary
# print('target_root: {}'.format(target_root))
# print('target_leaf: {}'.format(target_leaf))
target_root = self.bytes2hex(target_root.encode(self.cp))
# Needed from Vista and higher otherwise the link is considered
# empty (I have not found any information about this)
target_root = target_root + ('00' * 21)
# Create the IDLIST that represents the core of the LNK file
# Volume Item part
idlist_items += self.gen_idlist(
prefix_root + target_root + self.END_OF_STRING
)
# File entry parts
if not root_lnk:
items = [self.bytes2hex(x.encode(self.cp)) for x in p.parts[1:]]
for item in items[:-1]:
data = self.PREFIX_FOLDER + item + self.END_OF_STRING
idlist_items += self.gen_idlist(data)
data = prefix_of_target + items[-1] + self.END_OF_STRING
idlist_items += self.gen_idlist(data)
idlist = self.gen_idlist(idlist_items)
with open(link_name, 'wb') as fout:
fout.write(
binascii.unhexlify(''.join([
self.HeaderSize,
self.LinkCLSID,
self.LinkFlags,
file_attributes,
self.CreationTime,
self.AccessTime,
self.WriteTime,
self.FileSize,
self.IconIndex,
self.ShowCommand,
self.Hotkey,
self.Reserved,
self.Reserved2,
self.Reserved3,
idlist,
self.TerminalID,
]))
)
def create_lnk(link_target, link_name):
swl = SWinLnk()
link_target = Path(link_target).absolute()
swl.create_lnk(link_target, link_name)