Is it Possible to Prevent Renaming for Returned Anonymous Classes

Issues related to VMProtect
Locked
weloveayaka
Posts: 66
Joined: Wed Jul 05, 2023 6:21 am

Is it Possible to Prevent Renaming for Returned Anonymous Classes

Post by weloveayaka »

when enabling renaming, following code:

Code: Select all

 public static dynamic testAnonymousType()
    {
        var obj = new { Name = "John", Age = 30 };
        return obj;
    }
The properties Name and Age are also renamed due to the compiler generating internal classes and private fields. This behavior can lead to issues with JSON serialization, as the property names become incorrect.

Since anonymous classes cannot apply attributes, and applying [Obfuscation(Feature = "renaming", Exclude = true)] for this method does not work, is there a way to prevent such renaming? Or is disabling renaming globally the only solution?
(add [assembly: Obfuscation(Feature = "renaming", Exclude = true)]
and add [Obfuscation(Feature = "renaming", Exclude = false, ApplyToMembers = true)] each class -> this works)
Admin
Site Admin
Posts: 2686
Joined: Mon Aug 21, 2006 8:19 pm
Location: Russia, E-burg
Contact:

Re: Is it Possible to Prevent Renaming for Returned Anonymous Classes

Post by Admin »

Please show me how you use serialization for such types.
weloveayaka
Posts: 66
Joined: Wed Jul 05, 2023 6:21 am

Re: Is it Possible to Prevent Renaming for Returned Anonymous Classes

Post by weloveayaka »

following code throws Microsoft.CSharp.RuntimeBinder.RuntimeBinderException class XXX does not contain a definition for "Name"
no assembly level renaming Obfuscation attribute

Code: Select all

    [Obfuscation(Feature = "ultra", Exclude = true)]
    public static dynamic testAnonymousType2()
    {
        var obj = new { Name = "John", Age = 30 };
        return obj;
    }
    public static void testAnonymousType()
    {
        var obj = testAnonymousType2();
        Console.WriteLine($"Name: {obj.Name}, Age: {obj.Age} {obj.ToString()}");
    }
 
weloveayaka
Posts: 66
Joined: Wed Jul 05, 2023 6:21 am

Re: Is it Possible to Prevent Renaming for Returned Anonymous Classes

Post by weloveayaka »

Admin wrote: Tue Apr 02, 2024 3:47 am Please show me how you use serialization for such types.

Code: Select all

 public static dynamic testAnonymousType2()
    {
        var obj = new { Name = "John", Age = 30 };
        var converter = new System.Web.Script.Serialization.JavaScriptSerializer();
        Console.WriteLine("ToJSON1: " + converter.Serialize(obj));
        return obj;
    }
 
In real project, we are in external assembly to do this.
weloveayaka
Posts: 66
Joined: Wed Jul 05, 2023 6:21 am

Re: Is it Possible to Prevent Renaming for Returned Anonymous Classes

Post by weloveayaka »

a more serious problem:

When we use we mentioned workaround at the topic:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: “F6B57532” does not contains a defination for "testCallWithDynamicClass"

Code: Select all


[assembly: Obfuscation(Feature = "renaming", Exclude = true)]

namespace XXX
{
    Class Test{
	     void Main(){
	              var ttt = new TestDynamic();
                      ttt.testCallWithDynamic();
	     }
	}
    [Obfuscation(Feature = "renaming", Exclude = false, ApplyToMembers = true)] 
    public class TestDynamic
    {

        static void testCallWithDynamicClass(string xxx)
        {
            Console.WriteLine("testCallWithDynamicClass: " + xxx);
        }
        
        public void testCallWithDynamic()
        {
            dynamic d = new { Name = "abc" };
            testCallWithDynamicClass(d.Name);       /* because C# compiler use CallSite to make this call and uses string "testCallWithDynamicClass" to call the method, which is renamed by VMP.   */
        }
    }
}
if we dont add [assembly: Obfuscation(Feature = "renaming", Exclude = true)] and [Obfuscation(Feature = "renaming", Exclude = false, ApplyToMembers = true)] on the class
there is also RuntimeBinderException: does not contains a defination for "Name"
Admin
Site Admin
Posts: 2686
Joined: Mon Aug 21, 2006 8:19 pm
Location: Russia, E-burg
Contact:

Re: Is it Possible to Prevent Renaming for Returned Anonymous Classes

Post by Admin »

I have already wrote that the following code works like force renaming:

Code: Select all

[Obfuscation(Feature = "renaming", Exclude = false)]
Force renaming means that all symbols (including methods) pinned to this attribute will be renaming even the methods were used for dynamic calls. Please don't use this feature if you are not sure that you need it.

P.S. "[assembly: Obfuscation..." means that you use this attribute for all objects in the assembly by default. Obfuscation attributes specified for class/method/field/property override attributes of outer classes/assembly.
weloveayaka
Posts: 66
Joined: Wed Jul 05, 2023 6:21 am

Re: Is it Possible to Prevent Renaming for Returned Anonymous Classes

Post by weloveayaka »

Thank you for the clarification. We are using the renaming attribute solely as a workaround for the main issue at original post. However, the current problem persists even without any renaming tags, as dynamics are still encountering issues.

so the problem is RuntimeBinderException and JSON property has wrong name due to compiler generated class is internal and fields are private.
Last edited by weloveayaka on Tue Apr 02, 2024 8:26 am, edited 1 time in total.
Admin
Site Admin
Posts: 2686
Joined: Mon Aug 21, 2006 8:19 pm
Location: Russia, E-burg
Contact:

Re: Is it Possible to Prevent Renaming for Returned Anonymous Classes

Post by Admin »

Because you have to rremove the force renaming feature for all classes with dynamic calls!

P.S. The topic is closed.
weloveayaka
Posts: 66
Joined: Wed Jul 05, 2023 6:21 am

Re: Is it Possible to Prevent Renaming for Returned Anonymous Classes

Post by weloveayaka »

Admin wrote: Tue Apr 02, 2024 8:24 am Because you have to rremove the force renaming feature for all classes with dynamic calls!
Yes, I removed all renaming feature
Locked