forked from securifybv/ShellLink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shortcut.cs
536 lines (482 loc) · 19.7 KB
/
Shortcut.cs
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
using System;
using System.IO;
using System.Text;
using System.Linq;
using Securify.ShellLink.Flags;
using Securify.ShellLink.Structures;
namespace Securify.ShellLink
{
/// <summary>
/// The Shortcut class can be used to process & create Shell Link Binary File Format files.
/// In this format a structure is called a shell link, or shortcut, and is a data object
/// that contains information that can be used to access another data object. The Shell Link
/// Binary File Format is the format of Windows files with the extension "LNK".
///
/// Shell links are commonly used to support application launching and linking scenarios,
/// such as Object Linking and Embedding(OLE), but they also can be used by applications that
/// need the ability to store a reference to a target file.
/// </summary>
public class Shortcut : ShellLinkHeader
{
#region Constructor
/// <summary>
/// Constructor
/// </summary>
public Shortcut() : base()
{
ExtraData = new ExtraData();
}
#endregion // Constructor
/// <summary>
/// SHELL_LINK_HEADER: A ShellLinkHeader structure, which contains identification information,
/// timestamps, and flags that specify the presence of optional structures.
/// </summary>
public ShellLinkHeader ShellLinkHeader => (ShellLinkHeader)this;
#region LinkFlags
/// <inheritdoc />
public override LinkFlags LinkFlags
{
get
{
LinkFlags linkFlags = base.LinkFlags;
if (LinkTargetIDList != null)
{
linkFlags |= LinkFlags.HasLinkTargetIDList;
}
else
{
linkFlags &= ~LinkFlags.HasLinkTargetIDList;
}
if (LinkInfo != null)
{
linkFlags |= LinkFlags.HasLinkInfo;
}
else
{
linkFlags &= ~LinkFlags.HasLinkInfo;
}
if (StringData != null)
{
linkFlags &= ~(LinkFlags.HasName | LinkFlags.HasRelativePath | LinkFlags.HasWorkingDir | LinkFlags.HasArguments | LinkFlags.HasIconLocation);
if (StringData.NameString != null)
{
linkFlags |= LinkFlags.HasName;
}
if (StringData.RelativePath != null)
{
linkFlags |= LinkFlags.HasRelativePath;
}
if (StringData.WorkingDir != null)
{
linkFlags |= LinkFlags.HasWorkingDir;
}
if (StringData.CommandLineArguments != null)
{
linkFlags |= LinkFlags.HasArguments;
}
if (StringData.IconLocation != null)
{
linkFlags |= LinkFlags.HasIconLocation;
}
if(StringData.IsUnicode)
{
linkFlags |= LinkFlags.IsUnicode;
}
}
else
{
linkFlags &= ~(LinkFlags.HasName | LinkFlags.HasRelativePath | LinkFlags.HasWorkingDir | LinkFlags.HasArguments | LinkFlags.HasIconLocation);
}
if (ExtraData.EnvironmentVariableDataBlock != null)
{
linkFlags |= LinkFlags.HasExpString;
}
else
{
linkFlags &= ~LinkFlags.HasExpString;
}
if (ExtraData.DarwinDataBlock != null)
{
linkFlags |= LinkFlags.HasDarwinID;
}
else
{
linkFlags &= ~LinkFlags.HasDarwinID;
}
if (ExtraData.IconEnvironmentDataBlock != null)
{
linkFlags |= LinkFlags.HasExpIcon;
}
else
{
linkFlags &= ~LinkFlags.HasExpIcon;
}
if (ExtraData.ShimDataBlock != null)
{
linkFlags |= LinkFlags.RunWithShimLayer;
}
else
{
linkFlags &= ~LinkFlags.RunWithShimLayer;
}
if (ExtraData.PropertyStoreDataBlock != null)
{
linkFlags |= LinkFlags.EnableTargetMetadata;
}
return linkFlags;
}
}
#endregion // LinkFlags
/// <summary>
/// LINKTARGET_IDLIST: An optional LinkTargetIDList structure, which specifies the target of
/// the link. The presence of this structure is specified by the HasLinkTargetIDList bit in the
/// ShellLinkHeader.
/// </summary>
public LinkTargetIDList LinkTargetIDList { get; set; }
/// <summary>
/// LINKINFO: An optional LinkInfo structure, which specifies information necessary to resolve
/// the link target. The presence of this structure is specified by the HasLinkInfo bit in the
/// ShellLinkHeader.
/// </summary>
public LinkInfo LinkInfo { get; set; }
/// <summary>
/// STRING_DATA: Zero or more optional StringData structures, which are used to convey user
/// interface and path identification information. The presence of these structures is specified
/// by bits in the ShellLinkHeader.
/// </summary>
public StringData StringData { get; set; }
/// <summary>
/// EXTRA_DATA: Zero or more ExtraData structures.
/// </summary>
public ExtraData ExtraData { get; set; }
#region ReadFromFile
/// <summary>
/// Reads a lnk and returns a Shortcut object
/// </summary>
/// <param name="path">Path to the lnk file</param>
/// <returns>Shortcut object</returns>
public static Shortcut ReadFromFile(String path)
{
return Shortcut.FromByteArray(File.ReadAllBytes(path));
}
#endregion // ReadFromFile
#region CreateShortcut
/// <summary>
/// Create a new Shelllink (shortcut)
/// </summary>
/// <param name="path">Path the shortcut points to</param>
/// <returns>Shortcut object</returns>
public static Shortcut CreateShortcut(String path)
{
Shortcut lnk = new Shortcut();
lnk.ExtraData.EnvironmentVariableDataBlock = new EnvironmentVariableDataBlock(path);
return lnk;
}
/// <summary>
/// Create a new Shelllink (shortcut)
/// </summary>
/// <param name="path">Path the shortcut points to</param>
/// <param name="iconpath">Path to the file containing the icon</param>
/// <param name="iconindex">Index ot the icon</param>
/// <returns>Shortcut object</returns>
public static Shortcut CreateShortcut(String path, String iconpath, int iconindex)
{
Shortcut lnk = CreateShortcut(path);
lnk.IconIndex = iconindex;
lnk.StringData = new StringData(true)
{
IconLocation = iconpath
};
lnk.ExtraData.IconEnvironmentDataBlock = new IconEnvironmentDataBlock(iconpath);
return lnk;
}
/// <summary>
/// Create a new Shelllink (shortcut)
/// </summary>
/// <param name="path">Path the shortcut points to</param>
/// <param name="args">Command line arguments</param>
/// <returns>Shortcut object</returns>
public static Shortcut CreateShortcut(String path, String args)
{
Shortcut lnk = CreateShortcut(path);
lnk.StringData = new StringData(true)
{
CommandLineArguments = args
};
return lnk;
}
/// <summary>
/// Create a new Shelllink (shortcut)
/// </summary>
/// <param name="path">Path the shortcut points to</param>
/// <param name="args">Command line arguments</param>
/// <param name="iconpath">Path to the file containing the icon</param>
/// <param name="iconindex">Index ot the icon</param>
/// <returns>Shortcut object</returns>
public static Shortcut CreateShortcut(String path, String args, String iconpath, int iconindex)
{
Shortcut lnk = CreateShortcut(path, args);
lnk.IconIndex = iconindex;
lnk.StringData.IconLocation = iconpath;
lnk.ExtraData.IconEnvironmentDataBlock = new IconEnvironmentDataBlock(iconpath);
return lnk;
}
/// <summary>
/// Create a new Shelllink (shortcut)
/// </summary>
/// <param name="path">Path the shortcut points to</param>
/// <param name="args">Command line arguments</param>
/// <param name="workdir">Working directory</param>
/// <returns>Shortcut object</returns>
public static Shortcut CreateShortcut(String path, String args, String workdir)
{
Shortcut lnk = CreateShortcut(path, args);
lnk.StringData.WorkingDir = workdir;
return lnk;
}
/// <summary>
/// Create a new Shelllink (shortcut)
/// </summary>
/// <param name="path">Path the shortcut points to</param>
/// <param name="args">Command line arguments</param>
/// <param name="workdir">Working directory</param>
/// <param name="iconpath">Path to the file containing the icon</param>
/// <param name="iconindex">Index ot the icon</param>
/// <returns>Shortcut object</returns>
public static Shortcut CreateShortcut(String path, String args, String workdir, String iconpath, int iconindex)
{
Shortcut lnk = CreateShortcut(path, args, iconpath, iconindex);
lnk.StringData.WorkingDir = workdir;
return lnk;
}
#endregion // CreateShortcut
#region WriteToFile
/// <summary>
/// Writes Shortcut object to a file
/// </summary>
/// <param name="path">Target lnk path</param>
public void WriteToFile(String path)
{
File.WriteAllBytes(path, GetBytes());
}
#endregion // WriteToFile
#region Size
/// <summary>
/// The size of the ShellLink in bytes
/// </summary>
public int Size
{
get
{
int size = (int)HeaderSize + ExtraData.ExtraDataSize;
if ((LinkFlags & LinkFlags.HasLinkTargetIDList) != 0)
{
size += LinkTargetIDList.IDListSize + 2;
}
if ((LinkFlags & LinkFlags.HasLinkInfo) != 0)
{
size += (int)LinkInfo.LinkInfoSize;
}
if(StringData != null)
{
size += StringData.StringDataSize;
}
return size;
}
}
#endregion // Size
#region GetBytes
/// <inheritdoc />
public override byte[] GetBytes()
{
int Offset = 0;
byte[] lnk = new byte[Size];
Buffer.BlockCopy(base.GetBytes(), 0, lnk, 0, (int)HeaderSize);
Offset += (int)HeaderSize;
if ((LinkFlags & LinkFlags.HasLinkTargetIDList) != 0)
{
Buffer.BlockCopy(LinkTargetIDList.GetBytes(), 0, lnk, Offset, LinkTargetIDList.IDListSize + 2);
Offset += LinkTargetIDList.IDListSize + 2;
}
if ((LinkFlags & LinkFlags.HasLinkInfo) != 0)
{
Buffer.BlockCopy(LinkInfo.GetBytes(), 0, lnk, Offset, (int)LinkInfo.LinkInfoSize);
Offset += (int)LinkInfo.LinkInfoSize;
}
if(StringData != null)
{
Buffer.BlockCopy(StringData.GetBytes(), 0, lnk, Offset, (int)StringData.StringDataSize);
Offset += (int)StringData.StringDataSize;
}
Buffer.BlockCopy(ExtraData.GetBytes(), 0, lnk, Offset, (int)ExtraData.ExtraDataSize);
return lnk;
}
#endregion // GetBytes
#region ToString
/// <inheritdoc />
public override String ToString()
{
StringBuilder builder = new StringBuilder();
builder.Append(base.ToString());
if ((LinkFlags & LinkFlags.HasLinkTargetIDList) != 0)
{
builder.Append(LinkTargetIDList.ToString());
}
if ((LinkFlags & LinkFlags.HasLinkInfo) != 0)
{
builder.Append(LinkInfo.ToString());
}
if (StringData != null)
{
builder.Append(StringData.ToString());
}
if (ExtraData.ExtraDataSize > 4)
{
builder.Append(ExtraData.ToString());
}
return builder.ToString();
}
#endregion // ToString
#region FromByteArray
/// <summary>
/// Create a ShellLing from a given byte array
/// </summary>
/// <param name="ba">The byte array</param>
/// <returns>A Shortcut object</returns>
public static new Shortcut FromByteArray(byte[] ba)
{
Shortcut lnk = new Shortcut();
#region SHELL_LINK_HEADER
ShellLinkHeader Header = ShellLinkHeader.FromByteArray(ba);
UInt32 HeaderSize = BitConverter.ToUInt32(ba, 0);
Boolean IsUnicode = (Header.LinkFlags & LinkFlags.IsUnicode) != 0;
lnk.LinkFlags = Header.LinkFlags;
lnk.FileAttributes = Header.FileAttributes;
lnk.CreationTime = Header.CreationTime;
lnk.AccessTime = Header.AccessTime;
lnk.WriteTime = Header.WriteTime;
lnk.FileSize = Header.FileSize;
lnk.IconIndex = Header.IconIndex;
lnk.ShowCommand = Header.ShowCommand;
lnk.HotKey = Header.HotKey;
ba = ba.Skip((int)HeaderSize).ToArray();
#endregion // SHELL_LINK_HEADER
#region LINKTARGET_IDLIST
if ((Header.LinkFlags & LinkFlags.HasLinkTargetIDList) != 0)
{
lnk.LinkTargetIDList = LinkTargetIDList.FromByteArray(ba);
UInt16 IDListSize = BitConverter.ToUInt16(ba, 0);
ba = ba.Skip(IDListSize + 2).ToArray();
}
#endregion // LINKTARGET_IDLIST
#region LINKINFO
if ((Header.LinkFlags & LinkFlags.HasLinkInfo) != 0)
{
lnk.LinkInfo = LinkInfo.FromByteArray(ba);
UInt32 LinkInfoSize = BitConverter.ToUInt32(ba, 0);
ba = ba.Skip((int)LinkInfoSize).ToArray();
}
#endregion // LINKINFO
#region STRING_DATA
if ((Header.LinkFlags & LinkFlags.HasName) != 0)
{
if (lnk.StringData == null)
{
lnk.StringData = new StringData(IsUnicode);
}
UInt16 CountCharacters = BitConverter.ToUInt16(ba, 0);
if (IsUnicode)
{
lnk.StringData.NameString = Encoding.Unicode.GetString(ba.Skip(2).Take(CountCharacters * 2).ToArray()).TrimEnd(new char[] { (char)0 });
ba = ba.Skip(CountCharacters * 2 + 2).ToArray();
}
else
{
lnk.StringData.NameString = Encoding.Default.GetString(ba.Skip(2).Take(CountCharacters).ToArray()).TrimEnd(new char[] { (char)0 });
ba = ba.Skip(CountCharacters + 2).ToArray();
}
}
if ((Header.LinkFlags & LinkFlags.HasRelativePath) != 0)
{
if (lnk.StringData == null)
{
lnk.StringData = new StringData(IsUnicode);
}
UInt16 CountCharacters = BitConverter.ToUInt16(ba, 0);
if (IsUnicode)
{
lnk.StringData.RelativePath = Encoding.Unicode.GetString(ba.Skip(2).Take(CountCharacters * 2).ToArray()).TrimEnd(new char[] { (char)0 });
ba = ba.Skip(CountCharacters * 2 + 2).ToArray();
}
else
{
lnk.StringData.RelativePath = Encoding.Default.GetString(ba.Skip(2).Take(CountCharacters).ToArray()).TrimEnd(new char[] { (char)0 });
ba = ba.Skip(CountCharacters + 2).ToArray();
}
}
if ((Header.LinkFlags & LinkFlags.HasWorkingDir) != 0)
{
if (lnk.StringData == null)
{
lnk.StringData = new StringData(IsUnicode);
}
UInt16 CountCharacters = BitConverter.ToUInt16(ba, 0);
if (IsUnicode)
{
lnk.StringData.WorkingDir = Encoding.Unicode.GetString(ba.Skip(2).Take(CountCharacters * 2).ToArray()).TrimEnd(new char[] { (char)0 });
ba = ba.Skip(CountCharacters * 2 + 2).ToArray();
}
else
{
lnk.StringData.WorkingDir = Encoding.Default.GetString(ba.Skip(2).Take(CountCharacters).ToArray()).TrimEnd(new char[] { (char)0 });
ba = ba.Skip(CountCharacters + 2).ToArray();
}
}
if ((Header.LinkFlags & LinkFlags.HasArguments) != 0)
{
if (lnk.StringData == null)
{
lnk.StringData = new StringData(IsUnicode);
}
UInt16 CountCharacters = BitConverter.ToUInt16(ba, 0);
if (IsUnicode)
{
lnk.StringData.CommandLineArguments = Encoding.Unicode.GetString(ba.Skip(2).Take(CountCharacters * 2).ToArray()).TrimEnd(new char[] { (char)0 });
ba = ba.Skip(CountCharacters * 2 + 2).ToArray();
}
else
{
lnk.StringData.CommandLineArguments = Encoding.Default.GetString(ba.Skip(2).Take(CountCharacters).ToArray()).TrimEnd(new char[] { (char)0 });
ba = ba.Skip(CountCharacters + 2).ToArray();
}
}
if ((Header.LinkFlags & LinkFlags.HasIconLocation) != 0)
{
if (lnk.StringData == null)
{
lnk.StringData = new StringData(IsUnicode);
}
UInt16 CountCharacters = BitConverter.ToUInt16(ba, 0);
if (IsUnicode)
{
lnk.StringData.IconLocation = Encoding.Unicode.GetString(ba.Skip(2).Take(CountCharacters * 2).ToArray()).TrimEnd(new char[] { (char)0 });
ba = ba.Skip(CountCharacters * 2 + 2).ToArray();
}
else
{
lnk.StringData.IconLocation = Encoding.Default.GetString(ba.Skip(2).Take(CountCharacters).ToArray()).TrimEnd(new char[] { (char)0 });
ba = ba.Skip(CountCharacters + 2).ToArray();
}
}
#endregion // STRING_DATA
#region EXTRA_DATA
if (ba.Length >= 4)
{
lnk.ExtraData = ExtraData.FromByteArray(ba);
}
#endregion // EXTRA_DATA
return lnk;
}
#endregion // FromByteArray
}
}