AssemblyRef ReadAssemblyRef() {
			var asmRef = new AssemblyRefUser();
			if (ownerModule != null)
				ownerModule.UpdateRowId(asmRef);

			asmRef.Name = ReadId();
			SkipWhite();
			if (PeekChar() != ',')
				return asmRef;
			ReadChar();

			while (true) {
				SkipWhite();
				int c = PeekChar();
				if (c == -1 || c == ']')
					break;
				if (c == ',') {
					ReadChar();
					continue;
				}

				string key = ReadId();
				SkipWhite();
				if (PeekChar() != '=')
					continue;
				ReadChar();
				string value = ReadId();

				switch (key.ToUpperInvariant()) {
				case "VERSION":
					asmRef.Version = Utils.ParseVersion(value);
					break;

				case "PUBLICKEY":
					if (value.Equals("null", StringComparison.OrdinalIgnoreCase))
						asmRef.PublicKeyOrToken = new PublicKey();
					else
						asmRef.PublicKeyOrToken = PublicKeyBase.CreatePublicKey(Utils.ParseBytes(value));
					break;

				case "PUBLICKEYTOKEN":
					if (value.Equals("null", StringComparison.OrdinalIgnoreCase))
						asmRef.PublicKeyOrToken = new PublicKeyToken();
					else
						asmRef.PublicKeyOrToken = PublicKeyBase.CreatePublicKeyToken(Utils.ParseBytes(value));
					break;

				case "CULTURE":
					if (value.Equals("neutral", StringComparison.OrdinalIgnoreCase))
						asmRef.Culture = UTF8String.Empty;
					else
						asmRef.Culture = value;
					break;
				}
			}

			return asmRef;
		}
Example #2
0
 /// <summary>
 /// Called when no corlib assembly reference was found
 /// </summary>
 /// <returns></returns>
 AssemblyRef CreateDefaultCorLibAssemblyRef()
 {
     AssemblyRef asmRef;
     var asm = Assembly;
     if (asm != null && Find("System.Int32", false) != null)
         asmRef = new AssemblyRefUser(asm);
     else if (this.IsClr40)
         asmRef = AssemblyRefUser.CreateMscorlibReferenceCLR40();
     else if (this.IsClr20)
         asmRef = AssemblyRefUser.CreateMscorlibReferenceCLR20();
     else if (this.IsClr11)
         asmRef = AssemblyRefUser.CreateMscorlibReferenceCLR11();
     else if (this.IsClr10)
         asmRef = AssemblyRefUser.CreateMscorlibReferenceCLR10();
     else
         asmRef = AssemblyRefUser.CreateMscorlibReferenceCLR40();
     return UpdateRowId(asmRef);
 }
Example #3
0
		void OnWriterEvent(object sender, ModuleWriterListenerEventArgs e) {
			var writer = (ModuleWriterBase)sender;
			if (e.WriterEvent == ModuleWriterEvent.MDBeginAddResources) {
				ctx.Context.CheckCancellation();
				ctx.Context.Logger.Debug("Encrypting resources...");
				bool hasPacker = ctx.Context.Packer != null;

				List<EmbeddedResource> resources = ctx.Module.Resources.OfType<EmbeddedResource>().ToList();
				if (!hasPacker)
					ctx.Module.Resources.RemoveWhere(res => res is EmbeddedResource);

				// move resources
				string asmName = ctx.Name.RandomName(RenameMode.Letters);
				PublicKey pubKey = null;
				if (writer.TheOptions.StrongNameKey != null)
					pubKey = PublicKeyBase.CreatePublicKey(writer.TheOptions.StrongNameKey.PublicKey);
				var assembly = new AssemblyDefUser(asmName, new Version(0, 0), pubKey);
				assembly.Modules.Add(new ModuleDefUser(asmName + ".dll"));
				ModuleDef module = assembly.ManifestModule;
				assembly.ManifestModule.Kind = ModuleKind.Dll;
				var asmRef = new AssemblyRefUser(module.Assembly);
				if (!hasPacker) {
					foreach (EmbeddedResource res in resources) {
						res.Attributes = ManifestResourceAttributes.Public;
						module.Resources.Add(res);
						ctx.Module.Resources.Add(new AssemblyLinkedResource(res.Name, asmRef, res.Attributes));
					}
				}
				byte[] moduleBuff;
				using (var ms = new MemoryStream()) {
					module.Write(ms, new ModuleWriterOptions { StrongNameKey = writer.TheOptions.StrongNameKey });
					moduleBuff = ms.ToArray();
				}

				// compress
				moduleBuff = ctx.Context.Registry.GetService<ICompressionService>().Compress(
					moduleBuff,
					progress => ctx.Context.Logger.Progress((int)(progress * 10000), 10000));
				ctx.Context.Logger.EndProgress();
				ctx.Context.CheckCancellation();

				uint compressedLen = (uint)(moduleBuff.Length + 3) / 4;
				compressedLen = (compressedLen + 0xfu) & ~0xfu;
				var compressedBuff = new uint[compressedLen];
				Buffer.BlockCopy(moduleBuff, 0, compressedBuff, 0, moduleBuff.Length);
				Debug.Assert(compressedLen % 0x10 == 0);

				// encrypt
				uint keySeed = ctx.Random.NextUInt32() | 0x10;
				var key = new uint[0x10];
				uint state = keySeed;
				for (int i = 0; i < 0x10; i++) {
					state ^= state >> 13;
					state ^= state << 25;
					state ^= state >> 27;
					key[i] = state;
				}

				var encryptedBuffer = new byte[compressedBuff.Length * 4];
				int buffIndex = 0;
				while (buffIndex < compressedBuff.Length) {
					uint[] enc = ctx.ModeHandler.Encrypt(compressedBuff, buffIndex, key);
					for (int j = 0; j < 0x10; j++)
						key[j] ^= compressedBuff[buffIndex + j];
					Buffer.BlockCopy(enc, 0, encryptedBuffer, buffIndex * 4, 0x40);
					buffIndex += 0x10;
				}
				Debug.Assert(buffIndex == compressedBuff.Length);
				var size = (uint)encryptedBuffer.Length;

				TablesHeap tblHeap = writer.MetaData.TablesHeap;
				tblHeap.ClassLayoutTable[writer.MetaData.GetClassLayoutRid(ctx.DataType)].ClassSize = size;
				tblHeap.FieldTable[writer.MetaData.GetRid(ctx.DataField)].Flags |= (ushort)FieldAttributes.HasFieldRVA;
				encryptedResource = writer.Constants.Add(new ByteArrayChunk(encryptedBuffer), 8);

				// inject key values
				MutationHelper.InjectKeys(ctx.InitMethod,
				                          new[] { 0, 1 },
				                          new[] { (int)(size / 4), (int)(keySeed) });
			}
			else if (e.WriterEvent == ModuleWriterEvent.EndCalculateRvasAndFileOffsets) {
				TablesHeap tblHeap = writer.MetaData.TablesHeap;
				tblHeap.FieldRVATable[writer.MetaData.GetFieldRVARid(ctx.DataField)].RVA = (uint)encryptedResource.RVA;
			}
		}
        AssemblyRef ReadAssemblyRef()
        {
            var asmRef = new AssemblyRefUser();

            if (ownerModule != null)
            {
                ownerModule.UpdateRowId(asmRef);
            }

            asmRef.Name = ReadId();
            SkipWhite();
            if (PeekChar() != ',')
            {
                return(asmRef);
            }
            ReadChar();

            while (true)
            {
                SkipWhite();
                int c = PeekChar();
                if (c == -1 || c == ']')
                {
                    break;
                }
                if (c == ',')
                {
                    ReadChar();
                    continue;
                }

                string key = ReadId();
                SkipWhite();
                if (PeekChar() != '=')
                {
                    continue;
                }
                ReadChar();
                string value = ReadId();

                switch (key.ToUpperInvariant())
                {
                case "VERSION":
                    asmRef.Version = Utils.ParseVersion(value);
                    break;

                case "PUBLICKEY":
                    if (value.Equals("null", StringComparison.OrdinalIgnoreCase))
                    {
                        asmRef.PublicKeyOrToken = new PublicKey();
                    }
                    else
                    {
                        asmRef.PublicKeyOrToken = PublicKeyBase.CreatePublicKey(Utils.ParseBytes(value));
                    }
                    break;

                case "PUBLICKEYTOKEN":
                    if (value.Equals("null", StringComparison.OrdinalIgnoreCase))
                    {
                        asmRef.PublicKeyOrToken = new PublicKeyToken();
                    }
                    else
                    {
                        asmRef.PublicKeyOrToken = PublicKeyBase.CreatePublicKeyToken(Utils.ParseBytes(value));
                    }
                    break;

                case "CULTURE":
                    if (value.Equals("neutral", StringComparison.OrdinalIgnoreCase))
                    {
                        asmRef.Culture = UTF8String.Empty;
                    }
                    else
                    {
                        asmRef.Culture = value;
                    }
                    break;
                }
            }

            return(asmRef);
        }
Example #5
0
        AssemblyRef ReadAssemblyRef()
        {
            var asmRef = new AssemblyRefUser();
            if (ownerModule != null)
                ownerModule.UpdateRowId(asmRef);

            asmRef.Name = ReadId();
            SkipWhite();
            if (PeekChar() != ',')
                return asmRef;
            ReadChar();

            while (true) {
                SkipWhite();
                int c = PeekChar();
                if (c == -1 || c == ']')
                    break;
                if (c == ',') {
                    ReadChar();
                    continue;
                }

                string key = ReadId();
                SkipWhite();
                if (PeekChar() != '=')
                    continue;
                ReadChar();
                string value = ReadId();

                switch (key.ToUpperInvariant()) {
                case "VERSION":
                    asmRef.Version = Utils.ParseVersion(value);
                    break;

                case "CONTENTTYPE":
                    if (value.Equals("WindowsRuntime", StringComparison.OrdinalIgnoreCase))
                        asmRef.ContentType = AssemblyAttributes.ContentType_WindowsRuntime;
                    else
                        asmRef.ContentType = AssemblyAttributes.ContentType_Default;
                    break;

                case "RETARGETABLE":
                    if (value.Equals("Yes", StringComparison.OrdinalIgnoreCase))
                        asmRef.IsRetargetable = true;
                    else
                        asmRef.IsRetargetable = false;
                    break;

                case "PUBLICKEY":
                    if (value.Equals("null", StringComparison.OrdinalIgnoreCase) ||
                        value.Equals("neutral", StringComparison.OrdinalIgnoreCase))
                        asmRef.PublicKeyOrToken = new PublicKey();
                    else
                        asmRef.PublicKeyOrToken = PublicKeyBase.CreatePublicKey(Utils.ParseBytes(value));
                    break;

                case "PUBLICKEYTOKEN":
                    if (value.Equals("null", StringComparison.OrdinalIgnoreCase) ||
                        value.Equals("neutral", StringComparison.OrdinalIgnoreCase))
                        asmRef.PublicKeyOrToken = new PublicKeyToken();
                    else
                        asmRef.PublicKeyOrToken = PublicKeyBase.CreatePublicKeyToken(Utils.ParseBytes(value));
                    break;

                case "CULTURE":
                case "LANGUAGE":
                    if (value.Equals("neutral", StringComparison.OrdinalIgnoreCase))
                        asmRef.Culture = UTF8String.Empty;
                    else
                        asmRef.Culture = value;
                    break;
                }
            }

            return asmRef;
        }
Example #6
0
        AssemblyRef ReadAssemblyRef()
        {
            var asmRef = new AssemblyRefUser();

            if (ownerModule != null)
            {
                ownerModule.UpdateRowId(asmRef);
            }

            asmRef.Name = ReadAssemblyNameId();
            SkipWhite();
            if (PeekChar() != ',')
            {
                return(asmRef);
            }
            ReadChar();

            while (true)
            {
                SkipWhite();
                int c = PeekChar();
                if (c == -1 || c == ']')
                {
                    break;
                }
                if (c == ',')
                {
                    ReadChar();
                    continue;
                }

                string key = ReadId();
                SkipWhite();
                if (PeekChar() != '=')
                {
                    continue;
                }
                ReadChar();
                string value = ReadId();

                switch (key.ToUpperInvariant())
                {
                case "VERSION":
                    asmRef.Version = Utils.ParseVersion(value);
                    break;

                case "CONTENTTYPE":
                    if (value.Equals("WindowsRuntime", StringComparison.OrdinalIgnoreCase))
                    {
                        asmRef.ContentType = AssemblyAttributes.ContentType_WindowsRuntime;
                    }
                    else
                    {
                        asmRef.ContentType = AssemblyAttributes.ContentType_Default;
                    }
                    break;

                case "RETARGETABLE":
                    if (value.Equals("Yes", StringComparison.OrdinalIgnoreCase))
                    {
                        asmRef.IsRetargetable = true;
                    }
                    else
                    {
                        asmRef.IsRetargetable = false;
                    }
                    break;

                case "PUBLICKEY":
                    if (value.Equals("null", StringComparison.OrdinalIgnoreCase) ||
                        value.Equals("neutral", StringComparison.OrdinalIgnoreCase))
                    {
                        asmRef.PublicKeyOrToken = new PublicKey();
                    }
                    else
                    {
                        asmRef.PublicKeyOrToken = PublicKeyBase.CreatePublicKey(Utils.ParseBytes(value));
                    }
                    break;

                case "PUBLICKEYTOKEN":
                    if (value.Equals("null", StringComparison.OrdinalIgnoreCase) ||
                        value.Equals("neutral", StringComparison.OrdinalIgnoreCase))
                    {
                        asmRef.PublicKeyOrToken = new PublicKeyToken();
                    }
                    else
                    {
                        asmRef.PublicKeyOrToken = PublicKeyBase.CreatePublicKeyToken(Utils.ParseBytes(value));
                    }
                    break;

                case "CULTURE":
                case "LANGUAGE":
                    if (value.Equals("neutral", StringComparison.OrdinalIgnoreCase))
                    {
                        asmRef.Culture = UTF8String.Empty;
                    }
                    else
                    {
                        asmRef.Culture = value;
                    }
                    break;
                }
            }

            return(asmRef);
        }
Example #7
0
 AssemblyRef CreateCorLibAssemblyRef()
 {
     return(module.UpdateRowId(AssemblyRefUser.CreateMscorlibReferenceCLR20()));
 }