Example #1
0
        private void InitForm(object o)
        {
            if (o is MemberReference)
            {
                MemberReference mr = (MemberReference)o;
                txtOriginalName.Text = InsUtils.GetOldMemberName(mr);
                txtCurrentName.Text  = mr.Name;
            }
            else if (o is Resource)
            {
                Resource r = (Resource)o;
                txtOriginalName.Text = String.Empty;
                txtCurrentName.Text  = r.Name;
            }
            else
            {
                txtOriginalName.Text = String.Empty;
                txtCurrentName.Text  = o.ToString();
            }

            if (!String.IsNullOrEmpty(txtOriginalName.Text))
            {
                byte[] bytes = Encoding.Unicode.GetBytes(txtOriginalName.Text);
                txtOriginalNameHex.Text = BytesUtils.BytesToHexString(bytes, true);
            }

            if (!String.IsNullOrEmpty(txtCurrentName.Text))
            {
                byte[] bytes = Encoding.Unicode.GetBytes(txtCurrentName.Text);
                txtCurrentNameHex.Text = BytesUtils.BytesToHexString(bytes, true);
            }
        }
        private string GetCAInternalVisibleToReferenceString(string keyFile)
        {
            //PublicKey=00240000048000009400000006020000002400005253413100040000010001007b92cbd52d63863dde18c3fcbfcb11380ad0030031dec9f977ed149dcc4c802b4e6e9701c64bb6fd9a4031a2c8c02877b0a698c994a6c26fea913b2e01f52a0db7e6d00a4048ecab3f7de211a6c23cbf5d74eec6318c001395599ceb92a715ae9d4c1431fc31324c1163adfa623842aed9a70218ba9f0e9442c7a98e2c930f9e
            byte[] pubKey = TokenUtils.GetPublicKeyFromKeyFile(keyFile);
            string s      = BytesUtils.BytesToHexString(pubKey);

            return(String.Format("PublicKey={0}", s));
        }
 public static string GetReferenceTokenString(byte[] token)
 {
     if (token != null && token.Length > 0)
     {
         return(BytesUtils.BytesToHexString(token));
     }
     return("null");
 }
        private void ReplaceResourcesToken(byte[] bytes)
        {
            string searchToken = "PublicKeyToken=" + Options.txtOldTokenText;

            byte[] searchTokenBytes = Encoding.UTF8.GetBytes(searchToken);

            string replaceToken = "PublicKeyToken=" + Options.txtNewTokenText;

            byte[] replaceTokenBytes = Encoding.UTF8.GetBytes(replaceToken);

            int index = BytesUtils.ByteSearch(bytes, searchTokenBytes, 0, bytes.Length);

            while (index > 0)
            {
                for (int i = 0; i < replaceTokenBytes.Length; i++)
                {
                    bytes[index + i] = replaceTokenBytes[i];
                }
                index = BytesUtils.ByteSearch(bytes, searchTokenBytes, index + searchTokenBytes.Length, bytes.Length - index - searchTokenBytes.Length);
            }
        }
        private string ReplaceReferencesToken(string file)
        {
            string newFile = Path.ChangeExtension(file, ".Repl.il");

            if (File.Exists(newFile))
            {
                File.Delete(newFile);
            }

            StreamReader sr = null;
            StreamWriter sw = null;

            try
            {
                string oldToken = Options.txtOldTokenText;
                string newToken = Options.txtNewTokenText;

                string oldStr1 = GetAssemblyReferenceString(oldToken);
                string newStr1 = GetAssemblyReferenceString(newToken);

                string oldStr2 = GetCAReferenceString(oldToken);
                string newStr2 = GetCAReferenceString(newToken);

                string newStr3 = GetCAInternalVisibleToReferenceString(Options.ReplKeyFile);
                Regex  rgStr3  = new Regex(@"PublicKey=[0123456789abcdefABCDEF]*");

                string oldBin2 = BytesUtils.BytesToHexString(Encoding.ASCII.GetBytes(oldStr2), true);
                string newBin2 = BytesUtils.BytesToHexString(Encoding.ASCII.GetBytes(newStr2), true);

                string newBin3 = BytesUtils.BytesToHexString(Encoding.ASCII.GetBytes(newStr3), true);
                //PublicKey=50 75 62 6C 69 63 4B 65 79 3D
                Regex rgBin3 = new Regex(@"50 75 62 6C 69 63 4B 65 79 3D [\d\s]+");

                sr = new StreamReader(file);
                sw = new StreamWriter(newFile, false, System.Text.Encoding.Unicode);

                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.StartsWith(".assembly extern"))
                    {
                        sw.WriteLine(line);
                        while ((line = sr.ReadLine()) != null)
                        {
                            if (line.IndexOf(oldStr1) >= 0)
                            {
                                line = line.Replace(oldStr1, newStr1);
                            }

                            sw.WriteLine(line);

                            if (line == "}")
                            {
                                break;
                            }
                        }
                    }//end of if .assembly extern
                    else if (line.StartsWith("  .custom"))
                    {
                        StringBuilder sb = new StringBuilder();
                        //string lineStart = null;
                        string lineEnd  = null;
                        bool   isBinary = true;

                        while (line != null)
                        {
                            //remove comment
                            int p = line.LastIndexOf(" //");
                            if (p >= 0)
                            {
                                line = line.Substring(0, p);
                            }

                            line = line.Trim();
                            sb.Append(line);
                            sb.Append(" ");

                            if (lineEnd == null)
                            {
                                if (line.IndexOf("= {") >= 0)
                                {
                                    //lineStart = "= {";
                                    lineEnd  = "}";
                                    isBinary = false;
                                }
                                else if (line.IndexOf("= (") >= 0)
                                {
                                    //lineStart = "= (";
                                    lineEnd  = ")";
                                    isBinary = true;
                                }
                            }

                            if (lineEnd != null && line.EndsWith(lineEnd))
                            {
                                break;
                            }

                            line = sr.ReadLine();
                        }

                        line = sb.ToString();

                        if (isBinary)
                        {
                            if (line.IndexOf(oldBin2) > 0)
                            {
                                line = line.Replace(oldBin2, newBin2);
                            }
                            else
                            {
                                Match m = rgBin3.Match(line);
                                if (m.Success)
                                {
                                    string pubKeyString = m.Value.Trim().Substring("50 75 62 6C 69 63 4B 65 79 3D ".Length);
                                    pubKeyString = pubKeyString.Substring(0, pubKeyString.Length - 6); //remove " 00 00"
                                    byte[] pubKey = BytesUtils.HexStringToBytes(Encoding.ASCII.GetString(BytesUtils.HexStringToBytes(pubKeyString)));
                                    string token  = TokenUtils.GetPublicKeyTokenString(TokenUtils.GetPublicKeyToken(pubKey, Mono.Cecil.AssemblyHashAlgorithm.SHA1));
                                    if (token.Equals(oldToken, StringComparison.CurrentCultureIgnoreCase))
                                    {
                                        line = rgBin3.Replace(line, newBin3 + "00 00 ");
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (line.IndexOf(oldStr2) > 0)
                            {
                                line = line.Replace(oldStr2, newStr2);
                            }
                            else
                            {
                                Match m = rgStr3.Match(line);
                                if (m.Success)
                                {
                                    string pubKeyString = m.Value.Substring("PublicKey=".Length);
                                    byte[] pubKey       = BytesUtils.HexStringToBytes(pubKeyString);
                                    string token        = TokenUtils.GetPublicKeyTokenString(TokenUtils.GetPublicKeyToken(pubKey, Mono.Cecil.AssemblyHashAlgorithm.SHA1));
                                    if (token.Equals(oldToken, StringComparison.CurrentCultureIgnoreCase))
                                    {
                                        line = rgStr3.Replace(line, newStr3);
                                    }
                                }
                            }
                        }

                        sw.WriteLine(line);
                    }//end of if .custom
                    else
                    {
                        sw.WriteLine(line);
                    }
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }
                if (sw != null)
                {
                    sw.Close();
                }
            }
            return(newFile);
        }
Example #6
0
        private void FixByteReference(byte[] bytes, string adName, byte[] token)
        {
            const int TOKEN_LEN = 16;

            string searchName = String.Format("{0}, Version=", adName);

            byte[] searchBytes = Encoding.UTF8.GetBytes(searchName);

            string searchToken = "PublicKeyToken=";

            byte[] searchTokenBytes = Encoding.UTF8.GetBytes(searchToken);

            string searchNull = "null";

            byte[] searchNullBytes = Encoding.UTF8.GetBytes(searchNull);

            string searchNullLong = "null            ";//16 bytes

            byte[] searchNullLongBytes = Encoding.UTF8.GetBytes(searchNullLong);

            string tokenString = TokenUtils.GetReferenceTokenString(token);

            byte[] tokenStringBytes = Encoding.UTF8.GetBytes(tokenString);

            int index = BytesUtils.ByteSearch(bytes, searchBytes, 0, bytes.Length);

            while (index > 0)
            {
                index = BytesUtils.ByteSearch(bytes, searchTokenBytes, index + searchBytes.Length, bytes.Length - index - searchBytes.Length);
                if (index > 0)
                {
                    index = index + searchTokenBytes.Length;

                    if (token == null)
                    {
                        if (BytesUtils.ByteSearch(bytes, searchNullBytes, index, searchNullBytes.Length) == index)
                        {
                        }
                        else
                        {
                            for (int i = 0; i < searchNullBytes.Length; i++)
                            {
                                bytes[index + i] = searchNullBytes[i];
                            }
                            index += searchNullBytes.Length;
                            for (int i = 0; i < TOKEN_LEN - searchNullBytes.Length; i++)
                            {
                                bytes[index + i] = 0x20;
                            }
                        }
                    }
                    else
                    {
                        if (BytesUtils.ByteSearch(bytes, searchNullBytes, index, searchNullBytes.Length) == index)
                        {
                            if (BytesUtils.ByteSearch(bytes, searchNullLongBytes, index, searchNullLongBytes.Length) == index)
                            {
                                for (int i = 0; i < tokenStringBytes.Length; i++)
                                {
                                    bytes[index + i] = tokenStringBytes[i];
                                }
                            }
                            else
                            {
                                #region complicated, found PublicKeyToken=null and need to extend space to sign, ignore now

                                /*
                                 * //1. extend bytes array
                                 * //2. amend bytes length for custom attribute or resource
                                 * //
                                 * // below is old code for custom attribute only
                                 * {
                                 *  int endIndex = index + 4;
                                 *  int sizeIndex = 2;
                                 *  bool ok = false;
                                 *  if (endIndex - sizeIndex - 1 == (int)bytes[sizeIndex])
                                 *  {
                                 *      ok = true;
                                 *  }
                                 *  else
                                 *  {
                                 *      sizeIndex++;
                                 *      if (endIndex - sizeIndex - 1 == (int)bytes[sizeIndex])
                                 *      {
                                 *          ok = true;
                                 *      }
                                 *  }
                                 *
                                 *  if (ok)//need to extend
                                 *  {
                                 *      byte[] newBytes = new byte[bytes.Length + 12];
                                 *      Array.Copy(bytes, newBytes, index);
                                 *      Array.Copy(tokenStringBytes, newBytes, tokenStringBytes.Length);
                                 *      for (int i = index + 4; i < bytes.Length; i++)
                                 *      {
                                 *          newBytes[i + 12] = bytes[i];
                                 *      }
                                 *      newBytes[sizeIndex] += 12;
                                 *
                                 *      result = newBytes;
                                 *      bytes = newBytes;
                                 *  }
                                 *  else
                                 *  {
                                 *      endIndex = index + 16;
                                 *      sizeIndex = 2;
                                 *      ok = false;
                                 *      if (endIndex - sizeIndex - 1 == (int)bytes[sizeIndex])
                                 *      {
                                 *          ok = true;
                                 *      }
                                 *      else
                                 *      {
                                 *          sizeIndex++;
                                 *          if (endIndex - sizeIndex - 1 == (int)bytes[sizeIndex])
                                 *          {
                                 *              ok = true;
                                 *          }
                                 *      }
                                 *
                                 *      if (ok) //enough room
                                 *      {
                                 *          for (int i = 0; i < tokenStringBytes.Length; i++)
                                 *          {
                                 *              bytes[index + i] = tokenStringBytes[i];
                                 *          }
                                 *      }
                                 *  }
                                 * }
                                 */
                                #endregion complicated, found PublicKeyToken=null and need to extend space to sign
                            }
                        }
                        else
                        {
                            for (int i = 0; i < tokenStringBytes.Length; i++)
                            {
                                bytes[index + i] = tokenStringBytes[i];
                            }
                        }
                    }

                    index = BytesUtils.ByteSearch(bytes, searchBytes, index + searchTokenBytes.Length, bytes.Length - index - searchTokenBytes.Length);
                } //end if searchToken found
            }     //end if searchName found
        }
 public static string GetPublicKeyTokenString(byte[] token)
 {
     return(BytesUtils.BytesToHexString(token));
 }