Example #1
0
        private static void WriteHexEncodedValue(XmlWriter writer, RegValueEntryKind kind, IEnumerable <byte> bytes)
        {
            StringBuilder buffer = new StringBuilder();

            foreach (byte b in bytes)
            {
                buffer.Append(b.ToString("X2"));
            }
            writer.WriteValue(buffer.ToString());
        }
Example #2
0
        private static RegistryValueKind MapRegis3KindToNativeKind(RegValueEntryKind nativeKind)
        {
            switch (nativeKind)
            {
            case RegValueEntryKind.None:
                return(RegistryValueKind.None);

            case RegValueEntryKind.Unknown:
                return(RegistryValueKind.Unknown);

            default:
                return((RegistryValueKind)nativeKind);
            }
        }
 /// <summary>
 /// This constructor creates a named value from a Windows registry value
 /// </summary>
 /// <param name="key">Parent registry key</param>
 /// <param name="name">Name of the value</param>
 public RegValueEntry(RegistryKey key, string name)
 {
     Kind = MapNativeKindToRegis3Kind(key.GetValueKind(name));
     Value = key.GetValue(name, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
     Name = name;
     if (Value is string)
     {
         string temp = Value as string;
         if (temp.EndsWith("\0"))
         {
             Value = temp.Substring(0, temp.Length - 1);
         }
     }
     RemoveFlag = false;
 }
Example #4
0
 /// <summary>
 /// This constructor creates a named value from a Windows registry value
 /// </summary>
 /// <param name="key">Parent registry key</param>
 /// <param name="name">Name of the value</param>
 public RegValueEntry(RegistryKey key, string name)
 {
     Kind  = MapNativeKindToRegis3Kind(key.GetValueKind(name));
     Value = key.GetValue(name, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
     Name  = name;
     if (Value is string)
     {
         string temp = Value as string;
         if (temp.EndsWith("\0"))
         {
             Value = temp.Substring(0, temp.Length - 1);
         }
     }
     RemoveFlag = false;
 }
Example #5
0
 private void ExpectStartOfValueDefinition(char c)
 {
     if (c == '"')
     {
         ParserState = ExpectStringValueDefinition;
         Buffer.Clear();
     }
     else if (c == ':')
     {
         string typename = Buffer.ToString().ToLower();
         Buffer.Clear();
         if (typename.Equals("dword", StringComparison.OrdinalIgnoreCase))
         {
             ParserState = ExpectHexIntegerValue;
         }
         else if( typename.StartsWith("hex(") && typename.EndsWith(")") )
         {
             string[] tokens = typename.Split('(', ')');
             if (tokens.Length >= 2)
             {
                 int kindValueAsInt = 0;
                 if (!int.TryParse(tokens[1], NumberStyles.HexNumber, null, out kindValueAsInt))
                 {
                     throw SyntaxError("ERROR, '{0}' is not a valid hex() kind ", tokens[1]);
                 }
                 RegValueEntryKind kind = (RegValueEntryKind)kindValueAsInt;
                 Buffer.Clear();
                 CurrentDataKind = kind;
                 ParserState = ExpectStartOfMultiByteValueDefinition;
             }
         }
         else if (typename.Equals("hex"))
         {
             CurrentDataKind = RegValueEntryKind.Binary;
             ParserState = ExpectStartOfMultiByteValueDefinition;
         }
         else
         {
             throw SyntaxError("ERROR, value type '{0}' not supported", typename);
         }
     }
     else
     {
         Buffer.Append(c);
     }
 }
 /// <summary>
 /// The default constructor creates an unnamed value without contenet.
 /// </summary>
 public RegValueEntry()
 {
     Name = null;
     Kind = RegValueEntryKind.Unknown;
 }
 /// <summary>
 /// Define a string value
 /// </summary>
 /// <param name="value">content</param>
 public void SetStringValue(string value)
 {
     Kind = RegValueEntryKind.SZ;
     Value = value;
 }
Example #8
0
 /// <summary>
 /// Define a long value
 /// </summary>
 /// <param name="value">integer value</param>
 public void SetLongValue(long value)
 {
     Kind  = RegValueEntryKind.QWord;
     Value = value;
 }
Example #9
0
 /// <summary>
 /// Define an escaped integer value. If you're reading .REG files and you use the RegEnvReplace class to replace
 /// content with variables at runtime, you can specify something like this:
 ///
 /// "SomeValue"=qword:$$VARIABLE$$
 ///
 /// </summary>
 /// <param name="value">Name of the escaped int variable</param>
 public void SetEscapedLongValue(string value)
 {
     Kind  = RegValueEntryKind.QWord;
     Value = value;
 }
Example #10
0
 /// <summary>
 /// Define a multi-string value
 /// </summary>
 /// <param name="values">content</param>
 public void SetMultiStringValue(List <string> values)
 {
     Kind = RegValueEntryKind.MultiSZ;
     string[] stringArray = values.ToArray();
     Value = stringArray;
 }
Example #11
0
 /// <summary>
 /// Define a string value
 /// </summary>
 /// <param name="value">content</param>
 public void SetStringValue(string value)
 {
     Kind  = RegValueEntryKind.SZ;
     Value = value;
 }
 /// <summary>
 /// Associate 'None'-type with empty value
 /// </summary>
 public void SetNoneValue()
 {
     Value = null;
     Kind = RegValueEntryKind.None;
 }
 /// <summary>
 /// Define a long value
 /// </summary>
 /// <param name="value">integer value</param>
 public void SetLongValue(long value)
 {
     Kind = RegValueEntryKind.QWord;
     Value = value;
 }
 /// <summary>
 /// Define an integer value
 /// </summary>
 /// <param name="value">integer value</param>
 public void SetIntValue(int value)
 {
     Kind = RegValueEntryKind.DWord;
     Value = value;
 }
 /// <summary>
 /// Define an escaped integer value. If you're reading .REG files and you use the RegEnvReplace class to replace 
 /// content with variables at runtime, you can specify something like this:
 /// 
 /// "SomeValue"=qword:$$VARIABLE$$
 /// 
 /// </summary>
 /// <param name="value">Name of the escaped int variable</param>
 public void SetEscapedLongValue(string value)
 {
     Kind = RegValueEntryKind.QWord;
     Value = value;
 }
 /// <summary>
 /// Define an escaped integer value. If you're reading .REG files and you use the RegEnvReplace class to replace 
 /// content with variables at runtime, you can specify something like this:
 /// 
 /// "SomeValue"=dword:$$VARIABLE$$
 /// 
 /// </summary>
 /// <param name="value">Name of the escaped int variable</param>
 public void SetEscapedIntValue(string value)
 {
     Kind = RegValueEntryKind.DWord;
     Value = value;
 }
 /// <summary>
 /// Define a multi-string value
 /// </summary>
 /// <param name="values">content</param>
 public void SetMultiStringValue(List<string> values)
 {
     Kind = RegValueEntryKind.MultiSZ;
     string[] stringArray = values.ToArray();
     Value = stringArray;
 }
 /// <summary>
 /// Define a expanded string value
 /// </summary>
 /// <param name="value">content</param>
 public void SetExpandedStringValue(string value)
 {
     Kind = RegValueEntryKind.ExpandSZ;
     Value = value;
 }
Example #19
0
 /// <summary>
 /// The default constructor creates an unnamed value without contenet.
 /// </summary>
 public RegValueEntry()
 {
     Name = null;
     Kind = RegValueEntryKind.Unknown;
 }
Example #20
0
 /// <summary>
 /// Copy Constructor
 /// </summary>
 /// <param name="objectSrc"></param>
 public RegValueEntry(RegValueEntry objectSrc)
 {
     Name  = objectSrc.Name;
     Value = objectSrc.Value;
     Kind  = objectSrc.Kind;
 }
        /// <summary>
        /// Given hex-encoded binary data, set a blob type
        /// </summary>
        /// <param name="kind">Type of registry entry</param>
        /// <param name="bytes">Byte representation of the data</param>
        public void SetBinaryType(RegValueEntryKind kind, byte[] bytes)
        {
            Value = bytes;
            Kind = kind;

            if (Kind == RegValueEntryKind.ExpandSZ)
            {
                try
                {
                    string temp = Encoding.Unicode.GetString((byte[])Value);
                    while (temp.EndsWith("\0"))
                        temp = temp.Substring(0, temp.Length - 1);
                    Value = temp;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
            else if (Kind == RegValueEntryKind.MultiSZ)
            {
                try
                {
                    string temp = Encoding.Unicode.GetString(bytes);
                    while (temp.EndsWith("\0"))
                        temp = temp.Substring(0, temp.Length - 1);
                    Value = temp.Split('\0');
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
            else if (Kind == RegValueEntryKind.DWord)
            {
                try
                {
                    if (bytes.Length == 4)
                    {
                        Value = (int)BitConverter.ToInt32(bytes, 0);
                    }
                    else
                    {
                        Value = (int) BitConverter.ToInt64(bytes, 0);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
            else if (Kind == RegValueEntryKind.QWord)
            {
                try
                {
                    Value = BitConverter.ToInt64(bytes, 0);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
        }
Example #22
0
 /// <summary>
 /// Define a expanded string value
 /// </summary>
 /// <param name="value">content</param>
 public void SetExpandedStringValue(string value)
 {
     Kind  = RegValueEntryKind.ExpandSZ;
     Value = value;
 }
 /// <summary>
 /// This constructor creates a named value with unknown content
 /// </summary>
 /// <param name="name">Name of the value</param>
 public RegValueEntry(string name)
 {
     Name = name;
     Kind = RegValueEntryKind.Unknown;
     RemoveFlag = false;
 }
Example #24
0
 /// <summary>
 /// Define an escaped integer value. If you're reading .REG files and you use the RegEnvReplace class to replace
 /// content with variables at runtime, you can specify something like this:
 ///
 /// "SomeValue"=dword:$$VARIABLE$$
 ///
 /// </summary>
 /// <param name="value">Name of the escaped int variable</param>
 public void SetEscapedIntValue(string value)
 {
     Kind  = RegValueEntryKind.DWord;
     Value = value;
 }
Example #25
0
        /// <summary>
        /// Given hex-encoded binary data, set a blob type
        /// </summary>
        /// <param name="kind">Type of registry entry</param>
        /// <param name="bytes">Byte representation of the data</param>
        public void SetBinaryType(RegValueEntryKind kind, byte[] bytes)
        {
            Value = bytes;
            Kind  = kind;

            if (Kind == RegValueEntryKind.ExpandSZ)
            {
                try
                {
                    string temp = Encoding.Unicode.GetString((byte[])Value);
                    while (temp.EndsWith("\0"))
                    {
                        temp = temp.Substring(0, temp.Length - 1);
                    }
                    Value = temp;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
            else if (Kind == RegValueEntryKind.MultiSZ)
            {
                try
                {
                    string temp = Encoding.Unicode.GetString(bytes);
                    while (temp.EndsWith("\0"))
                    {
                        temp = temp.Substring(0, temp.Length - 1);
                    }
                    Value = temp.Split('\0');
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
            else if (Kind == RegValueEntryKind.DWord)
            {
                try
                {
                    if (bytes.Length == 4)
                    {
                        Value = (int)BitConverter.ToInt32(bytes, 0);
                    }
                    else if (bytes.Length == 8)
                    {
                        Value = (int)BitConverter.ToInt64(bytes, 0);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Note: Type of '{0}' is RegValueEntryKind.DWord, but bytes.Length is {1}", this, bytes.Length);
                    Console.WriteLine(e);
                    throw;
                }
            }
            else if (Kind == RegValueEntryKind.QWord)
            {
                try
                {
                    Value = BitConverter.ToInt64(bytes, 0);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
        }
Example #26
0
 /// <summary>
 /// Define an integer value
 /// </summary>
 /// <param name="value">integer value</param>
 public void SetIntValue(int value)
 {
     Kind  = RegValueEntryKind.DWord;
     Value = value;
 }
        private static RegistryValueKind MapRegis3KindToNativeKind(RegValueEntryKind nativeKind)
        {
            switch (nativeKind)
            {
                case RegValueEntryKind.None:
                    return RegistryValueKind.None;

                case RegValueEntryKind.Unknown:
                    return RegistryValueKind.Unknown;

                default:
                    return (RegistryValueKind)nativeKind;
            }
        }
Example #28
0
 /// <summary>
 /// Associate 'None'-type with empty value
 /// </summary>
 public void SetNoneValue()
 {
     Value = null;
     Kind  = RegValueEntryKind.None;
 }
 /// <summary>
 /// Copy Constructor
 /// </summary>
 /// <param name="objectSrc"></param>
 public RegValueEntry(RegValueEntry objectSrc)
 {
     Name = objectSrc.Name;
     Value = objectSrc.Value;
     Kind = objectSrc.Kind;
 }
Example #30
0
 /// <summary>
 /// This constructor creates a named value with unknown content
 /// </summary>
 /// <param name="name">Name of the value</param>
 public RegValueEntry(string name)
 {
     Name       = name;
     Kind       = RegValueEntryKind.Unknown;
     RemoveFlag = false;
 }
 private static void WriteHexEncodedValue(XmlWriter writer, RegValueEntryKind kind, IEnumerable<byte> bytes)
 {
     StringBuilder buffer = new StringBuilder();
     foreach (byte b in bytes)
     {
         buffer.Append(b.ToString("X2"));
     }
     writer.WriteValue(buffer.ToString());
 }
Example #32
0
        private void ParseXmlContent(string content)
        {
            RegValueEntry     CurrentValue   = null;
            RegKeyEntry       CurrentKey     = null;
            StringBuilder     CurrentContent = null;
            RegValueEntryKind CurrentKind    = RegValueEntryKind.Unknown;
            bool          isBase64Encoding   = false;
            List <string> currentStringList  = new List <string>();

            using (XmlReader reader = XmlReader.Create(new StringReader(content)))
            {
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                        if (reader.Name.Equals("registry"))
                        {
                            string version = reader.GetAttribute("version");
                            if (version == "2")
                            {
                                // ok, this version is supported
                            }
                            else
                            {
                                throw new System.Data.SyntaxErrorException("Unexpected XML format: must be using registry version 2.0 or higher");
                            }
                        }
                        else if (reader.Name.Equals("key"))
                        {
                            string name = reader.GetAttribute("name");
                            if (CurrentKey == null)
                            {
                                Trace.Assert(Result == null);
                                Result     = new RegKeyEntry(null, name);
                                CurrentKey = Result;
                            }
                            else
                            {
                                RegKeyEntry newKey = new RegKeyEntry(CurrentKey, name);
                                CurrentKey.Keys[newKey.Name.ToLower()] = newKey;
                                if (!reader.IsEmptyElement)
                                {
                                    CurrentKey = newKey;
                                }
                            }
                        }
                        else if ((CurrentKind == RegValueEntryKind.MultiSZ) && reader.Name.Equals("line"))
                        {
                            if (reader.IsEmptyElement)
                            {
                                currentStringList.Add("");
                            }
                            else
                            {
                                CurrentContent = new StringBuilder();

                                string encoding = reader.GetAttribute("encoding");
                                isBase64Encoding = (encoding != null) && encoding.Equals("base-64");
                            }
                        }
                        else
                        {
                            try
                            {
                                CurrentKind = (RegValueEntryKind)Enum.Parse(typeof(RegValueEntryKind), reader.Name);
                            }
                            catch (ArgumentException)
                            {
                                throw new System.Data.SyntaxErrorException(
                                          string.Format("ERROR, {0} is not a valid entry in a registry .XML file", reader.Name));
                            }
                            string name = reader.GetAttribute("name");
                            CurrentValue = new RegValueEntry(name);
                            if (name == null)
                            {
                                CurrentKey.DefaultValue = CurrentValue;
                            }
                            else
                            {
                                CurrentKey.Values[name.ToLower()] = CurrentValue;
                            }
                            if (reader.IsEmptyElement)
                            {
                                if (RegValueEntryKind.SZ == CurrentKind)
                                {
                                    CurrentValue.SetStringValue("");
                                }
                                else if (RegValueEntryKind.ExpandSZ == CurrentKind)
                                {
                                    CurrentValue.SetExpandedStringValue("");
                                }
                                else if (RegValueEntryKind.DWord == CurrentKind)
                                {
                                    CurrentValue.SetIntValue(0);
                                }
                                else if (RegValueEntryKind.QWord == CurrentKind)
                                {
                                    CurrentValue.SetLongValue(0);
                                }
                                else if (RegValueEntryKind.MultiSZ == CurrentKind)
                                {
                                    CurrentValue.SetMultiStringValue(new List <string>());
                                }
                                else
                                {
                                    CurrentValue.SetBinaryType(CurrentKind, new byte[] { });
                                }
                                CurrentValue = null;
                            }
                            else
                            {
                                CurrentContent = new StringBuilder();
                                string encoding = reader.GetAttribute("encoding");
                                isBase64Encoding = (encoding != null) && encoding.Equals("base-64");

                                if (CurrentKind == RegValueEntryKind.MultiSZ)
                                {
                                    currentStringList.Clear();
                                }
                                else
                                {
                                    CurrentContent = new StringBuilder();
                                }
                            }
                        }
                        break;

                    case XmlNodeType.Text:
                        if (CurrentContent != null)
                        {
                            CurrentContent.Append(reader.Value);
                        }
                        break;

                    case XmlNodeType.EndElement:
                        if (reader.Name.Equals("key"))
                        {
                            Trace.Assert(CurrentKey != null);
                            CurrentKey = CurrentKey.Parent;
                        }
                        else if ((CurrentKind == RegValueEntryKind.MultiSZ) && reader.Name.Equals("line"))
                        {
                            if (isBase64Encoding)
                            {
                                byte[] bytes = Convert.FromBase64String(CurrentContent.ToString());
                                currentStringList.Add(System.Text.Encoding.Unicode.GetString(bytes));
                            }
                            else
                            {
                                currentStringList.Add(CurrentContent.ToString());
                            }
                        }
                        else if (reader.Name.Equals("registry"))
                        {
                        }
                        else if (reader.Name.Equals(CurrentKind.ToString()))
                        {
                            if (RegValueEntryKind.SZ == CurrentKind)
                            {
                                if (isBase64Encoding)
                                {
                                    byte[] bytes = Convert.FromBase64String(CurrentContent.ToString());
                                    CurrentValue.SetStringValue(System.Text.Encoding.Unicode.GetString(bytes));
                                }
                                else
                                {
                                    CurrentValue.SetStringValue(CurrentContent.ToString());
                                }
                            }
                            else if (RegValueEntryKind.ExpandSZ == CurrentKind)
                            {
                                if (isBase64Encoding)
                                {
                                    byte[] bytes = Convert.FromBase64String(CurrentContent.ToString());
                                    CurrentValue.SetExpandedStringValue(System.Text.Encoding.Unicode.GetString(bytes));
                                }
                                else
                                {
                                    CurrentValue.SetExpandedStringValue(CurrentContent.ToString());
                                }
                            }
                            else if (RegValueEntryKind.DWord == CurrentKind)
                            {
                                string temp = CurrentContent.ToString();
                                if (temp.Contains("$$"))
                                {
                                    CurrentValue.SetEscapedIntValue(temp);
                                }
                                else
                                {
                                    CurrentValue.SetIntValue(int.Parse(temp));
                                }
                            }
                            else if (RegValueEntryKind.QWord == CurrentKind)
                            {
                                string temp = CurrentContent.ToString();
                                if (temp.Contains("$$"))
                                {
                                    CurrentValue.SetEscapedLongValue(temp);
                                }
                                else
                                {
                                    CurrentValue.SetLongValue(long.Parse(temp));
                                }
                            }
                            else if (RegValueEntryKind.MultiSZ == CurrentKind)
                            {
                                CurrentValue.SetMultiStringValue(currentStringList);
                                currentStringList.Clear();
                            }
                            else
                            {
                                CurrentValue.SetBinaryType(CurrentKind, DecodeHexByteArray(CurrentContent.ToString()));
                            }
                            CurrentValue = null;
                        }
                        break;
                    }
                }
            }
        }
 private void ExpectStartOfValueDefinition(char c)
 {
     if (c == '"')
     {
         ParserState = ExpectStringValueDefinition;
         Buffer.Clear();
     }
     else if (c == ':')
     {
         string typename = Buffer.ToString().ToLower();
         Buffer.Clear();
         if (typename.Equals("dword", StringComparison.OrdinalIgnoreCase))
         {
             ParserState = ExpectHexIntegerValue;
         }
         else if( typename.StartsWith("hex(") && typename.EndsWith(")") )
         {
             string[] tokens = typename.Split('(', ')');
             if (tokens.Length >= 2)
             {
                 int kindValueAsInt = 0;
                 if (!int.TryParse(tokens[1], NumberStyles.HexNumber, null, out kindValueAsInt))
                 {
                     throw SyntaxError("ERROR, '{0}' is not a valid hex() kind ", tokens[1]);
                 }
                 RegValueEntryKind kind = (RegValueEntryKind)kindValueAsInt;
                 Buffer.Clear();
                 CurrentDataKind = kind;
                 ParserState = ExpectStartOfMultiByteValueDefinition;
             }
         }
         else if (typename.Equals("hex"))
         {
             CurrentDataKind = RegValueEntryKind.Binary;
             ParserState = ExpectStartOfMultiByteValueDefinition;
         }
         else
         {
             throw SyntaxError("ERROR, value type '{0}' not supported", typename);
         }
     }
     else
     {
         Buffer.Append(c);
     }
 }