// i.e. Inter-Assembly References, e.g. InternalVisibleToAttributes public void Analyze(ConfuserContext context, INameService service, IDnlibDef def) { var module = def as ModuleDefMD; if (module == null) return; MDTable table; uint len; // MemberRef table = module.TablesStream.Get(Table.MemberRef); len = table.Rows; for (uint i = 1; i <= len; i++) { MemberRef memberRef = module.ResolveMemberRef(i); TypeDef declType = memberRef.DeclaringType.ResolveTypeDefThrow(); if (declType.Module != module && context.Modules.Contains((ModuleDefMD)declType.Module)) { var memberDef = (IDnlibDef)declType.ResolveThrow(memberRef); service.AddReference(memberDef, new MemberRefReference(memberRef, memberDef)); } } // TypeRef table = module.TablesStream.Get(Table.TypeRef); len = table.Rows; for (uint i = 1; i <= len; i++) { TypeRef typeRef = module.ResolveTypeRef(i); TypeDef typeDef = typeRef.ResolveTypeDefThrow(); if (typeDef.Module != module && context.Modules.Contains((ModuleDefMD)typeDef.Module)) { service.AddReference(typeDef, new TypeRefReference(typeRef, typeDef)); } } }
public AXAClaimController(IUnityContainer unityContainer, IClaimClientService claimClientService, IMetadataClientService metadataClientService, ApplicationModel applicationModel, AXAClaimModel claimModel, ClaimsPayloadManager claimPayloadManager, ClaimsRetrievalManager claimsRetrievalManager, IClaimNameSearchHandler claimNameSearchHandler, AppModel appModel, IClaimViewResolver viewResolver, AXAClaimAmountHistoryController claimAmountHistoryController, ISearchServiceHandler searchService, IClaimTreeviewResolver treeviewResolver, INameService nameService, IEventAggregator eventAggregator, IShellRulesHelper rulesHelper, IClaimsCodeLookupService claimscodelookupservice) : base(claimClientService, metadataClientService, claimModel, claimPayloadManager, claimsRetrievalManager, claimNameSearchHandler, appModel, viewResolver, claimAmountHistoryController, treeviewResolver, eventAggregator, rulesHelper, searchService, unityContainer, claimscodelookupservice) { this.appModel = appModel; this._gdiModel = Container.Resolve<AXAGenericDataItemModel>(); this.claimModel = claimModel; this.claimModel.GenericDataItemModel = this._gdiModel; this._payloadManager = claimPayloadManager; this._nameSearchHandler = claimNameSearchHandler; this.claimModel.ReloadTotals += new EventHandler(this.Model_ReloadTotals); this.claimModel.ReloadClaimants += new EventHandler(this.ClaimModel_ReloadClaimants); this.claimModel.DuplicateClaimCheckClick += new EventHandler<XIAP.Frontend.Infrastructure.Events.CommandEventArgs<ClaimHeaderDto>>(this.ClaimModel_DuplicateClaimCheckClick); this.claimModel.CustomCoverageVerification += new EventHandler(this.ClaimModel_CustomCoverageVerification); this.claimModel.NameInvolvementModel.GetNameForNameInvolvement += new EventHandler(this.NameInvolvementModel_GetNameForNameInvolvement); this.searchService = searchService; this.claimModel.TargetHeaderStatusCodeChanged += new EventHandler(this.ClaimModel_TargetHeaderStatusCodeChanged); this._genericDataSetBuilder = new ClaimsDataBuilder(claimPayloadManager); this._gdsController = new AXAGenericDataSetController((ClaimControllerBase<ClaimModel>)this, claimModel, this._gdiModel, this.Container, metadataClientService, this._genericDataSetBuilder); this.SetNumberOfGenericDataItemNodesToBeLoaded(); this._nameService = nameService; this.claimModel.ValidateClaim += new EventHandler(this.ClaimModel_ValidateClaim); this.claimAmountHistoryController = claimAmountHistoryController; this.claimAmountHistoryController.OnPaymentCancellation += new EventHandler(this.ClaimAmountHistoryController_OnPaymentCancellation); (this.claimModel.DocumentModel as AXADocumentModel).OnSaveClick += new EventHandler(this.AXAClaimController_OnSaveClick); this.retrievalManager = claimsRetrievalManager; (this.claimModel.DocumentModel as AXADocumentModel).RetrievalManager = claimsRetrievalManager; this.Model.HeaderPropertyChanged += new EventHandler<PropertyChangedEventArgs>(this.AXAClaimController_HeaderPropertyChanged); this.claimModel.OnINACTReviewClick += new EventHandler(this.ClaimModel_OnINACTReviewClick); this.claimModel.OnInactRecoveryReviewClick += new EventHandler(this.ClaimModel_OnInactRecoveryReviewClick); }
// i.e. Inter-Assembly References, e.g. InternalVisibleToAttributes public void Analyze(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def) { var module = def as ModuleDefMD; if (module == null) return; // MemberRef/MethodSpec var methods = module.GetTypes().SelectMany(type => type.Methods); foreach(var methodDef in methods) { foreach (var ov in methodDef.Overrides) { ProcessMemberRef(context, service, module, ov.MethodBody); ProcessMemberRef(context, service, module, ov.MethodDeclaration); } if (!methodDef.HasBody) continue; foreach (var instr in methodDef.Body.Instructions) { if (instr.Operand is MemberRef || instr.Operand is MethodSpec) ProcessMemberRef(context, service, module, (IMemberRef)instr.Operand); } } // TypeRef var table = module.TablesStream.Get(Table.TypeRef); uint len = table.Rows; for (uint i = 1; i <= len; i++) { TypeRef typeRef = module.ResolveTypeRef(i); TypeDef typeDef = typeRef.ResolveTypeDefThrow(); if (typeDef.Module != module && context.Modules.Contains((ModuleDefMD)typeDef.Module)) { service.AddReference(typeDef, new TypeRefReference(typeRef, typeDef)); } } }
public void Analyze(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def) { var method = def as MethodDef; if (method == null || !method.HasBody) return; // When a ldtoken instruction reference a definition, // most likely it would be used in reflection and thus probably should not be renamed. // Also, when ToString is invoked on enum, // the enum should not be renamed. for (int i = 0; i < method.Body.Instructions.Count; i++) { Instruction instr = method.Body.Instructions[i]; if (instr.OpCode.Code == Code.Ldtoken) { if (instr.Operand is MemberRef) { IMemberForwarded member = ((MemberRef)instr.Operand).ResolveThrow(); if (context.Modules.Contains((ModuleDefMD)member.Module)) service.SetCanRename(member, false); } else if (instr.Operand is IField) { FieldDef field = ((IField)instr.Operand).ResolveThrow(); if (context.Modules.Contains((ModuleDefMD)field.Module)) service.SetCanRename(field, false); } else if (instr.Operand is IMethod) { var im = (IMethod)instr.Operand; if (!im.IsArrayAccessors()) { MethodDef m = im.ResolveThrow(); if (context.Modules.Contains((ModuleDefMD)m.Module)) service.SetCanRename(method, false); } } else if (instr.Operand is ITypeDefOrRef) { if (!(instr.Operand is TypeSpec)) { TypeDef type = ((ITypeDefOrRef)instr.Operand).ResolveTypeDefThrow(); if (context.Modules.Contains((ModuleDefMD)type.Module) && HandleTypeOf(context, service, method, i)) { var t = type; do { DisableRename(service, t, false); t = t.DeclaringType; } while (t != null); } } } else throw new UnreachableException(); } else if ((instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt) && ((IMethod)instr.Operand).Name == "ToString") { HandleEnum(context, service, method, i); } else if (instr.OpCode.Code == Code.Ldstr) { TypeDef typeDef = method.Module.FindReflection((string)instr.Operand); if (typeDef != null) service.AddReference(typeDef, new StringTypeReference(instr, typeDef)); } } }
public JsonDataService( string dataRoot ) { DataRoot = dataRoot; Directory.CreateDirectory( DataRoot ); Directory.CreateDirectory( unitsDirectory = Path.Combine( DataRoot, "Units" ) ); Directory.CreateDirectory( placesDirectory = Path.Combine( DataRoot, "Places" ) ); Directory.CreateDirectory( playersDirectory = Path.Combine( DataRoot, "Players" ) ); _nameService = new DefaultNameService( dataRoot ); }
public void Analyze(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def) { if (def is TypeDef) Analyze(context, service, (TypeDef)def, parameters); else if (def is MethodDef) Analyze(context, service, (MethodDef)def, parameters); else if (def is PropertyDef) Analyze(context, service, (PropertyDef)def, parameters); else if (def is FieldDef) Analyze(context, service, (FieldDef)def, parameters); }
public void Analyze(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def) { var module = def as ModuleDef; if (module == null) return; string asmName = module.Assembly.Name.String; if (!string.IsNullOrEmpty(module.Assembly.Culture) && asmName.EndsWith(".resources")) { // Satellite assembly var satellitePattern = new Regex(string.Format("^(.*)\\.{0}\\.resources$", module.Assembly.Culture)); string nameAsmName = asmName.Substring(0, asmName.Length - ".resources".Length); ModuleDef mainModule = context.Modules.SingleOrDefault(mod => mod.Assembly.Name == nameAsmName); if (mainModule == null) { context.Logger.ErrorFormat("Could not find main assembly of satellite assembly '{0}'.", module.Assembly.FullName); throw new ConfuserException(null); } string format = "{0}." + module.Assembly.Culture + ".resources"; foreach (Resource res in module.Resources) { Match match = satellitePattern.Match(res.Name); if (!match.Success) continue; string typeName = match.Groups[1].Value; TypeDef type = mainModule.FindReflectionThrow(typeName); if (type == null) { context.Logger.WarnFormat("Could not find resource type '{0}'.", typeName); continue; } service.ReduceRenameMode(type, RenameMode.ASCII); service.AddReference(type, new ResourceReference(res, type, format)); } } else { string format = "{0}.resources"; foreach (Resource res in module.Resources) { Match match = ResourceNamePattern.Match(res.Name); if (!match.Success) continue; string typeName = match.Groups[1].Value; if (typeName.EndsWith(".g")) // WPF resources, ignore continue; TypeDef type = module.FindReflection(typeName); if (type == null) { context.Logger.WarnFormat("Could not find resource type '{0}'.", typeName); continue; } service.ReduceRenameMode(type, RenameMode.ASCII); service.AddReference(type, new ResourceReference(res, type, format)); } } }
public void Analyze(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def) { var method = def as MethodDef; if (method != null) { if (!method.HasBody) return; AnalyzeMethod(context, service, method); } var module = def as ModuleDefMD; if (module != null) { AnalyzeResources(context, service, module); } }
public void Analyze(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def) { if (def is ModuleDef) { foreach (var type in ((ModuleDef)def).GetTypes()) foreach (var prop in type.Properties) properties.AddListEntry(prop.Name, prop); return; } var method = def as MethodDef; if (method == null || !method.HasBody) return; AnalyzeMethod(context, service, method); }
public When_initialize_viewmodel() { // Setup test service with dummy data _service = Substitute.For<INameService>(); _service.GetNames().Returns(Task.FromResult(new[] { "Hello", "World" }.AsEnumerable())); var testScheduler = new TestScheduler(); _sut = new MainWindowViewModel(AsyncMethod.OnScheduler, _service, testScheduler); _sut.Initialize(); // The service has been subscribed to and everything is setup - just need kick it off testScheduler.Start(); }
public void PostRename(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def) { var module = def as ModuleDefMD; if (module == null) return; var wpfResInfo = context.Annotations.Get<Dictionary<string, Dictionary<string, BamlDocument>>>(module, BAMLKey); if (wpfResInfo == null) return; foreach (EmbeddedResource res in module.Resources.OfType<EmbeddedResource>()) { Dictionary<string, BamlDocument> resInfo; if (!wpfResInfo.TryGetValue(res.Name, out resInfo)) continue; var stream = new MemoryStream(); var writer = new ResourceWriter(stream); res.Data.Position = 0; var reader = new ResourceReader(new ImageStream(res.Data)); IDictionaryEnumerator enumerator = reader.GetEnumerator(); while (enumerator.MoveNext()) { var name = (string)enumerator.Key; string typeName; byte[] data; reader.GetResourceData(name, out typeName, out data); BamlDocument document; if (resInfo.TryGetValue(name, out document)) { var docStream = new MemoryStream(); docStream.Position = 4; BamlWriter.WriteDocument(document, docStream); docStream.Position = 0; docStream.Write(BitConverter.GetBytes((int)docStream.Length - 4), 0, 4); data = docStream.ToArray(); name = document.DocumentName; } writer.AddResourceData(name, typeName, data); } writer.Generate(); res.Data = MemoryImageStream.Create(stream.ToArray()); } }
void ProcessMemberRef(ConfuserContext context, INameService service, ModuleDefMD module, IMemberRef r) { var memberRef = r as MemberRef; if (r is MethodSpec) memberRef = ((MethodSpec)r).Method as MemberRef; if (memberRef != null) { if (memberRef.DeclaringType.TryGetArraySig() != null) return; TypeDef declType = memberRef.DeclaringType.ResolveTypeDefThrow(); if (declType.Module != module && context.Modules.Contains((ModuleDefMD)declType.Module)) { var memberDef = (IDnlibDef)declType.ResolveThrow(memberRef); service.AddReference(memberDef, new MemberRefReference(memberRef, memberDef)); } } }
void HandleEnum(ConfuserContext context, INameService service, MethodDef method, int index) { var target = (IMethod)method.Body.Instructions[index].Operand; if (target.FullName == "System.String System.Object::ToString()" || target.FullName == "System.String System.Enum::ToString(System.String)") { int prevIndex = index - 1; while (prevIndex >= 0 && method.Body.Instructions[prevIndex].OpCode.Code == Code.Nop) prevIndex--; if (prevIndex < 0) return; Instruction prevInstr = method.Body.Instructions[prevIndex]; TypeSig targetType; if (prevInstr.Operand is MemberRef) { var memberRef = (MemberRef)prevInstr.Operand; targetType = memberRef.IsFieldRef ? memberRef.FieldSig.Type : memberRef.MethodSig.RetType; } else if (prevInstr.Operand is IField) targetType = ((IField)prevInstr.Operand).FieldSig.Type; else if (prevInstr.Operand is IMethod) targetType = ((IMethod)prevInstr.Operand).MethodSig.RetType; else if (prevInstr.Operand is ITypeDefOrRef) targetType = ((ITypeDefOrRef)prevInstr.Operand).ToTypeSig(); else if (prevInstr.GetParameter(method.Parameters) != null) targetType = prevInstr.GetParameter(method.Parameters).Type; else if (prevInstr.GetLocal(method.Body.Variables) != null) targetType = prevInstr.GetLocal(method.Body.Variables).Type; else return; ITypeDefOrRef targetTypeRef = targetType.ToBasicTypeDefOrRef(); if (targetTypeRef == null) return; TypeDef targetTypeDef = targetTypeRef.ResolveTypeDefThrow(); if (targetTypeDef != null && targetTypeDef.IsEnum && context.Modules.Contains((ModuleDefMD)targetTypeDef.Module)) DisableRename(service, targetTypeDef); } }
void DisableRename(INameService service, TypeDef typeDef, bool memberOnly = true) { service.SetCanRename(typeDef, false); foreach (MethodDef m in typeDef.Methods) service.SetCanRename(m, false); foreach (FieldDef field in typeDef.Fields) service.SetCanRename(field, false); foreach (PropertyDef prop in typeDef.Properties) service.SetCanRename(prop, false); foreach (EventDef evt in typeDef.Events) service.SetCanRename(evt, false); foreach (TypeDef nested in typeDef.NestedTypes) DisableRename(service, nested, false); }
public void Analyze(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def) { var type = def as TypeDef; if (type == null || type.DeclaringType != null) return; if (type.Name.Contains("ViewModel")) { string viewNs = type.Namespace.Replace("ViewModels", "Views"); string viewName = type.Name.Replace("PageViewModel", "Page").Replace("ViewModel", "View"); TypeDef view = type.Module.Find(viewNs + "." + viewName, true); if (view != null) { service.SetCanRename(type, false); service.SetCanRename(view, false); } // Test for Multi-view string multiViewNs = type.Namespace + "." + type.Name.Replace("ViewModel", ""); foreach (var t in type.Module.Types) if (t.Namespace == multiViewNs) { service.SetCanRename(type, false); service.SetCanRename(t, false); } } }
public bool UpdateNameReference(ConfuserContext context, INameService service) { reference.Operand = typeDef.ReflectionFullName; return(true); }
/// <inheritdoc /> public bool DelayRenaming(INameService service, IDnlibDef currentDef) => false;
public BAMLAnalyzer(ConfuserContext context, INameService service) { this.context = context; this.service = service; PreInit(); }
void Analyze(ConfuserContext context, INameService service, FieldDef field, ProtectionParameters parameters) { if (ShouldExclude(field.DeclaringType, field)) { service.SetCanRename(field, false); } }
void Analyze(ConfuserContext context, INameService service, MethodDef method, ProtectionParameters parameters) { if (GetJsonContainerAttribute(method.DeclaringType) != null && method.IsConstructor) { service.SetParam(method, "renameArgs", "false"); } }
void AnalyzeResources(ConfuserContext context, INameService service, ModuleDefMD module) { if (analyzer == null) { analyzer = new BAMLAnalyzer(context, service); analyzer.AnalyzeElement += AnalyzeBAMLElement; } var wpfResInfo = new Dictionary<string, Dictionary<string, BamlDocument>>(); foreach (EmbeddedResource res in module.Resources.OfType<EmbeddedResource>()) { Match match = ResourceNamePattern.Match(res.Name); if (!match.Success) continue; var resInfo = new Dictionary<string, BamlDocument>(); res.Data.Position = 0; var reader = new ResourceReader(new ImageStream(res.Data)); IDictionaryEnumerator enumerator = reader.GetEnumerator(); while (enumerator.MoveNext()) { var name = (string)enumerator.Key; if (!name.EndsWith(".baml")) continue; string typeName; byte[] data; reader.GetResourceData(name, out typeName, out data); BamlDocument document = analyzer.Analyze(module, name, data); document.DocumentName = name; resInfo.Add(name, document); } if (resInfo.Count > 0) wpfResInfo.Add(res.Name, resInfo); } if (wpfResInfo.Count > 0) context.Annotations.Set(module, BAMLKey, wpfResInfo); }
public void PostRename(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def) { // }
public void PostRename(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def) { // }
public BAMLAnalyzer(ConfuserContext context, INameService service) { this.context = context; this.service = service; PreInit(); }
public FileService(IOutput output, INameService nameService) { _output = output; _nameService = nameService; }
public NameController(INameService nameService) { _nameService = nameService; }
public void PreRename(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def) { var module = def as ModuleDefMD; if (module == null || !parameters.GetParameter<bool>(context, def, "renXaml", true)) return; var wpfResInfo = context.Annotations.Get<Dictionary<string, Dictionary<string, BamlDocument>>>(module, BAMLKey); if (wpfResInfo == null) return; foreach (var res in wpfResInfo.Values) foreach (var doc in res.Values) { List<IBAMLReference> references; if (bamlRefs.TryGetValue(doc.DocumentName, out references)) { var newName = doc.DocumentName.ToUpperInvariant(); #region old code //if (newName.EndsWith(".BAML")) // newName = service.RandomName(RenameMode.Letters).ToLowerInvariant() + ".baml"; //else if (newName.EndsWith(".XAML")) // newName = service.RandomName(RenameMode.Letters).ToLowerInvariant() + ".xaml"; #endregion #region Niks patch fix /* * Nik's patch for maintaining relative paths. If the xaml file is referenced in this manner * "/some.namespace;component/somefolder/somecontrol.xaml" * then we want to keep the relative path and namespace intact. We should be obfuscating it like this - /some.namespace;component/somefolder/asjdjh2398498dswk.xaml * */ string[] completePath = newName.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries); string newShinyName = string.Empty; for (int i = 0; i <= completePath.Length - 2; i++) { newShinyName += completePath[i].ToLowerInvariant() + "/"; } if (newName.EndsWith(".BAML")) newName = newShinyName + service.RandomName(RenameMode.Letters).ToLowerInvariant() + ".baml"; else if (newName.EndsWith(".XAML")) newName = newShinyName + service.RandomName(RenameMode.Letters).ToLowerInvariant() + ".xaml"; context.Logger.Debug(String.Format("Preserving virtual paths. Replaced {0} with {1}", doc.DocumentName, newName)); #endregion bool renameOk = true; foreach (var bamlRef in references) if (!bamlRef.CanRename(doc.DocumentName, newName)) { renameOk = false; break; } if (renameOk) { foreach (var bamlRef in references) bamlRef.Rename(doc.DocumentName, newName); doc.DocumentName = newName; } } } }
void AnalyzeMethod(ConfuserContext context, INameService service, MethodDef method) { var dpRegInstrs = new List<Tuple<bool, Instruction>>(); var routedEvtRegInstrs = new List<Instruction>(); for (int i = 0; i < method.Body.Instructions.Count; i++) { Instruction instr = method.Body.Instructions[i]; if ((instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt)) { var regMethod = (IMethod)instr.Operand; if (regMethod.DeclaringType.FullName == "System.Windows.DependencyProperty" && regMethod.Name.String.StartsWith("Register")) { dpRegInstrs.Add(Tuple.Create(regMethod.Name.String.StartsWith("RegisterAttached"), instr)); } else if (regMethod.DeclaringType.FullName == "System.Windows.EventManager" && regMethod.Name.String == "RegisterRoutedEvent") { routedEvtRegInstrs.Add(instr); } } else if (instr.OpCode.Code == Code.Newobj) { var methodRef = (IMethod)instr.Operand; if (methodRef.DeclaringType.FullName == "System.Windows.Data.PropertyGroupDescription" && methodRef.Name == ".ctor" && i - 1 >= 0 && method.Body.Instructions[i - 1].OpCode.Code == Code.Ldstr) { foreach (var property in analyzer.LookupProperty((string)method.Body.Instructions[i - 1].Operand)) service.SetCanRename(property, false); } } else if (instr.OpCode == OpCodes.Ldstr) { var operand = ((string)instr.Operand).ToUpperInvariant(); if (operand.EndsWith(".BAML") || operand.EndsWith(".XAML")) { var match = UriPattern.Match(operand); if (match.Success) operand = match.Groups[1].Value; else if (operand.Contains("/")) context.Logger.WarnFormat("Fail to extract XAML name from '{0}'.", instr.Operand); var reference = new BAMLStringReference(instr); operand = operand.TrimStart('/'); var baml = operand.Substring(0, operand.Length - 5) + ".BAML"; var xaml = operand.Substring(0, operand.Length - 5) + ".XAML"; bamlRefs.AddListEntry(baml, reference); bamlRefs.AddListEntry(xaml, reference); } } } if (dpRegInstrs.Count == 0) return; var traceSrv = context.Registry.GetService<ITraceService>(); MethodTrace trace = traceSrv.Trace(method); bool erred = false; foreach (var instrInfo in dpRegInstrs) { int[] args = trace.TraceArguments(instrInfo.Item2); if (args == null) { if (!erred) context.Logger.WarnFormat("Failed to extract dependency property name in '{0}'.", method.FullName); erred = true; continue; } Instruction ldstr = method.Body.Instructions[args[0]]; if (ldstr.OpCode.Code != Code.Ldstr) { if (!erred) context.Logger.WarnFormat("Failed to extract dependency property name in '{0}'.", method.FullName); erred = true; continue; } var name = (string)ldstr.Operand; TypeDef declType = method.DeclaringType; bool found = false; if (instrInfo.Item1) // Attached DP { MethodDef accessor; if ((accessor = declType.FindMethod("Get" + name)) != null && accessor.IsStatic) { service.SetCanRename(accessor, false); found = true; } if ((accessor = declType.FindMethod("Set" + name)) != null && accessor.IsStatic) { service.SetCanRename(accessor, false); found = true; } } // Normal DP // Find CLR property for attached DP as well, because it seems attached DP can be use as normal DP as well. PropertyDef property = null; if ((property = declType.FindProperty(name)) != null) { service.SetCanRename(property, false); found = true; if (property.GetMethod != null) service.SetCanRename(property.GetMethod, false); if (property.SetMethod != null) service.SetCanRename(property.SetMethod, false); if (property.HasOtherMethods) { foreach (MethodDef accessor in property.OtherMethods) service.SetCanRename(accessor, false); } } if (!found) { if (instrInfo.Item1) context.Logger.WarnFormat("Failed to find the accessors of attached dependency property '{0}' in type '{1}'.", name, declType.FullName); else context.Logger.WarnFormat("Failed to find the CLR property of normal dependency property '{0}' in type '{1}'.", name, declType.FullName); } } erred = false; foreach (Instruction instr in routedEvtRegInstrs) { int[] args = trace.TraceArguments(instr); if (args == null) { if (!erred) context.Logger.WarnFormat("Failed to extract routed event name in '{0}'.", method.FullName); erred = true; continue; } Instruction ldstr = method.Body.Instructions[args[0]]; if (ldstr.OpCode.Code != Code.Ldstr) { if (!erred) context.Logger.WarnFormat("Failed to extract routed event name in '{0}'.", method.FullName); erred = true; continue; } var name = (string)ldstr.Operand; TypeDef declType = method.DeclaringType; EventDef eventDef = null; if ((eventDef = declType.FindEvent(name)) == null) { context.Logger.WarnFormat("Failed to find the CLR event of routed event '{0}' in type '{1}'.", name, declType.FullName); continue; } service.SetCanRename(eventDef, false); if (eventDef.AddMethod != null) service.SetCanRename(eventDef.AddMethod, false); if (eventDef.RemoveMethod != null) service.SetCanRename(eventDef.RemoveMethod, false); if (eventDef.InvokeMethod != null) service.SetCanRename(eventDef.InvokeMethod, false); if (eventDef.HasOtherMethods) { foreach (MethodDef accessor in eventDef.OtherMethods) service.SetCanRename(accessor, false); } } }
public void Analyze(ConfuserContext context, INameService service, IDnlibDef def) { var module = def as ModuleDefMD; if (module == null) return; MDTable table; uint len; // MemberRef table = module.TablesStream.Get(Table.Method); len = table.Rows; IEnumerable<MethodDef> methods = Enumerable.Range(1, (int)len) .Select(rid => module.ResolveMethod((uint)rid)); foreach (MethodDef method in methods) { foreach (MethodOverride methodImpl in method.Overrides) { if (methodImpl.MethodBody is MemberRef) AnalyzeMemberRef(context, service, (MemberRef)methodImpl.MethodBody); if (methodImpl.MethodDeclaration is MemberRef) AnalyzeMemberRef(context, service, (MemberRef)methodImpl.MethodDeclaration); } if (!method.HasBody) continue; foreach (Instruction instr in method.Body.Instructions) { if (instr.Operand is MemberRef) AnalyzeMemberRef(context, service, (MemberRef)instr.Operand); else if (instr.Operand is MethodSpec) { var spec = (MethodSpec)instr.Operand; if (spec.Method is MemberRef) AnalyzeMemberRef(context, service, (MemberRef)spec.Method); } } } // CustomAttribute table = module.TablesStream.Get(Table.CustomAttribute); len = table.Rows; IEnumerable<CustomAttribute> attrs = Enumerable.Range(1, (int)len) .Select(rid => module.ResolveHasCustomAttribute(module.TablesStream.ReadCustomAttributeRow((uint)rid).Parent)) .Distinct() .SelectMany(owner => owner.CustomAttributes); foreach (CustomAttribute attr in attrs) { if (attr.Constructor is MemberRef) AnalyzeMemberRef(context, service, (MemberRef)attr.Constructor); foreach (CAArgument arg in attr.ConstructorArguments) AnalyzeCAArgument(context, service, arg); foreach (CANamedArgument arg in attr.Fields) AnalyzeCAArgument(context, service, arg.Argument); foreach (CANamedArgument arg in attr.Properties) AnalyzeCAArgument(context, service, arg.Argument); TypeDef attrType = attr.AttributeType.ResolveTypeDefThrow(); if (!context.Modules.Contains((ModuleDefMD)attrType.Module)) continue; foreach (CANamedArgument fieldArg in attr.Fields) { FieldDef field = attrType.FindField(fieldArg.Name, new FieldSig(fieldArg.Type)); if (field == null) context.Logger.WarnFormat("Failed to resolve CA field '{0}::{1} : {2}'.", attrType, fieldArg.Name, fieldArg.Type); else service.AddReference(field, new CAMemberReference(fieldArg, field)); } foreach (CANamedArgument propertyArg in attr.Properties) { PropertyDef property = attrType.FindProperty(propertyArg.Name, new PropertySig(true, propertyArg.Type)); if (property == null) context.Logger.WarnFormat("Failed to resolve CA property '{0}::{1} : {2}'.", attrType, propertyArg.Name, propertyArg.Type); else service.AddReference(property, new CAMemberReference(propertyArg, property)); } } }
void Analyze(ConfuserContext context, INameService service, TypeDef type, ProtectionParameters parameters) { var attr = GetJsonContainerAttribute(type); if (attr == null) return; bool hasId = false; if (attr.HasConstructorArguments && attr.ConstructorArguments[0].Type.FullName == "System.String") hasId = true; else { foreach (var property in attr.Properties) { if (property.Name == "Id") hasId = true; } } if (!hasId) service.SetCanRename(type, false); }
public void PreRename(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def) { var module = def as ModuleDefMD; if (module == null || !parameters.GetParameter<bool>(context, def, "renXaml", true)) return; var wpfResInfo = context.Annotations.Get<Dictionary<string, Dictionary<string, BamlDocument>>>(module, BAMLKey); if (wpfResInfo == null) return; foreach (var res in wpfResInfo.Values) foreach (var doc in res.Values) { List<IBAMLReference> references; if (bamlRefs.TryGetValue(doc.DocumentName, out references)) { var newName = doc.DocumentName.ToUpperInvariant(); if (newName.EndsWith(".BAML")) newName = service.RandomName(RenameMode.Letters).ToLowerInvariant() + ".baml"; else if (newName.EndsWith(".XAML")) newName = service.RandomName(RenameMode.Letters).ToLowerInvariant() + ".xaml"; bool renameOk = true; foreach (var bamlRef in references) if (!bamlRef.CanRename(doc.DocumentName, newName)) { renameOk = false; break; } if (renameOk) { foreach (var bamlRef in references) bamlRef.Rename(doc.DocumentName, newName); doc.DocumentName = newName; } } } }
void Analyze(ConfuserContext context, INameService service, PropertyDef property, ProtectionParameters parameters) { if (ShouldExclude(property.DeclaringType, property)) { service.SetCanRename(property, false); } }
private void AnalyzeMethod(ConfuserContext context, INameService service, MethodDef method) { var dpRegInstrs = new List<Tuple<bool, Instruction>>(); var routedEvtRegInstrs = new List<Instruction>(); foreach (Instruction instr in method.Body.Instructions) { if ((instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt)) { var regMethod = (IMethod)instr.Operand; if (regMethod.DeclaringType.FullName == "System.Windows.DependencyProperty" && regMethod.Name.String.StartsWith("Register")) { dpRegInstrs.Add(Tuple.Create(regMethod.Name.String.StartsWith("RegisterAttached"), instr)); } else if (regMethod.DeclaringType.FullName == "System.Windows.EventManager" && regMethod.Name.String == "RegisterRoutedEvent") { routedEvtRegInstrs.Add(instr); } } } if (dpRegInstrs.Count == 0) return; var traceSrv = context.Registry.GetService<ITraceService>(); MethodTrace trace = traceSrv.Trace(method); bool erred = false; foreach (var instrInfo in dpRegInstrs) { int[] args = trace.TraceArguments(instrInfo.Item2); if (args == null) { if (!erred) context.Logger.WarnFormat("Failed to extract dependency property name in '{0}'.", method.FullName); erred = true; continue; } Instruction ldstr = method.Body.Instructions[args[0]]; if (ldstr.OpCode.Code != Code.Ldstr) { if (!erred) context.Logger.WarnFormat("Failed to extract dependency property name in '{0}'.", method.FullName); erred = true; continue; } var name = (string)ldstr.Operand; TypeDef declType = method.DeclaringType; bool found = false; if (instrInfo.Item1) // Attached DP { MethodDef accessor; if ((accessor = declType.FindMethod("Get" + name)) != null && accessor.IsStatic) { service.SetCanRename(accessor, false); found = true; } if ((accessor = declType.FindMethod("Set" + name)) != null && accessor.IsStatic) { service.SetCanRename(accessor, false); found = true; } } // Normal DP // Find CLR property for attached DP as well, because it seems attached DP can be use as normal DP as well. PropertyDef property = null; if ((property = declType.FindProperty(name)) != null) { found = true; if (property.GetMethod != null) service.SetCanRename(property.GetMethod, false); if (property.SetMethod != null) service.SetCanRename(property.SetMethod, false); if (property.HasOtherMethods) { foreach (MethodDef accessor in property.OtherMethods) service.SetCanRename(accessor, false); } } if (!found) { if (instrInfo.Item1) context.Logger.WarnFormat("Failed to find the accessors of attached dependency property '{0}' in type '{1}'.", name, declType.FullName); else context.Logger.WarnFormat("Failed to find the CLR property of normal dependency property '{0}' in type '{1}'.", name, declType.FullName); } } erred = false; foreach (Instruction instr in routedEvtRegInstrs) { int[] args = trace.TraceArguments(instr); if (args == null) { if (!erred) context.Logger.WarnFormat("Failed to extract routed event name in '{0}'.", method.FullName); erred = true; continue; } Instruction ldstr = method.Body.Instructions[args[0]]; if (ldstr.OpCode.Code != Code.Ldstr) { if (!erred) context.Logger.WarnFormat("Failed to extract routed event name in '{0}'.", method.FullName); erred = true; continue; } var name = (string)ldstr.Operand; TypeDef declType = method.DeclaringType; EventDef eventDef = null; if ((eventDef = declType.FindEvent(name)) == null) { context.Logger.WarnFormat("Failed to find the CLR event of routed event '{0}' in type '{1}'.", name, declType.FullName); continue; } if (eventDef.AddMethod != null) service.SetCanRename(eventDef.AddMethod, false); if (eventDef.RemoveMethod != null) service.SetCanRename(eventDef.RemoveMethod, false); if (eventDef.InvokeMethod != null) service.SetCanRename(eventDef.InvokeMethod, false); if (eventDef.HasOtherMethods) { foreach (MethodDef accessor in eventDef.OtherMethods) service.SetCanRename(accessor, false); } } }
void AnalyzeMethod(ConfuserContext context, INameService service, MethodDef method) { var dpRegInstrs = new List <Tuple <bool, Instruction> >(); var routedEvtRegInstrs = new List <Instruction>(); foreach (Instruction instr in method.Body.Instructions) { if ((instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt)) { var regMethod = (IMethod)instr.Operand; if (regMethod.DeclaringType.FullName == "System.Windows.DependencyProperty" && regMethod.Name.String.StartsWith("Register")) { dpRegInstrs.Add(Tuple.Create(regMethod.Name.String.StartsWith("RegisterAttached"), instr)); } else if (regMethod.DeclaringType.FullName == "System.Windows.EventManager" && regMethod.Name.String == "RegisterRoutedEvent") { routedEvtRegInstrs.Add(instr); } } else if (instr.OpCode == OpCodes.Ldstr) { var operand = ((string)instr.Operand).ToUpperInvariant(); if (operand.EndsWith(".BAML") || operand.EndsWith(".XAML")) { var match = UriPattern.Match(operand); if (match.Success) { operand = match.Groups[1].Value; } var reference = new BAMLStringReference(instr); operand = operand.TrimStart('/'); var baml = operand.Substring(0, operand.Length - 5) + ".BAML"; var xaml = operand.Substring(0, operand.Length - 5) + ".XAML"; bamlRefs.AddListEntry(baml, reference); bamlRefs.AddListEntry(xaml, reference); } } } if (dpRegInstrs.Count == 0) { return; } var traceSrv = context.Registry.GetService <ITraceService>(); MethodTrace trace = traceSrv.Trace(method); bool erred = false; foreach (var instrInfo in dpRegInstrs) { int[] args = trace.TraceArguments(instrInfo.Item2); if (args == null) { if (!erred) { context.Logger.WarnFormat("Failed to extract dependency property name in '{0}'.", method.FullName); } erred = true; continue; } Instruction ldstr = method.Body.Instructions[args[0]]; if (ldstr.OpCode.Code != Code.Ldstr) { if (!erred) { context.Logger.WarnFormat("Failed to extract dependency property name in '{0}'.", method.FullName); } erred = true; continue; } var name = (string)ldstr.Operand; TypeDef declType = method.DeclaringType; bool found = false; if (instrInfo.Item1) // Attached DP { MethodDef accessor; if ((accessor = declType.FindMethod("Get" + name)) != null && accessor.IsStatic) { service.SetCanRename(accessor, false); found = true; } if ((accessor = declType.FindMethod("Set" + name)) != null && accessor.IsStatic) { service.SetCanRename(accessor, false); found = true; } } // Normal DP // Find CLR property for attached DP as well, because it seems attached DP can be use as normal DP as well. PropertyDef property = null; if ((property = declType.FindProperty(name)) != null) { found = true; if (property.GetMethod != null) { service.SetCanRename(property.GetMethod, false); } if (property.SetMethod != null) { service.SetCanRename(property.SetMethod, false); } if (property.HasOtherMethods) { foreach (MethodDef accessor in property.OtherMethods) { service.SetCanRename(accessor, false); } } } if (!found) { if (instrInfo.Item1) { context.Logger.WarnFormat("Failed to find the accessors of attached dependency property '{0}' in type '{1}'.", name, declType.FullName); } else { context.Logger.WarnFormat("Failed to find the CLR property of normal dependency property '{0}' in type '{1}'.", name, declType.FullName); } } } erred = false; foreach (Instruction instr in routedEvtRegInstrs) { int[] args = trace.TraceArguments(instr); if (args == null) { if (!erred) { context.Logger.WarnFormat("Failed to extract routed event name in '{0}'.", method.FullName); } erred = true; continue; } Instruction ldstr = method.Body.Instructions[args[0]]; if (ldstr.OpCode.Code != Code.Ldstr) { if (!erred) { context.Logger.WarnFormat("Failed to extract routed event name in '{0}'.", method.FullName); } erred = true; continue; } var name = (string)ldstr.Operand; TypeDef declType = method.DeclaringType; EventDef eventDef = null; if ((eventDef = declType.FindEvent(name)) == null) { context.Logger.WarnFormat("Failed to find the CLR event of routed event '{0}' in type '{1}'.", name, declType.FullName); continue; } if (eventDef.AddMethod != null) { service.SetCanRename(eventDef.AddMethod, false); } if (eventDef.RemoveMethod != null) { service.SetCanRename(eventDef.RemoveMethod, false); } if (eventDef.InvokeMethod != null) { service.SetCanRename(eventDef.InvokeMethod, false); } if (eventDef.HasOtherMethods) { foreach (MethodDef accessor in eventDef.OtherMethods) { service.SetCanRename(accessor, false); } } } }
public AXADocumentDataController(IUnityContainer container, DocumentDataModel model, INameService nameService, AppModel appModel, IShellRulesHelper rulesHelper) : base(model, nameService, appModel, rulesHelper, container) { }
bool HandleTypeOf(ConfuserContext context, INameService service, MethodDef method, int index) { if (index + 1 >= method.Body.Instructions.Count) return true; var gtfh = method.Body.Instructions[index + 1].Operand as IMethod; if (gtfh == null || gtfh.FullName != "System.Type System.Type::GetTypeFromHandle(System.RuntimeTypeHandle)") return true; if (index + 2 < method.Body.Instructions.Count) { Instruction instr = method.Body.Instructions[index + 2]; var operand = instr.Operand as IMethod; if (instr.OpCode == OpCodes.Newobj && operand.FullName == "System.Void System.ComponentModel.ComponentResourceManager::.ctor(System.Type)") return false; if (instr.OpCode == OpCodes.Call || instr.OpCode == OpCodes.Callvirt) { switch (operand.DeclaringType.FullName) { case "System.Runtime.InteropServices.Marshal": return false; case "System.Type": if (operand.Name.StartsWith("Get") || operand.Name == "InvokeMember") return true; if (operand.Name == "get_AssemblyQualifiedName" || operand.Name == "get_FullName" || operand.Name == "get_Namespace") return true; return false; case "System.Reflection.MemberInfo": return operand.Name == "get_Name"; case "System.Object": return operand.Name == "ToString"; } } } if (index + 3 < method.Body.Instructions.Count) { Instruction instr = method.Body.Instructions[index + 3]; var operand = instr.Operand as IMethod; if (instr.OpCode == OpCodes.Call || instr.OpCode == OpCodes.Callvirt) { switch (operand.DeclaringType.FullName) { case "System.Runtime.InteropServices.Marshal": return false; } } } return false; }
static IEnumerable <IDnlibDef> GetTargetsWithDelay(IList <IDnlibDef> definitions, ConfuserContext context, INameService service) { var delayedItems = new List <IDnlibDef>(); var currentList = definitions; var lastCount = -1; while (currentList.Any()) { foreach (var def in currentList) { if (service.GetReferences(def).Any(r => r.DelayRenaming(service))) { delayedItems.Add(def); } else { yield return(def); } } if (delayedItems.Count == lastCount) { var errorBuilder = new StringBuilder(); errorBuilder.AppendLine("Failed to rename all targeted members, because the references are blocking each other."); errorBuilder.AppendLine("Remaining definitions: "); foreach (var def in delayedItems) { errorBuilder.Append("• ").AppendDescription(def, service).AppendLine(); } context.Logger.Warn(errorBuilder.ToString().Trim()); yield break; } lastCount = delayedItems.Count; currentList = delayedItems; delayedItems = new List <IDnlibDef>(); } }
public void PreRename(ConfuserContext context, INameService service, IDnlibDef def) { // }
private void AnalyzeMethod(ConfuserContext context, INameService service, MethodDef method) { var binding = new List <Tuple <bool, Instruction> >(); foreach (Instruction instr in method.Body.Instructions) { if ((instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt)) { var target = (IMethod)instr.Operand; if ((target.DeclaringType.FullName == "System.Windows.Forms.ControlBindingsCollection" || target.DeclaringType.FullName == "System.Windows.Forms.BindingsCollection") && target.Name == "Add" && target.MethodSig.Params.Count != 1) { binding.Add(Tuple.Create(true, instr)); } else if (target.DeclaringType.FullName == "System.Windows.Forms.Binding" && target.Name.String == ".ctor") { binding.Add(Tuple.Create(false, instr)); } } } if (binding.Count == 0) { return; } var traceSrv = context.Registry.GetService <ITraceService>(); MethodTrace trace = traceSrv.Trace(method); bool erred = false; foreach (var instrInfo in binding) { int[] args = trace.TraceArguments(instrInfo.Item2); if (args == null) { if (!erred) { context.Logger.WarnFormat("Failed to extract binding property name in '{0}'.", method.FullName); } erred = true; continue; } Instruction propertyName = method.Body.Instructions[args[0 + (instrInfo.Item1 ? 1 : 0)]]; if (propertyName.OpCode.Code != Code.Ldstr) { if (!erred) { context.Logger.WarnFormat("Failed to extract binding property name in '{0}'.", method.FullName); } erred = true; } else { List <PropertyDef> props; if (!properties.TryGetValue((string)propertyName.Operand, out props)) { if (!erred) { context.Logger.WarnFormat("Failed to extract target property in '{0}'.", method.FullName); } erred = true; } else { foreach (var property in props) { service.SetCanRename(property, false); } } } Instruction dataMember = method.Body.Instructions[args[2 + (instrInfo.Item1 ? 1 : 0)]]; if (dataMember.OpCode.Code != Code.Ldstr) { if (!erred) { context.Logger.WarnFormat("Failed to extract binding property name in '{0}'.", method.FullName); } erred = true; } else { List <PropertyDef> props; if (!properties.TryGetValue((string)dataMember.Operand, out props)) { if (!erred) { context.Logger.WarnFormat("Failed to extract target property in '{0}'.", method.FullName); } erred = true; } else { foreach (var property in props) { service.SetCanRename(property, false); } } } } }