Getting reference of calling Assembly.
-
How can I get a reference of the calling Assembly at Runtime? I've an assembly (RestrictedAssembly) and I want to ristrict access to it means no user who fullfills my criteria (in my case not having my desired public key token for assembly) can instantiate any type from my assembly. For better understanding, this is what I am trying to do. I've generated a public/private keypair file for myself, then I use it to sign all those assemblies which I want to allow Access to my RestrictedAssembly. On Runtime before instantiating any type from RestrictedAssembly, in constructor I am asking the caller to provide a Type object from which he's initiating a call to constructor. For example.
/**
* Constructor of public sealed class RestrictedType1
* Cotains in RestrictedAssembly.
* Caller is required to pass a type object
* o is the Type who wish to instantiate this class.
*/
public RestrictedType1(Type o) {
Type t = this.GetType ();
byte [] t1Token = o.Assembly.GetName().GetPublicKeyToken();
byte [] t2Token = t.Assembly.GetName().GetPublicKeyToken();
AccessNotAllowedEx ex = new AccessNotAllowedEx("Not Allowed");if(t1Token != null && t2Token != null) {
if(t1Token.Length == t2Token.Length) {
for(int i = 0; i< t1Token.Length; i++) {
if (t1Token[i] == t2Token[i])
continue;
else
throw ex;
}
// If get here successfullyDone;
ex = null;
} else
throw ex;
} else
throw ex;
}I am checking, if the public key tokens for both the calling and called assemblies are identical and if they are, then I am allowing the caller to instantiate my RestrictedType other wise I raise an exception. Now the problem with this approach is that, suppose I've an Assembly CallerOfRestrictedAssembly and I've signed it with my public/private key pair. Now from CallerOfRestrictedAssembly it is valid to do the following. Since both assemblies have same public key token.
RestrictedType1 res = new RestrictedType1(this.GetType);
However, suppose if some one initiate a call to RestrictedType1 constructor
RestrictedType1 res = new RestrictedType1(typeof(TypeFromCallerOfRestrictedAssembly));
This will cheat the constructor. Thus, I don't want caller to Pass Type argument into Constructor. and depend on it. What I am looking for is to get a reference of Calling Assembly into my Restrict
-
How can I get a reference of the calling Assembly at Runtime? I've an assembly (RestrictedAssembly) and I want to ristrict access to it means no user who fullfills my criteria (in my case not having my desired public key token for assembly) can instantiate any type from my assembly. For better understanding, this is what I am trying to do. I've generated a public/private keypair file for myself, then I use it to sign all those assemblies which I want to allow Access to my RestrictedAssembly. On Runtime before instantiating any type from RestrictedAssembly, in constructor I am asking the caller to provide a Type object from which he's initiating a call to constructor. For example.
/**
* Constructor of public sealed class RestrictedType1
* Cotains in RestrictedAssembly.
* Caller is required to pass a type object
* o is the Type who wish to instantiate this class.
*/
public RestrictedType1(Type o) {
Type t = this.GetType ();
byte [] t1Token = o.Assembly.GetName().GetPublicKeyToken();
byte [] t2Token = t.Assembly.GetName().GetPublicKeyToken();
AccessNotAllowedEx ex = new AccessNotAllowedEx("Not Allowed");if(t1Token != null && t2Token != null) {
if(t1Token.Length == t2Token.Length) {
for(int i = 0; i< t1Token.Length; i++) {
if (t1Token[i] == t2Token[i])
continue;
else
throw ex;
}
// If get here successfullyDone;
ex = null;
} else
throw ex;
} else
throw ex;
}I am checking, if the public key tokens for both the calling and called assemblies are identical and if they are, then I am allowing the caller to instantiate my RestrictedType other wise I raise an exception. Now the problem with this approach is that, suppose I've an Assembly CallerOfRestrictedAssembly and I've signed it with my public/private key pair. Now from CallerOfRestrictedAssembly it is valid to do the following. Since both assemblies have same public key token.
RestrictedType1 res = new RestrictedType1(this.GetType);
However, suppose if some one initiate a call to RestrictedType1 constructor
RestrictedType1 res = new RestrictedType1(typeof(TypeFromCallerOfRestrictedAssembly));
This will cheat the constructor. Thus, I don't want caller to Pass Type argument into Constructor. and depend on it. What I am looking for is to get a reference of Calling Assembly into my Restrict
-
How can I get a reference of the calling Assembly at Runtime? I've an assembly (RestrictedAssembly) and I want to ristrict access to it means no user who fullfills my criteria (in my case not having my desired public key token for assembly) can instantiate any type from my assembly. For better understanding, this is what I am trying to do. I've generated a public/private keypair file for myself, then I use it to sign all those assemblies which I want to allow Access to my RestrictedAssembly. On Runtime before instantiating any type from RestrictedAssembly, in constructor I am asking the caller to provide a Type object from which he's initiating a call to constructor. For example.
/**
* Constructor of public sealed class RestrictedType1
* Cotains in RestrictedAssembly.
* Caller is required to pass a type object
* o is the Type who wish to instantiate this class.
*/
public RestrictedType1(Type o) {
Type t = this.GetType ();
byte [] t1Token = o.Assembly.GetName().GetPublicKeyToken();
byte [] t2Token = t.Assembly.GetName().GetPublicKeyToken();
AccessNotAllowedEx ex = new AccessNotAllowedEx("Not Allowed");if(t1Token != null && t2Token != null) {
if(t1Token.Length == t2Token.Length) {
for(int i = 0; i< t1Token.Length; i++) {
if (t1Token[i] == t2Token[i])
continue;
else
throw ex;
}
// If get here successfullyDone;
ex = null;
} else
throw ex;
} else
throw ex;
}I am checking, if the public key tokens for both the calling and called assemblies are identical and if they are, then I am allowing the caller to instantiate my RestrictedType other wise I raise an exception. Now the problem with this approach is that, suppose I've an Assembly CallerOfRestrictedAssembly and I've signed it with my public/private key pair. Now from CallerOfRestrictedAssembly it is valid to do the following. Since both assemblies have same public key token.
RestrictedType1 res = new RestrictedType1(this.GetType);
However, suppose if some one initiate a call to RestrictedType1 constructor
RestrictedType1 res = new RestrictedType1(typeof(TypeFromCallerOfRestrictedAssembly));
This will cheat the constructor. Thus, I don't want caller to Pass Type argument into Constructor. and depend on it. What I am looking for is to get a reference of Calling Assembly into my Restrict
Syed Muhammad Kamran wrote:
How can I get a reference of the calling Assembly at Runtime?
You mean something like
Assembly.GetCallingAssembly()
method?? Don't forget to reference the System.Reflection namespace. PS. Even this got ways to be cracked with.Regards:rose:
-
Syed Muhammad Kamran wrote:
How can I get a reference of the calling Assembly at Runtime?
You mean something like
Assembly.GetCallingAssembly()
method?? Don't forget to reference the System.Reflection namespace. PS. Even this got ways to be cracked with.Regards:rose:
There are ways to even crack this..... You mean like this??
Assembly asm = Assembly.GetAssembly ( Type ( MyRestrictedAssembly ) );
byte[] b = asm.GetName ().GetPublicKey ();
this.GetType ().Assembly.GetName ().SetPublicKey ( b );
TypeFromRestrictedAssembly temp = new TypeFromRestrictedAssembly ( this.GetType ().Assembly );SMK
-
There are ways to even crack this..... You mean like this??
Assembly asm = Assembly.GetAssembly ( Type ( MyRestrictedAssembly ) );
byte[] b = asm.GetName ().GetPublicKey ();
this.GetType ().Assembly.GetName ().SetPublicKey ( b );
TypeFromRestrictedAssembly temp = new TypeFromRestrictedAssembly ( this.GetType ().Assembly );SMK
Well, not exactly. You can crack it using MSIL dissassembler. Just disassemble the code, copy/paste it, or remove the few lines that check for the calling assembly and save the dll files. Yes, it isn't that easy, but it's still doable. If you are not expecting hardcore persistant crackers to hut your application, it would be a nice way of protecting someone from using your code by referencing. Another way to protect your program from being used by reflection also could be the Dotfuscator. It's a small freeware tool to obfuscate your cde against disassembling. Still crackable, but a bit harder.
Regards:rose: