Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Getting reference of calling Assembly.

Getting reference of calling Assembly.

Scheduled Pinned Locked Moved C#
questionhelptutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Syed Muhammad Kamran
    wrote on last edited by
    #1

    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

    G N 2 Replies Last reply
    0
    • S Syed Muhammad Kamran

      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

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Please don't cross post.

      --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

      1 Reply Last reply
      0
      • S Syed Muhammad Kamran

        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

        N Offline
        N Offline
        Nader Elshehabi
        wrote on last edited by
        #3

        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:

        S 1 Reply Last reply
        0
        • N Nader Elshehabi

          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:

          S Offline
          S Offline
          Syed Muhammad Kamran
          wrote on last edited by
          #4

          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

          N 1 Reply Last reply
          0
          • S Syed Muhammad Kamran

            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

            N Offline
            N Offline
            Nader Elshehabi
            wrote on last edited by
            #5

            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:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups