private IEnumerable <MemberDependency> GetDependencies(IAssemblyFile file)
        {
            try
            {
                using (var stream = file.OpenRead())
                    using (var peFile = new PEReader(stream))
                    {
                        var metadataReader = GetMetadataReader(peFile);

                        AddReferencedAssemblies(metadataReader);

                        var helper = new DependencyFinderEngineHelper(_assemblyFilter, metadataReader, file);
                        helper.ComputeData();

                        // Remember this assembly as a user assembly.
                        _userAssemblies.Add(helper.CallingAssembly);

                        return(helper.MemberDependency);
                    }
            }
            catch (Exception exc)
            {
                // InvalidPEAssemblyExceptions may be expected and indicative of a non-PE file
                if (exc is InvalidPEAssemblyException)
                {
                    throw;
                }

                // Other exceptions are unexpected, though, and wil benefit from
                // more details on the scenario that hit them
                throw new PortabilityAnalyzerException(string.Format(LocalizedStrings.MetadataParsingExceptionMessage, file.Name), exc);
            }
        }
        private static IAssemblyFile TryGetExistingAssemblyFile(IDataContext context, out ISolution solution)
        {
            solution = context.GetData(JetBrains.ProjectModel.DataContext.DataConstants.SOLUTION);
            if (solution == null)
            {
                return(null);
            }
            IList <TreeModelNode> data = context.GetData(TreeModelBrowser.TREE_MODEL_NODES);

            if (data == null || data.Count != 1)
            {
                return(null);
            }
            IAssemblyFile assemblyFile = GetAssemblyFile(data.First());

            if (assemblyFile == null)
            {
                return(null);
            }
            var component = solution.TryGetComponent <AssemblyInfoCache>();

            if (component == null)
            {
                return(null);
            }
            if (!assemblyFile.Location.ExistsFile || AssemblyExplorerUtil.AssemblyIsBroken(assemblyFile.Location, component))
            {
                return(null);
            }

            return(assemblyFile);
        }
        private static bool FilterValidFiles(IAssemblyFile file, IProgressReporter _progressReport)
        {
            if (file.Exists)
            {
                return(true);
            }

            _progressReport.ReportIssue(string.Format(CultureInfo.CurrentCulture, LocalizedStrings.UnknownFile, file.Name));

            return(false);
        }
        private static bool FilterValidFiles(IAssemblyFile file, IProgressReporter _progressReport)
        {
            if (file.Exists)
            {
                return true;
            }

            _progressReport.ReportIssue(string.Format(LocalizedStrings.UnknownFile, file.Name));

            return false;
        }
        public FileSystemPath Execute(IAssemblyFile existingAssemblyFile, string newFileName, IProgressIndicator progressIndicator)
        {
            var context = new ModuleContext();
            var fileOptions = new ObfuscatedFile.Options
            {
                Filename = existingAssemblyFile.Location.FullPath,
                NewFilename = newFileName,
            };

            IObfuscatedFile obfuscationFile = CreateObfuscationFile(fileOptions, context);

            return Deobfuscate(obfuscationFile, progressIndicator);
        }
Exemplo n.º 6
0
        public FileSystemPath Execute(IAssemblyFile existingAssemblyFile, string newFileName, IProgressIndicator progressIndicator)
        {
            var context     = new ModuleContext();
            var fileOptions = new ObfuscatedFile.Options
            {
                Filename    = existingAssemblyFile.Location.FullPath,
                NewFilename = newFileName,
            };

            IObfuscatedFile obfuscationFile = CreateObfuscationFile(fileOptions, context);

            return(Deobfuscate(obfuscationFile, progressIndicator));
        }
        private IEnumerable <MemberDependency> GetDependencies(IAssemblyFile file)
        {
            try
            {
                using (var stream = file.OpenRead())
                {
                    using (var peFile = new PEReader(stream))
                    {
                        var metadataReader = GetMetadataReader(peFile);

                        var helper = new DependencyFinderEngineHelper(_assemblyFilter, metadataReader, file, _objectFinder);

                        if (_assemblyFilter.IsFrameworkAssembly(metadataReader.FormatAssemblyInfo()))
                        {
                            _nonUserAssemblies.Add(helper.CallingAssembly);
                            return(Enumerable.Empty <MemberDependency>());
                        }
                        else
                        {
                            AddReferencedAssemblies(metadataReader);

                            helper.ComputeData();

                            // Remember this assembly as a user assembly.
                            _userAssemblies.Add(helper.CallingAssembly);

                            return(helper.MemberDependency);
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                // InvalidPEAssemblyExceptions may be expected and indicative of a non-PE file
                if (exc is InvalidPEAssemblyException)
                {
                    throw;
                }

                // Occurs when we cannot find the System.Object assembly.
                if (exc is SystemObjectNotFoundException)
                {
                    throw;
                }

                // Other exceptions are unexpected, though, and will benefit from
                // more details on the scenario that hit them
                throw new PortabilityAnalyzerException(string.Format(CultureInfo.CurrentCulture, LocalizedStrings.MetadataParsingExceptionMessage, file.Name), exc);
            }
        }
        private string AskUser(IAssemblyFile existingAssemblyFile)
        {
            var saveFileDialog = new SaveFileDialog
            {
                OverwritePrompt = true,
                FileName = existingAssemblyFile.Location.NameWithoutExtension + "-deobfuscated" + existingAssemblyFile.Location.ExtensionWithDot,
                DefaultExt = "dll",
                Title = "Save Deobfuscated Assembly As...",
                Filter = "DLL File (*.dll)|*.dll|All Files (*.*)|*.*",
            };

            using (saveFileDialog)
            {
                if (saveFileDialog.ShowDialog() != DialogResult.OK)
                    return null;

                return saveFileDialog.FileName;
            }
        }
        public DependencyFinderEngineHelper(IDependencyFilter assemblyFilter, MetadataReader metadataReader, IAssemblyFile file)
        {
            _assemblyFilter = assemblyFilter;
            _reader = metadataReader;
            _assemblyLocation = file.Name;

            MemberDependency = new List<MemberDependency>();
            CallingAssembly = new AssemblyInfo
            {
                AssemblyIdentity = metadataReader.FormatAssemblyInfo().ToString(),
                FileVersion = file.Version ?? string.Empty,
                TargetFrameworkMoniker = metadataReader.GetTargetFrameworkMoniker() ?? string.Empty
            };

            // Get assembly info
            var assemblyDefinition = _reader.GetAssemblyDefinition();

            _currentAssemblyInfo = _reader.FormatAssemblyInfo(assemblyDefinition);
            _currentAssemblyName = _reader.GetString(assemblyDefinition.Name);
        }
        private string AskUser(IAssemblyFile existingAssemblyFile)
        {
            var saveFileDialog = new SaveFileDialog
            {
                OverwritePrompt = true,
                FileName        = existingAssemblyFile.Location.NameWithoutExtension + "-deobfuscated" + existingAssemblyFile.Location.ExtensionWithDot,
                DefaultExt      = "dll",
                Title           = "Save Deobfuscated Assembly As...",
                Filter          = "DLL File (*.dll)|*.dll|All Files (*.*)|*.*",
            };

            using (saveFileDialog)
            {
                if (saveFileDialog.ShowDialog() != DialogResult.OK)
                {
                    return(null);
                }

                return(saveFileDialog.FileName);
            }
        }
        public void Execute(IDataContext context, DelegateExecute nextExecute)
        {
            ISolution     solution;
            IAssemblyFile existingAssemblyFile = TryGetExistingAssemblyFile(context, out solution);

            if (existingAssemblyFile == null)
            {
                return;
            }

            var deobfuscator = solution.TryGetComponent <IAssemblyDeobfuscatorManager>();

            if (deobfuscator == null)
            {
                return;
            }

            string newFileName = AskUser(existingAssemblyFile);

            if (newFileName == null)
            {
                return;
            }

            FileSystemPath newAssembly = null;

            Shell.Instance.GetComponent <UITaskExecutor>().FreeThreaded.ExecuteTask("Deobfuscating...", TaskCancelable.Yes, progressIndicator =>
            {
                using (ReadLockCookie.Create())
                    newAssembly = deobfuscator.Execute(existingAssemblyFile, newFileName, progressIndicator);
            });

            if (newAssembly != null)
            {
                if (MessageBox.ShowYesNo("Deobfuscation complete!\nWould you like to open the deobfuscated assembly?"))
                {
                    AddToAssemblyExplorer(newAssembly, solution);
                }
            }
        }
Exemplo n.º 12
0
        public static IAssembly GetModuleAssembly(this IModule module)
        {
            var assemblyPsiModule = module as IAssemblyPsiModule;

            if (assemblyPsiModule != null)
            {
                return(assemblyPsiModule.Assembly.ToAssembly());
            }
            var project = module as IProject;

            if (project != null)
            {
                IAssemblyFile outputAssemblyFile = project.GetOutputAssemblyFile();
                if (outputAssemblyFile != null)
                {
                    return(outputAssemblyFile.Assembly);
                }
                return(null);
            }

            return(null);
        }
Exemplo n.º 13
0
        public static FileSystemPath GetModuleAssemblyLocation(this IModule module)
        {
            var assemblyPsiModule = module as IAssemblyPsiModule;

            if (assemblyPsiModule != null)
            {
                return(assemblyPsiModule.Assembly.Location);
            }
            var project = module as IProject;

            if (project != null)
            {
                IAssemblyFile outputAssemblyFile = project.GetOutputAssemblyFile();
                var           data = outputAssemblyFile as IAssemblyFileData;
                if (data != null)
                {
                    return(data.Location);
                }

                return(null);
            }

            return(null);
        }
        private static IAssemblyFile GetAssemblyFile(TreeModelNode assemblyNode)
        {
            IAssemblyFile assemblyFile     = null;
            var           assemblyFileNode = assemblyNode.DataValue as IAssemblyFileNode;

            if (assemblyFileNode != null)
            {
                assemblyFile = assemblyFileNode.GetAssemblyFile();
            }
            else
            {
                var assemblyReferenceNode = assemblyNode.DataValue as AssemblyReferenceNode;
                if (assemblyReferenceNode != null)
                {
                    IAssembly assemblyResolveResult = assemblyReferenceNode.Reference.GetModuleToAssemblyResolveResult();
                    if (assemblyResolveResult != null)
                    {
                        assemblyFile = assemblyResolveResult.GetFiles().FirstOrDefault();
                    }
                }
            }

            return(assemblyFile);
        }
Exemplo n.º 15
0
        public void Execute(IDataContext context, DelegateExecute nextExecute)
        {
            ISolution solution = context.GetData <ISolution>(JetBrains.ProjectModel.DataContext.DataConstants.SOLUTION);

            if (solution == null)
            {
                return;
            }

            var assemblyExplorerManager = SolutionEx.GetComponent <IAssemblyExplorerManager>(solution);
            var assemblyExplorer        = assemblyExplorerManager.Opened;

            if (assemblyExplorer == null)
            {
                return;
            }

#if DP10
            Assembly asm        = Assembly.GetExecutingAssembly();
            Stream   iconStream = asm.GetManifestResourceStream("JetBrains.DotPeek.Plugins.Console.Console.png");
            var      decoder    = new PngBitmapDecoder(iconStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            var      icon       = decoder.Frames[0];

            //var icon = new BitmapImage(new Uri(@"pack://application:,,,/JetBrains.DotPeek.Plugins.Console.1.0;Console.png", UriKind.RelativeOrAbsolute));
            //Assembly asm = Assembly.GetExecutingAssembly();
            //Stream iconStream = asm.GetManifestResourceStream("Console.png");
            //BitmapImage bitmap = new BitmapImage();
            //bitmap.BeginInit();
            //bitmap.StreamSource = iconStream;
            //bitmap.EndInit();
            //icon.Source = bitmap;
#elif DP11 || DP12
            IThemedIconManager themedIconManager = SolutionEx.GetComponent <IThemedIconManager>(solution);
            var icon = themedIconManager.GetIcon <ConsoleThemedIcons.Console>().CurrentImageSource;
#endif

            var console       = new ConsoleToolWindow(assemblyExplorerManager);
            var consoleWindow = new Window
            {
                Title   = "Console",
                Icon    = icon,
                Content = console,
                Width   = 640,
                Height  = 520,
                WindowStartupLocation = WindowStartupLocation.CenterScreen,
                ResizeMode            = ResizeMode.CanResize
            };
            consoleWindow.Show();

            // Do we have an assembly node selected somewhere? If so, load the assembly in the console
            var data = context.GetData(TreeModelBrowser.TREE_MODEL_NODES);
            if (data != null)
            {
                var node = data.FirstOrDefault();
                if (node != null)
                {
                    IAssemblyFile     assemblyFile     = null;
                    IAssemblyFileNode assemblyFileNode = node.DataValue as IAssemblyFileNode;
                    if (assemblyFileNode != null)
                    {
                        assemblyFile = ExplorerNodeEx.GetAssemblyFile(assemblyFileNode);
                    }
                    else
                    {
                        AssemblyReferenceNode assemblyReferenceNode = node.DataValue as AssemblyReferenceNode;
                        if (assemblyReferenceNode != null)
                        {
#if DP10
                            IAssembly assemblyResolveResult = ModuleReferencesResolveStoreEx.ResolveResult(assemblyReferenceNode.Reference);
#elif DP11 || DP12
                            IAssembly assemblyResolveResult = ModuleReferencesResolveStoreEx.GetModuleToAssemblyResolveResult(assemblyReferenceNode.Reference);
#endif
                            if (assemblyResolveResult != null)
                            {
                                assemblyFile = Enumerable.FirstOrDefault(assemblyResolveResult.GetFiles());
                            }
                        }
                    }

                    AssemblyInfoCache component = SolutionEx.TryGetComponent <AssemblyInfoCache>(solution);
#if DP10
                    if (component != null && assemblyFile.Location.ExistsFile)
                    {
                        console.LoadAssemblies(new[] { assemblyFile.Location.FullPath });
                    }
#elif DP11 || DP12
                    if (component != null && assemblyFile.Location.ExistsFile && !AssemblyExplorerUtil.AssemblyIsBroken(assemblyFile.Location, component))
                    {
                        console.LoadAssemblies(new[] { assemblyFile.Location.FullPath });
                    }
#endif
                }
            }
        }
        public DependencyFinderEngineHelper(IDependencyFilter assemblyFilter, MetadataReader metadataReader, IAssemblyFile file)
        {
            _assemblyFilter   = assemblyFilter;
            _reader           = metadataReader;
            _assemblyLocation = file.Name;

            MemberDependency = new List <MemberDependency>();
            CallingAssembly  = new AssemblyInfo
            {
                AssemblyIdentity       = metadataReader.FormatAssemblyInfo().ToString(),
                FileVersion            = file.Version ?? string.Empty,
                TargetFrameworkMoniker = metadataReader.GetTargetFrameworkMoniker() ?? string.Empty
            };

            // Get assembly info
            var assemblyDefinition = _reader.GetAssemblyDefinition();

            _currentAssemblyInfo = _reader.FormatAssemblyInfo(assemblyDefinition);
            _currentAssemblyName = _reader.GetString(assemblyDefinition.Name);
        }
        private IEnumerable<MemberDependency> GetDependencies(IAssemblyFile file)
        {
            try
            {
                using (var stream = file.OpenRead())
                using (var peFile = new PEReader(stream))
                {
                    var metadataReader = GetMetadataReader(peFile);

                    AddReferencedAssemblies(metadataReader);

                    var helper = new DependencyFinderEngineHelper(_assemblyFilter, metadataReader, file);
                    helper.ComputeData();

                    // Remember this assembly as a user assembly.
                    _userAssemblies.Add(helper.CallingAssembly);

                    return helper.MemberDependency;
                }
            }
            catch (Exception exc)
            {
                // InvalidPEAssemblyExceptions may be expected and indicative of a non-PE file
                if (exc is InvalidPEAssemblyException) throw;

                // Other exceptions are unexpected, though, and wil benefit from
                // more details on the scenario that hit them
                throw new PortabilityAnalyzerException(string.Format(LocalizedStrings.MetadataParsingExceptionMessage, file.Name), exc);
            }
        }
Exemplo n.º 18
0
 public ILStreamAssemblyFile(IAssemblyFile other, ITestOutputHelper output)
 {
     _other  = other;
     _output = output;
 }
 public AssemblyFileFrameworkFilter(IAssemblyFile file)
 {
     _name = file.Name;
 }