.NET Framework - Prevent Reflection of Private Members

Asked By Matthias Truxa
09-Feb-10 09:45 AM
Hello NG,

I am working on a licensing component. In my validation class I'd like to
temporarily cache this and that using static fields. However, in order to not
compromize security, I have to prevent any foreign code to access or change
these private static fields. Using reflection (and proper rights) anyone
could easily reflect these fields and change them, thus cracking my
validation procedure.

I experimented with the ReflectionPermission attribute, however, I was not
able to achieve what I need (or I do not understand the concept of these
permissions).

What is the best way to secure a single class in an assembly against any
caller who wants to access privates?

Thank you,
Matt
StrongNameIdentityPermission
(1)
ReflectionPermission
(1)
Decompilers
(1)
Compromize
(1)
Privates
(1)
Metadata
(1)
Callers
(1)
  Willem van Rumpt replied to Matthias Truxa
09-Feb-10 10:14 AM
I am not an expert on permissions, but I think the ReflectionPermission
attribute will only be useful in scenario's when the caller does not have
full trust. If the assembly is to be deployed on an end-user machine, I
cannot prevent him having full trust.


Well, short of not deploying the assembly locally at all, I do not think
there is anything you can do to prevent it. Maybe the licensing classes
in the framework (http://msdn.microsoft.com/en-us/library/fe8b1eh9.aspx)
can help you achieve what you want, without rolling your own licensing
component?
  Matthias Truxa replied to Matthias Truxa
09-Feb-10 10:44 AM
Hello myself,

seems this is not possible at all - one cannot prevent foreign code against
reflecting metadata of .net types - too bad. Full-Trust callers can always do
that, and additionally there are unmanaged components that can populate
metadata. Hence the problem is the metadata itself, i.e. the byte-code nature
of .NET assemblies.

Not even the StrongNameIdentityPermission (only authentified callers allowed
to use some member) secures any longer since .NET 2.0.

Regards,
Matt
  Peter Duniho replied to Matthias Truxa
09-Feb-10 12:15 PM
No, the problem is fundamental to the way client applications work.  For
managed code, it is slightly easier to reverse-engineer the code, but
the same issue exists for unmanaged code too.  There have been
decompilers around since long before .NET has existed, including those
which work for native compiled code.

As Willem says, the only way to prevent someone else from reverse
engineering your code is to not deliver it to them in the first place.
This is true no matter what language, framework, compilation, etc. that
the code uses.

Frankly, had .NET attempted to enforce reflection-based restrictions,
those restrictions would only provide a false sense of security.  The
way things are now, you have a very realistic sense of what is possible
and what is not.  If .NET had tried to prevent reflection of code based
on security permissions, it would simply have misled people into
thinking that there is a way to prevent reflection of code based on
security permissions.

Pete
Create New Account
help
StrongNameIdentityPermission not working in 2005 .NET Framework Hi all, I am having application in 2003 where and two console application. In class library i have one class to which i set strongnameidentitypermission. When i try to use this class in first console application which is having same expose to other. Please tell me why this is happening. thanks in advance. C# Discussions StrongNameIdentityPermission (1) Ful (1) Publickkey (1) Contrary to the Framework Version 1 (VS 2003), in the Framework version 2.0 (VS 2005) the StrongNameIdentityPermission is granted by default to all assemblies that run with Ful Trust, regardless of their 1, an assembly with Full Trust would be able to use Reflection to bypass a StrongNameIdentityPermission, so this permission demand wouldn't be a secure way to prevent access to your assembly even when using VS 2003. keywords: StrongNameIdentityPermission, not, working, in, 2005 description: Hi all, I am having application in 2003 where i
the assembly level I have: [assembly: PermissionSet(SecurityAction.RequestOptional)] at the method level I have [ReflectionPermission(SecurityAction.Demand, Flags = ReflectionPermissionFlag.RestrictedMemberAccess, RestrictedMemberAccess = true)] my method is empty. However this gives me this message: Request for the permission of type ". . ReflectionPermission. . ." failed Can you mix and match these permissions? I thought the method level permission was an explicit Demand for the security so would be ok? thanks C# Discussions ReflectionPermissionFlag (1) ReflectionPermission (1) SecurityAction (1) PermissionSet (1) AssemblyInfo (1) StrongName (1) Visual (1) Demand (1) Also, this
client to use or know about other internal classes in the assembly. Thanks C# Discussions StrongNameIdentityPermission (1) SecurityException (1) GetEntryAssembly (1) GetPublicKey (1) GetAssembly (1) GetName (1) InternalsVisibleTo (1) CheckAssemblies (1 someone could provide some specific advice that addresses your need. Pete You can use the StrongNameIdentityPermission to limit calling code to known assemblies. http: / / msdn.microsoft.com / en-us / library / system.security.permissions.strongnameidentitypermission.aspx - - Happy Coding! Morten Wennevik [C# MVP] ly wn Class cannot be internal, hence I and complete code sample that illustrates why you can't use internal classes? Chris Using StrongNameIdentityPermission it doesn't matter what visibility the classes have. You can even put it on