private DeobfOptions GetOptions()
        {
            DeobfOptions options = new DeobfOptions();

            options.Host                    = _host;
            options.Rows                    = _rows;
            options.SourceDir               = _sourceDir;
            options.OutputDir               = txtOutputDir.Text;
            options.TextInfoBox             = txtInfo;
            options.IsCancelPendingFunction = new DeobfOptions.IsCancelPendingDelegate(this.IsCancelPending);

            options.ApplyFrom(this);

            options.StringOptionSearchForMethod = (
                _selectedMethod != null &&
                !String.IsNullOrEmpty(txtMethod.Text)) ? _selectedMethod : null;

            options.StringOptionCalledMethod = (
                _selectedCalledMethod != null &&
                !String.IsNullOrEmpty(txtCalledMethod.Text)) ? _selectedCalledMethod : null;

            options.txtRegexText    = txtRegex.Text;
            options.BranchDirection = (BranchDirections)cboDirection.SelectedIndex;
            options.PluginList      = PluginUtils.GetSelectedPluginFromGrid(dgvPlugin);

            return(options);
        }
        public override void ParseLines()
        {
            _profiles = new List<DeobfProfile>();

            string profileName = null;
            DeobfOptions options = null;
            Type optionsType = typeof(DeobfOptions);

            foreach (string line in this.Lines)
            {
                if (line.StartsWith("[") && line.EndsWith("]"))
                {
                    if (profileName != null)
                    {
                        _profiles.Add(new DeobfProfile(profileName, options));
                    }

                    profileName = line.Substring(1, line.Length - 2);
                    options = new DeobfOptions();
                }
                else if (profileName != null)
                {
                    string[] s = line.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                    if (s.Length == 2)
                    {
                        string propertyName = s[0];
                        string propertyValue = s[1];

                        PropertyInfo pi = optionsType.GetProperty(propertyName);
                        if (pi != null)
                        {
                            if (propertyName.EndsWith("Checked"))
                            {
                                pi.SetValue(options, Convert.ToBoolean(propertyValue), null);
                            }
                            else if (propertyName.EndsWith("Count"))
                            {
                                pi.SetValue(options, Convert.ToInt32(propertyValue), null);
                            }
                            else
                            {
                                pi.SetValue(options, propertyValue, null);
                            }
                        }
                    }
                }
            }

            if (profileName != null)
            {
                _profiles.Add(new DeobfProfile(profileName, options));
            }
            
        }      
        public override void ParseLines()
        {
            _profiles = new List <DeobfProfile>();

            string       profileName = null;
            DeobfOptions options     = null;
            Type         optionsType = typeof(DeobfOptions);

            foreach (string line in this.Lines)
            {
                if (line.StartsWith("[") && line.EndsWith("]"))
                {
                    if (profileName != null)
                    {
                        _profiles.Add(new DeobfProfile(profileName, options));
                    }

                    profileName = line.Substring(1, line.Length - 2);
                    options     = new DeobfOptions();
                }
                else if (profileName != null)
                {
                    string[] s = line.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                    if (s.Length == 2)
                    {
                        string propertyName  = s[0];
                        string propertyValue = s[1];

                        PropertyInfo pi = optionsType.GetProperty(propertyName);
                        if (pi != null)
                        {
                            if (propertyName.EndsWith("Checked"))
                            {
                                pi.SetValue(options, Convert.ToBoolean(propertyValue), null);
                            }
                            else if (propertyName.EndsWith("Count"))
                            {
                                pi.SetValue(options, Convert.ToInt32(propertyValue), null);
                            }
                            else
                            {
                                pi.SetValue(options, propertyValue, null);
                            }
                        }
                    }
                }
            }

            if (profileName != null)
            {
                _profiles.Add(new DeobfProfile(profileName, options));
            }
        }
Beispiel #4
0
        public void ApplyFrom(DeobfOptions options)
        {
            Type optionsType = typeof(DeobfOptions);

            PropertyInfo[] pis = optionsType.GetProperties();

            foreach (PropertyInfo pi in pis)
            {
                string propertyName = pi.Name;
                if (propertyName.EndsWith("Checked") || propertyName.EndsWith("Count"))
                {
                    pi.SetValue(this, pi.GetValue(options, null), null);
                }
            }
        }
        public static bool IsValidName(string name, DeobfOptions options)
        {
            if (name == "<Module>")
                return true;
            if (regexHandledName.IsMatch(name))
                return true;

            if (options.chkNonAsciiChecked)
            {
                if (!regexAscii.IsMatch(name))
                {
                    return false;
                }
            }

            //if (name.Length < 3) return false; // e.g. PageSize.A4

            if (options.chkRandomNameChecked)
            {
                string[] splittedNames = name.Split('.');
                foreach (string splittedName in splittedNames)
                {
                    if (options.RandomFile.IsRandomName(splittedName))
                        return false;
                }
            }

            if (options.chkRegexChecked)
            {
                if (!String.IsNullOrEmpty(options.txtRegexText))
                {
                    if (Regex.IsMatch(name, options.txtRegexText))
                        return false;
                }

                if (options.RegexFile.IsRandomName(name))
                {
                    return false;
                }
            }

            return true;
        }
        public void ApplyFrom(DeobfOptions options)
        {
            Type optionsType = typeof(DeobfOptions);
            PropertyInfo[] pis = optionsType.GetProperties();

            foreach (PropertyInfo pi in pis)
            {
                string propertyName = pi.Name;
                if (propertyName.EndsWith("Checked") || propertyName.EndsWith("Count"))
                {
                    pi.SetValue(this, pi.GetValue(options, null), null);
                }
            }
        }
 public DeobfProfile(string name, DeobfOptions options) {
     this.Name = name;
     this.Options = options;
 }
        private void btnOK_Click(object sender, EventArgs e)
        {
            switch (btnOK.Text)
            {
            case Consts.Stop:
                _isCancelPending = true;
                return;

            default:
                break;
            }

            string outputDir = txtOutputDir.Text;

            if (!Directory.Exists(outputDir))
            {
                SimpleMessage.ShowInfo("Please choose output directory.");
                return;
            }

            if (_sourceDir != null && _sourceDir.Equals(outputDir))
            {
                Config.DeobfOutputDir = String.Empty;
            }
            else
            {
                Config.DeobfOutputDir = outputDir;
            }

            Config.DeobfFlowOptionBranchLoopCount = (int)nudLoopCount.Value;
            Config.DeobfFlowOptionMaxRefCount     = (int)nudMaxRefCount.Value;
            Config.LastRegex    = txtRegex.Text;
            Config.DeobfProfile = cboProfile.SelectedIndex;
            Config.DeobfFlowOptionBranchDirection = cboDirection.SelectedIndex;

            try
            {
                btnOK.Text       = Consts.Stop;
                _isCancelPending = false;

                Utils.EnableUI(this.Controls, false);

                DeobfOptions options = GetOptions();
                _host.TextInfo = options;
                Deobfuscator deobf = new Deobfuscator(options);
                deobf.Go();

                List <DeobfError> errors = deobf.Errors;
                if (errors.Count > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (DeobfError error in errors)
                    {
                        sb.Append(error.ToString());
                        sb.Append("\r\n\r\n");
                    }
                    SimpleMessage.ShowError(sb.ToString());
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                _host.TextInfo = null;
                btnOK.Text     = Consts.OK;

                Utils.EnableUI(this.Controls, true);
            }
        }
 public DeobfProfile(string name, DeobfOptions options)
 {
     this.Name    = name;
     this.Options = options;
 }
        private DeobfOptions GetOptions()
        {
            DeobfOptions options = new DeobfOptions();

            options.Host = _host;
            options.Rows = _rows;
            options.SourceDir = _sourceDir;
            options.OutputDir = txtOutputDir.Text;
            options.TextInfoBox = txtInfo;
            options.IsCancelPendingFunction = new DeobfOptions.IsCancelPendingDelegate(this.IsCancelPending);

            options.ApplyFrom(this);

            options.StringOptionSearchForMethod = (
                _selectedMethod != null &&
                !String.IsNullOrEmpty(txtMethod.Text)) ? _selectedMethod : null;

            options.StringOptionCalledMethod = (
                _selectedCalledMethod != null &&
                !String.IsNullOrEmpty(txtCalledMethod.Text)) ? _selectedCalledMethod : null;

            options.txtRegexText = txtRegex.Text;
            options.BranchDirection = (BranchDirections)cboDirection.SelectedIndex;
            options.PluginList = PluginUtils.GetSelectedPluginFromGrid(dgvPlugin);

            return options;
        }
        public void cmDeobf_Click(object sender, EventArgs e)
        {
            if (_currentMethod == null) return;

            if (_frmDeobfMethod == null)
            {
                _frmDeobfMethod = new frmDeobfMethod(_currentMethod);
            }
            else
            {
                _frmDeobfMethod.InitForm(_currentMethod);
            }

            if (_frmDeobfMethod.ShowDialog() != DialogResult.OK) return;

            bool resolveDirAdded = false;
            SimpleWaitCursor wc = new SimpleWaitCursor();
            try
            {
                resolveDirAdded = _form.Host.AddAssemblyResolveDir(_form.SourceDir);

                AssemblyDefinition ad = _currentMethod.DeclaringType.Module.Assembly;
                string file = null;
                foreach (TreeNode n in _form.TreeView.Nodes)
                {
                    if (ad.Equals(n.Tag))
                    {
                        file = _form.TreeViewHandler.GetTreeNodeText(n);
                        break;
                    }
                }

                if (file == null)
                    return;
                
                file = Path.Combine(_form.SourceDir, file);
                if (String.IsNullOrEmpty(file) || !File.Exists(file))
                {
                    throw new ApplicationException("Cannot find assembly file: " + file);
                }
                

                DeobfOptions options = new DeobfOptions();
                options.Clear();

                options.Host = _form.Host;
                options.Rows = new string[] { file };
                options.SourceDir = _form.SourceDir;
                options.OutputDir = _form.SourceDir;
                options.BranchDirection = _frmDeobfMethod.BranchDirection;

                options.LoopCount = Config.DeobfFlowOptionBranchLoopCount;
                options.MaxRefCount = Config.DeobfFlowOptionMaxRefCount;
                options.MaxMoveCount = Config.DeobfFlowOptionMaxMoveCount;

                options.chkPatternChecked = _frmDeobfMethod.AutoFlowPattern;
                options.chkBranchChecked = _frmDeobfMethod.AutoFlowBranch;
                options.chkCondBranchDownChecked = _frmDeobfMethod.AutoFlowConditionalBranchDown;
                options.chkCondBranchUpChecked = _frmDeobfMethod.AutoFlowConditionalBranchUp;
                
                options.chkSwitchChecked = _frmDeobfMethod.AutoFlowSwitch;
                options.chkUnreachableChecked = _frmDeobfMethod.AutoFlowUnreachable;
                options.chkRemoveExceptionHandlerChecked = _frmDeobfMethod.AutoFlowExceptionHandler;
                options.chkBoolFunctionChecked = _frmDeobfMethod.AutoFlowBoolFunction;
                options.chkDelegateCallChecked = _frmDeobfMethod.AutoFlowDelegateCall;
                options.chkDirectCallChecked = _frmDeobfMethod.AutoFlowDirectCall;
                options.chkBlockMoveChecked = _frmDeobfMethod.AutoFlowBlockMove;
                options.chkReflectorFixChecked = _frmDeobfMethod.AutoFlowReflectorFix;
                options.chkRemoveInvalidInstructionChecked = _frmDeobfMethod.AutoFlowRemoveInvalidInstruction;

                options.chkAutoStringChecked = _frmDeobfMethod.AutoString || _frmDeobfMethod.SelectedMethod != null;
                options.StringOptionSearchForMethod = _frmDeobfMethod.SelectedMethod;

                options.PluginList = _frmDeobfMethod.PluginList;
                options.chkInitLocalVarsChecked = _frmDeobfMethod.InitLocalVars;

                Deobfuscator deobf = new Deobfuscator(options);
                int deobfCount = deobf.DeobfMethod(file, _currentMethod);
                
                if (deobfCount > 0)
                {
                    InitBody(_currentMethod);
                }

                //need to be after InitBody
                _form.SetStatusCount(deobfCount);

            }
            catch (Exception ex)
            {
                SimpleMessage.ShowException(ex);
                InitBody(_currentMethod);
            }
            finally
            {
                if (resolveDirAdded)
                    _form.Host.RemoveAssemblyResolveDir(_form.SourceDir);
                wc.Dispose();
            }
        }
		public Deobfuscator(DeobfOptions options)
		{
			_options = options;
		}