bool IsProgramModule(DnModule module) { if (module.IsDynamic || module.IsInMemory) { return(true); } var filename = module.Name; if (!File.Exists(filename)) { return(true); } if (GacInfo.IsGacPath(filename)) { return(false); } var dnDebugger = module.Process.Debugger; if (IsInDirOrSubDir(Path.GetDirectoryName(dnDebugger.CLRPath), filename)) { return(false); } return(true); }
IEnumerable <string> FindAssembliesGacExactly(GacInfo gacInfo, IAssembly assembly, ModuleDef sourceModule) { var pkt = PublicKeyBase.ToPublicKeyToken(assembly.PublicKeyOrToken); if (gacInfo != null && pkt != null) { string pktString = pkt.ToString(); string verString = Utils.CreateVersionWithNoUndefinedValues(assembly.Version).ToString(); var cultureString = UTF8String.ToSystemStringOrEmpty(assembly.Culture); if (cultureString.Equals("neutral", StringComparison.OrdinalIgnoreCase)) { cultureString = string.Empty; } var asmSimpleName = UTF8String.ToSystemStringOrEmpty(assembly.Name); foreach (var subDir in gacInfo.SubDirs) { var baseDir = Path.Combine(gacInfo.Path, subDir); baseDir = Path.Combine(baseDir, asmSimpleName); baseDir = Path.Combine(baseDir, string.Format("{0}{1}_{2}_{3}", gacInfo.Prefix, verString, cultureString, pktString)); var pathName = Path.Combine(baseDir, asmSimpleName + ".dll"); if (File.Exists(pathName)) { yield return(pathName); } } } }
public static bool IsFrameworkAssembly(string filename, string?assemblySimpleName) { // Check if it's in one of the .NET runtime dirs if (Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(filename))) is string baseDir && Directory.Exists(Path.Combine(baseDir, "Microsoft.NETCore.App"))) { return(true); } if (assemblySimpleName is not null) { if (frameworkAssemblyNames.Contains(assemblySimpleName)) { return(true); } foreach (var prefix in frameworkAssemblyNamePrefixes) { if (assemblySimpleName.StartsWith(prefix, StringComparison.Ordinal)) { return(true); } } } // .NET Framework if (GacInfo.IsGacPath(filename)) { return(true); } return(false); }
IDsDocument ResolveNormal(IAssembly assembly, ModuleDef sourceModule) { var existingDocument = documentService.FindAssembly(assembly); if (existingDocument != null) { return(existingDocument); } var dotNetCoreAppVersion = dotNetCorePathProvider.TryGetDotNetCoreVersion(sourceModule); var document = LookupFromSearchPaths(assembly, sourceModule, dotNetCoreAppVersion); if (document != null) { return(documentService.GetOrAddCanDispose(document, assembly)); } var gacFile = GacInfo.FindInGac(assembly); if (gacFile != null) { return(documentService.TryGetOrCreateInternal(DsDocumentInfo.CreateDocument(gacFile), true, true)); } foreach (var path in GacInfo.OtherGacPaths) { document = TryLoadFromDir(assembly, true, path); if (document != null) { return(documentService.GetOrAddCanDispose(document, assembly)); } } return(null); }
public void Find() { foreach (var info in GacInfo.GetAssemblies(4)) { cancellationToken.ThrowIfCancellationRequested(); Add(info); } foreach (var info in GacInfo.GetAssemblies(2)) { cancellationToken.ThrowIfCancellationRequested(); Add(info); } }
static AssemblyResolver() { var windir = Environment.GetEnvironmentVariable("WINDIR"); if (!string.IsNullOrEmpty(windir)) { gac2Info = new GacInfo("", Path.Combine(windir, "assembly"), new string[] { "GAC_32", "GAC_64", "GAC_MSIL", "GAC" }); gac4Info = new GacInfo("v4.0_", Path.Combine(Path.Combine(windir, "Microsoft.NET"), "assembly"), new string[] { "GAC_32", "GAC_64", "GAC_MSIL" }); } }
/// <summary> /// Disable memory mapped I/O /// </summary> /// <param name="peImage">PE image</param> public static void DisableMemoryMappedIO(IPEImage peImage) { if (peImage == null) { return; } // Files in the GAC are read-only so there's no need to disable memory mapped I/O to // allow other programs to write to the file. if (GacInfo.IsGacPath(peImage.FileName)) { return; } peImage.UnsafeDisableMemoryMappedIO(); }
IEnumerable <string> FindAssembliesGacAny(GacInfo gacInfo, IAssembly assembly, ModuleDef sourceModule) { if (gacInfo != null) { var asmSimpleName = UTF8String.ToSystemStringOrEmpty(assembly.Name); foreach (var subDir in gacInfo.subDirs) { var baseDir = Path.Combine(gacInfo.path, subDir); baseDir = Path.Combine(baseDir, asmSimpleName); foreach (var dir in GetDirs(baseDir)) { var pathName = Path.Combine(dir, asmSimpleName + ".dll"); if (File.Exists(pathName)) { yield return(pathName); } } } } }
static IEnumerable<string> FindAssembliesGacExactly(GacInfo gacInfo, IAssembly assembly) { var pkt = PublicKeyBase.ToPublicKeyToken(assembly.PublicKeyOrToken); if (gacInfo != null && pkt != null) { string pktString = pkt.ToString(); string verString = CreateVersionWithNoUndefinedValues(assembly.Version).ToString(); var cultureString = UTF8String.ToSystemStringOrEmpty(assembly.Culture); if (cultureString.Equals("neutral", StringComparison.OrdinalIgnoreCase)) cultureString = string.Empty; var asmSimpleName = UTF8String.ToSystemStringOrEmpty(assembly.Name); foreach (var subDir in gacInfo.SubDirs) { var baseDir = Path.Combine(gacInfo.Path, subDir); baseDir = Path.Combine(baseDir, asmSimpleName); baseDir = Path.Combine(baseDir, $"{gacInfo.Prefix}{verString}_{cultureString}_{pktString}"); var pathName = Path.Combine(baseDir, asmSimpleName + ".dll"); if (File.Exists(pathName)) yield return pathName; } } }
private IEnumerable <string> FindAssembliesGacExactly(GacInfo gacInfo, IAssembly assembly, ModuleDef sourceModule) { var pkt = PublicKeyBase.ToPublicKeyToken(assembly.PublicKeyOrToken); if (gacInfo != null && pkt != null) { string pktString = pkt.ToString(); string verString = Utils.CreateVersionWithNoUndefinedValues(assembly.Version).ToString(); var asmSimpleName = UTF8String.ToSystemStringOrEmpty(assembly.Name); foreach (var subDir in gacInfo.subDirs) { var baseDir = Path.Combine(gacInfo.path, subDir); baseDir = Path.Combine(baseDir, asmSimpleName); baseDir = Path.Combine(baseDir, string.Format("{0}{1}__{2}", gacInfo.prefix, verString, pktString)); var pathName = Path.Combine(baseDir, asmSimpleName + ".dll"); if (File.Exists(pathName)) { yield return(pathName); } } } }
public static MemoryModuleDefFile Create(DnModule dnModule, bool loadSyms) { Debug.Assert(!dnModule.IsDynamic); Debug.Assert(dnModule.Address != 0); ulong address = dnModule.Address; var process = dnModule.Process; var data = new byte[dnModule.Size]; string location = dnModule.IsInMemory ? string.Empty : dnModule.Name; ProcessMemoryUtils.ReadMemory(process, address, data, 0, data.Length); var peImage = new PEImage(data, GetImageLayout(dnModule), true); var module = ModuleDefMD.Load(peImage); module.Location = location; bool autoUpdateMemory = false; //TODO: Init to default value if (GacInfo.IsGacPath(dnModule.Name)) { autoUpdateMemory = false; // GAC files are not likely to decrypt methods in memory } return(new MemoryModuleDefFile(process, address, data, dnModule.IsInMemory, module, loadSyms, autoUpdateMemory)); }
IDsDocument ResolveNormal(IAssembly assembly, ModuleDef sourceModule) { var fwkKind = GetFrameworkKind(sourceModule, out var netCoreVersion, out var sourceModuleDirectoryHint); IDsDocument document; switch (fwkKind) { case FrameworkKind.Unknown: case FrameworkKind.DotNetFramework2: case FrameworkKind.DotNetFramework4: var tempAsm = assembly; int gacVersion; if (!GacInfo.HasGAC2) { fwkKind = FrameworkKind.DotNetFramework4; } if (fwkKind == FrameworkKind.DotNetFramework4) { FrameworkRedirect.ApplyFrameworkRedirectV4(ref assembly); gacVersion = 4; } else if (fwkKind == FrameworkKind.DotNetFramework2) { FrameworkRedirect.ApplyFrameworkRedirectV2(ref assembly); gacVersion = 2; } else { Debug.Assert(fwkKind == FrameworkKind.Unknown); FrameworkRedirect.ApplyFrameworkRedirect(ref tempAsm, sourceModule); // OK : System.Runtime 4.0.20.0 => 4.0.0.0 // KO : System 4.0.0.0 => 2.0.0.0 if (tempAsm.Version.Major >= assembly.Version.Major) { assembly = tempAsm; } gacVersion = -1; } var existingDocument = documentService.FindAssembly(assembly); if (existingDocument != null) { return(existingDocument); } document = LookupFromSearchPaths(assembly, sourceModule, sourceModuleDirectoryHint, netCoreVersion); if (document != null) { return(documentService.GetOrAddCanDispose(document, assembly)); } var gacFile = GacInfo.FindInGac(assembly, gacVersion); if (gacFile != null) { return(documentService.TryGetOrCreateInternal(DsDocumentInfo.CreateDocument(gacFile), true, true)); } foreach (var gacPath in GacInfo.OtherGacPaths) { if (gacVersion == 4) { if (gacPath.Version != GacVersion.V4) { continue; } } else if (gacVersion == 2) { if (gacPath.Version != GacVersion.V2) { continue; } } else { Debug.Assert(gacVersion == -1); } document = TryLoadFromDir(assembly, checkVersion: true, checkPublicKeyToken: true, gacPath.Path); if (document != null) { return(documentService.GetOrAddCanDispose(document, assembly)); } } break; case FrameworkKind.DotNetCore: case FrameworkKind.Unity: document = LookupFromSearchPaths(assembly, sourceModule, sourceModuleDirectoryHint, netCoreVersion); if (document != null) { return(documentService.GetOrAddCanDispose(document, assembly)); } break; default: throw new InvalidOperationException(); } return(null); }
bool IsGacPath(string file) => GacInfo.IsGacPath(file) || IsUserGacPath(file);
static string?GetGacFilename(string asmFullName) => GacInfo.FindInGac(new AssemblyNameInfo(asmFullName));
bool CanSearchFile(DsDocumentNode node) => SearchSettings.SearchGacAssemblies || !GacInfo.IsGacPath(node.Document.Filename);
IDsDocument ResolveNormal(IAssembly assembly, ModuleDef sourceModule) { var fwkKind = GetFrameworkKind(sourceModule, out var netCoreVersion, out var sourceModuleDirectoryHint); if (fwkKind == FrameworkKind.DotNetCore && !dotNetCorePathProvider.HasDotNetCore) fwkKind = FrameworkKind.DotNetFramework4; IDsDocument document; switch (fwkKind) { case FrameworkKind.Unknown: case FrameworkKind.DotNetFramework2: case FrameworkKind.DotNetFramework4: var tempAsm = assembly; int gacVersion; if (!GacInfo.HasGAC2) fwkKind = FrameworkKind.DotNetFramework4; if (fwkKind == FrameworkKind.DotNetFramework4) { FrameworkRedirect.ApplyFrameworkRedirectV4(ref assembly); gacVersion = 4; } else if (fwkKind == FrameworkKind.DotNetFramework2) { FrameworkRedirect.ApplyFrameworkRedirectV2(ref assembly); gacVersion = 2; } else { Debug.Assert(fwkKind == FrameworkKind.Unknown); FrameworkRedirect.ApplyFrameworkRedirect(ref tempAsm, sourceModule); // OK : System.Runtime 4.0.20.0 => 4.0.0.0 // KO : System 4.0.0.0 => 2.0.0.0 if (tempAsm.Version.Major >= assembly.Version.Major) assembly = tempAsm; gacVersion = -1; } var existingDocument = documentService.FindAssembly(assembly); if (existingDocument != null) return existingDocument; document = LookupFromSearchPaths(assembly, sourceModule, sourceModuleDirectoryHint, netCoreVersion); if (document != null) return documentService.GetOrAddCanDispose(document, assembly); var gacFile = GacInfo.FindInGac(assembly, gacVersion); if (gacFile != null) return documentService.TryGetOrCreateInternal(DsDocumentInfo.CreateDocument(gacFile), true, true); foreach (var gacPath in GacInfo.OtherGacPaths) { if (gacVersion == 4) { if (gacPath.Version != GacVersion.V4) continue; } else if (gacVersion == 2) { if (gacPath.Version != GacVersion.V2) continue; } else Debug.Assert(gacVersion == -1); document = TryLoadFromDir(assembly, checkVersion: true, checkPublicKeyToken: true, gacPath.Path); if (document != null) return documentService.GetOrAddCanDispose(document, assembly); } break; case FrameworkKind.DotNetCore: case FrameworkKind.Unity: case FrameworkKind.SelfContainedDotNetCore: // If it's a self-contained .NET Core app, we don't need the version since we must only search // the current directory. Debug.Assert(fwkKind == FrameworkKind.DotNetCore || netCoreVersion == null); document = LookupFromSearchPaths(assembly, sourceModule, sourceModuleDirectoryHint, netCoreVersion); if (document != null) return documentService.GetOrAddCanDispose(document, assembly); break; default: throw new InvalidOperationException(); } return null; }
IEnumerable<string> FindAssembliesGacExactly(GacInfo gacInfo, AssemblyNameInfo assembly, ModuleDef sourceModule) { var pkt = PublicKeyBase.ToPublicKeyToken(assembly.PublicKeyOrToken); if (gacInfo != null && pkt != null) { string pktString = pkt.ToString(); string verString = Utils.CreateVersionWithNoUndefinedValues(assembly.Version).ToString(); var asmSimpleName = UTF8String.ToSystemStringOrEmpty(assembly.Name); foreach (var subDir in gacInfo.subDirs) { var baseDir = Path.Combine(gacInfo.path, subDir); baseDir = Path.Combine(baseDir, asmSimpleName); baseDir = Path.Combine(baseDir, string.Format("{0}{1}__{2}", gacInfo.prefix, verString, pktString)); var pathName = Path.Combine(baseDir, asmSimpleName + ".dll"); if (File.Exists(pathName)) yield return pathName; } } }
bool IsGacPath(string file) { return(GacInfo.IsGacPath(file) || IsUserGacPath(file)); }
IDsDocument ResolveNormal(IAssembly assembly, ModuleDef sourceModule) { var fwkKind = GetFrameworkKind(sourceModule, out var netCoreVersion, out var sourceModuleDirectoryHint); if (fwkKind == FrameworkKind.DotNetCore && !dotNetCorePathProvider.HasDotNetCore) { fwkKind = FrameworkKind.DotNetFramework4; } IDsDocument document; IDsDocument existingDocument; FindAssemblyOptions options; switch (fwkKind) { case FrameworkKind.Unknown: case FrameworkKind.DotNetFramework2: case FrameworkKind.DotNetFramework4: int gacVersion; if (!GacInfo.HasGAC2) { fwkKind = FrameworkKind.DotNetFramework4; } bool redirected; IAssembly tempAsm; if (fwkKind == FrameworkKind.DotNetFramework4) { redirected = FrameworkRedirect.TryApplyFrameworkRedirectV4(assembly, out tempAsm); if (redirected) { assembly = tempAsm; } gacVersion = 4; } else if (fwkKind == FrameworkKind.DotNetFramework2) { redirected = FrameworkRedirect.TryApplyFrameworkRedirectV2(assembly, out tempAsm); if (redirected) { assembly = tempAsm; } gacVersion = 2; } else { Debug.Assert(fwkKind == FrameworkKind.Unknown); redirected = FrameworkRedirect.TryApplyFrameworkRedirect(assembly, sourceModule, out tempAsm); // OK : System.Runtime 4.0.20.0 => 4.0.0.0 // KO : System 4.0.0.0 => 2.0.0.0 if (redirected && tempAsm.Version.Major >= assembly.Version.Major) { assembly = tempAsm; } else { redirected = false; } gacVersion = -1; } options = DsDocumentService.DefaultOptions; // If the assembly was redirected, always compare the version number. This prevents resolving // mscorlib 2.0 when a .NET 4 app references a .NET 2.0-3.5 dll. We should get mscorlib 4.0. if (redirected) { options |= FindAssemblyOptions.Version; } existingDocument = documentService.FindAssembly(assembly, options); if (existingDocument != null) { return(existingDocument); } document = LookupFromSearchPaths(assembly, sourceModule, sourceModuleDirectoryHint, netCoreVersion); if (document != null) { return(documentService.GetOrAddCanDispose(document, assembly)); } var gacFile = GacInfo.FindInGac(assembly, gacVersion); if (gacFile != null) { return(documentService.TryGetOrCreateInternal(DsDocumentInfo.CreateDocument(gacFile), true, true)); } foreach (var gacPath in GacInfo.OtherGacPaths) { if (gacVersion == 4) { if (gacPath.Version != GacVersion.V4) { continue; } } else if (gacVersion == 2) { if (gacPath.Version != GacVersion.V2) { continue; } } else { Debug.Assert(gacVersion == -1); } document = TryLoadFromDir(assembly, checkVersion: true, checkPublicKeyToken: true, gacPath.Path); if (document != null) { return(documentService.GetOrAddCanDispose(document, assembly)); } } break; case FrameworkKind.DotNetCore: case FrameworkKind.Unity: case FrameworkKind.SelfContainedDotNetCore: case FrameworkKind.WindowsUniversal: // If it's a self-contained .NET Core app, we don't need the version since we must only search // the current directory. Debug.Assert(fwkKind == FrameworkKind.DotNetCore || netCoreVersion == null); document = LookupFromSearchPaths(assembly, sourceModule, sourceModuleDirectoryHint, netCoreVersion); if (document != null) { return(documentService.GetOrAddCanDispose(document, assembly)); } // If it already exists in assembly explorer, use it options = DsDocumentService.DefaultOptions; if (IgnorePublicKey(fwkKind)) { options &= ~FindAssemblyOptions.PublicKeyToken; } existingDocument = documentService.FindAssembly(assembly, options); if (existingDocument != null) { return(existingDocument); } break; default: throw new InvalidOperationException(); } return(null); }
bool CanSearchFile(IDnSpyFileNode node) => SearchSettings.SearchGacAssemblies || !GacInfo.IsGacPath(node.DnSpyFile.Filename);
IEnumerable<string> FindAssembliesGacAny(GacInfo gacInfo, AssemblyNameInfo assembly, ModuleDef sourceModule) { if (gacInfo != null) { var asmSimpleName = UTF8String.ToSystemStringOrEmpty(assembly.Name); foreach (var subDir in gacInfo.subDirs) { var baseDir = Path.Combine(gacInfo.path, subDir); baseDir = Path.Combine(baseDir, asmSimpleName); foreach (var dir in GetDirs(baseDir)) { var pathName = Path.Combine(dir, asmSimpleName + ".dll"); if (File.Exists(pathName)) yield return pathName; } } } }
static AssemblyResolver() { var windir = Environment.GetEnvironmentVariable("WINDIR"); if (!string.IsNullOrEmpty(windir)) { gac2Info = new GacInfo("", Path.Combine(windir, "assembly"), new string[] { "GAC_32", "GAC_64", "GAC_MSIL", "GAC" }); gac4Info = new GacInfo("v4.0_", Path.Combine(Path.Combine(windir, "Microsoft.NET"), "assembly"), new string[] { "GAC_32", "GAC_64", "GAC_MSIL" }); } frmRedir2 = new Dictionary<string, FrameworkRedirectInfo>(StringComparer.OrdinalIgnoreCase); frmRedir4 = new Dictionary<string, FrameworkRedirectInfo>(StringComparer.OrdinalIgnoreCase); InitFrameworkRedirectV2(); InitFrameworkRedirectV4(); }