-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
393 lines (336 loc) · 15 KB
/
Program.cs
File metadata and controls
393 lines (336 loc) · 15 KB
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
using System.Text;
using System.Text.RegularExpressions;
using PeNet;
namespace PEDependencyAnalyzer
{
internal class Program
{
private class DllInfo
{
public required string Name { get; init; }
public required string FullPath { get; init; }
}
private record PublishOptions
{
public bool Enabled { get; init; }
public string DirectoryName { get; init; } = "publish";
public bool IncludeRuntimeDll { get; init; } = true;
public bool IncludeVirtualDll { get; init; } = true;
}
private static readonly HashSet<string> AnalyzedFiles = new(StringComparer.OrdinalIgnoreCase);
private static readonly Dictionary<string, DllInfo> SystemDependencies = new(StringComparer.OrdinalIgnoreCase);
private static readonly Dictionary<string, DllInfo> VirtualDependencies = new(StringComparer.OrdinalIgnoreCase);
private static readonly Dictionary<string, DllInfo> RuntimeDependencies = new(StringComparer.OrdinalIgnoreCase);
private static readonly Dictionary<string, DllInfo> OtherDependencies = new(StringComparer.OrdinalIgnoreCase);
// Cache for found DLLs
private static readonly Dictionary<string, string?> DllPathCache = new(StringComparer.OrdinalIgnoreCase);
// Compiled regular expressions
private static readonly Regex[] CompiledRuntimePatterns;
// Runtime DLL patterns
// For details, see https://learn.microsoft.com/en-us/cpp/windows/determining-which-dlls-to-redistribute?view=msvc-160
private static readonly string[] RuntimePatterns =
[
@"^vcruntime\d+(_\d+)?\.dll$", // Runtime Library for native code.
@"^vccorlib\d+\.dll$", // Runtime Library for managed code.
@"^msvcp\d+(_\d+)?\.dll$", // C++ Standard Library for native code.
@"^msvcr\d+\.dll$", // C++ Standard Library for native code.
@"^concrt\d+\.dll$", // Concurrency Runtime Library for native code.
@"^mfc\d+\.dll$", // Microsoft Foundation Classes (MFC) Library.
@"^mfc\d+.*\.dll$", // Microsoft Foundation Classes (MFC) Library Resources.
@"^mfc\d+u\.dll$", // MFC Library with Unicode support.
@"^mfcmifc80\.dll$", // MFC Managed Interfaces Library.
@"^mfcm\d+\.dll$", // MFC Managed Library.
@"^mfcm\d+u\.dll$", // MFC Managed Library with Unicode support.
@"^vcamp\d+\.dll$", // AMP Library for native code.
@"^vcomp\d+\.dll$", // OpenMP Library for native code.
// And additionally
@"^ucrtbase.*\.dll$", // ucrtbase.dll, ucrtbased.dll
@"^msvcrt.*\.dll$", // msvcrt.dll, msvcrtd.dll
@"^msvcp_win\.dll$" // msvcp_win.dll
];
private static readonly string[] SystemDirectories =
[
@"C:\Windows\system32",
@"C:\Windows\SysWOW64",
@"C:\Windows"
];
static Program()
{
// Compile regular expressions during initialization
CompiledRuntimePatterns = RuntimePatterns
.Select(pattern => new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase))
.ToArray();
}
private static void Main(string[] args)
{
if (args.Length == 0)
{
PrintUsage();
return;
}
var publishOptions = ParsePublishOptions(args);
var targetPath = args[0];
if (!File.Exists(targetPath) && !Directory.Exists(targetPath))
{
Console.WriteLine($"Path does not exist: {targetPath}");
return;
}
if (File.Exists(targetPath))
{
AnalyzeFile(targetPath, 0);
}
else
{
var files = Directory.GetFiles(targetPath, "*.exe", SearchOption.AllDirectories)
.Concat(Directory.GetFiles(targetPath, "*.dll", SearchOption.AllDirectories))
.ToArray();
// Parallel file processing
Parallel.ForEach(files, file =>
{
try
{
AnalyzeFile(file, 0);
}
catch (Exception ex)
{
Console.WriteLine($"Error analyzing {Path.GetFileName(file)}: {ex.Message}");
}
});
}
PrintDependenciesByType();
if (publishOptions.Enabled && File.Exists(targetPath))
{
PublishDependencies(targetPath, publishOptions);
}
}
private static void PrintUsage()
{
Console.WriteLine("Usage: PEDependencyAnalyzer <file_or_directory_path> [options]");
Console.WriteLine("Options:");
Console.WriteLine(" --publish[=directory_name] Enable publish mode (default directory: 'publish')");
Console.WriteLine(" --no-runtime Exclude runtime DLLs from publish");
Console.WriteLine(" --no-virtual Exclude virtual DLLs from publish");
Console.WriteLine("\nExamples:");
Console.WriteLine(" PEDependencyAnalyzer app.exe");
Console.WriteLine(" PEDependencyAnalyzer app.exe --publish");
Console.WriteLine(" PEDependencyAnalyzer app.exe --publish=dist --no-runtime");
}
private static PublishOptions ParsePublishOptions(string[] args)
{
var options = new PublishOptions
{
Enabled = false,
DirectoryName = "publish",
IncludeRuntimeDll = true,
IncludeVirtualDll = true
};
foreach (var arg in args.Skip(1)) // Skip the first argument (file path)
{
if (arg.StartsWith("--publish"))
{
options = options with { Enabled = true };
var parts = arg.Split('=', 2);
if (parts.Length > 1 && !string.IsNullOrWhiteSpace(parts[1]))
{
options = options with { DirectoryName = parts[1] };
}
}
else if (arg == "--no-runtime")
{
options = options with { IncludeRuntimeDll = false };
}
else if (arg == "--no-virtual")
{
options = options with { IncludeVirtualDll = false };
}
}
return options;
}
private static void PublishDependencies(string sourceFile, PublishOptions options)
{
var publishDir = Path.Combine(Path.GetDirectoryName(sourceFile) ?? ".", options.DirectoryName);
Directory.CreateDirectory(publishDir);
// Copy source file
var fileName = Path.GetFileName(sourceFile);
Console.WriteLine($"\nPublishing to {publishDir}");
Console.WriteLine($"Copying {fileName}");
File.Copy(sourceFile, Path.Combine(publishDir, fileName), true);
// Helper function to copy dependencies
void CopyDependencies(string title, IEnumerable<DllInfo> dependencies)
{
foreach (var dll in dependencies)
{
if (string.IsNullOrEmpty(dll.FullPath) || !File.Exists(dll.FullPath))
continue;
Console.WriteLine($"Copying {dll.Name}");
File.Copy(dll.FullPath, Path.Combine(publishDir, dll.Name), true);
}
}
// Copy other dependencies
CopyDependencies("Other Dependencies", OtherDependencies.Values);
// Copy runtime dependencies if enabled
if (options.IncludeRuntimeDll)
{
CopyDependencies("Runtime Dependencies", RuntimeDependencies.Values);
}
// Copy virtual dependencies if enabled
if (options.IncludeVirtualDll)
{
CopyDependencies("Virtual Dependencies", VirtualDependencies.Values);
}
Console.WriteLine("Publishing completed");
}
private static void ClassifyDependency(string dll, string? fullPath)
{
var dllInfo = new DllInfo { Name = dll, FullPath = fullPath ?? "" };
// First check if it's a virtual dependency (API Set)
if (dll.StartsWith("api-ms-win-", StringComparison.OrdinalIgnoreCase) ||
dll.StartsWith("ext-ms-win-", StringComparison.OrdinalIgnoreCase))
{
VirtualDependencies[dll] = dllInfo;
return;
}
// Then check if it's a runtime dependency
if (IsRuntimeDll(dll))
{
RuntimeDependencies[dll] = dllInfo;
return;
}
// Check if the DLL is in a system directory
if (!string.IsNullOrEmpty(fullPath) &&
SystemDirectories.Any(dir => fullPath.StartsWith(dir, StringComparison.OrdinalIgnoreCase)))
{
SystemDependencies[dll] = dllInfo;
return;
}
// If none of the above, check if it's in the predefined system DLLs list
if (IsSystemDll(dll))
{
SystemDependencies[dll] = dllInfo;
return;
}
// If none of the above conditions are met, it's another dependency
OtherDependencies[dll] = dllInfo;
}
private static bool IsRuntimeDll(string dll)
{
dll = dll.ToLower();
// Check using compiled regular expressions
if (CompiledRuntimePatterns.Any(regex => regex.IsMatch(dll)))
return true;
// Check specific runtime DLLs
string[] specificRuntimeDlls =
[
// .NET Runtime
"clr.dll", "coreclr.dll", "clrjit.dll", "mscorlib.dll",
"mscorwks.dll", "mscorsvr.dll", "mscoree.dll",
// Common Language Runtime
"fusion.dll", "diasymreader.dll",
// Other Runtime Components
"hostfxr.dll", "hostpolicy.dll"
];
return specificRuntimeDlls.Contains(dll);
}
private static bool IsSystemDll(string dll)
{
string[] systemDlls =
[
"kernel32.dll", "user32.dll", "gdi32.dll", "ntdll.dll", "advapi32.dll",
"comctl32.dll", "comdlg32.dll", "shell32.dll", "shlwapi.dll", "ole32.dll",
"oleaut32.dll", "rpcrt4.dll", "sechost.dll", "winspool.drv", "ws2_32.dll",
"bcrypt.dll", "combase.dll", "win32u.dll", "kernelbase.dll"
];
return systemDlls.Contains(dll.ToLower());
}
private static void PrintDependenciesByType()
{
var sb = new StringBuilder();
void AppendDependencies(string title, IEnumerable<DllInfo> dependencies)
{
sb.AppendLine($"\n{title}:")
.AppendLine(new string('-', title.Length + 1));
foreach (var dll in dependencies.OrderBy(x => x.Name))
{
sb.AppendLine($"{dll.Name,-40} {dll.FullPath}");
}
}
AppendDependencies("System Dependencies", SystemDependencies.Values);
AppendDependencies("Runtime Dependencies", RuntimeDependencies.Values);
AppendDependencies("Virtual Dependencies (API Sets)", VirtualDependencies.Values);
AppendDependencies("Other Dependencies", OtherDependencies.Values);
sb.AppendLine("\nSummary:")
.AppendLine("--------")
.AppendLine($"System Dependencies: {SystemDependencies.Count}")
.AppendLine($"Runtime Dependencies: {RuntimeDependencies.Count}")
.AppendLine($"Virtual Dependencies: {VirtualDependencies.Count}")
.AppendLine($"Other Dependencies: {OtherDependencies.Count}")
.AppendLine($"Total Dependencies: {SystemDependencies.Count + RuntimeDependencies.Count + VirtualDependencies.Count + OtherDependencies.Count}");
Console.Write(sb.ToString());
}
private static void AnalyzeFile(string filePath, int depth)
{
if (!File.Exists(filePath) || !AnalyzedFiles.Add(filePath.ToLower()))
return;
try
{
var peFile = new PeFile(filePath);
var imports = peFile.ImportedFunctions;
if (imports == null || !imports.Any()) return;
var dlls = imports.Select(f => f.DLL).Distinct();
foreach (var dll in dlls)
{
var dllPath = FindDll(dll, filePath);
if (dllPath == null) continue;
ClassifyDependency(dll, dllPath);
AnalyzeFile(dllPath, depth + 1);
}
}
catch (Exception ex)
{
Console.WriteLine($"{new string(' ', depth * 2)}Error analyzing {Path.GetFileName(filePath)}: {ex.Message}");
}
}
private static string? FindDll(string dllName, string importingModulePath)
{
// Check cache
if (DllPathCache.TryGetValue(dllName, out var cachedPath))
return cachedPath;
var moduleDirectory = Path.GetDirectoryName(importingModulePath);
if (moduleDirectory != null)
{
var fullPath = Path.Combine(moduleDirectory, dllName);
if (File.Exists(fullPath))
{
DllPathCache[dllName] = fullPath;
return fullPath;
}
}
var pathVar = Environment.GetEnvironmentVariable("PATH");
if (string.IsNullOrEmpty(pathVar))
{
DllPathCache[dllName] = null;
return null;
}
foreach (var path in pathVar.Split(Path.PathSeparator))
{
if (string.IsNullOrWhiteSpace(path))
continue;
try
{
var fullPath = Path.Combine(path, dllName);
if (File.Exists(fullPath))
{
DllPathCache[dllName] = fullPath;
return fullPath;
}
}
catch
{
// Skip invalid paths in PATH
}
}
DllPathCache[dllName] = null;
return null;
}
}
}